1 /**
2   ******************************************************************************
3   * @file    lib_i2c.c
4   * @author  Application Team
5   * @version V1.1.0
6   * @date    2019-10-28
7   * @brief   IIC library.
8   ******************************************************************************
9   * @attention
10   *
11   ******************************************************************************
12   */
13 #include "lib_i2c.h"
14 
15 //registers default reset values
16 #define I2C_ADDR_RSTValue       0
17 #define I2C_CTRL_RSTValue       0
18 #define I2C_CTRL2_RSTValue      0
19 
20 /* Private Functions -------------------------------------------------------- */
21 static uint16_t I2C_CheckState(uint8_t State);
22 static void I2C_SendStart(void);
23 static void I2C_SendRestart(void);
24 static void I2C_SendByte(uint8_t dat);
25 static void I2C_SendStop(void);
26 static uint8_t I2C_ReceiveByte(void);
27 static void I2C_ClearBus(uint32_t remap);
28 static void I2C_WaitForCrossPage(uint8_t sla);
29 
30 /**
31   * @brief  Checks required state.
32   * @param  State:
33                 Required state.
34   * @retval 0: state OK
35             !0: state Error, [15:8]Required status code, [7:0] real status code.
36   */
I2C_CheckState(uint8_t State)37 static uint16_t I2C_CheckState(uint8_t State)
38 {
39   uint16_t ret;
40   if (I2C_GetStatusCode() != State)
41   {
42     ret = (State<<8)|(I2C_GetStatusCode());
43     return ret;
44   }
45   else
46   {
47     return 0;
48   }
49 }
50 
51 /**
52   * @brief  Sends start signal.
53   * @param  None
54   * @retval None
55   */
I2C_SendStart(void)56 static void I2C_SendStart(void)
57 {
58   I2C_GenerateSTART(ENABLE);
59   while (I2C_GetINTStatus() == 0);
60   I2C_GenerateSTART(DISABLE);
61 }
62 
63 /**
64   * @brief  Sends restart signal.
65   * @param  None
66   * @retval None
67   */
I2C_SendRestart(void)68 static void I2C_SendRestart(void)
69 {
70   I2C_GenerateSTART(ENABLE);
71   I2C_ClearINTStatus();
72   while (I2C_GetINTStatus() == 0);
73   I2C_GenerateSTART(DISABLE);
74 }
75 
76 /**
77   * @brief  Sends stop signal.
78   * @param  None
79   * @retval None
80   */
I2C_SendStop(void)81 static void I2C_SendStop(void)
82 {
83   I2C_GenerateSTOP(ENABLE);
84   I2C_ClearINTStatus();
85   I2C_GenerateSTOP(DISABLE);
86 }
87 
88 /**
89   * @brief  Sends data.
90   * @param  dat:data to send.
91   * @retval None
92   */
I2C_SendByte(uint8_t dat)93 static void I2C_SendByte(uint8_t dat)
94 {
95   I2C_SendData(dat);
96   I2C_ClearINTStatus();
97   while (I2C_GetINTStatus() == 0);
98 }
99 
100 /**
101   * @brief  Receives byte.
102   * @param  None
103   * @retval Byte received
104   */
I2C_ReceiveByte(void)105 static uint8_t I2C_ReceiveByte(void)
106 {
107   I2C_ClearINTStatus();
108   while (I2C_GetINTStatus() == 0);
109   return I2C_ReceiveData();
110 }
111 
112 /**
113   * @brief  Waits until cross page operation done.
114   * @param  None
115   * @retval None
116   */
I2C_WaitForCrossPage(uint8_t sla)117 static void I2C_WaitForCrossPage(uint8_t sla)
118 {
119   do
120   {
121     I2C_SendRestart();
122     I2C_SendByte(sla);  //device address
123   }while (I2C_GetStatusCode() !=0x18);
124   I2C_SendStop();                 //stop
125 }
126 
127 /**
128   * @brief  Clears bus.
129   * @param  None
130   * @retval None
131   */
I2C_ClearBus(uint32_t remap)132 static void I2C_ClearBus(uint32_t remap)
133 {
134   __IO uint8_t i, j;
135 
136   if (remap) // I2C remap enable, SCL IOC4
137   {
138     GPIOC->DAT &= ~BIT4;
139     GPIOC->ATT |= BIT4;
140     GPIOC->OEN &= ~BIT4;
141     for (i=0; i<9; i++)
142     {
143       GPIOC->DAT |= BIT4;
144       for (j=0; j<100; j++)
145         __NOP();
146       GPIOC->DAT &= ~BIT4;
147       for (j=0; j<100; j++)
148         __NOP();
149     }
150     GPIOC->DAT |= BIT4;
151     GPIOC->OEN |= BIT4;
152     GPIOC->IE &= ~BIT4;
153   }
154   else       // I2C remap disable, SCL IOB13
155   {
156     GPIOB->DAT &= ~BIT13;
157     GPIOB->ATT |= BIT13;
158     GPIOB->OEN &= ~BIT13;
159     for (i=0; i<9; i++)
160     {
161       GPIOB->DAT |= BIT13;
162       for (j=0; j<100; j++)
163         __NOP();
164       GPIOB->DAT &= ~BIT13;
165       for (j=0; j<100; j++)
166         __NOP();
167     }
168     GPIOB->DAT |= BIT13;
169     GPIOB->OEN |= BIT13;
170     GPIOB->IE &= ~BIT13;
171   }
172 }
173 
174 /* Exported Functions ------------------------------------------------------- */
175 
176 /**
177   * @brief  Initializes the I2C peripheral registers to their default reset values.
178   * @param  remap: I2C_REMAP_ENABLE or I2C_REMAP_DISABLE
179   * @retval None
180   */
I2C_DeInit(uint32_t remap)181 void I2C_DeInit(uint32_t remap)
182 {
183   I2C->CTRL &= ~I2C_CTRL_EN;
184 
185   I2C->ADDR = I2C_ADDR_RSTValue;
186   I2C->CTRL = I2C_CTRL_RSTValue;
187   I2C->CTRL2 = I2C_CTRL2_RSTValue;
188 
189   I2C_ClearBus(remap);
190 }
191 
192 /**
193   * @brief  Fills each InitStruct member with its default value.
194   * @param  InitStruct: pointer to an I2C_InitType structure which will be initialized.
195   * @retval None
196   */
I2C_StructInit(I2C_InitType * InitStruct)197 void I2C_StructInit(I2C_InitType *InitStruct)
198 {
199   /*--------------- Reset I2C init structure parameters values ---------------*/
200   /* Initialize the AssertAcknowledge member */
201   InitStruct->AssertAcknowledge = I2C_ASSERTACKNOWLEDGE_DISABLE;
202   /* Initialize the ClockSource member */
203   InitStruct->ClockSource = I2C_CLOCKSOURCE_APBD256;
204   /* Initialize the GeneralCallAck member */
205   InitStruct->GeneralCallAck = I2C_GENERALCALLACK_DISABLE;
206   /* Initialize the SlaveAddr member */
207   InitStruct->SlaveAddr = 0;
208 }
209 
210 /**
211   * @brief  Initializes I2C.
212   * @param  InitStruct: I2C configuration.
213                 SlaveAddr: Own I2C slave address (7 bit)
214                 GeneralCallAck:
215                     I2C_GENERALCALLACK_ENABLE
216                     I2C_GENERALCALLACK_DISABLE
217                 AssertAcknowledge:
218                     I2C_ASSERTACKNOWLEDGE_ENABLE
219                     I2C_ASSERTACKNOWLEDGE_DISABLE
220                 ClockSource:
221                     I2C_CLOCKSOURCE_APBD256
222                     I2C_CLOCKSOURCE_APBD224
223                     I2C_CLOCKSOURCE_APBD192
224                     I2C_CLOCKSOURCE_APBD160
225                     I2C_CLOCKSOURCE_APBD960
226                     I2C_CLOCKSOURCE_APBD120
227                     I2C_CLOCKSOURCE_APBD60
228                     I2C_CLOCKSOURCE_TIM3OFD8
229   * @retval None.
230   */
I2C_Init(I2C_InitType * InitStruct)231 void I2C_Init(I2C_InitType *InitStruct)
232 {
233   uint32_t tmp;
234 
235   /* Check parameters */
236   assert_parameters(IS_I2C_GC(InitStruct->GeneralCallAck));
237   assert_parameters(IS_I2C_AA(InitStruct->AssertAcknowledge));
238   assert_parameters(IS_I2C_CLKSRC(InitStruct->ClockSource));
239 
240   I2C->ADDR = InitStruct->SlaveAddr\
241              |InitStruct->GeneralCallAck;
242   tmp = I2C->CTRL;
243   tmp &= ~(I2C_CTRL_CR\
244           |I2C_CTRL_AA);
245   tmp |= (InitStruct->ClockSource\
246          |InitStruct->AssertAcknowledge);
247   I2C->CTRL = tmp;
248 }
249 
250 /**
251   * @brief  Enables or disables I2C interrupt.
252   * @param  NewState:
253                 ENABLE
254                 DISABLE
255   * @retval None.
256   */
I2C_INTConfig(uint32_t NewState)257 void I2C_INTConfig(uint32_t NewState)
258 {
259   /* Check parameters */
260   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
261 
262   if (NewState == ENABLE)
263     I2C->CTRL2 |= I2C_CTRL2_INTEN;
264   else
265     I2C->CTRL2 &= ~I2C_CTRL2_INTEN;
266 }
267 
268 /**
269   * @brief  Gets I2C interrupt status.
270   * @param  None
271   * @retval Interrupt status.
272   */
I2C_GetINTStatus(void)273 uint8_t I2C_GetINTStatus(void)
274 {
275   if (I2C->CTRL&I2C_CTRL_SI)
276     return 1;
277   else
278     return 0;
279 }
280 
281 /**
282   * @brief  Clears I2C interrupt status.
283   * @param  None
284   * @retval None.
285   */
I2C_ClearINTStatus(void)286 void I2C_ClearINTStatus(void)
287 {
288   I2C->CTRL &= ~I2C_CTRL_SI;
289 }
290 
291 /**
292   * @brief  Reads a packge of data from slave device.
293   * @param  InitStruct: I2C_WRType
294                 SlaveAddr  : Slave device address
295                 SubAddress : start of slave device sub-address
296                 PageRange  : maximum range of page to Read operation
297                 pBuffer    : Read data pointer
298                 Length     : sum of Read datas
299                 SubAddrType:
300                     I2C_SUBADDR_1BYTE   (Slave device sub-address type: 1 byte)
301                     I2C_SUBADDR_2BYTE   (Slave device sub-address type: 2 bytes)
302                     I2C_SUBADDR_OTHER   (Slave device sub-address type: othres)
303   * @retval 0: true
304             ��0��status code
305                 bit15~8    status code(true)
306                 bit7~0     status code(false)
307   */
I2C_MasterReadBytes(I2C_WRType * InitStruct)308 uint16_t I2C_MasterReadBytes(I2C_WRType *InitStruct)
309 {
310   uint32_t i;
311   uint16_t ret_val;
312 
313   /* Check parameters */
314   assert_parameters(I2C_SUBADDR_TYPE(InitStruct->SubAddrType));
315 
316   I2C_AssertAcknowledgeConfig(ENABLE); //Enable AA
317   /*-------------------------------- START -----------------------------------*/
318   I2C_SendStart();
319   ret_val = I2C_CheckState(0x08);
320   if (ret_val)    return ret_val;
321 
322   /*------------------------------ Send SLA+W --------------------------------*/
323   /* Slave device sub-address type: 1 byte */
324   if (InitStruct->SubAddrType == I2C_SUBADDR_1BYTE)
325   {
326     I2C_SendByte(InitStruct->SlaveAddr);
327     ret_val = I2C_CheckState(0x18);
328     if (ret_val)    return ret_val;
329 
330     I2C_SendByte(InitStruct->SubAddress&0xFF);
331     ret_val = I2C_CheckState(0x28);
332     if (ret_val)    return ret_val;
333   }
334   /* Slave device sub-address type: 2 bytes */
335   if (InitStruct->SubAddrType == I2C_SUBADDR_2BYTE)
336   {
337     I2C_SendByte(InitStruct->SlaveAddr);
338     ret_val = I2C_CheckState(0x18);
339     if (ret_val)    return ret_val;
340 
341     I2C_SendByte((InitStruct->SubAddress>>8)&0xFF);
342     ret_val = I2C_CheckState(0x28);
343     if (ret_val)    return ret_val;
344 
345     I2C_SendByte(InitStruct->SubAddress&0xFF);
346     ret_val = I2C_CheckState(0x28);
347     if (ret_val)    return ret_val;
348   }
349   /* Slave device sub-address type: othres */
350   if (InitStruct->SubAddrType == I2C_SUBADDR_OTHER)
351   {
352     if (InitStruct->PageRange < 256) // 8 + x
353     {
354       I2C_SendByte(InitStruct->SlaveAddr|((InitStruct->SubAddress>>7)&0xE));
355       ret_val = I2C_CheckState(0x18);
356       if (ret_val)    return ret_val;
357 
358       I2C_SendByte(InitStruct->SubAddress&0xFF);
359       ret_val = I2C_CheckState(0x28);
360       if (ret_val)    return ret_val;
361     }
362     else                             // 16 + x
363     {
364       I2C_SendByte(InitStruct->SlaveAddr|((InitStruct->SubAddress>>15)&0xE));
365       ret_val = I2C_CheckState(0x18);
366       if (ret_val)    return ret_val;
367 
368       I2C_SendByte((InitStruct->SubAddress>>8)&0xFF);
369       ret_val = I2C_CheckState(0x28);
370       if (ret_val)    return ret_val;
371 
372       I2C_SendByte(InitStruct->SubAddress&0xFF);
373       ret_val = I2C_CheckState(0x28);
374       if (ret_val)    return ret_val;
375     }
376   }
377 
378   /*------------------------------- Restart ----------------------------------*/
379   I2C_SendRestart();  //restart
380   ret_val = I2C_CheckState(0x10);
381   if (ret_val)    return ret_val;
382 
383   /*----------------------------- Send SLA+R ---------------------------------*/
384     /* Slave device sub-address type: othres */
385   if (InitStruct->SubAddrType == I2C_SUBADDR_OTHER)
386   {
387     if (InitStruct->PageRange < 256) // 8 + x
388       I2C_SendByte(InitStruct->SlaveAddr|0x01|((InitStruct->SubAddress>>7)&0xE));
389     else // 16 + x
390       I2C_SendByte(InitStruct->SlaveAddr|0x01|((InitStruct->SubAddress>>15)&0xE));
391   }
392   else
393     I2C_SendByte(InitStruct->SlaveAddr|0x01);
394 
395   ret_val = I2C_CheckState(0x40);
396   if (ret_val)    return ret_val;
397 
398   /*----------------------------- Read datas ---------------------------------*/
399   for (i=0; i<(InitStruct->Length-1); i++)
400   {
401     *InitStruct->pBuffer = I2C_ReceiveByte();
402     InitStruct->pBuffer++;
403     ret_val = I2C_CheckState(0x50);
404     if (ret_val)    return ret_val;
405   }
406   /*-------------------- Read the last data, disable AA ----------------------*/
407   I2C_AssertAcknowledgeConfig(DISABLE);
408   *InitStruct->pBuffer = I2C_ReceiveByte();
409   ret_val = I2C_CheckState(0x58);
410   if (ret_val)    return ret_val;
411   /*--------------------------------- Stop -----------------------------------*/
412   I2C_SendStop();  //stop
413   return 0;
414 }
415 
416 /**
417   * @brief  Writes a packge of data to slave device.
418   * @param  InitStruct: I2C_WRType
419                 SlaveAddr  : Slave device address
420                 SubAddress : start of slave device sub-address
421                 PageRange  : maximum range of page to write operation
422                 pBuffer    : write data pointer
423                 Length     : sum of write datas
424                 SubAddrType:
425                     I2C_SUBADDR_1BYTE   (Slave device sub-address type: 1 byte)
426                     I2C_SUBADDR_2BYTE   (Slave device sub-address type: 2 bytes)
427                     I2C_SUBADDR_OTHER   (Slave device sub-address type: othres)
428   * @retval 0: true
429             ��0��status code
430                 bit15~8    status code(true)
431                 bit7~0     status code(false)
432   */
I2C_MasterWriteBytes(I2C_WRType * InitStruct)433 uint16_t I2C_MasterWriteBytes(I2C_WRType *InitStruct)
434 {
435   uint16_t ret_val;
436   uint32_t i;
437 
438   /* Check parameters */
439   assert_parameters(I2C_SUBADDR_TYPE(InitStruct->SubAddrType));
440 
441   I2C_AssertAcknowledgeConfig(ENABLE); //Enable AA
442   /*-------------------------------- START -----------------------------------*/
443   I2C_SendStart();
444   ret_val = I2C_CheckState(0x08);
445   if (ret_val)    return ret_val;
446 
447   /*------------------------------ Send SLA+W --------------------------------*/
448   /* Slave device sub-address type: 1 byte */
449   if (InitStruct->SubAddrType == I2C_SUBADDR_1BYTE)
450   {
451     I2C_SendByte(InitStruct->SlaveAddr);
452     ret_val = I2C_CheckState(0x18);
453     if (ret_val)    return ret_val;
454 
455     I2C_SendByte(InitStruct->SubAddress&0xFF);
456     ret_val = I2C_CheckState(0x28);
457     if (ret_val)    return ret_val;
458   }
459   /* Slave device sub-address type: 2 bytes */
460   else if (InitStruct->SubAddrType == I2C_SUBADDR_2BYTE)
461   {
462     I2C_SendByte(InitStruct->SlaveAddr);      //device address
463     ret_val = I2C_CheckState(0x18);
464     if (ret_val)    return ret_val;
465 
466     I2C_SendByte((InitStruct->SubAddress>>8)&0xFF);   //first word address
467     ret_val = I2C_CheckState(0x28);
468     if (ret_val)    return ret_val;
469 
470     I2C_SendByte(InitStruct->SubAddress&0xFF);        //second word address
471     ret_val = I2C_CheckState(0x28);
472     if (ret_val)    return ret_val;
473   }
474   /* Slave device sub-address type: othres */
475   else
476   {
477     if (InitStruct->PageRange < 256) // 8 + x
478     {
479       I2C_SendByte(InitStruct->SlaveAddr|((InitStruct->SubAddress>>7)&0xE));
480       ret_val = I2C_CheckState(0x18);
481       if (ret_val)    return ret_val;
482 
483       I2C_SendByte(InitStruct->SubAddress&0xFF);
484       ret_val = I2C_CheckState(0x28);
485       if (ret_val)    return ret_val;
486     }
487     else                             // 16 + x
488     {
489       I2C_SendByte(InitStruct->SlaveAddr|((InitStruct->SubAddress>>15)&0xE));
490       ret_val = I2C_CheckState(0x18);
491       if (ret_val)    return ret_val;
492 
493       I2C_SendByte((InitStruct->SubAddress>>8)&0xFF);
494       ret_val = I2C_CheckState(0x28);
495       if (ret_val)    return ret_val;
496 
497       I2C_SendByte(InitStruct->SubAddress&0xFF);
498       ret_val = I2C_CheckState(0x28);
499       if (ret_val)    return ret_val;
500     }
501   }
502 
503   /*----------------------------- Write datas --------------------------------*/
504   for (i=0; i<(InitStruct->Length); i++)
505   {
506     /* Reach the page boundary */
507     if ((i > 0) && ((InitStruct->SubAddress+i)%InitStruct->PageRange == 0))
508     {
509       I2C_SendStop();
510       I2C_WaitForCrossPage(InitStruct->SlaveAddr);
511       I2C_SendStart();                //start
512       ret_val = I2C_CheckState(0x08);
513       if (ret_val)    return ret_val;
514       /* WriteAddr: 1 byte */
515       if (InitStruct->SubAddrType == I2C_SUBADDR_1BYTE)
516       {
517         I2C_SendByte(InitStruct->SlaveAddr);
518         ret_val = I2C_CheckState(0x18);
519         if (ret_val)    return ret_val;
520 
521         I2C_SendByte((InitStruct->SubAddress+i)&0xFF);
522         ret_val = I2C_CheckState(0x28);
523         if (ret_val)    return ret_val;
524       }
525       /* WriteAddr: 2 byte */
526       if (InitStruct->SubAddrType == I2C_SUBADDR_2BYTE)
527       {
528         I2C_SendByte(InitStruct->SlaveAddr);      //device address
529         ret_val = I2C_CheckState(0x18);
530         if (ret_val)    return ret_val;
531 
532         I2C_SendByte(((InitStruct->SubAddress+i)>>8)&0xFF);   //first word address
533         ret_val = I2C_CheckState(0x28);
534         if (ret_val)    return ret_val;
535 
536         I2C_SendByte((InitStruct->SubAddress+i)&0xFF);        //second word address
537         ret_val = I2C_CheckState(0x28);
538         if (ret_val)    return ret_val;
539       }
540       /* WriteAddr: (16 or 8)+x*/
541       if (InitStruct->SubAddrType == I2C_SUBADDR_OTHER)
542       {
543         if (InitStruct->PageRange < 256) // 8 + x
544         {
545           I2C_SendByte(InitStruct->SlaveAddr|(((InitStruct->SubAddress+i)>>7)&0xE));
546           ret_val = I2C_CheckState(0x18);
547           if (ret_val)    return ret_val;
548 
549           I2C_SendByte((InitStruct->SubAddress+i)&0xFF);
550           ret_val = I2C_CheckState(0x28);
551           if (ret_val)    return ret_val;
552         }
553         else                             // 16 + x
554         {
555           I2C_SendByte(InitStruct->SlaveAddr|(((InitStruct->SubAddress+i)>>15)&0xE));
556           ret_val = I2C_CheckState(0x18);
557           if (ret_val)    return ret_val;
558 
559           I2C_SendByte(((InitStruct->SubAddress+i)>>8)&0xFF);
560           ret_val = I2C_CheckState(0x28);
561           if (ret_val)    return ret_val;
562 
563           I2C_SendByte((InitStruct->SubAddress+i)&0xFF);
564           ret_val = I2C_CheckState(0x28);
565           if (ret_val)    return ret_val;
566         }
567       }
568 
569       I2C_SendByte(*InitStruct->pBuffer);
570       InitStruct->pBuffer++;
571       ret_val = I2C_CheckState(0x28);
572       if (ret_val)    return ret_val;
573     }
574     /* Not reaching the page boundary */
575     else
576     {
577       I2C_SendByte(*InitStruct->pBuffer);
578       InitStruct->pBuffer++;
579       ret_val = I2C_CheckState(0x28);
580       if (ret_val)    return ret_val;
581     }
582   }
583 
584   I2C_SendStop();
585   I2C_WaitForCrossPage(InitStruct->SlaveAddr);
586   return 0;
587 }
588 
589 /**
590   * @brief  Enables or disables I2C.
591   * @param  NewState:
592                 ENABLE
593                 DISABLE
594   * @retval None.
595   */
I2C_Cmd(uint32_t NewState)596 void I2C_Cmd(uint32_t NewState)
597 {
598   /* Check parameters */
599   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
600 
601   if (NewState == ENABLE)
602     I2C->CTRL |= I2C_CTRL_EN;
603   else
604     I2C->CTRL &= ~I2C_CTRL_EN;
605 }
606 
607 /* I2C Exported Functions Group5:
608                                    Others ------------------------------------*/
609 
610 /**
611   * @brief  Configures sssert acknowledge.
612   * @param  NewState:
613                 ENABLE
614                 DISABLE
615   * @retval None.
616   */
I2C_AssertAcknowledgeConfig(uint32_t NewState)617 void I2C_AssertAcknowledgeConfig(uint32_t NewState)
618 {
619   /* Check parameters */
620   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
621 
622   if (NewState == ENABLE)
623     I2C->CTRL |= I2C_CTRL_AA;
624   else
625     I2C->CTRL &= ~I2C_CTRL_AA;
626 }
627 
628 /**
629   * @brief  Receives a byte data.
630   * @param  None.
631   * @retval Data received.
632   */
I2C_ReceiveData(void)633 uint8_t I2C_ReceiveData(void)
634 {
635   return I2C->DATA;
636 }
637 
638 /**
639   * @brief  Sends a byte data.
640   * @param  Dat:data to transmit.
641   * @retval None
642   */
I2C_SendData(uint8_t Dat)643 void I2C_SendData(uint8_t Dat)
644 {
645   I2C->DATA = Dat;
646 }
647 
648 /**
649   * @brief  Generates start signal.
650   * @param  NewState:
651                 ENABLE
652                 DISABLE
653   * @retval None.
654   */
I2C_GenerateSTART(uint32_t NewState)655 void I2C_GenerateSTART(uint32_t NewState)
656 {
657   /* Check parameters */
658   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
659 
660   if (NewState == ENABLE)
661     I2C->CTRL |= I2C_CTRL_STA;
662   else
663     I2C->CTRL &= ~I2C_CTRL_STA;
664 }
665 
666 /**
667   * @brief  Generates stop signal.
668   * @param  NewState:
669                 ENABLE
670                 DISABLE
671   * @retval None.
672   */
I2C_GenerateSTOP(uint32_t NewState)673 void I2C_GenerateSTOP(uint32_t NewState)
674 {
675   /* Check parameters */
676   assert_parameters(IS_FUNCTIONAL_STATE(NewState));
677 
678   if (NewState == ENABLE)
679     I2C->CTRL |= I2C_CTRL_STO;
680   else
681     I2C->CTRL &= ~I2C_CTRL_STO;
682 }
683 
684 /**
685   * @brief  Gets status code.
686   * @param  None
687   * @retval status code.
688   */
I2C_GetStatusCode(void)689 uint8_t I2C_GetStatusCode(void)
690 {
691   return (I2C->STS&I2C_STS_STS);
692 }
693 
694 /*********************************** END OF FILE ******************************/
695