1 /*********************************************************************************************************//**
2  * @file    ht32f5xxxx_i2c.c
3  * @version $Rev:: 6398         $
4  * @date    $Date:: 2022-10-27 #$
5  * @brief   This file provides all the I2C firmware functions.
6  *************************************************************************************************************
7  * @attention
8  *
9  * Firmware Disclaimer Information
10  *
11  * 1. The customer hereby acknowledges and agrees that the program technical documentation, including the
12  *    code, which is supplied by Holtek Semiconductor Inc., (hereinafter referred to as "HOLTEK") is the
13  *    proprietary and confidential intellectual property of HOLTEK, and is protected by copyright law and
14  *    other intellectual property laws.
15  *
16  * 2. The customer hereby acknowledges and agrees that the program technical documentation, including the
17  *    code, is confidential information belonging to HOLTEK, and must not be disclosed to any third parties
18  *    other than HOLTEK and the customer.
19  *
20  * 3. The program technical documentation, including the code, is provided "as is" and for customer reference
21  *    only. After delivery by HOLTEK, the customer shall use the program technical documentation, including
22  *    the code, at their own risk. HOLTEK disclaims any expressed, implied or statutory warranties, including
23  *    the warranties of merchantability, satisfactory quality and fitness for a particular purpose.
24  *
25  * <h2><center>Copyright (C) Holtek Semiconductor Inc. All rights reserved</center></h2>
26  ************************************************************************************************************/
27 
28 /* Includes ------------------------------------------------------------------------------------------------*/
29 #include "ht32f5xxxx_i2c.h"
30 
31 /** @addtogroup HT32F5xxxx_Peripheral_Driver HT32F5xxxx Peripheral Driver
32   * @{
33   */
34 
35 /** @defgroup I2C I2C
36   * @brief I2C driver modules
37   * @{
38   */
39 
40 
41 /* Private constants ---------------------------------------------------------------------------------------*/
42 /** @defgroup I2C_Private_Define I2C private definitions
43   * @{
44   */
45 /* I2C ENI2C mask                                                                                           */
46 #define CR_ENI2C_SET             ((u32)0x00000008)
47 #define CR_ENI2C_RESET           ((u32)0xFFFFFFF7)
48 
49 /* I2C ENGC mask                                                                                            */
50 #define CR_ENGC_SET              ((u32)0x00000004)
51 #define CR_ENGC_RESET            ((u32)0xFFFFFFFB)
52 
53 /* I2C AA mask                                                                                              */
54 #define CR_ACK_SET               ((u32)0x00000001)
55 #define CR_ACK_RESET             ((u32)0xFFFFFFFE)
56 
57 /* I2C PDMANACK mask                                                                                        */
58 #define CR_PDMANACK_SET          ((u32)0x00000400)
59 #define CR_PDMANACK_RESET        ((u32)0xFFFFFBFF)
60 
61 /* I2C ENTOUT mask                                                                                          */
62 #define CR_ENTOUT_SET            ((u32)0x00001000)
63 #define CR_ENTOUT_RESET          ((u32)0xFFFFEFFF)
64 
65 /* I2C COMBFILT mask                                                                                        */
66 #define CR_COMBFILTER_SET        ((u32)0x00002000)
67 #define CR_COMBFILTER_RESET      ((u32)0xFFFFDFFF)
68 
69 /* I2C Device Address 0 and Device Address 1 mask                                                           */
70 #define ADDR_DEVADDR0_SET        ((u32)0x00008000)
71 #define ADDR_DEVADDR0_RESET      ((u32)0xFFFF7FFF)
72 #define ADDR_DEVADDR1_SET        ((u32)0x80000000)
73 #define ADDR_DEVADDR1_RESET      ((u32)0x7FFFFFFF)
74 
75 /**
76   * @}
77   */
78 
79 /* Global functions ----------------------------------------------------------------------------------------*/
80 /** @defgroup I2C_Exported_Functions I2C exported functions
81   * @{
82   */
83 /*********************************************************************************************************//**
84  * @brief Deinitialize the I2C peripheral registers to their default reset values.
85  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
86  * @retval None
87  ************************************************************************************************************/
I2C_DeInit(HT_I2C_TypeDef * I2Cx)88 void I2C_DeInit(HT_I2C_TypeDef* I2Cx)
89 {
90   RSTCU_PeripReset_TypeDef RSTCUReset = {{0}};
91 
92   /* Check the parameters                                                                                   */
93   Assert_Param(IS_I2C(I2Cx));
94 
95   if (I2Cx == HT_I2C0)
96   {
97     RSTCUReset.Bit.I2C0 = 1;
98   }
99   #if (LIBCFG_I2C1)
100   else if (I2Cx == HT_I2C1)
101   {
102     RSTCUReset.Bit.I2C1 = 1;
103   }
104   #endif
105   #if (LIBCFG_I2C2)
106   else if (I2Cx == HT_I2C2)
107   {
108     RSTCUReset.Bit.I2C2 = 1;
109   }
110   #endif
111 
112   RSTCU_PeripReset(RSTCUReset, ENABLE);
113 }
114 
115 /*********************************************************************************************************//**
116  * @brief Initialize the I2Cx peripheral according to the specified parameters in the I2C_InitStruct.
117  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
118  * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure.
119  * @retval None
120  ************************************************************************************************************/
I2C_Init(HT_I2C_TypeDef * I2Cx,I2C_InitTypeDef * I2C_InitStruct)121 void I2C_Init(HT_I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
122 {
123   u32 PCLK_Freq = 0;
124   s32 sTmp = 0;
125   s32 SHPGR = 0;
126   s32 SLPGR = 0;
127 
128   /* Check the parameters                                                                                   */
129   Assert_Param(IS_I2C(I2Cx));
130   Assert_Param(IS_I2C_GENERAL_CALL(I2C_InitStruct->I2C_GeneralCall));
131   Assert_Param(IS_I2C_ACKNOWLEDGE_ADDRESS(I2C_InitStruct->I2C_AddressingMode));
132   Assert_Param(IS_I2C_ACKNOWLEDGE(I2C_InitStruct->I2C_Acknowledge));
133   Assert_Param(IS_I2C_ADDRESS(I2C_InitStruct->I2C_OwnAddress));
134   Assert_Param(IS_I2C_SPEED(I2C_InitStruct->I2C_Speed));
135 
136   #if (LIBCFG_I2C_NO_10BIT_MODE)
137   I2Cx->CR = (I2Cx->CR & 0xFFFFFFFA) | I2C_InitStruct->I2C_GeneralCall | I2C_InitStruct->I2C_Acknowledge;
138   #else
139   I2Cx->CR = (I2Cx->CR & 0xFFFFFF7A) | I2C_InitStruct->I2C_GeneralCall |
140              I2C_InitStruct->I2C_AddressingMode | I2C_InitStruct->I2C_Acknowledge;
141   #endif
142 
143   #if (LIBCFG_I2C_TWO_DEV_ADDR)
144   I2Cx->ADDR = (I2Cx->ADDR & 0xFFFF0000) | (ADDR_DEVADDR0_SET | (I2C_InitStruct->I2C_OwnAddress));
145   #else
146   I2Cx->ADDR = I2C_InitStruct->I2C_OwnAddress;
147   #endif
148 
149   if (I2Cx == HT_I2C0)
150     PCLK_Freq = CKCU_GetPeripFrequency(CKCU_PCLK_I2C0);
151   #if (LIBCFG_I2C1)
152   else if (I2Cx == HT_I2C1)
153     PCLK_Freq = CKCU_GetPeripFrequency(CKCU_PCLK_I2C1);
154   #endif
155   #if (LIBCFG_I2C2)
156   else if (I2Cx == HT_I2C2)
157     PCLK_Freq = CKCU_GetPeripFrequency(CKCU_PCLK_I2C2);
158   #endif
159 
160   switch (I2Cx->CR & 0xC000)
161   {
162     case 0:
163     {
164       sTmp = 6;
165       break;
166     }
167     case 0x4000:
168     {
169       sTmp = 8;
170       break;
171     }
172     case 0x8000:
173     {
174       sTmp = 9;
175       break;
176     }
177   }
178 
179   SHPGR = (PCLK_Freq * 9)/(I2C_InitStruct->I2C_Speed * 20) - sTmp - I2C_InitStruct->I2C_SpeedOffset;
180   SLPGR = (PCLK_Freq * 11)/(I2C_InitStruct->I2C_Speed * 20) - sTmp - I2C_InitStruct->I2C_SpeedOffset;
181 
182   SHPGR = (SHPGR < 0) ? 0 : SHPGR;
183   SLPGR = (SLPGR < 0) ? 0 : SLPGR;
184 
185   I2Cx->SHPGR = SHPGR;
186   I2Cx->SLPGR = SLPGR;
187 }
188 
189 /*********************************************************************************************************//**
190  * @brief Fill each I2C_InitStruct member with its default value.
191  * @param I2C_InitStruct: pointer to an I2C_InitTypeDef structure.
192  * @retval None
193  ************************************************************************************************************/
I2C_StructInit(I2C_InitTypeDef * I2C_InitStruct)194 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
195 {
196   I2C_InitStruct->I2C_GeneralCall = I2C_GENERALCALL_DISABLE;
197   I2C_InitStruct->I2C_AddressingMode = I2C_ADDRESSING_7BIT;
198   I2C_InitStruct->I2C_Acknowledge = I2C_ACK_DISABLE;
199   I2C_InitStruct->I2C_OwnAddress = 0;
200   I2C_InitStruct->I2C_Speed = 1000000;
201   I2C_InitStruct->I2C_SpeedOffset = 0;
202 }
203 
204 /*********************************************************************************************************//**
205  * @brief Enable or Disable the specified I2C peripheral.
206  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
207  * @param NewState: This parameter can be ENABLE or DISABLE.
208  * @retval None
209  ************************************************************************************************************/
I2C_Cmd(HT_I2C_TypeDef * I2Cx,ControlStatus NewState)210 void I2C_Cmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState)
211 {
212   /* Check the parameters                                                                                   */
213   Assert_Param(IS_I2C(I2Cx));
214   Assert_Param(IS_CONTROL_STATUS(NewState));
215 
216   if (NewState != DISABLE)
217   {
218     I2Cx->CR |= CR_ENI2C_SET;
219   }
220   else
221   {
222     I2Cx->CR &= CR_ENI2C_RESET;
223   }
224 }
225 
226 /*********************************************************************************************************//**
227  * @brief Generate STOP condition of I2C communication.
228  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
229  * @retval None
230  ************************************************************************************************************/
I2C_GenerateSTOP(HT_I2C_TypeDef * I2Cx)231 void I2C_GenerateSTOP(HT_I2C_TypeDef* I2Cx)
232 {
233   /* Check the parameters                                                                                   */
234   Assert_Param(IS_I2C(I2Cx));
235 
236   I2Cx->CR |= 0x2;
237 }
238 
239 /*********************************************************************************************************//**
240  * @brief Enable or Disable I2C interrupts.
241  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
242  * @param I2C_Int: specify if the I2C interrupt source to be enabled or disabled.
243  *   This parameter can be any combination of the following values:
244  *     @arg I2C_INT_STA
245  *     @arg I2C_INT_STO
246  *     @arg I2C_INT_ADRS
247  *     @arg I2C_INT_GCS
248  *     @arg I2C_INT_ARBLOS
249  *     @arg I2C_INT_RXNACK
250  *     @arg I2C_INT_BUSERR
251  *     @arg I2C_INT_TOUT
252  *     @arg I2C_INT_RXDNE
253  *     @arg I2C_INT_TXDE
254  *     @arg I2C_INT_RXBF
255  *     @arg I2C_INT_ALL
256  * @param NewState: This parameter can be ENABLE or DISABLE.
257  * @retval None
258  ************************************************************************************************************/
I2C_IntConfig(HT_I2C_TypeDef * I2Cx,u32 I2C_Int,ControlStatus NewState)259 void I2C_IntConfig(HT_I2C_TypeDef* I2Cx, u32 I2C_Int, ControlStatus NewState)
260 {
261   /* Check the parameters                                                                                   */
262   Assert_Param(IS_I2C(I2Cx));
263   Assert_Param(IS_I2C_INT(I2C_Int));
264   Assert_Param(IS_CONTROL_STATUS(NewState));
265 
266   if (NewState != DISABLE)
267   {
268     I2Cx->IER |= I2C_Int;
269   }
270    else
271   {
272     I2Cx->IER &= (u32)~I2C_Int;
273   }
274 }
275 
276 /*********************************************************************************************************//**
277  * @brief Enable or Disable I2C General Call.
278  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
279  * @param NewState: This parameter can be ENABLE or DISABLE.
280  * @retval None
281  ************************************************************************************************************/
I2C_GeneralCallCmd(HT_I2C_TypeDef * I2Cx,ControlStatus NewState)282 void I2C_GeneralCallCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState)
283 {
284   /* Check the parameters                                                                                   */
285   Assert_Param(IS_I2C(I2Cx));
286   Assert_Param(IS_CONTROL_STATUS(NewState));
287 
288   if (NewState != DISABLE)
289   {
290     I2Cx->CR |= CR_ENGC_SET;
291   }
292   else
293   {
294     I2Cx->CR &= CR_ENGC_RESET;
295   }
296 }
297 
298 /*********************************************************************************************************//**
299  * @brief Enable or Disable I2C sending acknowledgement.
300  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
301  * @param NewState: This parameter can be ENABLE or DISABLE.
302  * @retval None
303  ************************************************************************************************************/
I2C_AckCmd(HT_I2C_TypeDef * I2Cx,ControlStatus NewState)304 void I2C_AckCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState)
305 {
306   /* Check the parameters                                                                                   */
307   Assert_Param(IS_I2C(I2Cx));
308   Assert_Param(IS_CONTROL_STATUS(NewState));
309 
310   if (NewState != DISABLE)
311   {
312     I2Cx->CR |= CR_ACK_SET;
313   }
314   else
315   {
316     I2Cx->CR &= CR_ACK_RESET;
317   }
318 }
319 
320 /*********************************************************************************************************//**
321  * @brief Configure own address of the specified I2C.
322  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
323  * @param I2C_Address: specify own address of I2C.
324  * @retval None
325  ************************************************************************************************************/
I2C_SetOwnAddress(HT_I2C_TypeDef * I2Cx,I2C_AddressTypeDef I2C_Address)326 void I2C_SetOwnAddress(HT_I2C_TypeDef* I2Cx, I2C_AddressTypeDef I2C_Address)
327 {
328   /* Check the parameters                                                                                   */
329   Assert_Param(IS_I2C(I2Cx));
330   Assert_Param(IS_I2C_ADDRESS(I2C_Address));
331 
332   #if (LIBCFG_I2C_TWO_DEV_ADDR)
333   I2Cx->ADDR = (I2Cx->ADDR & 0xFFFFFF80) | (ADDR_DEVADDR0_SET | I2C_Address);
334   #else
335   I2Cx->ADDR = I2C_Address;
336   #endif
337 }
338 
339 #if (LIBCFG_I2C_TWO_DEV_ADDR)
340 /*********************************************************************************************************//**
341  * @brief Configure own address 1 of the specified I2C.
342  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
343  * @param I2C_Address: specify own address 1 of I2C.
344  * @retval None
345  ************************************************************************************************************/
I2C_SetOwnAddress1(HT_I2C_TypeDef * I2Cx,I2C_AddressTypeDef I2C_Address)346 void I2C_SetOwnAddress1(HT_I2C_TypeDef* I2Cx, I2C_AddressTypeDef I2C_Address)
347 {
348   /* Check the parameters                                                                                   */
349   Assert_Param(IS_I2C(I2Cx));
350   Assert_Param(IS_I2C_ADDRESS(I2C_Address));
351 
352   I2Cx->ADDR = (I2Cx->ADDR & 0xFF80FFFF) | (ADDR_DEVADDR1_SET | (I2C_Address << 16));
353 }
354 
355 /*********************************************************************************************************//**
356  * @brief Enable or Disable I2C own address.
357  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
358  * @param Address: specify the ADDR number.
359  *   This parameter can be one of the following values:
360  *     @arg I2C_DEV_ADDR_0 :
361  *     @arg I2C_DEV_ADDR_1 :
362  * @param NewState: This parameter can be ENABLE or DISABLE.
363  * @retval None
364  ************************************************************************************************************/
I2C_OwnAddressCmd(HT_I2C_TypeDef * I2Cx,I2C_ADDR_Enum Address,ControlStatus NewState)365 void I2C_OwnAddressCmd(HT_I2C_TypeDef* I2Cx, I2C_ADDR_Enum Address, ControlStatus NewState)
366 {
367   u32 value = ADDR_DEVADDR0_SET;
368 
369   /* Check the parameters                                                                                   */
370   Assert_Param(IS_I2C(I2Cx));
371   Assert_Param(IS_I2C_ADDR(Address));
372   Assert_Param(IS_CONTROL_STATUS(NewState));
373 
374   if (Address == I2C_DEV_ADDR_1)
375   {
376     value = value << 16;
377   }
378 
379   if (NewState != DISABLE)
380   {
381     I2Cx->ADDR |= value;
382   }
383   else
384   {
385     I2Cx->ADDR &= (~value);
386   }
387 }
388 #endif
389 
390 /*********************************************************************************************************//**
391  * @brief Start transmitting to target slave address.
392  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
393  * @param I2C_Address: specify the slave address which will be transmitted.
394  * @param I2C_Direction: This parameter can be I2C_MASTER_READ or I2C_MASTER_WRITE.
395  * @retval None
396  ************************************************************************************************************/
I2C_TargetAddressConfig(HT_I2C_TypeDef * I2Cx,I2C_AddressTypeDef I2C_Address,u32 I2C_Direction)397 void I2C_TargetAddressConfig(HT_I2C_TypeDef* I2Cx, I2C_AddressTypeDef I2C_Address, u32 I2C_Direction)
398 {
399   /* Check the parameters                                                                                   */
400   Assert_Param(IS_I2C(I2Cx));
401   Assert_Param(IS_I2C_ADDRESS(I2C_Address));
402   Assert_Param(IS_I2C_DIRECTION(I2C_Direction));
403 
404   /* Make sure the prior stop command has been finished                                                     */
405   while (I2Cx->CR & 0x2);
406 
407   if (I2C_Direction != I2C_MASTER_WRITE)
408   {
409     I2Cx->TAR = I2C_Address | I2C_MASTER_READ;
410   }
411    else
412   {
413     I2Cx->TAR = I2C_Address | I2C_MASTER_WRITE;
414   }
415 }
416 
417 /*********************************************************************************************************//**
418  * @brief Send a data word through the I2Cx peripheral.
419  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
420  * @param I2C_Data: Byte to be transmitted.
421  * @retval None
422  ************************************************************************************************************/
I2C_SendData(HT_I2C_TypeDef * I2Cx,u8 I2C_Data)423 void I2C_SendData(HT_I2C_TypeDef* I2Cx, u8 I2C_Data)
424 {
425   /* Check the parameters                                                                                   */
426   Assert_Param(IS_I2C(I2Cx));
427 
428   I2Cx->DR = I2C_Data;
429 }
430 
431 /*********************************************************************************************************//**
432  * @brief Return the received data by the I2Cx peripheral.
433  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
434  * @retval The value of the received data.
435  ************************************************************************************************************/
I2C_ReceiveData(HT_I2C_TypeDef * I2Cx)436 u8 I2C_ReceiveData(HT_I2C_TypeDef* I2Cx)
437 {
438   /* Check the parameters                                                                                   */
439   Assert_Param(IS_I2C(I2Cx));
440 
441   return (u8)I2Cx->DR;
442 }
443 
444 /*********************************************************************************************************//**
445  * @brief Read the specified I2C register and returns its value.
446  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
447  * @param I2C_Register: specify the register to read.
448  *   This parameter can be one of the following values:
449  *     @arg I2C_REGISTER_CR    : Control Register
450  *     @arg I2C_REGISTER_IER   : Interrupt Enable Register
451  *     @arg I2C_REGISTER_ADDR  : Address Register
452  *     @arg I2C_REGISTER_SR    : Status Register
453  *     @arg I2C_REGISTER_SHPGR : SCL High Period Generation Register
454  *     @arg I2C_REGISTER_SLPGR : SCL Low Period Generation Register
455  *     @arg I2C_REGISTER_DR    : Data Register
456  *     @arg I2C_REGISTER_TAR   : Target Register
457  * @retval None
458  ************************************************************************************************************/
I2C_ReadRegister(HT_I2C_TypeDef * I2Cx,u8 I2C_Register)459 u32 I2C_ReadRegister(HT_I2C_TypeDef* I2Cx, u8 I2C_Register)
460 {
461   vu32 tmp = 0;
462 
463   /* Check the parameters                                                                                   */
464   Assert_Param(IS_I2C(I2Cx));
465   Assert_Param(IS_I2C_REGISTER(I2C_Register));
466 
467   tmp = (u32)I2Cx;
468   tmp += I2C_Register;
469   return (*(u32 *)tmp);
470 }
471 
472 /*********************************************************************************************************//**
473  * @brief Check whether the specified I2C flag has been set.
474  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
475  * @param I2C_Flag: specify the flag to be check.
476  *   This parameter can be one of the following values:
477  *     @arg I2C_FLAG_STA     : I2C start condition transmitted flag (Master mode)
478  *     @arg I2C_FLAG_STO     : I2C stop condition detected flag (Slave flag)
479  *     @arg I2C_FLAG_ADRS    : I2C address flag
480  *     @arg I2C_FLAG_GCS     : I2C general call flag (Slave mode)
481  *     @arg I2C_FLAG_ARBLOS  : I2C arbitration loss flag (Master mode)
482  *     @arg I2C_FLAG_RXNACK  : I2C received not acknowledge flag
483  *     @arg I2C_FLAG_BUSERR  : I2C bus error flag
484  *     @arg I2C_FLAG_RXDNE   : I2C Rx data not empty flag
485  *     @arg I2C_FLAG_TXDE    : I2C Tx data empty flag
486  *     @arg I2C_FLAG_RXBF    : I2C RX buffer full flag
487  *     @arg I2C_FLAG_BUSBUSY : I2C bus busy flag
488  *     @arg I2C_FLAG_MASTER  : I2C master mode flag (Master flag)
489  *     @arg I2C_FLAG_TXNRX   : I2C transmitter mode flag
490  * @retval SET or RESET
491  ************************************************************************************************************/
I2C_GetFlagStatus(HT_I2C_TypeDef * I2Cx,u32 I2C_Flag)492 FlagStatus I2C_GetFlagStatus(HT_I2C_TypeDef* I2Cx, u32 I2C_Flag)
493 {
494   /* Check the parameters                                                                                   */
495   Assert_Param(IS_I2C(I2Cx));
496   Assert_Param(IS_I2C_FLAG(I2C_Flag));
497 
498   if ((I2Cx->SR & I2C_Flag) != (u32)RESET)
499   {
500     return (SET);
501   }
502   else
503   {
504     return (RESET);
505   }
506 }
507 
508 /*********************************************************************************************************//**
509  * @brief Check whether the specified I2C status has been active.
510  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
511  * @param I2C_Status: specify the flag that is to be check.
512  *   This parameter can be one of the following values:
513  *     @arg I2C_MASTER_SEND_START
514  *     @arg I2C_MASTER_RECEIVER_MODE
515  *     @arg I2C_MASTER_TRANSMITTER_MODE
516  *     @arg I2C_MASTER_RX_NOT_EMPTY
517  *     @arg I2C_MASTER_RX_NOT_EMPTY_NOBUSY
518  *     @arg I2C_MASTER_TX_EMPTY
519  *     @arg I2C_MASTER_RX_BUFFER_FULL
520  *     @arg I2C_SLAVE_ACK_TRANSMITTER_ADDRESS
521  *     @arg I2C_SLAVE_ACK_RECEIVER_ADDRESS
522  *     @arg I2C_SLAVE_ACK_GCALL_ADDRESS
523  *     @arg I2C_SLAVE_RX_NOT_EMPTY
524  *     @arg I2C_SLAVE_RX_NOT_EMPTY_STOP
525  *     @arg I2C_SLAVE_TX_EMPTY
526  *     @arg I2C_SLAVE_RX_BUFFER_FULL
527  *     @arg I2C_SLAVE_RECEIVED_NACK
528  *     @arg I2C_SLAVE_RECEIVED_NACK_STOP
529  *     @arg I2C_SLAVE_STOP_DETECTED
530  * @retval SUCCESS or ERROR
531  ************************************************************************************************************/
I2C_CheckStatus(HT_I2C_TypeDef * I2Cx,u32 I2C_Status)532 ErrStatus I2C_CheckStatus(HT_I2C_TypeDef* I2Cx, u32 I2C_Status)
533 {
534   /* Check the parameters                                                                                   */
535   Assert_Param(IS_I2C(I2Cx));
536   Assert_Param(IS_I2C_STATUS(I2C_Status));
537 
538   if (I2Cx->SR == I2C_Status)
539   {
540     return (SUCCESS);
541   }
542   else
543   {
544     return (ERROR);
545   }
546 }
547 
548 /*********************************************************************************************************//**
549  * @brief Clear the specified I2C flag.
550  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
551  * @param I2C_Flag: specify the flag that is to be cleared.
552  *   This parameter can be one of the following values:
553  *     @arg I2C_FLAG_ARBLOS : I2C arbitration flag
554  *     @arg I2C_FLAG_RXNACK : I2C receive not acknowledge flag
555  *     @arg I2C_FLAG_BUSERR : I2C Bus error flag
556  * @retval None
557  ************************************************************************************************************/
I2C_ClearFlag(HT_I2C_TypeDef * I2Cx,u32 I2C_Flag)558 void I2C_ClearFlag(HT_I2C_TypeDef* I2Cx, u32 I2C_Flag)
559 {
560   /* Check the parameters                                                                                   */
561   Assert_Param(IS_I2C(I2Cx));
562   Assert_Param(IS_I2C_CLEAR_FLAG(I2C_Flag));
563 
564   I2Cx->SR = I2C_Flag;
565 }
566 
567 /*********************************************************************************************************//**
568  * @brief Set the interval timing of the high period of the I2C clock.
569  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
570  * @param I2C_HighPeriod: specify the high period that is to be set.
571  *   This parameter must be a number between 0 and 0xFFFF.
572  * @retval None
573  ************************************************************************************************************/
I2C_SetSCLHighPeriod(HT_I2C_TypeDef * I2Cx,u32 I2C_HighPeriod)574 void I2C_SetSCLHighPeriod(HT_I2C_TypeDef* I2Cx, u32 I2C_HighPeriod)
575 {
576   /* Check the parameters                                                                                   */
577   Assert_Param(IS_I2C(I2Cx));
578   Assert_Param(IS_I2C_SCL_HIGH(I2C_HighPeriod));
579 
580   I2Cx->SHPGR = I2C_HighPeriod;
581 }
582 
583 /*********************************************************************************************************//**
584  * @brief Set the interval timing of low period of the I2C clock.
585  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
586  * @param I2C_LowPeriod: specify the low period that is to be set.
587  *   This parameter must be a number between 0 and 0xFFFF.
588  * @retval None
589  ************************************************************************************************************/
I2C_SetSCLLowPeriod(HT_I2C_TypeDef * I2Cx,u32 I2C_LowPeriod)590 void I2C_SetSCLLowPeriod(HT_I2C_TypeDef* I2Cx, u32 I2C_LowPeriod)
591 {
592   /* Check the parameters                                                                                   */
593   Assert_Param(IS_I2C(I2Cx));
594   Assert_Param(IS_I2C_SCL_LOW(I2C_LowPeriod));
595 
596   I2Cx->SLPGR = I2C_LowPeriod;
597 }
598 
599 #if (LIBCFG_PDMA)
600 /*********************************************************************************************************//**
601  * @brief Enable or Disable the I2Cx PDMA interface.
602  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
603  * @param I2C_PDMAREQ: specify the I2C PDMA transfer request to be enabled or disabled.
604  *   This parameter can be any combination of the following values:
605  *     @arg I2C_PDMAREQ_TX: Tx PDMA transfer request
606  *     @arg I2C_PDMAREQ_RX: Rx PDMA transfer request
607  * @param NewState: This parameter can be ENABLE or DISABLE.
608  * @retval None
609  ************************************************************************************************************/
I2C_PDMACmd(HT_I2C_TypeDef * I2Cx,u32 I2C_PDMAREQ,ControlStatus NewState)610 void I2C_PDMACmd(HT_I2C_TypeDef* I2Cx, u32 I2C_PDMAREQ, ControlStatus NewState)
611 {
612   /* Check the parameters                                                                                   */
613   Assert_Param(IS_I2C(I2Cx));
614   Assert_Param(IS_I2C_PDMA_REQ(I2C_PDMAREQ));
615   Assert_Param(IS_CONTROL_STATUS(NewState));
616 
617   if (NewState != DISABLE)
618   {
619     I2Cx->CR |= I2C_PDMAREQ;
620   }
621   else
622   {
623     I2Cx->CR &= ~I2C_PDMAREQ;
624   }
625 }
626 
627 /*********************************************************************************************************//**
628  * @brief Specify that the next PDMA transfer is the last one.
629  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
630  * @param NewState: This parameter can be ENABLE or DISABLE.
631  * @retval None
632  ************************************************************************************************************/
I2C_PDMANACKCmd(HT_I2C_TypeDef * I2Cx,ControlStatus NewState)633 void I2C_PDMANACKCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState)
634 {
635   /* Check the parameters                                                                                   */
636   Assert_Param(IS_I2C(I2Cx));
637   Assert_Param(IS_CONTROL_STATUS(NewState));
638 
639   if (NewState != DISABLE)
640   {
641     I2Cx->CR |= CR_PDMANACK_SET;
642   }
643   else
644   {
645     I2Cx->CR &= CR_PDMANACK_RESET;
646   }
647 }
648 #endif
649 
650 /*********************************************************************************************************//**
651  * @brief Enable or Disable the specified I2C time out function.
652  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
653  * @param NewState: This parameter can be ENABLE or DISABLE.
654  * @retval None
655  ************************************************************************************************************/
I2C_TimeOutCmd(HT_I2C_TypeDef * I2Cx,ControlStatus NewState)656 void I2C_TimeOutCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState)
657 {
658   /* Check the parameters                                                                                   */
659   Assert_Param(IS_I2C(I2Cx));
660   Assert_Param(IS_CONTROL_STATUS(NewState));
661 
662   if (NewState != DISABLE)
663   {
664     I2Cx->CR |= CR_ENTOUT_SET;
665   }
666   else
667   {
668     I2Cx->CR &= CR_ENTOUT_RESET;
669   }
670 }
671 
672 /*********************************************************************************************************//**
673  * @brief This function is used to set the I2C timeout value.
674  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
675  * @param I2C_Timeout: specify the timeout value.
676  * @retval None
677  ************************************************************************************************************/
I2C_SetTimeOutValue(HT_I2C_TypeDef * I2Cx,u32 I2C_Timeout)678 void I2C_SetTimeOutValue(HT_I2C_TypeDef* I2Cx, u32 I2C_Timeout)
679 {
680   /* Check the parameters                                                                                   */
681   Assert_Param(IS_I2C(I2Cx));
682   Assert_Param(IS_I2C_TIMEOUT(I2C_Timeout));
683 
684   I2Cx->TOUT = (I2C_Timeout | (I2Cx->TOUT & 0xFFFF0000));
685 }
686 
687 /*********************************************************************************************************//**
688  * @brief This function is used to set the prescaler of I2C timeout value.
689  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
690  * @param I2C_Prescaler: specify the I2C time out prescaler value.
691  *   This parameter can be one of the following values:
692  *     @arg I2C_PRESCALER_1   : I2C prescaler set to 1
693  *     @arg I2C_PRESCALER_2   : I2C prescaler set to 2
694  *     @arg I2C_PRESCALER_4   : I2C prescaler set to 4
695  *     @arg I2C_PRESCALER_16  : I2C prescaler set to 16
696  *     @arg I2C_PRESCALER_32  : I2C prescaler set to 32
697  *     @arg I2C_PRESCALER_64  : I2C prescaler set to 64
698  *     @arg I2C_PRESCALER_128 : I2C prescaler set to 128
699  * @retval None
700  ************************************************************************************************************/
I2C_SetTimeOutPrescaler(HT_I2C_TypeDef * I2Cx,u32 I2C_Prescaler)701 void I2C_SetTimeOutPrescaler(HT_I2C_TypeDef* I2Cx, u32 I2C_Prescaler)
702 {
703   /* Check the parameters                                                                                   */
704   Assert_Param(IS_I2C(I2Cx));
705   Assert_Param(IS_I2C_PRESCALER(I2C_Prescaler));
706 
707   I2Cx->TOUT = (I2C_Prescaler | (I2Cx->TOUT & 0x0000FFFF));
708 }
709 
710 #if (LIBCFG_I2C_NO_ADDR_MASK == 0)
711 /*********************************************************************************************************//**
712  * @brief This function is used to determine the prescaler of I2C timeout value.
713  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
714  * @param I2C_Mask: specify the bit position of I2C slave address to be masked.
715  *   This parameter can be any combination of the following values:
716  *     @arg I2C_MASKBIT_0 : Bit 0 of I2C slave address is masked
717  *     @arg I2C_MASKBIT_1 : Bit 1 of I2C slave address is masked
718  *     @arg I2C_MASKBIT_2 : Bit 2 of I2C slave address is masked
719  *     @arg I2C_MASKBIT_3 : Bit 3 of I2C slave address is masked
720  *     @arg I2C_MASKBIT_4 : Bit 4 of I2C slave address is masked
721  *     @arg I2C_MASKBIT_5 : Bit 5 of I2C slave address is masked
722  *     @arg I2C_MASKBIT_6 : Bit 6 of I2C slave address is masked
723  *     @arg I2C_MASKBIT_7 : Bit 7 of I2C slave address is masked
724  *     @arg I2C_MASKBIT_8 : Bit 8 of I2C slave address is masked
725  *     @arg I2C_MASKBIT_9 : Bit 9 of I2C slave address is masked
726  * @retval None
727  ************************************************************************************************************/
I2C_AddressMaskConfig(HT_I2C_TypeDef * I2Cx,u32 I2C_Mask)728 void I2C_AddressMaskConfig(HT_I2C_TypeDef* I2Cx, u32 I2C_Mask)
729 {
730   /* Check the parameters                                                                                   */
731   Assert_Param(IS_I2C(I2Cx));
732   Assert_Param(IS_I2C_ADDRESS_MASK(I2C_Mask));
733 
734   I2Cx->ADDMR = I2C_Mask;
735 }
736 #endif
737 
738 /*********************************************************************************************************//**
739  * @brief Return the received address by the I2Cx peripheral.
740  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
741  * @retval The value of the received address.
742  ************************************************************************************************************/
I2C_GetAddressBuffer(HT_I2C_TypeDef * I2Cx)743 u16 I2C_GetAddressBuffer(HT_I2C_TypeDef* I2Cx)
744 {
745   /* Check the parameters                                                                                   */
746   Assert_Param(IS_I2C(I2Cx));
747 
748   return ((u16)I2Cx->ADDSR);
749 }
750 
751 /*********************************************************************************************************//**
752  * @brief Enable or Disable the combinational filter.
753  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
754  * @param NewState: This parameter can be ENABLE or DISABLE.
755  * @retval None
756  ************************************************************************************************************/
I2C_CombFilterCmd(HT_I2C_TypeDef * I2Cx,ControlStatus NewState)757 void I2C_CombFilterCmd(HT_I2C_TypeDef* I2Cx, ControlStatus NewState)
758 {
759   /* Check the parameters                                                                                   */
760   Assert_Param(IS_I2C(I2Cx));
761   Assert_Param(IS_CONTROL_STATUS(NewState));
762 
763   if (NewState != DISABLE)
764   {
765     I2Cx->CR |= CR_COMBFILTER_SET;
766   }
767   else
768   {
769     I2Cx->CR &= CR_COMBFILTER_RESET;
770   }
771 }
772 
773 /*********************************************************************************************************//**
774  * @brief This function is used to determine the filter glitch width of 0~2 PCLK.
775  * @param I2Cx: where I2Cx is the selected I2C from the I2C peripherals.
776  * @param Seq_Filter_Select: specify the glitch width of 0~2 PCLK.
777  *   This parameter can be any combination of the following values:
778  *     @arg SEQ_FILTER_DISABLE : sequential filter is disabled
779  *     @arg SEQ_FILTER_1_PCLK  : filter glitch width of 1 PCLK
780  *     @arg SEQ_FILTER_2_PCLK  : filter glitch width of 2 PCLK
781  * @retval None
782  ************************************************************************************************************/
I2C_SequentialFilterConfig(HT_I2C_TypeDef * I2Cx,u32 Seq_Filter_Select)783 void I2C_SequentialFilterConfig(HT_I2C_TypeDef* I2Cx, u32 Seq_Filter_Select)
784 {
785   u32 SHPGR = I2Cx->SHPGR;
786   u32 SLPGR = I2Cx->SLPGR;
787 
788   /* Check the parameters                                                                                   */
789   Assert_Param(IS_I2C(I2Cx));
790   Assert_Param(IS_I2C_SEQ_FILTER_MASK(Seq_Filter_Select));
791 
792   switch (I2Cx->CR & 0xC000)
793   {
794     case 0:
795       if (Seq_Filter_Select == SEQ_FILTER_1_PCLK)
796       {
797         if (SHPGR >= 2)
798         {
799           SHPGR -= 2;
800           SLPGR -= 2;
801         }
802       }
803       else if (Seq_Filter_Select == SEQ_FILTER_2_PCLK)
804       {
805         if (SHPGR >= 2)
806         {
807           SHPGR -= 3;
808           SLPGR -= 3;
809         }
810       }
811       break;
812 
813     case 0x4000:
814       if (Seq_Filter_Select == SEQ_FILTER_DISABLE)
815       {
816         SHPGR += 2;
817         SLPGR += 2;
818       }
819       else if (Seq_Filter_Select == SEQ_FILTER_2_PCLK)
820       {
821         if (SHPGR >= 1)
822         {
823           SHPGR -= 1;
824           SLPGR -= 1;
825         }
826       }
827       break;
828 
829     case 0x8000:
830       if (Seq_Filter_Select == SEQ_FILTER_DISABLE)
831       {
832         SHPGR += 3;
833         SLPGR += 3;
834       }
835       else if (Seq_Filter_Select == SEQ_FILTER_1_PCLK)
836       {
837         SHPGR += 1;
838         SLPGR += 1;
839       }
840       break;
841 
842     default:
843       break;
844   }
845 
846   I2Cx->SHPGR = SHPGR;
847   I2Cx->SLPGR = SLPGR;
848   I2Cx->CR = (I2Cx->CR & 0x3FFF) | Seq_Filter_Select;
849 }
850 /**
851   * @}
852   */
853 
854 
855 /**
856   * @}
857   */
858 
859 /**
860   * @}
861   */
862