I²C Library
The I²C full master I²C module is available with a number of the ARM MCU models. The mikroC PRO for ARM provides a library which supports the master I²C mode.

- I²C library routines require you to specify the module you want to use. To select the desired I²C module, simply change the letter n in the routine prototype for a number from 0 to 9.
- Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
- Library routine prototypes may vary from family to family, so please pay attention to the desired routine section.
Library Routines
- I2Cn_Init
- I2Cn_Init_Advanced
- I2Cn_Enable
- I2Cn_Disable
- I2Cn_Start
- I2Cn_Get_Status
- I2Cn_Is_Idle
- I2Cn_Master_Slave_Addr_Set
- I2Cn_Master_Bus_Busy
- I2Cn_Master_Busy
- I2Cn_Read
- I2Cn_Write
- I2Cn_SetTimeoutCallback
- I2C_Set_Active
Generic Routines
I2Cn_Init
Prototype |
void I2Cn_Init(); |
||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This function configures and initializes the desired I²C module with the standard 100kbps bitrate. |
||||||||||||||||||||||
Parameters |
None. |
||||||||||||||||||||||
Returns |
Nothing. |
||||||||||||||||||||||
Requires |
MCU with the I²C module. |
||||||||||||||||||||||
Example |
// Initialize the I2C1 module with the standard bitrate of 100000 I2C1_Init(); |
||||||||||||||||||||||
Notes |
|
I2Cn_Init_Advanced
Prototype |
// for Stellaris MCUs :
void I2Cn_Init_Advanced(unsigned long bitrate); // for Stellaris MCUs with alternative PORT functions on GPIO pins :void I2Cn_Init_Advanced(unsigned long bitrate, const Module_Struct *module); // for CEC1x02, ST and Kinetis MCUs :void I2Cn_Init_Advanced(unsigned long : I2C_ClockSpeed, const Module_Struct *module); // for MSP432 :void I2Cn_Init_Advanced(unsigned long I2C_ClockSpeed, unsigned long clockSource, const Module_Struct *module); |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
This function configures and initializes the desired I²C module using custom bitrate setting. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
Parameters |
Stellaris
MSP432
STM32
CEC1x02
Kinetis
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
Returns |
Nothing. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
Requires |
MCU with the I²C module. |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
Example |
Stellaris// Initialize the I2C1 module in fast mode I2C1_Init_Advanced(_I2C_BITRATE_FAST_MODE); // Initialize the I2C1 module in fast mode on GPIO_PORTJ I2C1_Init_Advanced(_I2C_BITRATE_FAST_MODE, &_GPIO_MODULE_I2C1_J01); MSP432// Initialize the I2C1 module with 100Kbps speed on PA6/PA7 I2C1_Init_Advanced(100000, _I2C_CLOCK_SOURCE_SMCLK, &_GPIO_MODULE_I2CB0_A67); STM32// Initialize the I2C1 module with 100Kbps speed on PORTB I2C1_Init_Advanced(100000, &_GPIO_MODULE_I2C1_PB67); CEC1x02// Initialize the I2C0 module with 100Kbps speed I2C0_Init_Advanced(_I2C_100KHZ, &_GPIO_MODULE_I2C0_P015_P016); Kinetis// Initialize the I2C0 module with 100Kbps speed I2C0_Init_Advanced(100000, &_GPIO_MODULE_I2C0_PE24_25); |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
Notes |
Stellaris
STM32The peripheral input clock frequency must be at least :
|
I2Cn_Enable
Prototype |
void I2Cn_Enable(); |
---|---|
Description |
This routine enables I²C Master block. This routine is valid only for Stellaris devices. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
// Enable I2C1 Master block I2C1_Enable(); |
Notes |
|
I2Cn_Disable
Prototype |
void I2Cn_Disable(); |
---|---|
Description |
This routine disables I²C Master block. This routine is valid only for Stellaris devices. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
// Disable I2C1 Master block I2C1_Disable(); |
Notes |
|
I2Cn_Start
Prototype |
unsigned long I2Cn_Start(); |
---|---|
Description |
Determines if I²C bus is free and issues START signal. |
Parameters |
None. |
Returns |
If there is no error, routine returns 0. |
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
// Start I2C1 module I2C1_Start(); |
Notes |
|
I2Cn_Get_Status
Prototype |
unsigned long I2Cn_Get_Status(); |
---|---|
Description |
This routine returns the values of both I²C status registers, SR1 and SR2, combined in one 32-bit value. |
Parameters |
None. |
Returns |
Values of both I²C status registers. |
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
// Get Status for I2C1 module unsigned long stat; stat = I2C1_Get_Status(); |
Notes |
|
I2Cn_Is_Idle
Prototype |
unsigned I2Cn_Is_Idle(); |
---|---|
Description |
Tests if I²C bus is free. |
Parameters |
None. |
Returns |
Returns 1 if I²C bus is free, otherwise returns 0. |
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
if (I2C1_Is_Idle() { ... |
Notes |
|
I2Cn_Master_Slave_Addr_Set
Prototype |
void I2Cn_Master_Slave_Addr_Set(char slave_addr, char dir); |
||||||
---|---|---|---|---|---|---|---|
Description |
Sets the address that the I²C Master will place on the bus, i.e. Slave address. |
||||||
Parameters |
|
||||||
Returns |
Nothing. |
||||||
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
||||||
Example |
// Set address which will be used by Master to transmit data to I2C1_Master_Slave_Addr_Set(0x0A, _I2C_DIR_MASTER_TRANSMIT); |
||||||
Notes |
|
I2Cn_Master_Bus_Busy
Prototype |
char I2Cn_Master_Bus_Busy(); |
---|---|
Description |
Indicates whether or not the I²C bus is busy. |
Parameters |
None. |
Returns |
|
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
// If I2C bus is free : if (!I2C1_Master_Bus_Busy) { ... } |
Notes |
|
I2Cn_Master_Busy
Prototype |
char I2Cn_Master_Busy(); |
---|---|
Description |
Indicates whether or not the I²C Master is busy. |
Parameters |
None. |
Returns |
|
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
// If I2C Master is free : if (!I2C1_Master_Busy) { ... } |
Notes |
|
I2Cn_Read
Prototype |
// for Stellaris MCUs :
char I2Cn_Read(char *dat, char mode); // for CEC1x02, ST, Kinetis and MSP432 MCUs :void I2Cn_Read(char slave_address, char *ptrdata, unsigned long count, unsigned long END_mode); |
||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
Receives a byte from the I²C bus. |
||||||||||||||||
Parameters |
Stellaris
CEC1x02, STM32 and Kinetis
|
||||||||||||||||
Returns |
|
||||||||||||||||
Requires |
|
||||||||||||||||
Example |
StellarisI2C0_Master_Slave_Addr_Set(0x50, _I2C_DIR_MASTER_RECEIVE); // set slave address, and read operation I2C0_Read(res, _I2C_MASTER_MODE_SINGLE_RECEIVE); // issue I2C start, and read byte. CEC1x02, STM32, MSP432 and KinetisI2C1_Read(0x50, data_, num_bytes, END_MODE_STOP); // receive num_bytes, store it to data_, and issue a stop condition . |
||||||||||||||||
Notes |
|
I2Cn_Write
Prototype |
// for Stellaris MCUs :
unsigned I2Cn_Write(char dat, char mode); // for CEC1x02, ST, MSP432 and Kinetis MCUs :unsigned I2Cn_Write(unsigned char slave_address, unsigned char *buf, unsigned long count, unsigned long END_mode); |
||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
Sends data byte via the I²C bus. |
||||||||||||||||
Parameters |
Stellaris
CEC1x02, STM32, MSP432 and Kinetis
|
||||||||||||||||
Returns |
|
||||||||||||||||
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
||||||||||||||||
Example |
StellarisI2C0_Master_Slave_Addr_Set(0x50, _I2C_DIR_MASTER_TRANSMIT); // set slave address, and write operation I2C0_Write(rAddr, _I2C_MASTER_MODE_SINGLE_SEND); // issue I2C start, and send byte (address of EEPROM location) CEC1x02, STM32, MSP432 and KinetisI2C1_Write(0x50, data_, num_bytes, END_MODE_STOP); // write num_bytes, stored in data_, and issue a stop condition. |
||||||||||||||||
Notes |
|
I2Cn_SetTimeoutCallback
Prototype |
void I2Cn_SetTimeoutCallback(unsigned long timeout, void (*I2C_timeout)(char)); |
---|---|
Description |
This function checks for a I²C timeout condition in a user defined time period, after which it jumps to a callback function if this condition is met.
|
Parameters |
|
Returns |
Nothing. |
Requires |
I²C must be configured before using this function. See I2Cn_Init or I2Cn_Init_Advanced. |
Example |
// define callback function void I2C1_TimeoutCallback(char errorCode) { if (errorCode == _I2C_TIMEOUT_RD) { // do something if timeout is caused during read } if (errorCode == _I2C_TIMEOUT_WR) { // do something if timeout is caused during write } if (errorCode == _I2C_TIMEOUT_WAIT_FOR_IDLE) { // do something if timeout is caused during wait for idle } if (errorCode == _I2C_TIMEOUT_IDLE) { // do something if timeout is caused during idle } if (errorCode == _I2C_TIMEOUT_STOP) { // do something if timeout is caused during stop } if (errorCode == _I2C_TIMEOUT_START) { // do something if timeout is caused during start } if (errorCode == _I2C_TIMEOUT_INIT) { // do something if timeout is caused during initialization } } // initialize I2C module I2C1_Init(100000); // set timeout period and callback function I2C1_SetTimeoutCallback(1000, I2C1_TimeoutCallback); |
Notes |
|
I2C_Set_Active
Prototype |
// for Stellaris MCUs :
void I2C_Set_Active(void (*enable_ptr)(void), void (*master_slave_addr_set_ptr)(char, char), char (*write_ptr)(char, char), char (*read_ptr)(char*, char), void (*disable_ptr)(void)); // for MSP432 MCUs :void I2C_Set_Active(void (*read_ptr)(unsigned char, unsigned char*, unsigned long, unsigned long), unsigned (*write_ptr)(unsigned char, unsigned char*, unsigned long, unsigned long)); // for CEC1x02, ST and Kinetis MCUs :void I2C_Set_Active(unsigned (*start_ptr)(void), void (*read_ptr)(unsigned char, unsigned char*, unsigned long, unsigned long), unsigned (*write_ptr)(unsigned char, unsigned char*, unsigned long, unsigned long)); |
---|---|
Description |
Sets the active I²C module which will be used by the I²C routines. |
Parameters |
Stellaris
MSP432
STM32, CEC1x02 and Kinetis
|
Returns |
Nothing. |
Requires |
Used I²C module must be initialized before using this function. See the I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
StellarisI2C_Set_Active(&I2C1_Enable, &I2C1_Master_Slave_Addr_Set, &I2C1_Write, &I2C1_Read, &I2C1_Disable); // Sets the I2C1 module active MSP432I2C_Set_Active(&I2C1_Read, &I2C1_Write); // Sets the I2C1 module active STM32, CEC1x02 and KinetisI2C_Set_Active(&I2C1_Start, &I2C1_Read, &I2C1_Write); // Sets the I2C1 module active |
Notes |
Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library. |
I2C_Enable
Prototype |
void I2C_Enable(); |
---|---|
Description |
This routine enables I²C Master block. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
I2C_Set_Active(&I2C1_Enable, &I2C1_Master_Slave_Addr_Set, &I2C1_Read, &I2C1_Write, &I2C1_Disable); // Sets the I2C1 module active ... // Enable I2C1 Master block I2C_Enable(); |
Notes |
|
I2C_Disable
Prototype |
void I2C_Disable(); |
---|---|
Description |
This routine disables I²C Master block. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
Parameters |
None. |
Returns |
Nothing. |
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
I2C_Set_Active(&I2C1_Enable, &I2C1_Master_Slave_Addr_Set, &I2C1_Read, &I2C1_Write, &I2C1_Disable); // Sets the I2C1 module active ... // Disable I2C1 Master block I2C_Disable(); |
Notes |
|
I2C_Start
Prototype |
unsigned long I2C_Start(); |
---|---|
Description |
Determines if I²C bus is free and issues START signal. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
Parameters |
None. |
Returns |
If there is no error, routine returns 0. |
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
Example |
I2C_Set_Active(&I2C1_Start, &I2C1_Read, &I2C1_Write); // Sets the I2C1 module active ... // Start I2C1 module error = I2C_Start(); |
Notes |
|
I2C_Master_Slave_Addr_Set
Prototype |
void I2Cn_Master_Slave_Addr_Set(char slave_addr, char dir); |
||||||
---|---|---|---|---|---|---|---|
Description |
Sets the address that the I²C Master will place on the bus, i.e. Slave address. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
||||||
Parameters |
|
||||||
Returns |
Nothing. |
||||||
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
||||||
Example |
I2C_Set_Active(&I2C1_Enable, &I2C1_Master_Slave_Addr_Set, &I2C1_Read, &I2C1_Write, &I2C1_Disable); // Sets the I2C1 module active ... // Set address which will be used by Master to transmit data to I2C_Master_Slave_Addr_Set(0x0A, _I2C_DIR_MASTER_TRANSMIT); |
||||||
Notes |
|
I2C_Read
Prototype |
// for Stellaris MCUs :
char I2C_Read(char *dat, char mode); // for CEC1x02, ST, MSP432 and Kinetis MCUs :void I2C_Read(char slave_address, char *ptrdata, unsigned long count, unsigned long END_mode); |
||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
Receives a byte from the I²C bus. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
||||||||||||||||
Parameters |
Stellaris
CEC1x02, STM32, MSP432 and Kinetis
|
||||||||||||||||
Returns |
|
||||||||||||||||
Requires |
|
||||||||||||||||
Example |
StellarisI2C_Set_Active(&I2C1_Enable, &I2C1_Master_Slave_Addr_Set, &I2C1_Read, &I2C1_Write, &I2C1_Disable); // Sets the I2C1 module active ... I2C_Read(res, _I2C_MASTER_MODE_SINGLE_RECEIVE); // issue I2C start, and read byte. CEC1x02, STM32, MSP432 and KinetisI2C_Set_Active(&I2C1_Start, &I2C1_Read, &I2C1_Write); // Sets the I2C1 module active ... I2C_Read(0x50, data_, num_bytes, END_MODE_STOP); // receive num_bytes, store it to data_, and issue a stop condition . |
||||||||||||||||
Notes |
|
I2C_Write
Prototype |
// for Stellaris MCUs :
unsigned I2C_Write(char dat, char mode); // for CEC1x02, ST, MSP432 and Kinetis MCUs :unsigned I2C_Write(unsigned char slave_address, unsigned char *buf, unsigned long count, unsigned long END_mode); |
||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Description |
Sends data byte via the I²C bus. This is a generic routine which uses the active I²C module previously activated by the I2C_Set_Active routine. |
||||||||||||||||
Parameters |
Stellaris
CEC1x02, STM32, MSP432 and Kinetis
|
||||||||||||||||
Returns |
|
||||||||||||||||
Requires |
MCU with at least one I²C module. Used I²C module must be initialized before using this function. See I2Cn_Init and I2Cn_Init_Advanced routines. |
||||||||||||||||
Example |
StellarisI2C_Set_Active(&I2C1_Start, &I2C1_Read, &I2C1_Write); // Sets the I2C1 module active ... I2C_Write(rAddr, _I2C_MASTER_MODE_SINGLE_SEND); // issue I2C start, and send byte (address of EEPROM location) CEC1x02, STM32, MSP432 and KinetisI2C_Set_Active(&I2C1_Start, &I2C1_Read, &I2C1_Write); // Sets the I2C1 module active ... I2C_Write(0x50, data_, num_bytes, END_MODE_STOP); // write num_bytes, stored in data_, and issue a stop condition. |
||||||||||||||||
Notes |
|
What do you think about this topic ? Send us feedback!