1 ////////////////////////////////////////////////////////////////////////////////
2 /// @file     hal_i2c.c
3 /// @author   AE TEAM
4 /// @brief    THIS FILE PROVIDES ALL THE I2C FIRMWARE FUNCTIONS.
5 ////////////////////////////////////////////////////////////////////////////////
6 /// @attention
7 ///
8 /// THE EXISTING FIRMWARE IS ONLY FOR REFERENCE, WHICH IS DESIGNED TO PROVIDE
9 /// CUSTOMERS WITH CODING INFORMATION ABOUT THEIR PRODUCTS SO THEY CAN SAVE
10 /// TIME. THEREFORE, MINDMOTION SHALL NOT BE LIABLE FOR ANY DIRECT, INDIRECT OR
11 /// CONSEQUENTIAL DAMAGES ABOUT ANY CLAIMS ARISING OUT OF THE CONTENT OF SUCH
12 /// HARDWARE AND/OR THE USE OF THE CODING INFORMATION CONTAINED HEREIN IN
13 /// CONNECTION WITH PRODUCTS MADE BY CUSTOMERS.
14 ///
15 /// <H2><CENTER>&COPY; COPYRIGHT MINDMOTION </CENTER></H2>
16 ////////////////////////////////////////////////////////////////////////////////
17 
18 // Define to prevent recursive inclusion
19 #define _HAL_I2C_C_
20 
21 // Files includes
22 #include "hal_i2c.h"
23 #include "hal_rcc.h"
24 
25 ////////////////////////////////////////////////////////////////////////////////
26 /// @addtogroup MM32_Hardware_Abstract_Layer
27 /// @{
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// @addtogroup I2C_HAL
31 /// @{
32 
33 ////////////////////////////////////////////////////////////////////////////////
34 /// @addtogroup I2C_Exported_Functions
35 /// @{
36 
37 ////////////////////////////////////////////////////////////////////////////////
38 /// @brief  Deinitializes the i2c peripheral registers to their default
39 ///         reset values.
40 /// @param  i2c: where n can be 1 or 2 to select the I2C peripheral.
41 /// @retval None.
42 ////////////////////////////////////////////////////////////////////////////////
I2C_DeInit(I2C_TypeDef * i2c)43 void I2C_DeInit(I2C_TypeDef* i2c)
44 {
45     switch (*(vu32*)&i2c) {
46         case (u32)I2C1:  // I2C1_BASE:
47             exRCC_APB1PeriphReset(RCC_APB1ENR_I2C1);
48             break;
49         case (u32)I2C2:  // I2C2_BASE:
50             exRCC_APB1PeriphReset(RCC_APB1ENR_I2C2);
51             break;
52         default:
53             break;
54     }
55 }
56 
57 ////////////////////////////////////////////////////////////////////////////////
58 /// @brief  Initializes the i2c peripheral according to the specified
59 ///         parameters in the init_struct.
60 /// @param  i2c: select the I2C peripheral.
61 /// @param  init_struct: pointer to a I2C_InitTypeDef structure that
62 ///         contains the configuration information for the specified
63 ///         I2C peripheral.
64 /// @retval None.
65 ////////////////////////////////////////////////////////////////////////////////
I2C_Init(I2C_TypeDef * i2c,I2C_InitTypeDef * init_struct)66 void I2C_Init(I2C_TypeDef* i2c, I2C_InitTypeDef* init_struct)
67 {
68     u32 pclk1         = HSI_VALUE;
69     u32 minSclLowTime = 0;
70     u32 i2cPeriod     = 0;
71     u32 pclk1Period   = 0;
72 
73 
74     i2c->IC_ENABLE &= ~I2C_ENR_ENABLE;
75 
76 
77     pclk1       = RCC_GetPCLK1Freq();
78     pclk1Period = 1000000000 / pclk1;
79     i2cPeriod   = 1000000000 / init_struct->I2C_ClockSpeed;
80 
81     minSclLowTime = pclk1 / init_struct->I2C_ClockSpeed ;
82     i2cPeriod = 82 / pclk1Period;
83 
84     if (init_struct->I2C_ClockSpeed <= 100000) {
85         i2c->IC_SS_SCL_LCNT = (minSclLowTime - 13 - i2cPeriod) / 2;
86         i2c->IC_SS_SCL_HCNT = (minSclLowTime - 13 - i2cPeriod - i2c->IC_SS_SCL_LCNT);
87     }
88     else {
89         i2c->IC_FS_SCL_LCNT = (minSclLowTime - 13 - i2cPeriod ) / 2 + 4;
90         i2c->IC_FS_SCL_HCNT = (minSclLowTime - 13 - i2c->IC_FS_SCL_LCNT - i2cPeriod);
91     }
92 
93     i2c->IC_CON &= ~(I2C_CR_EMPINT     |   \
94                      I2C_CR_SLAVEDIS   |   \
95                      I2C_CR_REPEN      |   \
96                      I2C_CR_MASTER10   |   \
97                      I2C_CR_SLAVE10    |   \
98                      I2C_CR_FAST       |   \
99                      I2C_CR_MASTER);
100 
101     i2c->IC_CON =  I2C_CR_EMPINT       |   \
102                    I2C_CR_REPEN        |   \
103                    ((init_struct->I2C_Speed == I2C_CR_FAST) ? I2C_CR_FAST : I2C_CR_STD) | \
104                    ((init_struct->I2C_Mode)  ? I2C_CR_MASTER : 0x00);
105     i2c->IC_INTR_MASK &= INTR_MASK;
106 
107     i2c->IC_RX_TL = 0x00;
108     i2c->IC_TX_TL = 0x00;
109 }
110 
111 ////////////////////////////////////////////////////////////////////////////////
112 /// @brief  Fills each init_struct member with its default value.
113 /// @param  init_struct: pointer to an I2C_InitTypeDef structure
114 ///         which will be initialized.
115 /// @retval None.
116 ////////////////////////////////////////////////////////////////////////////////
I2C_StructInit(I2C_InitTypeDef * init_struct)117 void I2C_StructInit(I2C_InitTypeDef* init_struct)
118 {
119     init_struct->I2C_Mode       = I2C_CR_MASTER;
120     init_struct->I2C_OwnAddress = I2C_OWN_ADDRESS;
121     init_struct->I2C_Speed      = I2C_CR_STD;
122     init_struct->I2C_ClockSpeed = 100000;
123 }
124 
125 ////////////////////////////////////////////////////////////////////////////////
126 /// @brief  Enables or disables the specified I2C peripheral.
127 /// @param  i2c: select the I2C peripheral.
128 /// @param  state: new state of the i2c peripheral. This parameter
129 ///         can be: ENABLE or DISABLE.
130 /// @retval None.
131 ////////////////////////////////////////////////////////////////////////////////
I2C_Cmd(I2C_TypeDef * i2c,FunctionalState state)132 void I2C_Cmd(I2C_TypeDef* i2c, FunctionalState state)
133 {
134     (state) ? (i2c->IC_ENABLE |= I2C_ENR_ENABLE) : (i2c->IC_ENABLE &= ~I2C_ENR_ENABLE);
135 }
136 ////////////////////////////////////////////////////////////////////////////////
137 /// @brief  Enables or disables the specified I2C DMA requests.
138 /// @param  i2c: select the I2C peripheral.
139 /// @param  state: new state of the I2C DMA transfer.
140 ///         This parameter can be: ENABLE or DISABLE.
141 /// @retval None.
142 ////////////////////////////////////////////////////////////////////////////////
I2C_DMACmd(I2C_TypeDef * i2c,FunctionalState state)143 void I2C_DMACmd(I2C_TypeDef* i2c, FunctionalState state)
144 {
145     if (state) {
146         if (I2C_DMA_DIR == TDMAE_SET)
147             i2c->IC_DMA_CR |= TDMAE_SET;
148 
149         else
150             i2c->IC_DMA_CR |= RDMAE_SET;
151     }
152     else
153         i2c->IC_DMA_CR &= ~(I2C_DMA_RXEN | I2C_DMA_TXEN);
154 }
155 ////////////////////////////////////////////////////////////////////////////////
156 /// @brief  Generates i2c communication START condition.
157 /// @param  i2c: select the I2C peripheral.
158 /// @param  state: new state of the I2C START condition generation.
159 ///         This parameter can be: ENABLE or DISABLE.
160 /// @retval None.
161 ////////////////////////////////////////////////////////////////////////////////
I2C_GenerateSTART(I2C_TypeDef * i2c,FunctionalState state)162 void I2C_GenerateSTART(I2C_TypeDef* i2c, FunctionalState state)
163 {
164     (state) ? (i2c->IC_CON |= I2C_CR_REPEN) : (i2c->IC_CON &= ~I2C_CR_REPEN);
165 }
166 
167 ////////////////////////////////////////////////////////////////////////////////
168 /// @brief  Generates i2c communication STOP condition.
169 /// @param  i2c: select the I2C peripheral.
170 /// @param  state: new state of the I2C STOP condition generation.
171 ///         This parameter can be: ENABLE or DISABLE.
172 /// @retval None.
173 ////////////////////////////////////////////////////////////////////////////////
I2C_GenerateSTOP(I2C_TypeDef * i2c,FunctionalState state)174 void I2C_GenerateSTOP(I2C_TypeDef* i2c, FunctionalState state)
175 {
176     u16 overTime = 3000;
177 
178     i2c->IC_ENABLE |= I2C_ENR_ABORT;
179 
180     while (i2c->IC_ENABLE & I2C_ENR_ABORT) {
181         if (overTime-- == 0)
182             break;
183     }
184     i2c->IC_CLR_TX_ABRT;
185 }
186 
187 ////////////////////////////////////////////////////////////////////////////////
188 /// @brief  Configures the specified I2C own address2.
189 /// @param  i2c: select the I2C peripheral.
190 /// @param  addr: specifies the 7bit I2C own address2.
191 /// @retval None.
192 ////////////////////////////////////////////////////////////////////////////////
I2C_OwnAddress2Config(I2C_TypeDef * i2c,u8 addr)193 void I2C_OwnAddress2Config(I2C_TypeDef* i2c, u8 addr)
194 {
195     MODIFY_REG(i2c->IC_TAR, (u16)I2C_TAR_ADDR, (u16)(addr >> 1));
196 }
197 
198 ////////////////////////////////////////////////////////////////////////////////
199 /// @brief  Enables or disables the specified I2C dual addressing mode.
200 /// @param  i2c: select the I2C peripheral.
201 /// @param  state: new state of the I2C dual addressing mode.
202 ///         This parameter can be: ENABLE or DISABLE.
203 /// @retval None.
204 ////////////////////////////////////////////////////////////////////////////////
I2C_DualAddressCmd(I2C_TypeDef * i2c,FunctionalState state)205 void I2C_DualAddressCmd(I2C_TypeDef* i2c, FunctionalState state)
206 {
207     (state) ? (i2c->IC_TAR |= IC_TAR_ENDUAL_Set) : (i2c->IC_TAR &= IC_TAR_ENDUAL_Reset);
208 }
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// @brief  Enables or disables the specified I2C general call feature.
212 /// @param  i2c: select the I2C peripheral.
213 /// @param  state: new state of the I2C General call.
214 ///         This parameter can be: ENABLE or DISABLE.
215 /// @retval None.
216 ////////////////////////////////////////////////////////////////////////////////
I2C_GeneralCallCmd(I2C_TypeDef * i2c,FunctionalState state)217 void I2C_GeneralCallCmd(I2C_TypeDef* i2c, FunctionalState state)
218 {
219     (state) ? (i2c->IC_TAR |= I2C_TAR_SPECIAL) : (i2c->IC_TAR &= ~I2C_TAR_SPECIAL);
220 }
221 
222 ////////////////////////////////////////////////////////////////////////////////
223 /// @brief  Enables or disables the specified I2C interrupts.
224 /// @param  i2c: select the I2C peripheral.
225 /// @param  it: specifies the I2C interrupts sources to be enabled
226 ///         or disabled.
227 ///         This parameter can be any combination of the following values:
228 /// @arg    I2C_IT_RX_UNDER   : Rx Buffer is empty interrupt mask
229 /// @arg    I2C_IT_RX_OVER    : RX  Buffer Overrun interrupt mask
230 /// @arg    I2C_IT_RX_FULL    : Rx buffer full interrupt mask
231 /// @arg    I2C_IT_TX_OVER    : TX  Buffer Overrun interrupt mask
232 /// @arg    I2C_IT_TX_EMPTY   : TX_FIFO empty interrupt mask
233 /// @arg    I2C_IT_RD_REQ     : I2C work as slave or master interrupt mask
234 /// @arg    I2C_IT_TX_ABRT    : TX error interrupt  mask(Master mode)
235 /// @arg    I2C_IT_RX_DONE    : Master not ack interrupt mask(slave mode)
236 /// @arg    I2C_IT_ACTIVITY   : I2C activity interrupt mask
237 /// @arg    I2C_IT_STOP_DET   : stop condition  interrupt mask
238 /// @arg    I2C_IT_START_DET  : start condition  interrupt mask
239 /// @arg    I2C_IT_GEN_CALL   : a general call address and ack interrupt mask
240 /// @param  state: new state of the specified I2C interrupts.
241 ///   This parameter can be: ENABLE or DISABLE.
242 /// @retval None.
243 ////////////////////////////////////////////////////////////////////////////////
I2C_ITConfig(I2C_TypeDef * i2c,u16 it,FunctionalState state)244 void I2C_ITConfig(I2C_TypeDef* i2c, u16 it, FunctionalState state)
245 {
246     if (it == I2C_IT_RX_FULL)
247         I2C_ReadCmd(i2c);
248     (state) ? SET_BIT(i2c->IC_INTR_MASK, it) : CLEAR_BIT(i2c->IC_INTR_MASK, (u16)it);
249 }
250 
251 ////////////////////////////////////////////////////////////////////////////////
252 /// @brief  Sends a data byte through the i2c peripheral.
253 /// @param  i2c: select the I2C peripheral.
254 /// @param  dat: Byte to be transmitted..
255 /// @retval None.
256 ////////////////////////////////////////////////////////////////////////////////
I2C_SendData(I2C_TypeDef * i2c,u8 dat)257 void I2C_SendData(I2C_TypeDef* i2c, u8 dat)
258 {
259     i2c->IC_DATA_CMD = dat;
260 }
261 
262 ////////////////////////////////////////////////////////////////////////////////
263 /// @brief  Returns the most recent received data by the i2c peripheral.
264 /// @param  i2c: select the I2C peripheral.
265 /// @retval The value of the received data.
266 ////////////////////////////////////////////////////////////////////////////////
I2C_ReadCmd(I2C_TypeDef * i2c)267 void I2C_ReadCmd(I2C_TypeDef* i2c)
268 {
269     i2c->IC_DATA_CMD = I2C_DR_CMD;
270 }
271 
272 ////////////////////////////////////////////////////////////////////////////////
273 /// @brief  Returns the most recent received data by the i2c peripheral.
274 /// @param  i2c: select the I2C peripheral.
275 /// @retval The value of the received data.
276 ////////////////////////////////////////////////////////////////////////////////
I2C_ReceiveData(I2C_TypeDef * i2c)277 u8 I2C_ReceiveData(I2C_TypeDef* i2c)
278 {
279     I2C_CMD_DIR = 0;
280     return (u8)i2c->IC_DATA_CMD;
281 }
282 
283 ////////////////////////////////////////////////////////////////////////////////
284 /// @brief  Transmits the address byte to select the slave device.
285 /// @param  i2c: select the I2C peripheral.
286 /// @param  addr: specifies the slave address which will be transmitted
287 /// @param  dir: specifies whether the I2C device will be a
288 ///   Transmitter or a Receiver.
289 ///   This parameter can be one of the following values
290 /// @arg  I2C_Direction_Transmitter: Transmitter mode
291 /// @arg  I2C_Direction_Receiver: Receiver mode
292 /// @retval None.
293 ////////////////////////////////////////////////////////////////////////////////
I2C_Send7bitAddress(I2C_TypeDef * i2c,u8 addr,u8 dir)294 void I2C_Send7bitAddress(I2C_TypeDef* i2c, u8 addr, u8 dir)
295 {
296     i2c->IC_TAR = addr >> 1;
297 }
298 
299 ////////////////////////////////////////////////////////////////////////////////
300 /// @brief  Reads the specified I2C register and returns its value.
301 /// @param  i2c: select the I2C peripheral.
302 /// @param  reg: specifies the register to read.
303 ///         This parameter can be one of the following values:
304 /// @retval The value of the read register.
305 ////////////////////////////////////////////////////////////////////////////////
I2C_ReadRegister(I2C_TypeDef * i2c,u8 reg)306 u16 I2C_ReadRegister(I2C_TypeDef* i2c, u8 reg)
307 {
308     return (*(vu16*)(*((u32*)&i2c) + reg));
309 }
310 
311 ////////////////////////////////////////////////////////////////////////////////
312 /// @brief  Returns the last i2c Event.
313 /// @param  i2c: select the I2C peripheral.
314 /// @retval The last event
315 ////////////////////////////////////////////////////////////////////////////////
I2C_GetLastEvent(I2C_TypeDef * i2c)316 u32 I2C_GetLastEvent(I2C_TypeDef* i2c)
317 {
318     return (u32)i2c->IC_RAW_INTR_STAT & FLAG_Mask;
319 }
320 
321 ////////////////////////////////////////////////////////////////////////////////
322 /// @brief  Checks whether the last i2c Event is equal to the one passed
323 ///   as parameter.
324 /// @param  i2c: select the I2C peripheral.
325 /// @param  event: specifies the event to be checked.
326 ///   This parameter can be one of the following values:
327 /// @arg  I2C_EVENT_RX_UNDER : Rx Buffer is empty event
328 /// @arg  I2C_EVENT_RX_OVER  : RX  Buffer Overrun event
329 /// @arg  I2C_EVENTT_RX_FULL : Rx buffer full event
330 /// @arg  I2C_EVENT_TX_OVER  : TX  Buffer Overrun event
331 /// @arg  I2C_EVENT_TX_EMPTY : TX_FIFO empty event
332 /// @arg  I2C_EVENT_RD_REQ   : I2C work as slave or master event
333 /// @arg  I2C_EVENT_TX_ABRT  : TX error event(Master mode)
334 /// @arg  I2C_EVENT_RX_DONE  : Master not ack event(slave mode)
335 /// @arg  I2C_EVENT_ACTIVITY : I2C activity event
336 /// @arg  I2C_EVENT_STOP_DET : stop condition  event
337 /// @arg  I2C_EVENT_START_DET: start condition  event
338 /// @arg  I2C_EVENT_GEN_CALL : a general call address and ack event
339 /// - SUCCESS: Last event is equal to the I2C_EVENT
340 /// - ERROR: Last event is different from the I2C_EVENT
341 ////////////////////////////////////////////////////////////////////////////////
I2C_CheckEvent(I2C_TypeDef * i2c,u32 event)342 ErrorStatus I2C_CheckEvent(I2C_TypeDef* i2c, u32 event)
343 {
344     if ((event == I2C_EVENT_RX_FULL) && (I2C_CMD_DIR == 0)) {
345         i2c->IC_DATA_CMD = I2C_DR_CMD;
346         I2C_CMD_DIR       = 1;
347     }
348 
349     return (ErrorStatus)((i2c->IC_RAW_INTR_STAT & event) == event);
350 }
351 
352 ////////////////////////////////////////////////////////////////////////////////
353 /// @brief  Checks whether the specified I2C flag is set or not.
354 /// @param  i2c: select the I2C peripheral.
355 /// @param  flag: specifies the flag to check.
356 ///   This parameter can be one of the following values:
357 /// @arg  I2C_FLAG_RX_UNDER : Rx Buffer is empty flag
358 /// @arg  I2C_FLAG_RX_OVER  : RX  Buffer Overrun flag
359 /// @arg  I2C_FLAG_RX_FULL  : Rx buffer full flag
360 /// @arg  I2C_FLAG_TX_OVER  : TX  Buffer Overrun flag
361 /// @arg  I2C_FLAG_TX_EMPTY : TX_FIFO empty flag
362 /// @arg  I2C_FLAG_RD_REQ   : I2C work as slave or master flag
363 /// @arg  I2C_FLAG_TX_ABRT  : TX error flag(Master mode)
364 /// @arg  I2C_FLAG_RX_DONE  : Master not ack flag(slave mode)
365 /// @arg  I2C_FLAG_ACTIVITY : I2C activity flag
366 /// @arg  I2C_FLAG_STOP_DET : stop condition  flag
367 /// @arg  I2C_FLAG_START_DET: start condition  flag
368 /// @arg  I2C_FLAG_GEN_CALL : a general call address and ack flag
369 /// @retval The new state of I2C_FLAG (SET or RESET).
370 ////////////////////////////////////////////////////////////////////////////////
I2C_GetFlagStatus(I2C_TypeDef * i2c,u32 flag)371 FlagStatus I2C_GetFlagStatus(I2C_TypeDef* i2c, u32 flag)
372 {
373     if (flag & 0x8000)
374         return ((i2c->IC_STATUS & flag) ? SET : RESET);
375 
376     if ((flag == I2C_FLAG_RX_FULL) && (I2C_CMD_DIR == 0)) {
377         i2c->IC_DATA_CMD = I2C_DR_CMD;
378         I2C_CMD_DIR       = 1;
379     }
380     return (((i2c->IC_RAW_INTR_STAT & flag)) ? SET : RESET);
381 }
382 
383 ////////////////////////////////////////////////////////////////////////////////
384 /// @brief  Clears the i2c's pending flags.
385 /// @param  i2c: select the I2C peripheral.
386 /// @param  flag: specifies the flag to clear.
387 ///   This parameter can be any combination of the following values:
388 /// @arg  I2C_FLAG_RX_UNDER : Rx Buffer is empty flag
389 /// @arg  I2C_FLAG_RX_OVER  : RX  Buffer Overrun flag
390 /// @arg  I2C_FLAG_RX_FULL  : Rx buffer full flag
391 /// @arg  I2C_FLAG_TX_OVER  : TX  Buffer Overrun flag
392 /// @arg  I2C_FLAG_TX_EMPTY : TX_FIFO empty flag
393 /// @arg  I2C_FLAG_RD_REQ   : I2C work as slave or master flag
394 /// @arg  I2C_FLAG_TX_ABRT  : TX error flag(Master mode)
395 /// @arg  I2C_FLAG_RX_DONE  : Master not ack flag(slave mode)
396 /// @arg  I2C_FLAG_ACTIVITY : I2C activity flag
397 /// @arg  I2C_FLAG_STOP_DET : stop condition  flag
398 /// @arg  I2C_FLAG_START_DET: start condition  flag
399 /// @arg  I2C_FLAG_GEN_CALL : a general call address and ack flag
400 /// @retval None.
401 ////////////////////////////////////////////////////////////////////////////////
I2C_ClearFlag(I2C_TypeDef * i2c,u32 flag)402 void I2C_ClearFlag(I2C_TypeDef* i2c, u32 flag)
403 {
404     if ((flag & I2C_FLAG_RX_UNDER) == I2C_FLAG_RX_UNDER)
405         i2c->IC_CLR_RX_UNDER;
406     if ((flag & I2C_FLAG_RX_OVER) == I2C_FLAG_RX_OVER)
407         i2c->IC_CLR_RX_OVER;
408     if ((flag & I2C_FLAG_TX_OVER) == I2C_FLAG_TX_OVER)
409         i2c->IC_CLR_TX_OVER;
410     if ((flag & I2C_FLAG_RD_REQ) == I2C_FLAG_RD_REQ)
411         i2c->IC_CLR_RD_REQ;
412     if ((flag & I2C_FLAG_TX_ABRT) == I2C_FLAG_TX_ABRT)
413         i2c->IC_CLR_TX_ABRT;
414     if ((flag & I2C_FLAG_RX_DONE) == I2C_FLAG_RX_DONE)
415         i2c->IC_CLR_RX_DONE;
416     if ((flag & I2C_FLAG_ACTIVITY) == I2C_FLAG_ACTIVITY)
417         i2c->IC_CLR_ACTIVITY;
418     if ((flag & I2C_FLAG_STOP_DET) == I2C_FLAG_STOP_DET)
419         i2c->IC_CLR_STOP_DET;
420     if ((flag & I2C_FLAG_START_DET) == I2C_FLAG_START_DET)
421         i2c->IC_CLR_START_DET;
422     if ((flag & I2C_FLAG_GEN_CALL) == I2C_FLAG_GEN_CALL)
423         i2c->IC_CLR_GEN_CALL;
424 }
425 
426 ////////////////////////////////////////////////////////////////////////////////
427 /// @brief  Checks whether the specified I2C interrupt has occurred or not.
428 /// @param  i2c: select the I2C peripheral.
429 /// @param  it: specifies the interrupt source to check.
430 ///   This parameter can be one of the following values:
431 /// @arg  I2C_IT_RX_UNDER : Rx Buffer is empty interrupt
432 /// @arg  I2C_IT_RX_OVER  : RX  Buffer Overrun interrupt
433 /// @arg  I2C_IT_RX_FULL  : Rx buffer full interrupt
434 /// @arg  I2C_IT_TX_OVER  : TX  Buffer Overrun interrupt
435 /// @arg  I2C_IT_TX_EMPTY : TX_FIFO empty interrupt
436 /// @arg  I2C_IT_RD_REQ   : I2C work as slave or master interrupt
437 /// @arg  I2C_IT_TX_ABRT  : TX error interrupt  (Master mode)
438 /// @arg  I2C_IT_RX_DONE  : Master not ack interrupt (slave mode)
439 /// @arg  I2C_IT_ACTIVITY : I2C activity interrupt
440 /// @arg  I2C_IT_STOP_DET : stop condition  interrupt
441 /// @arg  I2C_IT_START_DET: start condition  interrupt
442 /// @arg  I2C_IT_GEN_CALL : a general call address and ack interrupt
443 /// @retval The new state of I2C_IT (SET or RESET).
444 ////////////////////////////////////////////////////////////////////////////////
I2C_GetITStatus(I2C_TypeDef * i2c,u32 it)445 ITStatus I2C_GetITStatus(I2C_TypeDef* i2c, u32 it)
446 {
447     return ((i2c->IC_RAW_INTR_STAT & it) ? SET : RESET);
448 }
449 
450 ////////////////////////////////////////////////////////////////////////////////
451 /// @brief  Clears the i2c interrupt pending bits.
452 /// @param  i2c: select the I2C peripheral.
453 /// @param  it: specifies the interrupt pending bit to clear.
454 ///   This parameter can be any combination of the following values:
455 /// @arg  I2C_IT_RX_UNDER : Rx Buffer is empty interrupt
456 /// @arg  I2C_IT_RX_OVER  : RX  Buffer Overrun interrupt
457 /// @arg  I2C_IT_RX_FULL  : Rx buffer full interrupt
458 /// @arg  I2C_IT_TX_OVER  : TX  Buffer Overrun interrupt
459 /// @arg  I2C_IT_TX_EMPTY : TX_FIFO empty interrupt
460 /// @arg  I2C_IT_RD_REQ   : I2C work as slave or master interrupt
461 /// @arg  I2C_IT_TX_ABRT  : TX error interrupt  (Master mode)
462 /// @arg  I2C_IT_RX_DONE  : Master not ack interrupt (slave mode)
463 /// @arg  I2C_IT_ACTIVITY : I2C activity interrupt
464 /// @arg  I2C_IT_STOP_DET : stop condition  interrupt
465 /// @arg  I2C_IT_START_DET: start condition  interrupt
466 /// @arg  I2C_IT_GEN_CALL : a general call address and ack interrupt
467 /// @retval None.
468 ////////////////////////////////////////////////////////////////////////////////
I2C_ClearITPendingBit(I2C_TypeDef * i2c,u32 it)469 void I2C_ClearITPendingBit(I2C_TypeDef* i2c, u32 it)
470 {
471     if ((it & I2C_IT_RX_UNDER) == I2C_FLAG_RX_UNDER)
472         i2c->IC_CLR_RX_UNDER;
473     if ((it & I2C_IT_RX_OVER) == I2C_FLAG_RX_OVER)
474         i2c->IC_CLR_RX_OVER;
475     if ((it & I2C_IT_TX_OVER) == I2C_FLAG_TX_OVER)
476         i2c->IC_CLR_TX_OVER;
477     if ((it & I2C_IT_RD_REQ) == I2C_FLAG_RD_REQ)
478         i2c->IC_CLR_RD_REQ;
479     if ((it & I2C_IT_TX_ABRT) == I2C_FLAG_TX_ABRT)
480         i2c->IC_CLR_TX_ABRT;
481     if ((it & I2C_IT_RX_DONE) == I2C_FLAG_RX_DONE)
482         i2c->IC_CLR_RX_DONE;
483     if ((it & I2C_IT_ACTIVITY) == I2C_FLAG_ACTIVITY)
484         i2c->IC_CLR_ACTIVITY;
485     if ((it & I2C_IT_STOP_DET) == I2C_FLAG_STOP_DET)
486         i2c->IC_CLR_STOP_DET;
487     if ((it & I2C_IT_START_DET) == I2C_FLAG_START_DET)
488         i2c->IC_CLR_START_DET;
489     if ((it & I2C_IT_GEN_CALL) == I2C_FLAG_GEN_CALL)
490         i2c->IC_CLR_GEN_CALL;
491 }
492 
493 ////////////////////////////////////////////////////////////////////////////////
494 //
495 //  New Function Interface
496 //
497 ////////////////////////////////////////////////////////////////////////////////
498 
499 ////////////////////////////////////////////////////////////////////////////////
500 /// @brief  Configures slave address.
501 /// @param  i2c: select the I2C peripheral.
502 /// @param  addr: specifies the slave address which will be transmitted
503 ///   This parameter can be one of the following values
504 /// @retval None.
505 ////////////////////////////////////////////////////////////////////////////////
I2C_SendSlaveAddress(I2C_TypeDef * i2c,u8 addr)506 void I2C_SendSlaveAddress(I2C_TypeDef* i2c, u8 addr)
507 {
508     WRITE_REG(i2c->IC_SAR, addr >> 1);
509 }
510 
511 ////////////////////////////////////////////////////////////////////////////////
512 /// @brief  Enables or disables the I2C slave mode.
513 /// @param  i2c: select the I2C peripheral.
514 /// @param  state: new state of the specified I2C interrupts.
515 ///   This parameter can be: ENABLE or DISABLE.
516 /// @retval None.
517 ////////////////////////////////////////////////////////////////////////////////
I2C_SlaveConfigure(I2C_TypeDef * i2c,FunctionalState state)518 void I2C_SlaveConfigure(I2C_TypeDef* i2c, FunctionalState state)
519 {
520     (state) ? CLEAR_BIT(i2c->IC_CON, I2C_CR_SLAVEDIS) : SET_BIT(i2c->IC_CON, I2C_CR_SLAVEDIS);
521 }
522 /// @}
523 
524 /// @}
525 
526 /// @}
527