31#include "freertos/FreeRTOS.h"
32#include "freertos/task.h"
33#include "driver/gpio.h"
38static const char *TAG =
"I2C";
40#define I2C_MASTER_NUM (i2c_port_t)CONFIG_I2C_MASTER_PORT_NUM
41#define I2C_MASTER_SDA_IO (gpio_num_t)CONFIG_SENSOR_SDA
42#define I2C_MASTER_SCL_IO (gpio_num_t)CONFIG_SENSOR_SCL
43#define I2C_MASTER_FREQ_HZ CONFIG_I2C_MASTER_FREQUENCY
44#define I2C_MASTER_TX_BUF_DISABLE 0
45#define I2C_MASTER_RX_BUF_DISABLE 0
47#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
48#include "soc/clk_tree_defs.h"
50i2c_master_dev_handle_t i2c_device;
54i2c_master_bus_handle_t bus_handle;
58#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
63int i2c_read_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
73#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
75 ret = i2c_master_transmit_receive(
77 (
const uint8_t *)®Addr,
86 cmd = i2c_cmd_link_create();
87 i2c_master_start(
cmd);
88 i2c_master_write_byte(
cmd, (devAddr << 1) | I2C_MASTER_WRITE,
true);
89 i2c_master_write_byte(
cmd, regAddr,
true);
92 i2c_cmd_link_delete(
cmd);
94 ESP_LOGE(TAG,
"PMU i2c_master_cmd_begin FAILED! > ");
97 cmd = i2c_cmd_link_create();
98 i2c_master_start(
cmd);
99 i2c_master_write_byte(
cmd, (devAddr << 1) | I2C_MASTER_READ,
true);
101 i2c_master_read(
cmd, data, len - 1, I2C_MASTER_ACK);
103 i2c_master_read_byte(
cmd, &data[len - 1], I2C_MASTER_NACK);
104 i2c_master_stop(
cmd);
106 i2c_cmd_link_delete(
cmd);
108 ESP_LOGE(TAG,
"i2c_read_error.");
111 return ret == ESP_OK ? 0 : -1;
124#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
125 uint8_t *write_buffer = (uint8_t *)malloc(
sizeof(uint8_t) * (len + 1));
129 write_buffer[0] = regAddr;
130 memcpy(write_buffer + 1, data, len);
132 ret = i2c_master_transmit(
139 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
140 i2c_master_start(
cmd);
141 i2c_master_write_byte(
cmd, (devAddr << 1) | I2C_MASTER_WRITE,
true);
142 i2c_master_write_byte(
cmd, regAddr,
true);
143 i2c_master_write(
cmd, data, len,
true);
144 i2c_master_stop(
cmd);
146 i2c_cmd_link_delete(
cmd);
148 ESP_LOGE(TAG,
"i2c_write_error.");
151 return ret == ESP_OK ? 0 : -1;
154esp_err_t i2c_drv_device_init(uint8_t address)
156#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
157 i2c_device_config_t i2c_dev_conf = {
158 .dev_addr_length = I2C_ADDR_BIT_LEN_7,
159 .device_address = address,
162 return i2c_master_bus_add_device(bus_handle, &i2c_dev_conf, &i2c_device);
176#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
178 ESP_LOGI(TAG,
"Implemented using read and write callback methods (Use higher version >= 5.0 API)");
180 i2c_master_bus_config_t i2c_bus_config;
181 memset(&i2c_bus_config, 0,
sizeof(i2c_bus_config));
182 i2c_bus_config.clk_source = I2C_CLK_SRC_DEFAULT;
186 i2c_bus_config.glitch_ignore_cnt = 7;
187 i2c_bus_config.flags.enable_internal_pullup =
true;
188 ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &bus_handle));
193 ESP_LOGI(TAG,
"Implemented using read and write callback methods (Use lower version < 5.0 API)");
195 i2c_config_t i2c_conf ;
196 memset(&i2c_conf, 0,
sizeof(i2c_conf));
197 i2c_conf.mode = I2C_MODE_MASTER;
200 i2c_conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
201 i2c_conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
210#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
212 esp_err_t err = ESP_OK;
213 uint8_t address = 0x00;
214 printf(
"Scand I2C Devices:\n");
215 printf(
" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
216 for (
int i = 0;
i < 128;
i += 16) {
218 for (
int j = 0; j < 16; j++) {
221 err = i2c_master_probe(bus_handle, address, 1000);
223 printf(
"%02x ", address);
224 }
else if (err == ESP_ERR_TIMEOUT) {
235 printf(
"Scand I2C Devices:\n");
236 printf(
" 0 1 2 3 4 5 6 7 8 9 a b c d e f\r\n");
237 for (
int i = 0;
i < 128;
i += 16) {
239 for (
int j = 0; j < 16; j++) {
242 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
243 i2c_master_start(
cmd);
244 i2c_master_write_byte(
cmd, (address << 1) | I2C_MASTER_WRITE,
true);
245 i2c_master_stop(
cmd);
246 esp_err_t ret = i2c_master_cmd_begin(
I2C_MASTER_NUM,
cmd, 50 / portTICK_PERIOD_MS);
247 i2c_cmd_link_delete(
cmd);
249 printf(
"%02x ", address);
250 }
else if (ret == ESP_ERR_TIMEOUT) {
263#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
264 return ESP_OK == i2c_master_probe(bus_handle, devAddr, 1000);
266 i2c_cmd_handle_t
cmd = i2c_cmd_link_create();
267 i2c_master_start(
cmd);
268 i2c_master_write_byte(
cmd, (devAddr << 1) | I2C_MASTER_WRITE,
true);
269 i2c_master_stop(
cmd);
270 esp_err_t ret = i2c_master_cmd_begin(
I2C_MASTER_NUM,
cmd, 50 / portTICK_PERIOD_MS);
271 i2c_cmd_link_delete(
cmd);
272 return (ret == ESP_OK);
#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)