test_i2c.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. /**
  7. * test environment UT_T2_I2C:
  8. * please prepare two ESP32-WROVER-KIT board.
  9. * Then connect GPIO18 and GPIO18, GPIO19 and GPIO19 between these two boards.
  10. */
  11. #include <stdio.h>
  12. #include <string.h>
  13. #include "unity.h"
  14. #include "test_utils.h"
  15. #include "unity_config.h"
  16. #include "driver/i2c.h"
  17. #include "esp_attr.h"
  18. #include "esp_log.h"
  19. #include "soc/gpio_periph.h"
  20. #include "soc/i2c_periph.h"
  21. #include "esp_system.h"
  22. #include "soc/uart_struct.h"
  23. #include "esp_private/periph_ctrl.h"
  24. #include "esp_rom_gpio.h"
  25. #include "hal/gpio_hal.h"
  26. #include "hal/uart_ll.h"
  27. #if SOC_I2C_SUPPORT_SLAVE // i2c test can't work without slave
  28. #define DATA_LENGTH 512 /*!<Data buffer length for test buffer*/
  29. #define RW_TEST_LENGTH 129 /*!<Data length for r/w test, any value from 0-DATA_LENGTH*/
  30. #define DELAY_TIME_BETWEEN_ITEMS_MS 1234 /*!< delay time between different test items */
  31. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32S3 || CONFIG_IDF_TARGET_ESP32H2
  32. #define I2C_SLAVE_SCL_IO 5 /*!<gpio number for i2c slave clock */
  33. #define I2C_SLAVE_SDA_IO 6 /*!<gpio number for i2c slave data */
  34. #else
  35. #define I2C_SLAVE_SCL_IO 19 /*!<gpio number for i2c slave clock */
  36. #define I2C_SLAVE_SDA_IO 18 /*!<gpio number for i2c slave data */
  37. #endif
  38. #define I2C_SLAVE_NUM I2C_NUM_0 /*!<I2C port number for slave dev */
  39. #define I2C_SLAVE_TX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave tx buffer size */
  40. #define I2C_SLAVE_RX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave rx buffer size */
  41. #if CONFIG_IDF_TARGET_ESP32C3 || CONFIG_IDF_TARGET_ESP32H2
  42. #define I2C_MASTER_SCL_IO 5 /*!<gpio number for i2c master clock */
  43. #define I2C_MASTER_SDA_IO 6 /*!<gpio number for i2c master data */
  44. #elif CONFIG_IDF_TARGET_ESP32S3
  45. #define I2C_MASTER_SCL_IO 2 /*!<gpio number for i2c master clock */
  46. #define I2C_MASTER_SDA_IO 1 /*!<gpio number for i2c master data */
  47. #else
  48. #define I2C_MASTER_SCL_IO 19 /*!< gpio number for I2C master clock */
  49. #define I2C_MASTER_SDA_IO 18 /*!< gpio number for I2C master data */
  50. #endif
  51. #define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C port number for master dev */
  52. #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
  53. #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
  54. #define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
  55. #define ESP_SLAVE_ADDR 0x28 /*!< ESP32 slave address, you can set any 7bit value */
  56. #define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
  57. #define READ_BIT I2C_MASTER_READ /*!< I2C master read */
  58. #define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
  59. #define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
  60. #define ACK_VAL 0x0 /*!< I2C ack value */
  61. #define NACK_VAL 0x1 /*!< I2C nack value */
  62. #define PULSE_IO 19
  63. #define PCNT_INPUT_IO 4
  64. #define PCNT_CTRL_FLOATING_IO 5
  65. #define HIGHEST_LIMIT 10000
  66. #define LOWEST_LIMIT -10000
  67. static DRAM_ATTR i2c_dev_t *const I2C[SOC_I2C_NUM] = { &I2C0,
  68. #if SOC_I2C_NUM > 1
  69. &I2C1,
  70. #endif
  71. };
  72. static esp_err_t i2c_master_write_slave(i2c_port_t i2c_num, uint8_t *data_wr, size_t size)
  73. {
  74. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  75. i2c_master_start(cmd);
  76. TEST_ESP_OK(i2c_master_write_byte(cmd, ( ESP_SLAVE_ADDR << 1 ) | WRITE_BIT, ACK_CHECK_EN));
  77. TEST_ESP_OK(i2c_master_write(cmd, data_wr, size, ACK_CHECK_EN));
  78. TEST_ESP_OK(i2c_master_stop(cmd));
  79. esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 5000 / portTICK_PERIOD_MS);
  80. i2c_cmd_link_delete(cmd);
  81. return ret;
  82. }
  83. static i2c_config_t i2c_master_init(void)
  84. {
  85. i2c_config_t conf_master = {
  86. .mode = I2C_MODE_MASTER,
  87. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  88. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  89. .master.clk_speed = I2C_MASTER_FREQ_HZ,
  90. .sda_io_num = I2C_MASTER_SDA_IO,
  91. .scl_io_num = I2C_MASTER_SCL_IO,
  92. .clk_flags = 0,
  93. };
  94. return conf_master;
  95. }
  96. static i2c_config_t i2c_slave_init(void)
  97. {
  98. i2c_config_t conf_slave = {
  99. .mode = I2C_MODE_SLAVE,
  100. .sda_io_num = I2C_SLAVE_SDA_IO,
  101. .scl_io_num = I2C_SLAVE_SCL_IO,
  102. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  103. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  104. .slave.addr_10bit_en = 0,
  105. .slave.slave_addr = ESP_SLAVE_ADDR,
  106. };
  107. return conf_slave;
  108. }
  109. TEST_CASE("I2C i2c_set_pin() fails if sda and scl gpios are same", "[i2c]")
  110. {
  111. TEST_ASSERT_EQUAL(ESP_ERR_INVALID_ARG, i2c_set_pin(0, 0, 0, true, true , I2C_MODE_SLAVE));
  112. }
  113. TEST_CASE("I2C config test", "[i2c]")
  114. {
  115. // master test
  116. i2c_config_t conf_master = i2c_master_init();
  117. gpio_pullup_t sda_pull_up_en[2] = {GPIO_PULLUP_DISABLE, GPIO_PULLUP_ENABLE};
  118. gpio_pullup_t scl_pull_up_en[2] = {GPIO_PULLUP_DISABLE, GPIO_PULLUP_ENABLE};
  119. for (int i = 0; i < 2; i++) {
  120. for (int j = 0; j < 2; j++) {
  121. conf_master.sda_pullup_en = sda_pull_up_en[i];
  122. conf_master.scl_pullup_en = scl_pull_up_en[j];
  123. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  124. I2C_MASTER_RX_BUF_DISABLE,
  125. I2C_MASTER_TX_BUF_DISABLE, 0));
  126. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  127. TEST_ASSERT_EQUAL_INT32(I2C[I2C_MASTER_NUM]->ctr.ms_mode, 1);
  128. TEST_ESP_OK(i2c_driver_delete(I2C_MASTER_NUM));
  129. }
  130. }
  131. // slave test
  132. i2c_config_t conf_slave = i2c_slave_init();
  133. for (int i = 0; i < 2; i++) {
  134. for (int j = 0; j < 2; j++) {
  135. conf_slave.sda_pullup_en = sda_pull_up_en[i];
  136. conf_slave.scl_pullup_en = scl_pull_up_en[j];
  137. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  138. I2C_SLAVE_RX_BUF_LEN,
  139. I2C_SLAVE_TX_BUF_LEN, 0));
  140. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  141. TEST_ASSERT_EQUAL_INT32(I2C[I2C_SLAVE_NUM] -> ctr.ms_mode, 0);
  142. TEST_ESP_OK(i2c_driver_delete(I2C_SLAVE_NUM));
  143. }
  144. }
  145. }
  146. TEST_CASE("I2C set and get period test", "[i2c]")
  147. {
  148. int high_period, low_period;
  149. i2c_config_t conf_master = i2c_master_init();
  150. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  151. I2C_MASTER_RX_BUF_DISABLE,
  152. I2C_MASTER_TX_BUF_DISABLE, 0));
  153. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  154. TEST_ESP_OK(i2c_set_period(I2C_MASTER_NUM, I2C_SCL_HIGH_PERIOD_V, I2C_SCL_HIGH_PERIOD_V));
  155. TEST_ESP_OK(i2c_get_period(I2C_MASTER_NUM, &high_period, &low_period));
  156. TEST_ASSERT_EQUAL_INT(I2C_SCL_HIGH_PERIOD_V, high_period);
  157. TEST_ASSERT_EQUAL_INT(I2C_SCL_HIGH_PERIOD_V, low_period);
  158. TEST_ASSERT_NOT_NULL((void *)i2c_set_period(I2C_MASTER_NUM, I2C_SCL_HIGH_PERIOD_V + 1, I2C_SCL_HIGH_PERIOD_V + 1));
  159. TEST_ESP_OK(i2c_set_period(I2C_MASTER_NUM, 300, 400));
  160. TEST_ESP_OK(i2c_get_period(I2C_MASTER_NUM, &high_period, &low_period));
  161. TEST_ASSERT_EQUAL_INT(300, high_period);
  162. TEST_ASSERT_EQUAL_INT(400, low_period);
  163. TEST_ESP_OK(i2c_driver_delete(I2C_MASTER_NUM));
  164. }
  165. TEST_CASE("I2C config FIFO test", "[i2c]")
  166. {
  167. i2c_config_t conf_slave = i2c_slave_init();
  168. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  169. I2C_SLAVE_RX_BUF_LEN,
  170. I2C_SLAVE_TX_BUF_LEN, 0));
  171. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  172. TEST_ASSERT_BIT_LOW(1, I2C[I2C_SLAVE_NUM]->fifo_conf.tx_fifo_rst);
  173. TEST_ESP_OK(i2c_reset_tx_fifo(I2C_SLAVE_NUM));
  174. TEST_ASSERT_BIT_LOW(0, I2C[I2C_SLAVE_NUM]->fifo_conf.tx_fifo_rst);
  175. TEST_ESP_OK(i2c_reset_rx_fifo(I2C_SLAVE_NUM));
  176. TEST_ASSERT_BIT_LOW(0, I2C[I2C_SLAVE_NUM]->fifo_conf.rx_fifo_rst);
  177. TEST_ESP_OK(i2c_driver_delete(I2C_SLAVE_NUM));
  178. }
  179. TEST_CASE("I2C timing test", "[i2c]")
  180. {
  181. int test_setup_time, test_data_time, test_stop_time, test_hold_time;
  182. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  183. i2c_config_t conf_master = i2c_master_init();
  184. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  185. I2C_MASTER_RX_BUF_DISABLE,
  186. I2C_MASTER_TX_BUF_DISABLE, 0));
  187. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  188. TEST_ESP_OK(i2c_set_start_timing(I2C_MASTER_NUM, 50, 60));
  189. TEST_ESP_OK(i2c_set_data_timing(I2C_MASTER_NUM, 80, 60));
  190. TEST_ESP_OK(i2c_set_stop_timing(I2C_MASTER_NUM, 100, 60));
  191. for (int i = 0; i < DATA_LENGTH; i++) {
  192. data_wr[i] = i;
  193. }
  194. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
  195. TEST_ESP_OK(i2c_get_start_timing(I2C_MASTER_NUM, &test_setup_time, &test_hold_time));
  196. TEST_ESP_OK(i2c_get_data_timing(I2C_MASTER_NUM, &test_data_time, &test_hold_time));
  197. TEST_ESP_OK(i2c_get_stop_timing(I2C_MASTER_NUM, &test_stop_time, &test_hold_time));
  198. TEST_ASSERT_EQUAL_INT32(50, test_setup_time);
  199. TEST_ASSERT_EQUAL_INT32(80, test_data_time);
  200. TEST_ASSERT_EQUAL_INT32(100, test_stop_time);
  201. TEST_ASSERT_EQUAL_INT32(60, test_hold_time);
  202. free(data_wr);
  203. i2c_driver_delete(I2C_MASTER_NUM);
  204. }
  205. TEST_CASE("I2C data mode test", "[i2c]")
  206. {
  207. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  208. i2c_trans_mode_t test_tx_trans_mode, test_rx_trans_mode;
  209. i2c_config_t conf_master = i2c_master_init();
  210. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  211. I2C_MASTER_RX_BUF_DISABLE,
  212. I2C_MASTER_TX_BUF_DISABLE, 0));
  213. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  214. for (int i = 0; i < DATA_LENGTH; i++) {
  215. data_wr[i] = i;
  216. }
  217. TEST_ESP_OK(i2c_set_data_mode(I2C_MASTER_NUM, I2C_DATA_MODE_LSB_FIRST, I2C_DATA_MODE_LSB_FIRST));
  218. TEST_ESP_OK(i2c_get_data_mode(I2C_MASTER_NUM, &test_tx_trans_mode, &test_rx_trans_mode));
  219. TEST_ASSERT_EQUAL_INT(I2C_DATA_MODE_LSB_FIRST, test_tx_trans_mode);
  220. TEST_ASSERT_EQUAL_INT(I2C_DATA_MODE_LSB_FIRST, test_rx_trans_mode);
  221. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
  222. TEST_ESP_OK(i2c_set_data_mode(I2C_MASTER_NUM, I2C_DATA_MODE_MSB_FIRST, I2C_DATA_MODE_MSB_FIRST));
  223. TEST_ESP_OK(i2c_get_data_mode(I2C_MASTER_NUM, &test_tx_trans_mode, &test_rx_trans_mode));
  224. TEST_ASSERT_EQUAL_INT(I2C_DATA_MODE_MSB_FIRST, test_tx_trans_mode);
  225. TEST_ASSERT_EQUAL_INT(I2C_DATA_MODE_MSB_FIRST, test_rx_trans_mode);
  226. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
  227. free(data_wr);
  228. i2c_driver_delete(I2C_MASTER_NUM);
  229. }
  230. TEST_CASE("I2C driver memory leaking check", "[i2c]")
  231. {
  232. esp_err_t ret;
  233. int size = esp_get_free_heap_size();
  234. for (uint32_t i = 0; i <= 1000; i++) {
  235. ret = i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  236. I2C_SLAVE_RX_BUF_LEN,
  237. I2C_SLAVE_TX_BUF_LEN, 0);
  238. TEST_ASSERT(ret == ESP_OK);
  239. vTaskDelay(10 / portTICK_PERIOD_MS);
  240. i2c_driver_delete(I2C_SLAVE_NUM);
  241. TEST_ASSERT(ret == ESP_OK);
  242. }
  243. TEST_ASSERT_INT_WITHIN(100, size, esp_get_free_heap_size());
  244. }
  245. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3, ESP32C2)
  246. // print the reading buffer
  247. static void disp_buf(uint8_t *buf, int len)
  248. {
  249. int i;
  250. for (i = 0; i < len; i++) {
  251. printf("%02x ", buf[i]);
  252. if (( i + 1 ) % 16 == 0) {
  253. printf("\n");
  254. }
  255. }
  256. printf("\n");
  257. }
  258. static void i2c_master_write_test(void)
  259. {
  260. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  261. int i;
  262. i2c_config_t conf_master = i2c_master_init();
  263. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  264. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  265. I2C_MASTER_RX_BUF_DISABLE,
  266. I2C_MASTER_TX_BUF_DISABLE, 0));
  267. unity_wait_for_signal("i2c slave init finish");
  268. unity_send_signal("master write");
  269. for (i = 0; i < DATA_LENGTH / 2; i++) {
  270. data_wr[i] = i;
  271. }
  272. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, DATA_LENGTH / 2);
  273. disp_buf(data_wr, i + 1);
  274. free(data_wr);
  275. unity_wait_for_signal("ready to delete");
  276. TEST_ESP_OK(i2c_driver_delete(I2C_MASTER_NUM));
  277. }
  278. static void i2c_slave_read_test(void)
  279. {
  280. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
  281. int size_rd = 0;
  282. int len = 0;
  283. i2c_config_t conf_slave = i2c_slave_init();
  284. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  285. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  286. I2C_SLAVE_RX_BUF_LEN,
  287. I2C_SLAVE_TX_BUF_LEN, 0));
  288. unity_send_signal("i2c slave init finish");
  289. unity_wait_for_signal("master write");
  290. while (1) {
  291. len = i2c_slave_read_buffer( I2C_SLAVE_NUM, data_rd + size_rd, DATA_LENGTH, 10000 / portTICK_PERIOD_MS);
  292. if (len == 0) {
  293. break;
  294. }
  295. size_rd += len;
  296. }
  297. disp_buf(data_rd, size_rd);
  298. for (int i = 0; i < size_rd; i++) {
  299. TEST_ASSERT(data_rd[i] == i);
  300. }
  301. free(data_rd);
  302. unity_send_signal("ready to delete");
  303. TEST_ESP_OK(i2c_driver_delete(I2C_SLAVE_NUM));
  304. }
  305. TEST_CASE_MULTIPLE_DEVICES("I2C master write slave test", "[i2c][test_env=UT_T2_I2C][timeout=150]", i2c_master_write_test, i2c_slave_read_test);
  306. static void master_read_slave_test(void)
  307. {
  308. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
  309. memset(data_rd, 0, DATA_LENGTH);
  310. i2c_config_t conf_master = i2c_master_init();
  311. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  312. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  313. I2C_MASTER_RX_BUF_DISABLE,
  314. I2C_MASTER_TX_BUF_DISABLE, 0));
  315. unity_wait_for_signal("i2c slave init finish");
  316. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  317. i2c_master_start(cmd);
  318. i2c_master_write_byte(cmd, ( ESP_SLAVE_ADDR << 1 ) | READ_BIT, ACK_CHECK_EN);
  319. unity_send_signal("slave write");
  320. unity_wait_for_signal("master read");
  321. i2c_master_read(cmd, data_rd, RW_TEST_LENGTH-1, ACK_VAL);
  322. i2c_master_read_byte(cmd, data_rd + RW_TEST_LENGTH-1, NACK_VAL);
  323. i2c_master_stop(cmd);
  324. i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 5000 / portTICK_PERIOD_MS);
  325. i2c_cmd_link_delete(cmd);
  326. vTaskDelay(100 / portTICK_PERIOD_MS);
  327. for (int i = 0; i < RW_TEST_LENGTH; i++) {
  328. printf("%d\n", data_rd[i]);
  329. TEST_ASSERT(data_rd[i]==i);
  330. }
  331. free(data_rd);
  332. unity_send_signal("ready to delete");
  333. i2c_driver_delete(I2C_MASTER_NUM);
  334. }
  335. static void slave_write_buffer_test(void)
  336. {
  337. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  338. int size_rd;
  339. i2c_config_t conf_slave = i2c_slave_init();
  340. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  341. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  342. I2C_SLAVE_RX_BUF_LEN,
  343. I2C_SLAVE_TX_BUF_LEN, 0));
  344. unity_send_signal("i2c slave init finish");
  345. unity_wait_for_signal("slave write");
  346. for (int i = 0; i < DATA_LENGTH / 2; i++) {
  347. data_wr[i] = i;
  348. }
  349. size_rd = i2c_slave_write_buffer(I2C_SLAVE_NUM, data_wr, RW_TEST_LENGTH, 2000 / portTICK_PERIOD_MS);
  350. disp_buf(data_wr, size_rd);
  351. unity_send_signal("master read");
  352. unity_wait_for_signal("ready to delete");
  353. free(data_wr);
  354. i2c_driver_delete(I2C_SLAVE_NUM);
  355. }
  356. TEST_CASE_MULTIPLE_DEVICES("I2C master read slave test", "[i2c][test_env=UT_T2_I2C][timeout=150]", master_read_slave_test, slave_write_buffer_test);
  357. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32)
  358. static void i2c_master_write_read_test(void)
  359. {
  360. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
  361. memset(data_rd, 0, DATA_LENGTH);
  362. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  363. i2c_config_t conf_master = i2c_master_init();
  364. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  365. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  366. I2C_MASTER_RX_BUF_DISABLE,
  367. I2C_MASTER_TX_BUF_DISABLE, 0));
  368. unity_wait_for_signal("i2c slave init finish");
  369. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  370. i2c_master_start(cmd);
  371. i2c_master_write_byte(cmd, ( ESP_SLAVE_ADDR << 1 ) | READ_BIT, ACK_CHECK_EN);
  372. unity_send_signal("slave write");
  373. unity_wait_for_signal("master read and write");
  374. i2c_master_read(cmd, data_rd, RW_TEST_LENGTH, ACK_VAL);
  375. i2c_master_read_byte(cmd, data_rd + RW_TEST_LENGTH, NACK_VAL);
  376. i2c_master_stop(cmd);
  377. i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 5000 / portTICK_PERIOD_MS);
  378. i2c_cmd_link_delete(cmd);
  379. vTaskDelay(100 / portTICK_PERIOD_MS);
  380. disp_buf(data_rd, RW_TEST_LENGTH);
  381. for (int i = 0; i < RW_TEST_LENGTH; i++) {
  382. TEST_ASSERT(data_rd[i] == i/2);
  383. }
  384. for (int i = 0; i < DATA_LENGTH; i++) {
  385. data_wr[i] = i % 3;
  386. }
  387. vTaskDelay(100 / portTICK_PERIOD_MS);
  388. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
  389. free(data_wr);
  390. free(data_rd);
  391. unity_send_signal("slave read");
  392. unity_wait_for_signal("ready to delete");
  393. i2c_driver_delete(I2C_MASTER_NUM);
  394. }
  395. static void i2c_slave_read_write_test(void)
  396. {
  397. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
  398. memset(data_rd, 0, DATA_LENGTH);
  399. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  400. int size_rd;
  401. i2c_config_t conf_slave = i2c_slave_init();
  402. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  403. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  404. I2C_SLAVE_RX_BUF_LEN,
  405. I2C_SLAVE_TX_BUF_LEN, 0));
  406. unity_send_signal("i2c slave init finish");
  407. unity_wait_for_signal("slave write");
  408. for (int i = 0; i < DATA_LENGTH / 2; i++) {
  409. data_wr[i] = i/2;
  410. }
  411. size_rd = i2c_slave_write_buffer(I2C_SLAVE_NUM, data_wr, RW_TEST_LENGTH, 2000 / portTICK_PERIOD_MS);
  412. disp_buf(data_wr, size_rd);
  413. unity_send_signal("master read and write");
  414. unity_wait_for_signal("slave read");
  415. size_rd = i2c_slave_read_buffer( I2C_SLAVE_NUM, data_rd, RW_TEST_LENGTH, 1000 / portTICK_PERIOD_MS);
  416. printf("slave read data is:\n");
  417. disp_buf(data_rd, size_rd);
  418. for (int i = 0; i < RW_TEST_LENGTH; i++) {
  419. TEST_ASSERT(data_rd[i] == i % 3);
  420. }
  421. free(data_wr);
  422. free(data_rd);
  423. unity_send_signal("ready to delete");
  424. i2c_driver_delete(I2C_SLAVE_NUM);
  425. }
  426. TEST_CASE_MULTIPLE_DEVICES("I2C read and write test", "[i2c][test_env=UT_T2_I2C][timeout=150]", i2c_master_write_read_test, i2c_slave_read_write_test);
  427. static void i2c_master_repeat_write(void)
  428. {
  429. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  430. int times = 3;
  431. i2c_config_t conf_master = i2c_master_init();
  432. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  433. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  434. I2C_MASTER_RX_BUF_DISABLE,
  435. I2C_MASTER_TX_BUF_DISABLE, 0));
  436. unity_wait_for_signal("i2c slave init finish");
  437. for (int j = 0; j < times; j++) {
  438. for (int i = 0; i < DATA_LENGTH; i++) {
  439. data_wr[i] = j + i;
  440. }
  441. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
  442. disp_buf(data_wr, RW_TEST_LENGTH);
  443. }
  444. free(data_wr);
  445. unity_send_signal("master write");
  446. unity_wait_for_signal("ready to delete");
  447. i2c_driver_delete(I2C_MASTER_NUM);
  448. }
  449. static void i2c_slave_repeat_read(void)
  450. {
  451. int size_rd = 0;
  452. int times = 3;
  453. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH * 3);
  454. i2c_config_t conf_slave = i2c_slave_init();
  455. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  456. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  457. I2C_SLAVE_RX_BUF_LEN,
  458. I2C_SLAVE_TX_BUF_LEN, 0));
  459. unity_send_signal("i2c slave init finish");
  460. unity_wait_for_signal("master write");
  461. while (1) {
  462. int len = i2c_slave_read_buffer( I2C_SLAVE_NUM, data_rd + size_rd, RW_TEST_LENGTH * 3, 10000 / portTICK_PERIOD_MS);
  463. if (len == 0) {
  464. break;
  465. }
  466. size_rd += len;
  467. }
  468. disp_buf(data_rd, size_rd);
  469. for (int j = 0; j < times; j++) {
  470. for (int i = 0; i < RW_TEST_LENGTH; i++) {
  471. printf("data: %d, %d\n", data_rd[j * RW_TEST_LENGTH + i], (i % 129 + j));
  472. TEST_ASSERT(data_rd[j * RW_TEST_LENGTH + i] == (i % 129 + j));
  473. }
  474. }
  475. free(data_rd);
  476. unity_send_signal("ready to delete");
  477. i2c_driver_delete(I2C_SLAVE_NUM);
  478. }
  479. TEST_CASE_MULTIPLE_DEVICES("I2C repeat write test", "[i2c][test_env=UT_T2_I2C][timeout=150]", i2c_master_repeat_write, i2c_slave_repeat_read);
  480. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3)
  481. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2, ESP32S3)
  482. static volatile bool exit_flag;
  483. static bool test_read_func;
  484. static void test_task(void *pvParameters)
  485. {
  486. SemaphoreHandle_t *sema = (SemaphoreHandle_t *) pvParameters;
  487. uint8_t *data = (uint8_t *) malloc(DATA_LENGTH);
  488. i2c_config_t conf_slave = i2c_slave_init();
  489. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  490. I2C_SLAVE_RX_BUF_LEN,
  491. I2C_SLAVE_TX_BUF_LEN, 0));
  492. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  493. while (exit_flag == false) {
  494. if (test_read_func) {
  495. i2c_slave_read_buffer(I2C_SLAVE_NUM, data, DATA_LENGTH, 0);
  496. } else {
  497. i2c_slave_write_buffer(I2C_SLAVE_NUM, data, DATA_LENGTH, 0);
  498. }
  499. vTaskDelay(10/portTICK_PERIOD_MS);
  500. }
  501. free(data);
  502. xSemaphoreGive(*sema);
  503. vTaskDelete(NULL);
  504. }
  505. TEST_CASE("test i2c_slave_read_buffer is not blocked when ticks_to_wait=0", "[i2c]")
  506. {
  507. SemaphoreHandle_t exit_sema = xSemaphoreCreateBinary();
  508. exit_flag = false;
  509. test_read_func = true;
  510. xTaskCreate(test_task, "tsk1", 2048, &exit_sema, 5, NULL);
  511. printf("Waiting for 5 sec\n");
  512. vTaskDelay(5000 / portTICK_PERIOD_MS);
  513. exit_flag = true;
  514. if (xSemaphoreTake(exit_sema, 1000 / portTICK_PERIOD_MS) == pdTRUE) {
  515. vSemaphoreDelete(exit_sema);
  516. } else {
  517. TEST_FAIL_MESSAGE("i2c_slave_read_buffer is blocked");
  518. }
  519. TEST_ESP_OK(i2c_driver_delete(I2C_SLAVE_NUM));
  520. }
  521. TEST_CASE("test i2c_slave_write_buffer is not blocked when ticks_to_wait=0", "[i2c]")
  522. {
  523. SemaphoreHandle_t exit_sema = xSemaphoreCreateBinary();
  524. exit_flag = false;
  525. test_read_func = false;
  526. xTaskCreate(test_task, "tsk1", 2048, &exit_sema, 5, NULL);
  527. printf("Waiting for 5 sec\n");
  528. vTaskDelay(5000 / portTICK_PERIOD_MS);
  529. exit_flag = true;
  530. if (xSemaphoreTake(exit_sema, 1000 / portTICK_PERIOD_MS) == pdTRUE) {
  531. vSemaphoreDelete(exit_sema);
  532. } else {
  533. TEST_FAIL_MESSAGE("i2c_slave_write_buffer is blocked");
  534. }
  535. TEST_ESP_OK(i2c_driver_delete(I2C_SLAVE_NUM));
  536. }
  537. TEST_CASE("I2C general API test", "[i2c]")
  538. {
  539. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  540. #define I2C_TEST_TIME 0x3ff
  541. #else
  542. #define I2C_TEST_TIME 0x1f
  543. #endif
  544. const int i2c_num = 0;
  545. i2c_config_t conf_master = {
  546. .mode = I2C_MODE_MASTER,
  547. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  548. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  549. .master.clk_speed = I2C_MASTER_FREQ_HZ,
  550. .sda_io_num = I2C_MASTER_SDA_IO,
  551. .scl_io_num = I2C_MASTER_SCL_IO,
  552. };
  553. TEST_ESP_OK(i2c_param_config( i2c_num, &conf_master));
  554. int time_get0, time_get1;
  555. for(int i = 10; i < I2C_TEST_TIME; i++) {
  556. //set period test
  557. TEST_ESP_OK(i2c_set_period(i2c_num, i, i));
  558. TEST_ESP_OK(i2c_get_period(i2c_num, &time_get0, &time_get1));
  559. TEST_ASSERT((time_get0 == i) && (time_get1 == i));
  560. //set start timing test
  561. TEST_ESP_OK(i2c_set_start_timing(i2c_num, i, i));
  562. TEST_ESP_OK(i2c_get_start_timing(i2c_num, &time_get0, &time_get1));
  563. TEST_ASSERT((time_get0 == i) && (time_get1 == i));
  564. //set stop timing test
  565. TEST_ESP_OK(i2c_set_stop_timing(i2c_num, i, i));
  566. TEST_ESP_OK(i2c_get_stop_timing(i2c_num, &time_get0, &time_get1));
  567. TEST_ASSERT((time_get0 == i) && (time_get1 == i));
  568. //set data timing test
  569. TEST_ESP_OK(i2c_set_data_timing(i2c_num, i, i));
  570. TEST_ESP_OK(i2c_get_data_timing(i2c_num, &time_get0, &time_get1));
  571. TEST_ASSERT((time_get0 == i) && (time_get1 == i));
  572. //set time out test
  573. TEST_ESP_OK(i2c_set_timeout(i2c_num, i));
  574. TEST_ESP_OK(i2c_get_timeout(i2c_num, &time_get0));
  575. TEST_ASSERT(time_get0 == i);
  576. }
  577. }
  578. //Init uart baud rate detection
  579. static void uart_aut_baud_det_init(int rxd_io_num)
  580. {
  581. gpio_hal_iomux_func_sel(GPIO_PIN_MUX_REG[rxd_io_num], PIN_FUNC_GPIO);
  582. gpio_set_direction(rxd_io_num, GPIO_MODE_INPUT_OUTPUT);
  583. esp_rom_gpio_connect_out_signal(rxd_io_num, I2CEXT0_SCL_OUT_IDX, 0, 0);
  584. esp_rom_gpio_connect_in_signal(rxd_io_num, U1RXD_IN_IDX, 0);
  585. periph_module_enable(PERIPH_UART1_MODULE);
  586. /* Reset all the bits */
  587. uart_ll_disable_intr_mask(&UART1, ~0);
  588. uart_ll_clr_intsts_mask(&UART1, ~0);
  589. uart_ll_set_autobaud_en(&UART1, true);
  590. }
  591. //Calculate I2C scl freq
  592. static void i2c_scl_freq_cal(void)
  593. {
  594. #if CONFIG_IDF_TARGET_ESP32 || CONFIG_IDF_TARGET_ESP32S2
  595. const int i2c_source_clk_freq = 80000000;
  596. const float i2c_cource_clk_period = 0.0125;
  597. int expt_cnt = 542;
  598. #else
  599. const int i2c_source_clk_freq = 18000000; // Clock sorce: RTC
  600. const float i2c_cource_clk_period = 0.056;
  601. int expt_cnt = 540;
  602. #endif
  603. int edg_cnt = uart_ll_get_rxd_edge_cnt(&UART1);
  604. int pospulse_cnt = uart_ll_get_pos_pulse_cnt(&UART1);
  605. int negpulse_cnt = uart_ll_get_neg_pulse_cnt(&UART1);
  606. int high_period_cnt = uart_ll_get_high_pulse_cnt(&UART1);
  607. int low_period_cnt = uart_ll_get_low_pulse_cnt(&UART1);
  608. if(edg_cnt != expt_cnt) {
  609. printf("\nedg_cnt != %d, test fail\n", expt_cnt);
  610. return;
  611. }
  612. printf("\nDetected SCL frequency: %d Hz\n", i2c_source_clk_freq / ((pospulse_cnt + negpulse_cnt) / 2) );
  613. printf("\nSCL high period %.3f (us), SCL low_period %.3f (us)\n\n", (float)(i2c_cource_clk_period * high_period_cnt), (float)(i2c_cource_clk_period * low_period_cnt));
  614. uart_ll_set_autobaud_en(&UART1, false);
  615. periph_module_disable(PERIPH_UART1_MODULE);
  616. }
  617. TEST_CASE("I2C SCL freq test (local test)", "[i2c][ignore]")
  618. {
  619. //Use the UART baud rate detection function to detect the I2C SCL frequency.
  620. const int i2c_num = 0;
  621. const int uart1_rxd_io = 5;
  622. i2c_config_t conf_master = {
  623. .mode = I2C_MODE_MASTER,
  624. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  625. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  626. .master.clk_speed = 400000,
  627. .sda_io_num = I2C_MASTER_SDA_IO,
  628. .scl_io_num = I2C_MASTER_SCL_IO,
  629. };
  630. uint8_t *data = (uint8_t *)malloc(30);
  631. TEST_ESP_OK(i2c_param_config( i2c_num, &conf_master));
  632. TEST_ESP_OK(i2c_driver_install(i2c_num, I2C_MODE_MASTER, 0, 0, 0));
  633. memset(data, 0, 0);
  634. uart_aut_baud_det_init(uart1_rxd_io);
  635. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  636. i2c_master_start(cmd);
  637. i2c_master_write(cmd, data, 30, ACK_CHECK_DIS);
  638. i2c_master_stop(cmd);
  639. i2c_master_cmd_begin(i2c_num, cmd, 5000 / portTICK_PERIOD_MS);
  640. i2c_cmd_link_delete(cmd);
  641. i2c_scl_freq_cal();
  642. free(data);
  643. TEST_ESP_OK(i2c_driver_delete(i2c_num));
  644. }
  645. #endif // SOC_I2C_SUPPORT_SLAVE