test_i2c.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. /**
  2. * test environment UT_T2_I2C:
  3. * please prepare two ESP32-WROVER-KIT board.
  4. * Then connect GPIO18 and GPIO18, GPIO19 and GPIO19 between these two boards.
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "unity.h"
  9. #include "test_utils.h"
  10. #include "unity_config.h"
  11. #include "driver/i2c.h"
  12. #include "esp_attr.h"
  13. #include "esp_log.h"
  14. #include "soc/gpio_sig_map.h"
  15. #include "soc/i2c_struct.h"
  16. #include "soc/i2c_reg.h"
  17. #include "esp_system.h"
  18. #include "driver/pcnt.h"
  19. #define DATA_LENGTH 512 /*!<Data buffer length for test buffer*/
  20. #define RW_TEST_LENGTH 129 /*!<Data length for r/w test, any value from 0-DATA_LENGTH*/
  21. #define DELAY_TIME_BETWEEN_ITEMS_MS 1234 /*!< delay time between different test items */
  22. #define I2C_SLAVE_SCL_IO 19 /*!<gpio number for i2c slave clock */
  23. #define I2C_SLAVE_SDA_IO 18 /*!<gpio number for i2c slave data */
  24. #define I2C_SLAVE_NUM I2C_NUM_0 /*!<I2C port number for slave dev */
  25. #define I2C_SLAVE_TX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave tx buffer size */
  26. #define I2C_SLAVE_RX_BUF_LEN (2*DATA_LENGTH) /*!<I2C slave rx buffer size */
  27. #define I2C_MASTER_SCL_IO 19 /*!< gpio number for I2C master clock */
  28. #define I2C_MASTER_SDA_IO 18 /*!< gpio number for I2C master data */
  29. #define I2C_MASTER_NUM I2C_NUM_1 /*!< I2C port number for master dev */
  30. #define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
  31. #define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master do not need buffer */
  32. #define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
  33. #define ESP_SLAVE_ADDR 0x28 /*!< ESP32 slave address, you can set any 7bit value */
  34. #define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
  35. #define READ_BIT I2C_MASTER_READ /*!< I2C master read */
  36. #define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
  37. #define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
  38. #define ACK_VAL 0x0 /*!< I2C ack value */
  39. #define NACK_VAL 0x1 /*!< I2C nack value */
  40. #define PULSE_IO 19
  41. #define PCNT_INPUT_IO 4
  42. #define PCNT_CTRL_FLOATING_IO 5
  43. #define HIGHEST_LIMIT 10000
  44. #define LOWEST_LIMIT -10000
  45. static DRAM_ATTR i2c_dev_t *const I2C[I2C_NUM_MAX] = { &I2C0, &I2C1 };
  46. static esp_err_t i2c_master_write_slave(i2c_port_t i2c_num, uint8_t *data_wr, size_t size)
  47. {
  48. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  49. i2c_master_start(cmd);
  50. TEST_ESP_OK(i2c_master_write_byte(cmd, ( ESP_SLAVE_ADDR << 1 ) | WRITE_BIT, ACK_CHECK_EN));
  51. TEST_ESP_OK(i2c_master_write(cmd, data_wr, size, ACK_CHECK_EN));
  52. TEST_ESP_OK(i2c_master_stop(cmd));
  53. esp_err_t ret = i2c_master_cmd_begin(i2c_num, cmd, 5000 / portTICK_RATE_MS);
  54. i2c_cmd_link_delete(cmd);
  55. return ret;
  56. }
  57. // print the reading buffer
  58. static void disp_buf(uint8_t *buf, int len)
  59. {
  60. int i;
  61. for (i = 0; i < len; i++) {
  62. printf("%02x ", buf[i]);
  63. if (( i + 1 ) % 16 == 0) {
  64. printf("\n");
  65. }
  66. }
  67. printf("\n");
  68. }
  69. static i2c_config_t i2c_master_init()
  70. {
  71. i2c_config_t conf_master = {
  72. .mode = I2C_MODE_MASTER,
  73. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  74. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  75. .master.clk_speed = I2C_MASTER_FREQ_HZ,
  76. .sda_io_num = I2C_MASTER_SDA_IO,
  77. .scl_io_num = I2C_MASTER_SCL_IO,
  78. };
  79. return conf_master;
  80. }
  81. static i2c_config_t i2c_slave_init()
  82. {
  83. i2c_config_t conf_slave = {
  84. .mode = I2C_MODE_SLAVE,
  85. .sda_io_num = I2C_SLAVE_SDA_IO,
  86. .scl_io_num = I2C_SLAVE_SCL_IO,
  87. .sda_pullup_en = GPIO_PULLUP_ENABLE,
  88. .scl_pullup_en = GPIO_PULLUP_ENABLE,
  89. .slave.addr_10bit_en = 0,
  90. .slave.slave_addr = ESP_SLAVE_ADDR,
  91. };
  92. return conf_slave;
  93. }
  94. TEST_CASE("I2C config test", "[i2c]")
  95. {
  96. // master test
  97. i2c_config_t conf_master = i2c_master_init();
  98. gpio_pullup_t sda_pull_up_en[2] = {GPIO_PULLUP_DISABLE, GPIO_PULLUP_ENABLE};
  99. gpio_pullup_t scl_pull_up_en[2] = {GPIO_PULLUP_DISABLE, GPIO_PULLUP_ENABLE};
  100. for (int i = 0; i < 2; i++) {
  101. for (int j = 0; j < 2; j++) {
  102. conf_master.sda_pullup_en = sda_pull_up_en[i];
  103. conf_master.scl_pullup_en = scl_pull_up_en[j];
  104. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  105. TEST_ASSERT_EQUAL_INT32(I2C[I2C_MASTER_NUM]->ctr.ms_mode, 1);
  106. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  107. I2C_MASTER_RX_BUF_DISABLE,
  108. I2C_MASTER_TX_BUF_DISABLE, 0));
  109. TEST_ESP_OK(i2c_driver_delete(I2C_MASTER_NUM));
  110. }
  111. }
  112. // slave test
  113. i2c_config_t conf_slave = i2c_slave_init();
  114. for (int i = 0; i < 2; i++) {
  115. for (int j = 0; j < 2; j++) {
  116. conf_master.sda_pullup_en = sda_pull_up_en[i];
  117. conf_master.scl_pullup_en = scl_pull_up_en[j];
  118. conf_slave.sda_pullup_en = sda_pull_up_en[i];
  119. conf_slave.scl_pullup_en = scl_pull_up_en[j];
  120. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  121. TEST_ASSERT_EQUAL_INT32(I2C[I2C_SLAVE_NUM] -> ctr.ms_mode, 0);
  122. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  123. I2C_SLAVE_RX_BUF_LEN,
  124. I2C_SLAVE_TX_BUF_LEN, 0));
  125. TEST_ESP_OK(i2c_driver_delete(I2C_SLAVE_NUM));
  126. }
  127. }
  128. }
  129. TEST_CASE("I2C set and get period test", "[i2c]")
  130. {
  131. int high_period, low_period;
  132. i2c_config_t conf_master = i2c_master_init();
  133. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  134. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  135. I2C_MASTER_RX_BUF_DISABLE,
  136. I2C_MASTER_TX_BUF_DISABLE, 0));
  137. TEST_ESP_OK(i2c_set_period(I2C_MASTER_NUM, I2C_SCL_HIGH_PERIOD_V, I2C_SCL_HIGH_PERIOD_V));
  138. TEST_ESP_OK(i2c_get_period(I2C_MASTER_NUM, &high_period, &low_period));
  139. TEST_ASSERT_EQUAL_INT(I2C_SCL_HIGH_PERIOD_V, high_period);
  140. TEST_ASSERT_EQUAL_INT(I2C_SCL_HIGH_PERIOD_V, low_period);
  141. TEST_ASSERT_NOT_NULL((void *)i2c_set_period(I2C_MASTER_NUM, I2C_SCL_HIGH_PERIOD_V + 1, I2C_SCL_HIGH_PERIOD_V + 1));
  142. TEST_ESP_OK(i2c_set_period(I2C_MASTER_NUM, 300, 400));
  143. TEST_ESP_OK(i2c_get_period(I2C_MASTER_NUM, &high_period, &low_period));
  144. TEST_ASSERT_EQUAL_INT(300, high_period);
  145. TEST_ASSERT_EQUAL_INT(400, low_period);
  146. TEST_ESP_OK(i2c_driver_delete(I2C_MASTER_NUM));
  147. }
  148. TEST_CASE("I2C config FIFO test", "[i2c]")
  149. {
  150. TEST_ASSERT_BIT_LOW(1, I2C[I2C_SLAVE_NUM]->fifo_conf.tx_fifo_rst);
  151. TEST_ESP_OK(i2c_reset_tx_fifo(I2C_SLAVE_NUM));
  152. TEST_ASSERT_BIT_LOW(0, I2C[I2C_SLAVE_NUM]->fifo_conf.tx_fifo_rst);
  153. TEST_ESP_OK(i2c_reset_rx_fifo(I2C_SLAVE_NUM));
  154. TEST_ASSERT_BIT_LOW(0, I2C[I2C_SLAVE_NUM]->fifo_conf.rx_fifo_rst);
  155. }
  156. TEST_CASE("I2C timing test", "[i2c]")
  157. {
  158. int test_setup_time, test_data_time, test_stop_time, test_hold_time;
  159. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  160. i2c_config_t conf_master = i2c_master_init();
  161. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  162. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  163. I2C_MASTER_RX_BUF_DISABLE,
  164. I2C_MASTER_TX_BUF_DISABLE, 0));
  165. TEST_ESP_OK(i2c_set_start_timing(I2C_MASTER_NUM, 50, 60));
  166. TEST_ESP_OK(i2c_set_data_timing(I2C_MASTER_NUM, 80, 60));
  167. TEST_ESP_OK(i2c_set_stop_timing(I2C_MASTER_NUM, 100, 60));
  168. for (int i = 0; i < DATA_LENGTH; i++) {
  169. data_wr[i] = i;
  170. }
  171. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
  172. TEST_ESP_OK(i2c_get_start_timing(I2C_MASTER_NUM, &test_setup_time, &test_hold_time));
  173. TEST_ESP_OK(i2c_get_data_timing(I2C_MASTER_NUM, &test_data_time, &test_hold_time));
  174. TEST_ESP_OK(i2c_get_stop_timing(I2C_MASTER_NUM, &test_stop_time, &test_hold_time));
  175. TEST_ASSERT_EQUAL_INT32(50, test_setup_time);
  176. TEST_ASSERT_EQUAL_INT32(80, test_data_time);
  177. TEST_ASSERT_EQUAL_INT32(100, test_stop_time);
  178. TEST_ASSERT_EQUAL_INT32(60, test_hold_time);
  179. free(data_wr);
  180. i2c_driver_delete(I2C_MASTER_NUM);
  181. }
  182. TEST_CASE("I2C data mode test", "[i2c]")
  183. {
  184. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  185. i2c_trans_mode_t test_tx_trans_mode, test_rx_trans_mode;
  186. i2c_config_t conf_master = i2c_master_init();
  187. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  188. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  189. I2C_MASTER_RX_BUF_DISABLE,
  190. I2C_MASTER_TX_BUF_DISABLE, 0));
  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_set_data_mode(I2C_MASTER_NUM, I2C_DATA_MODE_LSB_FIRST, I2C_DATA_MODE_LSB_FIRST));
  196. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
  197. TEST_ASSERT_EQUAL_INT(1, I2C[I2C_MASTER_NUM]->ctr.rx_lsb_first);
  198. TEST_ASSERT_EQUAL_INT(1, I2C[I2C_MASTER_NUM]->ctr.tx_lsb_first);
  199. TEST_ESP_OK(i2c_get_data_mode(I2C_MASTER_NUM, &test_tx_trans_mode, &test_rx_trans_mode));
  200. TEST_ASSERT_EQUAL_INT(1, test_tx_trans_mode);
  201. TEST_ASSERT_EQUAL_INT(1, test_rx_trans_mode);
  202. free(data_wr);
  203. i2c_driver_delete(I2C_MASTER_NUM);
  204. }
  205. TEST_CASE("I2C driver memory leaking check", "[i2c]")
  206. {
  207. esp_err_t ret;
  208. i2c_config_t conf_slave = i2c_slave_init();
  209. ret = i2c_param_config(I2C_SLAVE_NUM, &conf_slave);
  210. TEST_ASSERT(ret == ESP_OK);
  211. int size = esp_get_free_heap_size();
  212. for (uint32_t i = 0; i <= 1000; i++) {
  213. ret = i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  214. I2C_SLAVE_RX_BUF_LEN,
  215. I2C_SLAVE_TX_BUF_LEN, 0);
  216. TEST_ASSERT(ret == ESP_OK);
  217. vTaskDelay(10 / portTICK_RATE_MS);
  218. i2c_driver_delete(I2C_SLAVE_NUM);
  219. TEST_ASSERT(ret == ESP_OK);
  220. }
  221. TEST_ASSERT_INT_WITHIN(100, size, esp_get_free_heap_size());
  222. }
  223. static void i2c_master_write_test()
  224. {
  225. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  226. int i;
  227. i2c_config_t conf_master = i2c_master_init();
  228. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  229. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  230. I2C_MASTER_RX_BUF_DISABLE,
  231. I2C_MASTER_TX_BUF_DISABLE, 0));
  232. unity_wait_for_signal("i2c slave init finish");
  233. unity_send_signal("master write");
  234. for (i = 0; i < DATA_LENGTH / 2; i++) {
  235. data_wr[i] = i;
  236. }
  237. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, DATA_LENGTH / 2);
  238. disp_buf(data_wr, i + 1);
  239. free(data_wr);
  240. unity_wait_for_signal("ready to delete");
  241. TEST_ESP_OK(i2c_driver_delete(I2C_MASTER_NUM));
  242. }
  243. static void i2c_slave_read_test()
  244. {
  245. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
  246. int size_rd = 0;
  247. int len = 0;
  248. i2c_config_t conf_slave = i2c_slave_init();
  249. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  250. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  251. I2C_SLAVE_RX_BUF_LEN,
  252. I2C_SLAVE_TX_BUF_LEN, 0));
  253. unity_send_signal("i2c slave init finish");
  254. unity_wait_for_signal("master write");
  255. while (1) {
  256. len = i2c_slave_read_buffer( I2C_SLAVE_NUM, data_rd + size_rd, DATA_LENGTH, 10000 / portTICK_RATE_MS);
  257. if (len == 0) {
  258. break;
  259. }
  260. size_rd += len;
  261. }
  262. disp_buf(data_rd, size_rd);
  263. for (int i = 0; i < size_rd; i++) {
  264. TEST_ASSERT(data_rd[i] == i);
  265. }
  266. free(data_rd);
  267. unity_send_signal("ready to delete");
  268. TEST_ESP_OK(i2c_driver_delete(I2C_SLAVE_NUM));
  269. }
  270. 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);
  271. static void master_read_slave_test()
  272. {
  273. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
  274. memset(data_rd, 0, DATA_LENGTH);
  275. i2c_config_t conf_master = i2c_master_init();
  276. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  277. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  278. I2C_MASTER_RX_BUF_DISABLE,
  279. I2C_MASTER_TX_BUF_DISABLE, 0));
  280. unity_wait_for_signal("i2c slave init finish");
  281. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  282. i2c_master_start(cmd);
  283. i2c_master_write_byte(cmd, ( ESP_SLAVE_ADDR << 1 ) | READ_BIT, ACK_CHECK_EN);
  284. unity_send_signal("slave write");
  285. unity_wait_for_signal("master read");
  286. i2c_master_read(cmd, data_rd, RW_TEST_LENGTH-1, ACK_VAL);
  287. i2c_master_read_byte(cmd, data_rd + RW_TEST_LENGTH-1, NACK_VAL);
  288. i2c_master_stop(cmd);
  289. i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 5000 / portTICK_RATE_MS);
  290. i2c_cmd_link_delete(cmd);
  291. vTaskDelay(100 / portTICK_RATE_MS);
  292. for (int i = 0; i < RW_TEST_LENGTH; i++) {
  293. printf("%d\n", data_rd[i]);
  294. TEST_ASSERT(data_rd[i]==i);
  295. }
  296. free(data_rd);
  297. unity_send_signal("ready to delete");
  298. i2c_driver_delete(I2C_MASTER_NUM);
  299. }
  300. static void slave_write_buffer_test()
  301. {
  302. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  303. int size_rd;
  304. i2c_config_t conf_slave = i2c_slave_init();
  305. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  306. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  307. I2C_SLAVE_RX_BUF_LEN,
  308. I2C_SLAVE_TX_BUF_LEN, 0));
  309. unity_send_signal("i2c slave init finish");
  310. unity_wait_for_signal("slave write");
  311. for (int i = 0; i < DATA_LENGTH / 2; i++) {
  312. data_wr[i] = i;
  313. }
  314. size_rd = i2c_slave_write_buffer(I2C_SLAVE_NUM, data_wr, RW_TEST_LENGTH, 2000 / portTICK_RATE_MS);
  315. disp_buf(data_wr, size_rd);
  316. unity_send_signal("master read");
  317. unity_wait_for_signal("ready to delete");
  318. free(data_wr);
  319. i2c_driver_delete(I2C_SLAVE_NUM);
  320. }
  321. 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);
  322. static void i2c_master_write_read_test()
  323. {
  324. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
  325. memset(data_rd, 0, DATA_LENGTH);
  326. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  327. i2c_config_t conf_master = i2c_master_init();
  328. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  329. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  330. I2C_MASTER_RX_BUF_DISABLE,
  331. I2C_MASTER_TX_BUF_DISABLE, 0));
  332. unity_wait_for_signal("i2c slave init finish");
  333. i2c_cmd_handle_t cmd = i2c_cmd_link_create();
  334. i2c_master_start(cmd);
  335. i2c_master_write_byte(cmd, ( ESP_SLAVE_ADDR << 1 ) | READ_BIT, ACK_CHECK_EN);
  336. unity_send_signal("slave write");
  337. unity_wait_for_signal("master read and write");
  338. i2c_master_read(cmd, data_rd, RW_TEST_LENGTH, ACK_VAL);
  339. i2c_master_read_byte(cmd, data_rd + RW_TEST_LENGTH, NACK_VAL);
  340. i2c_master_stop(cmd);
  341. i2c_master_cmd_begin(I2C_MASTER_NUM, cmd, 5000 / portTICK_RATE_MS);
  342. i2c_cmd_link_delete(cmd);
  343. vTaskDelay(100 / portTICK_RATE_MS);
  344. disp_buf(data_rd, RW_TEST_LENGTH);
  345. for (int i = 0; i < RW_TEST_LENGTH; i++) {
  346. TEST_ASSERT(data_rd[i] == i/2);
  347. }
  348. for (int i = 0; i < DATA_LENGTH; i++) {
  349. data_wr[i] = i % 3;
  350. }
  351. vTaskDelay(100 / portTICK_RATE_MS);
  352. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
  353. free(data_wr);
  354. free(data_rd);
  355. unity_send_signal("slave read");
  356. unity_wait_for_signal("ready to delete");
  357. i2c_driver_delete(I2C_MASTER_NUM);
  358. }
  359. static void i2c_slave_read_write_test()
  360. {
  361. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH);
  362. memset(data_rd, 0, DATA_LENGTH);
  363. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  364. int size_rd;
  365. i2c_config_t conf_slave = i2c_slave_init();
  366. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  367. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  368. I2C_SLAVE_RX_BUF_LEN,
  369. I2C_SLAVE_TX_BUF_LEN, 0));
  370. unity_send_signal("i2c slave init finish");
  371. unity_wait_for_signal("slave write");
  372. for (int i = 0; i < DATA_LENGTH / 2; i++) {
  373. data_wr[i] = i/2;
  374. }
  375. size_rd = i2c_slave_write_buffer(I2C_SLAVE_NUM, data_wr, RW_TEST_LENGTH, 2000 / portTICK_RATE_MS);
  376. disp_buf(data_wr, size_rd);
  377. unity_send_signal("master read and write");
  378. unity_wait_for_signal("slave read");
  379. size_rd = i2c_slave_read_buffer( I2C_SLAVE_NUM, data_rd, RW_TEST_LENGTH, 1000 / portTICK_RATE_MS);
  380. printf("slave read data is:\n");
  381. disp_buf(data_rd, size_rd);
  382. for (int i = 0; i < RW_TEST_LENGTH; i++) {
  383. TEST_ASSERT(data_rd[i] == i % 3);
  384. }
  385. free(data_wr);
  386. free(data_rd);
  387. unity_send_signal("ready to delete");
  388. i2c_driver_delete(I2C_SLAVE_NUM);
  389. }
  390. 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);
  391. static void i2c_master_repeat_write()
  392. {
  393. uint8_t *data_wr = (uint8_t *) malloc(DATA_LENGTH);
  394. int times = 3;
  395. i2c_config_t conf_master = i2c_master_init();
  396. TEST_ESP_OK(i2c_param_config(I2C_MASTER_NUM, &conf_master));
  397. TEST_ESP_OK(i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER,
  398. I2C_MASTER_RX_BUF_DISABLE,
  399. I2C_MASTER_TX_BUF_DISABLE, 0));
  400. unity_wait_for_signal("i2c slave init finish");
  401. for (int j = 0; j < times; j++) {
  402. for (int i = 0; i < DATA_LENGTH; i++) {
  403. data_wr[i] = j + i;
  404. }
  405. i2c_master_write_slave(I2C_MASTER_NUM, data_wr, RW_TEST_LENGTH);
  406. disp_buf(data_wr, RW_TEST_LENGTH);
  407. }
  408. free(data_wr);
  409. unity_send_signal("master write");
  410. unity_wait_for_signal("ready to delete");
  411. i2c_driver_delete(I2C_MASTER_NUM);
  412. }
  413. static void i2c_slave_repeat_read()
  414. {
  415. int size_rd = 0;
  416. int times = 3;
  417. uint8_t *data_rd = (uint8_t *) malloc(DATA_LENGTH * 3);
  418. i2c_config_t conf_slave = i2c_slave_init();
  419. TEST_ESP_OK(i2c_param_config( I2C_SLAVE_NUM, &conf_slave));
  420. TEST_ESP_OK(i2c_driver_install(I2C_SLAVE_NUM, I2C_MODE_SLAVE,
  421. I2C_SLAVE_RX_BUF_LEN,
  422. I2C_SLAVE_TX_BUF_LEN, 0));
  423. unity_send_signal("i2c slave init finish");
  424. unity_wait_for_signal("master write");
  425. while (1) {
  426. int len = i2c_slave_read_buffer( I2C_SLAVE_NUM, data_rd + size_rd, RW_TEST_LENGTH * 3, 10000 / portTICK_RATE_MS);
  427. if (len == 0) {
  428. break;
  429. }
  430. size_rd += len;
  431. }
  432. disp_buf(data_rd, size_rd);
  433. for (int j = 0; j < times; j++) {
  434. for (int i = 0; i < RW_TEST_LENGTH; i++) {
  435. printf("data: %d, %d\n", data_rd[j * RW_TEST_LENGTH + i], (i % 129 + j));
  436. TEST_ASSERT(data_rd[j * RW_TEST_LENGTH + i] == (i % 129 + j));
  437. }
  438. }
  439. free(data_rd);
  440. unity_send_signal("ready to delete");
  441. i2c_driver_delete(I2C_SLAVE_NUM);
  442. }
  443. TEST_CASE_MULTIPLE_DEVICES("I2C repeat write test", "[i2c][test_env=UT_T2_I2C][timeout=150]", i2c_master_repeat_write, i2c_slave_repeat_read);