PebbleOS v4.47.0-512-g5285d4236
Loading...
Searching...
No Matches

I2C controller class. More...

Modules

 nRF5 I2C
 I2C bus driver for the nRF5 TWIM peripheral.
 
 SF32LB I2C
 I2C bus driver for the SF32LB I2C controller.
 

Data Structures

struct  pbl_i2c_msg
 A message of a transfer. More...
 
struct  pbl_i2c_bus_ops
 Bus driver operations. More...
 
struct  pbl_i2c_bus
 An I2C controller. More...
 
struct  pbl_i2c_dev
 A peripheral on a bus. More...
 

Macros

#define PBL_I2C_DEV(_bus, _addr)   {.bus = (_bus), .addr = (_addr)}
 Initializer for a struct pbl_i2c_dev.
 
#define PBL_I2C_BUS_STATE_DEFINE(sym)
 Define the class state of bus sym, for the PBL_I2C_*_DEFINE() macro of a driver.
 
#define PBL_I2C_BUS_INIT(sym, _name, _parent, _ops)
 Initializer for the struct pbl_i2c_bus of bus sym.
 

Functions

int pbl_i2c_bus_dev_init (const struct pbl_device *dev)
 Device init of every bus: sets the class state up, then calls the driver's init.
 
void pbl_i2c_bus_complete (const struct pbl_i2c_bus *bus, int status)
 Report the outcome of the transfer in flight.
 
int pbl_i2c_transfer (const struct pbl_i2c_dev *dev, struct pbl_i2c_msg *msgs, uint8_t num_msgs)
 Transfer messages to or from a device.
 
static int pbl_i2c_write (const struct pbl_i2c_dev *dev, const void *buf, uint32_t len)
 Write data.
 
static int pbl_i2c_read (const struct pbl_i2c_dev *dev, void *buf, uint32_t len)
 Read data.
 
static int pbl_i2c_write_read (const struct pbl_i2c_dev *dev, const void *wbuf, uint32_t wlen, void *rbuf, uint32_t rlen)
 Write data, then read data after a repeated START.
 
static int pbl_i2c_burst_read (const struct pbl_i2c_dev *dev, uint8_t reg, void *buf, uint32_t len)
 Read consecutive registers.
 
static int pbl_i2c_burst_write (const struct pbl_i2c_dev *dev, uint8_t reg, const void *buf, uint32_t len)
 Write consecutive registers.
 
static int pbl_i2c_reg_read_byte (const struct pbl_i2c_dev *dev, uint8_t reg, uint8_t *val)
 Read a register.
 
static int pbl_i2c_reg_write_byte (const struct pbl_i2c_dev *dev, uint8_t reg, uint8_t val)
 Write a register.
 
int pbl_i2c_reg_update_byte (const struct pbl_i2c_dev *dev, uint8_t reg, uint8_t mask, uint8_t val)
 Update bits of a register.
 

Variables

const struct pbl_device_class pbl_i2c_bus_class
 Class of the I2C buses.
 

Message flags

#define PBL_I2C_MSG_WRITE   0U
 Write to the device.
 
#define PBL_I2C_MSG_READ   PBL_BIT(0)
 Read from the device.
 
#define PBL_I2C_MSG_NOSTART   PBL_BIT(1)
 Continue the previous write without a repeated START or address.
 
#define PBL_I2C_MSG_DMA_SAFE   PBL_BIT(2)
 The buffer may be accessed through DMA: aligned to PBL_DCACHE_LINE_SIZE and owning the whole cache lines it covers, PBL_DCACHE_ROUND_UP(len) bytes.
 

Detailed Description

I2C controller class.

An I2C controller is a struct pbl_i2c_bus device, and a peripheral on it a struct pbl_i2c_dev: bus plus 7-bit address. A transfer is a list of messages, each a read or a write, sent as one bus transaction: a START, a repeated START between messages and a STOP after the last one. A write flagged PBL_I2C_MSG_NOSTART continues the previous write instead, e.g. data after a register address. Transfers block the calling task, are serialized per bus and must not be issued from ISRs. The controller is powered for the duration of each transfer.

static const struct pbl_i2c_dev s_accel = PBL_I2C_DEV(&s_i2c2.bus, 0x6a);
uint8_t id;
uint8_t xyz[6];
int ret;
ret = pbl_i2c_reg_read_byte(&s_accel, REG_WHO_AM_I, &id);
if (ret != 0) {
return ret;
}
return pbl_i2c_burst_read(&s_accel, REG_OUT_X_L, xyz, sizeof(xyz));
A peripheral on a bus.
Definition i2c.h:123
static int pbl_i2c_burst_read(const struct pbl_i2c_dev *dev, uint8_t reg, void *buf, uint32_t len)
Read consecutive registers.
Definition i2c.h:247
static int pbl_i2c_reg_read_byte(const struct pbl_i2c_dev *dev, uint8_t reg, uint8_t *val)
Read a register.
Definition i2c.h:279
#define PBL_I2C_DEV(_bus, _addr)
Initializer for a struct pbl_i2c_dev.
Definition i2c.h:136

Calls return 0 or a negative errno: -ENXIO when the address is not acknowledged, -EIO on a data NACK or a bus error, -ETIMEDOUT when the transfer does not finish, -ENOTSUP when the controller cannot issue the message sequence and -EINVAL for an invalid one.


Data Structure Documentation

◆ pbl_i2c_msg

struct pbl_i2c_msg

A message of a transfer.

Data Fields
uint8_t * buf Data to write or buffer to read into.
uint32_t flags Message flags.
uint32_t len Number of bytes.

◆ pbl_i2c_bus

struct pbl_i2c_bus

An I2C controller.

Data Fields
struct pbl_device dev Device.
const struct pbl_i2c_bus_ops * ops Driver operations.
struct pbl_i2c_bus_state * state Runtime state.

◆ pbl_i2c_dev

struct pbl_i2c_dev

A peripheral on a bus.

Data Fields
uint16_t addr 7-bit address.
const struct pbl_i2c_bus * bus Bus.

Macro Definition Documentation

◆ PBL_I2C_BUS_INIT

#define PBL_I2C_BUS_INIT (   sym,
  _name,
  _parent,
  _ops 
)
Value:
{ \
.ops = (_ops), \
.state = &sym##_i2c_state, \
}
#define PBL_DEVICE_INIT_CLASS(sym, _name, _init, _parent, _cls)
Initializer for the struct pbl_device of device instance sym, in class _cls.
Definition device.h:153
int pbl_i2c_bus_dev_init(const struct pbl_device *dev)
Device init of every bus: sets the class state up, then calls the driver's init.
const struct pbl_device_class pbl_i2c_bus_class
Class of the I2C buses.

Initializer for the struct pbl_i2c_bus of bus sym.

Parameters
symSymbol of the bus instance, with its state defined by PBL_I2C_BUS_STATE_DEFINE().
_nameName.
_parentParent device, or NULL.
_opsDriver operations.

◆ PBL_I2C_BUS_STATE_DEFINE

#define PBL_I2C_BUS_STATE_DEFINE (   sym)
Value:
static struct pbl_i2c_bus_state sym##_i2c_state
#define PBL_DEVICE_STATE_DEFINE(sym)
Define the runtime state of device instance sym.
Definition device.h:132

Define the class state of bus sym, for the PBL_I2C_*_DEFINE() macro of a driver.

Parameters
symSymbol of the bus instance.

◆ PBL_I2C_DEV

#define PBL_I2C_DEV (   _bus,
  _addr 
)    {.bus = (_bus), .addr = (_addr)}

Initializer for a struct pbl_i2c_dev.

Parameters
_busBus.
_addr7-bit address.

◆ PBL_I2C_MSG_DMA_SAFE

#define PBL_I2C_MSG_DMA_SAFE   PBL_BIT(2)

The buffer may be accessed through DMA: aligned to PBL_DCACHE_LINE_SIZE and owning the whole cache lines it covers, PBL_DCACHE_ROUND_UP(len) bytes.

Declare it with PBL_ALIGNED(PBL_DCACHE_LINE_SIZE) and that size.

◆ PBL_I2C_MSG_NOSTART

#define PBL_I2C_MSG_NOSTART   PBL_BIT(1)

Continue the previous write without a repeated START or address.

Writes only.

◆ PBL_I2C_MSG_READ

#define PBL_I2C_MSG_READ   PBL_BIT(0)

Read from the device.

◆ PBL_I2C_MSG_WRITE

#define PBL_I2C_MSG_WRITE   0U

Write to the device.

Function Documentation

◆ pbl_i2c_burst_read()

static int pbl_i2c_burst_read ( const struct pbl_i2c_dev *  dev,
uint8_t  reg,
void *  buf,
uint32_t  len 
)
inlinestatic

Read consecutive registers.

Parameters
devDevice.
regFirst register address.
[out]bufBuffer.
lenNumber of bytes.
Returns
0 or a negative errno.

References pbl_i2c_msg::buf, pbl_i2c_msg::len, and pbl_i2c_write_read().

Referenced by pbl_i2c_reg_read_byte().

◆ pbl_i2c_burst_write()

static int pbl_i2c_burst_write ( const struct pbl_i2c_dev *  dev,
uint8_t  reg,
const void *  buf,
uint32_t  len 
)
inlinestatic

Write consecutive registers.

Parameters
devDevice.
regFirst register address.
bufData.
lenNumber of bytes.
Returns
0 or a negative errno.

References pbl_i2c_msg::buf, pbl_i2c_msg::flags, pbl_i2c_msg::len, PBL_I2C_MSG_NOSTART, PBL_I2C_MSG_WRITE, and pbl_i2c_transfer().

◆ pbl_i2c_bus_complete()

void pbl_i2c_bus_complete ( const struct pbl_i2c_bus *  bus,
int  status 
)

Report the outcome of the transfer in flight.

Called by the bus driver, typically from its interrupt handler.

Parameters
busBus.
status0 or a negative errno, see I2C.

◆ pbl_i2c_bus_dev_init()

int pbl_i2c_bus_dev_init ( const struct pbl_device *  dev)

Device init of every bus: sets the class state up, then calls the driver's init.

Parameters
devBus device.
Returns
0 or a negative errno.

◆ pbl_i2c_read()

static int pbl_i2c_read ( const struct pbl_i2c_dev *  dev,
void *  buf,
uint32_t  len 
)
inlinestatic

Read data.

Parameters
devDevice.
[out]bufBuffer.
lenNumber of bytes.
Returns
0 or a negative errno.

References pbl_i2c_msg::buf, pbl_i2c_msg::len, PBL_I2C_MSG_READ, and pbl_i2c_transfer().

◆ pbl_i2c_reg_read_byte()

static int pbl_i2c_reg_read_byte ( const struct pbl_i2c_dev *  dev,
uint8_t  reg,
uint8_t *  val 
)
inlinestatic

Read a register.

Parameters
devDevice.
regRegister address.
[out]valRegister value.
Returns
0 or a negative errno.

References pbl_i2c_burst_read().

◆ pbl_i2c_reg_update_byte()

int pbl_i2c_reg_update_byte ( const struct pbl_i2c_dev *  dev,
uint8_t  reg,
uint8_t  mask,
uint8_t  val 
)

Update bits of a register.

Reads the register and writes it back, if it changes, with the mask bits set to val. Not atomic with respect to other users of the device.

Parameters
devDevice.
regRegister address.
maskBits to update.
valNew value of the mask bits.
Returns
0 or a negative errno.

◆ pbl_i2c_reg_write_byte()

static int pbl_i2c_reg_write_byte ( const struct pbl_i2c_dev *  dev,
uint8_t  reg,
uint8_t  val 
)
inlinestatic

Write a register.

Parameters
devDevice.
regRegister address.
valValue.
Returns
0 or a negative errno.

References pbl_i2c_msg::buf, and pbl_i2c_write().

◆ pbl_i2c_transfer()

int pbl_i2c_transfer ( const struct pbl_i2c_dev *  dev,
struct pbl_i2c_msg *  msgs,
uint8_t  num_msgs 
)

Transfer messages to or from a device.

Parameters
devDevice.
msgsMessages.
num_msgsNumber of messages.
Returns
0 or a negative errno.

Referenced by pbl_i2c_burst_write(), pbl_i2c_read(), pbl_i2c_write(), and pbl_i2c_write_read().

◆ pbl_i2c_write()

static int pbl_i2c_write ( const struct pbl_i2c_dev *  dev,
const void *  buf,
uint32_t  len 
)
inlinestatic

Write data.

Parameters
devDevice.
bufData.
lenNumber of bytes.
Returns
0 or a negative errno.

References pbl_i2c_msg::buf, pbl_i2c_msg::flags, pbl_i2c_msg::len, PBL_I2C_MSG_WRITE, and pbl_i2c_transfer().

Referenced by pbl_i2c_reg_write_byte().

◆ pbl_i2c_write_read()

static int pbl_i2c_write_read ( const struct pbl_i2c_dev *  dev,
const void *  wbuf,
uint32_t  wlen,
void *  rbuf,
uint32_t  rlen 
)
inlinestatic

Write data, then read data after a repeated START.

Parameters
devDevice.
wbufData to write.
wlenNumber of bytes to write.
[out]rbufBuffer to read into.
rlenNumber of bytes to read.
Returns
0 or a negative errno.

References pbl_i2c_msg::buf, pbl_i2c_msg::flags, pbl_i2c_msg::len, PBL_I2C_MSG_READ, PBL_I2C_MSG_WRITE, and pbl_i2c_transfer().

Referenced by pbl_i2c_burst_read().

Variable Documentation

◆ pbl_i2c_bus_class

const struct pbl_device_class pbl_i2c_bus_class
extern

Class of the I2C buses.