test_i2c.c 26 KB

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