Functions | |
uint | i2c_init (i2c_inst_t *i2c, uint baudrate) |
Initialise the I2C HW blockPut the I2C hardware into a known state, and enable it. Must be called before other functions. By default, the I2C is configured to operate as a master. More... | |
void | i2c_deinit (i2c_inst_t *i2c) |
Disable the I2C HW block. More... | |
uint | i2c_set_baudrate (i2c_inst_t *i2c, uint baudrate) |
Set I2C baudrateSet I2C bus frequency as close as possible to requested, and return actual rate set. Baudrate may not be as exactly requested due to clocking limitations. More... | |
void | i2c_set_slave_mode (i2c_inst_t *i2c, bool slave, uint8_t addr) |
Set I2C port to slave mode. More... | |
static uint | i2c_hw_index (i2c_inst_t *i2c) |
Convert I2C instance to hardware instance number. More... | |
int | i2c_write_blocking_until (i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop, absolute_time_t until) |
Attempt to write specified number of bytes to address, blocking until the specified absolute time is reached. More... | |
int | i2c_read_blocking_until (i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop, absolute_time_t until) |
Attempt to read specified number of bytes from address, blocking until the specified absolute time is reached. More... | |
static int | i2c_write_timeout_us (i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop, uint timeout_us) |
Attempt to write specified number of bytes to address, with timeout. More... | |
static int | i2c_read_timeout_us (i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop, uint timeout_us) |
Attempt to read specified number of bytes from address, with timeout. More... | |
int | i2c_write_blocking (i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len, bool nostop) |
Attempt to write specified number of bytes to address, blocking. More... | |
int | i2c_read_blocking (i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len, bool nostop) |
Attempt to read specified number of bytes from address, blocking. More... | |
static size_t | i2c_get_write_available (i2c_inst_t *i2c) |
Determine non-blocking write space available. More... | |
static size_t | i2c_get_read_available (i2c_inst_t *i2c) |
Determine number of bytes received. More... | |
static void | i2c_write_raw_blocking (i2c_inst_t *i2c, const uint8_t *src, size_t len) |
Write direct to TX FIFO. More... | |
static void | i2c_read_raw_blocking (i2c_inst_t *i2c, uint8_t *dst, size_t len) |
Read direct from RX FIFO. More... | |
static uint8_t | i2c_read_byte_raw (i2c_inst_t *i2c) |
Pop a byte from I2C Rx FIFO.This function is non-blocking and assumes the Rx FIFO isn't empty. More... | |
static void | i2c_write_byte_raw (i2c_inst_t *i2c, uint8_t value) |
Push a byte into I2C Tx FIFO.This function is non-blocking and assumes the Tx FIFO isn't full. More... | |
static uint | i2c_get_dreq (i2c_inst_t *i2c, bool is_tx) |
Return the DREQ to use for pacing transfers to/from a particular I2C instance. More... | |
i2c_inst_t | i2c0_inst |
I2C Controller API
The I2C bus is a two-wire serial interface, consisting of a serial data line SDA and a serial clock SCL. These wires carry information between the devices connected to the bus. Each device is recognized by a unique 7-bit address and can operate as either a “transmitter” or “receiver”, depending on the function of the device. Devices can also be considered as masters or slaves when performing data transfers. A master is a device that initiates a data transfer on the bus and generates the clock signals to permit that transfer. The first byte in the data transfer always contains the 7-bit address and a read/write bit in the LSB position. This API takes care of toggling the read/write bit. After this, any device addressed is considered a slave.
This API allows the controller to be set up as a master or a slave using the i2c_set_slave_mode function.
The external pins of each controller are connected to GPIO pins as defined in the GPIO muxing table in the datasheet. The muxing options give some IO flexibility, but each controller external pin should be connected to only one GPIO.
Note that the controller does NOT support High speed mode or Ultra-fast speed mode, the fastest operation being fast mode plus at up to 1000Kb/s.
See the datasheet for more information on the I2C controller and its usage.
void i2c_deinit | ( | i2c_inst_t * | i2c | ) |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
|
inlinestatic |
Convert I2C instance to hardware instance number.
i2c | I2C instance |
uint i2c_init | ( | i2c_inst_t * | i2c, |
uint | baudrate | ||
) |
Initialise the I2C HW blockPut the I2C hardware into a known state, and enable it. Must be called before other functions. By default, the I2C is configured to operate as a master.
The I2C bus frequency is set as close as possible to requested, and the actual rate set is returned
int i2c_read_blocking | ( | i2c_inst_t * | i2c, |
uint8_t | addr, | ||
uint8_t * | dst, | ||
size_t | len, | ||
bool | nostop | ||
) |
Attempt to read specified number of bytes from address, blocking.
i2c | Either i2c0 or i2c1 |
addr | 7-bit address of device to read from |
dst | Pointer to buffer to receive data |
len | Length of data in bytes to receive |
nostop | If true, master retains control of the bus at the end of the transfer (no Stop is issued), and the next transfer will begin with a Restart rather than a Start. |
int i2c_read_blocking_until | ( | i2c_inst_t * | i2c, |
uint8_t | addr, | ||
uint8_t * | dst, | ||
size_t | len, | ||
bool | nostop, | ||
absolute_time_t | until | ||
) |
Attempt to read specified number of bytes from address, blocking until the specified absolute time is reached.
i2c | Either i2c0 or i2c1 |
addr | 7-bit address of device to read from |
dst | Pointer to buffer to receive data |
len | Length of data in bytes to receive |
nostop | If true, master retains control of the bus at the end of the transfer (no Stop is issued), and the next transfer will begin with a Restart rather than a Start. |
until | The absolute time that the block will wait until the entire transaction is complete. |
|
inlinestatic |
Pop a byte from I2C Rx FIFO.This function is non-blocking and assumes the Rx FIFO isn't empty.
i2c | I2C instance. |
|
inlinestatic |
|
inlinestatic |
Attempt to read specified number of bytes from address, with timeout.
i2c | Either i2c0 or i2c1 |
addr | 7-bit address of device to read from |
dst | Pointer to buffer to receive data |
len | Length of data in bytes to receive |
nostop | If true, master retains control of the bus at the end of the transfer (no Stop is issued), and the next transfer will begin with a Restart rather than a Start. |
timeout_us | The time that the function will wait for the entire transaction to complete |
uint i2c_set_baudrate | ( | i2c_inst_t * | i2c, |
uint | baudrate | ||
) |
void i2c_set_slave_mode | ( | i2c_inst_t * | i2c, |
bool | slave, | ||
uint8_t | addr | ||
) |
int i2c_write_blocking | ( | i2c_inst_t * | i2c, |
uint8_t | addr, | ||
const uint8_t * | src, | ||
size_t | len, | ||
bool | nostop | ||
) |
Attempt to write specified number of bytes to address, blocking.
i2c | Either i2c0 or i2c1 |
addr | 7-bit address of device to write to |
src | Pointer to data to send |
len | Length of data in bytes to send |
nostop | If true, master retains control of the bus at the end of the transfer (no Stop is issued), and the next transfer will begin with a Restart rather than a Start. |
int i2c_write_blocking_until | ( | i2c_inst_t * | i2c, |
uint8_t | addr, | ||
const uint8_t * | src, | ||
size_t | len, | ||
bool | nostop, | ||
absolute_time_t | until | ||
) |
Attempt to write specified number of bytes to address, blocking until the specified absolute time is reached.
i2c | Either i2c0 or i2c1 |
addr | 7-bit address of device to write to |
src | Pointer to data to send |
len | Length of data in bytes to send |
nostop | If true, master retains control of the bus at the end of the transfer (no Stop is issued), and the next transfer will begin with a Restart rather than a Start. |
until | The absolute time that the block will wait until the entire transaction is complete. Note, an individual timeout of this value divided by the length of data is applied for each byte transfer, so if the first or subsequent bytes fails to transfer within that sub timeout, the function will return with an error. |
|
inlinestatic |
Push a byte into I2C Tx FIFO.This function is non-blocking and assumes the Tx FIFO isn't full.
i2c | I2C instance. |
value | Byte value. |
|
inlinestatic |
|
inlinestatic |
Attempt to write specified number of bytes to address, with timeout.
i2c | Either i2c0 or i2c1 |
addr | 7-bit address of device to write to |
src | Pointer to data to send |
len | Length of data in bytes to send |
nostop | If true, master retains control of the bus at the end of the transfer (no Stop is issued), and the next transfer will begin with a Restart rather than a Start. |
timeout_us | The time that the function will wait for the entire transaction to complete. Note, an individual timeout of this value divided by the length of data is applied for each byte transfer, so if the first or subsequent bytes fails to transfer within that sub timeout, the function will return with an error. |
i2c_inst_t i2c0_inst |
The I2C identifiers for use in I2C functions.
e.g. i2c_init(i2c0, 48000)