31#include "freertos/FreeRTOS.h"
32#include "freertos/task.h"
33#include "driver/gpio.h"
38#if CONFIG_I2C_COMMUNICATION_METHOD_BUILTIN_RW || CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
40static const char *TAG =
"I2C";
42#define I2C_MASTER_NUM (i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM
43#define I2C_MASTER_SDA_IO (gpio_num_t)CONFIG_SENSOR_SDA
44#define I2C_MASTER_SCL_IO (gpio_num_t)CONFIG_SENSOR_SCL
45#define I2C_MASTER_FREQ_HZ CONFIG_I2C_MASTER_FREQUENCY
46#define I2C_MASTER_TX_BUF_DISABLE 0
47#define I2C_MASTER_RX_BUF_DISABLE 0
49#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
50#include "soc/clk_tree_defs.h"
52i2c_master_dev_handle_t i2c_device;
56i2c_master_bus_handle_t bus_handle;
60#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
64bool i2c_wr_function(uint8_t addr, uint8_t reg, uint8_t *
buf,
size_t len,
bool writeReg,
bool isWrite)
72#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
75 uint8_t *write_buffer = (uint8_t *)malloc(
sizeof(uint8_t) * (len + 1));
79 write_buffer[0] = reg;
80 memcpy(write_buffer + 1,
buf, len);
82 ret = i2c_master_transmit(
89 ret = i2c_master_transmit(i2c_device,
buf, len, -1);
94 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
95 i2c_master_start(
cmd);
96 i2c_master_write_byte(
cmd, (addr << 1) | I2C_MASTER_WRITE,
true);
98 i2c_master_write_byte(
cmd, reg,
true);
100 i2c_master_write(
cmd,
buf, len,
true);
101 i2c_master_stop(
cmd);
103 i2c_cmd_link_delete(
cmd);
105 ESP_LOGE(TAG,
"i2c_write_error.");
109 return ret == ESP_OK ? true :
false;
120#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
122 ret = i2c_master_transmit_receive(i2c_device, (
const uint8_t *)®, 1,
buf, len, -1);
126 i2c_cmd_handle_t
cmd;
128 cmd = i2c_cmd_link_create();
129 i2c_master_start(
cmd);
130 i2c_master_write_byte(
cmd, (addr << 1) | I2C_MASTER_WRITE,
true);
131 i2c_master_write_byte(
cmd, reg,
true);
132 i2c_master_stop(
cmd);
134 i2c_cmd_link_delete(
cmd);
136 ESP_LOGE(TAG,
"i2c_master_cmd_begin failed!");
141 cmd = i2c_cmd_link_create();
142 i2c_master_start(
cmd);
143 i2c_master_write_byte(
cmd, (addr << 1) | I2C_MASTER_READ,
true);
145 i2c_master_read(
cmd,
buf, len - 1, I2C_MASTER_ACK);
147 i2c_master_read_byte(
cmd, &
buf[len - 1], I2C_MASTER_NACK);
148 i2c_master_stop(
cmd);
150 i2c_cmd_link_delete(
cmd);
152 ESP_LOGE(TAG,
"i2c_read_error.");
155 return ret == ESP_OK ? true :
false;
163int i2c_read_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
173#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
175 ret = i2c_master_transmit_receive(
177 (
const uint8_t *)®Addr,
184 i2c_cmd_handle_t
cmd;
186 cmd = i2c_cmd_link_create();
187 i2c_master_start(
cmd);
188 i2c_master_write_byte(
cmd, (devAddr << 1) | I2C_MASTER_WRITE,
true);
189 i2c_master_write_byte(
cmd, regAddr,
true);
190 i2c_master_stop(
cmd);
192 i2c_cmd_link_delete(
cmd);
194 ESP_LOGE(TAG,
"i2c_master_cmd_begin failed!");
197 cmd = i2c_cmd_link_create();
198 i2c_master_start(
cmd);
199 i2c_master_write_byte(
cmd, (devAddr << 1) | I2C_MASTER_READ,
true);
201 i2c_master_read(
cmd, data, len - 1, I2C_MASTER_ACK);
203 i2c_master_read_byte(
cmd, &data[len - 1], I2C_MASTER_NACK);
204 i2c_master_stop(
cmd);
206 i2c_cmd_link_delete(
cmd);
208 ESP_LOGE(TAG,
"i2c_read_error.");
211 return ret == ESP_OK ? 0 : -1;
224#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
225 uint8_t *write_buffer = (uint8_t *)malloc(
sizeof(uint8_t) * (len + 1));
229 write_buffer[0] = regAddr;
230 memcpy(write_buffer + 1, data, len);
232 ret = i2c_master_transmit(
239 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
240 i2c_master_start(
cmd);
241 i2c_master_write_byte(
cmd, (devAddr << 1) | I2C_MASTER_WRITE,
true);
242 i2c_master_write_byte(
cmd, regAddr,
true);
243 i2c_master_write(
cmd, data, len,
true);
244 i2c_master_stop(
cmd);
246 i2c_cmd_link_delete(
cmd);
248 ESP_LOGE(TAG,
"i2c_write_error.");
251 return ret == ESP_OK ? 0 : -1;
254esp_err_t i2c_drv_device_init(uint8_t address)
256#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
257 i2c_device_config_t i2c_dev_conf = {
258 .dev_addr_length = I2C_ADDR_BIT_LEN_7,
259 .device_address = address,
262 return i2c_master_bus_add_device(bus_handle, &i2c_dev_conf, &i2c_device);
276#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
278 ESP_LOGI(TAG,
"Implemented using read and write callback methods (Use higher version >= 5.0 API)");
280 i2c_master_bus_config_t i2c_bus_config;
281 memset(&i2c_bus_config, 0,
sizeof(i2c_bus_config));
282 i2c_bus_config.clk_source = I2C_CLK_SRC_DEFAULT;
286 i2c_bus_config.glitch_ignore_cnt = 7;
287 i2c_bus_config.flags.enable_internal_pullup =
true;
289 ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &bus_handle));
294 ESP_LOGI(TAG,
"Implemented using read and write callback methods (Use lower version < 5.0 API)");
296 i2c_config_t i2c_conf ;
297 memset(&i2c_conf, 0,
sizeof(i2c_conf));
298 i2c_conf.mode = I2C_MODE_MASTER;
301 i2c_conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
302 i2c_conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
311#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
313 esp_err_t err = ESP_OK;
314 uint8_t address = 0x00;
315 printf(
"Scan I2C Devices:\n");
316 printf(
" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
317 for (
int i = 0;
i < 128;
i += 16) {
319 for (
int j = 0; j < 16; j++) {
322 err = i2c_master_probe(bus_handle, address, 100);
324 printf(
"%02x ", address);
325 }
else if (err == ESP_ERR_TIMEOUT) {
336 printf(
"Scan I2C Devices:\n");
337 printf(
" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
338 for (
int i = 0;
i < 128;
i += 16) {
340 for (
int j = 0; j < 16; j++) {
343 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
344 i2c_master_start(
cmd);
345 i2c_master_write_byte(
cmd, (address << 1) | I2C_MASTER_WRITE,
true);
346 i2c_master_stop(
cmd);
347 esp_err_t ret = i2c_master_cmd_begin(
I2C_MASTER_NUM,
cmd, 50 / portTICK_PERIOD_MS);
348 i2c_cmd_link_delete(
cmd);
350 printf(
"%02x ", address);
351 }
else if (ret == ESP_ERR_TIMEOUT) {
364#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
365 return ESP_OK == i2c_master_probe(bus_handle, devAddr, 1000);
367 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
368 i2c_master_start(
cmd);
369 i2c_master_write_byte(
cmd, (devAddr << 1) | I2C_MASTER_WRITE,
true);
370 i2c_master_stop(
cmd);
371 esp_err_t ret = i2c_master_cmd_begin(
I2C_MASTER_NUM,
cmd, 50 / portTICK_PERIOD_MS);
372 i2c_cmd_link_delete(
cmd);
373 return (ret == ESP_OK);
bool i2c_wr_function(uint8_t addr, uint8_t reg, uint8_t *buf, size_t len, bool writeReg, bool isWrite)
#define I2C_MASTER_SCL_IO
esp_err_t i2c_drv_init(void)
i2c master initialization
#define I2C_MASTER_RX_BUF_DISABLE
bool i2c_drv_probe(uint8_t devAddr)
#define I2C_MASTER_FREQ_HZ
#define I2C_MASTER_TX_BUF_DISABLE
#define I2C_MASTER_SDA_IO
int i2c_write_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
int i2c_read_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)