aboutsummaryrefslogtreecommitdiff
path: root/CH5xx_ble_firmware_library/StdPeriphDriver/CH58x_i2c.c
blob: 48d33556b18537c0d0f2a18a7c47ba2421bd3bb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
/********************************** (C) COPYRIGHT *******************************
 * File Name          : CH58x_i2c.c
 * Author             : WCH
 * Version            : V1.0
 * Date               : 2021/03/15
 * Description
 *********************************************************************************
 * Copyright (c) 2021 Nanjing Qinheng Microelectronics Co., Ltd.
 * Attention: This software (modified or not) and binary are used for 
 * microcontroller manufactured by Nanjing Qinheng Microelectronics.
 *******************************************************************************/

#include "CH58x_common.h"

/*********************************************************************
 * @fn      I2C_Init
 *
 * @brief   Initializes the I2Cx peripheral according to the specified
 *          parameters in the I2C_InitStruct.
 *
 * @param   I2C_Mode        - refer to I2C_ModeTypeDef
 * @param   I2C_ClockSpeed  - Specifies the clock frequency(Hz).
 *                            This parameter must be set to a value lower than 400kHz
 * @param   I2C_DutyCycle   - Specifies the I2C fast mode duty cycle.refer to I2C_DutyTypeDef
 * @param   I2C_Ack         - Enables or disables the acknowledgement.refer to I2C_AckTypeDef
 * @param   I2C_AckAddr     - Specifies if 7-bit or 10-bit address is acknowledged.refer to I2C_AckAddrTypeDef
 * @param   I2C_OwnAddress1 - Specifies the first device own address.
 *                            This parameter can be a 7-bit or 10-bit address.
 *
 * @return  none
 */
void I2C_Init(I2C_ModeTypeDef I2C_Mode, UINT32 I2C_ClockSpeed, I2C_DutyTypeDef I2C_DutyCycle,
              I2C_AckTypeDef I2C_Ack, I2C_AckAddrTypeDef I2C_AckAddr, UINT16 I2C_OwnAddress1)
{
    uint32_t sysClock;
    uint16_t tmpreg;

    I2C_SoftwareResetCmd(ENABLE);
    I2C_SoftwareResetCmd(DISABLE);

    sysClock = GetSysClock();

    R16_I2C_CTRL2 &= ~RB_I2C_FREQ;
    R16_I2C_CTRL2 |= (sysClock / 1000000);

    R16_I2C_CTRL1 &= ~RB_I2C_PE;

    if(I2C_ClockSpeed <= 100000)
    {
        tmpreg = (sysClock / (I2C_ClockSpeed << 1)) & RB_I2C_CCR;

        if(tmpreg < 0x04)
            tmpreg = 0x04;

        R16_I2C_RTR = (((sysClock / 1000000) + 1) > 0x3F) ? 0x3F : ((sysClock / 1000000) + 1);
    }
    else
    {
        if(I2C_DutyCycle == I2C_DutyCycle_2)
        {
            tmpreg = (sysClock / (I2C_ClockSpeed * 3)) & RB_I2C_CCR;
        }
        else
        {
            tmpreg = (sysClock / (I2C_ClockSpeed * 25)) & RB_I2C_CCR;
            tmpreg |= I2C_DutyCycle_16_9;
        }

        if(tmpreg == 0)
        {
            tmpreg |= (uint16_t)0x0001;
        }

        tmpreg |= RB_I2C_F_S;
        R16_I2C_RTR = (uint16_t)((((sysClock / 1000000) * (uint16_t)300) / (uint16_t)1000) + (uint16_t)1);
    }
    R16_I2C_CKCFGR = tmpreg;

    R16_I2C_CTRL1 |= RB_I2C_PE;

    R16_I2C_CTRL1 &= ~(RB_I2C_SMBUS | RB_I2C_SMBTYPE | RB_I2C_ACK);
    R16_I2C_CTRL1 |= I2C_Mode | I2C_Ack;

    R16_I2C_OADDR1 &= ~0xFFFF;
    R16_I2C_OADDR1 |= I2C_AckAddr | I2C_OwnAddress1;
}

/*********************************************************************
 * @fn      I2C_Cmd
 *
 * @brief   Enables or disables the specified I2C peripheral.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_Cmd(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_PE;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_PE;
}

/*********************************************************************
 * @fn      I2C_GenerateSTART
 *
 * @brief   Generates I2Cx communication START condition.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_GenerateSTART(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_START;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_START;
}

/*********************************************************************
 * @fn      I2C_GenerateSTOP
 *
 * @brief   Generates I2Cx communication STOP condition.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_GenerateSTOP(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_STOP;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_STOP;
}

/*********************************************************************
 * @fn      I2C_AcknowledgeConfig
 *
 * @brief   Enables or disables the specified I2C acknowledge feature.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_AcknowledgeConfig(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_ACK;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_ACK;
}

/*********************************************************************
 * @fn      I2C_OwnAddress2Config
 *
 * @brief   Configures the specified I2C own address2.
 *
 * @param   Address     - specifies the 7bit I2C own address2.
 *
 * @return  none
 */
void I2C_OwnAddress2Config(uint8_t Address)
{
    R16_I2C_OADDR2 &= ~RB_I2C_ADD2;
    R16_I2C_OADDR2 |= (uint16_t)(Address & RB_I2C_ADD2);
}

/*********************************************************************
 * @fn      I2C_DualAddressCmd
 *
 * @brief   Enables or disables the specified I2C dual addressing mode.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_DualAddressCmd(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_OADDR2 |= RB_I2C_ENDUAL;
    else
        R16_I2C_OADDR2 &= ~RB_I2C_ENDUAL;
}

/*********************************************************************
 * @fn      I2C_GeneralCallCmd
 *
 * @brief   Enables or disables the specified I2C general call feature.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_GeneralCallCmd(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_ENGC;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_ENGC;
}

/*********************************************************************
 * @fn      I2C_ITConfig
 *
 * @brief   Enables or disables the specified I2C interrupts.
 *
 * @param   I2C_IT      - specifies the I2C interrupts sources to be enabled or disabled.
 *                        I2C_IT_BUF    - Buffer interrupt mask.
 *                        I2C_IT_EVT    - Event interrupt mask.
 *                        I2C_IT_ERR    - Error interrupt mask.
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_ITConfig(I2C_ITTypeDef I2C_IT, FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL2 |= I2C_IT;
    else
        R16_I2C_CTRL2 &= (uint16_t)~I2C_IT;
}

/*********************************************************************
 * @fn      I2C_SendData
 *
 * @brief   Sends a data byte through the I2Cx peripheral.
 *
 * @param   Data    - Byte to be transmitted.
 *
 * @return  none
 */
void I2C_SendData(uint8_t Data)
{
    R16_I2C_DATAR = Data;
}

/*********************************************************************
 * @fn      I2C_ReceiveData
 *
 * @brief   Returns the most recent received data by the I2Cx peripheral.
 *
 * @return  The value of the received data.
 */
uint8_t I2C_ReceiveData(void)
{
    return (uint8_t)R16_I2C_DATAR;
}

/*********************************************************************
 * @fn      I2C_Send7bitAddress
 *
 * @brief   Transmits the address byte to select the slave device.
 *
 * @param   Address         - specifies the slave address which will be transmitted.
 * @param   I2C_Direction   - specifies whether the I2C device will be a Transmitter or a Receiver.
 *                            I2C_Direction_Transmitter - Transmitter mode.
 *                            I2C_Direction_Receiver    - Receiver mode.
 *
 * @return  none
 */
void I2C_Send7bitAddress(uint8_t Address, uint8_t I2C_Direction)
{
    if(I2C_Direction != I2C_Direction_Transmitter)
        Address |= OADDR1_ADD0_Set;
    else
        Address &= OADDR1_ADD0_Reset;

    R16_I2C_DATAR = Address;
}

/*********************************************************************
 * @fn      I2C_SoftwareResetCmd
 *
 * @brief   Enables or disables the specified I2C software reset.
 *
 * @param   NewState        - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_SoftwareResetCmd(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_SWRST;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_SWRST;
}

/*********************************************************************
 * @fn      I2C_NACKPositionConfig
 *
 * @brief   Selects the specified I2C NACK position in master receiver mode.
 *
 * @param   I2C_NACKPosition    - specifies the NACK position.
 *                                I2C_NACKPosition_Next     - indicates that the next byte will be the last received byte.
 *                                I2C_NACKPosition_Current  - indicates that current byte is the last received byte.
 *
 * @return  none
 */
void I2C_NACKPositionConfig(uint16_t I2C_NACKPosition)
{
    if(I2C_NACKPosition == I2C_NACKPosition_Next)
        R16_I2C_CTRL1 |= I2C_NACKPosition_Next;
    else
        R16_I2C_CTRL1 &= I2C_NACKPosition_Current;
}

/*********************************************************************
 * @fn      I2C_SMBusAlertConfig
 *
 * @brief   Drives the SMBusAlert pin high or low for the specified I2C.
 *
 * @param   I2C_SMBusAlert      - specifies SMBAlert pin level.
 *                              I2C_SMBusAlert_Low  - SMBAlert pin driven low.
 *                              I2C_SMBusAlert_High - SMBAlert pin driven high.
 *
 * @return  none
 */
void I2C_SMBusAlertConfig(uint16_t I2C_SMBusAlert)
{
    if(I2C_SMBusAlert == I2C_SMBusAlert_Low)
        R16_I2C_CTRL1 |= I2C_SMBusAlert_Low;
    else
        R16_I2C_CTRL1 &= I2C_SMBusAlert_High;
}

/*********************************************************************
 * @fn      I2C_TransmitPEC
 *
 * @brief   Enables or disables the specified I2C PEC transfer.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_TransmitPEC(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_PEC;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_PEC;
}

/*********************************************************************
 * @fn      I2C_PECPositionConfig
 *
 * @brief   Selects the specified I2C PEC position.
 *
 * @param   I2C_PECPosition     - specifies the PEC position.
 *                                I2C_PECPosition_Next      - indicates that the next byte is PEC.
 *                                I2C_PECPosition_Current   - indicates that current byte is PEC.
 *
 * @return  none
 */
void I2C_PECPositionConfig(uint16_t I2C_PECPosition)
{
    if(I2C_PECPosition == I2C_PECPosition_Next)
        R16_I2C_CTRL1 |= I2C_PECPosition_Next;
    else
        R16_I2C_CTRL1 &= I2C_PECPosition_Current;
}

/*********************************************************************
 * @fn      I2C_CalculatePEC
 *
 * @brief   Enables or disables the PEC value calculation of the transferred bytes.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_CalculatePEC(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_ENPEC;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_ENPEC;
}

/*********************************************************************
 * @fn      I2C_GetPEC
 *
 * @brief   Returns the PEC value for the specified I2C.
 *
 * @return  The PEC value.
 */
uint8_t I2C_GetPEC(void)
{
    return (R16_I2C_STAR2 >> 8);
}

/*********************************************************************
 * @fn      I2C_ARPCmd
 *
 * @brief   Enables or disables the specified I2C ARP.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_ARPCmd(FunctionalState NewState)
{
    if(NewState != DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_EBARP;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_EBARP;
}

/*********************************************************************
 * @fn      I2C_StretchClockCmd
 *
 * @brief   Enables or disables the specified I2C Clock stretching.
 *
 * @param   NewState    - ENABLE or DISABLE.
 *
 * @return  none
 */
void I2C_StretchClockCmd(FunctionalState NewState)
{
    if(NewState == DISABLE)
        R16_I2C_CTRL1 |= RB_I2C_NOSTRETCH;
    else
        R16_I2C_CTRL1 &= ~RB_I2C_NOSTRETCH;
}

/*********************************************************************
 * @fn      I2C_FastModeDutyCycleConfig
 *
 * @brief   Selects the specified I2C fast mode duty cycle.
 *
 * @param   I2C_DutyCycle   - specifies the fast mode duty cycle.
 *                            I2C_DutyCycle_2       - I2C fast mode Tlow/Thigh = 2.
 *                            I2C_DutyCycle_16_9    - I2C fast mode Tlow/Thigh = 16/9.
 *
 * @return  none
 */
void I2C_FastModeDutyCycleConfig(uint16_t I2C_DutyCycle)
{
    if(I2C_DutyCycle != I2C_DutyCycle_16_9)
        R16_I2C_CKCFGR &= ~I2C_DutyCycle_16_9;
    else
        R16_I2C_CKCFGR |= I2C_DutyCycle_16_9;
}

/*********************************************************************
 * @fn      I2C_CheckEvent
 *
 * @brief   Checks whether the last I2Cx Event is equal to the one passed as parameter.
 *
 * @param   I2C_EVENT   - specifies the event to be checked.
 *                        I2C_EVENT_SLAVE_TRANSMITTER_ADDRESS_MATCHED           : EV1.
 *                        I2C_EVENT_SLAVE_RECEIVER_ADDRESS_MATCHED              : EV1.
 *                        I2C_EVENT_SLAVE_TRANSMITTER_SECONDADDRESS_MATCHED     : EV1.
 *                        I2C_EVENT_SLAVE_RECEIVER_SECONDADDRESS_MATCHED        : EV1.
 *                        I2C_EVENT_SLAVE_GENERALCALLADDRESS_MATCHED            : EV1.
 *                        I2C_EVENT_SLAVE_BYTE_RECEIVED                         : EV2.
 *                        (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_DUALF)      : EV2.
 *                        (I2C_EVENT_SLAVE_BYTE_RECEIVED | I2C_FLAG_GENCALL)    : EV2.
 *                        I2C_EVENT_SLAVE_BYTE_TRANSMITTED                      : EV3.
 *                        (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_DUALF)   : EV3.
 *                        (I2C_EVENT_SLAVE_BYTE_TRANSMITTED | I2C_FLAG_GENCALL) : EV3.
 *                        I2C_EVENT_SLAVE_ACK_FAILURE                           : EV3_2.
 *                        I2C_EVENT_SLAVE_STOP_DETECTED                         : EV4.
 *                        I2C_EVENT_MASTER_MODE_SELECT                          : EV5.
 *                        I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED            : EV6.
 *                        I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED               : EV6.
 *                        I2C_EVENT_MASTER_BYTE_RECEIVED                        : EV7.
 *                        I2C_EVENT_MASTER_BYTE_TRANSMITTING                    : EV8.
 *                        I2C_EVENT_MASTER_BYTE_TRANSMITTED                     : EV8_2.
 *                        I2C_EVENT_MASTER_MODE_ADDRESS10                       : EV9.
 *
 * @return  1 - SUCCESS or 0 - ERROR.
 */
uint8_t I2C_CheckEvent(uint32_t I2C_EVENT)
{
    uint32_t lastevent = 0;
    uint32_t flag1 = 0, flag2 = 0;
    uint8_t  status = 0;

    flag1 = R16_I2C_STAR1;
    flag2 = R16_I2C_STAR2;
    flag2 = flag2 << 16;

    lastevent = (flag1 | flag2) & FLAG_Mask;

    if((lastevent & I2C_EVENT) == I2C_EVENT)
    {
        status = !0;
    }
    else
    {
        status = 0;
    }

    return status;
}

/*********************************************************************
 * @fn      I2C_GetLastEvent
 *
 * @brief   Returns the last I2Cx Event.
 *
 * @return  The last event.
 */
uint32_t I2C_GetLastEvent(void)
{
    uint32_t lastevent = 0;
    uint32_t flag1 = 0, flag2 = 0;

    flag1 = R16_I2C_STAR1;
    flag2 = R16_I2C_STAR2;
    flag2 = flag2 << 16;
    lastevent = (flag1 | flag2) & FLAG_Mask;

    return lastevent;
}

/*********************************************************************
 * @fn      I2C_GetFlagStatus
 *
 * @brief   Checks whether the last I2Cx Event is equal to the one passed as parameter.
 *
 * @param   I2C_FLAG    - specifies the flag to check.
 *                        I2C_FLAG_DUALF        - Dual flag (Slave mode).
 *                        I2C_FLAG_SMBHOST      - SMBus host header (Slave mode).
 *                        I2C_FLAG_SMBDEFAULT   - SMBus default header (Slave mode).
 *                        I2C_FLAG_GENCALL      - General call header flag (Slave mode).
 *                        I2C_FLAG_TRA          - Transmitter/Receiver flag.
 *                        I2C_FLAG_BUSY         - Bus busy flag.
 *                        I2C_FLAG_MSL          - Master/Slave flag.
 *                        I2C_FLAG_SMBALERT     - SMBus Alert flag.
 *                        I2C_FLAG_TIMEOUT      - Timeout or Tlow error flag.
 *                        I2C_FLAG_PECERR       - PEC error in reception flag.
 *                        I2C_FLAG_OVR          - Overrun/Underrun flag (Slave mode).
 *                        I2C_FLAG_AF           - Acknowledge failure flag.
 *                        I2C_FLAG_ARLO         - Arbitration lost flag (Master mode).
 *                        I2C_FLAG_BERR         - Bus error flag.
 *                        I2C_FLAG_TXE          - Data register empty flag (Transmitter).
 *                        I2C_FLAG_RXNE         - Data register not empty (Receiver) flag.
 *                        I2C_FLAG_STOPF        - Stop detection flag (Slave mode).
 *                        I2C_FLAG_ADD10        - 10-bit header sent flag (Master mode).
 *                        I2C_FLAG_BTF          - Byte transfer finished flag.
 *                        I2C_FLAG_ADDR         - Address sent flag (Master mode) "ADSL"
 *                                                Address matched flag (Slave mode)"ENDA".
 *                        I2C_FLAG_SB           - Start bit flag (Master mode).
 *
 * @return  FlagStatus  - SET or RESET.
 */
FlagStatus I2C_GetFlagStatus(uint32_t I2C_FLAG)
{
    FlagStatus    bitstatus = RESET;
    __IO uint32_t i2creg = 0, i2cxbase = 0;

    i2cxbase = (uint32_t)BA_I2C;
    i2creg = I2C_FLAG >> 28;
    I2C_FLAG &= FLAG_Mask;

    if(i2creg != 0)
    {
        i2cxbase += 0x14;
    }
    else
    {
        I2C_FLAG = (uint32_t)(I2C_FLAG >> 16);
        i2cxbase += 0x18;
    }

    if(((*(__IO uint32_t *)i2cxbase) & I2C_FLAG) != (uint32_t)RESET)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }

    return bitstatus;
}

/*********************************************************************
 * @fn      I2C_ClearFlag
 *
 * @brief   Clears the I2Cx's pending flags.
 *
 * @param   I2C_FLAG    - specifies the flag to clear.
 *                        I2C_FLAG_SMBALERT     - SMBus Alert flag.
 *                        I2C_FLAG_TIMEOUT      - Timeout or Tlow error flag.
 *                        I2C_FLAG_PECERR       - PEC error in reception flag.
 *                        I2C_FLAG_OVR          - Overrun/Underrun flag (Slave mode).
 *                        I2C_FLAG_AF           - Acknowledge failure flag.
 *                        I2C_FLAG_ARLO         - Arbitration lost flag (Master mode).
 *                        I2C_FLAG_BERR         - Bus error flag.
 *
 * @return  none
 */
void I2C_ClearFlag(uint32_t I2C_FLAG)
{
    uint32_t flagpos = 0;

    flagpos = I2C_FLAG & FLAG_Mask;
    R16_I2C_STAR1 = (uint16_t)~flagpos;
}

/*********************************************************************
 * @fn      I2C_GetITStatus
 *
 * @brief   Checks whether the specified I2C interrupt has occurred or not.
 *
 * @param   II2C_IT     - specifies the interrupt source to check.
 *                        I2C_FLAG_SMBALERT     - SMBus Alert flag.
 *                        I2C_FLAG_TIMEOUT      - Timeout or Tlow error flag.
 *                        I2C_FLAG_PECERR       - PEC error in reception flag.
 *                        I2C_FLAG_OVR          - Overrun/Underrun flag (Slave mode).
 *                        I2C_FLAG_AF           - Acknowledge failure flag.
 *                        I2C_FLAG_ARLO         - Arbitration lost flag (Master mode).
 *                        I2C_FLAG_BERR         - Bus error flag.
 *                        I2C_FLAG_TXE          - Data register empty flag (Transmitter).
 *                        I2C_FLAG_RXNE         - Data register not empty (Receiver) flag.
 *                        I2C_FLAG_STOPF        - Stop detection flag (Slave mode).
 *                        I2C_FLAG_ADD10        - 10-bit header sent flag (Master mode).
 *                        I2C_FLAG_BTF          - Byte transfer finished flag.
 *                        I2C_FLAG_ADDR         - Address sent flag (Master mode) "ADSL"
 *                                                Address matched flag (Slave mode)"ENDA".
 *                        I2C_FLAG_SB           - Start bit flag (Master mode).
 *
 * @return  none
 */
ITStatus I2C_GetITStatus(uint32_t I2C_IT)
{
    ITStatus bitstatus = RESET;
    uint32_t enablestatus = 0;

    enablestatus = (uint32_t)(((I2C_IT & ITEN_Mask) >> 16) & (R16_I2C_CTRL2));
    I2C_IT &= FLAG_Mask;

    if(((R16_I2C_STAR1 & I2C_IT) != (uint32_t)RESET) && enablestatus)
    {
        bitstatus = SET;
    }
    else
    {
        bitstatus = RESET;
    }

    return bitstatus;
}

/*********************************************************************
 * @fn      I2C_ClearITPendingBit
 *
 * @brief   Clears the I2Cx interrupt pending bits.
 *
 * @param   I2C_IT      - specifies the interrupt pending bit to clear.
 *                        I2C_IT_SMBALERT   - SMBus Alert interrupt.
 *                        I2C_IT_TIMEOUT    - Timeout or Tlow error interrupt.
 *                        I2C_IT_PECERR     - PEC error in reception  interrupt.
 *                        I2C_IT_OVR        - Overrun/Underrun interrupt (Slave mode).
 *                        I2C_IT_AF         - Acknowledge failure interrupt.
 *                        I2C_IT_ARLO       - Arbitration lost interrupt (Master mode).
 *                        I2C_IT_BERR       - Bus error interrupt.
 *
 * @return  none
 */
void I2C_ClearITPendingBit(uint32_t I2C_IT)
{
    uint32_t flagpos = 0;

    flagpos = I2C_IT & FLAG_Mask;
    R16_I2C_STAR1 = (uint16_t)~flagpos;
}