1 /**
2 ******************************************************************************
3 * @file  HAL_i2c.c
4 * @author  IC Applications Department
5 * @version  V0.8
6 * @date  2019_08_02
7 * @brief  This file provides all the I2C firmware functions.
8 ******************************************************************************
9 * @copy
10 *
11 * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
12 * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
13 * TIME. AS A RESULT, HOLOCENE SHALL NOT BE HELD LIABLE FOR ANY
14 * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
15 * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
16 * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
17 *
18 * <h2><center>&copy; COPYRIGHT 2016 HOLOCENE</center></h2>
19 */
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "HAL_i2c.h"
23 #include "HAL_rcc.h"
24 
25 
26 /** @addtogroup StdPeriph_Driver
27 * @{
28 */
29 
30 /** @defgroup I2C
31 * @brief I2C driver modules
32 * @{
33 */
34 
35 /** @defgroup I2C_Private_TypesDefinitions
36 * @{
37 */
38 
39 /**
40 * @}
41 */
42 
43 /** @defgroup I2C_Private_Defines
44 * @{
45 */
46 
47 /*I2c Enable disable*/
48 #define IC_ENABLE_Reset         ((uint16_t)0xFFFE)
49 #define IC_ENABLE_Set           ((uint16_t)0x0001)
50 #define IC_CON_RESET                        ((uint16_t)0xFE8A)
51 #define INTR_MASK               ((uint16_t)0xC000)
52 
53 /*I2c DMA  reset*/
54 #define DMA_CR_TDMAE_RDMAE_Reset ((uint16_t)0xFFFC)
55 
56 /* I2C START mask */
57 #define IC_CON_START_Set           ((uint16_t)0x0020)
58 #define IC_CON_START_Reset         ((uint16_t)0xFFDF)
59 
60 /* I2C STOP mask */
61 #define IC_DATA_CMD_STOP_Set     ((uint16_t)0x0200)
62 #define IC_DATA_CMD_STOP_Reset   ((uint16_t)0xFDFF)
63 
64 /* I2C ADD2 mask */
65 #define IC_TAR_Reset         ((uint16_t)0xFF00)
66 
67 /* I2C IC_10BITADDR_MASTER bit mask */
68 #define IC_TAR_ENDUAL_Set         ((uint16_t)0x1000)
69 #define IC_TAR_ENDUAL_Reset       ((uint16_t)0xEFFF)
70 
71 /* I2C SPECIAL、GC_OR_START bits mask */
72 #define IC_TAR_GC_Set            ((uint16_t)0x0800)
73 #define IC_TAR_GC_Reset          ((uint16_t)0xF7FF)
74 
75 /* I2C FLAG mask */
76 #define FLAG_Mask               ((uint32_t)0x00793FFF)
77 
78 //static
79  uint8_t I2C_CMD_DIR = 0;
80 
81 /*新增加的用户变量,外部调用时需要更新该变量值*/
82 uint16_t I2C_DMA_DIR = 0;
83 
84 /**
85 * @}
86 */
87 
88 /** @defgroup I2C_Private_Macros
89 * @{
90 */
91 
92 /**
93 * @}
94 */
95 
96 /** @defgroup I2C_Private_Variables
97 * @{
98 */
99 
100 /**
101 * @}
102 */
103 
104 /** @defgroup I2C_Private_FunctionPrototypes
105 * @{
106 */
107 
108 /**
109 * @}
110 */
111 
112 /** @defgroup I2C_Private_Functions
113 * @{
114 */
115 
116 /**
117 * @brief  Deinitializes the I2Cx peripheral registers to their default
118 *   reset values.
119 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
120 * @retval : None
121 */
I2C_DeInit(I2C_TypeDef * I2Cx)122 void I2C_DeInit(I2C_TypeDef* I2Cx)
123 {
124   /* Check the parameters */
125   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
126   switch (*(uint32_t*)&I2Cx)
127   {
128   case I2C1_BASE:
129     /* Enable I2C1 reset state */
130     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
131     /* Release I2C1 from reset state */
132     RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
133     break;
134 
135   default:
136     break;
137   }
138 }
139 
140 /**
141 * @brief  Initializes the I2Cx peripheral according to the specified
142 *   parameters in the I2C_InitStruct.
143 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
144 * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
145 *   contains the configuration information for the specified
146 *   I2C peripheral.
147 * @retval : None
148 */
I2C_Init(I2C_TypeDef * I2Cx,I2C_InitTypeDef * I2C_InitStruct)149 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
150 {
151 
152   uint16_t tmpreg = 0;
153   uint32_t pclk1 = 8000000;
154   uint32_t minSclLowTime = 0;
155   uint32_t i2cPeriod = 0;
156   uint32_t pclk1Period = 0;
157   RCC_ClocksTypeDef  rcc_clocks;
158   /* Check the parameters */
159   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
160   assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
161   assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
162   /*---------------------------- I2Cx IC_ENABLE Configuration ------------------------*/
163   /* Disable the selected I2C peripheral */
164   I2Cx->IC_ENABLE &= IC_ENABLE_Reset;
165 
166   /* Get pclk1 frequency value */
167   RCC_GetClocksFreq(&rcc_clocks);
168   pclk1 = rcc_clocks.PCLK1_Frequency;
169 
170   /* Set pclk1 period value */
171   pclk1Period = 1000000000/pclk1;
172 
173   i2cPeriod = 1000000000/I2C_InitStruct->I2C_ClockSpeed; //ns unit
174   tmpreg = 0;
175 
176     /*Get the I2Cx IC_CON value */
177   tmpreg = I2Cx->IC_CON;
178   /*Clear TX_EMPTY_CTRL,IC_SLAVE_DISABLE,IC_RESTART_EN,IC_10BITADDR_SLAVE,SPEED,MASTER_MODE bits*/
179   tmpreg &= IC_CON_RESET;
180 
181   /* Configure speed in standard mode */
182   if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
183   {
184     minSclLowTime = i2cPeriod/pclk1Period;
185         I2Cx->IC_SS_SCL_LCNT = minSclLowTime/2;
186         I2Cx->IC_SS_SCL_HCNT = minSclLowTime - I2Cx->IC_SS_SCL_LCNT;
187         I2C_InitStruct->I2C_Speed = I2C_Speed_STANDARD;
188   }
189   else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
190   {
191         minSclLowTime = i2cPeriod/pclk1Period;
192         I2Cx->IC_FS_SCL_LCNT = minSclLowTime/2;
193         I2Cx->IC_FS_SCL_HCNT = minSclLowTime - I2Cx->IC_FS_SCL_LCNT;
194         I2C_InitStruct->I2C_Speed = I2C_Speed_FAST;
195   }
196 
197 
198   /*Set TX_EMPTY_CTRL,IC_SLAVE_DISABLE,IC_RESTART_EN,IC_10BITADDR_SLAVE,SPEED,MASTER_MODE bits*/
199   tmpreg = TX_EMPTY_CTRL | IC_SLAVE_DISABLE | IC_RESTART_EN |IC_7BITADDR_MASTER | I2C_InitStruct->I2C_Speed | I2C_InitStruct->I2C_Mode;
200   /* Write to I2Cx IC_CON */
201   I2Cx->IC_CON = tmpreg;
202 
203   /*---------------------------- I2Cx IC_INTR_MASK Configuration ------------------------*/
204   /* Get the I2Cx IC_INTR_MASK value */
205   tmpreg = I2Cx->IC_INTR_MASK;
206   /* clear the I2Cx IC_INTR_MASK value */
207   tmpreg &= INTR_MASK;
208   /* Write to IC_INTR_MASK */
209   I2Cx->IC_INTR_MASK = tmpreg;
210 
211   /* Write to IC_RX_TL  */
212   I2Cx->IC_RX_TL = 0x0; //rxfifo depth is 1
213   /* Write to IC_TX_TL  */
214   I2Cx->IC_TX_TL = 0x1; //tcfifo depth is 1
215 
216 }
217 
218 /**
219 * @brief  Fills each I2C_InitStruct member with its default value.
220 * @param I2C_InitStruct: pointer to an I2C_InitTypeDef structure
221 *   which will be initialized.
222 * @retval : None
223 */
I2C_StructInit(I2C_InitTypeDef * I2C_InitStruct)224 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
225 {
226   /*---------------- Reset I2C init structure parameters values ----------------*/
227   /* Initialize the I2C_Mode member */
228   I2C_InitStruct->I2C_Mode = I2C_Mode_MASTER;
229   /* Initialize the I2C_OwnAddress member */
230   I2C_InitStruct->I2C_OwnAddress = 0xA8;
231   /* Initialize the I2C_Speed member */
232   I2C_InitStruct->I2C_Speed = I2C_Speed_STANDARD;
233   /* initialize the I2C_ClockSpeed member */
234   I2C_InitStruct->I2C_ClockSpeed = 100000;
235 }
236 
237 /**
238 * @brief  Enables or disables the specified I2C peripheral.
239 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
240 * @param NewState: new state of the I2Cx peripheral. This parameter
241 *   can be: ENABLE or DISABLE.
242 * @retval : None
243 */
I2C_Cmd(I2C_TypeDef * I2Cx,FunctionalState NewState)244 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
245 {
246   /* Check the parameters */
247   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
248   assert_param(IS_FUNCTIONAL_STATE(NewState));
249   if (NewState != DISABLE)
250   {
251     /* Enable the selected I2C peripheral */
252     I2Cx->IC_ENABLE |= IC_ENABLE_Set;
253   }
254   else
255   {
256     /* Disable the selected I2C peripheral */
257     I2Cx->IC_ENABLE &= IC_ENABLE_Reset;
258   }
259 }
260 
261 /**
262 * @brief  Enables or disables the specified I2C DMA requests.
263 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
264 * @param    DMA_Direcction : TDMAE_SET,RDMAE_SET
265 *   This parameter can be any combination of the following values:
266 * @arg TDMAE_SET    :DMA TX set
267 * @arg RDMAE_SET    :DMA RX set
268 * @param NewState: new state of the I2C DMA transfer.
269 *   This parameter can be: ENABLE or DISABLE.
270 * @retval : None
271 */
I2C_DMACmd(I2C_TypeDef * I2Cx,FunctionalState NewState)272 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
273 {
274   /* Check the parameters */
275   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
276   assert_param(IS_FUNCTIONAL_STATE(NewState));
277   if (NewState != DISABLE)
278   {
279     /* Enable the selected I2C DMA requests */
280     if(I2C_DMA_DIR==TDMAE_SET)
281     {
282       /* Enable the selected I2C TX DMA requests */
283       I2Cx->IC_DMA_CR |= TDMAE_SET;
284     }
285     else if(I2C_DMA_DIR==RDMAE_SET)
286     {
287       /* Enable the selected I2C TX DMA requests */
288       I2Cx->IC_DMA_CR |= RDMAE_SET;
289     }
290   }
291   else
292   {
293     /* Disable the selected I2C DMA requests */
294     I2Cx->IC_DMA_CR &= DMA_CR_TDMAE_RDMAE_Reset;
295   }
296 }
297 
298 
299 /**
300 * @brief  Generates I2Cx communication START condition.
301 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
302 * @param NewState: new state of the I2C START condition generation.
303 *   This parameter can be: ENABLE or DISABLE.
304 * @retval : None.
305 */
I2C_GenerateSTART(I2C_TypeDef * I2Cx,FunctionalState NewState)306 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
307 {
308   /* Check the parameters */
309   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
310   assert_param(IS_FUNCTIONAL_STATE(NewState));
311   if (NewState != DISABLE)
312   {
313     /* Generate a START condition */
314     I2Cx->IC_CON |= IC_CON_START_Set;
315   }
316   else
317   {
318     /* Disable the START condition generation */
319     I2Cx->IC_CON &= IC_CON_START_Reset;
320   }
321 }
322 
323 /**
324 * @brief  Generates I2Cx communication STOP condition.
325 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
326 * @param NewState: new state of the I2C STOP condition generation.
327 *   This parameter can be: ENABLE or DISABLE.
328 * @retval : None.
329 */
I2C_GenerateSTOP(I2C_TypeDef * I2Cx,FunctionalState NewState)330 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
331 {
332   /* Check the parameters */
333   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
334   assert_param(IS_FUNCTIONAL_STATE(NewState));
335   if (NewState != DISABLE)
336   {
337     /* Generate a STOP condition */
338     I2Cx->IC_DATA_CMD |= IC_DATA_CMD_STOP_Set;
339   }
340   else
341   {
342     /* Disable the STOP condition generation */
343     I2Cx->IC_DATA_CMD &= IC_DATA_CMD_STOP_Reset;
344   }
345 }
346 
347 /**
348 * @brief  Configures the specified I2C own address2.
349 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
350 * @param Address: specifies the 7bit I2C own address2.
351 * @retval : None.
352 */
I2C_OwnAddress2Config(I2C_TypeDef * I2Cx,uint8_t Address)353 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
354 //void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address)
355 {
356   uint16_t tmpreg = 0;
357   /* Check the parameters */
358   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
359   /* Get the old register value */
360   tmpreg = I2Cx->IC_TAR;
361   /* Reset I2Cx Own address2 bit [7:0] */
362   tmpreg &= IC_TAR_Reset;
363   /* Set I2Cx Own address2 */
364   tmpreg |= Address>>1;
365   /* Store the new register value */
366   I2Cx->IC_TAR = tmpreg;
367 }
368 
369 /**
370 * @brief  Enables or disables the specified I2C dual addressing mode.
371 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
372 * @param NewState: new state of the I2C dual addressing mode.
373 *   This parameter can be: ENABLE or DISABLE.
374 * @retval : None
375 */
I2C_DualAddressCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)376 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
377 {
378   /* Check the parameters */
379   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
380   assert_param(IS_FUNCTIONAL_STATE(NewState));
381   if (NewState != DISABLE)
382   {
383     /* Enable dual addressing mode */
384     I2Cx->IC_TAR |= IC_TAR_ENDUAL_Set;
385   }
386   else
387   {
388     /* Disable dual addressing mode */
389     I2Cx->IC_TAR &= IC_TAR_ENDUAL_Reset;
390   }
391 }
392 
393 /**
394 * @brief  Enables or disables the specified I2C general call feature.
395 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
396 * @param NewState: new state of the I2C General call.
397 *   This parameter can be: ENABLE or DISABLE.
398 * @retval : None
399 */
I2C_GeneralCallCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)400 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
401 {
402   /* Check the parameters */
403   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
404   assert_param(IS_FUNCTIONAL_STATE(NewState));
405   if (NewState != DISABLE)
406   {
407     /* Enable generall call */
408     I2Cx->IC_TAR |= IC_TAR_GC_Set;
409   }
410   else
411   {
412     /* Disable generall call */
413     I2Cx->IC_TAR &= IC_TAR_GC_Reset;
414   }
415 }
416 
417 /**
418 * @brief  Enables or disables the specified I2C interrupts.
419 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
420 * @param I2C_IT: specifies the I2C interrupts sources to be enabled
421 *   or disabled.
422 *   This parameter can be any combination of the following values:
423 * @arg I2C_IT_RX_UNDER: Rx Buffer is empty interrupt mask
424 * @arg I2C_IT_RX_OVER : RX  Buffer Overrun interrupt mask
425 * @arg I2C_IT_RX_FULL : Rx buffer full interrupt mask
426 * @arg I2C_IT_TX_OVER : TX  Buffer Overrun interrupt mask
427 * @arg I2C_IT_TX_EMPTY  : TX_FIFO empty interrupt mask
428 * @arg I2C_IT_RD_REQ    : I2C work as slave or master interrupt mask
429 * @arg I2C_IT_TX_ABRT   : TX error interrupt  mask(Master mode)
430 * @arg I2C_IT_RX_DONE   : Master not ack interrupt mask(slave mode)
431 * @arg I2C_IT_ACTIVITY  : I2C activity interrupt mask
432 * @arg I2C_IT_STOP_DET  : stop condition  interrupt mask
433 * @arg I2C_IT_START_DET : start condition  interrupt mask
434 * @arg I2C_IT_GEN_CALL  : a general call address and ack interrupt mask
435 * @param NewState: new state of the specified I2C interrupts.
436 *   This parameter can be: ENABLE or DISABLE.
437 * @retval : None
438 */
I2C_ITConfig(I2C_TypeDef * I2Cx,uint16_t I2C_IT,FunctionalState NewState)439 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
440 {
441   /* Check the parameters */
442   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
443   assert_param(IS_FUNCTIONAL_STATE(NewState));
444   assert_param(IS_I2C_CONFIG_IT(I2C_IT));
445 
446   if(I2C_IT == I2C_IT_RX_FULL)
447   {
448     I2C1->IC_DATA_CMD = CMD_READ;
449   }
450 
451   if (NewState != DISABLE)
452   {
453     /* Enable the selected I2C interrupts */
454     I2Cx->IC_INTR_MASK |= I2C_IT;
455   }
456   else
457   {
458     /* Disable the selected I2C interrupts */
459     I2Cx->IC_INTR_MASK &= (uint16_t)~I2C_IT;
460   }
461 }
462 
463 /**
464 * @brief  Sends a data byte through the I2Cx peripheral.
465 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
466 * @param Data: Byte to be transmitted..
467 * @retval : None
468 */
I2C_SendData(I2C_TypeDef * I2Cx,uint8_t Data)469 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
470 {
471   /* Check the parameters */
472   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
473   /* Write in the IC_DATA_CMD register the data to be sent */
474   I2Cx->IC_DATA_CMD = CMD_WRITE | Data;
475 }
476 
477 /**
478 * @brief  Returns the most recent received data by the I2Cx peripheral.
479 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
480 * @retval : The value of the received data.
481 
482 */
I2C_ReadCmd(I2C_TypeDef * I2Cx)483 void I2C_ReadCmd(I2C_TypeDef* I2Cx)
484 {
485   /* Check the parameters */
486   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
487 
488   I2Cx->IC_DATA_CMD = CMD_READ;
489 }
490 
491 /**
492 * @brief  Returns the most recent received data by the I2Cx peripheral.
493 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
494 * @retval : The value of the received data.
495 */
I2C_ReceiveData(I2C_TypeDef * I2Cx)496 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
497 {
498   /* Check the parameters */
499   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
500   I2C_CMD_DIR= 0;
501   /* Return the data in the IC_DATA_CMD register */
502   return (uint8_t)I2Cx->IC_DATA_CMD;
503 }
504 
505 /**
506 * @brief  Transmits the address byte to select the slave device.
507 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
508 * @param Address: specifies the slave address which will be transmitted
509 * @param I2C_Direction: specifies whether the I2C device will be a
510 *   Transmitter or a Receiver.
511 *   This parameter can be one of the following values
512 * @arg I2C_Direction_Transmitter: Transmitter mode
513 * @arg I2C_Direction_Receiver: Receiver mode
514 * @retval : None.
515 */
I2C_Send7bitAddress(I2C_TypeDef * I2Cx,uint8_t Address,uint8_t I2C_Direction)516 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
517 {
518   /* Store the new register value */
519   I2Cx->IC_TAR = Address>>1;
520 }
521 
522 /**
523 * @brief  Reads the specified I2C register and returns its value.
524 * @param I2C_Register: specifies the register to read.
525 *   This parameter can be one of the following values:
526 * @retval : The value of the read register.
527 */
I2C_ReadRegister(I2C_TypeDef * I2Cx,uint8_t I2C_Register)528 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
529 {
530   /* Check the parameters */
531   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
532   assert_param(IS_I2C_REGISTER(I2C_Register));
533   /* Return the selected register value */
534   return (*(__IO uint16_t *)(*((__IO uint32_t *)&I2Cx) + I2C_Register));
535 }
536 
537 /**
538 * @brief  Returns the last I2Cx Event.
539 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
540 * @retval : The last event
541 */
I2C_GetLastEvent(I2C_TypeDef * I2Cx)542 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)
543 {
544   uint32_t lastevent = 0;
545   uint32_t flag1 = 0;
546   /* Check the parameters */
547   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
548   /* Read the I2Cx status register */
549   flag1 = I2Cx->IC_RAW_INTR_STAT;
550 
551   /* Get the last event value from I2C status register */
552   lastevent = (flag1 ) & FLAG_Mask;
553   /* Return status */
554   return lastevent;
555 }
556 
557 
558 
559 /**
560 * @brief  Checks whether the last I2Cx Event is equal to the one passed
561 *   as parameter.
562 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
563 * @param I2C_EVENT: specifies the event to be checked.
564 *   This parameter can be one of the following values:
565 * @arg I2C_EVENT_RX_UNDER: Rx Buffer is empty event
566 * @arg I2C_EVENT_RX_OVER : RX  Buffer Overrun event
567 * @arg I2C_EVENTT_RX_FULL : Rx buffer full event
568 * @arg I2C_EVENT_TX_OVER : TX  Buffer Overrun event
569 * @arg I2C_EVENT_TX_EMPTY   : TX_FIFO empty event
570 * @arg I2C_EVENT_RD_REQ : I2C work as slave or master event
571 * @arg I2C_EVENT_TX_ABRT    : TX error event(Master mode)
572 * @arg I2C_EVENT_RX_DONE    : Master not ack event(slave mode)
573 * @arg I2C_EVENT_ACTIVITY   : I2C activity event
574 * @arg I2C_EVENT_STOP_DET   : stop condition  event
575 * @arg I2C_EVENT_START_DET  : start condition  event
576 * @arg I2C_EVENT_GEN_CALL   : a general call address and ack event
577 * - SUCCESS: Last event is equal to the I2C_EVENT
578 * - ERROR: Last event is different from the I2C_EVENT
579 */
I2C_CheckEvent(I2C_TypeDef * I2Cx,uint32_t I2C_EVENT)580 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
581 {
582   uint32_t lastevent = 0;
583   uint32_t flag1 = 0;
584   ErrorStatus status = ERROR;
585   /* Check the parameters */
586   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
587   assert_param(IS_I2C_EVENT(I2C_EVENT));
588 
589   if((I2C_EVENT == I2C_EVENT_RX_FULL)&&(I2C_CMD_DIR==0))
590   {
591     I2C1->IC_DATA_CMD = CMD_READ;
592     I2C_CMD_DIR = 1;
593   }
594   /* Read the I2Cx status register */
595   flag1 = I2Cx->IC_RAW_INTR_STAT;
596   //flag1 = I2Cx->IC_INTR_STAT;
597   /* Get the last event value from I2C status register */
598   lastevent = (flag1 ) & I2C_EVENT;
599 
600   /* Check whether the last event is equal to I2C_EVENT */
601   if (lastevent == I2C_EVENT )
602     //if((I2Cx->IC_RAW_INTR_STAT & I2C_EVENT) != (uint32_t)RESET)
603   {
604     /* SUCCESS: last event is equal to I2C_EVENT */
605     status = SUCCESS;
606   }
607   else
608   {
609     /* ERROR: last event is different from I2C_EVENT */
610     status = ERROR;
611   }
612   /* Return status */
613   return status;
614 
615 }
616 
617 /**
618 * @brief  Checks whether the specified I2C flag is set or not.
619 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
620 * @param I2C_FLAG: specifies the flag to check.
621 *   This parameter can be one of the following values:
622 * @arg I2C_FLAG_RX_UNDER: Rx Buffer is empty flag
623 * @arg I2C_FLAG_RX_OVER : RX  Buffer Overrun flag
624 * @arg I2C_FLAG_RX_FULL : Rx buffer full flag
625 * @arg I2C_FLAG_TX_OVER : TX  Buffer Overrun flag
626 * @arg I2C_FLAG_TX_EMPTY: TX_FIFO empty flag
627 * @arg I2C_FLAG_RD_REQ  : I2C work as slave or master flag
628 * @arg I2C_FLAG_TX_ABRT : TX error flag(Master mode)
629 * @arg I2C_FLAG_RX_DONE : Master not ack flag(slave mode)
630 * @arg I2C_FLAG_ACTIVITY: I2C activity flag
631 * @arg I2C_FLAG_STOP_DET: stop condition  flag
632 * @arg I2C_FLAG_START_DET: start condition  flag
633 * @arg I2C_FLAG_GEN_CALL : a general call address and ack flag
634 * @retval : The new state of I2C_FLAG (SET or RESET).
635 */
I2C_GetFlagStatus(I2C_TypeDef * I2Cx,uint32_t I2C_FLAG)636 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
637 {
638   FlagStatus bitstatus = RESET;
639   __IO uint32_t i2creg = 0, i2cxbase = 0;
640   /* Check the parameters */
641   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
642   assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
643   /* Check the status of the specified I2C flag and set I2C direction*/
644   if((I2C_FLAG == I2C_FLAG_RX_FULL)&&(I2C_CMD_DIR==0))
645   {
646     I2Cx->IC_DATA_CMD = CMD_READ;
647     I2C_CMD_DIR = 1;
648   }
649 
650   /* Check the status of the specified I2C flag */
651   if((I2Cx->IC_RAW_INTR_STAT & I2C_FLAG) != (uint32_t)RESET)
652   {
653     /* I2C_FLAG is set */
654     bitstatus = SET;
655   }
656   else
657   {
658     /* I2C_FLAG is reset */
659     bitstatus = RESET;
660   }
661 
662   /* Return the I2C_FLAG status */
663   return  bitstatus;
664 }
665 
666 /**
667 * @brief  Clears the I2Cx's pending flags.
668 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
669 * @param I2C_FLAG: specifies the flag to clear.
670 *   This parameter can be any combination of the following values:
671 * @arg I2C_FLAG_RX_UNDER: Rx Buffer is empty flag
672 * @arg I2C_FLAG_RX_OVER : RX  Buffer Overrun flag
673 * @arg I2C_FLAG_RX_FULL : Rx buffer full flag
674 * @arg I2C_FLAG_TX_OVER : TX  Buffer Overrun flag
675 * @arg I2C_FLAG_TX_EMPTY: TX_FIFO empty flag
676 * @arg I2C_FLAG_RD_REQ  : I2C work as slave or master flag
677 * @arg I2C_FLAG_TX_ABRT : TX error flag(Master mode)
678 * @arg I2C_FLAG_RX_DONE : Master not ack flag(slave mode)
679 * @arg I2C_FLAG_ACTIVITY: I2C activity flag
680 * @arg I2C_FLAG_STOP_DET: stop condition  flag
681 * @arg I2C_FLAG_START_DET: start condition  flag
682 * @arg I2C_FLAG_GEN_CALL : a general call address and ack flag
683 * @retval : None
684 */
I2C_ClearFlag(I2C_TypeDef * I2Cx,uint32_t I2C_FLAG)685 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
686 {
687   __IO uint32_t i2creg = 0, i2cxbase = 0;
688   /* Check the parameters */
689   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
690   assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
691   /* Get the I2Cx peripheral base address */
692   i2cxbase = (*(uint32_t*)&(I2Cx));
693 
694   if(I2C_FLAG==I2C_FLAG_RX_UNDER)
695   {
696     /* Get the I2Cx SR1 register address */
697     i2cxbase += 0x44;
698     /* Clear the selected I2C flag */
699     I2Cx->IC_CLR_RX_UNDER = (uint16_t)I2C_FLAG;
700   }
701   if(I2C_FLAG==I2C_FLAG_RX_OVER)
702   {
703     /* Get the I2Cx SR1 register address */
704     i2cxbase += 0x48;
705     /* Clear the selected I2C flag */
706     I2Cx->IC_CLR_RX_OVER = (uint16_t)I2C_FLAG;
707   }
708   if(I2C_FLAG==I2C_FLAG_TX_OVER)
709   {
710     /* Get the I2Cx SR1 register address */
711     i2cxbase += 0x4C;
712     /* Clear the selected I2C flag */
713     I2Cx->IC_CLR_TX_OVER = (uint16_t)I2C_FLAG;
714   }
715   if(I2C_FLAG==I2C_FLAG_RD_REQ)
716   {
717     /* Get the I2Cx SR1 register address */
718     i2cxbase += 0x50;
719     /* Clear the selected I2C flag */
720     I2Cx->IC_CLR_RD_REQ = (uint16_t)I2C_FLAG;
721   }
722   if(I2C_FLAG==I2C_FLAG_TX_ABRT)
723   {
724     /* Get the I2Cx SR1 register address */
725     i2cxbase += 0x54;
726     /* Clear the selected I2C flag */
727     I2Cx->IC_CLR_TX_ABRT = (uint16_t)I2C_FLAG;
728   }
729   if(I2C_FLAG==I2C_FLAG_RX_DONE)
730   {
731     /* Get the I2Cx SR1 register address */
732     i2cxbase += 0x58;
733     /* Clear the selected I2C flag */
734     I2Cx->IC_CLR_RX_DONE = (uint16_t)I2C_FLAG;
735   }
736   if(I2C_FLAG==I2C_FLAG_ACTIVITY)
737   {
738     /* Get the I2Cx SR1 register address */
739     i2cxbase += 0x5C;
740     /* Clear the selected I2C flag */
741     I2Cx->IC_CLR_ACTIVITY = (uint16_t)I2C_FLAG;
742   }
743   if(I2C_FLAG==I2C_FLAG_STOP_DET)
744   {
745     /* Get the I2Cx SR1 register address */
746     i2cxbase += 0x60;
747     /* Clear the selected I2C flag */
748     I2Cx->IC_CLR_STOP_DET = (uint16_t)I2C_FLAG;
749   }
750   if(I2C_FLAG==I2C_FLAG_START_DET)
751   {
752     /* Get the I2Cx SR1 register address */
753     i2cxbase += 0x64;
754     /* Clear the selected I2C flag */
755     I2Cx->IC_CLR_START_DET = (uint16_t)I2C_FLAG;
756   }
757   if(I2C_FLAG==I2C_FLAG_GEN_CALL)
758   {
759     /* Get the I2Cx SR1 register address */
760     i2cxbase += 0x68;
761     /* Clear the selected I2C flag */
762     I2Cx->IC_CLR_GEN_CALL = (uint16_t)I2C_FLAG;
763   }
764 }
765 
766 /**
767 * @brief  Checks whether the specified I2C interrupt has occurred or not.
768 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
769 * @param I2C_IT: specifies the interrupt source to check.
770 *   This parameter can be one of the following values:
771 * @arg I2C_IT_RX_UNDER: Rx Buffer is empty interrupt
772 * @arg I2C_IT_RX_OVER : RX  Buffer Overrun interrupt
773 * @arg I2C_IT_RX_FULL : Rx buffer full interrupt
774 * @arg I2C_IT_TX_OVER : TX  Buffer Overrun interrupt
775 * @arg I2C_IT_TX_EMPTY  : TX_FIFO empty interrupt
776 * @arg I2C_IT_RD_REQ    : I2C work as slave or master interrupt
777 * @arg I2C_IT_TX_ABRT   : TX error interrupt  (Master mode)
778 * @arg I2C_IT_RX_DONE   : Master not ack interrupt (slave mode)
779 * @arg I2C_IT_ACTIVITY  : I2C activity interrupt
780 * @arg I2C_IT_STOP_DET  : stop condition  interrupt
781 * @arg I2C_IT_START_DET : start condition  interrupt
782 * @arg I2C_IT_GEN_CALL  : a general call address and ack interrupt
783 * @retval : The new state of I2C_IT (SET or RESET).
784 */
I2C_GetITStatus(I2C_TypeDef * I2Cx,uint32_t I2C_IT)785 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
786 {
787   ITStatus bitstatus = RESET;
788   /* Check the parameters */
789   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
790   assert_param(IS_I2C_GET_IT(I2C_IT));
791 
792   /* Check the status of the specified I2C flag */
793   if((I2Cx->IC_RAW_INTR_STAT & I2C_IT) != (uint32_t)RESET)
794   {
795     /* I2C_IT is set */
796     bitstatus = SET;
797   }
798   else
799   {
800     /* I2C_IT is reset */
801     bitstatus = RESET;
802   }
803 
804   /* Return the I2C_IT status */
805   return  bitstatus;
806 }
807 
808 /**
809 * @brief  Clears the I2Cx interrupt pending bits.
810 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
811 * @param I2C_IT: specifies the interrupt pending bit to clear.
812 *   This parameter can be any combination of the following values:
813 * @arg I2C_IT_RX_UNDER: Rx Buffer is empty interrupt
814 * @arg I2C_IT_RX_OVER : RX  Buffer Overrun interrupt
815 * @arg I2C_IT_RX_FULL : Rx buffer full interrupt
816 * @arg I2C_IT_TX_OVER : TX  Buffer Overrun interrupt
817 * @arg I2C_IT_TX_EMPTY  : TX_FIFO empty interrupt
818 * @arg I2C_IT_RD_REQ    : I2C work as slave or master interrupt
819 * @arg I2C_IT_TX_ABRT   : TX error interrupt  (Master mode)
820 * @arg I2C_IT_RX_DONE   : Master not ack interrupt (slave mode)
821 * @arg I2C_IT_ACTIVITY  : I2C activity interrupt
822 * @arg I2C_IT_STOP_DET  : stop condition  interrupt
823 * @arg I2C_IT_START_DET : start condition  interrupt
824 * @arg I2C_IT_GEN_CALL  : a general call address and ack interrupt
825 * @retval : None
826 */
I2C_ClearITPendingBit(I2C_TypeDef * I2Cx,uint32_t I2C_IT)827 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
828 {
829   /* Check the parameters */
830   assert_param(IS_I2C_ALL_PERIPH(I2Cx));
831   assert_param(IS_I2C_CLEAR_IT(I2C_IT));
832 
833   /* Clear the selected I2C flag */
834   I2Cx->IC_INTR_MASK &= (uint16_t)~I2C_IT;
835 }
836 
837 /**
838 * @}
839 */
840 
841 /**
842 * @}
843 */
844 
845 /**
846 * @}
847 */
848 /*-------------------------(C) COPYRIGHT 2016 HOLOCENE ----------------------*/
849