spi_flash_emulation.cpp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2015-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. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include "esp_spi_flash.h"
  14. #include "spi_flash_emulation.h"
  15. static SpiFlashEmulator* s_emulator = nullptr;
  16. void spi_flash_emulator_set(SpiFlashEmulator* e)
  17. {
  18. s_emulator = e;
  19. }
  20. esp_err_t spi_flash_erase_sector(size_t sec)
  21. {
  22. if (!s_emulator) {
  23. return ESP_ERR_FLASH_OP_TIMEOUT;
  24. }
  25. if (!s_emulator->erase(sec)) {
  26. return ESP_ERR_FLASH_OP_FAIL;
  27. }
  28. return ESP_OK;
  29. }
  30. esp_err_t spi_flash_write(size_t des_addr, const void *src_addr, size_t size)
  31. {
  32. if (!s_emulator) {
  33. return ESP_ERR_FLASH_OP_TIMEOUT;
  34. }
  35. if (!s_emulator->write(des_addr, reinterpret_cast<const uint32_t*>(src_addr), size)) {
  36. return ESP_ERR_FLASH_OP_FAIL;
  37. }
  38. return ESP_OK;
  39. }
  40. esp_err_t spi_flash_read(size_t src_addr, void *des_addr, size_t size)
  41. {
  42. if (!s_emulator) {
  43. return ESP_ERR_FLASH_OP_TIMEOUT;
  44. }
  45. if (!s_emulator->read(reinterpret_cast<uint32_t*>(des_addr), src_addr, size)) {
  46. return ESP_ERR_FLASH_OP_FAIL;
  47. }
  48. return ESP_OK;
  49. }
  50. // timing data for ESP8266, 160MHz CPU frequency, 80MHz flash requency
  51. // all values in microseconds
  52. // values are for block sizes starting at 4 bytes and going up to 4096 bytes
  53. static size_t readTimes[] = {7, 5, 6, 7, 11, 18, 32, 60, 118, 231, 459};
  54. static size_t writeTimes[] = {19, 23, 35, 57, 106, 205, 417, 814, 1622, 3200, 6367};
  55. static size_t blockEraseTime = 37142;
  56. static size_t timeInterp(uint32_t bytes, size_t* lut)
  57. {
  58. int lz = __builtin_clz(bytes / 4);
  59. int log_size = 32 - lz;
  60. size_t x2 = 1 << (log_size + 2);
  61. size_t y2 = lut[log_size];
  62. size_t x1 = 1 << (log_size + 1);
  63. size_t y1 = lut[log_size - 1];
  64. return (bytes - x1) * (y2 - y1) / (x2 - x1) + y1;
  65. }
  66. size_t SpiFlashEmulator::getReadOpTime(uint32_t bytes)
  67. {
  68. return timeInterp(bytes, readTimes);
  69. }
  70. size_t SpiFlashEmulator::getWriteOpTime(uint32_t bytes)
  71. {
  72. return timeInterp(bytes, writeTimes);
  73. }
  74. size_t SpiFlashEmulator::getEraseOpTime()
  75. {
  76. return blockEraseTime;
  77. }