I2C
I2C Controller driver. The driver supports one instance of a I2C controller with fixed pin configuration and configurable datarate.
A call to voltix_i2c_init() initializes the controller with a rate of 250kbps.
Two sets of transfer functions are provided.
The voltix_i2c_x_atomic(...) API encapsulates reads and writes in a critical section and works even before the Voltix runtime is initialized.
It is used to initialize the on-board peripherals during early startup.
The voltix_i2c_x(...) should be used in all other cases.
Important
Always check the return code of voltix_i2c_x(...) to check for a potential reset/teardown during the transfer.
Example usage
1
2#include "voltix.h"
3#include "voltix_gpio.h"
4#include "voltix_timing.h"
5#include "printf.h"
6#include "voltix_max20361.h"
7#include "voltix_i2c.h"
8
9/* I2C device address of MAX20361. */
10#define DEV_ADDR 0x15
11/* Address of ID register. */
12#define REG_ADDR 0x0
13
14void lateinit(void) {
15 voltix_i2c_init();
16}
17
18int main(void) {
19 int rc;
20 uint8_t rx_buf;
21 uint8_t tx_buf = REG_ADDR;
22
23 for (;;) {
24 /* Write address of register to be read to device. */
25 voltix_i2c_write(DEV_ADDR, &tx_buf, 1);
26 /* Read register value. */
27 rc = voltix_i2c_read(&rx_buf, 1, DEV_ADDR);
28 printf("RC: %d, Value: %02X\r\n", rc, rx_buf);
29
30 voltix_sleep_ms(1000);
31 }
32}
API Reference
-
enum voltix_i2c_freq_t
I2C SCL frequency.
Values:
-
enumerator VOLTIX_I2C_FREQ_100K
100kHz SCL frequency.
-
enumerator VOLTIX_I2C_FREQ_250K
250kHz SCL frequency.
-
enumerator VOLTIX_I2C_FREQ_400K
400kHz SCL frequency.
-
enumerator VOLTIX_I2C_FREQ_100K
Values:
-
enumerator VOLTIX_ERR_COMMI2C
I2C communication error.
-
enumerator VOLTIX_ERR_COMMI2C
-
void voltix_i2c_init(void)
Initializes I2C peripheral. Must be called once after reset before I2C is used.
-
voltix_rc_t voltix_i2c_write(uint8_t dev_addr, uint8_t *data, size_t n_data)
Transmits the given data to an I2C peripheral.
- Parameters:
dev_addr – I2C address of peripheral device.
data – Pointer to data.
n_data – Size of data buffer.
- Return values:
VOLTIX_SUCCESS – Write completed successfully.
VOLTIX_ERR_COMMI2C – I2C communication error.
-
voltix_rc_t voltix_i2c_read(uint8_t *buffer, size_t n_data, uint8_t dev_addr)
Reads the specified number of bytes from an I2C peripheral.
- Parameters:
buffer – Pointer to a destination buffer where data gets stored.
n_data – Number of bytes to read.
dev_addr – I2C address of peripheral device.
- Return values:
VOLTIX_SUCCESS – Read completed successfully.
VOLTIX_ERR_COMMI2C – I2C communication error.
-
voltix_rc_t voltix_i2c_write_atomic(uint8_t dev_addr, uint8_t *data, size_t n_data)
Transmits the given data to an I2C peripheral.
Transmits the given data to an I2C peripheral within a critical section. Should only be used for short transfers as it will drain the capacitor and sink the system without checkpointing if energy becomes low.
- Parameters:
dev_addr – I2C address of peripheral device.
data – Pointer to data.
n_data – Size of data buffer.
- Return values:
VOLTIX_SUCCESS – Write completed successfully.
VOLTIX_ERR_COMMI2C – I2C communication error.
-
voltix_rc_t voltix_i2c_read_atomic(uint8_t *buffer, size_t n_data, uint8_t dev_addr)
Reads the specified number of bytes from an I2C peripheral.
Reads the specified number of bytes from an I2C peripheral within a critical section. Should only be used for short transfers as it will drain the capacitor and sink the system without checkpointing if energy becomes low.
- Parameters:
buffer – Pointer to a destination buffer where data gets stored.
n_data – Number of bytes to read.
dev_addr – I2C address of peripheral device.
- Return values:
VOLTIX_SUCCESS – Read completed successfully.
VOLTIX_ERR_COMMI2C – I2C communication error.
-
void voltix_i2c_set_freq(voltix_i2c_freq_t freq)
Sets the I2C clock frequency.
- Parameters:
freq –