Explanation of SMBus.c,h Defining SMBUS (I2C)-Related Procedures
SMBUS (I2C)
In many sample and application codes using TWENET, the I2C bus is typically not accessed directly through the low-level AHI library procedures.
Instead, a set of simplified read/write functions defined in SMBus.c
and SMBus.h
is used.
The following is a list of the function names provided:
void vSMBusInit(void);
void vSMBusInit_setClk(uint32 u32Clk);
void vSMBusDeInit();
void vSMBusDeInit(void);
bool_t bSMBusWrite(uint8 u8Address, uint8 u8Command,
uint8 u8Length, uint8* pu8Data);
bool_t bSMBusSequentialRead(uint8 u8Address, uint8 u8Length,
uint8* pu8Data);
SMBus.c SMBus.h
These are a set of functions summarizing typical I2C procedures, with source code stored in TWENETmwx/source/sensors/legacy
.
- If the TWENETmwx library is not linked (C project), copy and use this source code.
vSMBusInit()
void vSMBusInit(void);
Initializes the I2C bus.
The pins used are DIO14 (CLK) and DIO15 (DATA), with a clock frequency of 100 Kbps.
vSMBusInit_setClk(uint32)
{{{cpp {noln=true} void vSMBusInit_setClk(uint32 u32Clk); }}}
Using this function instead of vSMBusInit()
initializes the I2C bus with the clock specified by u32clk
.
- If
u32clk >= 7
andu32clk <= 255
⇒ Initializes with clock frequency3200000ul / (u32clk + 1)
. For example, specifying31
gives 100 kHz, and7
gives 400 kHz. - If
u32clk >= 12500
andu32clk <= 400000
⇒ Initializes withu32clk
directly. - Otherwise ⇒ Undefined behavior.
※ The actual frequency will be the closest available based on the hardware clock divisor. For detailed calculations, refer to the source code (for TWELITE GOLD, see NXP’s FSL library specifications).
// Implementation for TWELITE GOLD
PUBLIC void TWENET_smbus_vSMBusInit_setClk(uint32_t u32clk)
{
...
if (u32clk >= 7 && u32clk <= 255) { // given by divisor
u32clk = 3200000ul / (u32clk + 1);
}
if (u32clk < 12500ul || u32clk > 400000ul) {
u32clk = 100000ul;
}
// initialize the I2C
//... rest of the implementation
_conf.baudRate_bps = u32clk;
I2C_MasterGetDefaultConfig(&_conf); // FSL library function
}
// Implementation for TWELITE BLUE/RED
void vSMBusInit_setClk(uint32 u32Clk)
{
/* convert clock to clock divisor */
if (u32Clk > 255) {
u32Clk = (3200000UL / u32Clk) - 1;
}
if (u32Clk < 7 || u32Clk > 255) u32Clk = 31; // set 100kHz
/* run bus at specified value */
vAHI_SiMasterConfigure(TRUE, FALSE, u32Clk);
// 16/[(PreScaler + 1) x 5]MHz
// --> 31:100KHz, 7:400KHz, 47:66KHz
}
vSMBusDeInit()
void vSMBusDeInit();
Stops the use of the I2C bus.
- For TWELITE BLUE/RED, calls
vAHI_SiMasterDisable()
. - For TWELITE GOLD, stops the I2C bus usage and restores DIO14 and DIO15 pins to the TWENET library’s initial configuration (input ports with pull-up).
bSMBusWrite()
bool_t bSMBusWrite(
uint8 u8Address,
uint8 u8Command,
uint8 u8Length,
uint8* pu8Data)
Performs a write operation on the I2C bus.
u8Address
⇒ Specifies the device address on the I2C bus.u8Command
⇒ Specifies the command (first transfer byte) for the write operation.u8Length
⇒ Number of subsequent bytes. If0
, only the command byte is transferred. If greater than0
,pu8Data
must be defined.pu8Data
⇒ Pointer to the memory area containing the bytes to be transferred.
Returns TRUE
if the transfer succeeds, and FALSE
if it fails.
bSMBusSequentialRead()
bool_t bSMBusSequentialRead(
uint8 u8Address,
uint8 u8Length,
uint8* pu8Data)
Performs a read operation on the I2C bus.
u8Address
⇒ Specifies the device address on the I2C bus.u8Length
⇒ Specifies the number of bytes to read (must be 1 or more).pu8Data
⇒ Pointer to the memory area where the read bytes will be stored.
Returns TRUE
if the transfer succeeds, and FALSE
if it fails.
Others
Handling During Sleep
For TWELITE BLUE/RED, hardware reinitialization is required after waking from sleep.
IfvSMBusDeInit()
is not called, the pin state is undefined (or follows the JN516x peripheral specification).
You can reinitialize by callingvSMBusInit()
orvSMBusInit_setClk()
again.For TWELITE GOLD, the behavior is as follows:
- RAM ON (normal) sleep: If the I2C bus was initialized before sleep, it will be reinitialized upon waking. If the I2C bus was not in use, normal general-purpose IO wake-up handling will apply.
- RAM OFF (deep) sleep: Upon waking, TWENET will reinitialize the pins as input pins with pull-up enabled.
TWELITE GOLD Implementation
AHI-compatible functions are not provided; instead, corresponding functions are implemented in SMBus.c
and SMBus.h
.
// Implementations in the TWENETcmpt library
PUBLIC void TWENET_smbus_vSMBusInit(void);
PUBLIC void TWENET_smbus_vSMBusDeInit(void);
PUBLIC void TWENET_smbus_vSMBusInit_setClk(uint32 u32clk);
PUBLIC bool_t TWENET_smbus_bSMBusWrite(uint8 u8Address,
uint8 u8Command, uint8 u8Length, uint8* pu8Data);
PUBLIC bool_t TWENET_smbus_bSMBusSequentialRead(uint8 u8Address,
uint8 u8Length, uint8* pu8Data);
// SMBus.h uses inline definitions to call the above functions
static inline void vSMBusInit(void) {
TWENET_smbus_vSMBusInit();
}
static inline void vSMBusInit_setClk(uint32 u32Clk) {
TWENET_smbus_vSMBusInit_setClk(u32Clk);
}
static inline void vSMBusDeInit() {
TWENET_smbus_vSMBusDeInit();
}
static inline void vSMBusDeInit(void) {
TWENET_smbus_vSMBusInit();
}
static inline bool_t bSMBusWrite(uint8 u8Address, uint8 u8Command,
uint8 u8Length, uint8* pu8Data) {
return TWENET_smbus_bSMBusWrite(u8Address, u8Command, u8Length, pu8Data);
}
static inline bool_t bSMBusSequentialRead(uint8 u8Address, uint8 u8Length,
uint8* pu8Data) {
return TWENET_smbus_bSMBusSequentialRead(u8Address, u8Length, pu8Data);
}