test_sdio.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. // Copyright 2019 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "unity.h"
  15. #include "esp_serial_slave_link/essl_sdio.h"
  16. #include "driver/sdspi_host.h"
  17. #include "test_utils.h"
  18. #include "param_test.h"
  19. #include "esp_log.h"
  20. #include "driver/spi_common.h"
  21. #include "soc/soc_caps.h"
  22. #include "ccomp_timer.h"
  23. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3)
  24. #if SOC_SDMMC_HOST_SUPPORTED && SOC_SDIO_SLAVE_SUPPORTED
  25. #include "driver/sdio_slave.h"
  26. #include "driver/sdmmc_host.h"
  27. #define TIMEOUT_MAX UINT32_MAX
  28. #define INT_MASK_ALL 0xff
  29. #define SDIO_SLAVE_QUEUE_SIZE 20
  30. #define RX_BUFFER_SIZE 2048
  31. #define RX_BUFFER_NUM 10
  32. #define TX_BUFFER_SIZE 2048
  33. #define REG_ADDR_MAX 60
  34. //the test should run accross the boundary, i.e. over 0x100000 bytes.
  35. //TEST_CNT > 512
  36. #define TEST_CNT 10000
  37. #define TEST_SDSPI_HOST HSPI_HOST
  38. #define TEST_SDSPI_DMACHAN 1
  39. #define TEST_RESET_DATA_LEN 10
  40. #ifndef MIN
  41. #define MIN(a, b) ((a)<(b)? (a): (b))
  42. #endif
  43. typedef enum {
  44. SDIO_1BIT = 0,
  45. SDIO_4BIT = 1,
  46. SDIO_SPI = 2,
  47. } sdio_mode_t;
  48. typedef void (*sdio_test_func)(essl_handle_t handle);
  49. typedef struct {
  50. const char test_name[16];
  51. sdio_mode_t sdio_mode;
  52. uint32_t freq;
  53. bool check_data;
  54. bool packet_mode;
  55. } sdio_test_config_t;
  56. sdio_test_config_t test_cfg_array[] = {
  57. //the first item will be the default config used by all tests
  58. {
  59. .test_name = "HS4B",
  60. .sdio_mode = SDIO_4BIT,
  61. .freq = SDMMC_FREQ_HIGHSPEED,
  62. .check_data = true,
  63. },
  64. {
  65. .test_name = "HS1B",
  66. .sdio_mode = SDIO_1BIT,
  67. .freq = SDMMC_FREQ_HIGHSPEED,
  68. .check_data = true,
  69. },
  70. {
  71. .test_name = "SPI",
  72. .sdio_mode = SDIO_SPI,
  73. .freq = SDMMC_FREQ_HIGHSPEED,
  74. .check_data = true,
  75. },
  76. //the performance test is only done when psram is not enabled
  77. #if !CONFIG_SPIRAM && !CONFIG_FREERTOS_CHECK_PORT_CRITICAL_COMPLIANCE
  78. {
  79. .test_name = "HS4B (perf)",
  80. .sdio_mode = SDIO_4BIT,
  81. .freq = SDMMC_FREQ_HIGHSPEED,
  82. },
  83. {
  84. .test_name = "HS1B (perf)",
  85. .sdio_mode = SDIO_1BIT,
  86. .freq = SDMMC_FREQ_HIGHSPEED,
  87. },
  88. {
  89. .test_name = "SPI (perf)",
  90. .sdio_mode = SDIO_SPI,
  91. .freq = SDMMC_FREQ_HIGHSPEED,
  92. },
  93. #endif
  94. };
  95. sdio_test_config_t packet_config = {
  96. .test_name = "HS4B packet",
  97. .sdio_mode = SDIO_4BIT,
  98. .freq = SDMMC_FREQ_HIGHSPEED,
  99. .check_data = true,
  100. .packet_mode = true,
  101. };
  102. const sdio_test_config_t* default_config = &test_cfg_array[0];
  103. #define TEST_SIZE (sizeof(test_cfg_array)/sizeof(sdio_test_config_t))
  104. static const char MASTER_TAG[] = "master";
  105. static const char SLAVE_TAG[] = "slave";
  106. /*******************************************************************************
  107. * Master
  108. ******************************************************************************/
  109. static sdmmc_card_t s_card;
  110. typedef void (*test_func_t)(essl_handle_t handle, const sdio_test_config_t* config);
  111. static void init_sdmmc_host(void);
  112. static void init_essl(essl_handle_t *out_handle, const sdio_test_config_t *conf);
  113. static void deinit_essl(essl_handle_t handle, const sdio_test_config_t *conf);
  114. static void test_framework_master(test_func_t test_func, const sdio_test_config_t* config)
  115. {
  116. ESP_LOGI(MASTER_TAG, "### Testing %s... ####", config->test_name);
  117. essl_handle_t handle;
  118. esp_err_t err;
  119. init_essl(&handle, config);
  120. err = essl_init(handle, TIMEOUT_MAX);
  121. TEST_ESP_OK(err);
  122. (*test_func)(handle, config);
  123. deinit_essl(handle, config);
  124. }
  125. static void init_sdmmc_host(void)
  126. {
  127. esp_err_t err;
  128. sdmmc_slot_config_t slot_config = SDMMC_SLOT_CONFIG_DEFAULT();
  129. err = sdmmc_host_init();
  130. TEST_ESP_OK(err);
  131. err = sdmmc_host_init_slot(SDMMC_HOST_SLOT_1, &slot_config);
  132. TEST_ESP_OK(err);
  133. }
  134. static void init_essl(essl_handle_t *out_handle, const sdio_test_config_t *conf)
  135. {
  136. sdmmc_host_t config;
  137. esp_err_t err;
  138. spi_bus_config_t bus_config;
  139. /* Probe */
  140. switch (conf->sdio_mode) {
  141. case SDIO_4BIT:
  142. ESP_LOGI(MASTER_TAG, "Probe using SD 4-bit...\n");
  143. config = (sdmmc_host_t)SDMMC_HOST_DEFAULT();
  144. config.flags = SDMMC_HOST_FLAG_4BIT;
  145. config.max_freq_khz = conf->freq;
  146. init_sdmmc_host();
  147. break;
  148. case SDIO_1BIT:
  149. ESP_LOGI(MASTER_TAG, "Probe using SD 1-bit...\n");
  150. config = (sdmmc_host_t)SDMMC_HOST_DEFAULT();
  151. config.flags = SDMMC_HOST_FLAG_1BIT;
  152. config.max_freq_khz = conf->freq;
  153. init_sdmmc_host();
  154. break;
  155. case SDIO_SPI:
  156. bus_config = (spi_bus_config_t) {
  157. .mosi_io_num = SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_CMD,
  158. .miso_io_num = SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_D0,
  159. .sclk_io_num = SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_CLK,
  160. .quadwp_io_num = -1,
  161. .quadhd_io_num = -1,
  162. };
  163. err = spi_bus_initialize(TEST_SDSPI_HOST, &bus_config, TEST_SDSPI_DMACHAN);
  164. TEST_ESP_OK(err);
  165. sdspi_device_config_t device_config = SDSPI_DEVICE_CONFIG_DEFAULT();
  166. device_config.host_id = TEST_SDSPI_HOST;
  167. device_config.gpio_cs = SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_D3;
  168. device_config.gpio_int= SDIO_SLAVE_SLOT1_IOMUX_PIN_NUM_D1;
  169. err = gpio_install_isr_service(0);
  170. TEST_ASSERT(err == ESP_OK || err == ESP_ERR_INVALID_STATE);
  171. sdspi_dev_handle_t sdspi_handle;
  172. err = sdspi_host_init();
  173. TEST_ESP_OK(err);
  174. err = sdspi_host_init_device(&device_config, &sdspi_handle);
  175. TEST_ESP_OK(err);
  176. ESP_LOGI(MASTER_TAG, "Probe using SPI...\n");
  177. config = (sdmmc_host_t)SDSPI_HOST_DEFAULT();
  178. config.slot = sdspi_handle;
  179. break;
  180. }
  181. sdmmc_card_t *card = &s_card;
  182. //wait for at least 5 seconds
  183. int retry_times = 5;
  184. do {
  185. if (sdmmc_card_init(&config, card) == ESP_OK) {
  186. break;
  187. }
  188. ESP_LOGW(MASTER_TAG, "slave init failed, retry...");
  189. vTaskDelay(1000 / portTICK_PERIOD_MS);
  190. } while (--retry_times);
  191. TEST_ASSERT_MESSAGE(retry_times != 0, "Initializing slave failed.");
  192. essl_sdio_config_t ser_config = {
  193. .card = card,
  194. .recv_buffer_size = RX_BUFFER_SIZE,
  195. };
  196. err = essl_sdio_init_dev(out_handle, &ser_config);
  197. TEST_ESP_OK(err);
  198. err = essl_init(*out_handle, TIMEOUT_MAX);
  199. TEST_ESP_OK(err);
  200. }
  201. static void deinit_essl(essl_handle_t handle, const sdio_test_config_t *conf)
  202. {
  203. esp_err_t err;
  204. essl_sdio_deinit_dev(handle);
  205. if (conf->sdio_mode == SDIO_SPI) {
  206. gpio_uninstall_isr_service();
  207. err = sdspi_host_deinit();
  208. TEST_ESP_OK(err);
  209. err = spi_bus_free(TEST_SDSPI_HOST);
  210. TEST_ESP_OK(err);
  211. } else {
  212. err = sdmmc_host_deinit();
  213. TEST_ESP_OK(err);
  214. }
  215. }
  216. static void send_finish_test(essl_handle_t handle)
  217. {
  218. //the slave needs a signal to quite the test
  219. essl_send_slave_intr(handle, BIT(7), TIMEOUT_MAX);
  220. }
  221. static void test_int(essl_handle_t handle, const sdio_test_config_t* config)
  222. {
  223. esp_err_t err;
  224. err = essl_set_intr_ena(handle, INT_MASK_ALL, TIMEOUT_MAX);
  225. TEST_ESP_OK(err);
  226. err = essl_wait_int(handle, 0);
  227. TEST_ASSERT_EQUAL_HEX(ESP_ERR_TIMEOUT, err);
  228. //tests all 8 interrupts of the slave, in which int 7 is used to terminate the test on the slave.
  229. for (int i = 0; i < 8; i ++) {
  230. uint32_t int_st;
  231. err = essl_send_slave_intr(handle, BIT(i), TIMEOUT_MAX);
  232. TEST_ESP_OK(err);
  233. //the slave should return interrupt with the same bit in 10 ms
  234. err = essl_wait_int(handle, 10);
  235. TEST_ESP_OK(err);
  236. err = essl_get_intr(handle, NULL, &int_st, TIMEOUT_MAX);
  237. TEST_ESP_OK(err);
  238. //check and clear the returned interrupt
  239. TEST_ASSERT_EQUAL_HEX(BIT(i), int_st);
  240. err = essl_clear_intr(handle, int_st, TIMEOUT_MAX);
  241. TEST_ESP_OK(err);
  242. }
  243. }
  244. static void test_sdio_interrupt_master(void)
  245. {
  246. test_framework_master(test_int, default_config);
  247. }
  248. static void test_reg(essl_handle_t handle, const sdio_test_config_t* config)
  249. {
  250. esp_err_t err;
  251. uint8_t data[REG_ADDR_MAX];
  252. srand(850);
  253. //initialize the buffer
  254. for (int i = 0; i < REG_ADDR_MAX; i++) {
  255. data[i] = rand();
  256. err = essl_write_reg(handle, i, data[i], NULL, 10);
  257. TEST_ESP_OK(err);
  258. }
  259. for (int i = 0; i < 512; i++) {
  260. //randomly write one
  261. int offset = rand() % REG_ADDR_MAX;
  262. uint8_t data_write = rand();
  263. data[offset] = data_write;
  264. err = essl_write_reg(handle, offset, data_write, NULL, 10);
  265. TEST_ESP_OK(err);
  266. //randomly read another one and compare
  267. offset = rand() % REG_ADDR_MAX;
  268. uint8_t data_read;
  269. err = essl_read_reg(handle, offset, &data_read, 10);
  270. TEST_ESP_OK(err);
  271. TEST_ASSERT_EQUAL_HEX8(data[offset], data_read);
  272. }
  273. send_finish_test(handle);
  274. }
  275. static void test_sdio_reg_master(void)
  276. {
  277. test_framework_master(test_reg, default_config);
  278. }
  279. static uint8_t tx_buffer[TX_BUFFER_SIZE*2];
  280. static uint8_t rcv_buffer[RX_BUFFER_SIZE*RX_BUFFER_NUM];
  281. static void init_tx_buffer(void)
  282. {
  283. srand(776);
  284. for (int i = 0; i < sizeof(tx_buffer); i++) {
  285. tx_buffer[i] = rand();
  286. }
  287. }
  288. static void get_master_send_data(int offset, uint8_t** out_start, int* out_len)
  289. {
  290. int page_cnt = offset / TX_BUFFER_SIZE;
  291. int offset_in_page = offset % TX_BUFFER_SIZE;
  292. srand(page_cnt);
  293. int page_offset = (rand() % (sizeof(tx_buffer) - (TX_BUFFER_SIZE) + 1)) & (~3);
  294. *out_start = &tx_buffer[page_offset + offset_in_page];
  295. *out_len = TX_BUFFER_SIZE - offset_in_page;
  296. }
  297. static void log_performance_tohost(uint32_t speed, const sdio_test_config_t* config)
  298. {
  299. if (!config->check_data) {
  300. switch (config->sdio_mode) {
  301. case SDIO_4BIT:
  302. TEST_PERFORMANCE_GREATER_THAN(SDIO_THROUGHPUT_KBSEC_TOHOST_4BIT, "%d", speed);
  303. break;
  304. case SDIO_1BIT:
  305. TEST_PERFORMANCE_GREATER_THAN(SDIO_THROUGHPUT_KBSEC_TOHOST_1BIT, "%d", speed);
  306. break;
  307. case SDIO_SPI:
  308. TEST_PERFORMANCE_GREATER_THAN(SDIO_THROUGHPUT_KBSEC_TOHOST_SPI, "%d", speed);
  309. break;
  310. }
  311. }
  312. }
  313. static void test_tp_tohost_master(essl_handle_t handle, const sdio_test_config_t* config)
  314. {
  315. esp_err_t err;
  316. int expected_length = TEST_CNT * TX_BUFFER_SIZE;
  317. int recv_size = 4096;
  318. init_tx_buffer();
  319. //wait for the slave to get ready
  320. vTaskDelay(20);
  321. int remain_length = expected_length;
  322. int offset = 0;
  323. // Two counters are used. The `esp_timer_get_time()` is for the typical time, and the
  324. // `ccomp_timer` is for performance test to reduce influence caused by cache miss.
  325. int64_t pre_us = esp_timer_get_time();
  326. err = ccomp_timer_start();
  327. assert(err == ESP_OK);
  328. // though the flow is the same, the check of config->check_data influences the throughput much, put it outside
  329. if (config->check_data) {
  330. do {
  331. size_t rcv_len;
  332. err = essl_get_packet(handle, rcv_buffer, recv_size, &rcv_len, TIMEOUT_MAX);
  333. TEST_ASSERT(err == ESP_OK || err == ESP_ERR_NOT_FINISHED);
  334. TEST_ASSERT_LESS_OR_EQUAL(remain_length, rcv_len);
  335. //compare until all received data are used
  336. int compared_len = 0;
  337. do {
  338. //get the expected master sent data, there may be several segments, so get and compare
  339. //several times
  340. uint8_t* cmp_start;
  341. int seg_len;
  342. get_master_send_data(offset, &cmp_start, &seg_len);
  343. int cmp_len = MIN(rcv_len-compared_len, seg_len);
  344. TEST_ASSERT_EQUAL_HEX8_ARRAY(cmp_start, &rcv_buffer[compared_len], cmp_len);
  345. compared_len += cmp_len;
  346. offset += cmp_len;
  347. } while (compared_len < rcv_len);
  348. remain_length -= rcv_len;
  349. } while (remain_length > 0);
  350. } else {
  351. do {
  352. size_t rcv_len;
  353. err = essl_get_packet(handle, rcv_buffer, recv_size, &rcv_len, TIMEOUT_MAX);
  354. TEST_ASSERT(err == ESP_OK || err == ESP_ERR_NOT_FINISHED);
  355. TEST_ASSERT_LESS_OR_EQUAL(remain_length, rcv_len);
  356. offset += rcv_len;
  357. remain_length -= rcv_len;
  358. } while (remain_length > 0);
  359. }
  360. int64_t c_time_ms = ccomp_timer_stop()/1000;
  361. int64_t end_us = esp_timer_get_time();
  362. uint32_t total_time_ms = (end_us - pre_us)/1000;
  363. ESP_LOGI(MASTER_TAG, "test done, total time: %d ms (%d ms compensated), bytes transferred: %d", total_time_ms, (int)c_time_ms, expected_length);
  364. uint32_t throughput_byte_per_ms = expected_length / c_time_ms;
  365. ESP_LOGI(MASTER_TAG, "Throughput: compensated %.2lf MB/s, typical %.2lf MB/s",
  366. throughput_byte_per_ms/1000., expected_length/(total_time_ms*1000.));
  367. log_performance_tohost(throughput_byte_per_ms, config);
  368. send_finish_test(handle);
  369. }
  370. static void log_performance_frhost(uint32_t speed, const sdio_test_config_t* config)
  371. {
  372. if (!config->check_data) {
  373. switch (config->sdio_mode) {
  374. case SDIO_4BIT:
  375. TEST_PERFORMANCE_GREATER_THAN(SDIO_THROUGHPUT_KBSEC_FRHOST_4BIT, "%d", speed);
  376. break;
  377. case SDIO_1BIT:
  378. TEST_PERFORMANCE_GREATER_THAN(SDIO_THROUGHPUT_KBSEC_FRHOST_1BIT, "%d", speed);
  379. break;
  380. case SDIO_SPI:
  381. TEST_PERFORMANCE_GREATER_THAN(SDIO_THROUGHPUT_KBSEC_FRHOST_SPI, "%d", speed);
  382. break;
  383. }
  384. }
  385. }
  386. static void test_tp_frhost_master(essl_handle_t handle, const sdio_test_config_t* config)
  387. {
  388. esp_err_t err;
  389. int expected_length = TEST_CNT * TX_BUFFER_SIZE;
  390. init_tx_buffer();
  391. //wait for the slave to get ready
  392. vTaskDelay(20);
  393. int remain_length = expected_length;
  394. int offset = 0;
  395. // Two counters are used. The `esp_timer_get_time()` is for the typical time, and the
  396. // `ccomp_timer` is for performance test to reduce influence caused by cache miss.
  397. int64_t pre_us = esp_timer_get_time();
  398. err = ccomp_timer_start();
  399. assert(err == ESP_OK);
  400. do {
  401. int send_len;
  402. uint8_t* send_start;
  403. get_master_send_data(offset, &send_start, &send_len);
  404. TEST_ASSERT_EQUAL(TX_BUFFER_SIZE, send_len);
  405. err = essl_send_packet(handle, send_start, send_len, TIMEOUT_MAX);
  406. TEST_ASSERT(err == ESP_OK);
  407. remain_length -= send_len;
  408. offset += send_len;
  409. } while (remain_length > 0);
  410. int64_t c_time_ms = ccomp_timer_stop()/1000;
  411. int64_t end_us = esp_timer_get_time();
  412. uint32_t total_time_ms = (end_us - pre_us)/1000;
  413. ESP_LOGI(MASTER_TAG, "test done, total time: %d ms (%d ms compensated), bytes transferred: %d", total_time_ms, (int)c_time_ms, expected_length);
  414. uint32_t throughput_byte_per_ms = expected_length / c_time_ms;
  415. ESP_LOGI(MASTER_TAG, "Throughput: compensated %.2lf MB/s, typical %.2lf MB/s",
  416. throughput_byte_per_ms/1000., expected_length/(total_time_ms*1000.));
  417. log_performance_frhost(throughput_byte_per_ms, config);
  418. send_finish_test(handle);
  419. }
  420. void test_reset_master(essl_handle_t handle, const sdio_test_config_t* config)
  421. {
  422. init_tx_buffer();
  423. //wait for the slave to stop, reset and start again
  424. vTaskDelay(10);
  425. for (int i = 0; i < 10; i++) {
  426. WORD_ALIGNED_ATTR uint8_t buffer[TEST_RESET_DATA_LEN];
  427. size_t read_len;
  428. esp_err_t err = essl_get_packet(handle, buffer, TEST_RESET_DATA_LEN, &read_len, portMAX_DELAY);
  429. if (err == ESP_ERR_NOT_FINISHED) {
  430. TEST_ASSERT_LESS_THAN(10, i);
  431. err = ESP_OK;
  432. }
  433. TEST_ESP_OK(err);
  434. TEST_ASSERT_EQUAL(TEST_RESET_DATA_LEN, read_len);
  435. TEST_ASSERT_EQUAL_HEX8_ARRAY(tx_buffer + 4*i, buffer, read_len);
  436. }
  437. for (int i = 0; i < 10; i++) {
  438. esp_err_t err = essl_send_packet(handle, tx_buffer + i * 8, TEST_RESET_DATA_LEN, portMAX_DELAY);
  439. TEST_ESP_OK(err);
  440. }
  441. send_finish_test(handle);
  442. }
  443. void test_sdio_reset_master(void)
  444. {
  445. test_framework_master(test_reset_master, &packet_config);
  446. }
  447. /*******************************************************************************
  448. * Slave
  449. ******************************************************************************/
  450. typedef struct {
  451. int queued_cnt;
  452. bool s_finished;
  453. } slave_context_t;
  454. typedef void (*test_func_slave_t)(slave_context_t *context, const sdio_test_config_t* config);
  455. static slave_context_t slave_context;
  456. static void event_cb(uint8_t event)
  457. {
  458. ESP_EARLY_LOGI(SLAVE_TAG, "event: %d", event);
  459. sdio_slave_send_host_int(event);
  460. if (event == 7) slave_context.s_finished = true;
  461. }
  462. static void wait_for_finish(slave_context_t *ctx)
  463. {
  464. while (!ctx->s_finished) {
  465. vTaskDelay(10);
  466. }
  467. //wait for host to read the respond from slave
  468. vTaskDelay(10);
  469. }
  470. static void test_framework_slave(test_func_slave_t test_func, const sdio_test_config_t* config)
  471. {
  472. slave_context.s_finished = false;
  473. esp_err_t err;
  474. sdio_slave_config_t slave_config = {
  475. .sending_mode = (config->packet_mode? SDIO_SLAVE_SEND_PACKET: SDIO_SLAVE_SEND_STREAM),
  476. .send_queue_size = SDIO_SLAVE_QUEUE_SIZE,
  477. .recv_buffer_size = RX_BUFFER_SIZE,
  478. .event_cb = event_cb,
  479. };
  480. err = sdio_slave_initialize(&slave_config);
  481. TEST_ESP_OK(err);
  482. err = sdio_slave_start();
  483. TEST_ESP_OK(err);
  484. ESP_LOGI(SLAVE_TAG, "slave ready");
  485. test_func(&slave_context, config);
  486. sdio_slave_stop();
  487. sdio_slave_deinit();
  488. }
  489. static void test_int_slave(slave_context_t* ctx, const sdio_test_config_t* config)
  490. {
  491. wait_for_finish(ctx);
  492. }
  493. static void test_sdio_interrupt_slave(void)
  494. {
  495. test_framework_slave(test_int_slave, default_config);
  496. }
  497. static void test_tp_tohost_slave(slave_context_t* ctx, const sdio_test_config_t* config)
  498. {
  499. #define QUEUE_FULL() (ctx->queued_cnt == SDIO_SLAVE_QUEUE_SIZE)
  500. #define QUEUE_EMPTY() (ctx->queued_cnt == 0)
  501. init_tx_buffer();
  502. esp_err_t err;
  503. int offset = 0;
  504. for (int i = 0; i < TEST_CNT; i++) {
  505. do {
  506. void* arg;
  507. //when the queue is full, do a blocking wait for 10ms, otherwise non-blocking
  508. err = sdio_slave_send_get_finished(&arg, QUEUE_FULL()? 1: 0);
  509. if (err == ESP_OK) {
  510. ctx->queued_cnt --;
  511. continue;
  512. }
  513. TEST_ASSERT_EQUAL(ESP_ERR_TIMEOUT, err);
  514. } while (QUEUE_FULL());
  515. uint8_t* start;
  516. int send_len;
  517. get_master_send_data(offset, &start, &send_len);
  518. TEST_ASSERT_EQUAL(TX_BUFFER_SIZE, send_len);
  519. err = sdio_slave_send_queue(start, send_len, NULL, portMAX_DELAY);
  520. TEST_ESP_OK(err);
  521. ctx->queued_cnt++;
  522. offset += TX_BUFFER_SIZE;
  523. }
  524. while (!QUEUE_EMPTY()) {
  525. void* arg;
  526. err = sdio_slave_send_get_finished(&arg, portMAX_DELAY);
  527. TEST_ESP_OK(err);
  528. ctx->queued_cnt--;
  529. }
  530. wait_for_finish(ctx);
  531. }
  532. static void slave_parepare_recv_buffer(void)
  533. {
  534. for (int i = 0; i < RX_BUFFER_NUM; i++) {
  535. sdio_slave_buf_handle_t buf_handle = sdio_slave_recv_register_buf(&rcv_buffer[i*RX_BUFFER_SIZE]);
  536. esp_err_t err = sdio_slave_recv_load_buf(buf_handle);
  537. TEST_ESP_OK(err);
  538. }
  539. }
  540. static void test_tp_frhost_slave(slave_context_t *ctx, const sdio_test_config_t* config)
  541. {
  542. esp_err_t err;
  543. init_tx_buffer();
  544. slave_parepare_recv_buffer();
  545. int offset = 0;
  546. for (int i = 0; i < TEST_CNT; i++) {
  547. sdio_slave_buf_handle_t buf_handle;
  548. uint8_t* buf;
  549. size_t rcv_len;
  550. err = sdio_slave_recv(&buf_handle, &buf, &rcv_len, portMAX_DELAY);
  551. TEST_ESP_OK(err);
  552. if (config->check_data) {
  553. //compare until all received data are used
  554. int compared_len = 0;
  555. do {
  556. //get the expected master sent data, there may be several segments, so get and compare
  557. //several times
  558. uint8_t* cmp_start;
  559. int seg_len;
  560. get_master_send_data(offset, &cmp_start, &seg_len);
  561. int cmp_len = MIN(rcv_len-compared_len, seg_len);
  562. TEST_ASSERT_EQUAL_HEX8_ARRAY(cmp_start, &buf[compared_len], cmp_len);
  563. compared_len += cmp_len;
  564. offset += cmp_len;
  565. } while (compared_len < rcv_len);
  566. } else {
  567. offset += rcv_len;
  568. }
  569. err = sdio_slave_recv_load_buf(buf_handle);
  570. TEST_ESP_OK(err);
  571. }
  572. wait_for_finish(ctx);
  573. }
  574. static void slave_tx_rx_short_data(void)
  575. {
  576. esp_err_t err;
  577. for (int i = 0; i < 10; i++) {
  578. err = sdio_slave_send_queue(tx_buffer + 4*i, TEST_RESET_DATA_LEN, (void*)i, portMAX_DELAY);
  579. TEST_ESP_OK(err);
  580. }
  581. for (int i = 0; i < 10; i++) {
  582. uint8_t* addr;
  583. size_t size;
  584. sdio_slave_buf_handle_t recv_handle;
  585. err = sdio_slave_recv(&recv_handle, &addr, &size, portMAX_DELAY);
  586. TEST_ESP_OK(err);
  587. TEST_ASSERT_EQUAL(TEST_RESET_DATA_LEN, size);
  588. TEST_ASSERT_EQUAL_HEX8_ARRAY(tx_buffer+i*8, addr, size);
  589. }
  590. for (int i = 0; i < 10; i++) {
  591. void* arg;
  592. err = sdio_slave_send_get_finished(&arg, portMAX_DELAY);
  593. TEST_ESP_OK(err);
  594. TEST_ASSERT_EQUAL(i, arg);
  595. }
  596. }
  597. void test_reset_slave(slave_context_t *context, const sdio_test_config_t* config)
  598. {
  599. sdio_slave_stop();
  600. esp_err_t err = sdio_slave_reset();
  601. TEST_ESP_OK(err);
  602. err = sdio_slave_start();
  603. TEST_ESP_OK(err);
  604. init_tx_buffer();
  605. slave_parepare_recv_buffer();
  606. slave_tx_rx_short_data();
  607. wait_for_finish(context);
  608. }
  609. void test_sdio_reset_slave(void)
  610. {
  611. test_framework_slave(test_reset_slave, &packet_config);
  612. }
  613. TEST_CASE_MULTIPLE_DEVICES("sdio interrupt", "[sdio][test_env=UT_SDIO]", test_sdio_interrupt_master, test_sdio_interrupt_slave);
  614. TEST_CASE_MULTIPLE_DEVICES("sdio register", "[sdio][test_env=UT_SDIO]", test_sdio_reg_master, test_sdio_interrupt_slave);
  615. #if !CONFIG_FREERTOS_UNICORE
  616. TEST_CASE_MULTIPLE_DEVICES("sdio reset", "[sdio][test_env=UT_SDIO]", test_sdio_reset_master, test_sdio_reset_slave);
  617. #else
  618. //Currently there is weird issue on the runner, when tested with single core config, seems to relate to receiving
  619. TEST_CASE_MULTIPLE_DEVICES("sdio reset", "[sdio][test_env=UT_SDIO][ignore]", test_sdio_reset_master, test_sdio_reset_slave);
  620. #endif
  621. static void test_sdio_frhost_master(const void* pset, void* context)
  622. {
  623. test_framework_master(test_tp_frhost_master, pset);
  624. }
  625. static void test_sdio_frhost_slave(const void* pset, void* context)
  626. {
  627. test_framework_slave(test_tp_frhost_slave, pset);
  628. }
  629. static void test_sdio_tohost_master(const void* pset, void* context)
  630. {
  631. test_framework_master(test_tp_tohost_master, pset);
  632. }
  633. static void test_sdio_tohost_slave(const void* pset, void* context)
  634. {
  635. test_framework_slave(test_tp_tohost_slave, pset);
  636. }
  637. static void null_pre(void** arg)
  638. {
  639. }
  640. static void null_post(void* arg)
  641. {
  642. }
  643. ptest_func_t frhost_master = {
  644. .pre_test = null_pre,
  645. .loop = test_sdio_frhost_master,
  646. .post_test = null_post,
  647. };
  648. ptest_func_t frhost_slave = {
  649. .pre_test = null_pre,
  650. .loop = test_sdio_frhost_slave,
  651. .post_test = null_post,
  652. };
  653. PARAM_GROUP_DECLARE_TYPE(IO_MODE, sdio_test_config_t, test_cfg_array);
  654. #if !CONFIG_FREERTOS_UNICORE
  655. TEST_MASTER_SLAVE(SDIO_FRHOST, test_cfg_array, "[sdio][timeout=180][test_env=UT_SDIO]", &frhost_master, &frhost_slave);
  656. #else
  657. //Currently there is weird issue on the runner, when tested with single core config, seems to relate to receiving
  658. TEST_MASTER_SLAVE(SDIO_FRHOST, test_cfg_array, "[sdio][timeout=180][test_env=UT_SDIO][ignore]", &frhost_master, &frhost_slave);
  659. #endif
  660. ptest_func_t tohost_master = {
  661. .pre_test = null_pre,
  662. .loop = test_sdio_tohost_master,
  663. .post_test = null_post,
  664. };
  665. ptest_func_t tohost_slave = {
  666. .pre_test = null_pre,
  667. .loop = test_sdio_tohost_slave,
  668. .post_test = null_post,
  669. };
  670. TEST_MASTER_SLAVE(SDIO_TOHOST, test_cfg_array, "[sdio][timeout=180][test_env=UT_SDIO]", &tohost_master, &tohost_slave);
  671. #endif //SOC_SDMMC_HOST_SUPPORTED && SOC_SDIO_SLAVE_SUPPORTED
  672. #endif