| M480 BSP V3.05.001 The Board Support Package for M480 Series

i2c.h
Go to the documentation of this file.
1 /****************************************************************************/
9 #ifndef __I2C_H__
10 #define __I2C_H__
11
12 #ifdef __cplusplus
13 extern "C"
14 {
15 #endif
16
17
30 /*---------------------------------------------------------------------------------------------------------*/
31 /* I2C_CTL constant definitions. */
32 /*---------------------------------------------------------------------------------------------------------*/
33 #define I2C_CTL_STA_SI 0x28UL
34 #define I2C_CTL_STA_SI_AA 0x2CUL
35 #define I2C_CTL_STO_SI 0x18UL
36 #define I2C_CTL_STO_SI_AA 0x1CUL
37 #define I2C_CTL_SI 0x08UL
38 #define I2C_CTL_SI_AA 0x0CUL
39 #define I2C_CTL_STA 0x20UL
40 #define I2C_CTL_STO 0x10UL
41 #define I2C_CTL_AA 0x04UL
43 /*---------------------------------------------------------------------------------------------------------*/
44 /* I2C GCMode constant definitions. */
45 /*---------------------------------------------------------------------------------------------------------*/
46 #define I2C_GCMODE_ENABLE 1
47 #define I2C_GCMODE_DISABLE 0
49 /*---------------------------------------------------------------------------------------------------------*/
50 /* I2C SMBUS constant definitions. */
51 /*---------------------------------------------------------------------------------------------------------*/
52 #define I2C_SMBH_ENABLE 1
53 #define I2C_SMBD_ENABLE 0
54 #define I2C_PECTX_ENABLE 1
55 #define I2C_PECTX_DISABLE 0
57 /* end of group I2C_EXPORTED_CONSTANTS */
58
73 #define I2C_SET_CONTROL_REG(i2c, u8Ctrl) ((i2c)->CTL0 = ((i2c)->CTL0 & ~0x3c) | (u8Ctrl))
74
85 #define I2C_START(i2c) ((i2c)->CTL0 = ((i2c)->CTL0 & ~I2C_CTL0_SI_Msk) | I2C_CTL0_STA_Msk)
86
97 #define I2C_WAIT_READY(i2c) while(!((i2c)->CTL0 & I2C_CTL0_SI_Msk))
98
109 #define I2C_GET_DATA(i2c) ((i2c)->DAT)
110
122 #define I2C_SET_DATA(i2c, u8Data) ((i2c)->DAT = (u8Data))
123
134 #define I2C_GET_STATUS(i2c) ((i2c)->STATUS0)
135
147 #define I2C_GET_TIMEOUT_FLAG(i2c) ( ((i2c)->TOCTL & I2C_TOCTL_TOIF_Msk) == I2C_TOCTL_TOIF_Msk ? 1:0 )
148
160 #define I2C_GET_WAKEUP_FLAG(i2c) ( ((i2c)->WKSTS & I2C_WKSTS_WKIF_Msk) == I2C_WKSTS_WKIF_Msk ? 1:0 )
161
172 #define I2C_CLEAR_WAKEUP_FLAG(i2c) ((i2c)->WKSTS = I2C_WKSTS_WKIF_Msk)
173
185 #define I2C_SMBUS_GET_STATUS(i2c) ((i2c)->BUSSTS)
186
197 #define I2C_SMBUS_GET_PEC_VALUE(i2c) ((i2c)->PKTCRC)
198
210 #define I2C_SMBUS_SET_PACKET_BYTE_COUNT(i2c, u32PktSize) ((i2c)->PKTSIZE = (u32PktSize))
211
223 #define I2C_SMBUS_ENABLE_ALERT(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_ALERTEN_Msk)
224
236 #define I2C_SMBUS_DISABLE_ALERT(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_ALERTEN_Msk)
237
249 #define I2C_SMBUS_SET_SUSCON_OUT(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_SCTLOEN_Msk)
250
262 #define I2C_SMBUS_SET_SUSCON_IN(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_SCTLOEN_Msk)
263
274 #define I2C_SMBUS_SET_SUSCON_HIGH(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_SCTLOSTS_Msk)
275
276
287 #define I2C_SMBUS_SET_SUSCON_LOW(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_SCTLOSTS_Msk)
288
299 #define I2C_SMBUS_ACK_MANUAL(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_ACKMEN_Msk)
300
311 #define I2C_SMBUS_ACK_AUTO(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_ACKMEN_Msk)
312
323 #define I2C_SMBUS_9THBIT_INT_ENABLE(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_ACKM9SI_Msk)
324
335 #define I2C_SMBUS_9THBIT_INT_DISABLE(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_ACKM9SI_Msk)
336
347 #define I2C_SMBUS_RST_PEC_AT_START_ENABLE(i2c) ((i2c)->BUSCTL |= I2C_BUSCTL_PECCLR_Msk)
348
359 #define I2C_SMBUS_RST_PEC_AT_START_DISABLE(i2c) ((i2c)->BUSCTL &= ~I2C_BUSCTL_PECCLR_Msk)
360
368 #define I2C_ENABLE_RX_PDMA(i2c) ((i2c)->CTL1 |= I2C_CTL1_RXPDMAEN_Msk)
369
377 #define I2C_ENABLE_TX_PDMA(i2c) ((i2c)->CTL1 |= I2C_CTL1_TXPDMAEN_Msk)
378
386 #define I2C_DISABLE_RX_PDMA(i2c) ((i2c)->CTL1 &= ~I2C_CTL1_RXPDMAEN_Msk)
387
395 #define I2C_DISABLE_TX_PDMA(i2c) ((i2c)->CTL1 &= ~I2C_CTL1_TXPDMAEN_Msk)
396
404 #define I2C_ENABLE_PDMA_STRETCH(i2c) ((i2c)->CTL1 |= I2C_CTL1_PDMASTR_Msk)
405
413 #define I2C_DISABLE_PDMA_STRETCH(i2c) ((i2c)->CTL1 &= ~I2C_CTL1_PDMASTR_Msk)
414
422 #define I2C_DISABLE_RST_PDMA(i2c) ((i2c)->CTL1 |= I2C_CTL1_PDMARST_Msk)
423
424 /*---------------------------------------------------------------------------------------------------------*/
425 /* inline functions */
426 /*---------------------------------------------------------------------------------------------------------*/
427
428 /* Declare these inline functions here to avoid MISRA C 2004 rule 8.1 error */
429 __STATIC_INLINE void I2C_STOP(I2C_T *i2c);
430
440 __STATIC_INLINE void I2C_STOP(I2C_T *i2c)
441 {
442
443 (i2c)->CTL0 |= (I2C_CTL0_SI_Msk | I2C_CTL0_STO_Msk);
444 while(i2c->CTL0 & I2C_CTL0_STO_Msk)
445 {
446 }
447 }
448
449 void I2C_ClearTimeoutFlag(I2C_T *i2c);
450 void I2C_Close(I2C_T *i2c);
451 void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack);
452 void I2C_DisableInt(I2C_T *i2c);
453 void I2C_EnableInt(I2C_T *i2c);
454 uint32_t I2C_GetBusClockFreq(I2C_T *i2c);
455 uint32_t I2C_GetIntFlag(I2C_T *i2c);
456 uint32_t I2C_GetStatus(I2C_T *i2c);
457 uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock);
458 uint8_t I2C_GetData(I2C_T *i2c);
459 void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode);
460 void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask);
461 uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock);
462 void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout);
463 void I2C_DisableTimeout(I2C_T *i2c);
464 void I2C_EnableWakeup(I2C_T *i2c);
465 void I2C_DisableWakeup(I2C_T *i2c);
466 void I2C_SetData(I2C_T *i2c, uint8_t u8Data);
467 void I2C_SMBusClearInterruptFlag(I2C_T *i2c, uint8_t u8SMBusIntFlag);
468 uint8_t I2C_WriteByte(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data);
469 uint32_t I2C_WriteMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data[], uint32_t u32wLen);
470 uint8_t I2C_WriteByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data);
471 uint32_t I2C_WriteMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data[], uint32_t u32wLen);
472 uint8_t I2C_WriteByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data);
473 uint32_t I2C_WriteMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data[], uint32_t u32wLen);
474 uint8_t I2C_ReadByte(I2C_T *i2c, uint8_t u8SlaveAddr);
475 uint32_t I2C_ReadMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t rdata[], uint32_t u32rLen);
476 uint8_t I2C_ReadByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr);
477 uint32_t I2C_ReadMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t rdata[], uint32_t u32rLen);
478 uint8_t I2C_ReadByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr);
479 uint32_t I2C_ReadMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t rdata[], uint32_t u32rLen);
480 uint32_t I2C_SMBusGetStatus(I2C_T *i2c);
481 void I2C_SMBusSetPacketByteCount(I2C_T *i2c, uint32_t u32PktSize);
482 void I2C_SMBusOpen(I2C_T *i2c, uint8_t u8HostDevice);
483 void I2C_SMBusClose(I2C_T *i2c);
484 void I2C_SMBusPECTxEnable(I2C_T *i2c, uint8_t u8PECTxEn);
485 uint8_t I2C_SMBusGetPECValue(I2C_T *i2c);
486 void I2C_SMBusIdleTimeout(I2C_T *i2c, uint32_t us, uint32_t u32Hclk);
487 void I2C_SMBusTimeout(I2C_T *i2c, uint32_t ms, uint32_t u32Pclk);
488 void I2C_SMBusClockLoTimeout(I2C_T *i2c, uint32_t ms, uint32_t u32Pclk);
489 /* end of group I2C_EXPORTED_FUNCTIONS */
491 /* end of group I2C_Driver */
493 /* end of group Standard_Driver */
495
496 #ifdef __cplusplus
497 }
498 #endif
499
500 #endif
501
502 /*** (C) COPYRIGHT 2016 Nuvoton Technology Corp. ***/
I2C_DisableInt
void I2C_DisableInt(I2C_T *i2c)
Disable Interrupt of I2C Controller.
Definition: i2c.c:160
I2C_ClearTimeoutFlag
void I2C_ClearTimeoutFlag(I2C_T *i2c)
Clear Time-out Counter flag.
Definition: i2c.c:104
I2C_Close
void I2C_Close(I2C_T *i2c)
Disable specify I2C Controller.
Definition: i2c.c:71
I2C_WriteMultiBytesOneReg
uint32_t I2C_WriteMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data[], uint32_t u32wLen)
Specify a byte register address and write multi bytes to Slave.
Definition: i2c.c:863
I2C_EnableInt
void I2C_EnableInt(I2C_T *i2c)
Enable Interrupt of I2C Controller.
Definition: i2c.c:175
I2C_WriteMultiBytesTwoRegs
uint32_t I2C_WriteMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data[], uint32_t u32wLen)
Specify two bytes register address and write multi bytes to Slave.
Definition: i2c.c:993
I2C_WriteByte
uint8_t I2C_WriteByte(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data)
Write a byte to Slave.
Definition: i2c.c:689
I2C_SetBusClockFreq
uint32_t I2C_SetBusClockFreq(I2C_T *i2c, uint32_t u32BusClock)
Set I2C Bus Clock.
Definition: i2c.c:216
I2C_EnableWakeup
void I2C_EnableWakeup(I2C_T *i2c)
Enable I2C Wake-up Function.
Definition: i2c.c:421
I2C_T
Definition: i2c_reg.h:26
I2C_GetBusClockFreq
uint32_t I2C_GetBusClockFreq(I2C_T *i2c)
Get I2C Bus Clock.
Definition: i2c.c:189
I2C_GetIntFlag
uint32_t I2C_GetIntFlag(I2C_T *i2c)
Get Interrupt Flag.
Definition: i2c.c:245
I2C_EnableTimeout
void I2C_EnableTimeout(I2C_T *i2c, uint8_t u8LongTimeout)
Enable Time-out Counter Function and support Long Time-out.
Definition: i2c.c:382
I2C_DisableTimeout
void I2C_DisableTimeout(I2C_T *i2c)
Disable Time-out Counter Function.
Definition: i2c.c:406
I2C_SMBusClose
void I2C_SMBusClose(I2C_T *i2c)
Disable SMBus function.
Definition: i2c.c:525
I2C_SMBusGetStatus
uint32_t I2C_SMBusGetStatus(I2C_T *i2c)
To get SMBus Status.
Definition: i2c.c:451
I2C_ReadByteOneReg
uint8_t I2C_ReadByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr)
Specify a byte register address and read a byte from Slave.
Definition: i2c.c:1176
I2C_ReadMultiBytes
uint32_t I2C_ReadMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t rdata[], uint32_t u32rLen)
Read multi bytes from Slave.
Definition: i2c.c:1112
I2C_GetData
uint8_t I2C_GetData(I2C_T *i2c)
Read a Byte from I2C Bus.
Definition: i2c.c:284
I2C_SMBusOpen
void I2C_SMBusOpen(I2C_T *i2c, uint8_t u8HostDevice)
Init SMBus Host/Device Mode.
Definition: i2c.c:499
I2C_DisableWakeup
void I2C_DisableWakeup(I2C_T *i2c)
Disable I2C Wake-up Function.
Definition: i2c.c:436
I2C_SMBusTimeout
void I2C_SMBusTimeout(I2C_T *i2c, uint32_t ms, uint32_t u32Pclk)
Calculate Time-out of SMBus active period.
Definition: i2c.c:616
I2C_T::CTL0
__IO uint32_t CTL0
Definition: i2c_reg.h:1046
I2C_SMBusGetPECValue
uint8_t I2C_SMBusGetPECValue(I2C_T *i2c)
Get SMBus CRC value.
Definition: i2c.c:566
I2C_GetStatus
uint32_t I2C_GetStatus(I2C_T *i2c)
Get I2C Bus Status Code.
Definition: i2c.c:270
I2C_SMBusIdleTimeout
void I2C_SMBusIdleTimeout(I2C_T *i2c, uint32_t us, uint32_t u32Hclk)
Calculate Time-out of SMBus idle period.
Definition: i2c.c:584
I2C_ReadByteTwoRegs
uint8_t I2C_ReadByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr)
Specify two bytes register address and read a byte from Slave.
Definition: i2c.c:1326
I2C_CTL0_STO_Msk
#define I2C_CTL0_STO_Msk
Definition: i2c_reg.h:1089
I2C_WriteByteOneReg
uint8_t I2C_WriteByteOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t data)
Specify a byte register address and write a byte to Slave.
Definition: i2c.c:800
I2C_ReadMultiBytesTwoRegs
uint32_t I2C_ReadMultiBytesTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t rdata[], uint32_t u32rLen)
Specify two bytes register address and read multi bytes from Slave.
Definition: i2c.c:1406
I2C_SMBusClearInterruptFlag
void I2C_SMBusClearInterruptFlag(I2C_T *i2c, uint8_t u8SMBusIntFlag)
Clear SMBus Interrupt Flag.
Definition: i2c.c:467
I2C_SetSlaveAddrMask
void I2C_SetSlaveAddrMask(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddrMask)
Configure the mask bits of 7-bit Slave Address.
Definition: i2c.c:350
I2C_ReadByte
uint8_t I2C_ReadByte(I2C_T *i2c, uint8_t u8SlaveAddr)
Read a byte from Slave.
Definition: i2c.c:1055
I2C_CTL0_SI_Msk
#define I2C_CTL0_SI_Msk
Definition: i2c_reg.h:1086
I2C_WriteByteTwoRegs
uint8_t I2C_WriteByteTwoRegs(I2C_T *i2c, uint8_t u8SlaveAddr, uint16_t u16DataAddr, uint8_t data)
Specify two bytes register address and Write a byte to Slave.
Definition: i2c.c:925
I2C_SetData
void I2C_SetData(I2C_T *i2c, uint8_t u8Data)
Send a byte to I2C Bus.
Definition: i2c.c:299
I2C_SMBusSetPacketByteCount
void I2C_SMBusSetPacketByteCount(I2C_T *i2c, uint32_t u32PktSize)
Set SMBus Bytes Counts of Transmission or Reception.
Definition: i2c.c:483
I2C_ReadMultiBytesOneReg
uint32_t I2C_ReadMultiBytesOneReg(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t u8DataAddr, uint8_t rdata[], uint32_t u32rLen)
Specify a byte register address and read multi bytes from Slave.
Definition: i2c.c:1248
I2C_SMBusClockLoTimeout
void I2C_SMBusClockLoTimeout(I2C_T *i2c, uint32_t ms, uint32_t u32Pclk)
Calculate Cumulative Clock low Time-out of SMBus active period.
Definition: i2c.c:652
I2C_WriteMultiBytes
uint32_t I2C_WriteMultiBytes(I2C_T *i2c, uint8_t u8SlaveAddr, uint8_t data[], uint32_t u32wLen)
Write multi bytes to Slave.
Definition: i2c.c:741
I2C_SetSlaveAddr
void I2C_SetSlaveAddr(I2C_T *i2c, uint8_t u8SlaveNo, uint8_t u8SlaveAddr, uint8_t u8GCMode)
Set 7-bit Slave Address and GC Mode.
Definition: i2c.c:318
I2C_SMBusPECTxEnable
void I2C_SMBusPECTxEnable(I2C_T *i2c, uint8_t u8PECTxEn)
Enable SMBus PEC Transmit Function.
Definition: i2c.c:542
I2C_STOP
__STATIC_INLINE void I2C_STOP(I2C_T *i2c)
The macro is used to set STOP condition of I2C Bus.
Definition: i2c.h:440
I2C_Open
uint32_t I2C_Open(I2C_T *i2c, uint32_t u32BusClock)
Enable specify I2C Controller and set Clock Divider.
Definition: i2c.c:37
I2C_Trigger
void I2C_Trigger(I2C_T *i2c, uint8_t u8Start, uint8_t u8Stop, uint8_t u8Si, uint8_t u8Ack)
Set Control bit of I2C Controller.
Definition: i2c.c:123