Skip to content

Latest commit

 

History

History
255 lines (164 loc) · 6.25 KB

README.md

File metadata and controls

255 lines (164 loc) · 6.25 KB

I2C

I2C driver support for hardware abstraction layer

Minimalist code

uint16_t value, i2cPeriphAddr, i2cRegAddr;
rt_dev_t i2c_bus;

// init
i2c_bus = i2c_getFreeDevice();
i2c_setBaudSpeed(i2c_bus, I2C_BAUD_400K);
i2c_setAddressWidth(i2c_bus, 7);
i2c_enable(i2c_bus);

// send and receive data
i2cPeriphAddr = 0x2A;
i2cRegAddr = 0x12;
value = i2c_readreg(i2c_bus, i2cPeriphAddr, i2cRegAddr, I2C_REG8 | I2C_REGADDR8);

value = 10;
i2c_writereg(i2c_bus, i2cPeriphAddr, i2cRegAddr, value, I2C_REG8 | I2C_REGADDR8);

API

Device assignation

i2c_getFreeDevice

rt_dev_t i2c_getFreeDevice();

Gives a free i2c bus device number

i2c_open

int i2c_open(rt_dev_t device);

Open an i2c bus

i2c_close

int i2c_close(rt_dev_t device);

Close an i2c bus

Device enable/disable

i2c_enable

int i2c_enable(rt_dev_t device);

Enable the specified i2c bus device

i2c_disable

int i2c_disable(rt_dev_t device);

Disable the specified i2c bus device

Device settings

i2c_setBaudSpeed

int i2c_setBaudSpeed(rt_dev_t device, uint32_t baudSpeed);

Sets the speed of receive and transmit

i2c_baudSpeed

uint32_t i2c_baudSpeed(rt_dev_t device);

Gets the true baud speed

i2c_effectiveBaudSpeed

uint32_t i2c_effectiveBaudSpeed(rt_dev_t device);

Gets the effective baud speed

i2c_setAddressWidth

int i2c_setAddressWidth(rt_dev_t device, uint8_t addressWidth);

Sets the address width of slaves

i2c_addressWidth

uint8_t i2c_addressWidth(rt_dev_t device);

Gets the address width of slaves

Device low level interface

i2c_start

int i2c_start(rt_dev_t device);

Sends a start condition

i2c_restart

int i2c_restart(rt_dev_t device);

Sends a restart condition

i2c_stop

int i2c_stop(rt_dev_t device);

Sends a stop condition

i2c_idle

int i2c_idle(rt_dev_t device);

Sends a idle condition

i2c_ack

int i2c_ack(rt_dev_t device);

Sends a ack condition

i2c_nack

int i2c_nack(rt_dev_t device);

Sends a nack condition

i2c_putc

int i2c_putc(rt_dev_t device, const char data);

Puts a byte

i2c_getc

uint8_t i2c_getc(rt_dev_t device);

Gets a byte

High level functions, registers level

i2c_readreg

uint16_t i2c_readreg(rt_dev_t device, uint16_t address, uint16_t reg, uint8_t flags);

Read a register at address 'reg' in i2c chip with address 'address'

i2c_readregs

ssize_t i2c_readregs(rt_dev_t device, uint16_t address, uint16_t reg, uint8_t regs[], size_t size, uint8_t flags);

Read 'size' registers beginning at address 'reg' in i2c chip with address 'address'

i2c_writereg

int i2c_writereg(rt_dev_t device, uint16_t address, uint16_t reg, uint16_t value, uint8_t flags);

Write 'value' in register at address 'reg' in i2c chip with address 'address'

i2c_writeregs

int i2c_writeregs(rt_dev_t device, uint16_t address, uint16_t reg, uint8_t regs[], size_t size, uint8_t flags);

Write 'regs' in registers that start at address 'reg' in i2c chip

flags parameter should be use with the following flag :

  • I2C_REG8 (default) or I2C_REG16 to indicate if register of the distant device have 8 or 16 bits register size (could be change for each transaction)

  • I2C_REGADDR8 (default) or I2C_REGADDR16 to indicate the size of register address

  • I2C_READ_RESTART (default) or I2C_READ_STOPSTART to indicate if device need a restart or a stop followed by a start between read transaction (both exists)

This flag should be used with (|) pipes/logic or. It is not necessary to precise it if you use default value

Development status

Device assignation, configuration, send and read data fully functional

TODO

  • review error return in high level function and i2c_getc

Supported architectures

Header file : i2c.h

High level implementation file : i2c.c

Family Implementation file
pic24f i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
pic24fj i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
pic24ep i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
pic24hj i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
dspic33fj i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
dspic33ep i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
dspic33ev i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
dspic30f i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
dspic33ch i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
dspic33ck i2c_pic24_dspic30f_dspic33.c / i2c_pic24_dspic30f_dspic33.h
pic32mm i2c_pic32.c / i2c_pic32.h
pic32mk i2c_pic32.c / i2c_pic32.h
pic32mx i2c_pic32.c / i2c_pic32.h
pic32mzec i2c_pic32.c / i2c_pic32.h
pic32mzef i2c_pic32.c / i2c_pic32.h
pic32mzda i2c_pic32.c / i2c_pic32.h