test_read_write.c 12 KB

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