objc

i2c.h
Go to the documentation of this file.
1 
11 #pragma once
12 #include "gpio.h"
13 #include <stdbool.h>
14 #include <stddef.h>
15 #include <stdint.h>
16 
18 // TYPES
19 
25 typedef struct hw_i2c_t {
26  uint8_t adapter;
29  uint32_t baudrate;
30  uint8_t reserved[3];
31 } hw_i2c_t;
32 
34 // LIFECYCLE
35 
47 
64 hw_i2c_t hw_i2c_init(uint8_t adapter, uint8_t sda, uint8_t scl,
65  uint32_t baudrate);
66 
76 void hw_i2c_finalize(hw_i2c_t *i2c);
77 
79 // PROPERTIES
80 
90 uint8_t hw_i2c_count(void);
91 
100 static inline bool hw_i2c_valid(hw_i2c_t *i2c) {
101  return i2c && i2c->baudrate > 0;
102 }
103 
105 // METHODS
106 
119 bool hw_i2c_detect(hw_i2c_t *i2c, uint8_t addr);
120 
143 size_t hw_i2c_xfr(hw_i2c_t *i2c, uint8_t addr, void *data, size_t tx, size_t rx,
144  uint32_t timeout_ms);
145 
159 size_t hw_i2c_read(hw_i2c_t *i2c, uint8_t addr, uint8_t reg, void *data,
160  size_t len, uint32_t timeout_ms);
161 
175 size_t hw_i2c_write(hw_i2c_t *i2c, uint8_t addr, uint8_t reg, const void *data,
176  size_t len, uint32_t timeout_ms);
hw_i2c_t hw_i2c_init_default(uint32_t baudrate)
Initialize an I2C interface using default pins and adapter.
struct hw_i2c_t hw_i2c_t
I2C adapter.
static bool hw_i2c_valid(hw_i2c_t *i2c)
Get true if the I2C interface is valid.
Definition: i2c.h:100
hw_gpio_t sda
I2C data pin number.
Definition: i2c.h:27
GPIO (General Purpose Input/Output) interface.
uint32_t baudrate
I2C baud rate in Hz.
Definition: i2c.h:29
size_t hw_i2c_xfr(hw_i2c_t *i2c, uint8_t addr, void *data, size_t tx, size_t rx, uint32_t timeout_ms)
Perform an I2C transfer operation (read, write, or combined).
hw_gpio_t scl
I2C clock pin number.
Definition: i2c.h:28
bool hw_i2c_detect(hw_i2c_t *i2c, uint8_t addr)
Detect if an I2C device is present at the specified address.
void hw_i2c_finalize(hw_i2c_t *i2c)
Finalize and release an I2C interface.
I2C adapter.
Definition: i2c.h:25
size_t hw_i2c_write(hw_i2c_t *i2c, uint8_t addr, uint8_t reg, const void *data, size_t len, uint32_t timeout_ms)
Write data to a specific register of an I2C device.
uint8_t reserved[3]
Reserved for user data.
Definition: i2c.h:30
size_t hw_i2c_read(hw_i2c_t *i2c, uint8_t addr, uint8_t reg, void *data, size_t len, uint32_t timeout_ms)
Read data from a specific register of an I2C device.
uint8_t hw_i2c_count(void)
Get the total number of available I2C adapters.
hw_i2c_t hw_i2c_init(uint8_t adapter, uint8_t sda, uint8_t scl, uint32_t baudrate)
Initialize an I2C interface with specific adapter and pins.
uint8_t adapter
I2C adapter number (0, 1, etc.)
Definition: i2c.h:26
GPIO logical pin structure.
Definition: gpio.h:60