GlobLib
HAL and API libraries for MCUs and hardware.

This module contains low level functions for I2C interaction. More...

Files

file  stm32f103cb_i2c.h
 Header file for stm32f103cb I2C.
 

Macros

#define I2C_TIMEOUT   500000
 The number of loop cycles to wait when polling peripheral registers.
 
#define I2C_MY_ADDRESS   0x32
 The address of this device.
 

Enumerations

Functions

mcu_error I2C_setup (i2c_periph peripheral)
 Initialize I2C data structure and setup port. More...
 
mcu_error I2C_start (i2c_periph peripheral)
 Generate a start condition on the bus. More...
 
mcu_error I2C_stop (i2c_periph peripheral)
 Generate a stop condition on the bus. More...
 
mcu_error I2C_address (i2c_periph peripheral, uint8_t address, uint8_t operation)
 Send a seven bit address on the bus. More...
 
mcu_error I2C_write (i2c_periph peripheral, uint8_t byte)
 Send one byte on the bus. More...
 
uint8_t I2C_read (i2c_periph peripheral)
 Read one byte from the bus without acknowledge generation. More...
 
uint8_t I2C_repeatRead (i2c_periph peripheral)
 Read one byte from the bus with acknowledge generation. More...
 

Detailed Description

This module contains low level functions for I2C interaction.

The I2C peripherals are mapped to the following pins for the ports

PULLUP RESISTORS (AROUND 4.7k) ARE NEEDED ON THESE LINES

I2C_setup() sets up a peripheral port with the following:

Author
Stuart Ianna
Version
0.1
Date
June 2018
Warning
None
Bug:
None
Todo:
Compilers
  • arm-none-eabi-gcc (15:4.9.3+svn231177-1) 4.9.3 20150529 (prerelease)
Minimum working example (blocking)
#define I2C_SLAVE_ADDRESS 0xA0
int main(void){
volatile int i;
uint8_t recData;
//Setup a port
while (1) {
//Typical read sequence
I2C_address(I2C_2,I2C_SLAVE_ADDRESS,I2C_WRITE);
I2C_address(I2C_2,I2C_SLAVE_ADDRESS,I2C_READ);
recData = I2C_repeatRead(I2C_2)<<8;
I2C_stop(I2C_2); //Need to send stop before last byte read
recData |= I2C_read(I2C_2);
//Typical write sequence
I2C_address(I2C_2,MPU_DEVICE_ADDRESS,I2C_WRITE);
I2C_write(I2C_2,address);
}
return 0;
}

Enumeration Type Documentation

enum i2c_periph

I2C ports available on the MCU.

Enumerator
I2C_1 

First I2C port.

I2C_2 

Second I2C port.

Definition at line 68 of file stm32f103cb_i2c.h.

Function Documentation

mcu_error I2C_address ( i2c_periph  peripheral,
uint8_t  address,
uint8_t  operation 
)

Send a seven bit address on the bus.

Parameters
peripheralI2C peripheral to modify.
addressThe slave address of the device
operationread or write operation
Returns
Error number as defined in mcu_error, E_I2C_NOERROR if no error, E_I2C_ADD if address wasn't sent.

Definition at line 503 of file stm32f103cb_i2c.c.

uint8_t I2C_read ( i2c_periph  peripheral)

Read one byte from the bus without acknowledge generation.

This operation will stil return mcu_error if an error has occured. The generated error will also be streamed on the debug output stream.

This is typically used at the end of a read chain, or when only a sigle byte is read.

Parameters
peripheralI2C peripheral to modify.
Returns
Error number as defined in mcu_error, E_I2C_NOERROR if no error, E_I2C_WRITE if byte wasn't transmitted.

Definition at line 227 of file stm32f103cb_i2c.c.

uint8_t I2C_repeatRead ( i2c_periph  peripheral)

Read one byte from the bus with acknowledge generation.

This operation will stil return mcu_error if an error has occured. The generated error will also be streamed on the debug output stream.

This is typically used when another read operation is to follow.

Parameters
peripheralI2C peripheral to modify.
Returns
Error number as defined in mcu_error, E_I2C_NOERROR if no error, E_I2C_WRITE if byte wasn't transmitted.

Definition at line 302 of file stm32f103cb_i2c.c.

mcu_error I2C_setup ( i2c_periph  peripheral)

Initialize I2C data structure and setup port.

This must be called before any other feature is used.

Parameters
peripheralI2C peripheral to associate with the port.
Returns
Error number as defined in mcu_error, E_I2C_NOERROR if no error, E_I2C_PORT if port doesn't exist

Definition at line 49 of file stm32f103cb_i2c.c.

mcu_error I2C_start ( i2c_periph  peripheral)

Generate a start condition on the bus.

Parameters
peripheralI2C peripheral to modify.
Returns
Error number as defined in mcu_error, E_I2C_NOERROR if no error, E_I2C_START if start wasn't generated.

Definition at line 381 of file stm32f103cb_i2c.c.

mcu_error I2C_stop ( i2c_periph  peripheral)

Generate a stop condition on the bus.

Parameters
peripheralI2C peripheral to modify.
Returns
Error number as defined in mcu_error, E_I2C_NOERROR if no error, E_I2C_STOP if stop wasn't generated.

Definition at line 459 of file stm32f103cb_i2c.c.

mcu_error I2C_write ( i2c_periph  peripheral,
uint8_t  byte 
)

Send one byte on the bus.

Parameters
peripheralI2C peripheral to modify.
byteThe byte to send.
Returns
Error number as defined in mcu_error, E_I2C_NOERROR if no error, E_I2C_WRITE if byte wasn't transmitted.

Definition at line 156 of file stm32f103cb_i2c.c.