Skip to content

Commit 660244e

Browse files
mythbuster5espressif-bot
authored andcommitted
refactor(i2c): Replace i2c test from version one to version two
1 parent 1e516f0 commit 660244e

File tree

15 files changed

+663
-1190
lines changed

15 files changed

+663
-1190
lines changed

components/esp_driver_i2c/test_apps/i2c_test_apps/main/CMakeLists.txt

Lines changed: 21 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,35 +2,27 @@ set(srcs "test_app_main.c"
22
"test_i2c_common.c"
33
)
44

5-
if(CONFIG_SOC_I2C_SUPPORT_SLAVE)
6-
if(CONFIG_I2C_ENABLE_SLAVE_DRIVER_VERSION_2)
7-
list(APPEND srcs "test_i2c_slave_v2.c")
8-
else()
9-
list(APPEND srcs "test_i2c_multi.c")
10-
11-
if(CONFIG_SOC_I2C_SLAVE_SUPPORT_BROADCAST)
12-
list(APPEND srcs "test_i2c_broadcast.c")
13-
endif()
14-
15-
if(CONFIG_SOC_I2C_SLAVE_SUPPORT_I2CRAM_ACCESS)
16-
list(APPEND srcs "test_i2c_ram.c")
17-
endif()
18-
19-
if(CONFIG_SOC_I2C_SUPPORT_10BIT_ADDR AND CONFIG_SOC_I2C_SUPPORT_SLAVE)
20-
list(APPEND srcs "test_i2c_10bit.c")
21-
endif()
22-
23-
if(CONFIG_SOC_LP_I2C_SUPPORTED)
24-
list(APPEND srcs "test_lp_i2c.c")
25-
endif()
26-
27-
if(CONFIG_SOC_I2C_SUPPORT_SLEEP_RETENTION)
28-
list(APPEND srcs "test_i2c_sleep_retention.c")
29-
endif()
30-
31-
if(CONFIG_I2C_ISR_IRAM_SAFE)
32-
list(APPEND srcs "test_i2c_iram.c")
33-
endif()
5+
if(CONFIG_SOC_I2C_SUPPORT_SLAVE AND CONFIG_SOC_I2C_SLAVE_CAN_GET_STRETCH_CAUSE)
6+
list(APPEND srcs "test_i2c_multi.c")
7+
8+
if(CONFIG_SOC_I2C_SLAVE_SUPPORT_BROADCAST)
9+
list(APPEND srcs "test_i2c_broadcast.c")
10+
endif()
11+
12+
if(CONFIG_SOC_I2C_SUPPORT_10BIT_ADDR)
13+
list(APPEND srcs "test_i2c_10bit.c")
14+
endif()
15+
16+
if(CONFIG_SOC_LP_I2C_SUPPORTED)
17+
list(APPEND srcs "test_lp_i2c.c")
18+
endif()
19+
20+
if(CONFIG_SOC_I2C_SUPPORT_SLEEP_RETENTION)
21+
list(APPEND srcs "test_i2c_sleep_retention.c")
22+
endif()
23+
24+
if(CONFIG_I2C_ISR_IRAM_SAFE)
25+
list(APPEND srcs "test_i2c_iram.c")
3426
endif()
3527
endif()
3628

components/esp_driver_i2c/test_apps/i2c_test_apps/main/test_board.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023-2024 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*/
@@ -42,6 +42,14 @@ extern "C" {
4242
#define TEST_I2C_PORT 0
4343
#define DATA_LENGTH 100
4444

45+
/**
46+
* @brief Slave test event
47+
*/
48+
typedef enum {
49+
I2C_SLAVE_EVT_RX,
50+
I2C_SLAVE_EVT_TX
51+
} i2c_slave_event_t;
52+
4553
/**
4654
* @brief Display buffer
4755
*
Lines changed: 104 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* SPDX-FileCopyrightText: 2023 Espressif Systems (Shanghai) CO LTD
2+
* SPDX-FileCopyrightText: 2023-2025 Espressif Systems (Shanghai) CO LTD
33
*
44
* SPDX-License-Identifier: Unlicense OR CC0-1.0
55
*/
@@ -22,14 +22,77 @@
2222
#include "test_utils.h"
2323
#include "test_board.h"
2424

25-
static QueueHandle_t s_receive_queue;
25+
static QueueHandle_t event_queue;
26+
static uint8_t *temp_data;
27+
static size_t temp_len = 0;
2628

27-
static IRAM_ATTR bool example_i2c_rx_done_callback(i2c_slave_dev_handle_t channel, const i2c_slave_rx_done_event_data_t *edata, void *user_data)
29+
static IRAM_ATTR bool i2c_slave_request_cb(i2c_slave_dev_handle_t i2c_slave, const i2c_slave_request_event_data_t *evt_data, void *arg)
2830
{
29-
BaseType_t high_task_wakeup = pdFALSE;
30-
QueueHandle_t receive_queue = (QueueHandle_t)user_data;
31-
xQueueSendFromISR(receive_queue, edata, &high_task_wakeup);
32-
return high_task_wakeup == pdTRUE;
31+
BaseType_t xTaskWoken;
32+
i2c_slave_event_t evt = I2C_SLAVE_EVT_TX;
33+
xQueueSendFromISR(event_queue, &evt, &xTaskWoken);
34+
return xTaskWoken;
35+
}
36+
37+
static IRAM_ATTR bool i2c_slave_receive_cb(i2c_slave_dev_handle_t i2c_slave, const i2c_slave_rx_done_event_data_t *evt_data, void *arg)
38+
{
39+
BaseType_t xTaskWoken;
40+
i2c_slave_event_t evt = I2C_SLAVE_EVT_RX;
41+
memcpy(temp_data, evt_data->buffer, evt_data->length);
42+
temp_len = evt_data->length;
43+
xQueueSendFromISR(event_queue, &evt, &xTaskWoken);
44+
return xTaskWoken;
45+
}
46+
47+
static void i2c_slave_read_test_10bit(void)
48+
{
49+
unity_wait_for_signal("i2c master init first");
50+
i2c_slave_dev_handle_t handle;
51+
event_queue = xQueueCreate(2, sizeof(i2c_slave_event_t));
52+
assert(event_queue);
53+
temp_data = heap_caps_malloc(DATA_LENGTH, MALLOC_CAP_DEFAULT);
54+
assert(temp_data);
55+
56+
i2c_slave_config_t i2c_slv_config = {
57+
.i2c_port = TEST_I2C_PORT,
58+
.clk_source = I2C_CLK_SRC_DEFAULT,
59+
.scl_io_num = I2C_SLAVE_SCL_IO,
60+
.sda_io_num = I2C_SLAVE_SDA_IO,
61+
.slave_addr = 0x134,
62+
.send_buf_depth = DATA_LENGTH,
63+
.receive_buf_depth = DATA_LENGTH,
64+
.flags.enable_internal_pullup = true,
65+
.addr_bit_len = I2C_ADDR_BIT_LEN_10,
66+
};
67+
68+
TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config, &handle));
69+
70+
i2c_slave_event_callbacks_t cbs = {
71+
.on_receive = i2c_slave_receive_cb,
72+
.on_request = i2c_slave_request_cb,
73+
};
74+
75+
TEST_ESP_OK(i2c_slave_register_event_callbacks(handle, &cbs, NULL));
76+
77+
unity_send_signal("i2c slave init finish");
78+
79+
unity_wait_for_signal("master write");
80+
81+
i2c_slave_event_t evt;
82+
if (xQueueReceive(event_queue, &evt, portMAX_DELAY) == pdTRUE) {
83+
if (evt == I2C_SLAVE_EVT_RX) {
84+
disp_buf(temp_data, temp_len);
85+
printf("length is %x\n", temp_len);
86+
for (int i = 0; i < temp_len; i++) {
87+
TEST_ASSERT(temp_data[i] == i);
88+
}
89+
}
90+
}
91+
92+
unity_send_signal("ready to delete");
93+
free(temp_data);
94+
vQueueDelete(event_queue);
95+
TEST_ESP_OK(i2c_del_slave_device(handle));
3396
}
3497

3598
static void i2c_master_write_test_10bit(void)
@@ -74,47 +137,6 @@ static void i2c_master_write_test_10bit(void)
74137
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
75138
}
76139

77-
static void i2c_slave_read_test_10bit(void)
78-
{
79-
unity_wait_for_signal("i2c master init first");
80-
uint8_t data_rd[DATA_LENGTH] = {0};
81-
82-
i2c_slave_config_t i2c_slv_config = {
83-
.addr_bit_len = I2C_ADDR_BIT_LEN_10,
84-
.clk_source = I2C_CLK_SRC_DEFAULT,
85-
.i2c_port = TEST_I2C_PORT,
86-
.send_buf_depth = 256,
87-
.scl_io_num = I2C_SLAVE_SCL_IO,
88-
.sda_io_num = I2C_SLAVE_SDA_IO,
89-
.slave_addr = 0x134,
90-
};
91-
92-
i2c_slave_dev_handle_t slave_handle;
93-
TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config, &slave_handle));
94-
95-
s_receive_queue = xQueueCreate(1, sizeof(i2c_slave_rx_done_event_data_t));
96-
i2c_slave_event_callbacks_t cbs = {
97-
.on_recv_done = example_i2c_rx_done_callback,
98-
};
99-
ESP_ERROR_CHECK(i2c_slave_register_event_callbacks(slave_handle, &cbs, s_receive_queue));
100-
101-
i2c_slave_rx_done_event_data_t rx_data;
102-
TEST_ESP_OK(i2c_slave_receive(slave_handle, data_rd, DATA_LENGTH));
103-
104-
unity_send_signal("i2c slave init finish");
105-
106-
unity_wait_for_signal("master write");
107-
xQueueReceive(s_receive_queue, &rx_data, pdMS_TO_TICKS(1000));
108-
109-
disp_buf(data_rd, DATA_LENGTH);
110-
for (int i = 0; i < DATA_LENGTH; i++) {
111-
TEST_ASSERT(data_rd[i] == i);
112-
}
113-
vQueueDelete(s_receive_queue);
114-
unity_send_signal("ready to delete");
115-
TEST_ESP_OK(i2c_del_slave_device(slave_handle));
116-
}
117-
118140
TEST_CASE_MULTIPLE_DEVICES("I2C master write slave test 10 bit", "[i2c][test_env=generic_multi_device][timeout=150]", i2c_master_write_test_10bit, i2c_slave_read_test_10bit);
119141

120142
static void master_read_slave_test_10bit(void)
@@ -134,59 +156,74 @@ static void master_read_slave_test_10bit(void)
134156
.dev_addr_length = I2C_ADDR_BIT_LEN_10,
135157
.device_address = 0x134,
136158
.scl_speed_hz = 100000,
159+
.scl_wait_us = 20000,
137160
};
138161

139162
i2c_master_dev_handle_t dev_handle;
140163
TEST_ESP_OK(i2c_master_bus_add_device(bus_handle, &dev_cfg, &dev_handle));
141164

142165
unity_wait_for_signal("i2c slave init finish");
143166

144-
printf("Slave please write data to buffer\n");
145-
146-
unity_send_signal("slave write");
147-
unity_wait_for_signal("master read");
148-
149167
TEST_ESP_OK(i2c_master_receive(dev_handle, data_rd, DATA_LENGTH, -1));
150168
vTaskDelay(100 / portTICK_PERIOD_MS);
151169
for (int i = 0; i < DATA_LENGTH; i++) {
152-
printf("%d\n", data_rd[i]);
170+
printf("%x\n", data_rd[i]);
153171
TEST_ASSERT(data_rd[i] == i);
154172
}
155-
unity_send_signal("ready to delete 10bit");
173+
unity_send_signal("ready to delete master read test");
156174

157175
TEST_ESP_OK(i2c_master_bus_rm_device(dev_handle));
158176
TEST_ESP_OK(i2c_del_master_bus(bus_handle));
159177
}
160178

161179
static void slave_write_buffer_test_10bit(void)
162180
{
163-
uint8_t data_wr[DATA_LENGTH] = {0};
181+
i2c_slave_dev_handle_t handle;
182+
uint8_t data_wr[DATA_LENGTH];
183+
event_queue = xQueueCreate(2, sizeof(i2c_slave_event_t));
184+
assert(event_queue);
164185

165186
i2c_slave_config_t i2c_slv_config = {
166-
.addr_bit_len = I2C_ADDR_BIT_LEN_10,
167-
.clk_source = I2C_CLK_SRC_DEFAULT,
168187
.i2c_port = TEST_I2C_PORT,
169-
.send_buf_depth = 256,
188+
.clk_source = I2C_CLK_SRC_DEFAULT,
170189
.scl_io_num = I2C_SLAVE_SCL_IO,
171190
.sda_io_num = I2C_SLAVE_SDA_IO,
172191
.slave_addr = 0x134,
192+
.send_buf_depth = DATA_LENGTH,
193+
.receive_buf_depth = DATA_LENGTH,
194+
.flags.enable_internal_pullup = true,
195+
.addr_bit_len = I2C_ADDR_BIT_LEN_10,
173196
};
174197

175-
i2c_slave_dev_handle_t slave_handle;
176-
TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config, &slave_handle));
198+
TEST_ESP_OK(i2c_new_slave_device(&i2c_slv_config, &handle));
199+
200+
i2c_slave_event_callbacks_t cbs = {
201+
.on_receive = i2c_slave_receive_cb,
202+
.on_request = i2c_slave_request_cb,
203+
};
204+
205+
TEST_ESP_OK(i2c_slave_register_event_callbacks(handle, &cbs, NULL));
177206

178207
unity_send_signal("i2c slave init finish");
179208

180-
unity_wait_for_signal("slave write");
181209
for (int i = 0; i < DATA_LENGTH; i++) {
182210
data_wr[i] = i;
183211
}
184212

185-
TEST_ESP_OK(i2c_slave_transmit(slave_handle, data_wr, DATA_LENGTH, -1));
186-
disp_buf(data_wr, DATA_LENGTH);
187-
unity_send_signal("master read");
188-
unity_wait_for_signal("ready to delete 10bit");
189-
TEST_ESP_OK(i2c_del_slave_device(slave_handle));
213+
i2c_slave_event_t evt;
214+
uint32_t write_len;
215+
while (true) {
216+
if (xQueueReceive(event_queue, &evt, portMAX_DELAY) == pdTRUE) {
217+
if (evt == I2C_SLAVE_EVT_TX) {
218+
TEST_ESP_OK(i2c_slave_write(handle, data_wr, DATA_LENGTH, &write_len, 1000));
219+
break;
220+
}
221+
}
222+
}
223+
224+
unity_wait_for_signal("ready to delete master read test");
225+
vQueueDelete(event_queue);
226+
TEST_ESP_OK(i2c_del_slave_device(handle));
190227
}
191228

192229
TEST_CASE_MULTIPLE_DEVICES("I2C master read slave test 10 bit", "[i2c][test_env=generic_multi_device][timeout=150]", master_read_slave_test_10bit, slave_write_buffer_test_10bit);

0 commit comments

Comments
 (0)