v1.5.0
Go to the documentation of this file.
7 #ifndef _HARDWARE_I2C_H
8 #define _HARDWARE_I2C_H
12 #include "hardware/structs/i2c.h"
13 #include "hardware/regs/dreq.h"
16 #ifndef PARAM_ASSERTIONS_ENABLED_I2C
17 #define PARAM_ASSERTIONS_ENABLED_I2C 0
68 #define i2c0 (&i2c0_inst)
69 #define i2c1 (&i2c1_inst)
71 #if !defined(PICO_DEFAULT_I2C_INSTANCE) && defined(PICO_DEFAULT_I2C)
72 #define PICO_DEFAULT_I2C_INSTANCE (__CONCAT(i2c,PICO_DEFAULT_I2C))
75 #ifdef PICO_DEFAULT_I2C_INSTANCE
76 #define i2c_default PICO_DEFAULT_I2C_INSTANCE
137 bool restart_on_next;
147 invalid_params_if(I2C, i2c !=
i2c0 && i2c !=
i2c1);
148 return i2c ==
i2c1 ? 1 : 0;
156 static inline i2c_inst_t *i2c_get_instance(uint instance) {
157 static_assert(NUM_I2CS == 2,
"");
158 invalid_params_if(I2C, instance >= NUM_I2CS);
213 int i2c_write_timeout_per_char_us(
i2c_inst_t *i2c, uint8_t addr,
const uint8_t *src,
size_t len,
bool nostop, uint timeout_per_char_us);
232 int i2c_read_timeout_per_char_us(
i2c_inst_t *i2c, uint8_t addr, uint8_t *dst,
size_t len,
bool nostop, uint timeout_per_char_us);
269 const size_t IC_TX_BUFFER_DEPTH = 16;
270 return IC_TX_BUFFER_DEPTH - i2c_get_hw(i2c)->txflr;
281 return i2c_get_hw(i2c)->rxflr;
295 for (
size_t i = 0; i < len; ++i) {
299 i2c_get_hw(i2c)->data_cmd = *src++;
314 for (
size_t i = 0; i < len; ++i) {
317 *dst++ = (uint8_t)i2c_get_hw(i2c)->data_cmd;
332 assert(hw->status & I2C_IC_STATUS_RFNE_BITS);
333 return (uint8_t)hw->data_cmd;
347 assert(hw->status & I2C_IC_STATUS_TFNF_BITS);
348 hw->data_cmd = value;
359 static_assert(DREQ_I2C0_RX == DREQ_I2C0_TX + 1,
"");
360 static_assert(DREQ_I2C1_RX == DREQ_I2C1_TX + 1,
"");
361 static_assert(DREQ_I2C1_TX == DREQ_I2C0_TX + 2,
"");
static void i2c_write_byte_raw(i2c_inst_t *i2c, uint8_t value)
Push a byte into I2C Tx FIFO.
Definition: i2c.h:345
static absolute_time_t make_timeout_time_us(uint64_t us)
Convenience method to get the timestamp a number of microseconds from the current time.
Definition: time.h:131
#define i2c1
Identifier for I2C HW Block 1.
Definition: i2c.h:69
static void i2c_write_raw_blocking(i2c_inst_t *i2c, const uint8_t *src, size_t len)
Write direct to TX FIFO.
Definition: i2c.h:294
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 ...
Definition: i2c.c:247
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.
Definition: i2c.c:243
static size_t i2c_get_write_available(i2c_inst_t *i2c)
Determine non-blocking write space available.
Definition: i2c.h:268
uint i2c_set_baudrate(i2c_inst_t *i2c, uint baudrate)
Set I2C baudrate.
Definition: i2c.c:64
void i2c_set_slave_mode(i2c_inst_t *i2c, bool slave, uint8_t addr)
Set I2C port to slave mode.
Definition: i2c.c:114
static size_t i2c_get_read_available(i2c_inst_t *i2c)
Determine number of bytes received.
Definition: i2c.h:280
#define i2c0
Identifier for I2C HW Block 0.
Definition: i2c.h:68
void i2c_deinit(i2c_inst_t *i2c)
Disable the I2C HW block.
Definition: i2c.c:60
i2c_inst_t i2c0_inst
Definition: i2c.c:15
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.
Definition: i2c.h:358
static uint i2c_hw_index(i2c_inst_t *i2c)
Convert I2C instance to hardware instance number.
Definition: i2c.h:146
static void i2c_read_raw_blocking(i2c_inst_t *i2c, uint8_t *dst, size_t len)
Read direct from RX FIFO.
Definition: i2c.h:313
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.
Definition: i2c.h:208
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...
Definition: i2c.c:327
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.
Definition: i2c.h:227
static uint8_t i2c_read_byte_raw(i2c_inst_t *i2c)
Pop a byte from I2C Rx FIFO.
Definition: i2c.h:330
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.
Definition: i2c.c:323
uint i2c_init(i2c_inst_t *i2c, uint baudrate)
Initialise the I2C HW block.
Definition: i2c.c:34