spi_flash_emulation.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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. const int lut_size = sizeof(readTimes)/sizeof(readTimes[0]);
  59. int lz = __builtin_clz(bytes / 4);
  60. int log_size = 32 - lz;
  61. size_t x2 = 1 << (log_size + 2);
  62. size_t y2 = lut[std::min(log_size, lut_size - 1)];
  63. size_t x1 = 1 << (log_size + 1);
  64. size_t y1 = lut[log_size - 1];
  65. return (bytes - x1) * (y2 - y1) / (x2 - x1) + y1;
  66. }
  67. size_t SpiFlashEmulator::getReadOpTime(uint32_t bytes)
  68. {
  69. return timeInterp(bytes, readTimes);
  70. }
  71. size_t SpiFlashEmulator::getWriteOpTime(uint32_t bytes)
  72. {
  73. return timeInterp(bytes, writeTimes);
  74. }
  75. size_t SpiFlashEmulator::getEraseOpTime()
  76. {
  77. return blockEraseTime;
  78. }