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
/**************************************************************************//**
 * @file     pan_i2c.h
 * @version  V1.00
 * $Revision: 2 $
 * $Date: 2023/11/08  $
 * @brief    Panchip series I2C driver header file
 *
 * @note
 * Copyright (C) 2023 Panchip Technology Corp. All rights reserved.
 *****************************************************************************/
#ifndef __PAN_I2C_H__
#define __PAN_I2C_H__
 
/**
 * @brief I2c Interface
 * @defgroup i2c_interface I2c Interface
 * @{
 */
 
#ifdef __cplusplus
extern "C"
{
#endif
 
 
/** 
 * @brief Structure with i2c init feature.
 *
 * @param I2C_ClockSpeed     Specifies the clock frequency.
 * @param I2C_Mode                Specifies the I2C mode.This parameter can be a value of
 *                                                      \ref PAN_I2C_MODE_MASTER,\ref PAN_I2C_MODE_SLAVE
 * @param I2C_DutyCycle        Specifies the I2C fast mode duty cycle.This parameter
 *                                                     can be a value of \ref I2C_DutyCycle_16_9, \ref I2C_DutyCycle_2
 * @param I2C_OwnAddress1    Specifies the first device own address,
 *                                                     This parameter can be a 7-bit or 10-bit address.
 * @param I2C_AcknowledgedAddress        Specifies if 7-bit or 10-bit address is acknowledged.
 *                                                     This parameter can be a value of 
 *                                                        \ref I2C_AcknowledgedAddress_7bit, \ref I2C_AcknowledgedAddress_10bit
 */
typedef struct
{
  uint32_t I2C_ClockSpeed;          /*!< Specifies the clock frequency.
                                         This parameter must be set to a value lower than 400kHz */
 
  uint16_t I2C_Mode;                /*!< Specifies the I2C mode.
                                         This parameter can be a value of @ref I2C_mode */
 
  uint16_t I2C_DutyCycle;           /*!< Specifies the I2C fast mode duty cycle.
                                         This parameter can be a value of @ref I2C_duty_cycle_in_fast_mode */
 
  uint16_t I2C_OwnAddress1;         /*!< Specifies the first device own address.
                                         This parameter can be a 7-bit or 10-bit address. */
 
  uint16_t I2C_AcknowledgedAddress; /*!< Specifies if 7-bit or 10-bit address is acknowledged.
                                         This parameter can be a value of @ref I2C_acknowledged_address */
}I2C_InitTypeDef;
 
/* Exported constants --------------------------------------------------------*/
 
 
#define I2C_DYNAMIC_TAR_UPDATE 0
 
 
/** @defgroup I2C_mode
 * @brief    I2C operational modes
 * @{
 */
#define I2C_MODE_MASTER  ((uint16_t)0x0021) /*!< I2C Master Mode */
#define I2C_MODE_SLAVE   ((uint16_t)0x0000) /*!< I2C Slave Mode */
 
/**
 * @brief    Check if the I2C mode is valid.
 * @param    MODE: I2C mode to check.
 * @retval   true if the mode is valid, false otherwise.
 */
#define IS_I2C_MODE(MODE) (((MODE) == I2C_MODE_MASTER) || ((MODE) == I2C_MODE_SLAVE))
 
/** @} */
 
/** @defgroup I2C_speed_mode
 * @brief    I2C communication speed modes
 * @{
 */
#define I2C_SPEED_STANDARD_MODE (uint16_t)(0x0002)  /*!< Standard Mode (up to 100 Kbps) */
#define I2C_SPEED_FAST_MODE     (uint16_t)(0x0004)  /*!< Fast Mode (up to 400 Kbps) */
#define I2C_SPEED_HIGH_MODE     (uint16_t)(0x0006)  /*!< High-Speed Mode (up to 3.4 Mbps) */
/** @} */
 
 
/** @defgroup I2C_duty_cycle
 * @brief    I2C fast mode duty cycle options
 * @{
 */
#define I2C_DutyCycle_16_9                      ((uint16_t)0x4000) /*!< I2C fast mode Tlow/Thigh = 16/9 */
#define I2C_DutyCycle_2                         ((uint16_t)0xBFFF) /*!< I2C fast mode Tlow/Thigh = 2 */
#define I2C_DutyCycle_1_1                       ((uint16_t)0x0800)
#define I2C_DutyCycle_1_2                       ((uint16_t)0x0400)
 
 
 
/* @brief    I2C fast mode duty cycle validation */
#define IS_I2C_DUTY_CYCLE(CYCLE)  (((CYCLE) == I2C_DutyCycle_16_9) || \
                                   ((CYCLE) == I2C_DutyCycle_2))
/** @} */
 
/** @defgroup I2C_transfer_direction
 * @{
 */
#define  I2C_Direction_Transmitter              ((uint16_t)0x0000)
#define  I2C_Direction_Receiver                 ((uint16_t)0x0100)
#define IS_I2C_DIRECTION(DIRECTION)             (((DIRECTION) == I2C_Direction_Transmitter) || \
                                                 ((DIRECTION) == I2C_Direction_Receiver))
/** @} */
 
/** @defgroup I2C_acknowledged_address
 * @{
 */
#define I2C_AcknowledgedAddress_7bit            ((uint16_t)0x4000)
#define I2C_AcknowledgedAddress_10bit           ((uint16_t)0xC000)
#define IS_I2C_ACKNOWLEDGE_ADDRESS(ADDRESS)     (((ADDRESS) == I2C_AcknowledgedAddress_7bit) || \
                                                 ((ADDRESS) == I2C_AcknowledgedAddress_10bit))
/** @} */
 
/** @defgroup I2C_Cmd  
 * @{
 */
#define I2C_CMD_WR                              ((uint8_t)0x00)
#define I2C_CMD_RD                              ((uint8_t)0x01)
#define I2C_CMD_STOP                            ((uint8_t)0x02)
#define I2C_CMD_RESTART                         ((uint8_t)0x04)
/** @} */
 
/** @defgroup I2C_Receive_FIFO_Threshold_Value
 * @{
 */
#define I2C_RX_TL_0                             ((uint8_t)0x00)
#define I2C_RX_TL_1                             ((uint8_t)0x01)
#define I2C_RX_TL_2                             ((uint8_t)0x02)
#define I2C_RX_TL_3                             ((uint8_t)0x03)
#define I2C_RX_TL_4                             ((uint8_t)0x04)
#define I2C_RX_TL_5                             ((uint8_t)0x05)
#define I2C_RX_TL_6                             ((uint8_t)0x06)
#define I2C_RX_TL_7                             ((uint8_t)0x07)
#define I2C_RX_TL_8                             ((uint8_t)0x08)
/** @} */
 
/** @defgroup I2C_Transmit_FIFO_Threshold_Value
 * @{
 */
#define I2C_TX_TL_0                             ((uint8_t)0x00)
#define I2C_TX_TL_1                             ((uint8_t)0x01)
#define I2C_TX_TL_2                             ((uint8_t)0x02)
#define I2C_TX_TL_3                             ((uint8_t)0x03)
#define I2C_TX_TL_4                             ((uint8_t)0x04)
#define I2C_TX_TL_5                             ((uint8_t)0x05)
#define I2C_TX_TL_6                             ((uint8_t)0x06)
#define I2C_TX_TL_7                             ((uint8_t)0x07)
#define I2C_TX_TL_8                             ((uint8_t)0x08)
/** @} */
 
/** @defgroup I2C_interrupts_definition
* @{
*/
 
#define I2C_IT_RX_UNDER                         ((uint16_t)0x0001)
#define I2C_IT_RX_OVER                          ((uint16_t)0x0002)
#define I2C_IT_RX_FULL                          ((uint16_t)0x0004)
#define I2C_IT_TX_OVER                          ((uint16_t)0x0008)
#define I2C_IT_TX_EMPTY                         ((uint16_t)0x0010)
#define I2C_IT_RD_REQ                           ((uint16_t)0x0020)
#define I2C_IT_TX_ABORT                         ((uint16_t)0x0040)
#define I2C_IT_RX_DONE                          ((uint16_t)0x0080)
#define I2C_IT_ACTIVITY                         ((uint16_t)0x0100)
#define I2C_IT_STOP_DET                         ((uint16_t)0x0200)
#define I2C_IT_START_DET                        ((uint16_t)0x0400)
#define I2C_IT_GEN_CALL                         ((uint16_t)0x0800)
#define I2C_IT_MST_ON_HOLD                      ((uint16_t)0x2000)
#define I2C_IT_ALL                              ((uint16_t)0x2FFF)
 
#define IS_I2C_CLEAR_IT(IT)                     ((((IT) & (uint16_t)0x0FFF) == 0x00) && ((IT) != (uint16_t)0x00))
 
#define IS_I2C_GET_IT(IT)                       (((IT) == I2C_IT_RX_UNDER) || ((IT) == I2C_IT_RX_OVER) || \
                                                 ((IT) == I2C_IT_RX_FULL)  || ((IT) == I2C_IT_TX_OVER) || \
                                                 ((IT) == I2C_IT_TX_EMPTY) || ((IT) == I2C_IT_RD_REQ) || \
                                                 ((IT) == I2C_IT_TX_ABORT) || ((IT) == I2C_IT_RX_DONE) || \
                                                 ((IT) == I2C_IT_ACTIVITY) || ((IT) == I2C_IT_STOP_DET) || \
                                                 ((IT) == I2C_IT_START_DET)|| ((IT) == I2C_IT_GEN_CALL))
/** @} */
 
/** @defgroup I2C_flags_definition
 * @{
 */
#define I2C_FLAG_SLV_ACTIVITY                   ((uint32_t)0x00000040)
#define I2C_FLAG_MST_ACTIVITY                   ((uint32_t)0x00000020)
#define I2C_FLAG_RFF                              ((uint32_t)0x00000010)
#define I2C_FLAG_RFNE                              ((uint32_t)0x00000008)
#define I2C_FLAG_TFE                            ((uint32_t)0x00000004)
#define I2C_FLAG_TFNF                           ((uint32_t)0x00000002)
#define I2C_FLAG_ACTIVITY                         ((uint32_t)0x00000001)
#define IS_I2C_CLEAR_FLAG(FLAG)                 ((((FLAG) & (uint16_t)0x007F) == 0x00) && ((FLAG) != (uint16_t)0x00))
#define IS_I2C_GET_FLAG(FLAG)                   (((FLAG) == I2C_FLAG_ACTIVITY) || ((FLAG) == I2C_FLAG_TFNF) || \
                                                 ((FLAG) == I2C_FLAG_TFE) || ((FLAG) == I2C_FLAG_RFNE) || \
                                                 ((FLAG) == I2C_FLAG_RFF) || ((FLAG) == I2C_FLAG_MST_ACTIVITY) || \
                                                 ((FLAG) == I2C_FLAG_SLV_ACTIVITY))
/** @} */
 
/** @defgroup I2C_Mask_Definitions
 * @brief    I2C mask definitions for various flags and interrupt enables.
 * @{
 */
#define FLAG_MASK       ((uint32_t)0x007F0FFF)  /*!< I2C FLAG mask */
#define ITEN_MASK       ((uint16_t)0x2FFF)     /*!< I2C Interrupt Enable mask */
#define IT_FLAG_MASK    ((uint16_t)0x2FFF)     /*!< I2C FLAG mask */
/** @} */
 
/**
  * @brief  Set I2C mode .
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  Mode: role of the I2Cx peripheral.
  *          This parameter can be: I2C_MODE_MASTER or I2C_MODE_SLAVE.
  * @note   This function should be called before enabling
            the I2C Peripheral.
  * @retval None
  */
__STATIC_INLINE void I2C_SetMode(I2C_T* I2Cx, uint16_t Mode)
{
    (Mode == I2C_MODE_MASTER)?(I2Cx->CON |= (I2C_CON_IC_SLAVE_DISABLE | I2C_CON_MASTER_MODE)):(I2Cx->CON &= ~(I2C_CON_IC_SLAVE_DISABLE | I2C_CON_MASTER_MODE));
}
/**
  * @brief  Set I2C Tx fifo threshold value .
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  ThresholdValue:Transmit FIFO Threshold Level
  *          This parameter can be @I2C_Transmit_FIFO_Threshold_Value.
  * @note   Controls the level of entries (or below) that trigger the TX_EMPTY interrupt (bit 4 in
  *         IC_RAW_INTR_STAT register). The valid range is 0-8, with the additional
  *         restriction that it may not be set to value larger than the depth of the buffer. If an
  *         attempt is made to do that, the actual value set will be the maximum depth of the
  *         buffer.A value of 0 sets the threshold for 0 entries, and a value of 8 sets the threshold for
  *         8 entries.
  * @retval None
  */
__STATIC_INLINE void I2C_SetTxTirggerLevel(I2C_T* I2Cx, uint8_t ThresholdValue)
{
    I2Cx->TX_TL = (I2Cx->TX_TL & ~I2C_TX_TL) | (ThresholdValue & 0xF);
}
 
/**
  * @brief  Set I2C Rx fifo threshold value .
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  ThresholdValue:Receive FIFO Threshold Level
  *          This parameter can be @I2C_Receive_FIFO_Threshold_Value.
  * @note   Controls the level of entries (or below) that trigger the RX_FULL interrupt (bit 2 in
  *         IC_RAW_INTR_STAT register). The valid range is 0-8, with the additional
  *         restriction that it may not be set to value larger than the depth of the buffer. If an
  *         attempt is made to do that, the actual value set will be the maximum depth of the
  *         buffer.A value of 0 sets the threshold for 0 entries, and a value of 8 sets the threshold for
  *         8 entries.
  * @retval None
  */
__STATIC_INLINE void I2C_SetRxTirggerLevel(I2C_T* I2Cx, uint8_t ThresholdValue)
{
    I2Cx->RX_TL = (I2Cx->RX_TL & ~I2C_RX_TL) | (ThresholdValue & 0xF);
}
/**
  * @brief  Enables or disables the specified I2C peripheral.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  NewState: new state of the I2Cx peripheral.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
__STATIC_INLINE void I2C_Cmd(I2C_T* I2Cx, FunctionalState NewState)
{
    (NewState != DISABLE)?(I2Cx->IC_ENABLE |= I2C_ENABLE_ENABLE):(I2Cx->IC_ENABLE &= (uint32_t)~((uint32_t)I2C_ENABLE_ENABLE));
}
/**
  * @brief  Enables or disables the specified I2C general call feature.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  NewState: new state of the I2C General call.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
__STATIC_INLINE void I2C_GeneralCallCmd(I2C_T* I2Cx, FunctionalState NewState)
{
    (NewState != DISABLE)?(I2Cx->TAR = (I2Cx->TAR | I2C_TAR_SPECIAL) & (~I2C_TAR_GC_OR_START)):(I2Cx->TAR = I2Cx->TAR);
}
 
/**
  * @brief  Sends a data byte through the I2Cx peripheral.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  Data: Byte to be transmitted..
            Cmd : I2C_CMD_RD/I2C_CMD_WR, I2C_CMD_RESTART, or I2C_CMD_STOP
  * @retval None
  */
__STATIC_INLINE void I2C_SendDataCmd(I2C_T* I2Cx, uint8_t Data, uint8_t Cmd)
{
    I2Cx->DATACMD = ((Cmd << 8 ) | (Data & 0xFF));
}
/**
  * @brief  Sends a data byte through the I2Cx peripheral.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  Cmd: I2C_CMD_RD/I2C_CMD_WR,I2C_CMD_RESTART, or I2C_CMD_STOP
  * @retval None
  */
__STATIC_INLINE void I2C_SendCmd(I2C_T* I2Cx, uint8_t Cmd)
{
    I2Cx->DATACMD |= (Cmd << 8);
}
/**
  * @brief  Returns the most recent received data by the I2Cx peripheral.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @retval The value of the received data.
  */
__STATIC_INLINE uint8_t I2C_ReceiveData(I2C_T* I2Cx)
{
    return (uint8_t)I2Cx->DATACMD;
}
/**
  * @brief  Aborting I2C Transfers.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @retval None
  */
__STATIC_INLINE void I2C_AbortTransfer(I2C_T* I2Cx)
{
    I2Cx->IC_ENABLE |= I2C_ENABLE_ABORT;
}
/**
  * @brief  Enables or disables the specified I2C DMA requests.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  NewState: new state of the I2C DMA transfer.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
__STATIC_INLINE void I2C_DMACmd(I2C_T* I2Cx, FunctionalState NewState)
{
    (NewState != DISABLE)?(I2Cx->DMA_CR |= (I2C_DMA_CR_RDMAE | I2C_DMA_CR_TDMAE)):(I2Cx->DMA_CR &= (uint32_t)~((uint32_t)(I2C_DMA_CR_RDMAE | I2C_DMA_CR_TDMAE)));
}
/**
  * @brief  DMA Transmit Data Level Register.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  Level: This parameter controls the level at which a DMA request is made by the transmit logic
  * @retval None
  */
__STATIC_INLINE void I2C_DMATransferDataLevel(I2C_T* I2Cx, uint8_t Level)
{
    I2Cx->DMA_TDLR |= (Level & 0x07);
}
/**
  * @brief  DMA Receive Data Level Register.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  Level: This parameter controls the level at which a DMA request is made by the receive logic.
  * @retval None
  */
__STATIC_INLINE void I2C_DMAReceiveDataLevel(I2C_T* I2Cx, uint8_t Level)
{
    I2Cx->DMA_RDLR |= (Level & 0x07);
}
/**
  * @brief  Checks whether the specified I2C interrupt has occurred or not.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  I2C_IT: specifies the interrupt source to check.
  *          This parameter can be one of the following values:
  *          @arg I2C_IT_RX_UNDER
  *          @arg I2C_IT_RX_OVER
  *          @arg I2C_IT_RX_FULL
  *          @arg I2C_IT_TX_OVER
  *          @arg I2C_IT_TX_EMPTY
  *          @arg I2C_IT_RD_REQ
  *          @arg I2C_IT_TX_ABRT
  *          @arg I2C_IT_RX_DONE
  *          @arg I2C_IT_ACTIVITY
  *          @arg I2C_IT_STOP_DET
  *          @arg I2C_IT_START_DET
  *          @arg I2C_IT_GEN_CALL
  *          @arg I2C_IT_MST_ON_HOLD
  * @retval The new state of I2C_IT (SET or RESET).
  */
__STATIC_INLINE ITStatus I2C_GetITStatus(I2C_T* I2Cx, uint16_t I2C_IT)
{
    return ((I2Cx->INTR_STAT & (I2C_IT & IT_FLAG_MASK)) != (uint32_t)RESET)?(SET):(RESET);
}
/**
  * @brief  Checks whether the specified I2C interrupt has occurred or not.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  I2C_IT: specifies the interrupt source to check.
  *          This parameter can be one of the following values:
  *             @arg I2C_IT_RX_UNDER
  *             @arg I2C_IT_RX_OVER
  *             @arg I2C_IT_RX_FULL
  *             @arg I2C_IT_TX_OVER
  *             @arg I2C_IT_TX_EMPTY
  *             @arg I2C_IT_RD_REQ
  *             @arg I2C_IT_TX_ABRT
  *             @arg I2C_IT_RX_DONE
  *             @arg I2C_IT_ACTIVITY
  *             @arg I2C_IT_STOP_DET
  *             @arg I2C_IT_START_DET
  *             @arg I2C_IT_GEN_CALL
  *             @arg I2C_IT_MST_ON_HOLD
  * @retval The new state of I2C_IT (SET or RESET).
  */
__STATIC_INLINE ITStatus I2C_GetRawITStatus(I2C_T* I2Cx, uint16_t I2C_IT)
{
    return ((I2Cx->RAW_INTR_STAT & (I2C_IT & IT_FLAG_MASK)) != (uint32_t)RESET)?(SET):(RESET);
}
/**
  * @brief  Enables or disables the specified I2C interrupts.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  I2C_IT: specifies the I2C interrupts sources to be enabled or disabled.
  *          This parameter can be any combination of the following values:
  *             @arg I2C_IT_RX_UNDER
  *             @arg I2C_IT_RX_OVER
  *             @arg I2C_IT_RX_FULL
  *             @arg I2C_IT_TX_OVER
  *             @arg I2C_IT_TX_EMPTY
  *             @arg I2C_IT_RD_REQ
  *             @arg I2C_IT_TX_ABRT
  *             @arg I2C_IT_RX_DONE
  *             @arg I2C_IT_ACTIVITY
  *             @arg I2C_IT_STOP_DET
  *             @arg I2C_IT_START_DET
  *             @arg I2C_IT_GEN_CALL
  * @param  NewState: new state of the specified I2C interrupts.
  *          This parameter can be: ENABLE or DISABLE.
  * @retval None
  */
__STATIC_INLINE void I2C_ITConfig(I2C_T* I2Cx, uint16_t I2C_IT, FunctionalState NewState)
{
    (NewState != DISABLE)?(I2Cx->INTR_MASK |= I2C_IT):(I2Cx->INTR_MASK &= (uint16_t)~I2C_IT);
}
 
/**
  * @brief  Enables or disables all the specified I2C interrupts.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @retval None
  */
__STATIC_INLINE void I2C_DisableAllIT(I2C_T* I2Cx)
{
    I2Cx->INTR_MASK = 0x0;
}
/**
  * @brief  Clears all the I2Cx's interrupt pending bits.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @retval None
  */
__STATIC_INLINE void I2C_ClearAllITPendingBit(I2C_T* I2Cx)
{
    (void)(I2Cx->CLR_INTR);
}
 
/**
  * @brief  get the source of tx_abort.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @retval true or false
  */
__STATIC_INLINE bool I2C_AbortSrcCheck(I2C_T* I2Cx,uint32_t IC_MSK)
{
    return (I2Cx->TX_ABRT_SRC & IC_MSK)?(true):(false);
}
 
 
/**
  * @brief  set slave address.
  * @param  I2Cx: where x can be 1, 2 or 3 to select the I2C peripheral.
  * @param  addr: slave address.
  * @retval true or false
  */
__STATIC_INLINE void I2C_SetSlaveAddr(I2C_T* I2Cx,uint32_t addr)
{
    I2Cx->SAR = (I2Cx->SAR & ~I2C_TAR_TAR) | addr;
}
 
 
/**
  * @brief  Initializes the I2Cx peripheral according to the specified
  *         parameters in the I2C_InitStruct.
  *
  * @note   To use the I2C at 400 KHz (in fast mode), the PCLK1 frequency
  *         (I2C peripheral input clock) must be a multiple of 10 MHz.
  *
  * @param  I2Cx: where I2Cx is a i2c peripheral base address.
  * @param  I2C_InitStruct: pointer to a I2C_InitTypeDef structure that contains
  *         the configuration information for the specified I2C peripheral.
  * @note   This function should be called before enabling
            the I2C Peripheral.
  * @retval false    init fail
  * @retval true    init succeed
  *
  *    @code:
    * I2C_InitTypeDef  I2C_InitStructure;
  * 
    * I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
    * I2C_InitStructure.I2C_OwnAddress1 = I2C_SLAVE_OWN_ADDRESS7;
    * I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
    * I2C_InitStructure.I2C_ClockSpeed = I2C_SPEED_100K;
    * I2C_Cmd(I2Cx, DISABLE);
    * bool ret = I2C_Init(I2Cx, &I2C_InitStructure);
  *
  * @endcode
  */
bool I2C_Init(I2C_T* I2Cx, I2C_InitTypeDef* I2C_InitStruct);
/**
  * @brief  Configure the target address for any master transaction.
  * @param  I2Cx: where I2Cx is a i2c peripheral base address.
  * @param  Address: specifies the slave 7-bit address which will be transmitted
  * @return None.
  */
void I2C_Set7bitAddress(I2C_T* I2Cx, uint8_t Address);
/**
  * @brief  Configure the target address for any master transaction.
  * @param  I2Cx: where I2Cx is a i2c peripheral base address.
  * @param  Address: specifies the slave 10-bit address which will be transmitted
  * @return None.
  */
void I2C_Set10bitAddress(I2C_T* I2Cx, uint16_t Address);
 
/**
  * @brief  Checks whether the specified I2C flag is set or not.
  * @param  I2Cx: where I2Cx is a i2c peripheral base address.
  * @param  I2C_FLAG: specifies the flag to check.
  *          This parameter can be one of the following values:
  *          @arg I2C_FLAG_SLV_ACTIVITY
  *          @arg I2C_FLAG_MST_ACTIVITY
  *          @arg I2C_FLAG_RFF
  *          @arg I2C_FLAG_TFE
  *          @arg I2C_FLAG_TFNF
  *          @arg I2C_FLAG_ACTIVITY
  *          @arg I2C_FLAG_MST_ACTIVITY
  * @return The new state of I2C_FLAG (SET or RESET).
  */
FlagStatus I2C_GetFlagStatus(I2C_T* I2Cx, uint32_t I2C_FLAG);
/**
  * @brief  Clears the I2Cx's interrupt pending bits.
  * @param  I2Cx: where I2Cx is a i2c peripheral base address.
  * @param  I2C_IT: specifies the interrupt pending bit to clear.
  *             @arg I2C_IT_RX_UNDER
  *             @arg I2C_IT_RX_OVER
  *             @arg I2C_IT_RX_FULL
  *             @arg I2C_IT_TX_OVER
  *             @arg I2C_IT_TX_EMPTY
  *             @arg I2C_IT_RD_REQ
  *             @arg I2C_IT_TX_ABRT
  *             @arg I2C_IT_RX_DONE
  *             @arg I2C_IT_ACTIVITY
  *             @arg I2C_IT_STOP_DET
  *             @arg I2C_IT_START_DET
  *             @arg I2C_IT_GEN_CALL
  *             @arg I2C_IT_MST_ON_HOLD
  * @return None
  */
void I2C_ClearITPendingBit(I2C_T* I2Cx, uint16_t I2C_IT);
 
/**
  * @brief  read data from the I2Cx peripheral.
  * @param  I2Cx: where I2Cx is a i2c peripheral base address.
  * @param  WriteBuf: cache of stored readings.
  * @param  Size: number of data read from I2C peripheral.
  * @return none.
  */
void I2C_Write(I2C_T* I2Cx,uint8_t *WriteBuf,uint32_t Size);
 
/**
  * @brief  write data to the I2Cx peripheral.
  * @param  I2Cx: where I2Cx is a i2c peripheral base address.
  * @param  ReadBuf: cache of stored writings.
  * @param  Size: number of data read from I2C peripheral.
  * @return None.
  */
void I2C_Read(I2C_T* I2Cx,uint8_t *ReadBuf,uint32_t Size);
 
/**@} */
 
#ifdef __cplusplus
}
#endif
 
#endif //__PAN_I2C_H__
 
/*** (C) COPYRIGHT 2016 Panchip Technology Corp. ***/