test_read_write.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 <rom/spi_flash.h>
  24. #include "../cache_utils.h"
  25. #include "soc/timer_group_struct.h"
  26. #include "soc/timer_group_reg.h"
  27. /* Base offset in flash for tests. */
  28. static size_t start;
  29. static void setup_tests()
  30. {
  31. if (start == 0) {
  32. const esp_partition_t *part = get_test_data_partition();
  33. start = part->address;
  34. printf("Test data partition @ 0x%x\n", start);
  35. }
  36. }
  37. #ifndef CONFIG_SPI_FLASH_MINIMAL_TEST
  38. #define CONFIG_SPI_FLASH_MINIMAL_TEST 1
  39. #endif
  40. static void fill(char *dest, int32_t start, int32_t len)
  41. {
  42. for (int32_t i = 0; i < len; i++) {
  43. *(dest + i) = (char) (start + i);
  44. }
  45. }
  46. static int cmp_or_dump(const void *a, const void *b, size_t len)
  47. {
  48. int r = memcmp(a, b, len);
  49. if (r != 0) {
  50. for (int i = 0; i < len; i++) {
  51. fprintf(stderr, "%02x", ((unsigned char *) a)[i]);
  52. }
  53. fprintf(stderr, "\n");
  54. for (int i = 0; i < len; i++) {
  55. fprintf(stderr, "%02x", ((unsigned char *) b)[i]);
  56. }
  57. fprintf(stderr, "\n");
  58. }
  59. return r;
  60. }
  61. static void IRAM_ATTR test_read(int src_off, int dst_off, int len)
  62. {
  63. uint32_t src_buf[16];
  64. char dst_buf[64], dst_gold[64];
  65. fprintf(stderr, "src=%d dst=%d len=%d\n", src_off, dst_off, len);
  66. memset(src_buf, 0xAA, sizeof(src_buf));
  67. fill(((char *) src_buf) + src_off, src_off, len);
  68. ESP_ERROR_CHECK(spi_flash_erase_sector((start + src_off) / SPI_FLASH_SEC_SIZE));
  69. spi_flash_disable_interrupts_caches_and_other_cpu();
  70. esp_rom_spiflash_result_t rc = esp_rom_spiflash_write(start, src_buf, sizeof(src_buf));
  71. spi_flash_enable_interrupts_caches_and_other_cpu();
  72. TEST_ASSERT_EQUAL_INT(rc, ESP_ROM_SPIFLASH_RESULT_OK);
  73. memset(dst_buf, 0x55, sizeof(dst_buf));
  74. memset(dst_gold, 0x55, sizeof(dst_gold));
  75. fill(dst_gold + dst_off, src_off, len);
  76. ESP_ERROR_CHECK(spi_flash_read(start + src_off, dst_buf + dst_off, len));
  77. TEST_ASSERT_EQUAL_INT(cmp_or_dump(dst_buf, dst_gold, sizeof(dst_buf)), 0);
  78. }
  79. TEST_CASE("Test spi_flash_read", "[spi_flash]")
  80. {
  81. setup_tests();
  82. #if CONFIG_SPI_FLASH_MINIMAL_TEST
  83. test_read(0, 0, 0);
  84. test_read(0, 0, 4);
  85. test_read(0, 0, 16);
  86. test_read(0, 0, 64);
  87. test_read(0, 0, 1);
  88. test_read(0, 1, 1);
  89. test_read(1, 0, 1);
  90. test_read(1, 1, 1);
  91. test_read(1, 1, 2);
  92. test_read(1, 1, 3);
  93. test_read(1, 1, 4);
  94. test_read(1, 1, 5);
  95. test_read(3, 2, 5);
  96. test_read(0, 0, 17);
  97. test_read(0, 1, 17);
  98. test_read(1, 0, 17);
  99. test_read(1, 1, 17);
  100. test_read(1, 1, 18);
  101. test_read(1, 1, 19);
  102. test_read(1, 1, 20);
  103. test_read(1, 1, 21);
  104. test_read(3, 2, 21);
  105. test_read(4, 4, 60);
  106. test_read(59, 0, 5);
  107. test_read(60, 0, 4);
  108. test_read(60, 0, 3);
  109. test_read(60, 0, 2);
  110. test_read(63, 0, 1);
  111. test_read(64, 0, 0);
  112. test_read(59, 59, 5);
  113. test_read(60, 60, 4);
  114. test_read(60, 60, 3);
  115. test_read(60, 60, 2);
  116. test_read(63, 63, 1);
  117. test_read(64, 64, 0);
  118. #else
  119. /* This will run a more thorough test but will slam flash pretty hard. */
  120. for (int src_off = 1; src_off < 16; src_off++) {
  121. for (int dst_off = 0; dst_off < 16; dst_off++) {
  122. for (int len = 0; len < 32; len++) {
  123. test_read(dst_off, src_off, len);
  124. }
  125. }
  126. }
  127. #endif
  128. }
  129. static void IRAM_ATTR test_write(int dst_off, int src_off, int len)
  130. {
  131. char src_buf[64], dst_gold[64];
  132. uint32_t dst_buf[16];
  133. fprintf(stderr, "dst=%d src=%d len=%d\n", dst_off, src_off, len);
  134. memset(src_buf, 0x55, sizeof(src_buf));
  135. fill(src_buf + src_off, src_off, len);
  136. // Fills with 0xff
  137. ESP_ERROR_CHECK(spi_flash_erase_sector((start + dst_off) / SPI_FLASH_SEC_SIZE));
  138. memset(dst_gold, 0xff, sizeof(dst_gold));
  139. if (len > 0) {
  140. int pad_left_off = (dst_off & ~3U);
  141. memset(dst_gold + pad_left_off, 0xff, 4);
  142. if (dst_off + len > pad_left_off + 4 && (dst_off + len) % 4 != 0) {
  143. int pad_right_off = ((dst_off + len) & ~3U);
  144. memset(dst_gold + pad_right_off, 0xff, 4);
  145. }
  146. fill(dst_gold + dst_off, src_off, len);
  147. }
  148. ESP_ERROR_CHECK(spi_flash_write(start + dst_off, src_buf + src_off, len));
  149. spi_flash_disable_interrupts_caches_and_other_cpu();
  150. esp_rom_spiflash_result_t rc = esp_rom_spiflash_read(start, dst_buf, sizeof(dst_buf));
  151. spi_flash_enable_interrupts_caches_and_other_cpu();
  152. TEST_ASSERT_EQUAL_INT(rc, ESP_ROM_SPIFLASH_RESULT_OK);
  153. TEST_ASSERT_EQUAL_INT(cmp_or_dump(dst_buf, dst_gold, sizeof(dst_buf)), 0);
  154. }
  155. TEST_CASE("Test spi_flash_write", "[spi_flash]")
  156. {
  157. setup_tests();
  158. #if CONFIG_SPI_FLASH_MINIMAL_TEST
  159. test_write(0, 0, 0);
  160. test_write(0, 0, 4);
  161. test_write(0, 0, 16);
  162. test_write(0, 0, 64);
  163. test_write(0, 0, 1);
  164. test_write(0, 1, 1);
  165. test_write(1, 0, 1);
  166. test_write(1, 1, 1);
  167. test_write(1, 1, 2);
  168. test_write(1, 1, 3);
  169. test_write(1, 1, 4);
  170. test_write(1, 1, 5);
  171. test_write(3, 2, 5);
  172. test_write(4, 4, 60);
  173. test_write(59, 0, 5);
  174. test_write(60, 0, 4);
  175. test_write(60, 0, 3);
  176. test_write(60, 0, 2);
  177. test_write(63, 0, 1);
  178. test_write(64, 0, 0);
  179. test_write(59, 59, 5);
  180. test_write(60, 60, 4);
  181. test_write(60, 60, 3);
  182. test_write(60, 60, 2);
  183. test_write(63, 63, 1);
  184. test_write(64, 64, 0);
  185. #else
  186. /* This will run a more thorough test but will slam flash pretty hard. */
  187. for (int dst_off = 1; dst_off < 16; dst_off++) {
  188. for (int src_off = 0; src_off < 16; src_off++) {
  189. for (int len = 0; len < 16; len++) {
  190. test_write(dst_off, src_off, len);
  191. }
  192. }
  193. }
  194. #endif
  195. /*
  196. * Test writing from ROM, IRAM and caches. We don't know what exactly will be
  197. * written, we're testing that there's no crash here.
  198. *
  199. * NB: At the moment these only support aligned addresses, because memcpy
  200. * is not aware of the 32-but load requirements for these regions.
  201. */
  202. ESP_ERROR_CHECK(spi_flash_write(start, (char *) 0x40000000, 16));
  203. ESP_ERROR_CHECK(spi_flash_write(start, (char *) 0x40070000, 16));
  204. ESP_ERROR_CHECK(spi_flash_write(start, (char *) 0x40078000, 16));
  205. ESP_ERROR_CHECK(spi_flash_write(start, (char *) 0x40080000, 16));
  206. }