test_read_write.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /*
  2. * SPDX-FileCopyrightText: 2010-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. // Test for spi_flash_{read,write}.
  7. #include <assert.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <sys/param.h>
  12. #include <unity.h>
  13. #include <test_utils.h>
  14. #include <esp_spi_flash.h>
  15. #include "../cache_utils.h"
  16. #include "soc/timer_periph.h"
  17. #include "esp_heap_caps.h"
  18. #if CONFIG_IDF_TARGET_ESP32
  19. #include "esp32/rom/spi_flash.h"
  20. #elif CONFIG_IDF_TARGET_ESP32S2
  21. #include "esp32s2/rom/spi_flash.h"
  22. #elif CONFIG_IDF_TARGET_ESP32S3
  23. #include "esp32s3/rom/spi_flash.h"
  24. #elif CONFIG_IDF_TARGET_ESP32C3
  25. #include "esp32c3/rom/spi_flash.h"
  26. #elif CONFIG_IDF_TARGET_ESP32H2
  27. #include "esp32h2/rom/spi_flash.h"
  28. #endif
  29. #define MIN_BLOCK_SIZE 12
  30. /* Base offset in flash for tests. */
  31. static size_t start;
  32. static void setup_tests(void)
  33. {
  34. if (start == 0) {
  35. const esp_partition_t *part = get_test_data_partition();
  36. start = part->address;
  37. printf("Test data partition @ 0x%x\n", start);
  38. }
  39. }
  40. #ifndef CONFIG_SPI_FLASH_MINIMAL_TEST
  41. #define CONFIG_SPI_FLASH_MINIMAL_TEST 1
  42. #endif
  43. static void fill(char *dest, int32_t start, int32_t len)
  44. {
  45. for (int32_t i = 0; i < len; i++) {
  46. *(dest + i) = (char) (start + i);
  47. }
  48. }
  49. static int cmp_or_dump(const void *a, const void *b, size_t len)
  50. {
  51. int r = memcmp(a, b, len);
  52. if (r != 0) {
  53. for (int i = 0; i < len; i++) {
  54. fprintf(stderr, "%02x", ((unsigned char *) a)[i]);
  55. }
  56. fprintf(stderr, "\n");
  57. for (int i = 0; i < len; i++) {
  58. fprintf(stderr, "%02x", ((unsigned char *) b)[i]);
  59. }
  60. fprintf(stderr, "\n");
  61. }
  62. return r;
  63. }
  64. static void IRAM_ATTR test_read(int src_off, int dst_off, int len)
  65. {
  66. uint32_t src_buf[16];
  67. char dst_buf[64], dst_gold[64];
  68. fprintf(stderr, "src=%d dst=%d len=%d\n", src_off, dst_off, len);
  69. memset(src_buf, 0xAA, sizeof(src_buf));
  70. fill(((char *) src_buf) + src_off, src_off, len);
  71. ESP_ERROR_CHECK(spi_flash_erase_sector((start + src_off) / SPI_FLASH_SEC_SIZE));
  72. spi_flash_disable_interrupts_caches_and_other_cpu();
  73. esp_rom_spiflash_result_t rc = esp_rom_spiflash_write(start, src_buf, sizeof(src_buf));
  74. spi_flash_enable_interrupts_caches_and_other_cpu();
  75. TEST_ASSERT_EQUAL_HEX(rc, ESP_ROM_SPIFLASH_RESULT_OK);
  76. memset(dst_buf, 0x55, sizeof(dst_buf));
  77. memset(dst_gold, 0x55, sizeof(dst_gold));
  78. fill(dst_gold + dst_off, src_off, len);
  79. ESP_ERROR_CHECK(spi_flash_read(start + src_off, dst_buf + dst_off, len));
  80. TEST_ASSERT_EQUAL_INT(cmp_or_dump(dst_buf, dst_gold, sizeof(dst_buf)), 0);
  81. }
  82. TEST_CASE("Test spi_flash_read", "[spi_flash][esp_flash]")
  83. {
  84. setup_tests();
  85. #if CONFIG_SPI_FLASH_MINIMAL_TEST
  86. test_read(0, 0, 0);
  87. test_read(0, 0, 4);
  88. test_read(0, 0, 16);
  89. test_read(0, 0, 64);
  90. test_read(0, 0, 1);
  91. test_read(0, 1, 1);
  92. test_read(1, 0, 1);
  93. test_read(1, 1, 1);
  94. test_read(1, 1, 2);
  95. test_read(1, 1, 3);
  96. test_read(1, 1, 4);
  97. test_read(1, 1, 5);
  98. test_read(3, 2, 5);
  99. test_read(0, 0, 17);
  100. test_read(0, 1, 17);
  101. test_read(1, 0, 17);
  102. test_read(1, 1, 17);
  103. test_read(1, 1, 18);
  104. test_read(1, 1, 19);
  105. test_read(1, 1, 20);
  106. test_read(1, 1, 21);
  107. test_read(3, 2, 21);
  108. test_read(4, 4, 60);
  109. test_read(59, 0, 5);
  110. test_read(60, 0, 4);
  111. test_read(60, 0, 3);
  112. test_read(60, 0, 2);
  113. test_read(63, 0, 1);
  114. test_read(64, 0, 0);
  115. test_read(59, 59, 5);
  116. test_read(60, 60, 4);
  117. test_read(60, 60, 3);
  118. test_read(60, 60, 2);
  119. test_read(63, 63, 1);
  120. test_read(64, 64, 0);
  121. #else
  122. /* This will run a more thorough test but will slam flash pretty hard. */
  123. for (int src_off = 1; src_off < 16; src_off++) {
  124. for (int dst_off = 0; dst_off < 16; dst_off++) {
  125. for (int len = 0; len < 32; len++) {
  126. test_read(dst_off, src_off, len);
  127. }
  128. }
  129. }
  130. #endif
  131. }
  132. extern void spi_common_set_dummy_output(esp_rom_spiflash_read_mode_t mode);
  133. extern void spi_dummy_len_fix(uint8_t spi, uint8_t freqdiv);
  134. static void IRAM_ATTR fix_rom_func(void)
  135. {
  136. uint32_t freqdiv = 0;
  137. #if CONFIG_ESPTOOLPY_FLASHFREQ_80M && !CONFIG_ESPTOOLPY_OCT_FLASH
  138. freqdiv = 1;
  139. #elif CONFIG_ESPTOOLPY_FLASHFREQ_80M && CONFIG_ESPTOOLPY_OCT_FLASH
  140. freqdiv = 2;
  141. #elif CONFIG_ESPTOOLPY_FLASHFREQ_40M
  142. freqdiv = 2;
  143. #elif CONFIG_ESPTOOLPY_FLASHFREQ_26M
  144. freqdiv = 3;
  145. #elif CONFIG_ESPTOOLPY_FLASHFREQ_20M
  146. freqdiv = 4;
  147. #elif CONFIG_ESPTOOLPY_FLASHFREQ_120M
  148. freqdiv = 2;
  149. #endif
  150. #if CONFIG_IDF_TARGET_ESP32
  151. uint32_t dummy_bit = 0;
  152. #if CONFIG_ESPTOOLPY_FLASHFREQ_80M
  153. dummy_bit = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_80M;
  154. #elif CONFIG_ESPTOOLPY_FLASHFREQ_40M
  155. dummy_bit = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_40M;
  156. #elif CONFIG_ESPTOOLPY_FLASHFREQ_26M
  157. dummy_bit = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_26M;
  158. #elif CONFIG_ESPTOOLPY_FLASHFREQ_20M
  159. dummy_bit = ESP_ROM_SPIFLASH_DUMMY_LEN_PLUS_20M;
  160. #endif
  161. g_rom_spiflash_dummy_len_plus[1] = dummy_bit;
  162. #else
  163. spi_dummy_len_fix(1, freqdiv);
  164. #endif//CONFIG_IDF_TARGET_ESP32
  165. esp_rom_spiflash_read_mode_t read_mode;
  166. #if CONFIG_ESPTOOLPY_FLASHMODE_QIO
  167. read_mode = ESP_ROM_SPIFLASH_QIO_MODE;
  168. #elif CONFIG_ESPTOOLPY_FLASHMODE_QOUT
  169. read_mode = ESP_ROM_SPIFLASH_QOUT_MODE;
  170. #elif CONFIG_ESPTOOLPY_FLASHMODE_DIO
  171. read_mode = ESP_ROM_SPIFLASH_DIO_MODE;
  172. #elif CONFIG_ESPTOOLPY_FLASHMODE_DOUT
  173. read_mode = ESP_ROM_SPIFLASH_DOUT_MODE;
  174. #elif CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_STR
  175. read_mode = ESP_ROM_SPIFLASH_OPI_STR_MODE;
  176. #elif CONFIG_ESPTOOLPY_FLASH_SAMPLE_MODE_DTR
  177. read_mode = ESP_ROM_SPIFLASH_OPI_DTR_MODE;
  178. #endif
  179. #if !CONFIG_IDF_TARGET_ESP32S2 && !CONFIG_IDF_TARGET_ESP32
  180. spi_common_set_dummy_output(read_mode);
  181. #endif //!CONFIG_IDF_TARGET_ESP32S2
  182. esp_rom_spiflash_config_clk(freqdiv, 1);
  183. #if !CONFIG_ESPTOOLPY_OCT_FLASH
  184. esp_rom_spiflash_config_readmode(read_mode);
  185. #endif
  186. }
  187. static void IRAM_ATTR test_write(int dst_off, int src_off, int len)
  188. {
  189. char src_buf[64], dst_gold[64];
  190. uint32_t dst_buf[16];
  191. fprintf(stderr, "dst=%d src=%d len=%d\n", dst_off, src_off, len);
  192. memset(src_buf, 0x55, sizeof(src_buf));
  193. fill(src_buf + src_off, src_off, len);
  194. // Fills with 0xff
  195. ESP_ERROR_CHECK(spi_flash_erase_sector((start + dst_off) / SPI_FLASH_SEC_SIZE));
  196. memset(dst_gold, 0xff, sizeof(dst_gold));
  197. if (len > 0) {
  198. int pad_left_off = (dst_off & ~3U);
  199. memset(dst_gold + pad_left_off, 0xff, 4);
  200. if (dst_off + len > pad_left_off + 4 && (dst_off + len) % 4 != 0) {
  201. int pad_right_off = ((dst_off + len) & ~3U);
  202. memset(dst_gold + pad_right_off, 0xff, 4);
  203. }
  204. fill(dst_gold + dst_off, src_off, len);
  205. }
  206. ESP_ERROR_CHECK(spi_flash_write(start + dst_off, src_buf + src_off, len));
  207. fix_rom_func();
  208. spi_flash_disable_interrupts_caches_and_other_cpu();
  209. esp_rom_spiflash_result_t rc = esp_rom_spiflash_read(start, dst_buf, sizeof(dst_buf));
  210. spi_flash_enable_interrupts_caches_and_other_cpu();
  211. TEST_ASSERT_EQUAL_HEX(rc, ESP_ROM_SPIFLASH_RESULT_OK);
  212. TEST_ASSERT_EQUAL_INT(cmp_or_dump(dst_buf, dst_gold, sizeof(dst_buf)), 0);
  213. }
  214. TEST_CASE("Test spi_flash_write", "[spi_flash][esp_flash]")
  215. {
  216. setup_tests();
  217. #if CONFIG_SPI_FLASH_MINIMAL_TEST
  218. test_write(0, 0, 0);
  219. test_write(0, 0, 4);
  220. test_write(0, 0, 16);
  221. test_write(0, 0, 64);
  222. test_write(0, 0, 1);
  223. test_write(0, 1, 1);
  224. test_write(1, 0, 1);
  225. test_write(1, 1, 1);
  226. test_write(1, 1, 2);
  227. test_write(1, 1, 3);
  228. test_write(1, 1, 4);
  229. test_write(1, 1, 5);
  230. test_write(3, 2, 5);
  231. test_write(4, 4, 60);
  232. test_write(59, 0, 5);
  233. test_write(60, 0, 4);
  234. test_write(60, 0, 3);
  235. test_write(60, 0, 2);
  236. test_write(63, 0, 1);
  237. test_write(64, 0, 0);
  238. test_write(59, 59, 5);
  239. test_write(60, 60, 4);
  240. test_write(60, 60, 3);
  241. test_write(60, 60, 2);
  242. test_write(63, 63, 1);
  243. test_write(64, 64, 0);
  244. #else
  245. /* This will run a more thorough test but will slam flash pretty hard. */
  246. for (int dst_off = 1; dst_off < 16; dst_off++) {
  247. for (int src_off = 0; src_off < 16; src_off++) {
  248. for (int len = 0; len < 16; len++) {
  249. test_write(dst_off, src_off, len);
  250. }
  251. }
  252. }
  253. #endif
  254. /*
  255. * Test writing from ROM, IRAM and caches. We don't know what exactly will be
  256. * written, we're testing that there's no crash here.
  257. *
  258. * NB: At the moment these only support aligned addresses, because memcpy
  259. * is not aware of the 32-but load requirements for these regions.
  260. */
  261. #if CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32C3
  262. #define TEST_SOC_IROM_ADDR (SOC_IROM_LOW)
  263. #define TEST_SOC_CACHE_RAM_BANK0_ADDR (SOC_IRAM_LOW)
  264. #define TEST_SOC_CACHE_RAM_BANK1_ADDR (SOC_IRAM_LOW + 0x2000)
  265. #define TEST_SOC_CACHE_RAM_BANK2_ADDR (SOC_IRAM_LOW + 0x4000)
  266. #define TEST_SOC_CACHE_RAM_BANK3_ADDR (SOC_IRAM_LOW + 0x6000)
  267. #define TEST_SOC_IRAM_ADDR (SOC_IRAM_LOW + 0x8000)
  268. #define TEST_SOC_RTC_IRAM_ADDR (SOC_RTC_IRAM_LOW)
  269. #define TEST_SOC_RTC_DRAM_ADDR (SOC_RTC_DRAM_LOW)
  270. ESP_ERROR_CHECK(spi_flash_write(start, (char *) TEST_SOC_IROM_ADDR, 16));
  271. ESP_ERROR_CHECK(spi_flash_write(start, (char *) TEST_SOC_IRAM_ADDR, 16));
  272. ESP_ERROR_CHECK(spi_flash_write(start, (char *) TEST_SOC_CACHE_RAM_BANK0_ADDR, 16));
  273. ESP_ERROR_CHECK(spi_flash_write(start, (char *) TEST_SOC_CACHE_RAM_BANK1_ADDR, 16));
  274. ESP_ERROR_CHECK(spi_flash_write(start, (char *) TEST_SOC_CACHE_RAM_BANK2_ADDR, 16));
  275. ESP_ERROR_CHECK(spi_flash_write(start, (char *) TEST_SOC_CACHE_RAM_BANK3_ADDR, 16));
  276. ESP_ERROR_CHECK(spi_flash_write(start, (char *) TEST_SOC_RTC_IRAM_ADDR, 16));
  277. ESP_ERROR_CHECK(spi_flash_write(start, (char *) TEST_SOC_RTC_DRAM_ADDR, 16));
  278. #else
  279. ESP_ERROR_CHECK(spi_flash_write(start, (char *) 0x40000000, 16));
  280. ESP_ERROR_CHECK(spi_flash_write(start, (char *) 0x40070000, 16));
  281. ESP_ERROR_CHECK(spi_flash_write(start, (char *) 0x40078000, 16));
  282. ESP_ERROR_CHECK(spi_flash_write(start, (char *) 0x40080000, 16));
  283. #endif
  284. }
  285. #ifdef CONFIG_SPIRAM
  286. TEST_CASE("spi_flash_read can read into buffer in external RAM", "[spi_flash]")
  287. {
  288. uint8_t* buf_ext = (uint8_t*) heap_caps_malloc(SPI_FLASH_SEC_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  289. TEST_ASSERT_NOT_NULL(buf_ext);
  290. uint8_t* buf_int = (uint8_t*) heap_caps_malloc(SPI_FLASH_SEC_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  291. TEST_ASSERT_NOT_NULL(buf_int);
  292. TEST_ESP_OK(spi_flash_read(0x1000, buf_int, SPI_FLASH_SEC_SIZE));
  293. TEST_ESP_OK(spi_flash_read(0x1000, buf_ext, SPI_FLASH_SEC_SIZE));
  294. TEST_ASSERT_EQUAL(0, memcmp(buf_ext, buf_int, SPI_FLASH_SEC_SIZE));
  295. free(buf_ext);
  296. free(buf_int);
  297. }
  298. TEST_CASE("spi_flash_write can write from external RAM buffer", "[spi_flash]")
  299. {
  300. uint32_t* buf_ext = (uint32_t*) heap_caps_malloc(SPI_FLASH_SEC_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  301. TEST_ASSERT_NOT_NULL(buf_ext);
  302. srand(0);
  303. for (size_t i = 0; i < SPI_FLASH_SEC_SIZE / sizeof(uint32_t); i++)
  304. {
  305. uint32_t val = rand();
  306. buf_ext[i] = val;
  307. }
  308. uint8_t* buf_int = (uint8_t*) heap_caps_malloc(SPI_FLASH_SEC_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  309. TEST_ASSERT_NOT_NULL(buf_int);
  310. /* Write to flash from buf_ext */
  311. const esp_partition_t *part = get_test_data_partition();
  312. TEST_ESP_OK(spi_flash_erase_range(part->address, SPI_FLASH_SEC_SIZE));
  313. TEST_ESP_OK(spi_flash_write(part->address, buf_ext, SPI_FLASH_SEC_SIZE));
  314. /* Read back to buf_int and compare */
  315. TEST_ESP_OK(spi_flash_read(part->address, buf_int, SPI_FLASH_SEC_SIZE));
  316. TEST_ASSERT_EQUAL(0, memcmp(buf_ext, buf_int, SPI_FLASH_SEC_SIZE));
  317. free(buf_ext);
  318. free(buf_int);
  319. }
  320. TEST_CASE("spi_flash_read less than 16 bytes into buffer in external RAM", "[spi_flash]")
  321. {
  322. uint8_t *buf_ext_8 = (uint8_t *) heap_caps_malloc(MIN_BLOCK_SIZE, MALLOC_CAP_SPIRAM | MALLOC_CAP_8BIT);
  323. TEST_ASSERT_NOT_NULL(buf_ext_8);
  324. uint8_t *buf_int_8 = (uint8_t *) heap_caps_malloc(MIN_BLOCK_SIZE, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  325. TEST_ASSERT_NOT_NULL(buf_int_8);
  326. uint8_t data_8[MIN_BLOCK_SIZE];
  327. for (int i = 0; i < MIN_BLOCK_SIZE; i++) {
  328. data_8[i] = i;
  329. }
  330. const esp_partition_t *part = get_test_data_partition();
  331. TEST_ESP_OK(spi_flash_erase_range(part->address, SPI_FLASH_SEC_SIZE));
  332. TEST_ESP_OK(spi_flash_write(part->address, data_8, MIN_BLOCK_SIZE));
  333. TEST_ESP_OK(spi_flash_read(part->address, buf_ext_8, MIN_BLOCK_SIZE));
  334. TEST_ESP_OK(spi_flash_read(part->address, buf_int_8, MIN_BLOCK_SIZE));
  335. TEST_ASSERT_EQUAL(0, memcmp(buf_ext_8, data_8, MIN_BLOCK_SIZE));
  336. TEST_ASSERT_EQUAL(0, memcmp(buf_int_8, data_8, MIN_BLOCK_SIZE));
  337. if (buf_ext_8) {
  338. free(buf_ext_8);
  339. buf_ext_8 = NULL;
  340. }
  341. if (buf_int_8) {
  342. free(buf_int_8);
  343. buf_int_8 = NULL;
  344. }
  345. }
  346. #endif // CONFIG_SPIRAM