esp_flash_api.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796
  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. #include "esp_flash_internal.h"
  24. #include <freertos/task.h>
  25. #include "esp_timer.h"
  26. static const char TAG[] = "spi_flash";
  27. #ifdef CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  28. #define MAX_WRITE_CHUNK CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE /* write in chunks */
  29. #else
  30. #define MAX_WRITE_CHUNK 8192 /* write in chunks */
  31. #endif // CONFIG_SPI_FLASH_WRITE_CHUNK_SIZE
  32. #define MAX_READ_CHUNK 16384
  33. #ifdef CONFIG_SPI_FLASH_DANGEROUS_WRITE_ABORTS
  34. #define UNSAFE_WRITE_ADDRESS abort()
  35. #else
  36. #define UNSAFE_WRITE_ADDRESS return ESP_ERR_INVALID_ARG
  37. #endif
  38. /* CHECK_WRITE_ADDRESS macro to fail writes which land in the
  39. bootloader, partition table, or running application region.
  40. */
  41. #if CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  42. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE)
  43. #else /* FAILS or ABORTS */
  44. #define CHECK_WRITE_ADDRESS(CHIP, ADDR, SIZE) do { \
  45. if (CHIP && CHIP->os_func->region_protected && CHIP->os_func->region_protected(CHIP->os_func_data, ADDR, SIZE)) { \
  46. UNSAFE_WRITE_ADDRESS; \
  47. } \
  48. } while(0)
  49. #endif // CONFIG_SPI_FLASH_DANGEROUS_WRITE_ALLOWED
  50. #define IO_STR_LEN 7
  51. static const char io_mode_str[][IO_STR_LEN] = {
  52. "slowrd",
  53. "fastrd",
  54. "dout",
  55. "dio",
  56. "qout",
  57. "qio",
  58. };
  59. _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_io_mode_t defined in spi_flash_ll.h");
  60. esp_err_t esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* flash_id);
  61. static esp_err_t spiflash_start_default(esp_flash_t *chip);
  62. static esp_err_t spiflash_end_default(esp_flash_t *chip, esp_err_t err);
  63. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip);
  64. typedef struct {
  65. esp_err_t (*start)(esp_flash_t *chip);
  66. esp_err_t (*end)(esp_flash_t *chip, esp_err_t err);
  67. esp_err_t (*chip_check)(esp_flash_t **inout_chip);
  68. } rom_spiflash_api_func_t;
  69. // These functions can be placed in the ROM. For now we use the code in IDF.
  70. DRAM_ATTR static rom_spiflash_api_func_t default_spiflash_rom_api = {
  71. .start = spiflash_start_default,
  72. .end = spiflash_end_default,
  73. .chip_check = check_chip_pointer_default,
  74. };
  75. DRAM_ATTR rom_spiflash_api_func_t *rom_spiflash_api_funcs = &default_spiflash_rom_api;
  76. /* Static function to notify OS of a new SPI flash operation.
  77. If returns an error result, caller must abort. If returns ESP_OK, caller must
  78. call rom_spiflash_api_funcs->end() before returning.
  79. */
  80. static esp_err_t IRAM_ATTR spiflash_start_default(esp_flash_t *chip)
  81. {
  82. if (chip->os_func != NULL && chip->os_func->start != NULL) {
  83. esp_err_t err = chip->os_func->start(chip->os_func_data);
  84. if (err != ESP_OK) {
  85. return err;
  86. }
  87. }
  88. chip->host->driver->dev_config(chip->host);
  89. return ESP_OK;
  90. }
  91. /* Static function to notify OS that SPI flash operation is complete.
  92. */
  93. static esp_err_t IRAM_ATTR spiflash_end_default(esp_flash_t *chip, esp_err_t err)
  94. {
  95. if (chip->os_func != NULL
  96. && chip->os_func->end != NULL) {
  97. esp_err_t end_err = chip->os_func->end(chip->os_func_data);
  98. if (err == ESP_OK) {
  99. err = end_err; // Only return the 'end' error if we haven't already failed
  100. }
  101. }
  102. return err;
  103. }
  104. // check that the 'chip' parameter is properly initialised
  105. static esp_err_t check_chip_pointer_default(esp_flash_t **inout_chip)
  106. {
  107. esp_flash_t *chip = *inout_chip;
  108. if (chip == NULL) {
  109. chip = esp_flash_default_chip;
  110. }
  111. *inout_chip = chip;
  112. if (chip == NULL || !esp_flash_chip_driver_initialized(chip)) {
  113. return ESP_ERR_FLASH_NOT_INITIALISED;
  114. }
  115. return ESP_OK;
  116. }
  117. /* Return true if regions 'a' and 'b' overlap at all, based on their start offsets and lengths. */
  118. inline static bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len);
  119. /* Top-level API functions, calling into chip_drv functions via chip->drv */
  120. static esp_err_t detect_spi_flash_chip(esp_flash_t *chip);
  121. bool esp_flash_chip_driver_initialized(const esp_flash_t *chip)
  122. {
  123. if (!chip->chip_drv) return false;
  124. return true;
  125. }
  126. esp_err_t IRAM_ATTR esp_flash_init(esp_flash_t *chip)
  127. {
  128. esp_err_t err = ESP_OK;
  129. if (chip == NULL || chip->host == NULL || chip->host->driver == NULL ||
  130. ((memspi_host_inst_t*)chip->host)->spi == NULL) {
  131. return ESP_ERR_INVALID_ARG;
  132. }
  133. //read chip id
  134. uint32_t flash_id;
  135. int retries = 10;
  136. do {
  137. err = esp_flash_read_chip_id(chip, &flash_id);
  138. } while (err == ESP_ERR_FLASH_NOT_INITIALISED && retries-- > 0);
  139. if (err != ESP_OK) {
  140. return err;
  141. }
  142. chip->chip_id = flash_id;
  143. if (!esp_flash_chip_driver_initialized(chip)) {
  144. // Detect chip_drv
  145. err = detect_spi_flash_chip(chip);
  146. if (err != ESP_OK) {
  147. return err;
  148. }
  149. }
  150. // Detect flash size
  151. uint32_t size;
  152. err = esp_flash_get_size(chip, &size);
  153. if (err != ESP_OK) {
  154. ESP_LOGE(TAG, "failed to get chip size");
  155. return err;
  156. }
  157. ESP_LOGI(TAG, "flash io: %s", io_mode_str[chip->read_mode]);
  158. err = rom_spiflash_api_funcs->start(chip);
  159. if (err != ESP_OK) {
  160. return err;
  161. }
  162. if (err == ESP_OK) {
  163. // Try to set the flash mode to whatever default mode was chosen
  164. err = chip->chip_drv->set_io_mode(chip);
  165. if (err == ESP_ERR_FLASH_NO_RESPONSE && !esp_flash_is_quad_mode(chip)) {
  166. //some chips (e.g. Winbond) don't support to clear QE, treat as success
  167. err = ESP_OK;
  168. }
  169. }
  170. // Done: all fields on 'chip' are initialised
  171. return rom_spiflash_api_funcs->end(chip, err);
  172. }
  173. //this is not public, but useful in unit tests
  174. esp_err_t IRAM_ATTR esp_flash_read_chip_id(esp_flash_t* chip, uint32_t* flash_id)
  175. {
  176. esp_err_t err = rom_spiflash_api_funcs->start(chip);
  177. if (err != ESP_OK) {
  178. return err;
  179. }
  180. // Send generic RDID command twice, check for a matching result and retry in case we just powered on (inner
  181. // function fails if it sees all-ones or all-zeroes.)
  182. err = chip->host->driver->read_id(chip->host, flash_id);
  183. if (err == ESP_OK) { // check we see the same ID twice, in case of transient power-on errors
  184. uint32_t new_id;
  185. err = chip->host->driver->read_id(chip->host, &new_id);
  186. if (err == ESP_OK && (new_id != *flash_id)) {
  187. err = ESP_ERR_FLASH_NOT_INITIALISED;
  188. }
  189. }
  190. return rom_spiflash_api_funcs->end(chip, err);
  191. }
  192. static esp_err_t IRAM_ATTR detect_spi_flash_chip(esp_flash_t *chip)
  193. {
  194. esp_err_t err;
  195. uint32_t flash_id = chip->chip_id;
  196. // Detect the chip and set the chip_drv structure for it
  197. const spi_flash_chip_t **drivers = esp_flash_registered_chips;
  198. while (*drivers != NULL && !esp_flash_chip_driver_initialized(chip)) {
  199. chip->chip_drv = *drivers;
  200. // start/end SPI operation each time, for multitasking
  201. // and also so esp_flash_registered_flash_drivers can live in flash
  202. ESP_LOGD(TAG, "trying chip: %s", chip->chip_drv->name);
  203. err = rom_spiflash_api_funcs->start(chip);
  204. if (err != ESP_OK) {
  205. return err;
  206. }
  207. if (chip->chip_drv->probe(chip, flash_id) != ESP_OK) {
  208. chip->chip_drv = NULL;
  209. }
  210. // if probe succeeded, chip->drv stays set
  211. drivers++;
  212. err = rom_spiflash_api_funcs->end(chip, err);
  213. if (err != ESP_OK) {
  214. return err;
  215. }
  216. }
  217. if (!esp_flash_chip_driver_initialized(chip)) {
  218. return ESP_ERR_NOT_FOUND;
  219. }
  220. ESP_LOGI(TAG, "detected chip: %s", chip->chip_drv->name);
  221. return ESP_OK;
  222. }
  223. /* Convenience macro for beginning of all API functions.
  224. * Check the return value of `rom_spiflash_api_funcs->chip_check` is correct,
  225. * and the chip supports the operation in question.
  226. */
  227. #define VERIFY_CHIP_OP(OP) do { \
  228. if (err != ESP_OK) return err; \
  229. if (chip->chip_drv->OP == NULL) { \
  230. return ESP_ERR_FLASH_UNSUPPORTED_CHIP; \
  231. } \
  232. } while (0)
  233. esp_err_t IRAM_ATTR esp_flash_read_id(esp_flash_t *chip, uint32_t *out_id)
  234. {
  235. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  236. //Accept uninitialized chip when reading chip id
  237. if (err != ESP_OK && !(err == ESP_ERR_FLASH_NOT_INITIALISED && chip != NULL)) return err;
  238. if (out_id == NULL) return ESP_ERR_INVALID_ARG;
  239. err = rom_spiflash_api_funcs->start(chip);
  240. if (err != ESP_OK) {
  241. return err;
  242. }
  243. err = chip->host->driver->read_id(chip->host, out_id);
  244. return rom_spiflash_api_funcs->end(chip, err);
  245. }
  246. esp_err_t IRAM_ATTR esp_flash_get_size(esp_flash_t *chip, uint32_t *out_size)
  247. {
  248. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  249. VERIFY_CHIP_OP(detect_size);
  250. if (out_size == NULL) {
  251. return ESP_ERR_INVALID_ARG;
  252. }
  253. if (chip->size != 0) {
  254. *out_size = chip->size;
  255. return ESP_OK;
  256. }
  257. err = rom_spiflash_api_funcs->start(chip);
  258. if (err != ESP_OK) {
  259. return err;
  260. }
  261. uint32_t detect_size;
  262. err = chip->chip_drv->detect_size(chip, &detect_size);
  263. if (err == ESP_OK) {
  264. chip->size = detect_size;
  265. }
  266. return rom_spiflash_api_funcs->end(chip, err);
  267. }
  268. esp_err_t IRAM_ATTR esp_flash_erase_chip(esp_flash_t *chip)
  269. {
  270. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  271. VERIFY_CHIP_OP(erase_chip);
  272. CHECK_WRITE_ADDRESS(chip, 0, chip->size);
  273. err = rom_spiflash_api_funcs->start(chip);
  274. if (err != ESP_OK) {
  275. return err;
  276. }
  277. err = chip->chip_drv->erase_chip(chip);
  278. return rom_spiflash_api_funcs->end(chip, err);
  279. }
  280. esp_err_t IRAM_ATTR esp_flash_erase_region(esp_flash_t *chip, uint32_t start, uint32_t len)
  281. {
  282. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  283. VERIFY_CHIP_OP(erase_sector);
  284. VERIFY_CHIP_OP(erase_block);
  285. CHECK_WRITE_ADDRESS(chip, start, len);
  286. uint32_t block_erase_size = chip->chip_drv->erase_block == NULL ? 0 : chip->chip_drv->block_erase_size;
  287. uint32_t sector_size = chip->chip_drv->sector_size;
  288. if (sector_size == 0 || (block_erase_size % sector_size) != 0) {
  289. return ESP_ERR_FLASH_NOT_INITIALISED;
  290. }
  291. if (start > chip->size || start + len > chip->size) {
  292. return ESP_ERR_INVALID_ARG;
  293. }
  294. if ((start % chip->chip_drv->sector_size) != 0 || (len % chip->chip_drv->sector_size) != 0) {
  295. // Can only erase multiples of the sector size, starting at sector boundary
  296. return ESP_ERR_INVALID_ARG;
  297. }
  298. err = ESP_OK;
  299. // Check for write protected regions overlapping the erase region
  300. if (chip->chip_drv->get_protected_regions != NULL &&
  301. chip->chip_drv->num_protectable_regions > 0) {
  302. err = rom_spiflash_api_funcs->start(chip);
  303. if (err != ESP_OK) {
  304. return err;
  305. }
  306. uint64_t protected = 0;
  307. err = chip->chip_drv->get_protected_regions(chip, &protected);
  308. if (err == ESP_OK && protected != 0) {
  309. for (int i = 0; i < chip->chip_drv->num_protectable_regions && err == ESP_OK; i++) {
  310. const esp_flash_region_t *region = &chip->chip_drv->protectable_regions[i];
  311. if ((protected & BIT64(i))
  312. && regions_overlap(start, len, region->offset, region->size)) {
  313. err = ESP_ERR_FLASH_PROTECTED;
  314. }
  315. }
  316. }
  317. // Don't lock the SPI flash for the entire erase, as this may be very long
  318. err = rom_spiflash_api_funcs->end(chip, err);
  319. }
  320. #ifdef CONFIG_SPI_FLASH_YIELD_DURING_ERASE
  321. int64_t no_yield_time_us = 0;
  322. #endif
  323. while (err == ESP_OK && len >= sector_size) {
  324. #ifdef CONFIG_SPI_FLASH_YIELD_DURING_ERASE
  325. int64_t start_time_us = esp_timer_get_time();
  326. #endif
  327. err = rom_spiflash_api_funcs->start(chip);
  328. if (err != ESP_OK) {
  329. return err;
  330. }
  331. #ifndef CONFIG_SPI_FLASH_BYPASS_BLOCK_ERASE
  332. // If possible erase an entire multi-sector block
  333. if (block_erase_size > 0 && len >= block_erase_size && (start % block_erase_size) == 0) {
  334. err = chip->chip_drv->erase_block(chip, start);
  335. start += block_erase_size;
  336. len -= block_erase_size;
  337. } else
  338. #endif
  339. {
  340. // Otherwise erase individual sector only
  341. err = chip->chip_drv->erase_sector(chip, start);
  342. start += sector_size;
  343. len -= sector_size;
  344. }
  345. err = rom_spiflash_api_funcs->end(chip, err);
  346. #ifdef CONFIG_SPI_FLASH_YIELD_DURING_ERASE
  347. no_yield_time_us += (esp_timer_get_time() - start_time_us);
  348. if (no_yield_time_us / 1000 >= CONFIG_SPI_FLASH_ERASE_YIELD_DURATION_MS) {
  349. no_yield_time_us = 0;
  350. if (chip->os_func->yield) {
  351. chip->os_func->yield(chip->os_func_data);
  352. }
  353. }
  354. #endif
  355. }
  356. return err;
  357. }
  358. esp_err_t IRAM_ATTR esp_flash_get_chip_write_protect(esp_flash_t *chip, bool *out_write_protected)
  359. {
  360. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  361. VERIFY_CHIP_OP(get_chip_write_protect);
  362. if (out_write_protected == NULL) {
  363. return ESP_ERR_INVALID_ARG;
  364. }
  365. err = rom_spiflash_api_funcs->start(chip);
  366. if (err != ESP_OK) {
  367. return err;
  368. }
  369. err = chip->chip_drv->get_chip_write_protect(chip, out_write_protected);
  370. return rom_spiflash_api_funcs->end(chip, err);
  371. }
  372. esp_err_t IRAM_ATTR esp_flash_set_chip_write_protect(esp_flash_t *chip, bool write_protect)
  373. {
  374. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  375. VERIFY_CHIP_OP(set_chip_write_protect);
  376. //TODO: skip writing if already locked or unlocked
  377. err = rom_spiflash_api_funcs->start(chip);
  378. if (err != ESP_OK) {
  379. return err;
  380. }
  381. err = chip->chip_drv->set_chip_write_protect(chip, write_protect);
  382. return rom_spiflash_api_funcs->end(chip, err);
  383. }
  384. 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)
  385. {
  386. if(out_num_regions != NULL) {
  387. *out_num_regions = 0; // In case caller doesn't check result
  388. }
  389. esp_err_t err = rom_spiflash_api_funcs->chip_check((esp_flash_t **)&chip);
  390. VERIFY_CHIP_OP(get_protected_regions);
  391. if(out_regions == NULL || out_num_regions == NULL) {
  392. return ESP_ERR_INVALID_ARG;
  393. }
  394. *out_num_regions = chip->chip_drv->num_protectable_regions;
  395. *out_regions = chip->chip_drv->protectable_regions;
  396. return ESP_OK;
  397. }
  398. static esp_err_t find_region(const esp_flash_t *chip, const esp_flash_region_t *region, uint8_t *index)
  399. {
  400. if (region == NULL) {
  401. return ESP_ERR_INVALID_ARG;
  402. }
  403. for(*index = 0; *index < chip->chip_drv->num_protectable_regions; (*index)++) {
  404. if (memcmp(&chip->chip_drv->protectable_regions[*index],
  405. region, sizeof(esp_flash_region_t)) == 0) {
  406. return ESP_OK;
  407. }
  408. }
  409. return ESP_ERR_NOT_FOUND;
  410. }
  411. esp_err_t IRAM_ATTR esp_flash_get_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool *out_protected)
  412. {
  413. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  414. VERIFY_CHIP_OP(get_protected_regions);
  415. if (out_protected == NULL) {
  416. return ESP_ERR_INVALID_ARG;
  417. }
  418. uint8_t index;
  419. err = find_region(chip, region, &index);
  420. if (err != ESP_OK) {
  421. return err;
  422. }
  423. uint64_t protection_mask = 0;
  424. err = rom_spiflash_api_funcs->start(chip);
  425. if (err != ESP_OK) {
  426. return err;
  427. }
  428. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  429. if (err == ESP_OK) {
  430. *out_protected = protection_mask & (1LL << index);
  431. }
  432. return rom_spiflash_api_funcs->end(chip, err);
  433. }
  434. esp_err_t IRAM_ATTR esp_flash_set_protected_region(esp_flash_t *chip, const esp_flash_region_t *region, bool protect)
  435. {
  436. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  437. VERIFY_CHIP_OP(set_protected_regions);
  438. uint8_t index;
  439. err = find_region(chip, region, &index);
  440. if (err != ESP_OK) {
  441. return err;
  442. }
  443. uint64_t protection_mask = 0;
  444. err = rom_spiflash_api_funcs->start(chip);
  445. if (err != ESP_OK) {
  446. return err;
  447. }
  448. err = chip->chip_drv->get_protected_regions(chip, &protection_mask);
  449. if (err == ESP_OK) {
  450. if (protect) {
  451. protection_mask |= (1LL << index);
  452. } else {
  453. protection_mask &= ~(1LL << index);
  454. }
  455. err = chip->chip_drv->set_protected_regions(chip, protection_mask);
  456. }
  457. return rom_spiflash_api_funcs->end(chip, err);
  458. }
  459. esp_err_t IRAM_ATTR esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
  460. {
  461. if (length == 0) {
  462. return ESP_OK;
  463. }
  464. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  465. VERIFY_CHIP_OP(read);
  466. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  467. return ESP_ERR_INVALID_ARG;
  468. }
  469. //when the cache is disabled, only the DRAM can be read, check whether we need to receive in another buffer in DRAM.
  470. bool direct_read = chip->host->driver->supports_direct_read(chip->host, buffer);
  471. uint8_t* temp_buffer = NULL;
  472. //each time, we at most read this length
  473. //after that, we release the lock to allow some other operations
  474. size_t read_chunk_size = MIN(MAX_READ_CHUNK, length);
  475. if (!direct_read) {
  476. size_t actual_len = 0;
  477. if (chip->os_func->get_temp_buffer != NULL) {
  478. temp_buffer = chip->os_func->get_temp_buffer(chip->os_func_data, read_chunk_size, &actual_len);
  479. read_chunk_size = actual_len;
  480. }
  481. if (temp_buffer == NULL) {
  482. return ESP_ERR_NO_MEM;
  483. }
  484. }
  485. err = ESP_OK;
  486. do {
  487. err = rom_spiflash_api_funcs->start(chip);
  488. if (err != ESP_OK) {
  489. break;
  490. }
  491. //if required (dma buffer allocated), read to the buffer instead of the original buffer
  492. uint8_t* buffer_to_read = (temp_buffer)? temp_buffer : buffer;
  493. // Length we will read this iteration is either the chunk size or the remaining length, whichever is smaller
  494. size_t length_to_read = MIN(read_chunk_size, length);
  495. if (err == ESP_OK) {
  496. err = chip->chip_drv->read(chip, buffer_to_read, address, length_to_read);
  497. }
  498. if (err != ESP_OK) {
  499. rom_spiflash_api_funcs->end(chip, err);
  500. break;
  501. }
  502. //even if this is failed, the data is still valid, copy before quit
  503. err = rom_spiflash_api_funcs->end(chip, err);
  504. //copy back to the original buffer
  505. if (temp_buffer) {
  506. memcpy(buffer, temp_buffer, length_to_read);
  507. }
  508. address += length_to_read;
  509. length -= length_to_read;
  510. buffer = (void*)((intptr_t)buffer + length_to_read);
  511. } while (err == ESP_OK && length > 0);
  512. if (chip->os_func->release_temp_buffer != NULL) {
  513. chip->os_func->release_temp_buffer(chip->os_func_data, temp_buffer);
  514. }
  515. return err;
  516. }
  517. esp_err_t IRAM_ATTR esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
  518. {
  519. if (length == 0) {
  520. return ESP_OK;
  521. }
  522. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  523. VERIFY_CHIP_OP(write);
  524. CHECK_WRITE_ADDRESS(chip, address, length);
  525. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  526. return ESP_ERR_INVALID_ARG;
  527. }
  528. //when the cache is disabled, only the DRAM can be read, check whether we need to copy the data first
  529. bool direct_write = chip->host->driver->supports_direct_write(chip->host, buffer);
  530. err = ESP_OK;
  531. /* Write output in chunks, either by buffering on stack or
  532. by artificially cutting into MAX_WRITE_CHUNK parts (in an OS
  533. environment, this prevents writing from causing interrupt or higher priority task
  534. starvation.) */
  535. do {
  536. uint32_t write_len;
  537. const void *write_buf;
  538. if (direct_write) {
  539. write_len = MIN(length, MAX_WRITE_CHUNK);
  540. write_buf = buffer;
  541. } else {
  542. uint32_t buf[8];
  543. write_len = MIN(length, sizeof(buf));
  544. memcpy(buf, buffer, write_len);
  545. write_buf = buf;
  546. }
  547. err = rom_spiflash_api_funcs->start(chip);
  548. if (err != ESP_OK) {
  549. return err;
  550. }
  551. err = chip->chip_drv->write(chip, write_buf, address, write_len);
  552. address += write_len;
  553. buffer = (void *)((intptr_t)buffer + write_len);
  554. length -= write_len;
  555. err = rom_spiflash_api_funcs->end(chip, err);
  556. } while (err == ESP_OK && length > 0);
  557. return err;
  558. }
  559. //currently the legacy implementation is used, from flash_ops.c
  560. esp_err_t spi_flash_write_encrypted(size_t dest_addr, const void *src, size_t size);
  561. esp_err_t IRAM_ATTR esp_flash_write_encrypted(esp_flash_t *chip, uint32_t address, const void *buffer, uint32_t length)
  562. {
  563. /*
  564. * Since currently this feature is supported only by the hardware, there
  565. * is no way to support non-standard chips. We use the legacy
  566. * implementation and skip the chip and driver layers.
  567. */
  568. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  569. if (err != ESP_OK) return err;
  570. if (buffer == NULL || address > chip->size || address+length > chip->size) {
  571. return ESP_ERR_INVALID_ARG;
  572. }
  573. return spi_flash_write_encrypted(address, buffer, length);
  574. }
  575. inline static IRAM_ATTR bool regions_overlap(uint32_t a_start, uint32_t a_len,uint32_t b_start, uint32_t b_len)
  576. {
  577. uint32_t a_end = a_start + a_len;
  578. uint32_t b_end = b_start + b_len;
  579. return (a_end > b_start && b_end > a_start);
  580. }
  581. //currently the legacy implementation is used, from flash_ops.c
  582. esp_err_t spi_flash_read_encrypted(size_t src, void *dstv, size_t size);
  583. esp_err_t IRAM_ATTR esp_flash_read_encrypted(esp_flash_t *chip, uint32_t address, void *out_buffer, uint32_t length)
  584. {
  585. /*
  586. * Since currently this feature is supported only by the hardware, there
  587. * is no way to support non-standard chips. We use the legacy
  588. * implementation and skip the chip and driver layers.
  589. */
  590. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  591. if (err != ESP_OK) return err;
  592. return spi_flash_read_encrypted(address, out_buffer, length);
  593. }
  594. // test only, non-public
  595. IRAM_ATTR esp_err_t esp_flash_get_io_mode(esp_flash_t* chip, bool* qe)
  596. {
  597. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  598. VERIFY_CHIP_OP(get_io_mode);
  599. esp_flash_io_mode_t io_mode;
  600. err = rom_spiflash_api_funcs->start(chip);
  601. if (err != ESP_OK) {
  602. return err;
  603. }
  604. err = chip->chip_drv->get_io_mode(chip, &io_mode);
  605. err = rom_spiflash_api_funcs->end(chip, err);
  606. if (err == ESP_OK) {
  607. *qe = (io_mode == SPI_FLASH_QOUT);
  608. }
  609. return err;
  610. }
  611. IRAM_ATTR esp_err_t esp_flash_set_io_mode(esp_flash_t* chip, bool qe)
  612. {
  613. esp_err_t err = rom_spiflash_api_funcs->chip_check(&chip);
  614. VERIFY_CHIP_OP(set_io_mode);
  615. chip->read_mode = (qe? SPI_FLASH_QOUT: SPI_FLASH_SLOWRD);
  616. err = rom_spiflash_api_funcs->start(chip);
  617. if (err != ESP_OK) {
  618. return err;
  619. }
  620. err = chip->chip_drv->set_io_mode(chip);
  621. return rom_spiflash_api_funcs->end(chip, err);
  622. }
  623. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  624. esp_err_t esp_flash_app_disable_protect(bool disable)
  625. {
  626. if (disable) {
  627. return esp_flash_app_disable_os_functions(esp_flash_default_chip);
  628. } else {
  629. return esp_flash_app_enable_os_functions(esp_flash_default_chip);
  630. }
  631. }
  632. #endif
  633. /*------------------------------------------------------------------------------
  634. Adapter layer to original api before IDF v4.0
  635. ------------------------------------------------------------------------------*/
  636. #ifndef CONFIG_SPI_FLASH_USE_LEGACY_IMPL
  637. /* Translate any ESP_ERR_FLASH_xxx error code (new API) to a generic ESP_ERR_xyz error code
  638. */
  639. static IRAM_ATTR esp_err_t spi_flash_translate_rc(esp_err_t err)
  640. {
  641. switch (err) {
  642. case ESP_OK:
  643. case ESP_ERR_INVALID_ARG:
  644. case ESP_ERR_NO_MEM:
  645. return err;
  646. case ESP_ERR_FLASH_NOT_INITIALISED:
  647. case ESP_ERR_FLASH_PROTECTED:
  648. return ESP_ERR_INVALID_STATE;
  649. case ESP_ERR_NOT_FOUND:
  650. case ESP_ERR_FLASH_UNSUPPORTED_HOST:
  651. case ESP_ERR_FLASH_UNSUPPORTED_CHIP:
  652. return ESP_ERR_NOT_SUPPORTED;
  653. case ESP_ERR_FLASH_NO_RESPONSE:
  654. return ESP_ERR_INVALID_RESPONSE;
  655. default:
  656. ESP_EARLY_LOGE(TAG, "unexpected spi flash error code: 0x%x", err);
  657. abort();
  658. }
  659. }
  660. esp_err_t IRAM_ATTR spi_flash_erase_range(uint32_t start_addr, uint32_t size)
  661. {
  662. esp_err_t err = esp_flash_erase_region(NULL, start_addr, size);
  663. return spi_flash_translate_rc(err);
  664. }
  665. esp_err_t IRAM_ATTR spi_flash_write(size_t dst, const void *srcv, size_t size)
  666. {
  667. esp_err_t err = esp_flash_write(NULL, srcv, dst, size);
  668. return spi_flash_translate_rc(err);
  669. }
  670. esp_err_t IRAM_ATTR spi_flash_read(size_t src, void *dstv, size_t size)
  671. {
  672. esp_err_t err = esp_flash_read(NULL, dstv, src, size);
  673. return spi_flash_translate_rc(err);
  674. }
  675. #endif // CONFIG_SPI_FLASH_USE_LEGACY_IMPL