spi_flash_emulation.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_partition.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 esp_partition_erase_range(const esp_partition_t* partition,
  21. size_t offset, size_t size)
  22. {
  23. if (!s_emulator) {
  24. return ESP_ERR_FLASH_OP_TIMEOUT;
  25. }
  26. if (size % SPI_FLASH_SEC_SIZE != 0) {
  27. return ESP_ERR_INVALID_SIZE;
  28. }
  29. if (offset % SPI_FLASH_SEC_SIZE != 0) {
  30. return ESP_ERR_INVALID_ARG;
  31. }
  32. size_t start_sector = offset / SPI_FLASH_SEC_SIZE;
  33. size_t num_sectors = size / SPI_FLASH_SEC_SIZE;
  34. for (size_t sector = start_sector; sector < (start_sector + num_sectors); sector++) {
  35. if (!s_emulator->erase(sector)) {
  36. return ESP_ERR_FLASH_OP_FAIL;
  37. }
  38. }
  39. return ESP_OK;
  40. }
  41. esp_err_t esp_partition_read(const esp_partition_t* partition,
  42. size_t src_offset, void* dst, size_t size)
  43. {
  44. if (!s_emulator) {
  45. return ESP_ERR_FLASH_OP_TIMEOUT;
  46. }
  47. if (!s_emulator->read(reinterpret_cast<uint32_t*>(dst), src_offset, size)) {
  48. return ESP_ERR_FLASH_OP_FAIL;
  49. }
  50. return ESP_OK;
  51. }
  52. esp_err_t esp_partition_read_raw(const esp_partition_t* partition,
  53. size_t src_offset, void* dst, size_t size)
  54. {
  55. if (!s_emulator) {
  56. return ESP_ERR_FLASH_OP_TIMEOUT;
  57. }
  58. if (!s_emulator->read(reinterpret_cast<uint32_t*>(dst), src_offset, size)) {
  59. return ESP_ERR_FLASH_OP_FAIL;
  60. }
  61. return ESP_OK;
  62. }
  63. esp_err_t esp_partition_write(const esp_partition_t* partition,
  64. size_t dst_offset, const void* src, size_t size)
  65. {
  66. if (!s_emulator) {
  67. return ESP_ERR_FLASH_OP_TIMEOUT;
  68. }
  69. if (!s_emulator->write(dst_offset, reinterpret_cast<const uint32_t*>(src), size)) {
  70. return ESP_ERR_FLASH_OP_FAIL;
  71. }
  72. return ESP_OK;
  73. }
  74. esp_err_t esp_partition_write_raw(const esp_partition_t* partition,
  75. size_t dst_offset, const void* src, size_t size)
  76. {
  77. if (!s_emulator) {
  78. return ESP_ERR_FLASH_OP_TIMEOUT;
  79. }
  80. if (!s_emulator->write(dst_offset, reinterpret_cast<const uint32_t*>(src), size)) {
  81. return ESP_ERR_FLASH_OP_FAIL;
  82. }
  83. return ESP_OK;
  84. }
  85. // timing data for ESP8266, 160MHz CPU frequency, 80MHz flash requency
  86. // all values in microseconds
  87. // values are for block sizes starting at 4 bytes and going up to 4096 bytes
  88. static size_t readTimes[] = {7, 5, 6, 7, 11, 18, 32, 60, 118, 231, 459};
  89. static size_t writeTimes[] = {19, 23, 35, 57, 106, 205, 417, 814, 1622, 3200, 6367};
  90. static size_t blockEraseTime = 37142;
  91. static size_t timeInterp(uint32_t bytes, size_t* lut)
  92. {
  93. const int lut_size = sizeof(readTimes)/sizeof(readTimes[0]);
  94. int lz = __builtin_clz(bytes / 4);
  95. int log_size = 32 - lz;
  96. size_t x2 = 1 << (log_size + 2);
  97. size_t y2 = lut[std::min(log_size, lut_size - 1)];
  98. size_t x1 = 1 << (log_size + 1);
  99. size_t y1 = lut[log_size - 1];
  100. return (bytes - x1) * (y2 - y1) / (x2 - x1) + y1;
  101. }
  102. size_t SpiFlashEmulator::getReadOpTime(uint32_t bytes)
  103. {
  104. return timeInterp(bytes, readTimes);
  105. }
  106. size_t SpiFlashEmulator::getWriteOpTime(uint32_t bytes)
  107. {
  108. return timeInterp(bytes, writeTimes);
  109. }
  110. size_t SpiFlashEmulator::getEraseOpTime()
  111. {
  112. return blockEraseTime;
  113. }