test_spi_bus_lock.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "sdkconfig.h"
  7. #include "esp_log.h"
  8. #include "driver/spi_master.h"
  9. #include "driver/gpio.h"
  10. #include "esp_flash_spi_init.h"
  11. #include "test/test_common_spi.h"
  12. #include "unity.h"
  13. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3, ESP32C3)
  14. #if CONFIG_IDF_TARGET_ESP32
  15. // The VSPI pins on UT_T1_ESP_FLASH are connected to a external flash
  16. #define TEST_BUS_PIN_NUM_MISO VSPI_IOMUX_PIN_NUM_MISO
  17. #define TEST_BUS_PIN_NUM_MOSI VSPI_IOMUX_PIN_NUM_MOSI
  18. #define TEST_BUS_PIN_NUM_CLK VSPI_IOMUX_PIN_NUM_CLK
  19. #define TEST_BUS_PIN_NUM_CS VSPI_IOMUX_PIN_NUM_CS
  20. #define TEST_BUS_PIN_NUM_WP VSPI_IOMUX_PIN_NUM_WP
  21. #define TEST_BUS_PIN_NUM_HD VSPI_IOMUX_PIN_NUM_HD
  22. #elif CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3
  23. #define TEST_BUS_PIN_NUM_MISO FSPI_IOMUX_PIN_NUM_MISO
  24. #define TEST_BUS_PIN_NUM_MOSI FSPI_IOMUX_PIN_NUM_MOSI
  25. #define TEST_BUS_PIN_NUM_CLK FSPI_IOMUX_PIN_NUM_CLK
  26. #define TEST_BUS_PIN_NUM_CS FSPI_IOMUX_PIN_NUM_CS
  27. #define TEST_BUS_PIN_NUM_WP FSPI_IOMUX_PIN_NUM_WP
  28. #define TEST_BUS_PIN_NUM_HD FSPI_IOMUX_PIN_NUM_HD
  29. #endif
  30. typedef struct {
  31. union {
  32. spi_device_handle_t handle;
  33. esp_flash_t* chip;
  34. };
  35. bool finished;
  36. } task_context_t;
  37. #ifndef CONFIG_ESP32_SPIRAM_SUPPORT
  38. const static char TAG[] = "test_spi";
  39. void spi_task1(void* arg)
  40. {
  41. //task1 send 50 polling transactions, acquire the bus and send another 50
  42. int count=0;
  43. spi_transaction_t t = {
  44. .flags = SPI_TRANS_USE_TXDATA,
  45. .tx_data = { 0x80, 0x12, 0x34, 0x56 },
  46. .length = 4*8,
  47. };
  48. spi_device_handle_t handle = ((task_context_t*)arg)->handle;
  49. for( int j = 0; j < 50; j ++ ) {
  50. TEST_ESP_OK(spi_device_polling_transmit( handle, &t ));
  51. ESP_LOGI(TAG, "task1:%d", count++ );
  52. }
  53. TEST_ESP_OK(spi_device_acquire_bus( handle, portMAX_DELAY ));
  54. for( int j = 0; j < 50; j ++ ) {
  55. TEST_ESP_OK(spi_device_polling_transmit( handle, &t ));
  56. ESP_LOGI(TAG, "task1:%d", count++ );
  57. }
  58. spi_device_release_bus(handle);
  59. ESP_LOGI(TAG, "task1 terminates");
  60. ((task_context_t*)arg)->finished = true;
  61. vTaskDelete(NULL);
  62. }
  63. void spi_task2(void* arg)
  64. {
  65. int count=0;
  66. //task2 acquire the bus, send 50 polling transactions and then 50 non-polling
  67. spi_transaction_t t = {
  68. .flags = SPI_TRANS_USE_TXDATA,
  69. .tx_data = { 0x80, 0x12, 0x34, 0x56 },
  70. .length = 4*8,
  71. };
  72. spi_transaction_t *ret_t;
  73. spi_device_handle_t handle = ((task_context_t*)arg)->handle;
  74. TEST_ESP_OK(spi_device_acquire_bus( handle, portMAX_DELAY ));
  75. for (int i = 0; i < 50; i ++) {
  76. TEST_ESP_OK(spi_device_polling_transmit(handle, &t));
  77. ESP_LOGI( TAG, "task2: %d", count++ );
  78. }
  79. for( int j = 0; j < 50; j ++ ) {
  80. TEST_ESP_OK(spi_device_queue_trans(handle, &t, portMAX_DELAY));
  81. }
  82. for( int j = 0; j < 50; j ++ ) {
  83. TEST_ESP_OK(spi_device_get_trans_result(handle, &ret_t, portMAX_DELAY));
  84. assert(ret_t == &t);
  85. ESP_LOGI( TAG, "task2: %d", count++ );
  86. }
  87. spi_device_release_bus(handle);
  88. vTaskDelay(1);
  89. ESP_LOGI(TAG, "task2 terminates");
  90. ((task_context_t*)arg)->finished = true;
  91. vTaskDelete(NULL);
  92. }
  93. void spi_task3(void* arg)
  94. {
  95. //task3 send 30 polling transactions, acquire the bus, send 20 polling transactions and then 50 non-polling
  96. int count=0;
  97. spi_transaction_t t = {
  98. .flags = SPI_TRANS_USE_TXDATA,
  99. .tx_data = { 0x80, 0x12, 0x34, 0x56 },
  100. .length = 4*8,
  101. };
  102. spi_transaction_t *ret_t;
  103. spi_device_handle_t handle = ((task_context_t*)arg)->handle;
  104. for (int i = 0; i < 30; i ++) {
  105. TEST_ESP_OK(spi_device_polling_transmit(handle, &t));
  106. ESP_LOGI( TAG, "task3: %d", count++ );
  107. }
  108. TEST_ESP_OK(spi_device_acquire_bus( handle, portMAX_DELAY ));
  109. for (int i = 0; i < 20; i ++) {
  110. TEST_ESP_OK(spi_device_polling_transmit(handle, &t));
  111. ESP_LOGI( TAG, "task3: %d", count++ );
  112. }
  113. for (int j = 0; j < 50; j++) {
  114. TEST_ESP_OK(spi_device_queue_trans(handle, &t, portMAX_DELAY));
  115. }
  116. for (int j = 0; j < 50; j++) {
  117. TEST_ESP_OK(spi_device_get_trans_result(handle, &ret_t, portMAX_DELAY));
  118. assert(ret_t == &t);
  119. ESP_LOGI(TAG, "task3: %d", count++);
  120. }
  121. spi_device_release_bus(handle);
  122. ESP_LOGI(TAG, "task3 terminates");
  123. ((task_context_t*)arg)->finished = true;
  124. vTaskDelete(NULL);
  125. }
  126. static void write_large_buffer(esp_flash_t *chip, const esp_partition_t *part, const uint8_t *source, size_t length)
  127. {
  128. printf("Erasing chip %p, %d bytes\n", chip, length);
  129. TEST_ESP_OK(esp_flash_erase_region(chip, part->address, (length + SPI_FLASH_SEC_SIZE) & ~(SPI_FLASH_SEC_SIZE - 1)) );
  130. printf("Writing chip %p, %d bytes from source %p\n", chip, length, source);
  131. // note writing to unaligned address
  132. TEST_ESP_OK(esp_flash_write(chip, source, part->address + 1, length) );
  133. printf("Write done.\n");
  134. }
  135. static void read_and_check(esp_flash_t *chip, const esp_partition_t *part, const uint8_t *source, size_t length)
  136. {
  137. printf("Checking chip %p, %d bytes\n", chip, length);
  138. uint8_t *buf = malloc(length);
  139. TEST_ASSERT_NOT_NULL(buf);
  140. TEST_ESP_OK(esp_flash_read(chip, buf, part->address + 1, length) );
  141. TEST_ASSERT_EQUAL_HEX8_ARRAY(source, buf, length);
  142. free(buf);
  143. // check nothing was written at beginning or end
  144. uint8_t ends[8];
  145. TEST_ESP_OK(esp_flash_read(chip, ends, part->address, sizeof(ends)) );
  146. TEST_ASSERT_EQUAL_HEX8(0xFF, ends[0]);
  147. TEST_ASSERT_EQUAL_HEX8(source[0], ends[1]);
  148. TEST_ESP_OK(esp_flash_read(chip, ends, part->address + length, sizeof(ends)) );
  149. TEST_ASSERT_EQUAL_HEX8(source[length - 1], ends[0]);
  150. TEST_ASSERT_EQUAL_HEX8(0xFF, ends[1]);
  151. TEST_ASSERT_EQUAL_HEX8(0xFF, ends[2]);
  152. TEST_ASSERT_EQUAL_HEX8(0xFF, ends[3]);
  153. }
  154. void spi_task4(void* arg)
  155. {
  156. esp_flash_t *chip = ((task_context_t*)arg)->chip;
  157. // buffer in RAM
  158. const int test_len = 16400;
  159. uint8_t *source_buf = heap_caps_malloc(test_len, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  160. TEST_ASSERT_NOT_NULL(source_buf);
  161. srand(676);
  162. for (int i = 0; i < test_len; i++) {
  163. source_buf[i] = rand();
  164. }
  165. ESP_LOGI(TAG, "Testing chip %p...", chip);
  166. const esp_partition_t *part = get_test_data_partition();
  167. TEST_ASSERT(part->size > test_len + 2 + SPI_FLASH_SEC_SIZE);
  168. write_large_buffer(chip, part, source_buf, test_len);
  169. read_and_check(chip, part, source_buf, test_len);
  170. free(source_buf);
  171. ESP_LOGI(TAG, "task4 terminates");
  172. ((task_context_t*)arg)->finished = true;
  173. vTaskDelete(NULL);
  174. }
  175. static void test_bus_lock(bool test_flash)
  176. {
  177. task_context_t context1={};
  178. task_context_t context2={};
  179. task_context_t context3={};
  180. task_context_t context4={};
  181. TaskHandle_t task1, task2, task3, task4;
  182. esp_err_t ret;
  183. spi_bus_config_t buscfg=SPI_BUS_TEST_DEFAULT_CONFIG();
  184. buscfg.miso_io_num = TEST_BUS_PIN_NUM_MISO;
  185. buscfg.mosi_io_num = TEST_BUS_PIN_NUM_MOSI;
  186. buscfg.sclk_io_num = TEST_BUS_PIN_NUM_CLK;
  187. spi_device_interface_config_t devcfg=SPI_DEVICE_TEST_DEFAULT_CONFIG();
  188. devcfg.queue_size = 100;
  189. //Initialize the SPI bus and 3 devices
  190. ret=spi_bus_initialize(TEST_SPI_HOST, &buscfg, 1);
  191. TEST_ESP_OK(ret);
  192. ret=spi_bus_add_device(TEST_SPI_HOST, &devcfg, &context1.handle);
  193. TEST_ESP_OK(ret);
  194. ret=spi_bus_add_device(TEST_SPI_HOST, &devcfg, &context2.handle);
  195. TEST_ESP_OK(ret);
  196. //only have 3 cs pins, leave one for the flash
  197. devcfg.spics_io_num = -1;
  198. ret=spi_bus_add_device(TEST_SPI_HOST, &devcfg, &context3.handle);
  199. TEST_ESP_OK(ret);
  200. esp_flash_spi_device_config_t flash_cfg = {
  201. .host_id = TEST_SPI_HOST,
  202. .cs_id = 2,
  203. .cs_io_num = TEST_BUS_PIN_NUM_CS,
  204. .io_mode = SPI_FLASH_DIO,
  205. .speed = ESP_FLASH_5MHZ,
  206. .input_delay_ns = 0,
  207. };
  208. //Clamp the WP and HD pins to VDD to make it work in DIO mode
  209. gpio_set_direction(TEST_BUS_PIN_NUM_HD, GPIO_MODE_OUTPUT);
  210. gpio_set_direction(TEST_BUS_PIN_NUM_WP, GPIO_MODE_OUTPUT);
  211. gpio_set_level(TEST_BUS_PIN_NUM_HD, 1);
  212. gpio_set_level(TEST_BUS_PIN_NUM_WP, 1);
  213. esp_flash_t *chip;
  214. (void) chip;
  215. if (test_flash) {
  216. ret = spi_bus_add_flash_device(&chip, &flash_cfg);
  217. TEST_ESP_OK(ret);
  218. ret = esp_flash_init(chip);
  219. TEST_ESP_OK(ret);
  220. context4.chip = chip;
  221. }
  222. ESP_LOGI(TAG, "Start testing...");
  223. xTaskCreate( spi_task1, "task1", 4096, &context1, 0, &task1 );
  224. xTaskCreate( spi_task2, "task2", 4096, &context2, 0, &task2 );
  225. xTaskCreate( spi_task3, "task3", 4096, &context3, 0, &task3 );
  226. if (test_flash) {
  227. xTaskCreate( spi_task4, "task4", 2048, &context4, 0, &task4 );
  228. } else {
  229. context4.finished = true;
  230. }
  231. for(;;){
  232. vTaskDelay(10);
  233. if (context1.finished && context2.finished && context3.finished && context4.finished) break;
  234. }
  235. TEST_ESP_OK(spi_bus_remove_device(context1.handle));
  236. TEST_ESP_OK(spi_bus_remove_device(context2.handle));
  237. TEST_ESP_OK(spi_bus_remove_device(context3.handle));
  238. if (test_flash) {
  239. TEST_ESP_OK(spi_bus_remove_flash_device(chip));
  240. }
  241. TEST_ESP_OK(spi_bus_free(TEST_SPI_HOST) );
  242. }
  243. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  244. //no runners
  245. TEST_CASE("spi bus lock, with flash","[spi][test_env=UT_T1_ESP_FLASH]")
  246. {
  247. test_bus_lock(true);
  248. }
  249. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  250. TEST_CASE("spi bus lock","[spi]")
  251. {
  252. test_bus_lock(false);
  253. }
  254. #if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  255. //SPI1 not supported by driver
  256. static IRAM_ATTR esp_err_t test_polling_send(spi_device_handle_t handle)
  257. {
  258. for (int i = 0; i < 10; i++) {
  259. spi_transaction_t trans = {
  260. .length = 16,
  261. .flags = SPI_TRANS_USE_TXDATA | SPI_TRANS_USE_RXDATA,
  262. };
  263. esp_err_t err = spi_device_polling_transmit(handle, &trans);
  264. if (err != ESP_OK) {
  265. return err;
  266. }
  267. }
  268. return ESP_OK;
  269. }
  270. static IRAM_ATTR NOINLINE_ATTR void test_acquire(spi_device_handle_t handle)
  271. {
  272. esp_err_t err = spi_device_acquire_bus(handle, portMAX_DELAY);
  273. if (err == ESP_OK) {
  274. err = test_polling_send(handle);
  275. spi_device_release_bus(handle);
  276. }
  277. TEST_ESP_OK(err);
  278. }
  279. TEST_CASE("spi master can be used on SPI1", "[spi]")
  280. {
  281. spi_device_interface_config_t dev_cfg = {
  282. .mode = 1,
  283. .clock_speed_hz = 1*1000*1000,
  284. .spics_io_num = -1,
  285. .queue_size = 1,
  286. };
  287. spi_device_handle_t handle;
  288. esp_err_t err;
  289. err = spi_bus_add_device(SPI1_HOST, &dev_cfg, &handle);
  290. TEST_ESP_OK(err);
  291. err = test_polling_send(handle);
  292. TEST_ESP_OK(err);
  293. test_acquire(handle);
  294. err = spi_bus_remove_device(handle);
  295. TEST_ESP_OK(err);
  296. }
  297. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S2)
  298. //TODO: add a case when a non-polling transaction happened in the bus-acquiring time and then release the bus then queue a new trans
  299. #endif //!CONFIG_ESP32_SPIRAM_SUPPORT
  300. #endif //!TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3, ESP32C3)