1 /**
2 ******************************************************************************
3 * @file HAL_i2c.c
4 * @author AE Team
5 * @version V2.0.0
6 * @date 22/08/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>© COPYRIGHT 2017 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 uint16_t I2C_DMA_DIR = 0;
82
83 /**
84 * @}
85 */
86
87 /** @defgroup I2C_Private_Macros
88 * @{
89 */
90
91 /**
92 * @}
93 */
94
95 /** @defgroup I2C_Private_Variables
96 * @{
97 */
98
99 /**
100 * @}
101 */
102
103 /** @defgroup I2C_Private_FunctionPrototypes
104 * @{
105 */
106
107 /**
108 * @}
109 */
110
111 /** @defgroup I2C_Private_Functions
112 * @{
113 */
114
115 /**
116 * @brief Deinitializes the I2Cx peripheral registers to their default
117 * reset values.
118 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
119 * @retval : None
120 */
I2C_DeInit(I2C_TypeDef * I2Cx)121 void I2C_DeInit(I2C_TypeDef* I2Cx)
122 {
123 /* Check the parameters */
124 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
125 switch (*(uint32_t*)&I2Cx)
126 {
127 case I2C1_BASE:
128 /* Enable I2C1 reset state */
129 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, ENABLE);
130 /* Release I2C1 from reset state */
131 RCC_APB1PeriphResetCmd(RCC_APB1Periph_I2C1, DISABLE);
132 break;
133
134 default:
135 break;
136 }
137 }
138
139 /**
140 * @brief Initializes the I2Cx peripheral according to the specified
141 * parameters in the I2C_InitStruct.
142 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
143 * @param I2C_InitStruct: pointer to a I2C_InitTypeDef structure that
144 * contains the configuration information for the specified
145 * I2C peripheral.
146 * @retval : None
147 */
I2C_Init(I2C_TypeDef * I2Cx,I2C_InitTypeDef * I2C_InitStruct)148 void I2C_Init(I2C_TypeDef* I2Cx, I2C_InitTypeDef* I2C_InitStruct)
149 {
150
151 uint16_t tmpreg = 0;
152 uint32_t pclk1 = 8000000;
153 /* uint32_t minSclLowTime = 0; */
154 uint32_t i2cPeriod = 0;
155 uint32_t pclk1Period = 0;
156 RCC_ClocksTypeDef rcc_clocks;
157 /* Check the parameters */
158 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
159 assert_param(IS_I2C_MODE(I2C_InitStruct->I2C_Mode));
160 assert_param(IS_I2C_CLOCK_SPEED(I2C_InitStruct->I2C_ClockSpeed));
161 /*---------------------------- I2Cx IC_ENABLE Configuration ------------------------*/
162 /* Disable the selected I2C peripheral */
163 I2Cx->IC_ENABLE &= IC_ENABLE_Reset;
164
165 /* Get pclk1 frequency value */
166 RCC_GetClocksFreq(&rcc_clocks);
167 pclk1 = rcc_clocks.PCLK1_Frequency;
168
169 /* Set pclk1 period value */
170 pclk1Period = 1000000000/pclk1;
171
172 i2cPeriod = 1000000000/I2C_InitStruct->I2C_ClockSpeed; /*ns unit*/
173 tmpreg = 0;
174 /* Configure speed in standard mode */
175 if (I2C_InitStruct->I2C_ClockSpeed <= 100000)
176 {
177 tmpreg = (i2cPeriod/pclk1Period)/2;
178 I2Cx->IC_SS_SCL_LCNT = tmpreg;
179 tmpreg = (i2cPeriod - pclk1Period*I2Cx->IC_SS_SCL_LCNT)/pclk1Period;
180 /* Write to I2Cx IC_SS_SCL_HCNT */
181 I2Cx->IC_SS_SCL_HCNT = tmpreg;
182
183
184 }
185 else /* (I2C_InitStruct->I2C_ClockSpeed <= 400000) */
186 {
187 tmpreg = (i2cPeriod/pclk1Period)/2;
188 I2Cx->IC_FS_SCL_LCNT = tmpreg;
189 tmpreg = (i2cPeriod - pclk1Period*I2Cx->IC_FS_SCL_LCNT)/pclk1Period;
190 /* Write to I2Cx IC_FS_SCL_HCNT */
191 I2Cx->IC_FS_SCL_HCNT = tmpreg;
192 }
193
194 /* Get the I2Cx IC_CON value */
195 tmpreg = I2Cx->IC_CON;
196 /* Clear TX_EMPTY_CTRL,IC_SLAVE_DISABLE,IC_RESTART_EN,IC_10BITADDR_SLAVE,SPEED,MASTER_MODE bits */
197 tmpreg &= IC_CON_RESET;
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 u8 tempReg=0;
333 tempReg=tempReg;
334 I2Cx->IC_ENABLE |= 0x02;
335 while(I2Cx->IC_ENABLE&0x02);
336 tempReg = I2Cx->IC_CLR_TX_ABRT;
337 }
338
339 /**
340 * @brief Configures the specified I2C own address2.
341 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
342 * @param Address: specifies the 7bit I2C own address2.
343 * @retval : None.
344 */
I2C_OwnAddress2Config(I2C_TypeDef * I2Cx,uint8_t Address)345 void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint8_t Address)
346 /* void I2C_OwnAddress2Config(I2C_TypeDef* I2Cx, uint16_t Address) */
347 {
348 uint16_t tmpreg = 0;
349 /* Check the parameters */
350 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
351 /* Get the old register value */
352 tmpreg = I2Cx->IC_TAR;
353 /* Reset I2Cx Own address2 bit [7:0] */
354 tmpreg &= IC_TAR_Reset;
355 /* Set I2Cx Own address2 */
356 tmpreg |= Address>>1;
357 /* Store the new register value */
358 I2Cx->IC_TAR = tmpreg;
359 }
360
361 /**
362 * @brief Enables or disables the specified I2C dual addressing mode.
363 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
364 * @param NewState: new state of the I2C dual addressing mode.
365 * This parameter can be: ENABLE or DISABLE.
366 * @retval : None
367 */
I2C_DualAddressCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)368 void I2C_DualAddressCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
369 {
370 /* Check the parameters */
371 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
372 assert_param(IS_FUNCTIONAL_STATE(NewState));
373 if (NewState != DISABLE)
374 {
375 /* Enable dual addressing mode */
376 I2Cx->IC_TAR |= IC_TAR_ENDUAL_Set;
377 }
378 else
379 {
380 /* Disable dual addressing mode */
381 I2Cx->IC_TAR &= IC_TAR_ENDUAL_Reset;
382 }
383 }
384
385 /**
386 * @brief Enables or disables the specified I2C general call feature.
387 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
388 * @param NewState: new state of the I2C General call.
389 * This parameter can be: ENABLE or DISABLE.
390 * @retval : None
391 */
I2C_GeneralCallCmd(I2C_TypeDef * I2Cx,FunctionalState NewState)392 void I2C_GeneralCallCmd(I2C_TypeDef* I2Cx, FunctionalState NewState)
393 {
394 /* Check the parameters */
395 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
396 assert_param(IS_FUNCTIONAL_STATE(NewState));
397 if (NewState != DISABLE)
398 {
399 /* Enable generall call */
400 I2Cx->IC_TAR |= IC_TAR_GC_Set;
401 }
402 else
403 {
404 /* Disable generall call */
405 I2Cx->IC_TAR &= IC_TAR_GC_Reset;
406 }
407 }
408
409 /**
410 * @brief Enables or disables the specified I2C interrupts.
411 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
412 * @param I2C_IT: specifies the I2C interrupts sources to be enabled
413 * or disabled.
414 * This parameter can be any combination of the following values:
415 * @arg I2C_IT_RX_UNDER: Rx Buffer is empty interrupt mask
416 * @arg I2C_IT_RX_OVER : RX Buffer Overrun interrupt mask
417 * @arg I2C_IT_RX_FULL : Rx buffer full interrupt mask
418 * @arg I2C_IT_TX_OVER : TX Buffer Overrun interrupt mask
419 * @arg I2C_IT_TX_EMPTY : TX_FIFO empty interrupt mask
420 * @arg I2C_IT_RD_REQ : I2C work as slave or master interrupt mask
421 * @arg I2C_IT_TX_ABRT : TX error interrupt mask(Master mode)
422 * @arg I2C_IT_RX_DONE : Master not ack interrupt mask(slave mode)
423 * @arg I2C_IT_ACTIVITY : I2C activity interrupt mask
424 * @arg I2C_IT_STOP_DET : stop condition interrupt mask
425 * @arg I2C_IT_START_DET : start condition interrupt mask
426 * @arg I2C_IT_GEN_CALL : a general call address and ack interrupt mask
427 * @param NewState: new state of the specified I2C interrupts.
428 * This parameter can be: ENABLE or DISABLE.
429 * @retval : None
430 */
I2C_ITConfig(I2C_TypeDef * I2Cx,uint16_t I2C_IT,FunctionalState NewState)431 void I2C_ITConfig(I2C_TypeDef* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
432 {
433 /* Check the parameters */
434 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
435 assert_param(IS_FUNCTIONAL_STATE(NewState));
436 assert_param(IS_I2C_CONFIG_IT(I2C_IT));
437
438 if(I2C_IT == I2C_IT_RX_FULL)
439 {
440 I2Cx->IC_DATA_CMD = CMD_READ;
441 }
442
443 if (NewState != DISABLE)
444 {
445 /* Enable the selected I2C interrupts */
446 I2Cx->IC_INTR_MASK |= I2C_IT;
447 }
448 else
449 {
450 /* Disable the selected I2C interrupts */
451 I2Cx->IC_INTR_MASK &= (uint16_t)~I2C_IT;
452 }
453 }
454
455 /**
456 * @brief Sends a data byte through the I2Cx peripheral.
457 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
458 * @param Data: Byte to be transmitted..
459 * @retval : None
460 */
I2C_SendData(I2C_TypeDef * I2Cx,uint8_t Data)461 void I2C_SendData(I2C_TypeDef* I2Cx, uint8_t Data)
462 {
463 /* Check the parameters */
464 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
465 /* Write in the IC_DATA_CMD register the data to be sent */
466 I2Cx->IC_DATA_CMD = CMD_WRITE | Data;
467 }
468
469 /**
470 * @brief Sends last data byte through the I2Cx peripheral.
471 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
472 * @param Data: Byte to be transmitted..
473 * @retval : None
474 */
I2C_SendLastDataOrStop(I2C_TypeDef * I2Cx,uint8_t Data)475 void I2C_SendLastDataOrStop(I2C_TypeDef* I2Cx, uint8_t Data)
476 {
477 /* Check the parameters */
478 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
479 /* Write in the IC_DATA_CMD register the data to be sent */
480 I2Cx->IC_DATA_CMD = 0x200 | Data;
481 }
482
483 /**
484 * @brief Read last data byte through the I2Cx peripheral.
485 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
486 * @param Data: Byte to be Recieved..
487 * @retval : None
488 */
I2C_ReadLastDataOrStop(I2C_TypeDef * I2Cx)489 uint8_t I2C_ReadLastDataOrStop(I2C_TypeDef* I2Cx)
490 {
491 /* Check the parameters */
492 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
493 /* Write in the IC_DATA_CMD register the data to be sent */
494 I2Cx->IC_DATA_CMD = 0x300 ;
495 return 0;
496 }
497 /**
498 * @brief Returns the most recent received data by the I2Cx peripheral.
499 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
500 * @retval : The value of the received data.
501
502 */
I2C_ReadCmd(I2C_TypeDef * I2Cx)503 void I2C_ReadCmd(I2C_TypeDef* I2Cx)
504 {
505 /* Check the parameters */
506 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
507
508 I2Cx->IC_DATA_CMD = CMD_READ;
509 }
510
511 /**
512 * @brief Returns the most recent received data by the I2Cx peripheral.
513 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
514 * @retval : The value of the received data.
515 */
I2C_ReceiveData(I2C_TypeDef * I2Cx)516 uint8_t I2C_ReceiveData(I2C_TypeDef* I2Cx)
517 {
518 /* Check the parameters */
519 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
520 I2C_CMD_DIR= 0;
521 /* Return the data in the IC_DATA_CMD register */
522 return (uint8_t)I2Cx->IC_DATA_CMD;
523 }
524
525 /**
526 * @brief Transmits the address byte to select the slave device.
527 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
528 * @param Address: specifies the slave address which will be transmitted
529 * @param I2C_Direction: specifies whether the I2C device will be a
530 * Transmitter or a Receiver.
531 * This parameter can be one of the following values
532 * @arg I2C_Direction_Transmitter: Transmitter mode
533 * @arg I2C_Direction_Receiver: Receiver mode
534 * @retval : None.
535 */
I2C_Send7bitAddress(I2C_TypeDef * I2Cx,uint8_t Address,uint8_t I2C_Direction)536 void I2C_Send7bitAddress(I2C_TypeDef* I2Cx, uint8_t Address, uint8_t I2C_Direction)
537 {
538 /* Store the new register value */
539 I2Cx->IC_TAR = Address>>1;
540 }
541
542 /**
543 * @brief Reads the specified I2C register and returns its value.
544 * @param I2C_Register: specifies the register to read.
545 * This parameter can be one of the following values:
546 * @retval : The value of the read register.
547 */
I2C_ReadRegister(I2C_TypeDef * I2Cx,uint8_t I2C_Register)548 uint16_t I2C_ReadRegister(I2C_TypeDef* I2Cx, uint8_t I2C_Register)
549 {
550 /* Check the parameters */
551 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
552 assert_param(IS_I2C_REGISTER(I2C_Register));
553 /* Return the selected register value */
554 return (*(__IO uint16_t *)(*((__IO uint32_t *)&I2Cx) + I2C_Register));
555 }
556
557 /**
558 * @brief Returns the last I2Cx Event.
559 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
560 * @retval : The last event
561 */
I2C_GetLastEvent(I2C_TypeDef * I2Cx)562 uint32_t I2C_GetLastEvent(I2C_TypeDef* I2Cx)
563 {
564 uint32_t lastevent = 0;
565 uint32_t flag1 = 0;
566 /* Check the parameters */
567 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
568 /* Read the I2Cx status register */
569 flag1 = I2Cx->IC_RAW_INTR_STAT;
570
571 /* Get the last event value from I2C status register */
572 lastevent = (flag1 ) & FLAG_Mask;
573 /* Return status */
574 return lastevent;
575 }
576
577
578
579 /**
580 * @brief Checks whether the last I2Cx Event is equal to the one passed
581 * as parameter.
582 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
583 * @param I2C_EVENT: specifies the event to be checked.
584 * This parameter can be one of the following values:
585 * @arg I2C_EVENT_RX_UNDER: Rx Buffer is empty event
586 * @arg I2C_EVENT_RX_OVER : RX Buffer Overrun event
587 * @arg I2C_EVENTT_RX_FULL : Rx buffer full event
588 * @arg I2C_EVENT_TX_OVER : TX Buffer Overrun event
589 * @arg I2C_EVENT_TX_EMPTY : TX_FIFO empty event
590 * @arg I2C_EVENT_RD_REQ : I2C work as slave or master event
591 * @arg I2C_EVENT_TX_ABRT : TX error event(Master mode)
592 * @arg I2C_EVENT_RX_DONE : Master not ack event(slave mode)
593 * @arg I2C_EVENT_ACTIVITY : I2C activity event
594 * @arg I2C_EVENT_STOP_DET : stop condition event
595 * @arg I2C_EVENT_START_DET : start condition event
596 * @arg I2C_EVENT_GEN_CALL : a general call address and ack event
597 * - SUCCESS: Last event is equal to the I2C_EVENT
598 * - ERROR: Last event is different from the I2C_EVENT
599 */
I2C_CheckEvent(I2C_TypeDef * I2Cx,uint32_t I2C_EVENT)600 ErrorStatus I2C_CheckEvent(I2C_TypeDef* I2Cx, uint32_t I2C_EVENT)
601 {
602 uint32_t lastevent = 0;
603 uint32_t flag1 = 0;
604 ErrorStatus status = ERROR;
605 /* Check the parameters */
606 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
607 assert_param(IS_I2C_EVENT(I2C_EVENT));
608
609 if((I2C_EVENT == I2C_EVENT_RX_FULL)&&(I2C_CMD_DIR==0))
610 {
611 I2Cx->IC_DATA_CMD = CMD_READ;
612 I2C_CMD_DIR = 1;
613 }
614 /* Read the I2Cx status register */
615 flag1 = I2Cx->IC_RAW_INTR_STAT;
616 /* flag1 = I2Cx->IC_INTR_STAT; */
617 /* Get the last event value from I2C status register */
618 lastevent = (flag1 ) & I2C_EVENT;
619
620 /* Check whether the last event is equal to I2C_EVENT */
621 if (lastevent == I2C_EVENT )
622 /* if((I2Cx->IC_RAW_INTR_STAT & I2C_EVENT) != (uint32_t)RESET) */
623 {
624 /* SUCCESS: last event is equal to I2C_EVENT */
625 status = SUCCESS;
626 }
627 else
628 {
629 /* ERROR: last event is different from I2C_EVENT */
630 status = ERROR;
631 }
632 /* Return status */
633 return status;
634
635 }
636
637 /**
638 * @brief Checks whether the specified I2C flag is set or not.
639 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
640 * @param I2C_FLAG: specifies the flag to check.
641 * This parameter can be one of the following values:
642 * @arg I2C_FLAG_RX_UNDER: Rx Buffer is empty flag
643 * @arg I2C_FLAG_RX_OVER : RX Buffer Overrun flag
644 * @arg I2C_FLAG_RX_FULL : Rx buffer full flag
645 * @arg I2C_FLAG_TX_OVER : TX Buffer Overrun flag
646 * @arg I2C_FLAG_TX_EMPTY: TX_FIFO empty flag
647 * @arg I2C_FLAG_RD_REQ : I2C work as slave or master flag
648 * @arg I2C_FLAG_TX_ABRT : TX error flag(Master mode)
649 * @arg I2C_FLAG_RX_DONE : Master not ack flag(slave mode)
650 * @arg I2C_FLAG_ACTIVITY: I2C activity flag
651 * @arg I2C_FLAG_STOP_DET: stop condition flag
652 * @arg I2C_FLAG_START_DET: start condition flag
653 * @arg I2C_FLAG_GEN_CALL : a general call address and ack flag
654 * @arg I2C_STATUS_FLAG_ACTIVITY
655 * @arg I2C_STATUS_FLAG_TFNF
656 * @arg I2C_STATUS_FLAG_TFE
657 * @arg I2C_STATUS_FLAG_RFNE
658 * @arg I2C_STATUS_FLAG_RFF
659 * @arg I2C_STATUS_FLAG_M_ACTIVITY
660 * @arg I2C_STATUS_FLAG_S_ACTIVITY
661 * @retval : The new state of I2C_FLAG (SET or RESET).
662 */
I2C_GetFlagStatus(I2C_TypeDef * I2Cx,uint32_t I2C_FLAG)663 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
664 {
665 FlagStatus bitstatus = RESET;
666 __IO uint32_t i2creg = 0, i2cxbase = 0;
667 ((void)i2creg);
668 ((void)i2cxbase);
669
670 /* Check the parameters */
671 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
672 assert_param(IS_I2C_GET_FLAG(I2C_FLAG));
673
674 if(I2C_FLAG & 0x8000)
675 {
676 if((I2Cx->IC_STATUS & I2C_FLAG) != (uint32_t)RESET)
677 {
678 /* I2C_FLAG is set */
679 bitstatus = SET;
680 }
681 else
682 {
683 /* I2C_FLAG is reset */
684 bitstatus = RESET;
685 }
686 }
687 else
688 {
689 if((I2C_FLAG == I2C_FLAG_RX_FULL)&&(I2C_CMD_DIR==0))
690 {
691 I2Cx->IC_DATA_CMD = CMD_READ;
692 I2C_CMD_DIR = 1;
693 }
694 /* Check the status of the specified I2C flag */
695 if((I2Cx->IC_RAW_INTR_STAT & I2C_FLAG) != (uint32_t)RESET)
696 {
697 /* I2C_FLAG is set */
698 bitstatus = SET;
699 }
700 else
701 {
702 /* I2C_FLAG is reset */
703 bitstatus = RESET;
704 }
705 }
706 /* Return the I2C_FLAG status */
707 return bitstatus;
708 }
709
710 /**
711 * @brief Clears the I2Cx's pending flags.
712 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
713 * @param I2C_FLAG: specifies the flag to clear.
714 * This parameter can be any combination of the following values:
715 * @arg I2C_FLAG_RX_UNDER: Rx Buffer is empty flag
716 * @arg I2C_FLAG_RX_OVER : RX Buffer Overrun flag
717 * @arg I2C_FLAG_RX_FULL : Rx buffer full flag
718 * @arg I2C_FLAG_TX_OVER : TX Buffer Overrun flag
719 * @arg I2C_FLAG_TX_EMPTY: TX_FIFO empty flag
720 * @arg I2C_FLAG_RD_REQ : I2C work as slave or master flag
721 * @arg I2C_FLAG_TX_ABRT : TX error flag(Master mode)
722 * @arg I2C_FLAG_RX_DONE : Master not ack flag(slave mode)
723 * @arg I2C_FLAG_ACTIVITY: I2C activity flag
724 * @arg I2C_FLAG_STOP_DET: stop condition flag
725 * @arg I2C_FLAG_START_DET: start condition flag
726 * @arg I2C_FLAG_GEN_CALL : a general call address and ack flag
727 * @retval : None
728 */
I2C_ClearFlag(I2C_TypeDef * I2Cx,uint32_t I2C_FLAG)729 void I2C_ClearFlag(I2C_TypeDef* I2Cx, uint32_t I2C_FLAG)
730 {
731 /* Check the parameters */
732 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
733 assert_param(IS_I2C_CLEAR_FLAG(I2C_FLAG));
734
735 if((I2C_FLAG & I2C_FLAG_RX_UNDER) == I2C_FLAG_RX_UNDER)
736 {((void)I2Cx->IC_CLR_RX_UNDER);}
737 if((I2C_FLAG & I2C_FLAG_RX_OVER) == I2C_FLAG_RX_OVER)
738 {((void)I2Cx->IC_CLR_RX_OVER);}
739 if((I2C_FLAG & I2C_FLAG_TX_OVER) == I2C_FLAG_TX_OVER)
740 {((void)I2Cx->IC_CLR_TX_OVER);}
741 if((I2C_FLAG & I2C_FLAG_RD_REQ) == I2C_FLAG_RD_REQ)
742 {((void)I2Cx->IC_CLR_RD_REQ);}
743 if((I2C_FLAG & I2C_FLAG_TX_ABRT) == I2C_FLAG_TX_ABRT)
744 {((void)I2Cx->IC_CLR_TX_ABRT);}
745 if((I2C_FLAG & I2C_FLAG_RX_DONE) == I2C_FLAG_RX_DONE)
746 {((void)I2Cx->IC_CLR_RX_DONE);}
747 if((I2C_FLAG & I2C_FLAG_ACTIVITY) == I2C_FLAG_ACTIVITY)
748 {((void)I2Cx->IC_CLR_ACTIVITY);}
749 if((I2C_FLAG & I2C_FLAG_STOP_DET) == I2C_FLAG_STOP_DET)
750 {((void)I2Cx->IC_CLR_STOP_DET);}
751 if((I2C_FLAG & I2C_FLAG_START_DET) == I2C_FLAG_START_DET)
752 {((void)I2Cx->IC_CLR_START_DET);}
753 if((I2C_FLAG & I2C_FLAG_GEN_CALL) == I2C_FLAG_GEN_CALL)
754 {((void)I2Cx->IC_CLR_GEN_CALL);}
755 }
756
757 /**
758 * @brief Checks whether the specified I2C interrupt has occurred or not.
759 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
760 * @param I2C_IT: specifies the interrupt source to check.
761 * This parameter can be one of the following values:
762 * @arg I2C_IT_RX_UNDER: Rx Buffer is empty interrupt
763 * @arg I2C_IT_RX_OVER : RX Buffer Overrun interrupt
764 * @arg I2C_IT_RX_FULL : Rx buffer full interrupt
765 * @arg I2C_IT_TX_OVER : TX Buffer Overrun interrupt
766 * @arg I2C_IT_TX_EMPTY : TX_FIFO empty interrupt
767 * @arg I2C_IT_RD_REQ : I2C work as slave or master interrupt
768 * @arg I2C_IT_TX_ABRT : TX error interrupt (Master mode)
769 * @arg I2C_IT_RX_DONE : Master not ack interrupt (slave mode)
770 * @arg I2C_IT_ACTIVITY : I2C activity interrupt
771 * @arg I2C_IT_STOP_DET : stop condition interrupt
772 * @arg I2C_IT_START_DET : start condition interrupt
773 * @arg I2C_IT_GEN_CALL : a general call address and ack interrupt
774 * @retval : The new state of I2C_IT (SET or RESET).
775 */
I2C_GetITStatus(I2C_TypeDef * I2Cx,uint32_t I2C_IT)776 ITStatus I2C_GetITStatus(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
777 {
778 ITStatus bitstatus = RESET;
779 /* Check the parameters */
780 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
781 assert_param(IS_I2C_GET_IT(I2C_IT));
782
783 /* Check the status of the specified I2C flag */
784 if((I2Cx->IC_RAW_INTR_STAT & I2C_IT) != (uint32_t)RESET)
785 {
786 /* I2C_IT is set */
787 bitstatus = SET;
788 }
789 else
790 {
791 /* I2C_IT is reset */
792 bitstatus = RESET;
793 }
794
795 /* Return the I2C_IT status */
796 return bitstatus;
797 }
798
799 /**
800 * @brief Clears the I2Cx interrupt pending bits.
801 * @param I2Cx: where x can be 1 or 2 to select the I2C peripheral.
802 * @param I2C_IT: specifies the interrupt pending bit to clear.
803 * This parameter can be any combination of the following values:
804 * @arg I2C_IT_RX_UNDER: Rx Buffer is empty interrupt
805 * @arg I2C_IT_RX_OVER : RX Buffer Overrun interrupt
806 * @arg I2C_IT_RX_FULL : Rx buffer full interrupt
807 * @arg I2C_IT_TX_OVER : TX Buffer Overrun interrupt
808 * @arg I2C_IT_TX_EMPTY : TX_FIFO empty interrupt
809 * @arg I2C_IT_RD_REQ : I2C work as slave or master interrupt
810 * @arg I2C_IT_TX_ABRT : TX error interrupt (Master mode)
811 * @arg I2C_IT_RX_DONE : Master not ack interrupt (slave mode)
812 * @arg I2C_IT_ACTIVITY : I2C activity interrupt
813 * @arg I2C_IT_STOP_DET : stop condition interrupt
814 * @arg I2C_IT_START_DET : start condition interrupt
815 * @arg I2C_IT_GEN_CALL : a general call address and ack interrupt
816 * @retval : None
817 */
I2C_ClearITPendingBit(I2C_TypeDef * I2Cx,uint32_t I2C_IT)818 void I2C_ClearITPendingBit(I2C_TypeDef* I2Cx, uint32_t I2C_IT)
819 {
820 /* Check the parameters */
821 assert_param(IS_I2C_ALL_PERIPH(I2Cx));
822 assert_param(IS_I2C_CLEAR_IT(I2C_IT));
823
824 /* Clear the selected I2C flag */
825
826 if((I2C_IT & I2C_IT_RX_UNDER) == I2C_FLAG_RX_UNDER)
827 {((void)I2Cx->IC_CLR_RX_UNDER);}
828 if((I2C_IT & I2C_IT_RX_OVER) == I2C_FLAG_RX_OVER)
829 {((void)I2Cx->IC_CLR_RX_OVER);}
830 if((I2C_IT & I2C_IT_TX_OVER) == I2C_FLAG_TX_OVER)
831 {((void)I2Cx->IC_CLR_TX_OVER);}
832 if((I2C_IT & I2C_IT_RD_REQ) == I2C_FLAG_RD_REQ)
833 {((void)I2Cx->IC_CLR_RD_REQ);}
834 if((I2C_IT & I2C_IT_TX_ABRT) == I2C_FLAG_TX_ABRT)
835 {((void)I2Cx->IC_CLR_TX_ABRT);}
836 if((I2C_IT & I2C_IT_RX_DONE) == I2C_FLAG_RX_DONE)
837 {((void)I2Cx->IC_CLR_RX_DONE);}
838 if((I2C_IT & I2C_IT_ACTIVITY) == I2C_FLAG_ACTIVITY)
839 {((void)I2Cx->IC_CLR_ACTIVITY);}
840 if((I2C_IT & I2C_IT_STOP_DET) == I2C_FLAG_STOP_DET)
841 {((void)I2Cx->IC_CLR_STOP_DET);}
842 if((I2C_IT & I2C_IT_START_DET) == I2C_FLAG_START_DET)
843 {((void)I2Cx->IC_CLR_START_DET);}
844 if((I2C_IT & I2C_IT_GEN_CALL) == I2C_FLAG_GEN_CALL)
845 {((void)I2Cx->IC_CLR_GEN_CALL);}
846
847 }
848
849 /**
850 * @}
851 */
852
853 /**
854 * @}
855 */
856
857 /**
858 * @}
859 */
860
861 /*-------------------------(C) COPYRIGHT 2017 MindMotion ----------------------*/
862