Skip to content

Commit 72358cc

Browse files
committed
Merge branch 'feat/modify_i2c_bus_null_mem_addr' into 'master'
feat(i2c_bus): support removing the restrictions on register addresses Closes AEG-2238 See merge request ae_group/esp-iot-solution!1248
2 parents 1e5c05b + c726ec2 commit 72358cc

File tree

6 files changed

+85
-1
lines changed

6 files changed

+85
-1
lines changed

components/i2c_bus/CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# ChangeLog
22

3+
## v1.4.0 - 2025-3-13
4+
5+
### Enhancements:
6+
7+
- Support removing the restriction on ``NULL_I2C_MEM_ADDR``, allowing users to refer to all eligible register addresses.
8+
39
## v1.3.0 - 2025-2-13
410

511
### Enhancements:

components/i2c_bus/Kconfig

+7
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ menu "Bus Options"
4141
help
4242
Set the maximum number of software I2C ports that can be used. This option is only applicable when
4343
software I2C support is enabled.
44+
45+
config I2C_BUS_REMOVE_NULL_MEM_ADDR
46+
bool "Remove the limitation of NULL_MEM_ADDR, any register address will be sent"
47+
default n
48+
help
49+
Enable this option to disable NULL_MEM_ADDR. This allows any register address to be sent.
50+
4451
endmenu
4552

4653
endmenu

components/i2c_bus/i2c_bus.c

+24
Original file line numberDiff line numberDiff line change
@@ -365,11 +365,17 @@ static esp_err_t i2c_bus_read_reg8(i2c_bus_device_handle_t dev_handle, uint8_t m
365365
#endif
366366
{
367367
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
368+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
368369
if (mem_address != NULL_I2C_MEM_ADDR) {
370+
#endif
369371
i2c_master_start(cmd);
370372
i2c_master_write_byte(cmd, (i2c_device->dev_addr << 1) | I2C_MASTER_WRITE, I2C_ACK_CHECK_EN);
371373
i2c_master_write_byte(cmd, mem_address, I2C_ACK_CHECK_EN);
374+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
375+
} else {
376+
ESP_LOGD(TAG, "register address 0x%X is skipped and will not be sent", NULL_I2C_MEM_ADDR);
372377
}
378+
#endif
373379

374380
i2c_master_start(cmd);
375381
i2c_master_write_byte(cmd, (i2c_device->dev_addr << 1) | I2C_MASTER_READ, I2C_ACK_CHECK_EN);
@@ -402,11 +408,17 @@ esp_err_t i2c_bus_read_reg16(i2c_bus_device_handle_t dev_handle, uint16_t mem_ad
402408
#endif
403409
{
404410
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
411+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
405412
if (mem_address != NULL_I2C_MEM_16BIT_ADDR) {
413+
#endif
406414
i2c_master_start(cmd);
407415
i2c_master_write_byte(cmd, (i2c_device->dev_addr << 1) | I2C_MASTER_WRITE, I2C_ACK_CHECK_EN);
408416
i2c_master_write(cmd, memAddress8, 2, I2C_ACK_CHECK_EN);
417+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
418+
} else {
419+
ESP_LOGD(TAG, "register address 0x%X is skipped and will not be sent", NULL_I2C_MEM_16BIT_ADDR);
409420
}
421+
#endif
410422
i2c_master_start(cmd);
411423
i2c_master_write_byte(cmd, (i2c_device->dev_addr << 1) | I2C_MASTER_READ, I2C_ACK_CHECK_EN);
412424
i2c_master_read(cmd, data, data_len, I2C_MASTER_LAST_NACK);
@@ -437,9 +449,15 @@ static esp_err_t i2c_bus_write_reg8(i2c_bus_device_handle_t dev_handle, uint8_t
437449
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
438450
i2c_master_start(cmd);
439451
i2c_master_write_byte(cmd, (i2c_device->dev_addr << 1) | I2C_MASTER_WRITE, I2C_ACK_CHECK_EN);
452+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
440453
if (mem_address != NULL_I2C_MEM_ADDR) {
454+
#endif
441455
i2c_master_write_byte(cmd, mem_address, I2C_ACK_CHECK_EN);
456+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
457+
} else {
458+
ESP_LOGD(TAG, "register address 0x%X is skipped and will not be sent", NULL_I2C_MEM_ADDR);
442459
}
460+
#endif
443461
i2c_master_write(cmd, (uint8_t *)data, data_len, I2C_ACK_CHECK_EN);
444462
i2c_master_stop(cmd);
445463
ret = i2c_master_cmd_begin_with_conf(i2c_device->i2c_bus->i2c_port, cmd, I2C_BUS_TICKS_TO_WAIT, &i2c_device->conf);
@@ -472,9 +490,15 @@ esp_err_t i2c_bus_write_reg16(i2c_bus_device_handle_t dev_handle, uint16_t mem_a
472490
i2c_master_start(cmd);
473491
i2c_master_write_byte(cmd, (i2c_device->dev_addr << 1) | I2C_MASTER_WRITE, I2C_ACK_CHECK_EN);
474492

493+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
475494
if (mem_address != NULL_I2C_MEM_16BIT_ADDR) {
495+
#endif
476496
i2c_master_write(cmd, memAddress8, 2, I2C_ACK_CHECK_EN);
497+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
498+
} else {
499+
ESP_LOGD(TAG, "register address 0x%X is skipped and will not be sent", NULL_I2C_MEM_16BIT_ADDR);
477500
}
501+
#endif
478502

479503
i2c_master_write(cmd, (uint8_t *)data, data_len, I2C_ACK_CHECK_EN);
480504
i2c_master_stop(cmd);

components/i2c_bus/i2c_bus_v2.c

+20
Original file line numberDiff line numberDiff line change
@@ -360,11 +360,16 @@ static esp_err_t i2c_bus_read_reg8(i2c_bus_device_handle_t dev_handle, uint8_t m
360360
} else
361361
#endif
362362
{
363+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
363364
if (mem_address != NULL_I2C_MEM_ADDR) {
365+
#endif
364366
ret = i2c_master_transmit_receive(i2c_device->dev_handle, &mem_address, 1, data, data_len, I2C_BUS_TICKS_TO_WAIT);
367+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
365368
} else {
369+
ESP_LOGD(TAG, "register address 0x%X is skipped and will not be sent", NULL_I2C_MEM_ADDR);
366370
ret = i2c_master_receive(i2c_device->dev_handle, data, data_len, I2C_BUS_TICKS_TO_WAIT);
367371
}
372+
#endif
368373
}
369374
I2C_BUS_MUTEX_GIVE(i2c_device->i2c_bus->mutex, ESP_FAIL);
370375
return ret;
@@ -390,11 +395,16 @@ esp_err_t i2c_bus_read_reg16(i2c_bus_device_handle_t dev_handle, uint16_t mem_ad
390395
} else
391396
#endif
392397
{
398+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
393399
if (mem_address != NULL_I2C_MEM_16BIT_ADDR) {
400+
#endif
394401
ret = i2c_master_transmit_receive(i2c_device->dev_handle, memAddress8, 2, data, data_len, I2C_BUS_TICKS_TO_WAIT);
402+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
395403
} else {
404+
ESP_LOGD(TAG, "register address 0x%X is skipped and will not be sent", NULL_I2C_MEM_16BIT_ADDR);
396405
ret = i2c_master_receive(i2c_device->dev_handle, data, data_len, I2C_BUS_TICKS_TO_WAIT);
397406
}
407+
#endif
398408
}
399409
I2C_BUS_MUTEX_GIVE(i2c_device->i2c_bus->mutex, ESP_FAIL);
400410
return ret;
@@ -417,7 +427,9 @@ static esp_err_t i2c_bus_write_reg8(i2c_bus_device_handle_t dev_handle, uint8_t
417427
} else
418428
#endif
419429
{
430+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
420431
if (mem_address != NULL_I2C_MEM_ADDR) {
432+
#endif
421433
uint8_t *data_addr = malloc(data_len + 1);
422434
if (data_addr == NULL) {
423435
ESP_LOGE(TAG, "data_addr memory alloc fail");
@@ -430,9 +442,12 @@ static esp_err_t i2c_bus_write_reg8(i2c_bus_device_handle_t dev_handle, uint8_t
430442
}
431443
ret = i2c_master_transmit(i2c_device->dev_handle, data_addr, data_len + 1, I2C_BUS_TICKS_TO_WAIT);
432444
free(data_addr);
445+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
433446
} else {
447+
ESP_LOGD(TAG, "register address 0x%X is skipped and will not be sent", NULL_I2C_MEM_ADDR);
434448
ret = i2c_master_transmit(i2c_device->dev_handle, data, data_len, I2C_BUS_TICKS_TO_WAIT);
435449
}
450+
#endif
436451
}
437452
I2C_BUS_MUTEX_GIVE(i2c_device->i2c_bus->mutex, ESP_FAIL);
438453
return ret;
@@ -457,7 +472,9 @@ esp_err_t i2c_bus_write_reg16(i2c_bus_device_handle_t dev_handle, uint16_t mem_a
457472
} else
458473
#endif
459474
{
475+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
460476
if (mem_address != NULL_I2C_MEM_16BIT_ADDR) {
477+
#endif
461478
uint8_t *data_addr = malloc(data_len + 2);
462479
if (data_addr == NULL) {
463480
ESP_LOGE(TAG, "data_addr memory alloc fail");
@@ -471,9 +488,12 @@ esp_err_t i2c_bus_write_reg16(i2c_bus_device_handle_t dev_handle, uint16_t mem_a
471488
}
472489
ret = i2c_master_transmit(i2c_device->dev_handle, data_addr, data_len + 2, I2C_BUS_TICKS_TO_WAIT);
473490
free(data_addr);
491+
#if !CONFIG_I2C_BUS_REMOVE_NULL_MEM_ADDR
474492
} else {
493+
ESP_LOGD(TAG, "register address 0x%X is skipped and will not be sent", NULL_I2C_MEM_16BIT_ADDR);
475494
ret = i2c_master_transmit(i2c_device->dev_handle, data, data_len, I2C_BUS_TICKS_TO_WAIT);
476495
}
496+
#endif
477497
}
478498
I2C_BUS_MUTEX_GIVE(i2c_device->i2c_bus->mutex, ESP_FAIL);
479499
return ret;

components/i2c_bus/idf_component.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
version: "1.3.0"
1+
version: "1.4.0"
22
description: The I2C Bus Driver supports both hardware and software I2C.
33
url: https://github.com/espressif/esp-iot-solution/tree/master/components/i2c_bus
44
repository: https://github.com/espressif/esp-iot-solution.git

components/i2c_bus/test_apps/main/test_i2c_bus.c

+27
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,33 @@ TEST_CASE("I2C bus scan test", "[i2c_bus][scan]")
431431
TEST_ASSERT(i2c_bus == NULL);
432432
}
433433

434+
TEST_CASE("I2C bus register address restriction test", "[i2c_bus][NULL_I2C_MEM_ADDR]")
435+
{
436+
uint8_t *data_wr = (uint8_t *) malloc(RW_TEST_LENGTH);
437+
for (int i = 0; i < RW_TEST_LENGTH; i++) {
438+
data_wr[i] = i;
439+
}
440+
441+
i2c_config_t conf = {
442+
.mode = I2C_MODE_MASTER,
443+
.sda_io_num = I2C_MASTER_SDA_IO,
444+
.sda_pullup_en = GPIO_PULLUP_ENABLE,
445+
.scl_io_num = I2C_MASTER_SCL_IO,
446+
.scl_pullup_en = GPIO_PULLUP_ENABLE,
447+
.master.clk_speed = I2C_MASTER_FREQ_HZ,
448+
};
449+
i2c_bus_handle_t i2c_bus = i2c_bus_create(I2C_NUM_0, &conf);
450+
TEST_ASSERT(i2c_bus != NULL);
451+
i2c_bus_device_handle_t i2c_device1 = i2c_bus_device_create(i2c_bus, 0x01, I2C_MASTER_FREQ_HZ);
452+
TEST_ASSERT(i2c_device1 != NULL);
453+
i2c_bus_write_bytes(i2c_device1, NULL_I2C_MEM_ADDR, RW_TEST_LENGTH, data_wr);
454+
i2c_bus_device_delete(&i2c_device1);
455+
TEST_ASSERT(i2c_device1 == NULL);
456+
TEST_ASSERT(ESP_OK == i2c_bus_delete(&i2c_bus));
457+
free(data_wr);
458+
TEST_ASSERT(i2c_bus == NULL);
459+
}
460+
434461
#if CONFIG_I2C_BUS_SUPPORT_SOFTWARE
435462

436463
TEST_CASE("I2C soft bus init-deinit test", "[soft][bus][i2c_bus]")

0 commit comments

Comments
 (0)