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.


  Important :

Library Routines

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
  • 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.
  • I2C modules are set on the following pins :

    Stellaris

    • I2C0 :
      • PB3/PB3 - all Cortex M3 and M4 MCUs.
    • I2C1 :
      • PG0/PG1 - TM4C129x Cortex M4 MCUs.
      • PA6/PA7 - all other Cortex M4 MCUs not listed before.
      • PA0/PA1 - all Cortex M3 MCUs.
    • I2C2 :
      • PL0/PL1 - TM4C129x Cortex M4 MCUs.
      • PE4/PB5 - all other Cortex M4 MCUs not listed before.
    • I2C3 :
      • PK4/PK5 - TM4C129x Cortex M4 MCUs.
      • PD0/PD1 - all other Cortex M4 MCUs not listed before.
    • I2C4 :
      • PK6/PK7 - TM4C129x Cortex M4 MCUs.
      • PG2/PG3 - all other Cortex M4 MCUs not listed before.
    • I2C5 :
      • PB0/PB1 - TM4C129x Cortex M4 MCUs.
      • PG6/PG7 - LM4F112x, LM4F122x, LM4F132x, LM4F212x and LM4F232x Cortex M4 MCUs.
      • PB6/PB7 - all other Cortex M4 MCUs not listed before.
    • I2C6 :
      • PA6/PA7 - TM4C129x Cortex M4 MCUs.
    • I2C7 :
      • PD0/PD1 - TM4C129x Cortex M4 MCUs.
    • I2C8 :
      • PA2/PA3 - TM4C129x Cortex M4 MCUs.
    • I2C9 :
      • PA0/PA1 - TM4C129x Cortex M4 MCUs.

      Refer to the table below for correct values of the bit rate in respect with system clock :

      System Clock Standard Mode
      4 MHz 100 Kbps
      6 MHz 100 Kbps
      12.5 MHz 89 Kbps
      16.7 MHz 93 Kbps
      20 MHz 100 Kbps
      25 MHz 96.2 Kbps
      33 MHz 97.1 Kbps
      40 MHz 100 Kbps
      50MHz 100 Kbps
      80 MHz 100 Kbps

    MSP432

    • I2C0 :
      • PA6/PA7
    • I2C1 :
      • PC12/PC13
    • I2C2 :
      • PB6/PB7
    • I2C3 :
      • PE10/PE11

    STM32

    • I2C1 :
      • PB6/PB7 all Cortex M0, M3 and M4 MCUs.
    • I2C2 :
      • PB10/PB11 - all Cortex M0 and M3 MCUs.
      • PA9/PA10 - STM32F302x, STM32F303x, STM32F313x, STM32F358x, STM32F373x, STM32F378x, and STM32F383x Cortex M4 MCUs.
      • PF1/PF0 - STM32F301x Cortex M4 MCUs.
      • PB10/PB3 - STM32F401x and STM32F411x Cortex M4 MCUs.
    • I2C3 :
      • PH7/PH8 - all Cortex M3 MCUs.
      • PA8/PC9 - STM32F302x Cortex M4 MCUs.
      • PD0/PD1 - STM32F401x and STM32F411x Cortex M4 MCUs.

    The peripheral input clock frequency must be at least :

    • 2 MHz in Standard mode (100 Kbps)
    • 4 MHz in Fast mode (400 Kbps)

    CEC1302

    • I2C0 :
      • P015/P016
    • I2C1 :
      • P022/P023
    • I2C2 :
      • P020/P021
    • I2C3 :
      • P024/P025

    CEC1702

    • I2C0 :
      • P004/P003
    • I2C1 :
      • P155/P154
    • I2C2 :
      • P010/P007
    • I2C3 :
      • P013/P012

    Kinetis

    • I2C0 :
      • PE24/PE25
    • I2C1 :
      • PE1/PE0
    • I2C2 :
      • PA12/PA11

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

  • bitrate: requested serial clock rate :
    Value Description
    _I2C_BITRATE_STANDARD_MODE 100 kbps bit rate.
    _I2C_BITRATE_FAST_MODE 400 kbps bit rate.
    _I2C_BITRATE_FAST_MODE_PLUS Fast-mode plus, 1 Mbps (available only for Stellaris Cortex M4 devices).
    _I2C_BITRATE_HIGH_SPEED_MODE High-speed mode, 3.33 Mbps (available only for Stellaris Cortex M4 devices).
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_I2C and pressing Ctrl + Space.

MSP432

  • I2C_ClockSpeed: requested serial clock rate.
  • clockSource: clock source for I²C module. Valid values :
    Description Predefined library const
    Externally provided clock _I2C_CLOCK_SOURCE_UCLK
    Auxiliary clock _I2C_CLOCK_SOURCE_ACLK
    Low-speed subsystem master clock _I2C_CLOCK_SOURCE_SMCLK
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_I2C and pressing Ctrl + Space.

STM32

  • I2C_ClockSpeed: requested serial clock rate.
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_I2C and pressing Ctrl + Space.

CEC1x02

  • I2C_ClockSpeed: requested serial clock rate. Valid values

    Value Description
    _I2C_100KHZ Standard mode, 100 kbps bit rate.
    _I2C_400KHZ Full speed, 400 kbps bit rate.
    _I2C_1MHZ Fast mode, 1 Mbps bit rate.
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_I2C and pressing Ctrl + Space.

Kinetis

  • I2C_ClockSpeed: requested serial clock rate.
  • module: appropriate module pinout. Use Code Assistant to list available module pinouts by typing _GPIO_MODULE_I2C and pressing Ctrl + Space.
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
  • 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.
  • For available working modes for a specific MCU please read the appropriate datasheet.

Stellaris

  • Refer to the table below for correct values of the bit rate in respect with system clock :
    System Clock Standard Mode Fast Mode Fast Mode Plus High-Speed Mode
    4 MHz 100 Kbps - - -
    6 MHz 100 Kbps - - -
    12.5 MHz 89 Kbps 312 Kbps - -
    16.7 MHz 93 Kbps 278 Kbps - -
    20 MHz 100 Kbps 333 Kbps - -
    25 MHz 96.2 Kbps 312 Kbps - -
    33 MHz 97.1 Kbps 330 Kbps - -
    40 MHz 100 Kbps 400 Kbps 1000 Kbps 3.33 Mbps
    50MHz 100 Kbps 357 Kbps 833 Kbps 2.77 Mbps
    80 MHz 100 Kbps 400 Kbps 1000 Kbps 3.33 Mbps

STM32


The peripheral input clock frequency must be at least :

  • 2 MHz in Standard mode (100 Kbps)
  • 4 MHz in Fast mode (400 Kbps)

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
  • 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.
  • This routine is valid only for Stellaris devices.

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
  • 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.
  • This routine is valid only for Stellaris devices.

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
  • 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.
  • This routine is valid for CEC1x02, ST and Kinetis devices.

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
  • 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.
  • This routine is valid for ST, Kinetis and MSP432 devices.

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
  • 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.
  • This routine is valid only for ST, Kinetis and MSP432 devices.

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
  • slave_addr: I²C slave address.
  • dir: I²C Master transceiver direction :
    Value Description
    _I2C_DIR_MASTER_RECEIVE Master receive.
    _I2C_DIR_MASTER_TRANSMIT Master transmit.
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
  • 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.
  • This routine is valid only for Stellaris devices.
  • Device address is shifted to the left by one place in the library and the R/W bit is added automatically.

I2Cn_Master_Bus_Busy

Prototype

char I2Cn_Master_Bus_Busy();

Description

Indicates whether or not the I²C bus is busy.

Parameters

None.

Returns
  • 1 - if the I²C bus is busy,
  • 0 - otherwise.
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
  • 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.
  • This routine is valid only for Stellaris devices.

I2Cn_Master_Busy

Prototype

char I2Cn_Master_Busy();

Description

Indicates whether or not the I²C Master is busy.

Parameters

None.

Returns
  • 1 - if the I²C Master is busy,
  • 0 - otherwise.
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
  • 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.
  • This routine is valid only for Stellaris devices.

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

  • dat: received data.
  • mode: mode parameter. Available modes :
    Value Description
    _I2C_MASTER_MODE_SINGLE_RECEIVE Single byte receive.
    _I2C_MASTER_MODE_BURST_RECEIVE_START Start receiving burst of data (used when receiving first byte of data burst).
    _I2C_MASTER_MODE_BURST_RECEIVE_CONT Continue receiving burst of data (used when receiving data burst bytes other than first and last).
    _I2C_MASTER_MODE_BURST_RECEIVE_FINISH End of data burst reception (used when receiving last byte of data burst).

CEC1x02, STM32 and Kinetis

  • slave_address: slave address.
  • ptrdata: pointer to the received data.
  • count: number of bytes to be received.
  • END_mode: mode in which the I²C module will be set after the reading. Available modes :
    Value Description
    END_MODE_RESTART I²C bus will issue a restart.
    END_MODE_STOP I²C bus will issue a stop.
Returns
  • 0 if there were no errors.
  • 1 if write collision was detected on the I²C bus.
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

Stellaris

I2C0_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 Kinetis

I2C1_Read(0x50, data_, num_bytes, END_MODE_STOP);          // receive num_bytes, store it to data_, and issue a stop condition .
Notes
  • 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.

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

  • dat: data to be sent.
  • mode: mode parameter. Available modes :
    Value Description
    _I2C_MASTER_MODE_SINGLE_SEND Single byte transmit.
    _I2C_MASTER_MODE_BURST_SEND_START Start transmitting burst of data (used when transmitting first byte of data burst).
    _I2C_MASTER_MODE_BURST_SEND_CONT Continue transmitting burst of data (used when transmitting data burst bytes other than first and last).
    _I2C_MASTER_MODE_BURST_SEND_FINISH End of data burst transmission (used when transmitting last byte of data burst).

CEC1x02, STM32, MSP432 and Kinetis

  • slave_address: slave address.
  • buf: pointer to the data to be transmitted.
  • count: number of bytes to be transmitted.
  • END_mode: mode in which the I²C module will be set after the writing. Available modes :
    Value Description
    END_MODE_RESTART I²C bus will issue a restart.
    END_MODE_STOP I²C bus will issue a stop.
Returns
  • 0 if there were no errors.
  • 1 if write collision was detected on the I²C bus.
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

Stellaris

I2C0_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 Kinetis

I2C1_Write(0x50, data_, num_bytes, END_MODE_STOP);          // write num_bytes, stored in data_, and issue a stop condition.
Notes
  • 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.
  • CEC1x02 and ST Specific: Device address is shifted to the left by one place in the library and the R/W bit is added automatically.

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.
Different I²C operations can cause a timeout (read, write, etc.) so user may test which one caused it, as in the example below.

Parameters
  • timeout: Represents a number of while loops in which the I²C timeout condition is checked.
  • I2C_timeout: pointer to the callback function. Callback function must be written according to this function prototype :
    void I2C1_TimeoutCallback(char errorCode) {
    
    }	
    
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
  • 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 3.
  • Number of I²C modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.

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

Stellaris

I2C_Set_Active(&I2C1_Enable, &I2C1_Master_Slave_Addr_Set, &I2C1_Write, &I2C1_Read, &I2C1_Disable); // Sets the I2C1 module active

MSP432

I2C_Set_Active(&I2C1_Read, &I2C1_Write); // Sets the I2C1 module active

STM32, CEC1x02 and Kinetis

I2C_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
  • This routine is valid only for Stellaris devices.

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
  • This routine is valid only for Stellaris devices.

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
  • This routine is valid for CEC1x02, ST and Kinetis devices.

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
  • slave_addr: I²C slave address.
  • dir: I²C Master transceiver direction :
    Value Description
    _I2C_DIR_MASTER_RECEIVE Master receive.
    _I2C_DIR_MASTER_TRANSMIT Master transmit.
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
  • This routine is valid only for Stellaris devices.
  • Device address is shifted to the left by one place in the library and the R/W bit is added automatically.

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

  • dat: received data.
  • mode: mode parameter. Available modes :
    Value Description
    _I2C_MASTER_MODE_SINGLE_RECEIVE Single byte receive.
    _I2C_MASTER_MODE_BURST_RECEIVE_START Start receiving burst of data (used when receiving first byte of data burst).
    _I2C_MASTER_MODE_BURST_RECEIVE_CONT Continue receiving burst of data (used when receiving data burst bytes other than first and last).
    _I2C_MASTER_MODE_BURST_RECEIVE_FINISH End of data burst reception (used when receiving last byte of data burst).

CEC1x02, STM32, MSP432 and Kinetis

  • slave_address: slave address.
  • ptrdata: pointer to the received data.
  • count: number of bytes to be received.
  • END_mode: mode in which the I²C module will be set after the reading. Available modes :
    Value Description
    END_MODE_RESTART I²C bus will issue a restart.
    END_MODE_STOP I²C bus will issue a stop.
Returns
  • 0 if there were no errors.
  • 1 if write collision was detected on the I²C bus.
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

Stellaris

I2C_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 Kinetis

I2C_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
  • CEC1x02 and ST Specific: Device address is shifted to the left by one place in the library and the R/W bit is added automatically.

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

  • dat: data to be sent.
  • mode: mode parameter. Available modes :
    Value Description
    _I2C_MASTER_MODE_SINGLE_SEND Single byte transmit.
    _I2C_MASTER_MODE_BURST_SEND_START Start transmitting burst of data (used when transmitting first byte of data burst).
    _I2C_MASTER_MODE_BURST_SEND_CONT Continue transmitting burst of data (used when transmitting data burst bytes other than first and last).
    _I2C_MASTER_MODE_BURST_SEND_FINISH End of data burst transmission (used when transmitting last byte of data burst).

CEC1x02, STM32, MSP432 and Kinetis

  • slave_address: slave address.
  • buf: pointer to the data to be transmitted.
  • count: number of bytes to be transmitted.
  • END_mode: mode in which the I²C module will be set after the writing. Available modes :
    Value Description
    END_MODE_RESTART I²C bus will issue a restart.
    END_MODE_STOP I²C bus will issue a stop.
Returns
  • 0 if there were no errors.
  • 1 if write collision was detected on the I²C bus.
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

Stellaris

I2C_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 Kinetis

I2C_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
  • CEC1x02 and ST Specific: Device address is shifted to the left by one place in the library and the R/W bit is added automatically.
Copyright (c) 2002-2019 mikroElektronika. All rights reserved.
What do you think about this topic ? Send us feedback!
Want more examples and libraries? 
Find them on LibStock - A place for the code