SensorLib 0.5.0
Multi-platform sensor driver library for Arduino, PlatformIO, and ESP-IDF
Loading...
Searching...
No Matches
esp_idf_touch/main/i2c_driver.cpp
Go to the documentation of this file.
1
30#include <cstring>
31#include "freertos/FreeRTOS.h"
32#include "freertos/task.h"
33#include "driver/gpio.h"
34#include "esp_log.h"
35#include "sdkconfig.h"
36#include "i2c_driver.h"
37
38static const char *TAG = "I2C";
39
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"
49
50i2c_master_dev_handle_t i2c_device;
51
52// * Using the new API of esp-idf 5.x, you need to pass the I2C BUS handle,
53// * which is useful when the bus shares multiple devices.
54i2c_master_bus_handle_t bus_handle;
55#endif
56
57
58#if CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
59
63int i2c_read_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
64{
65 esp_err_t ret;
66 if (len == 0) {
67 return ESP_OK;
68 }
69 if (data == NULL) {
70 return ESP_FAIL;
71 }
72
73#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
74
75 ret = i2c_master_transmit_receive(
76 i2c_device,
77 (const uint8_t *)&regAddr,
78 1,
79 data,
80 len,
81 -1);
82#else
83
84 i2c_cmd_handle_t cmd;
85
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);
90 i2c_master_stop(cmd);
91 ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdTICKS_TO_MS(1000));
92 i2c_cmd_link_delete(cmd);
93 if (ret != ESP_OK) {
94 ESP_LOGE(TAG, "PMU i2c_master_cmd_begin FAILED! > ");
95 return ESP_FAIL;
96 }
97 cmd = i2c_cmd_link_create();
98 i2c_master_start(cmd);
99 i2c_master_write_byte(cmd, (devAddr << 1) | I2C_MASTER_READ, true);
100 if (len > 1) {
101 i2c_master_read(cmd, data, len - 1, I2C_MASTER_ACK);
102 }
103 i2c_master_read_byte(cmd, &data[len - 1], I2C_MASTER_NACK);
104 i2c_master_stop(cmd);
105 ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdTICKS_TO_MS(1000));
106 i2c_cmd_link_delete(cmd);
107 if (ret != ESP_OK) {
108 ESP_LOGE(TAG, "i2c_read_error.");
109 }
110#endif
111 return ret == ESP_OK ? 0 : -1;
112}
113
117int i2c_write_callback(uint8_t devAddr, uint8_t regAddr, uint8_t *data, uint8_t len)
118{
119 esp_err_t ret;
120 if (data == NULL) {
121 return ESP_FAIL;
122 }
123
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));
126 if (!write_buffer) {
127 return -1;
128 }
129 write_buffer[0] = regAddr;
130 memcpy(write_buffer + 1, data, len);
131
132 ret = i2c_master_transmit(
133 i2c_device,
134 write_buffer,
135 len + 1,
136 -1);
137 free(write_buffer);
138#else
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);
145 ret = i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, pdTICKS_TO_MS(1000));
146 i2c_cmd_link_delete(cmd);
147 if (ret != ESP_OK) {
148 ESP_LOGE(TAG, "i2c_write_error.");
149 }
150#endif
151 return ret == ESP_OK ? 0 : -1;
152}
153
154esp_err_t i2c_drv_device_init(uint8_t address)
155{
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,
160 .scl_speed_hz = I2C_MASTER_FREQ_HZ,
161 };
162 return i2c_master_bus_add_device(bus_handle, &i2c_dev_conf, &i2c_device);
163#else
164 return ESP_OK;
165#endif
166}
167
168#endif //CONFIG_I2C_COMMUNICATION_METHOD_CALLBACK_RW
169
170
174esp_err_t i2c_drv_init(void)
175{
176#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
177
178 ESP_LOGI(TAG, "Implemented using read and write callback methods (Use higher version >= 5.0 API)");
179
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;
183 i2c_bus_config.i2c_port = I2C_MASTER_NUM;
184 i2c_bus_config.scl_io_num = (gpio_num_t)I2C_MASTER_SCL_IO;
185 i2c_bus_config.sda_io_num = (gpio_num_t)I2C_MASTER_SDA_IO;
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));
189
190 return ESP_OK;
191#else
192
193 ESP_LOGI(TAG, "Implemented using read and write callback methods (Use lower version < 5.0 API)");
194
195 i2c_config_t i2c_conf ;
196 memset(&i2c_conf, 0, sizeof(i2c_conf));
197 i2c_conf.mode = I2C_MODE_MASTER;
198 i2c_conf.sda_io_num = I2C_MASTER_SDA_IO;
199 i2c_conf.scl_io_num = I2C_MASTER_SCL_IO;
200 i2c_conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
201 i2c_conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
202 i2c_conf.master.clk_speed = I2C_MASTER_FREQ_HZ;
203 i2c_param_config(I2C_MASTER_NUM, &i2c_conf);
204 return i2c_driver_install(I2C_MASTER_NUM, i2c_conf.mode, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
205#endif
206}
207
209{
210#if (ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)) && defined(CONFIG_SENSORLIB_ESP_IDF_NEW_API)
211
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) {
217 printf("%02x: ", i);
218 for (int j = 0; j < 16; j++) {
219 fflush(stdout);
220 address = i + j;
221 err = i2c_master_probe(bus_handle, address, 1000);
222 if (err == ESP_OK) {
223 printf("%02x ", address);
224 } else if (err == ESP_ERR_TIMEOUT) {
225 printf("UU ");
226 } else {
227 printf("-- ");
228 }
229 }
230 printf("\r\n");
231 }
232 printf("\n\n\n");
233#else
234 uint8_t address;
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) {
238 printf("%02x: ", i);
239 for (int j = 0; j < 16; j++) {
240 fflush(stdout);
241 address = i + 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);
248 if (ret == ESP_OK) {
249 printf("%02x ", address);
250 } else if (ret == ESP_ERR_TIMEOUT) {
251 printf("UU ");
252 } else {
253 printf("-- ");
254 }
255 }
256 printf("\r\n");
257 }
258#endif
259}
260
261bool i2c_drv_probe(uint8_t devAddr)
262{
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);
265#else
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);
273#endif //ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5,0,0)
274}
275
#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
@license MIT License
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)
uint8_t i