I2C

I2C Controller driver. The driver supports one instance of a I2C controller with fixed pin configuration and configurable datarate.

A call to riotee_i2c_init() initializes the controller with a rate of 250kbps.

Two sets of transfer functions are provided. The riotee_i2c_x_atomic(...) API encapsulates reads and writes in a critical section and works even before the Riotee runtime is initialized. It is used to initialize the on-board peripherals during early startup.

The riotee_i2c_x(...) should be used in all other cases.

Important

Always check the return code of riotee_i2c_x(...) to check for a potential reset/teardown during the transfer.

Example usage

 1
 2#include "riotee.h"
 3#include "riotee_gpio.h"
 4#include "riotee_timing.h"
 5#include "printf.h"
 6#include "riotee_max20361.h"
 7#include "riotee_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  riotee_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    riotee_i2c_write(DEV_ADDR, &tx_buf, 1);
26    /* Read register value. */
27    rc = riotee_i2c_read(&rx_buf, 1, DEV_ADDR);
28    printf("RC: %d, Value: %02X\r\n", rc, rx_buf);
29
30    riotee_sleep_ms(1000);
31  }
32}

API Reference

enum riotee_i2c_freq_t

I2C SCL frequency.

Values:

enumerator RIOTEE_I2C_FREQ_100K

100kHz SCL frequency.

enumerator RIOTEE_I2C_FREQ_250K

250kHz SCL frequency.

enumerator RIOTEE_I2C_FREQ_400K

400kHz SCL frequency.

enum [anonymous]

Values:

enumerator RIOTEE_ERR_COMMI2C

I2C communication error.

void riotee_i2c_init(void)

Initializes I2C peripheral. Must be called once after reset before I2C is used.

riotee_rc_t riotee_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:
  • RIOTEE_SUCCESS – Write completed successfully.

  • RIOTEE_ERR_COMMI2C – I2C communication error.

riotee_rc_t riotee_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:
  • RIOTEE_SUCCESS – Read completed successfully.

  • RIOTEE_ERR_COMMI2C – I2C communication error.

riotee_rc_t riotee_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:
  • RIOTEE_SUCCESS – Write completed successfully.

  • RIOTEE_ERR_COMMI2C – I2C communication error.

riotee_rc_t riotee_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:
  • RIOTEE_SUCCESS – Read completed successfully.

  • RIOTEE_ERR_COMMI2C – I2C communication error.

void riotee_i2c_set_freq(riotee_i2c_freq_t freq)

Sets the I2C clock frequency.

Parameters:

freq