esp_flash_api.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  1. // Copyright 2015-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 <stdlib.h>
  15. #include <stdio.h>
  16. #include <sys/param.h>
  17. #include <string.h>
  18. #include "spi_flash_chip_driver.h"
  19. #include "memspi_host_driver.h"
  20. #include "esp_log.h"
  21. #include "sdkconfig.h"
  22. #include "esp_heap_caps.h"
  23. static const char TAG[] = "spi_flash";
  24. #define MAX_WRITE_CHUNK 8192 /* write in chunks */
  25. #define MAX_READ_CHUNK 16384
  26. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  27. #define UNSAFE_WRITE_ADDRESS abort()
  28. #else
  29. #define UNSAFE_WRITE_ADDRESS return ESP_ERR_INVALID_ARG
  30. #endif
  31. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  32. bootloader, partition table, or running application region.
  33. */
  34. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  35. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE)
  36. #else /* FAILS or ABORTS */
  37. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE) do { \
  38. if (CHIP && CHIP->host->region_protected && CHIP->host->region_protected(CHIP->host, ADDR, SIZE)) { \
  39. UNSAFE_WRITE_ADDRESS; \
  40. } \
  41. } while(0)
  42. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  43. #define IO_STR_LEN 7
  44. static const char io_mode_str[][IO_STR_LEN] = {
  45. "slowrd",
  46. "fastrd",
  47. "dout",
  48. "dio",
  49. "qout",
  50. "qio",
  51. };
  52. _Static_assert(sizeof(io_mode_str)/IO_STR_LEN == SPI_FLASH_READ_MODE_MAX, "the io_mode_str should be consistent with the esp_flash_read_mode_t defined in spi_flash_ll.h");
  53. /* Static function to notify OS of a new SPI flash operation.
  54. If returns an error result, caller must abort. If returns ESP_OK, caller must
  55. call spiflash_end() before returning.
  56. */
  57. static esp_err_t IRAM_ATTR spiflash_start(esp_flash_t *chip)
  58. {
  59. if (chip->os_func != NULL && chip->os_func->start != NULL) {
  60. esp_err_t err = chip->os_func->start(chip->os_func_data);
  61. if (err != ESP_OK) {
  62. return err;
  63. }
  64. }
  65. chip->host->dev_config(chip->host);
  66. return ESP_OK;
  67. }
  68. /* Static function to notify OS that SPI flash operation is complete.
  69. */
  70. static esp_err_t IRAM_ATTR spiflash_end(const esp_flash_t *chip, esp_err_t err)
  71. {
  72. if (chip->os_func != NULL
  73. && chip->os_func->end != NULL) {
  74. esp_err_t end_err = chip->os_func->end(chip->os_func_data);
  75. if (err == ESP_OK) {
  76. err = end_err; // Only return the 'end' error if we haven't already failed
  77. }
  78. }
  79. return err;
  80. }
  81. /* Return true if regions 'a' and 'b' overlap at all, based on their start offsets and lengths. */
  82. inline static bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len);
  83. /* Top-level API functions, calling into chip_drv functions via chip->drv */
  84. static esp_err_t detect_spi_flash_chip(esp_flash_t *chip);
  85. bool esp_flash_chip_driver_initialized(const esp_flash_t *chip)
  86. {
  87. if (!chip->chip_drv) return false;
  88. return true;
  89. }
  90. esp_err_t IRAM_ATTR esp_flash_init(esp_flash_t *chip)
  91. {
  92. esp_err_t err = ESP_OK;
  93. if (chip == NULL || chip->host == NULL || chip->host->driver_data == NULL ||
  94. ((memspi_host_data_t*)chip->host->driver_data)->spi == NULL) {
  95. return ESP_ERR_INVALID_ARG;
  96. }
  97. if (!esp_flash_chip_driver_initialized(chip)) {
  98. // Detect chip_drv
  99. err = detect_spi_flash_chip(chip);
  100. if (err != ESP_OK) {
  101. return err;
  102. }
  103. }
  104. // Detect flash size
  105. uint32_t size;
  106. err = esp_flash_get_size(chip, &size);
  107. if (err != ESP_OK) {
  108. ESP_LOGE(TAG, "failed to get chip size");
  109. return err;
  110. }
  111. ESP_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  112. err = spiflash_start(chip);
  113. if (err != ESP_OK) {
  114. return err;
  115. }
  116. if (err == ESP_OK) {
  117. // Try to set the flash mode to whatever default mode was chosen
  118. err = chip->chip_drv->set_read_mode(chip);
  119. }
  120. // Done: all fields on 'chip' are initialised
  121. return spiflash_end(chip, err);
  122. }
  123. static esp_err_t IRAM_ATTR detect_spi_flash_chip(esp_flash_t *chip)
  124. {
  125. esp_err_t err;
  126. uint32_t flash_id;
  127. int retries = 10;
  128. do {
  129. err = spiflash_start(chip);
  130. if (err != ESP_OK) {
  131. return err;
  132. }
  133. // Send generic RDID command twice, check for a matching result and retry in case we just powered on (inner
  134. // function fails if it sees all-ones or all-zeroes.)
  135. err = chip->host->read_id(chip->host, &flash_id);
  136. if (err == ESP_OK) { // check we see the same ID twice, in case of transient power-on errors
  137. uint32_t new_id;
  138. err = chip->host->read_id(chip->host, &new_id);
  139. if (err == ESP_OK && (new_id != flash_id)) {
  140. err = ESP_ERR_FLASH_NOT_INITIALISED;
  141. }
  142. }
  143. err = spiflash_end(chip, err);
  144. } while (err != ESP_OK && retries-- > 0);
  145. // Detect the chip and set the chip_drv structure for it
  146. const spi_flash_chip_t **drivers = esp_flash_registered_chips;
  147. while (*drivers != NULL && !esp_flash_chip_driver_initialized(chip)) {
  148. chip->chip_drv = *drivers;
  149. // start/end SPI operation each time, for multitasking
  150. // and also so esp_flash_registered_flash_drivers can live in flash
  151. ESP_LOGD(TAG, "trying chip: %s", chip->chip_drv->name);
  152. err = spiflash_start(chip);
  153. if (err != ESP_OK) {
  154. return err;
  155. }
  156. if (chip->chip_drv->probe(chip, flash_id) != ESP_OK) {
  157. chip->chip_drv = NULL;
  158. }
  159. // if probe succeeded, chip->drv stays set
  160. drivers++;
  161. err = spiflash_end(chip, err);
  162. if (err != ESP_OK) {
  163. return err;
  164. }
  165. }
  166. if (!esp_flash_chip_driver_initialized(chip)) {
  167. return ESP_ERR_NOT_FOUND;
  168. }
  169. ESP_LOGI(TAG, "detected chip: %s", chip->chip_drv->name);
  170. return ESP_OK;
  171. }
  172. // Convenience macro for beginning of all API functions,
  173. // check that the 'chip' parameter is properly initialised
  174. // and supports the operation in question
  175. #define VERIFY_OP(OP) do { \
  176. if (chip == NULL) { \
  177. chip = esp_flash_default_chip; \
  178. } \
  179. if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) { \
  180. return ESP_ERR_FLASH_NOT_INITIALISED; \
  181. } \
  182. if (chip->chip_drv->OP == NULL) { \
  183. return ESP_ERR_FLASH_UNSUPPORTED_CHIP; \
  184. } \
  185. } while (0)
  186. esp_err_t IRAM_ATTR esp_flash_read_id(esp_flash_t *chip, uint32_t *out_id)
  187. {
  188. if (chip == NULL) {
  189. chip = esp_flash_default_chip;
  190. }
  191. if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) {
  192. return ESP_ERR_FLASH_NOT_INITIALISED;
  193. }
  194. if (out_id == NULL) {
  195. return ESP_ERR_INVALID_ARG;
  196. }
  197. esp_err_t err = spiflash_start(chip);
  198. if (err != ESP_OK) {
  199. return err;
  200. }
  201. err = chip->host->read_id(chip->host, out_id);
  202. return spiflash_end(chip, err);
  203. }
  204. esp_err_t IRAM_ATTR esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
  205. {
  206. VERIFY_OP(detect_size);
  207. if (out_size == NULL) {
  208. return ESP_ERR_INVALID_ARG;
  209. }
  210. if (chip->size != 0) {
  211. *out_size = chip->size;
  212. return ESP_OK;
  213. }
  214. esp_err_t err = spiflash_start(chip);
  215. if (err != ESP_OK) {
  216. return err;
  217. }
  218. uint32_t detect_size;
  219. err = chip->chip_drv->detect_size(chip, &detect_size);
  220. if (err == ESP_OK) {
  221. chip->size = detect_size;
  222. }
  223. return spiflash_end(chip, err);
  224. }
  225. esp_err_t IRAM_ATTR esp_flash_erase_chip(esp_flash_t *chip)
  226. {
  227. VERIFY_OP(erase_chip);
  228. CHECK_WRITE_ADDRESS(chip, 0, chip->size);
  229. bool write_protect = false;
  230. esp_err_t err = spiflash_start(chip);
  231. if (err != ESP_OK) {
  232. return err;
  233. }
  234. err = esp_flash_get_chip_write_protect(chip, &write_protect);
  235. if (err == ESP_OK && write_protect) {
  236. err = ESP_ERR_FLASH_PROTECTED;
  237. }
  238. if (err == ESP_OK) {
  239. err = chip->chip_drv->erase_chip(chip);
  240. }
  241. return spiflash_end(chip, err);
  242. }
  243. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  244. {
  245. VERIFY_OP(erase_sector);
  246. VERIFY_OP(erase_block);
  247. CHECK_WRITE_ADDRESS(chip, start, len);
  248. uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size;
  249. uint32_t sector_size = chip->chip_drv->sector_size;
  250. bool write_protect = false;
  251. if (sector_size == 0 || (block_erase_size % sector_size) != 0) {
  252. return ESP_ERR_FLASH_NOT_INITIALISED;
  253. }
  254. if (start > chip->size || start + len > chip->size) {
  255. return ESP_ERR_INVALID_ARG;
  256. }
  257. if ((start % chip->chip_drv->sector_size) != 0 || (len % chip->chip_drv->sector_size) != 0) {
  258. // Can only erase multiples of the sector size, starting at sector boundary
  259. return ESP_ERR_INVALID_ARG;
  260. }
  261. esp_err_t err = spiflash_start(chip);
  262. if (err != ESP_OK) {
  263. return err;
  264. }
  265. // Check for write protection on whole chip
  266. if (chip->chip_drv->get_chip_write_protect != NULL) {
  267. err = chip->chip_drv->get_chip_write_protect(chip, &write_protect);
  268. if (err == ESP_OK && write_protect) {
  269. err = ESP_ERR_FLASH_PROTECTED;
  270. }
  271. }
  272. // Check for write protected regions overlapping the erase region
  273. if (err == ESP_OK && chip->chip_drv->get_protected_regions != NULL && chip->chip_drv->num_protectable_regions > 0) {
  274. uint64_t protected = 0;
  275. err = chip->chip_drv->get_protected_regions(chip, &protected);
  276. if (err == ESP_OK && protected != 0) {
  277. for (int i = 0; i < chip->chip_drv->num_protectable_regions && err == ESP_OK; i++) {
  278. const esp_flash_region_t *region = &chip->chip_drv->protectable_regions[i];
  279. if ((protected & BIT64(i))
  280. && regions_overlap(start, len, region->offset, region->size)) {
  281. err = ESP_ERR_FLASH_PROTECTED;
  282. }
  283. }
  284. }
  285. }
  286. // Don't lock the SPI flash for the entire erase, as this may be very long
  287. err = spiflash_end(chip, err);
  288. while (err == ESP_OK && len >= sector_size) {
  289. err = spiflash_start(chip);
  290. if (err != ESP_OK) {
  291. return err;
  292. }
  293. // If possible erase an entire multi-sector block
  294. if (block_erase_size > 0 && len >= block_erase_size && (start % block_erase_size) == 0) {
  295. err = chip->chip_drv->erase_block(chip, start);
  296. start += block_erase_size;
  297. len -= block_erase_size;
  298. }
  299. else {
  300. // Otherwise erase individual sector only
  301. err = chip->chip_drv->erase_sector(chip, start);
  302. start += sector_size;
  303. len -= sector_size;
  304. }
  305. err = spiflash_end(chip, err);
  306. }
  307. return err;
  308. }
  309. esp_err_t IRAM_ATTR esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *write_protected)
  310. {
  311. VERIFY_OP(get_chip_write_protect);
  312. if (write_protected == NULL) {
  313. return ESP_ERR_INVALID_ARG;
  314. }
  315. esp_err_t err = spiflash_start(chip);
  316. if (err != ESP_OK) {
  317. return err;
  318. }
  319. err = chip->chip_drv->get_chip_write_protect(chip, write_protected);
  320. return spiflash_end(chip, err);
  321. }
  322. esp_err_t IRAM_ATTR esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect)
  323. {
  324. VERIFY_OP(set_chip_write_protect);
  325. //TODO: skip writing if already locked or unlocked
  326. esp_err_t err = spiflash_start(chip);
  327. if (err != ESP_OK) {
  328. return err;
  329. }
  330. err = chip->chip_drv->set_chip_write_protect(chip, write_protect);
  331. return spiflash_end(chip, err);
  332. }
  333. esp_err_t esp_flash_get_protectable_regions(const esp_flash_t *chip, const esp_flash_region_t **out_regions, uint32_t *out_num_regions)
  334. {
  335. if(out_num_regions != NULL) {
  336. *out_num_regions = 0; // In case caller doesn't check result
  337. }
  338. VERIFY_OP(get_protected_regions);
  339. if(out_regions == NULL || out_num_regions == NULL) {
  340. return ESP_ERR_INVALID_ARG;
  341. }
  342. *out_num_regions = chip->chip_drv->num_protectable_regions;
  343. *out_regions = chip->chip_drv->protectable_regions;
  344. return ESP_OK;
  345. }
  346. static esp_err_t find_region(const esp_flash_t *chip, const esp_flash_region_t *region, uint8_t *index)
  347. {
  348. if (region == NULL) {
  349. return ESP_ERR_INVALID_ARG;
  350. }
  351. for(*index = 0; *index < chip->chip_drv->num_protectable_regions; (*index)++) {
  352. if (memcmp(&chip->chip_drv->protectable_regions[*index],
  353. region, sizeof(esp_flash_region_t)) == 0) {
  354. return ESP_OK;
  355. }
  356. }
  357. return ESP_ERR_NOT_FOUND;
  358. }
  359. esp_err_t IRAM_ATTR esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected)
  360. {
  361. VERIFY_OP(get_protected_regions);
  362. if (out_protected == NULL) {
  363. return ESP_ERR_INVALID_ARG;
  364. }
  365. uint8_t index;
  366. esp_err_t err = find_region(chip, region, &index);
  367. if (err != ESP_OK) {
  368. return err;
  369. }
  370. uint64_t protection_mask = 0;
  371. err = spiflash_start(chip);
  372. if (err != ESP_OK) {
  373. return err;
  374. }
  375. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  376. if (err == ESP_OK) {
  377. *out_protected = protection_mask & (1LL << index);
  378. }
  379. return spiflash_end(chip, err);
  380. }
  381. esp_err_t IRAM_ATTR esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect)
  382. {
  383. VERIFY_OP(set_protected_regions);
  384. uint8_t index;
  385. esp_err_t err = find_region(chip, region, &index);
  386. if (err != ESP_OK) {
  387. return err;
  388. }
  389. uint64_t protection_mask = 0;
  390. err = spiflash_start(chip);
  391. if (err != ESP_OK) {
  392. return err;
  393. }
  394. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  395. if (err == ESP_OK) {
  396. if (protect) {
  397. protection_mask |= (1LL << index);
  398. } else {
  399. protection_mask &= ~(1LL << index);
  400. }
  401. err = chip->chip_drv->set_protected_regions(chip, protection_mask);
  402. }
  403. return spiflash_end(chip, err);
  404. }
  405. esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  406. {
  407. if (length == 0) {
  408. return ESP_OK;
  409. }
  410. VERIFY_OP(read);
  411. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  412. return ESP_ERR_INVALID_ARG;
  413. }
  414. //when the cache is disabled, only the DRAM can be read, check whether we need to receive in another buffer in DRAM.
  415. bool direct_read = chip->host->supports_direct_read(chip->host, buffer);
  416. uint8_t* temp_buffer = NULL;
  417. if (!direct_read) {
  418. uint32_t length_to_allocate = MAX(MAX_READ_CHUNK, length);
  419. length_to_allocate = (length_to_allocate+3)&(~3);
  420. temp_buffer = heap_caps_malloc(length_to_allocate, MALLOC_CAP_INTERNAL | MALLOC_CAP_8BIT);
  421. ESP_LOGV(TAG, "allocate temp buffer: %p", temp_buffer);
  422. if (temp_buffer == NULL) return ESP_ERR_NO_MEM;
  423. }
  424. esp_err_t err = ESP_OK;
  425. do {
  426. err = spiflash_start(chip);
  427. if (err != ESP_OK) {
  428. break;
  429. }
  430. //if required (dma buffer allocated), read to the buffer instead of the original buffer
  431. uint8_t* buffer_to_read = (temp_buffer)? temp_buffer : buffer;
  432. //each time, we at most read this length
  433. //after that, we release the lock to allow some other operations
  434. uint32_t length_to_read = MIN(MAX_READ_CHUNK, length);
  435. if (err == ESP_OK) {
  436. err = chip->chip_drv->read(chip, buffer_to_read, address, length_to_read);
  437. }
  438. if (err != ESP_OK) {
  439. spiflash_end(chip, err);
  440. break;
  441. }
  442. //even if this is failed, the data is still valid, copy before quit
  443. err = spiflash_end(chip, err);
  444. //copy back to the original buffer
  445. if (temp_buffer) {
  446. memcpy(buffer, temp_buffer, length_to_read);
  447. }
  448. address += length_to_read;
  449. length -= length_to_read;
  450. buffer += length_to_read;
  451. } while (err == ESP_OK && length > 0);
  452. free(temp_buffer);
  453. return err;
  454. }
  455. esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  456. {
  457. if (length == 0) {
  458. return ESP_OK;
  459. }
  460. VERIFY_OP(write);
  461. CHECK_WRITE_ADDRESS(chip, address, length);
  462. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  463. return ESP_ERR_INVALID_ARG;
  464. }
  465. //when the cache is disabled, only the DRAM can be read, check whether we need to copy the data first
  466. bool direct_write = chip->host->supports_direct_write(chip->host, buffer);
  467. esp_err_t err = ESP_OK;
  468. /* Write output in chunks, either by buffering on stack or
  469. by artificially cutting into MAX_WRITE_CHUNK parts (in an OS
  470. environment, this prevents writing from causing interrupt or higher priority task
  471. starvation.) */
  472. do {
  473. uint32_t write_len;
  474. const void *write_buf;
  475. if (direct_write) {
  476. write_len = MIN(length, MAX_WRITE_CHUNK);
  477. write_buf = buffer;
  478. } else {
  479. uint32_t buf[8];
  480. write_len = MIN(length, sizeof(buf));
  481. memcpy(buf, buffer, write_len);
  482. write_buf = buf;
  483. }
  484. err = spiflash_start(chip);
  485. if (err != ESP_OK) {
  486. return err;
  487. }
  488. err = chip->chip_drv->write(chip, write_buf, address, write_len);
  489. address += write_len;
  490. buffer = (void *)((intptr_t)buffer + write_len);
  491. length -= write_len;
  492. err = spiflash_end(chip, err);
  493. } while (err == ESP_OK && length > 0);
  494. return err;
  495. }
  496. esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
  497. {
  498. VERIFY_OP(write_encrypted);
  499. if (((memspi_host_data_t*)chip->host->driver_data)->spi != 0) {
  500. // Encrypted operations have to use SPI0
  501. return ESP_ERR_FLASH_UNSUPPORTED_HOST;
  502. }
  503. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  504. return ESP_ERR_INVALID_ARG;
  505. }
  506. esp_err_t err = spiflash_start(chip);
  507. if (err != ESP_OK) {
  508. return err;
  509. }
  510. err = chip->chip_drv->write_encrypted(chip, buffer, address, length);
  511. return spiflash_end(chip, err);
  512. }
  513. inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len)
  514. {
  515. uint32_t a_end = a_start + a_len;
  516. uint32_t b_end = b_start + b_len;
  517. return (a_end > b_start && b_end > a_start);
  518. }
  519. /*------------------------------------------------------------------------------
  520. Adapter layer to original api before IDF v4.0
  521. ------------------------------------------------------------------------------*/
  522. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  523. static esp_err_t spi_flash_translate_rc(esp_err_t err)
  524. {
  525. switch (err) {
  526. case ESP_OK:
  527. return ESP_OK;
  528. case ESP_ERR_INVALID_ARG:
  529. return ESP_ERR_INVALID_ARG;
  530. case ESP_ERR_FLASH_NOT_INITIALISED:
  531. case ESP_ERR_FLASH_PROTECTED:
  532. return ESP_ERR_INVALID_STATE;
  533. case ESP_ERR_NOT_FOUND:
  534. case ESP_ERR_FLASH_UNSUPPORTED_HOST:
  535. case ESP_ERR_FLASH_UNSUPPORTED_CHIP:
  536. return ESP_ERR_NOT_SUPPORTED;
  537. case ESP_ERR_FLASH_NO_RESPONSE:
  538. return ESP_ERR_INVALID_RESPONSE;
  539. default:
  540. ESP_EARLY_LOGE(TAG, "unexpected spi flash error code: %x", err);
  541. abort();
  542. }
  543. return ESP_OK;
  544. }
  545. esp_err_t spi_flash_erase_range(uint32_t start_addr, uint32_t size)
  546. {
  547. esp_err_t err = esp_flash_erase_region(NULL, start_addr, size);
  548. return spi_flash_translate_rc(err);
  549. }
  550. esp_err_t spi_flash_write(size_t dst, const void *srcv, size_t size)
  551. {
  552. esp_err_t err = esp_flash_write(NULL, srcv, dst, size);
  553. return spi_flash_translate_rc(err);
  554. }
  555. esp_err_t spi_flash_read(size_t src, void *dstv, size_t size)
  556. {
  557. esp_err_t err = esp_flash_read(NULL, dstv, src, size);
  558. return spi_flash_translate_rc(err);
  559. }
  560. esp_err_t spi_flash_unlock(void)
  561. {
  562. esp_err_t err = esp_flash_set_chip_write_protect(NULL, false);
  563. return spi_flash_translate_rc(err);
  564. }
  565. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL