1 /**
2 ******************************************************************************
3 * @file    HAL_i2c.c
4 * @author  AE Team
5 * @version V1.0.0
6 * @date    28/7/2017
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, MindMotion 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 MindMotion</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 
79 static 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     case I2C2_BASE:
135         /* Enable I2C2 reset state */
136         RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, ENABLE);
137         /* Release I2C2 from reset state */
138         RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C2, DISABLE);
139         break;
140     default:
141         break;
142     }
143 }
144 
145 /**
146 * @brief  Initializes the I2Cx peripheral according to the specified
147 *   parameters in the I2C_InitStruct.
148 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
149 * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
150 *   contains the configuration information for the specified
151 *   I2C peripheral.
152 * @retval : None
153 */
I2C_Init(I2C_TypeDef * I2Cx,I2C_InitTypeDef * I2C_InitStruct)154 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
155 {
156 
157     uint16_t tmpreg = 0;
158     uint32_t pclk1 = 8000000;
159     uint32_t minSclLowTime = 0;
160     uint32_t i2cPeriod = 0;
161     uint32_t pclk1Period = 0;
162     RCC_ClocksTypeDef  rcc_clocks;
163     /* Check the parameters */
164     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
165     assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
166     assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
167     /*---------------------------- I2Cx IC_ENABLE Configuration ------------------------*/
168     /* Disable the selected I2C peripheral */
169     I2Cx->IC_ENABLE &= IC_ENABLE_Reset;
170 
171     /* Get pclk1 frequency value */
172     RCC_GetClocksFreq(&rcc_clocks);
173     pclk1 = rcc_clocks.PCLK1_Frequency;
174 
175     /* Set pclk1 period value */
176     pclk1Period = 1000000000/pclk1;
177 
178     i2cPeriod = 1000000000/I2C_InitStruct->I2C_ClockSpeed; //ns unit
179     tmpreg = 0;
180     /* Configure speed in standard mode */
181     if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
182     {
183         /* Standard mode speed calculate */
184         minSclLowTime = 4700; //ns unit
185         tmpreg = minSclLowTime/pclk1Period;
186         /* Write to I2Cx IC_SS_SCL_LCNT */
187         I2Cx->IC_SS_SCL_LCNT = tmpreg;
188         tmpreg = (i2cPeriod - pclk1Period*I2Cx->IC_SS_SCL_LCNT)/pclk1Period;
189         /* Write to I2Cx IC_SS_SCL_HCNT */
190         I2Cx->IC_SS_SCL_HCNT = tmpreg;
191 
192 
193     }
194     else /*(I2C_InitStruct->I2C_ClockSpeed <= 400000)*/
195     {
196         /* Configure speed in fast mode */
197         minSclLowTime = 1300; //ns unit
198         tmpreg = minSclLowTime/pclk1Period;
199         /* Write to I2Cx IC_FS_SCL_LCNT */
200         I2Cx->IC_FS_SCL_LCNT = tmpreg;
201         tmpreg = (i2cPeriod - pclk1Period*I2Cx->IC_FS_SCL_LCNT)/pclk1Period;
202         /* Write to I2Cx IC_FS_SCL_HCNT */
203         I2Cx->IC_FS_SCL_HCNT = tmpreg;
204     }
205 
206     /*Get the I2Cx IC_CON value */
207     tmpreg = I2Cx->IC_CON;
208     /*Clear TX_EMPTY_CTRL,IC_SLAVE_DISABLE,IC_RESTART_EN,IC_10BITADDR_SLAVE,SPEED,MASTER_MODE bits*/
209     tmpreg &= IC_CON_RESET;
210     /*Set TX_EMPTY_CTRL,IC_SLAVE_DISABLE,IC_RESTART_EN,IC_10BITADDR_SLAVE,SPEED,MASTER_MODE bits*/
211     tmpreg = TX_EMPTY_CTRL | IC_SLAVE_DISABLE | IC_RESTART_EN |IC_7BITADDR_MASTER | I2C_InitStruct->I2C_Speed | I2C_InitStruct->I2C_Mode;
212     /* Write to I2Cx IC_CON */
213     I2Cx->IC_CON = tmpreg;
214 
215     /*---------------------------- I2Cx IC_INTR_MASK Configuration ------------------------*/
216     /* Get the I2Cx IC_INTR_MASK value */
217     tmpreg = I2Cx->IC_INTR_MASK;
218     /* clear the I2Cx IC_INTR_MASK value */
219     tmpreg &= INTR_MASK;
220     /* Write to IC_INTR_MASK */
221     I2Cx->IC_INTR_MASK = tmpreg;
222 
223     /* Write to IC_RX_TL  */
224     I2Cx->IC_RX_TL = 0x0; //rxfifo depth is 1
225     /* Write to IC_TX_TL  */
226     I2Cx->IC_TX_TL = 0x1; //tcfifo depth is 1
227 
228 }
229 
230 /**
231 * @brief  Fills each I2C_InitStruct member with its default value.
232 * @param I2C_InitStruct: pointer to an I2C_InitTypeDef structure
233 *   which will be initialized.
234 * @retval : None
235 */
I2C_StructInit(I2C_InitTypeDef * I2C_InitStruct)236 void I2C_StructInit(I2C_InitTypeDef* I2C_InitStruct)
237 {
238     /*---------------- Reset I2C init structure parameters values ----------------*/
239     /* Initialize the I2C_Mode member */
240     I2C_InitStruct->I2C_Mode = I2C_Mode_MASTER;
241     /* Initialize the I2C_OwnAddress member */
242     I2C_InitStruct->I2C_OwnAddress = 0xA8;
243     /* Initialize the I2C_Speed member */
244     I2C_InitStruct->I2C_Speed = I2C_Speed_STANDARD;
245     /* initialize the I2C_ClockSpeed member */
246     I2C_InitStruct->I2C_ClockSpeed = 100000;
247 }
248 
249 /**
250 * @brief  Enables or disables the specified I2C peripheral.
251 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
252 * @param NewState: new state of the I2Cx peripheral. This parameter
253 *   can be: ENABLE or DISABLE.
254 * @retval : None
255 */
I2C_Cmd(I2C_TypeDef * I2Cx,FunctionalState NewState)256 void I2C_Cmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
257 {
258     /* Check the parameters */
259     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
260     assert_param(IS_FUNCTIONAL_STATE(NewState));
261     if (NewState != DISABLE)
262     {
263         /* Enable the selected I2C peripheral */
264         I2Cx->IC_ENABLE |= IC_ENABLE_Set;
265     }
266     else
267     {
268         /* Disable the selected I2C peripheral */
269         I2Cx->IC_ENABLE &= IC_ENABLE_Reset;
270     }
271 }
272 
273 /**
274 * @brief  Enables or disables the specified I2C DMA requests.
275 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
276 * @param	DMA_Direcction : TDMAE_SET,RDMAE_SET
277 *   This parameter can be any combination of the following values:
278 * @arg TDMAE_SET	:DMA TX set
279 * @arg RDMAE_SET	:DMA RX set
280 * @param NewState: new state of the I2C DMA transfer.
281 *   This parameter can be: ENABLE or DISABLE.
282 * @retval : None
283 */
I2C_DMACmd(I2C_TypeDef * I2Cx,FunctionalState NewState)284 void I2C_DMACmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
285 {
286     /* Check the parameters */
287     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
288     assert_param(IS_FUNCTIONAL_STATE(NewState));
289     if (NewState != DISABLE)
290     {
291         /* Enable the selected I2C DMA requests */
292         if(I2C_DMA_DIR==TDMAE_SET)
293         {
294             /* Enable the selected I2C TX DMA requests */
295             I2Cx->IC_DMA_CR |= TDMAE_SET;
296         }
297         else if(I2C_DMA_DIR==RDMAE_SET)
298         {
299             /* Enable the selected I2C TX DMA requests */
300             I2Cx->IC_DMA_CR |= RDMAE_SET;
301         }
302     }
303     else
304     {
305         /* Disable the selected I2C DMA requests */
306         I2Cx->IC_DMA_CR &= DMA_CR_TDMAE_RDMAE_Reset;
307     }
308 }
309 
310 
311 /**
312 * @brief  Generates I2Cx communication START condition.
313 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
314 * @param NewState: new state of the I2C START condition generation.
315 *   This parameter can be: ENABLE or DISABLE.
316 * @retval : None.
317 */
I2C_GenerateSTART(I2C_TypeDef * I2Cx,FunctionalState NewState)318 void I2C_GenerateSTART(I2C_TypeDef* I2Cx, FunctionalState NewState)
319 {
320     /* Check the parameters */
321     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
322     assert_param(IS_FUNCTIONAL_STATE(NewState));
323     if (NewState != DISABLE)
324     {
325         /* Generate a START condition */
326         I2Cx->IC_CON |= IC_CON_START_Set;
327     }
328     else
329     {
330         /* Disable the START condition generation */
331         I2Cx->IC_CON &= IC_CON_START_Reset;
332     }
333 }
334 
335 /**
336 * @brief  Generates I2Cx communication STOP condition.
337 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
338 * @param NewState: new state of the I2C STOP condition generation.
339 *   This parameter can be: ENABLE or DISABLE.
340 * @retval : None.
341 */
I2C_GenerateSTOP(I2C_TypeDef * I2Cx,FunctionalState NewState)342 void I2C_GenerateSTOP(I2C_TypeDef* I2Cx, FunctionalState NewState)
343 {
344     u16 overTime = 3000;
345     I2Cx->IC_ENABLE |= 0x02;
346     while(I2Cx->IC_ENABLE&0x02)
347     {
348         if(0 == overTime --)
349         {
350             break;
351         }
352     }
353     I2Cx->IC_CLR_TX_ABRT;
354 }
355 
356 /**
357 * @brief  Configures the specified I2C own address2.
358 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
359 * @param Address: specifies the 7bit I2C own address2.
360 * @retval : None.
361 */
I2C_OwnAddress2Config(I2C_TypeDef * I2Cx,uint8_t Address)362 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
363 //void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address)
364 {
365     uint16_t tmpreg = 0;
366     /* Check the parameters */
367     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
368     /* Get the old register value */
369     tmpreg = I2Cx->IC_TAR;
370     /* Reset I2Cx Own address2 bit [7:0] */
371     tmpreg &= IC_TAR_Reset;
372     /* Set I2Cx Own address2 */
373     tmpreg |= Address>>1;
374     /* Store the new register value */
375     I2Cx->IC_TAR = tmpreg;
376 }
377 
378 /**
379 * @brief  Enables or disables the specified I2C dual addressing mode.
380 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
381 * @param NewState: new state of the I2C dual addressing mode.
382 *   This parameter can be: ENABLE or DISABLE.
383 * @retval : None
384 */
I2C_DualAddressCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)385 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
386 {
387     /* Check the parameters */
388     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
389     assert_param(IS_FUNCTIONAL_STATE(NewState));
390     if (NewState != DISABLE)
391     {
392         /* Enable dual addressing mode */
393         I2Cx->IC_TAR |= IC_TAR_ENDUAL_Set;
394     }
395     else
396     {
397         /* Disable dual addressing mode */
398         I2Cx->IC_TAR &= IC_TAR_ENDUAL_Reset;
399     }
400 }
401 
402 /**
403 * @brief  Enables or disables the specified I2C general call feature.
404 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
405 * @param NewState: new state of the I2C General call.
406 *   This parameter can be: ENABLE or DISABLE.
407 * @retval : None
408 */
I2C_GeneralCallCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)409 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
410 {
411     /* Check the parameters */
412     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
413     assert_param(IS_FUNCTIONAL_STATE(NewState));
414     if (NewState != DISABLE)
415     {
416         /* Enable generall call */
417         I2Cx->IC_TAR |= IC_TAR_GC_Set;
418     }
419     else
420     {
421         /* Disable generall call */
422         I2Cx->IC_TAR &= IC_TAR_GC_Reset;
423     }
424 }
425 
426 /**
427 * @brief  Enables or disables the specified I2C interrupts.
428 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
429 * @param I2C_IT: specifies the I2C interrupts sources to be enabled
430 *   or disabled.
431 *   This parameter can be any combination of the following values:
432 * @arg I2C_IT_RX_UNDER: Rx Buffer is empty interrupt mask
433 * @arg I2C_IT_RX_OVER : RX  Buffer Overrun interrupt mask
434 * @arg I2C_IT_RX_FULL : Rx buffer full interrupt mask
435 * @arg I2C_IT_TX_OVER : TX  Buffer Overrun interrupt mask
436 * @arg I2C_IT_TX_EMPTY	: TX_FIFO empty interrupt mask
437 * @arg I2C_IT_RD_REQ	: I2C work as slave or master interrupt mask
438 * @arg I2C_IT_TX_ABRT	: TX error interrupt  mask(Master mode)
439 * @arg I2C_IT_RX_DONE	: Master not ack interrupt mask(slave mode)
440 * @arg I2C_IT_ACTIVITY	: I2C activity interrupt mask
441 * @arg I2C_IT_STOP_DET	: stop condition  interrupt mask
442 * @arg I2C_IT_START_DET	: start condition  interrupt mask
443 * @arg I2C_IT_GEN_CALL	: a general call address and ack interrupt mask
444 * @param NewState: new state of the specified I2C interrupts.
445 *   This parameter can be: ENABLE or DISABLE.
446 * @retval : None
447 */
I2C_ITConfig(I2C_TypeDef * I2Cx,uint16_t I2C_IT,FunctionalState NewState)448 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
449 {
450     /* Check the parameters */
451     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
452     assert_param(IS_FUNCTIONAL_STATE(NewState));
453     assert_param(IS_I2C_CONFIG_IT(I2C_IT));
454 
455     if(I2C_IT == I2C_IT_RX_FULL)
456     {
457         I2Cx->IC_DATA_CMD = CMD_READ;
458     }
459 
460     if (NewState != DISABLE)
461     {
462         /* Enable the selected I2C interrupts */
463         I2Cx->IC_INTR_MASK |= I2C_IT;
464     }
465     else
466     {
467         /* Disable the selected I2C interrupts */
468         I2Cx->IC_INTR_MASK &= (uint16_t)~I2C_IT;
469     }
470 }
471 
472 /**
473 * @brief  Sends a data byte through the I2Cx peripheral.
474 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
475 * @param Data: Byte to be transmitted..
476 * @retval : None
477 */
I2C_SendData(I2C_TypeDef * I2Cx,uint8_t Data)478 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
479 {
480     /* Check the parameters */
481     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
482     /* Write in the IC_DATA_CMD register the data to be sent */
483     I2Cx->IC_DATA_CMD = CMD_WRITE | Data;
484 }
485 
486 /**
487 * @brief  Returns the most recent received data by the I2Cx peripheral.
488 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
489 * @retval : The value of the received data.
490 
491 */
I2C_ReadCmd(I2C_TypeDef * I2Cx)492 void I2C_ReadCmd(I2C_TypeDef* I2Cx)
493 {
494     /* Check the parameters */
495     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
496 
497     I2Cx->IC_DATA_CMD = CMD_READ;
498 }
499 
500 /**
501 * @brief  Returns the most recent received data by the I2Cx peripheral.
502 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
503 * @retval : The value of the received data.
504 */
I2C_ReceiveData(I2C_TypeDef * I2Cx)505 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
506 {
507     /* Check the parameters */
508     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
509     I2C_CMD_DIR= 0;
510     /* Return the data in the IC_DATA_CMD register */
511     return (uint8_t)I2Cx->IC_DATA_CMD;
512 }
513 
514 /**
515 * @brief  Transmits the address byte to select the slave device.
516 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
517 * @param Address: specifies the slave address which will be transmitted
518 * @param I2C_Direction: specifies whether the I2C device will be a
519 *   Transmitter or a Receiver.
520 *   This parameter can be one of the following values
521 * @arg I2C_Direction_Transmitter: Transmitter mode
522 * @arg I2C_Direction_Receiver: Receiver mode
523 * @retval : None.
524 */
I2C_Send7bitAddress(I2C_TypeDef * I2Cx,uint8_t Address,uint8_t I2C_Direction)525 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
526 {
527     /* Store the new register value */
528     I2Cx->IC_TAR = Address>>1;
529 }
530 
531 /**
532 * @brief  Reads the specified I2C register and returns its value.
533 * @param I2C_Register: specifies the register to read.
534 *   This parameter can be one of the following values:
535 * @retval : The value of the read register.
536 */
I2C_ReadRegister(I2C_TypeDef * I2Cx,uint8_t I2C_Register)537 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
538 {
539     /* Check the parameters */
540     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
541     assert_param(IS_I2C_REGISTER(I2C_Register));
542     /* Return the selected register value */
543     return (*(__IO uint16_t *)(*((__IO uint32_t *)&I2Cx) + I2C_Register));
544 }
545 
546 /**
547 * @brief  Returns the last I2Cx Event.
548 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
549 * @retval : The last event
550 */
I2C_GetLastEvent(I2C_TypeDef * I2Cx)551 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)
552 {
553     uint32_t lastevent = 0;
554     uint32_t flag1 = 0;
555     /* Check the parameters */
556     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
557     /* Read the I2Cx status register */
558     flag1 = I2Cx->IC_RAW_INTR_STAT;
559 
560     /* Get the last event value from I2C status register */
561     lastevent = (flag1 ) & FLAG_Mask;
562     /* Return status */
563     return lastevent;
564 }
565 
566 
567 
568 /**
569 * @brief  Checks whether the last I2Cx Event is equal to the one passed
570 *   as parameter.
571 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
572 * @param I2C_EVENT: specifies the event to be checked.
573 *   This parameter can be one of the following values:
574 * @arg I2C_EVENT_RX_UNDER: Rx Buffer is empty event
575 * @arg I2C_EVENT_RX_OVER : RX  Buffer Overrun event
576 * @arg I2C_EVENTT_RX_FULL : Rx buffer full event
577 * @arg I2C_EVENT_TX_OVER : TX  Buffer Overrun event
578 * @arg I2C_EVENT_TX_EMPTY	: TX_FIFO empty event
579 * @arg I2C_EVENT_RD_REQ	: I2C work as slave or master event
580 * @arg I2C_EVENT_TX_ABRT	: TX error event(Master mode)
581 * @arg I2C_EVENT_RX_DONE	: Master not ack event(slave mode)
582 * @arg I2C_EVENT_ACTIVITY	: I2C activity event
583 * @arg I2C_EVENT_STOP_DET	: stop condition  event
584 * @arg I2C_EVENT_START_DET	: start condition  event
585 * @arg I2C_EVENT_GEN_CALL	: a general call address and ack event
586 * - SUCCESS: Last event is equal to the I2C_EVENT
587 * - ERROR: Last event is different from the I2C_EVENT
588 */
I2C_CheckEvent(I2C_TypeDef * I2Cx,uint32_t I2C_EVENT)589 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
590 {
591     uint32_t lastevent = 0;
592     uint32_t flag1 = 0;
593     ErrorStatus status = ERROR;
594     /* Check the parameters */
595     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
596     assert_param(IS_I2C_EVENT(I2C_EVENT));
597 
598     if((I2C_EVENT == I2C_EVENT_RX_FULL)&&(I2C_CMD_DIR==0))
599     {
600         I2Cx->IC_DATA_CMD = CMD_READ;
601         I2C_CMD_DIR = 1;
602     }
603     /* Read the I2Cx status register */
604     flag1 = I2Cx->IC_RAW_INTR_STAT;
605     //flag1 = I2Cx->IC_INTR_STAT;
606     /* Get the last event value from I2C status register */
607     lastevent = (flag1 ) & I2C_EVENT;
608 
609     /* Check whether the last event is equal to I2C_EVENT */
610     if (lastevent == I2C_EVENT )
611         //if((I2Cx->IC_RAW_INTR_STAT & I2C_EVENT) != (uint32_t)RESET)
612     {
613         /* SUCCESS: last event is equal to I2C_EVENT */
614         status = SUCCESS;
615     }
616     else
617     {
618         /* ERROR: last event is different from I2C_EVENT */
619         status = ERROR;
620     }
621     /* Return status */
622     return status;
623 
624 }
625 
626 /**
627 * @brief  Checks whether the specified I2C flag is set or not.
628 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
629 * @param I2C_FLAG: specifies the flag to check.
630 *   This parameter can be one of the following values:
631 * @arg I2C_FLAG_RX_UNDER: Rx Buffer is empty flag
632 * @arg I2C_FLAG_RX_OVER : RX  Buffer Overrun flag
633 * @arg I2C_FLAG_RX_FULL : Rx buffer full flag
634 * @arg I2C_FLAG_TX_OVER : TX  Buffer Overrun flag
635 * @arg I2C_FLAG_TX_EMPTY: TX_FIFO empty flag
636 * @arg I2C_FLAG_RD_REQ	: I2C work as slave or master flag
637 * @arg I2C_FLAG_TX_ABRT	: TX error flag(Master mode)
638 * @arg I2C_FLAG_RX_DONE	: Master not ack flag(slave mode)
639 * @arg I2C_FLAG_ACTIVITY: I2C activity flag
640 * @arg I2C_FLAG_STOP_DET: stop condition  flag
641 * @arg I2C_FLAG_START_DET: start condition  flag
642 * @arg I2C_FLAG_GEN_CALL : a general call address and ack flag
643 * @retval : The new state of I2C_FLAG (SET or RESET).
644 */
I2C_GetFlagStatus(I2C_TypeDef * I2Cx,uint32_t I2C_FLAG)645 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
646 {
647     FlagStatus bitstatus = RESET;
648     __IO uint32_t i2creg = 0, i2cxbase = 0;
649     /* Check the parameters */
650     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
651     assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
652 
653     if(I2C_FLAG & 0x8000)
654     {
655         if((I2Cx->IC_STATUS & I2C_FLAG) != (uint32_t)RESET)
656         {
657             /* I2C_FLAG is set */
658             bitstatus = SET;
659         }
660         else
661         {
662             /* I2C_FLAG is reset */
663             bitstatus = RESET;
664         }
665     }
666     else
667     {
668         if((I2C_FLAG == I2C_FLAG_RX_FULL)&&(I2C_CMD_DIR==0))
669         {
670             I2Cx->IC_DATA_CMD = CMD_READ;
671             I2C_CMD_DIR = 1;
672         }
673         /* Check the status of the specified I2C flag */
674         if((I2Cx->IC_RAW_INTR_STAT & I2C_FLAG) != (uint32_t)RESET)
675         {
676             /* I2C_FLAG is set */
677             bitstatus = SET;
678         }
679         else
680         {
681             /* I2C_FLAG is reset */
682             bitstatus = RESET;
683         }
684     }
685 
686     /* Return the I2C_FLAG status */
687     return  bitstatus;
688 }
689 
690 /**
691 * @brief  Clears the I2Cx's pending flags.
692 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
693 * @param I2C_FLAG: specifies the flag to clear.
694 *   This parameter can be any combination of the following values:
695 * @arg I2C_FLAG_RX_UNDER: Rx Buffer is empty flag
696 * @arg I2C_FLAG_RX_OVER : RX  Buffer Overrun flag
697 * @arg I2C_FLAG_RX_FULL : Rx buffer full flag
698 * @arg I2C_FLAG_TX_OVER : TX  Buffer Overrun flag
699 * @arg I2C_FLAG_TX_EMPTY: TX_FIFO empty flag
700 * @arg I2C_FLAG_RD_REQ	: I2C work as slave or master flag
701 * @arg I2C_FLAG_TX_ABRT	: TX error flag(Master mode)
702 * @arg I2C_FLAG_RX_DONE	: Master not ack flag(slave mode)
703 * @arg I2C_FLAG_ACTIVITY: I2C activity flag
704 * @arg I2C_FLAG_STOP_DET: stop condition  flag
705 * @arg I2C_FLAG_START_DET: start condition  flag
706 * @arg I2C_FLAG_GEN_CALL : a general call address and ack flag
707 * @retval : None
708 */
I2C_ClearFlag(I2C_TypeDef * I2Cx,uint32_t I2C_FLAG)709 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
710 {
711     /* Check the parameters */
712     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
713     assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
714 
715     if((I2C_FLAG & I2C_FLAG_RX_UNDER) 	== I2C_FLAG_RX_UNDER) {I2Cx->IC_CLR_RX_UNDER;}
716     if((I2C_FLAG & I2C_FLAG_RX_OVER)	== I2C_FLAG_RX_OVER)  {I2Cx->IC_CLR_RX_OVER;}
717     if((I2C_FLAG & I2C_FLAG_TX_OVER) 	== I2C_FLAG_TX_OVER)  {I2Cx->IC_CLR_TX_OVER;}
718     if((I2C_FLAG & I2C_FLAG_RD_REQ) 	== I2C_FLAG_RD_REQ)   {I2Cx->IC_CLR_RD_REQ;}
719     if((I2C_FLAG & I2C_FLAG_TX_ABRT) 	== I2C_FLAG_TX_ABRT)  {I2Cx->IC_CLR_TX_ABRT;}
720     if((I2C_FLAG & I2C_FLAG_RX_DONE) 	== I2C_FLAG_RX_DONE)  {I2Cx->IC_CLR_RX_DONE;}
721     if((I2C_FLAG & I2C_FLAG_ACTIVITY) 	== I2C_FLAG_ACTIVITY) {I2Cx->IC_CLR_ACTIVITY;}
722     if((I2C_FLAG & I2C_FLAG_STOP_DET) 	== I2C_FLAG_STOP_DET) {I2Cx->IC_CLR_STOP_DET;}
723     if((I2C_FLAG & I2C_FLAG_START_DET) 	== I2C_FLAG_START_DET){I2Cx->IC_CLR_START_DET;}
724     if((I2C_FLAG & I2C_FLAG_GEN_CALL) 	== I2C_FLAG_GEN_CALL) {I2Cx->IC_CLR_GEN_CALL;}
725 }
726 
727 /**
728 * @brief  Checks whether the specified I2C interrupt has occurred or not.
729 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
730 * @param I2C_IT: specifies the interrupt source to check.
731 *   This parameter can be one of the following values:
732 * @arg I2C_IT_RX_UNDER: Rx Buffer is empty interrupt
733 * @arg I2C_IT_RX_OVER : RX  Buffer Overrun interrupt
734 * @arg I2C_IT_RX_FULL : Rx buffer full interrupt
735 * @arg I2C_IT_TX_OVER : TX  Buffer Overrun interrupt
736 * @arg I2C_IT_TX_EMPTY	: TX_FIFO empty interrupt
737 * @arg I2C_IT_RD_REQ	: I2C work as slave or master interrupt
738 * @arg I2C_IT_TX_ABRT	: TX error interrupt  (Master mode)
739 * @arg I2C_IT_RX_DONE	: Master not ack interrupt (slave mode)
740 * @arg I2C_IT_ACTIVITY	: I2C activity interrupt
741 * @arg I2C_IT_STOP_DET	: stop condition  interrupt
742 * @arg I2C_IT_START_DET	: start condition  interrupt
743 * @arg I2C_IT_GEN_CALL	: a general call address and ack interrupt
744 * @retval : The new state of I2C_IT (SET or RESET).
745 */
I2C_GetITStatus(I2C_TypeDef * I2Cx,uint32_t I2C_IT)746 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
747 {
748     ITStatus bitstatus = RESET;
749     /* Check the parameters */
750     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
751     assert_param(IS_I2C_GET_IT(I2C_IT));
752 
753     /* Check the status of the specified I2C flag */
754     if((I2Cx->IC_RAW_INTR_STAT & I2C_IT) != (uint32_t)RESET)
755     {
756         /* I2C_IT is set */
757         bitstatus = SET;
758     }
759     else
760     {
761         /* I2C_IT is reset */
762         bitstatus = RESET;
763     }
764 
765     /* Return the I2C_IT status */
766     return  bitstatus;
767 }
768 
769 /**
770 * @brief  Clears the I2Cx interrupt pending bits.
771 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
772 * @param I2C_IT: specifies the interrupt pending bit to clear.
773 *   This parameter can be any combination of the following values:
774 * @arg I2C_IT_RX_UNDER: Rx Buffer is empty interrupt
775 * @arg I2C_IT_RX_OVER : RX  Buffer Overrun interrupt
776 * @arg I2C_IT_RX_FULL : Rx buffer full interrupt
777 * @arg I2C_IT_TX_OVER : TX  Buffer Overrun interrupt
778 * @arg I2C_IT_TX_EMPTY	: TX_FIFO empty interrupt
779 * @arg I2C_IT_RD_REQ	: I2C work as slave or master interrupt
780 * @arg I2C_IT_TX_ABRT	: TX error interrupt  (Master mode)
781 * @arg I2C_IT_RX_DONE	: Master not ack interrupt (slave mode)
782 * @arg I2C_IT_ACTIVITY	: I2C activity interrupt
783 * @arg I2C_IT_STOP_DET	: stop condition  interrupt
784 * @arg I2C_IT_START_DET	: start condition  interrupt
785 * @arg I2C_IT_GEN_CALL	: a general call address and ack interrupt
786 * @retval : None
787 */
I2C_ClearITPendingBit(I2C_TypeDef * I2Cx,uint32_t I2C_IT)788 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
789 {
790     /* Check the parameters */
791     assert_param(IS_I2C_ALL_PERIPH(I2Cx));
792     assert_param(IS_I2C_CLEAR_IT(I2C_IT));
793 
794     if((I2C_IT & I2C_IT_RX_UNDER) 	== I2C_FLAG_RX_UNDER) {I2Cx->IC_CLR_RX_UNDER;}
795     if((I2C_IT & I2C_IT_RX_OVER)	== I2C_FLAG_RX_OVER)  {I2Cx->IC_CLR_RX_OVER;}
796     if((I2C_IT & I2C_IT_TX_OVER) 	== I2C_FLAG_TX_OVER)  {I2Cx->IC_CLR_TX_OVER;}
797     if((I2C_IT & I2C_IT_RD_REQ) 	== I2C_FLAG_RD_REQ)   {I2Cx->IC_CLR_RD_REQ;}
798     if((I2C_IT & I2C_IT_TX_ABRT) 	== I2C_FLAG_TX_ABRT)  {I2Cx->IC_CLR_TX_ABRT;}
799     if((I2C_IT & I2C_IT_RX_DONE) 	== I2C_FLAG_RX_DONE)  {I2Cx->IC_CLR_RX_DONE;}
800     if((I2C_IT & I2C_IT_ACTIVITY) 	== I2C_FLAG_ACTIVITY) {I2Cx->IC_CLR_ACTIVITY;}
801     if((I2C_IT & I2C_IT_STOP_DET) 	== I2C_FLAG_STOP_DET) {I2Cx->IC_CLR_STOP_DET;}
802     if((I2C_IT & I2C_IT_START_DET) 	== I2C_FLAG_START_DET){I2Cx->IC_CLR_START_DET;}
803     if((I2C_IT & I2C_IT_GEN_CALL) 	== I2C_FLAG_GEN_CALL) {I2Cx->IC_CLR_GEN_CALL;}
804 }
805 
806 /**
807 * @}
808 */
809 
810 /**
811 * @}
812 */
813 
814 /**
815 * @}
816 */
817 
818 /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/
819