@web-font-path: "roboto-debian.css";
I2C Controller API. More...
Macros | |
#define | I2C_NUM(i2c) |
Returns the I2C number for a I2C instance. | |
#define | I2C_INSTANCE(num) |
Returns the I2C instance with the given I2C number. | |
#define | I2C_DREQ_NUM(i2c, is_tx) |
Returns the dreq_num_t used for pacing DMA transfers to or from this I2C instance. If is_tx is true, then it is for transfers to the I2C instance else for transfers from the I2C instance. | |
Functions | |
uint | i2c_init (i2c_inst_t *i2c, uint baudrate) |
Initialise the I2C HW block. | |
void | i2c_deinit (i2c_inst_t *i2c) |
Disable the I2C HW block. | |
uint | i2c_set_baudrate (i2c_inst_t *i2c, uint baudrate) |
Set I2C baudrate. | |
void | i2c_set_slave_mode (i2c_inst_t *i2c, bool slave, uint8_t addr) |
Set I2C port to slave mode. | |
static uint | i2c_get_index (i2c_inst_t *i2c) |
Convert I2C instance to hardware instance number. | |
static i2c_hw_t * | i2c_get_hw (i2c_inst_t *i2c) |
Return pointer to structure containing i2c hardware registers. | |
static i2c_inst_t * | i2c_get_instance (uint num) |
Convert I2C hardware instance number to I2C instance. | |
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. | |
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. | |
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. | |
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. | |
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. | |
int | i2c_write_burst_blocking (i2c_inst_t *i2c, uint8_t addr, const uint8_t *src, size_t len) |
Attempt to write specified number of bytes to address, blocking in burst mode. | |
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. | |
int | i2c_read_burst_blocking (i2c_inst_t *i2c, uint8_t addr, uint8_t *dst, size_t len) |
Attempt to read specified number of bytes from address, blocking in burst mode. | |
static size_t | i2c_get_write_available (i2c_inst_t *i2c) |
Determine non-blocking write space available. | |
static size_t | i2c_get_read_available (i2c_inst_t *i2c) |
Determine number of bytes received. | |
static void | i2c_write_raw_blocking (i2c_inst_t *i2c, const uint8_t *src, size_t len) |
Write direct to TX FIFO. | |
static void | i2c_read_raw_blocking (i2c_inst_t *i2c, uint8_t *dst, size_t len) |
Read direct from RX FIFO. | |
static uint8_t | i2c_read_byte_raw (i2c_inst_t *i2c) |
Pop a byte from I2C Rx FIFO. | |
static void | i2c_write_byte_raw (i2c_inst_t *i2c, uint8_t value) |
Push a byte into I2C Tx FIFO. | |
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. | |
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.
#define I2C_DREQ_NUM | ( | i2c, | |
is_tx | |||
) |
Returns the dreq_num_t used for pacing DMA transfers to or from this I2C instance. If is_tx is true, then it is for transfers to the I2C instance else for transfers from the I2C instance.
Note this macro is intended to resolve at compile time, and does no parameter checking
#define I2C_INSTANCE | ( | num | ) |
Returns the I2C instance with the given I2C number.
Note this macro is intended to resolve at compile time, and does no parameter checking
#define I2C_NUM | ( | i2c | ) |
Returns the I2C number for a I2C instance.
Note this macro is intended to resolve at compile time, and does no parameter checking
void i2c_deinit | ( | i2c_inst_t * | i2c | ) |
|
inlinestatic |
|
inlinestatic |
Return pointer to structure containing i2c hardware registers.
i2c | I2C instance |
|
inlinestatic |
Convert I2C instance to hardware instance number.
i2c | I2C instance |
|
inlinestatic |
Convert I2C hardware instance number to I2C instance.
num | Number of I2C, 0 or 1 |
|
inlinestatic |
|
inlinestatic |
uint i2c_init | ( | i2c_inst_t * | i2c, |
uint | baudrate | ||
) |
Initialise the I2C HW block.
Put 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. |
int i2c_read_burst_blocking | ( | i2c_inst_t * | i2c, |
uint8_t | addr, | ||
uint8_t * | dst, | ||
size_t | len | ||
) |
Attempt to read specified number of bytes from address, blocking in burst mode.
This version of the function will not issue a stop and will not restart on the next read. This allows you to read consecutive bytes of data without having to resend a stop bit and (for example) without having to send address byte(s) repeatedly
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 |
|
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. |
int i2c_write_burst_blocking | ( | i2c_inst_t * | i2c, |
uint8_t | addr, | ||
const uint8_t * | src, | ||
size_t | len | ||
) |
Attempt to write specified number of bytes to address, blocking in burst mode.
This version of the function will not issue a stop and will not restart on the next write. This allows you to write consecutive bytes of data without having to resend a stop bit and (for example) without having to send address byte(s) repeatedly
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 |
|
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. |
|
extern |
The I2C identifiers for use in I2C functions.
e.g. i2c_init(i2c0, 48000)