tao_z
2022-05-29 fa8669b0092240642af78e84c0e89f596444fdad
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
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
/**
  ******************************************************************************
  * @file     xl_i2c.c
  * @author   kirk  ,xu.wang
  * @version  4.5.2
  * @date     Fri Mar 26 17:29:12 2021
  * @brief    This file provide function about I2C firmware program 
  ******************************************************************************
  * @attention
    *
  * 2019 by Chipways Communications,Inc. All Rights Reserved.
  * This software is supplied under the terms of a license
  * agreement or non-disclosure agreement with Chipways.
  * Passing on and copying of this document,and communication
  * of its contents is not permitted without prior written
  * authorization.
  *
  * <h2><center>&copy; COPYRIGHT 2019 Chipways</center></h2>
  ******************************************************************************
  */
 
#if defined(__cplusplus)
extern "C" {
#endif /* __cplusplus */
 
/* Includes ---------------------------------------------------------------*/
#include "xl_i2c.h"
    
    
/** @addtogroup XL6600_StdPeriph_Driver
  * @{
  */
 
/** @defgroup I2C   I2C Module 
  * @brief I2C Driver Modules Library
  * @{
  */
 
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/    
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/
 
/** @defgroup I2C_Private_Functions 
  * @{
  */ 
 
/**
 * @brief  I2C»Ö¸´³õʼ״̬
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @retval None
 */
void I2C_Deinit(I2C_Type *I2Cx)
{
   I2Cx->ENABLE =  0x0000u;        
   I2Cx->CR     =  0x007Fu;      
   I2Cx->TAR    =  0x0055u;    
   I2Cx->SAR    =  0x0055u;    
   I2Cx->SSCH   =  0x0190u;    
   I2Cx->SSCL   =  0x01d6u;    
   I2Cx->FSCH   =  0x003cu;    
   I2Cx->FSCL   =  0x0082u;    
   I2Cx->RXTL   =  0x0000u;    
   I2Cx->TXTL   =  0x0000u;    
 
}
/**
 * @brief ÉèÖÃͨѶËÙ¶È
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  I2C_SourceClk: I2CʱÖÓÔ´Ñ¡Ôñ£¬ÏµÍ³Ê±ÖÓ
 * @param  I2C_SpeedMode: Í¨Ñ¶ËÙ¶Èģʽ
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C_StandardMode £º ±ê׼ģʽ 100k
 *            @arg I2C_FastMode £º     ¿ìËÙģʽ 400k
 *            @arg I2C_HighspeedMode £º¸ßËÙģʽ 3.4M
 * @retval None
 */
static void I2C_SetSpeed(I2C_Type *I2Cx, uint16_t I2C_SourceClk,uint16_t I2C_SpeedMode)
{
     uint16_t IC_HCNT;
     uint16_t IC_LCNT;
    /* set I2C speed in standaed mode */    
    if(I2C_SpeedMode == I2C_StandardMode)
    {
        IC_HCNT=(uint16_t)(4000u*(I2C_SourceClk)/1000u);
        I2Cx->SSCH = (uint32_t)IC_HCNT;
        IC_LCNT=(uint16_t)(4700u*(I2C_SourceClk)/1000u);
        I2Cx->SSCL = (uint32_t)IC_LCNT;        
    }
    /* set I2C speed in fast mode */    
    else
    {
        IC_HCNT=(uint16_t)(600u*(I2C_SourceClk)/1000u);
        I2Cx->FSCH = (uint32_t)IC_HCNT;
        IC_LCNT=(uint16_t)(1300u*(I2C_SourceClk)/1000u);
        I2Cx->FSCL = (uint32_t)IC_LCNT;                
    }
}    
 
/**
 * @brief ³õʼ»¯I2CÄ£¿é
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param I2C_InitStruct I2C ³õʼ»¯µÄ²ÎÊý½á¹¹Ìå
 * @retval None
 */
void I2C_Init(I2C_Type *I2Cx, const I2C_InitTypeDef * I2C_InitStruct)
{
    uint32_t temp1;
    
    /* Set the I2Cx  control register */
    I2Cx->CR = (I2C_InitStruct->I2C_MasterModeEn | I2C_InitStruct->I2C_SlaveModeDis |\
                            I2C_InitStruct->I2C_SendRestart | I2C_InitStruct->I2C_SpeedMode |\
                            I2C_InitStruct->I2C_SADmode | I2C_InitStruct->I2C_MADmode);
    
    /* set I2C speed  */        
    I2C_SetSpeed(I2Cx,I2C_InitStruct->I2C_SourceClk,I2C_InitStruct->I2C_SpeedMode);
 
    /*  IC_RXTL : I2C Receive FIFO Threshold Register */
    I2Cx->RXTL = I2C_InitStruct->I2C_RXTL;
    
    /*  IC_TXTL : I2C Transmit FIFO Threshold Register */
    I2Cx->TXTL = I2C_InitStruct->I2C_TXTL;
    
    /*************************only used in master mode ***************************/                
    /* Set the I2Cx  TAR register,only used in master mode */        
    temp1 = I2Cx->TAR;
    
    temp1 &= ~I2C_TAR_TAD_MASK;    
    
    temp1 |= I2C_InitStruct->I2C_TargetAddress;        
    
    I2Cx->TAR = temp1;    
 
    /*************************only used in slave mode ***************************/                
    /* IC_SAR : I2C slave address register */    
    I2Cx->SAR = I2C_InitStruct->I2C_SlaveAddress;
}
    
 
/**
 * @brief I2C Ê¹ÄÜ
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  NewState: ×´Ì¬Ñ¡Ôñ
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg ENABLE £ºÊ¹ÄÜ
 *            @arg DISENABLE £ºÊ§ÄÜ
 * @retval None
 */
void I2C_EnableCmd(I2C_Type *I2Cx, FunctionalState NewState)
{
    /* enable the I2C moudle */
    if(NewState != DISABLE )
    {
        /* Enable the selected I2C peripheral */
        I2Cx->ENABLE |=  I2C_ENABLE_EN_MASK ;
    }
    else 
    {
        /* Disable the selected I2C peripheral */
        I2Cx->ENABLE &= ~I2C_ENABLE_EN_MASK;
    }
}
 
/**
 * @brief I2C ²úÉúÒ»¸öCALLÃüÁî
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @retval None
 */
void I2C_GeneralCall( I2C_Type* I2Cx)
{
    /* set special mode */    
    I2Cx->TAR |=I2C_TAR_SPECIAL_MASK;
    
    /* set General Call */        
    I2Cx->TAR &= ~I2C_TAR_GOS_MASK;
}
 
 
/**
 * @brief I2CͨÓõ÷ÓÃÓ¦´ðʹÄÜ
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  NewState: ×´Ì¬Ñ¡Ôñ
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg ENABLE £ºÊ¹ÄÜ
 *            @arg DISENABLE £ºÊ§ÄÜ
 * @retval None
 */
void I2C_GeneralCallAckEnableCmd(I2C_Type* I2Cx, FunctionalState NewState)
{
    if(NewState != DISABLE )
    {
        I2Cx->ENABLE |=  I2C_AGC_ACKGC_MASK ;
    }
    else 
    {
        I2Cx->ENABLE &= ~I2C_AGC_ACKGC_MASK;
    }
}
 
 
/**
 * @brief I2C ²úÉúÒ»¸öStartByteÃüÁî
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  TargetAddress: Ä¿±êµØÖ·
 * @retval None
 */
void I2C_StartByte(I2C_Type* I2Cx, uint32_t TargetAddress)
{    
    uint32_t temp;
    
    temp = I2Cx->TAR;
    
    temp &= ~I2C_TAR_TAD_MASK;
    
    temp |= (I2C_TAR_SPECIAL_MASK | I2C_TAR_GOS_MASK | TargetAddress);
    
    I2Cx->TAR = temp;
}
 
 
/**
 * @brief I2C ÖжÏʹÄܺ¯Êý
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  I2C_Interrupt: ÖжÏÔ´
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg    I2C_RXUNDERInterrupt     RX UNDER Interrupt 
 *            @arg     I2C_RXOVERInterrupt      RX OVER Interrupt
 *            @arg     I2C_RXFULLInterrupt      RX FULL Interrupt
 *            @arg     I2C_TXOVERInterrupt      TX OVER Interrupt 
 *            @arg     I2C_TXEMPTYInterrupt     TX EMPTY Interrupt
 *            @arg     I2C_RDREQInterrupt       RD REQ Interrupt 
 *            @arg     I2C_TXABRTInterrupt      TX ABRT Interrupt
 *            @arg     I2C_RXDONEInterrupt      RX DONE Interrupt 
 *            @arg     I2C_ACTIVITYInterrupt    ACTIVITY Interrupt
 *            @arg     I2C_STOPDETInterrupt     STOP DET Interrupt
 *            @arg     I2C_STARTDETInterrupt    START DET Interrupt 
 *            @arg     I2C_GENCALLInterrupt     GEN CALL Interrupt
 * @param  NewState: ×´Ì¬Ñ¡Ôñ
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg ENABLE £ºÊ¹ÄÜ
 *            @arg DISENABLE £ºÊ§ÄÜ
 * @retval None
 */
void I2C_InterruptEn(I2C_Type *I2Cx, const I2C_InterruptTypeDef I2C_Interrupt, FunctionalState NewState)
{
  uint16_t itmask;
 
  /* Get the interrupt enable index */
  itmask = (uint16_t)((uint32_t)1u << (uint8_t)I2C_Interrupt);
    
  if (NewState != DISABLE)
  {
    /* Enable the selected I2C interrupts */
    I2Cx->INTRM |= (uint32_t)itmask;
  }
  else
  {
    /* Disable the selected I2C interrupts */
    I2Cx->INTRM &= ~(uint32_t)itmask;
  }
}
/**
 * @brief I2C µÃµ½ÖжÏ״̬º¯Êý
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  I2C_IntStatusType: ÖжÏÔ´
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg    I2C_RXUNDERInterruptStatus      RX UNDER Interrupt Masked Status.
 *            @arg    I2C_RXOVERInterruptStatus       RX OVER Interrupt Masked Status.
 *            @arg    I2C_RXFULLInterruptStatus       RX FULL Interrupt Masked Status. 
 *            @arg    I2C_TXOVERInterruptStatus       TX OVER Interrupt Masked Status. 
 *            @arg    I2C_TXEMPTYInterruptStatus      TX EMPTY Interrupt Masked Status.
 *            @arg    I2C_RDREQInterruptStatus        RD REQ Interrupt Masked Status. 
 *            @arg    I2C_TXABRTInterruptStatus       TX ABRT Interrupt Masked Status. 
 *            @arg    I2C_RXDONEInterruptStatus       RX DONE Interrupt Masked Status. 
 *            @arg    I2C_ACTIVITYInterruptStatus     ACTIVITY Interrupt Masked Status. 
 *            @arg    I2C_STOPDETInterruptStatus      STOP DET Interrupt Masked Status. 
 *            @arg    I2C_STARTDETInterruptStatus     START DET Interrupt Masked Status. 
 *            @arg    I2C_GENCALLInterruptStatus      GEN CALL Interrupt Masked Status. 
 * @retval ·µ»ØÖжÏ״̬
 */
uint16_t I2C_GetMaskedIntStatus(const I2C_Type *I2Cx, I2C_ITStatusMaskedDef I2C_IntStatusType)
{
    uint16_t IntStatusTemp;
    /* Get all the interrupt status */
    IntStatusTemp =(uint16_t)(I2Cx->IS);
    
    /* get the selected interrupt status */
    return (uint16_t)((uint32_t)IntStatusTemp & ((uint32_t)1 << (uint8_t)I2C_IntStatusType));
}
 
 
/**
 * @brief I2C µÃµ½I2CԭʼÖжÏ״̬
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  I2C_RawIntStatusType: ÖжÏÔ´
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg    I2C_RXUNDERInterruptRawStatus   RX UNDER Interrupt Raw Status. 
 *            @arg    I2C_RXOVERInterruptRawStatus    RX OVER InterruptRawStatus. 
 *            @arg    I2C_RXFULLInterruptRawStatus    RX FULL InterruptRawStatus.  
 *            @arg    I2C_TXOVERInterruptRawStatus    TX OVER InterruptRawStatus. 
 *            @arg    I2C_TXEMPTYInterruptRawStatus   TX EMPTY InterruptRawStatus. 
 *            @arg    I2C_RDREQInterruptRawStatus     RD REQ Interrupt Raw Status.  
 *            @arg    I2C_TXABRTInterruptRawStatus    TX ABRT Interrupt Raw Status. 
 *            @arg    I2C_RXDONEInterruptRawStatus    RX DONE Interrupt Raw Status. 
 *            @arg    I2C_ACTIVITYInterruptRawStatus  ACTIVITY Interrupt Raw Status. 
 *            @arg    I2C_STOPDETInterruptRawStatus   STOP DET Interrupt Raw Status.  
 *            @arg    I2C_STARTDETInterruptRawStatus  START DET Interrupt Raw Status. 
 *            @arg    I2C_GENCALLInterruptRawStatus   GEN CALL Interrupt Raw Status. 
 * @retval ·µ»ØÖжÏ״̬
 */
uint16_t I2C_GetRawIntgStatus(const I2C_Type *I2Cx,I2C_RawITStatusTypeDef I2C_RawIntStatusType)
{
    uint16_t RawIntStatusTemp;
 
    /* Get all the Raw interrupt status */
    RawIntStatusTemp =(uint16_t)(I2Cx->RIS) ;
    
    /* get the selected Raw interrupt status */
    return (uint16_t)((uint32_t)RawIntStatusTemp & ((uint32_t)1u << (uint8_t)I2C_RawIntStatusType));
}
 
/**
 * @brief I2C Çå³ýÖжϱêÖ¾
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  I2C_Interrupt2Clear: ÖжÏÔ´
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg    I2C_AllInterruptClear          Combined and Individual Clear.
 *            @arg    I2C_RXUNDERInterruptClear      Clear RX UNDER Interrupt  
 *            @arg    I2C_RXOVERInterruptClear       Clear RX OVER Interrupt
 *            @arg    I2C_TXOVERInterruptClear       Clear TX OVER Interrupt 
 *            @arg    I2C_RDREQInterruptClear        Clear RD REQ Interrupt 
 *            @arg    I2C_TXABRTInterruptClear       Clear TX ABRT Interrupt 
 *            @arg    I2C_RXDONEInterruptClear       Clear RX DONE Interrupt 
 *            @arg    I2C_ACTIVITYInterruptClear     Clear ACTIVITY Interrupt 
 *            @arg    I2C_STOPDETInterruptClear      Clear STOP DET Interrupt 
 *            @arg    I2C_STARTDETInterruptClear     Clear START DET Interrupt 
 *            @arg    I2C_GENCALLInterruptClear      Clear GEN CALL Interrupt 
 * @retval ·µ»ØÖжÏ״̬
 */
uint16_t I2C_ClearInterrupt(const I2C_Type *I2Cx, I2C_InterruptClearDef I2C_Interrupt2Clear)
{
    uint16_t interrupt2clear;
     
    /*!< Clear GEN_CALL Interrupt Register */
    if(I2C_Interrupt2Clear == I2C_GENCALLInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CGC);
    }
    /*!< Clear START_DET Interrupt Register */
    else if(I2C_Interrupt2Clear == I2C_STARTDETInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CSTART);
    }
    /*!< Clear STOP_DET Interrupt Register */
    else if(I2C_Interrupt2Clear == I2C_STOPDETInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CSTOP);
    }    
    /*!< Clear ACTIVITY Interrupt Register */
    else if(I2C_Interrupt2Clear == I2C_ACTIVITYInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CACT);
    }
    /*!< Clear RX_DONE Interrupt Register */
    else if(I2C_Interrupt2Clear==I2C_RXDONEInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CRD);        
    }
    /*!< Clear TX_ABRT Interrupt Register */
    else if(I2C_Interrupt2Clear==I2C_TXABRTInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CTXA);        
    }
    /*!< Clear RD_REQ Interrupt Register */
    else if(I2C_Interrupt2Clear==I2C_RDREQInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CRR);        
    }
    /*!< Clear TX_OVER Interrupt Register */
    else if(I2C_Interrupt2Clear==I2C_TXOVERInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CTO);        
    }
    /*!< Clear RX_OVER Interrupt Register */
    else if(I2C_Interrupt2Clear==I2C_RXOVERInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CRO);        
    }
    /*!< Clear RX_UNDER Interrupt Register */
    else if(I2C_Interrupt2Clear == I2C_RXUNDERInterruptClear)
    {
        interrupt2clear=(uint16_t)(I2Cx->CRU);    
    }
    /* Clear all Interrupt */
    else
    {
        interrupt2clear=(uint16_t)(I2Cx->CCI);            
    }
    
    return interrupt2clear;
}
 
/**
 * @brief I2C »ñÈ¡I2C״̬
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  I2C_StatusType: ×´Ì¬ÀàÐÍ
 *            @arg    I2C_ActivityStatus                    I2C Activity Status. 
 *            @arg      I2C_TransmitFIFONotFullStatus         Transmit FIFO Not Full.
 *            @arg    I2C_TransmitFIFOEmptyStatus           Transmit FIFO Completely Empty
 *            @arg    I2C_ReceiveFIFONotEmptyStatus         Receive FIFO not Empty.
 *            @arg    I2C_ReceiveFIFOFullStatus             Receive FIFO Completely Full. 
 *            @arg    I2C_MasterFSMActStatus                Master FSM Activity Status. 
 *            @arg    I2C_SlaveFSMActStatus                 Slave FSM Activity Status. 
 * @retval ·µ»ØÖжÏ״̬
 */
uint8_t I2C_GetStatus(const I2C_Type *I2Cx, I2C_StatusTypeDef I2C_StatusType)
{
    uint8_t StatusTemp;
    /* Get all the status */
    StatusTemp =(uint8_t)(I2Cx->STATUS);
 
    /* get the selected status */
    return (StatusTemp &(uint8_t)(1u << (uint8_t)I2C_StatusType));
}
 
 
/**
 * @brief I2C »ñÈ¡I2CÖÕÖ¹Ô´
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  I2C_ABRTSourceType: ÖÕÖ¹Ô´
 * @retval ·µ»ØÖжÏ״̬
 */
uint16_t I2C_GetAbortSource(const I2C_Type *I2Cx, I2C_ABRTSourceTypeDef I2C_ABRTSourceType)
{
    uint16_t StatusTemp;
    /* Get all the status */
    StatusTemp = (uint16_t)(I2Cx->TXAS);
 
    /* get the selected status */
    return (uint16_t)((uint32_t)StatusTemp & ((uint32_t)1u << (uint8_t)I2C_ABRTSourceType));
}
 
/**
 * @brief I2C »ñÈ¡I2CʹÄÜ״̬
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  I2C_EnableStatusType: Ê¹ÄܵÄ״̬
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg    I2C_ENStatusI2CENStatus                  I2C_en Status.
 *            @arg    I2C_ENStatusSlaveRXOperationAborted      Slave-Receiver Operation Aborted.  
 *            @arg  I2C_ENStatusSlaveFIFOFilledAndFlushed   Slave FIFO Filled and Flushed.  
 * @retval ·µ»ØÖжÏ״̬
 */
uint8_t I2C_GetEnableStatus(const I2C_Type *I2Cx, I2C_EnableStatusTypeDef I2C_EnableStatusType)
{
    uint8_t StatusTemp;
    /* Get all the status */
    StatusTemp =(uint8_t)(I2Cx->ES);
 
    /* get the selected status */
    return (StatusTemp &(uint8_t)(1u << (uint8_t)I2C_EnableStatusType));
}
 
/**
 * @brief I2C »ñÈ¡I2C FIFOµÄ״̬
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  FIFOLevelDef: FIFOµÄ״̬Դ
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C_TransmitFIFOLevel               Transmit FIFO Level
 *            @arg  I2C_ReceiveFIFOLevel               Receive FIFO Level 
 * @retval ·µ»ØÖжÏ״̬
 */
uint8_t I2C_GetFIFOLev(const I2C_Type *I2Cx, I2C_TXRXFIFOLevelDef FIFOLevelDef)
{
    uint8_t fifolevel;
     
    /* Check the FIFO Level is transmit or receive*/
    if(FIFOLevelDef==I2C_TransmitFIFOLevel)
    {
        fifolevel=(uint8_t)(I2Cx->TXFLR &I2C_TXFLR_TXFL_MASK);
    }
    else
    {
        fifolevel=(uint8_t)(I2Cx->RXFLR &I2C_RXFLR_RXFL_MASK);        
    }
  /* return the fifolevel Value */
    return fifolevel;      
}
 
/**
 * @brief I2C ·¢ËÍÊý¾Ý
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  u8DataBuff: ·¢Ë͵ÄÊý¾Ý
 * @param  isstop: ÊÇ·ñ·¢ËÍֹͣλ
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C_No_Stop £º²»·¢ËÍSTOP
 *            @arg I2C_Stop £º·¢ËÍSTOP
 * @retval None
 */
void I2C_WriteData(I2C_Type *I2Cx, uint8_t u8DataBuff,I2C_IsStopDef isstop)
{
    if(isstop == I2C_Stop)
    {
        /* write to the DAT */
        I2Cx->DBC = (uint32_t)u8DataBuff | I2C_DBC_STOP_MASK;
    }
    else
    {
        /* write to the DAT */
        I2Cx->DBC = (uint32_t)u8DataBuff;
    }
}
 
/**
 * @brief I2C ½ÓÊÕÊý¾ÝÃüÁî
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  isstop: ÊÇ·ñ·¢ËÍֹͣλ
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C_No_Stop £º²»·¢ËÍSTOP
 *            @arg I2C_Stop £º·¢ËÍSTOP
 * @retval ½ÓÊÕµÄÊý¾Ý
 */
void I2C_ReadDataCmd(I2C_Type *I2Cx ,I2C_IsStopDef isstop)
{
    if(isstop == I2C_Stop)
    {
        I2Cx->DBC = (I2C_DBC_CMD_MASK|I2C_DBC_STOP_MASK);
    }
    else
    {
        I2Cx->DBC = I2C_DBC_CMD_MASK;
    }
}
 
/**
 * @brief I2C ½ÓÊÕÊý¾Ýº¯Êý
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @retval ½ÓÊÕµÄÊý¾Ý
 */
uint8_t I2C_ReadData(const I2C_Type *I2Cx)
{
    uint8_t dtemp;
    
    dtemp = (uint8_t)I2Cx->DBC;
    
  return dtemp ;
}
 
 
/**
 * @brief I2C ÖØÐ·¢ËÍ¿ªÊ¼²¢ÇÒÊǶÁÃüÁî
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @retval None
 */
void I2C_ResStartRead(I2C_Type *I2Cx)
{
    I2Cx->DBC = I2C_DBC_RESTART_MASK|I2C_DBC_CMD_MASK;
}
 
/**
 * @brief I2C ÖØÐ¿ªÊ¼·¢ËÍÊý¾Ý
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @retval None
 */
void I2C_ResStartWrite(I2C_Type *I2Cx,uint8_t u8DataBuff,I2C_IsStopDef isstop)
{
    if(isstop == I2C_Stop)
    {
        I2Cx->DBC = (uint32_t)u8DataBuff |I2C_DBC_RESTART_MASK| I2C_DBC_STOP_MASK;
    }
    else
    {
        I2Cx->DBC = (uint32_t)u8DataBuff | I2C_DBC_RESTART_MASK ;
    }
}
 
/**
 * @brief I2C SDAÑÓʱ
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  I2Cclkfreq: Ê±ÖÓÆµÂÊ
 * @param  DelayReq: ÑÓʱÖÜÆÚ
 * @retval None
 */
void I2C_SetSDADelay(I2C_Type *I2Cx, const uint8_t I2Cclkfreq, const uint8_t DelayReq)
{
     uint8_t temp;
 
     /* calculate the time delay,I2Cclkfreq in Mhz,DelayReq in ns*/
     temp = (uint8_t)(((uint16_t)I2Cclkfreq * (uint16_t)DelayReq /1000u) + 1u);
 
     /*  set the amount of time delay */
     I2Cx->SDAS = (uint32_t)temp;
}
 
/**
 * @brief I2C SDA±£³Öʱ¼äÉèÖÃ
 * @param  I2Cx: I2CÍâÉè
 *        Õâ¸ö²ÎÊý¿ÉÒÔÈ¡ÏÂÃæµÄÖµ:
 *            @arg I2C0 £ºI2C0ÍâÉè
 *            @arg I2C1 £ºI2C1ÍâÉè
 * @param  holdtime: ±£³ÖµÄʱ¼ä
 * @retval None
 */
void I2C_SetSDAHoldTime(I2C_Type *I2Cx, uint16_t holdtime)
{
     I2Cx->SDAHOLD = (uint32_t)holdtime;
}
 
/**
  * @}
  */
 
/**
  * @}
  */
 
/**
  * @}
  */
 
#ifdef __cplusplus
}
#endif /* __cplusplus */
 
 
/*******************************************************************************
 * EOF
 ******************************************************************************/