VSF Documented
i2c_master.h
Go to the documentation of this file.
1/*****************************************************************************
2 * Copyright(C)2009-2026 by VSF Team *
3 * *
4 * Licensed under the Apache License, Version 2.0 (the "License"); *
5 * you may not use this file except in compliance with the License. *
6 * You may obtain a copy of the License at *
7 * *
8 * http://www.apache.org/licenses/LICENSE-2.0 *
9 * *
10 * Unless required by applicable law or agreed to in writing, software *
11 * distributed under the License is distributed on an "AS IS" BASIS, *
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. *
13 * See the License for the specific language governing permissions and *
14 * limitations under the License. *
15 * *
16 ****************************************************************************/
17
18/*
19 * Clean-room re-implementation of ESP-IDF "driver/i2c_master.h" (new driver,
20 * v5.2+).
21 *
22 * Authored from ESP-IDF v5.2 public API only. No code copied from the
23 * ESP-IDF source tree. The VSF port bridges onto
24 *
25 * hal/driver/common/template/vsf_template_i2c.h
26 *
27 * via a caller-supplied pool of vsf_i2c_t * instances (see
28 * vsf_espidf_cfg_t::i2c
29 * ).
30 *
31 * The new ESP-IDF I2C driver uses a bus-device model:
32 * - Bus = one I2C controller (port 0 / 1), mapped to a vsf_i2c_t*.
33 * - Device = a soft handle carrying (bus, address, speed).
34 *
35 * Only Master mode is implemented (matches the vast majority of ESP-IDF
36 * application use cases). Slave mode can be added in a future i2c_slave.h.
37 *
38 * All transfer functions are synchronous / blocking, matching ESP-IDF
39 * semantics. The timeout parameter is forwarded as a FreeRTOS tick limit.
40 */
41
42#ifndef __VSF_ESPIDF_DRIVER_I2C_MASTER_H__
43#define __VSF_ESPIDF_DRIVER_I2C_MASTER_H__
44
45#include <stdint.h>
46#include <stdbool.h>
47#include <stddef.h>
48
49#include "esp_err.h"
50#include "driver/i2c_types.h"
51#include "driver/gpio.h"
52
53#ifdef __cplusplus
54extern "C" {
55#endif
56
57/*============================ TYPES =========================================*/
58
63
68
77typedef struct {
85 struct {
87 } flags;
89
93typedef struct {
98 struct {
100 } flags;
102
103/*============================ BUS LIFECYCLE =================================*/
104
118 i2c_master_bus_handle_t *ret_bus_handle);
119
127
128/*============================ DEVICE LIFECYCLE ==============================*/
129
141 i2c_master_bus_handle_t bus_handle,
142 const i2c_device_config_t *dev_config,
143 i2c_master_dev_handle_t *ret_handle);
144
149
150/*============================ DATA TRANSFER =================================*/
151
161 const uint8_t *write_buffer,
162 size_t write_size,
163 int xfer_timeout_ms);
164
174 uint8_t *read_buffer,
175 size_t read_size,
176 int xfer_timeout_ms);
177
192 const uint8_t *write_buffer,
193 size_t write_size,
194 uint8_t *read_buffer,
195 size_t read_size,
196 int xfer_timeout_ms);
197
198/*============================ BUS UTILITIES =================================*/
199
206 uint16_t address,
207 int xfer_timeout_ms);
208
213
221 i2c_master_bus_handle_t bus_handle,
222 int timeout_ms);
223
224#ifdef __cplusplus
225}
226#endif
227
228#endif /* __VSF_ESPIDF_DRIVER_I2C_MASTER_H__ */
int esp_err_t
Definition esp_err.h:41
esp_err_t i2c_master_transmit(i2c_master_dev_handle_t i2c_dev, const uint8_t *write_buffer, size_t write_size, int xfer_timeout_ms)
Master write (START + addr/W + data + STOP).
Definition driver_i2c_port.c:396
esp_err_t i2c_master_receive(i2c_master_dev_handle_t i2c_dev, uint8_t *read_buffer, size_t read_size, int xfer_timeout_ms)
Master read (START + addr/R + data + STOP).
Definition driver_i2c_port.c:432
struct i2c_master_bus_t * i2c_master_bus_handle_t
Opaque handle for an I2C master bus.
Definition i2c_master.h:62
esp_err_t i2c_master_bus_wait_all_done(i2c_master_bus_handle_t bus_handle, int timeout_ms)
Wait until all queued transfers are complete.
Definition driver_i2c_port.c:576
struct i2c_master_dev_t * i2c_master_dev_handle_t
Opaque handle for an I2C device attached to a master bus.
Definition i2c_master.h:67
esp_err_t i2c_master_bus_reset(i2c_master_bus_handle_t bus_handle)
Reset the I2C bus (toggle SCL to release SDA).
Definition driver_i2c_port.c:559
esp_err_t i2c_master_transmit_receive(i2c_master_dev_handle_t i2c_dev, const uint8_t *write_buffer, size_t write_size, uint8_t *read_buffer, size_t read_size, int xfer_timeout_ms)
Combined write-then-read with repeated START.
Definition driver_i2c_port.c:467
esp_err_t i2c_new_master_bus(const i2c_master_bus_config_t *bus_config, i2c_master_bus_handle_t *ret_bus_handle)
Create a new I2C master bus.
Definition driver_i2c_port.c:262
esp_err_t i2c_del_master_bus(i2c_master_bus_handle_t bus_handle)
Delete the I2C master bus and release the port.
Definition driver_i2c_port.c:322
esp_err_t i2c_master_bus_add_device(i2c_master_bus_handle_t bus_handle, const i2c_device_config_t *dev_config, i2c_master_dev_handle_t *ret_handle)
Attach a device to the master bus.
Definition driver_i2c_port.c:349
esp_err_t i2c_master_probe(i2c_master_bus_handle_t bus_handle, uint16_t address, int xfer_timeout_ms)
Probe a device on the bus (write 0 bytes).
Definition driver_i2c_port.c:524
esp_err_t i2c_master_bus_rm_device(i2c_master_dev_handle_t handle)
Remove a device from the master bus.
Definition driver_i2c_port.c:382
int i2c_port_num_t
I2C port number.
Definition i2c_types.h:42
int i2c_clock_source_t
I2C clock source.
Definition i2c_types.h:70
i2c_addr_bit_len_t
I2C device address bit length.
Definition i2c_types.h:58
int32_t gpio_num_t
GPIO pin number (flat encoding, see file comment).
Definition gpio.h:59
unsigned short uint16_t
Definition stdint.h:7
unsigned uint32_t
Definition stdint.h:9
unsigned char uint8_t
Definition stdint.h:5
Per-device configuration attached to a master bus.
Definition i2c_master.h:93
uint32_t disable_ack_check
Definition i2c_master.h:99
uint32_t scl_speed_hz
Definition i2c_master.h:96
uint16_t device_address
Definition i2c_master.h:95
uint32_t scl_wait_us
Definition i2c_master.h:97
i2c_addr_bit_len_t dev_addr_length
Definition i2c_master.h:94
I2C master bus configuration.
Definition i2c_master.h:77
gpio_num_t scl_io_num
Definition i2c_master.h:80
gpio_num_t sda_io_num
Definition i2c_master.h:81
int intr_priority
Definition i2c_master.h:83
i2c_port_num_t i2c_port
Definition i2c_master.h:79
uint32_t enable_internal_pullup
Definition i2c_master.h:86
size_t trans_queue_depth
Definition i2c_master.h:84
i2c_clock_source_t clk_source
Definition i2c_master.h:78
uint8_t glitch_ignore_cnt
Definition i2c_master.h:82
Definition driver_i2c_port.c:82
Definition driver_i2c_port.c:96
Generated from commit: vsfteam/vsf@015f4d1