test_i2c.c 21 KB

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