-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathi2cdev_legacy.c
More file actions
793 lines (683 loc) · 29.5 KB
/
i2cdev_legacy.c
File metadata and controls
793 lines (683 loc) · 29.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
/*
* The MIT License (MIT)
*
* Copyright (c) 2018 Ruslan V. Uss <unclerus@gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
/**
* @file i2cdev.c
*
* ESP-IDF I2C master thread-safe functions for communication with I2C slave
*
* Copyright (c) 2018 Ruslan V. Uss <unclerus@gmail.com>
* Updated 2025 by quinkq to use newer ESP-IDF I2C master driver API
* MIT Licensed as described in the file LICENSE
*/
#include "esp_idf_lib_helpers.h" // For HELPER_TARGET_IS_ESP32 etc.
#include "i2cdev.h" // Common header
#include <driver/i2c.h> // Legacy I2C driver
#include <esp_log.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <inttypes.h>
#include <sdkconfig.h>
#if !HELPER_TARGET_IS_ESP8266
#include <soc/clk_tree_defs.h> // For APB_CLK_FREQ
#endif
#include <string.h>
static const char *TAG = "i2cdev_legacy";
typedef struct
{
SemaphoreHandle_t lock;
i2c_config_t config; // Use legacy config struct
bool installed;
uint32_t ref_count;
i2c_dev_t *devices[CONFIG_I2CDEV_MAX_DEVICES_PER_PORT]; // Track devices registered on this port
} i2c_port_state_t;
static i2c_port_state_t states[I2C_NUM_MAX] = { 0 };
#if CONFIG_I2CDEV_NOLOCK
#define SEMAPHORE_TAKE(port)
#else
#define SEMAPHORE_TAKE(port) \
do \
{ \
if (!xSemaphoreTake(states[port].lock, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT))) \
{ \
ESP_LOGE(TAG, "Could not take port mutex %d", port); \
return ESP_ERR_TIMEOUT; \
} \
} \
while (0)
#endif
#if CONFIG_I2CDEV_NOLOCK
#define SEMAPHORE_GIVE(port)
#else
#define SEMAPHORE_GIVE(port) \
do \
{ \
if (!xSemaphoreGive(states[port].lock)) \
{ \
ESP_LOGE(TAG, "Could not give port mutex %d", port); \
return ESP_FAIL; \
} \
} \
while (0)
#endif
/**
* @brief Register an I2C device for tracking and resource management
*
* This function adds a device to the port's tracking array, which helps with:
* - Monitoring which devices are active on each port
* - Proper cleanup when the system shuts down
* - Diagnostics and debugging
*
* Each port can track up to CONFIG_I2CDEV_MAX_DEVICES_PER_PORT devices.
*
* @param dev Device descriptor to register
* @return ESP_OK if registration succeeded, or an error code
*/
static esp_err_t register_device(i2c_dev_t *dev)
{
if (!dev || dev->port >= I2C_NUM_MAX)
return ESP_ERR_INVALID_ARG;
if (!states[dev->port].lock)
return ESP_ERR_INVALID_STATE;
esp_err_t ret = ESP_ERR_NO_MEM;
// Take the mutex directly instead of using the macro
if (xSemaphoreTake(states[dev->port].lock, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT)) != pdTRUE)
{
ESP_LOGE(TAG, "[0x%02x at %d] Could not take port mutex for registration", dev->addr, dev->port);
return ESP_ERR_TIMEOUT;
}
// Search for an empty slot in the device tracking array
for (int i = 0; i < CONFIG_I2CDEV_MAX_DEVICES_PER_PORT; i++)
{
if (states[dev->port].devices[i] == NULL)
{
// Found empty slot - register the device here
states[dev->port].devices[i] = dev;
ESP_LOGV(TAG, "[0x%02x at %d] Registered device in slot %d", dev->addr, dev->port, i);
ret = ESP_OK;
break;
}
}
// All slots full - this will still allow communication but prevents automatic cleanup
if (ret != ESP_OK)
{
ESP_LOGW(TAG, "[0x%02x at %d] No free slots to register device", dev->addr, dev->port);
}
// Release the mutex
if (!xSemaphoreGive(states[dev->port].lock))
{
ESP_LOGE(TAG, "[0x%02x at %d] Could not give port mutex after registration", dev->addr, dev->port);
// If can't give the mutex, that's a serious error that overrides the registration result
return ESP_FAIL;
}
return ret;
}
/**
* @brief Deregister a device and update reference counting
*
* This function:
* 1. Removes the device from the port's tracking array
* 2. Decrements the port's reference count
* 3. Cleans up the I2C driver if this was the last device on the port
*
* This is called during device cleanup to ensure proper resource management.
*
* @param dev Device descriptor to deregister
*/
static void deregister_device(i2c_dev_t *dev)
{
if (!dev || dev->port >= I2C_NUM_MAX)
return;
// Don't use macros that return values since this is a void function
if (states[dev->port].lock)
{
if (xSemaphoreTake(states[dev->port].lock, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT)) != pdTRUE)
{
ESP_LOGE(TAG, "[0x%02x at %d] Could not take port mutex for deregistration", dev->addr, dev->port);
return; // Cannot proceed without lock
}
// Find the device in the tracking array
for (int i = 0; i < CONFIG_I2CDEV_MAX_DEVICES_PER_PORT; i++)
{
if (states[dev->port].devices[i] == dev)
{
// Clear this slot
states[dev->port].devices[i] = NULL;
ESP_LOGV(TAG, "[0x%02x at %d] Deregistered device from slot %d", dev->addr, dev->port, i);
break;
}
}
// Manage reference counting for this port
if (states[dev->port].ref_count > 0)
{
states[dev->port].ref_count--;
ESP_LOGD(TAG, "[Port %d] Decremented ref_count to %" PRIu32, dev->port, states[dev->port].ref_count);
// If this was the last device using this port, clean up the driver
if (states[dev->port].ref_count == 0 && states[dev->port].installed)
{
ESP_LOGI(TAG, "[Port %d] Last device removed, uninstalling driver", dev->port);
i2c_driver_delete(dev->port);
states[dev->port].installed = false;
}
}
// Release the mutex
if (!xSemaphoreGive(states[dev->port].lock))
{
ESP_LOGE(TAG, "[Port %d] Could not give port mutex after deregistration", dev->port);
// Can't do much about this error except log it
}
}
}
esp_err_t i2cdev_init()
{
memset(states, 0, sizeof(states));
#if !CONFIG_I2CDEV_NOLOCK
for (int i = 0; i < I2C_NUM_MAX; i++)
{
states[i].lock = xSemaphoreCreateMutex();
if (!states[i].lock)
{
ESP_LOGE(TAG, "Could not create port mutex %d", i);
return ESP_FAIL;
}
}
#endif
return ESP_OK;
}
esp_err_t i2cdev_done()
{
ESP_LOGV(TAG, "Cleaning up I2C subsystem (legacy)...");
for (int i = 0; i < I2C_NUM_MAX; i++)
{
if (!states[i].lock)
continue;
if (states[i].installed)
{
SEMAPHORE_TAKE(i);
// First, clean up any devices still registered on this port
for (int j = 0; j < CONFIG_I2CDEV_MAX_DEVICES_PER_PORT; j++)
{
if (states[i].devices[j] != NULL)
{
i2c_dev_t *dev = states[i].devices[j];
ESP_LOGW(TAG, "[Port %d] Device 0x%02x still registered during cleanup", i, dev->addr);
states[i].devices[j] = NULL;
}
}
i2c_driver_delete(i);
states[i].installed = false;
states[i].ref_count = 0;
SEMAPHORE_GIVE(i);
}
#if !CONFIG_I2CDEV_NOLOCK
vSemaphoreDelete(states[i].lock);
#endif
states[i].lock = NULL;
}
ESP_LOGV(TAG, "I2C subsystem cleanup finished (legacy).");
return ESP_OK;
}
esp_err_t i2c_dev_create_mutex(i2c_dev_t *dev)
{
#if !CONFIG_I2CDEV_NOLOCK
if (!dev)
return ESP_ERR_INVALID_ARG;
ESP_LOGV(TAG, "[0x%02x at %d] Creating device mutex", dev->addr, dev->port);
// Initialize device pins to -1 to ensure consistent pattern with new driver
if (dev->sda_pin == 0 && dev->scl_pin == 0)
{
dev->sda_pin = -1;
dev->scl_pin = -1;
ESP_LOGD(TAG, "[0x%02x at %d] Initialized pins to -1", dev->addr, dev->port);
}
if (dev->mutex)
{
ESP_LOGW(TAG, "[0x%02x at %d] Device mutex already exists", dev->addr, dev->port);
return ESP_OK; // Already created
}
dev->mutex = xSemaphoreCreateMutex();
if (!dev->mutex)
{
ESP_LOGE(TAG, "[0x%02x at %d] Could not create device mutex", dev->addr, dev->port);
return ESP_FAIL;
}
// Register device for tracking
esp_err_t reg_res = register_device(dev);
if (reg_res != ESP_OK)
{
ESP_LOGW(TAG, "[0x%02x at %d] Could not register device: %s", dev->addr, dev->port, esp_err_to_name(reg_res));
// Continue anyway since this is not critical
}
#endif
return ESP_OK;
}
esp_err_t i2c_dev_delete_mutex(i2c_dev_t *dev)
{
#if !CONFIG_I2CDEV_NOLOCK
if (!dev)
return ESP_ERR_INVALID_ARG;
ESP_LOGV(TAG, "[0x%02x at %d] Deleting device mutex and cleaning up", dev->addr, dev->port);
// Deregister and update ref counts
deregister_device(dev);
// Delete mutex if exists
if (dev->mutex)
{
vSemaphoreDelete(dev->mutex);
dev->mutex = NULL;
}
else
{
ESP_LOGV(TAG, "[0x%02x at %d] Device mutex was NULL", dev->addr, dev->port);
}
#endif
return ESP_OK;
}
esp_err_t i2c_dev_take_mutex(i2c_dev_t *dev)
{
#if !CONFIG_I2CDEV_NOLOCK
if (!dev)
return ESP_ERR_INVALID_ARG;
ESP_LOGV(TAG, "[0x%02x at %d] Taking mutex", dev->addr, dev->port);
if (!dev->mutex)
{
ESP_LOGE(TAG, "[0x%02x at %d] Attempt to take NULL mutex!", dev->addr, dev->port);
return ESP_ERR_INVALID_STATE;
}
if (!xSemaphoreTake(dev->mutex, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT)))
{
ESP_LOGE(TAG, "[0x%02x at %d] Could not take device mutex (timeout %d ms)", dev->addr, dev->port, CONFIG_I2CDEV_TIMEOUT);
return ESP_ERR_TIMEOUT;
}
#endif
return ESP_OK;
}
esp_err_t i2c_dev_give_mutex(i2c_dev_t *dev)
{
#if !CONFIG_I2CDEV_NOLOCK
if (!dev)
return ESP_ERR_INVALID_ARG;
ESP_LOGV(TAG, "[0x%02x at %d] Giving mutex", dev->addr, dev->port);
if (!dev->mutex)
{
ESP_LOGE(TAG, "[0x%02x at %d] Attempt to give NULL mutex!", dev->addr, dev->port);
return ESP_ERR_INVALID_STATE;
}
if (!xSemaphoreGive(dev->mutex))
{
ESP_LOGE(TAG, "[0x%02x at %d] Could not give device mutex", dev->addr, dev->port);
return ESP_FAIL;
}
#endif
return ESP_OK;
}
inline static bool cfg_equal(const i2c_config_t *a, const i2c_config_t *b)
{
bool clock_equal;
#ifdef CONFIG_IDF_TARGET_ESP8266
clock_equal = (a->clk_stretch_tick == b->clk_stretch_tick);
#else
clock_equal = (a->master.clk_speed == b->master.clk_speed);
#endif
return a->mode == b->mode && a->scl_io_num == b->scl_io_num && a->sda_io_num == b->sda_io_num && a->scl_pullup_en == b->scl_pullup_en && a->sda_pullup_en == b->sda_pullup_en && clock_equal;
// Note: Ignoring clk_flags for comparison as it might not be consistently set by users
}
/**
* @brief Configure and initialize the I2C port for a device
*
* This function is responsible for:
* 1. Determining which pins to use (from device or config)
* 2. Validating pin configuration
* 3. Installing/configuring the I2C driver if not already done
* 4. Managing reference counting for the port
* 5. Setting up clock stretching timeout
*
* This is a critical function that must succeed before any I2C operations
* can be performed with a device.
*
* @param dev Device descriptor with configuration info
* @return ESP_OK on success, or an error code on failure
*/
static esp_err_t i2c_setup_port(i2c_dev_t *dev)
{
if (!dev)
{
ESP_LOGE(TAG, "Device is NULL");
return ESP_ERR_INVALID_ARG;
}
if (dev->port >= I2C_NUM_MAX)
{
ESP_LOGE(TAG, "Invalid I2C port number: %d", dev->port);
return ESP_ERR_INVALID_ARG;
}
// Pin Selection Logic:
// Pins are taken from dev->cfg.xyz_io_num.
// If -1, Kconfig defaults are used.
gpio_num_t sda_pin; // Effective SDA pin to be used
gpio_num_t scl_pin; // Effective SCL pin to be used
if (dev->cfg.sda_io_num == (gpio_num_t) -1)
{
sda_pin = (gpio_num_t)CONFIG_I2CDEV_DEFAULT_SDA_PIN;
}
else
{
sda_pin = dev->cfg.sda_io_num;
}
if (dev->cfg.scl_io_num == (gpio_num_t) -1)
{
scl_pin = (gpio_num_t)CONFIG_I2CDEV_DEFAULT_SCL_PIN;
}
else
{
scl_pin = dev->cfg.scl_io_num;
}
ESP_LOGD(TAG, "[0x%02x at %d] Based on cfg: sda_cfg=%d, scl_cfg=%d. Effective pins for setup: SDA=%d, SCL=%d", dev->addr, dev->port, dev->cfg.sda_io_num, dev->cfg.scl_io_num, sda_pin, scl_pin);
// Perform basic validation of effective pins
if (sda_pin < 0 || scl_pin < 0)
{
ESP_LOGE(TAG, "[0x%02x at %d] Invalid effective SDA/SCL pins (%d, %d). Check Kconfig defaults if cfg pins were -1.", dev->addr, dev->port, sda_pin, scl_pin);
return ESP_ERR_INVALID_ARG;
}
if (sda_pin == scl_pin)
{
ESP_LOGE(TAG, "[0x%02x at %d] Effective SDA and SCL pins cannot be the same (%d).", dev->addr, dev->port, sda_pin);
return ESP_ERR_INVALID_ARG;
}
// Initialize common fields
i2c_config_t legacy_cfg = { .mode = I2C_MODE_MASTER,
.sda_io_num = sda_pin, // Use locally determined pins
.scl_io_num = scl_pin, // Use locally determined pins
.sda_pullup_en = dev->cfg.sda_pullup_en ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE,
.scl_pullup_en = dev->cfg.scl_pullup_en ? GPIO_PULLUP_ENABLE : GPIO_PULLUP_DISABLE
};
#ifdef CONFIG_IDF_TARGET_ESP8266
// ESP8266 uses clk_stretch_tick instead of master.clk_speed
// Clock speed will be handled during driver installation
uint32_t desired_speed = dev->cfg.master.clk_speed > 0 ? dev->cfg.master.clk_speed : 400000;
ESP_LOGD(TAG, "Final I2C config for port %d: SDA=%d, SCL=%d, speed=%lu (ESP8266)", dev->port, legacy_cfg.sda_io_num, legacy_cfg.scl_io_num, (unsigned long)desired_speed);
#else
// ESP32 family uses master.clk_speed
legacy_cfg.master.clk_speed = dev->cfg.master.clk_speed > 0 ? dev->cfg.master.clk_speed : 400000;
ESP_LOGD(TAG, "Final I2C config for port %d: SDA=%d, SCL=%d, speed=%lu", dev->port, legacy_cfg.sda_io_num, legacy_cfg.scl_io_num, (unsigned long)legacy_cfg.master.clk_speed);
#endif
#ifdef CONFIG_IDF_TARGET_ESP32
legacy_cfg.clk_flags = 0;
#endif
esp_err_t err = ESP_OK;
// Part 1: Driver Installation / Reconfiguration
if (!cfg_equal(&legacy_cfg, &states[dev->port].config) || !states[dev->port].installed)
{
ESP_LOGD(TAG, "[0x%02x at %d] Reconfiguring I2C driver", dev->addr, dev->port);
if (states[dev->port].installed)
{
ESP_LOGD(TAG, "Uninstalling previous I2C driver configuration for port %d", dev->port);
i2c_driver_delete(dev->port);
states[dev->port].installed = false;
states[dev->port].ref_count = 0;
}
vTaskDelay(1);
// Target-specific driver installation/configuration sequence
#if HELPER_TARGET_IS_ESP32
#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 0)
ESP_LOGD(TAG, "Using IDF >= 5.1.0 driver install order for ESP32 family");
err = i2c_driver_install(dev->port, legacy_cfg.mode, 0, 0, 0);
if (err == ESP_OK)
{
err = i2c_param_config(dev->port, &legacy_cfg);
}
#else
ESP_LOGD(TAG, "Using IDF < 5.1.0 driver install order for ESP32 family");
err = i2c_param_config(dev->port, &legacy_cfg);
if (err == ESP_OK)
{
err = i2c_driver_install(dev->port, legacy_cfg.mode, 0, 0, 0);
}
#endif
#elif HELPER_TARGET_IS_ESP8266
ESP_LOGD(TAG, "Using ESP8266 specific driver installation");
legacy_cfg.clk_stretch_tick = dev->timeout_ticks ? dev->timeout_ticks : I2CDEV_MAX_STRETCH_TIME;
err = i2c_driver_install(dev->port, legacy_cfg.mode);
if (err == ESP_OK)
{
err = i2c_param_config(dev->port, &legacy_cfg);
}
// ESP8266 note: Clock speed is not directly configurable through i2c_config_t
// The desired speed was: %lu Hz", desired_speed
#else
// If legacy mode is off, and target detection fails, this avoids a compile error.
// The legacy driver just won't support any target in this case.
ESP_LOGW(TAG, "i2cdev_legacy.c: No specific target (ESP32/ESP32-S2/ESP32-S3/ESP32-C3/ESP32-C6/ESP8266) detected "
"for driver installation. Legacy driver might be inactive or misconfigured.");
err = ESP_ERR_NOT_SUPPORTED; // Indicate that setup can't proceed.
#endif
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to install/configure I2C driver for port %d: %d (%s)", dev->port, err, esp_err_to_name(err));
states[dev->port].installed = false; // Ensure state reflects failure
return err;
}
memcpy(&states[dev->port].config, &legacy_cfg, sizeof(i2c_config_t));
states[dev->port].installed = true;
states[dev->port].ref_count++;
dev->sda_pin = legacy_cfg.sda_io_num;
dev->scl_pin = legacy_cfg.scl_io_num;
ESP_LOGD(TAG, "I2C driver successfully installed/reconfigured on port %d, ref_count=%" PRIu32, dev->port, states[dev->port].ref_count);
}
else
{
states[dev->port].ref_count++;
ESP_LOGV(TAG, "I2C driver already installed on port %d with matching config, ref_count=%" PRIu32, dev->port, states[dev->port].ref_count);
dev->sda_pin = states[dev->port].config.sda_io_num;
dev->scl_pin = states[dev->port].config.scl_io_num;
}
// Part 2: Timeout Configuration (ESP32 family specific hardware timeout)
#if HELPER_TARGET_IS_ESP32
int current_timeout_hw;
err = i2c_get_timeout(dev->port, ¤t_timeout_hw);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to get HW timeout for port %d: %d (%s)", dev->port, err, esp_err_to_name(err));
return err;
}
uint32_t timeout_ticks_val = dev->timeout_ticks ? dev->timeout_ticks : I2CDEV_MAX_STRETCH_TIME;
if (timeout_ticks_val != (uint32_t)current_timeout_hw)
{
ESP_LOGV(TAG, "Port %d: Updating HW timeout from %d to %" PRIu32 " ticks", dev->port, current_timeout_hw, timeout_ticks_val);
err = i2c_set_timeout(dev->port, timeout_ticks_val);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to set HW timeout for port %d: %d (%s)", dev->port, err, esp_err_to_name(err));
return err;
}
ESP_LOGD(TAG, "HW Timeout: ticks = %" PRIu32 " (%" PRIu32 " usec) on port %d", timeout_ticks_val, timeout_ticks_val / 80, dev->port);
}
#endif
return ESP_OK;
}
esp_err_t i2c_dev_probe(const i2c_dev_t *dev, i2c_dev_type_t operation_type)
{
if (!dev)
return ESP_ERR_INVALID_ARG;
SEMAPHORE_TAKE(dev->port);
esp_err_t res = i2c_setup_port((i2c_dev_t *)dev);
if (res == ESP_OK)
{
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, dev->addr << 1 | (operation_type == I2C_DEV_READ ? 1 : 0), true);
// Alternative Write-style probe for better device compatibility
// many devices don't respond well to blind read probes.
// i2c_master_write_byte(cmd, dev->addr << 1 | 0, true); // Force write bit (0)
i2c_master_stop(cmd);
res = i2c_master_cmd_begin(dev->port, cmd, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT));
i2c_cmd_link_delete(cmd);
}
SEMAPHORE_GIVE(dev->port);
return res;
}
esp_err_t i2c_dev_read(const i2c_dev_t *dev, const void *out_data, size_t out_size, void *in_data, size_t in_size)
{
if (!dev || !in_data || !in_size)
return ESP_ERR_INVALID_ARG;
SEMAPHORE_TAKE(dev->port);
// Use a local status variable to track errors
esp_err_t err = i2c_setup_port((i2c_dev_t *)dev);
if (err == ESP_OK)
{
// Only create a command handle if setup was successful
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
if (out_data && out_size)
{
// Write phase - typically used to specify a register address
i2c_master_start(cmd);
i2c_master_write_byte(cmd, dev->addr << 1, true); // Addr + Write bit (0)
i2c_master_write(cmd, (void *)out_data, out_size, true);
}
// Read phase - get data from the device
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->addr << 1) | 1, true); // Addr + Read bit (1)
i2c_master_read(cmd, in_data, in_size,
I2C_MASTER_LAST_NACK); // NACK the last byte to signal end
i2c_master_stop(cmd);
// Execute the command
err = i2c_master_cmd_begin(dev->port, cmd, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "i2c_master_cmd_begin failed for read: %d (%s)", err, esp_err_to_name(err));
}
// Always delete the command handle
i2c_cmd_link_delete(cmd);
}
// Always release the semaphore before returning
SEMAPHORE_GIVE(dev->port);
return err;
}
esp_err_t i2c_dev_write(const i2c_dev_t *dev, const void *out_reg, size_t out_reg_size, const void *out_data, size_t out_size)
{
if (!dev)
return ESP_ERR_INVALID_ARG;
if ((!out_reg || !out_reg_size) && (!out_data || !out_size))
return ESP_ERR_INVALID_ARG;
SEMAPHORE_TAKE(dev->port);
// Use a local status variable to track errors
esp_err_t err = i2c_setup_port((i2c_dev_t *)dev);
if (err == ESP_OK)
{
// Only create a command handle if setup was successful
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, dev->addr << 1, true);
// Write register address/command if provided
if (out_reg && out_reg_size)
{
i2c_master_write(cmd, (void *)out_reg, out_reg_size, true);
}
// Write data if provided
if (out_data && out_size)
{
i2c_master_write(cmd, (void *)out_data, out_size, true);
}
i2c_master_stop(cmd);
// Execute the command
err = i2c_master_cmd_begin(dev->port, cmd, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "i2c_master_cmd_begin failed for write: %d (%s)", err, esp_err_to_name(err));
}
// Always delete the command handle
i2c_cmd_link_delete(cmd);
}
// Always release the semaphore before returning
SEMAPHORE_GIVE(dev->port);
return err;
}
esp_err_t i2c_dev_write_reg(const i2c_dev_t *dev, uint8_t reg, const void *out_data, size_t out_size)
{
if (!dev || !out_data || !out_size)
return ESP_ERR_INVALID_ARG;
SEMAPHORE_TAKE(dev->port);
// Use a local status variable to track errors
esp_err_t err = i2c_setup_port((i2c_dev_t *)dev);
if (err == ESP_OK)
{
// Only create a command handle if setup was successful
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->addr << 1) | I2C_MASTER_WRITE, true); // Addr + Write bit
i2c_master_write_byte(cmd, reg, true); // Register address
if (out_data && out_size)
{
i2c_master_write(cmd, (void *)out_data, out_size, true); // Data to write
}
i2c_master_stop(cmd);
// Execute the command
err = i2c_master_cmd_begin(dev->port, cmd, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "i2c_master_cmd_begin failed for write_reg: %d (%s)", err, esp_err_to_name(err));
}
// Always delete the command handle
i2c_cmd_link_delete(cmd);
}
// Always release the semaphore before returning
SEMAPHORE_GIVE(dev->port);
return err;
}
esp_err_t i2c_dev_read_reg(const i2c_dev_t *dev, uint8_t reg, void *in_data, size_t in_size)
{
if (!dev || !in_data || !in_size)
return ESP_ERR_INVALID_ARG;
SEMAPHORE_TAKE(dev->port);
// Use a local status variable to track errors
esp_err_t err = i2c_setup_port((i2c_dev_t *)dev);
if (err == ESP_OK)
{
// Only create a command handle if setup was successful
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, reg, true);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev->addr << 1) | I2C_MASTER_READ, true);
i2c_master_read(cmd, in_data, in_size, I2C_MASTER_LAST_NACK);
i2c_master_stop(cmd);
// Execute the command
err = i2c_master_cmd_begin(dev->port, cmd, pdMS_TO_TICKS(CONFIG_I2CDEV_TIMEOUT));
if (err != ESP_OK)
{
ESP_LOGE(TAG, "i2c_master_cmd_begin failed for read_reg: %d (%s)", err, esp_err_to_name(err));
}
// Always delete the command handle
i2c_cmd_link_delete(cmd);
}
// Always release the semaphore before returning
SEMAPHORE_GIVE(dev->port);
return err;
}
// Implementation of i2c_dev_check_present (updated version of i2c_dev_probe) using legacy I2C driver
esp_err_t i2c_dev_check_present(const i2c_dev_t *dev)
{
if (!dev)
return ESP_ERR_INVALID_ARG;
ESP_LOGV(TAG, "[0x%02x at %d] Checking device presence (legacy driver)...", dev->addr, dev->port);
// Use the exact same pattern as i2c_dev_probe with WRITE operation to ensure consistent behavior
return i2c_dev_probe(dev, I2C_DEV_WRITE);
}