WL_Ext_Perf.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright 2015-2017 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 "WL_Ext_Perf.h"
  14. #include <stdlib.h>
  15. #include "esp_log.h"
  16. static const char *TAG = "wl_ext_perf";
  17. #define WL_EXT_RESULT_CHECK(result) \
  18. if (result != ESP_OK) { \
  19. ESP_LOGE(TAG,"%s(%d): result = 0x%08x", __FUNCTION__, __LINE__, result); \
  20. return (result); \
  21. }
  22. WL_Ext_Perf::WL_Ext_Perf(): WL_Flash()
  23. {
  24. this->sector_buffer = NULL;
  25. }
  26. WL_Ext_Perf::~WL_Ext_Perf()
  27. {
  28. free(this->sector_buffer);
  29. }
  30. esp_err_t WL_Ext_Perf::config(WL_Config_s *cfg, Flash_Access *flash_drv)
  31. {
  32. wl_ext_cfg_t *config = (wl_ext_cfg_t *)cfg;
  33. this->fat_sector_size = config->fat_sector_size;
  34. this->flash_sector_size = cfg->sector_size;
  35. this->sector_buffer = (uint32_t *)malloc(cfg->sector_size);
  36. if (this->sector_buffer == NULL) {
  37. return ESP_ERR_NO_MEM;
  38. }
  39. this->size_factor = this->flash_sector_size / this->fat_sector_size;
  40. if (this->size_factor < 1) {
  41. return ESP_ERR_INVALID_ARG;
  42. }
  43. return WL_Flash::config(cfg, flash_drv);
  44. }
  45. esp_err_t WL_Ext_Perf::init()
  46. {
  47. return WL_Flash::init();
  48. }
  49. size_t WL_Ext_Perf::chip_size()
  50. {
  51. return WL_Flash::chip_size();
  52. }
  53. size_t WL_Ext_Perf::sector_size()
  54. {
  55. return this->fat_sector_size;
  56. }
  57. esp_err_t WL_Ext_Perf::erase_sector(size_t sector)
  58. {
  59. return this->erase_sector_fit(sector, 1);
  60. }
  61. esp_err_t WL_Ext_Perf::erase_sector_fit(uint32_t start_sector, uint32_t count)
  62. {
  63. ESP_LOGV(TAG, "%s begin, start_sector = 0x%08x, count = %i", __func__, start_sector, count);
  64. // This method works with one flash device sector and able to erase "count" of fatfs sectors from this sector
  65. esp_err_t result = ESP_OK;
  66. uint32_t pre_check_start = start_sector % this->size_factor;
  67. for (int i = 0; i < this->size_factor; i++) {
  68. if ((i < pre_check_start) || (i >= count + pre_check_start)) {
  69. result = this->read(start_sector / this->size_factor * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size);
  70. WL_EXT_RESULT_CHECK(result);
  71. }
  72. }
  73. result = WL_Flash::erase_sector(start_sector / this->size_factor); // erase comlete flash sector
  74. WL_EXT_RESULT_CHECK(result);
  75. // And write back only data that should not be erased...
  76. for (int i = 0; i < this->size_factor; i++) {
  77. if ((i < pre_check_start) || (i >= count + pre_check_start)) {
  78. result = this->write(start_sector / this->size_factor * this->flash_sector_size + i * this->fat_sector_size, &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)], this->fat_sector_size);
  79. WL_EXT_RESULT_CHECK(result);
  80. }
  81. }
  82. return ESP_OK;
  83. }
  84. esp_err_t WL_Ext_Perf::erase_range(size_t start_address, size_t size)
  85. {
  86. esp_err_t result = ESP_OK;
  87. if ((start_address % this->fat_sector_size) != 0) {
  88. result = ESP_ERR_INVALID_ARG;
  89. }
  90. if (((size % this->fat_sector_size) != 0) || (size == 0)) {
  91. result = ESP_ERR_INVALID_ARG;
  92. }
  93. WL_EXT_RESULT_CHECK(result);
  94. // The range to erase could be allocated in any possible way
  95. // ---------------------------------------------------------
  96. // | | | | |
  97. // |0|0|x|x|x|x|x|x|x|x|x|x|x|x|0|0|
  98. // | pre | rest | rest | post | <- check ranges
  99. //
  100. // Pre check - the data that is not fit to the full sector at the begining of the erased block
  101. // Post check - the data that are not fit to the full sector at the end of the erased block
  102. // rest - data that are fit to the flash device sector at the middle of the erased block
  103. //
  104. // In case of pre and post check situations the data of the non erased area have to be readed first and then
  105. // stored back.
  106. // For the rest area this operation not needed because complete flash device sector will be erased.
  107. ESP_LOGV(TAG, "%s begin, addr = 0x%08x, size = %i", __func__, start_address, size);
  108. // Calculate pre check values
  109. uint32_t pre_check_start = (start_address / this->fat_sector_size) % this->size_factor;
  110. uint32_t sectors_count = size / this->fat_sector_size;
  111. uint32_t pre_check_count = (this->size_factor - pre_check_start);
  112. if (pre_check_count > sectors_count) {
  113. pre_check_count = sectors_count;
  114. }
  115. // Calculate post ckeck
  116. uint32_t post_check_count = (sectors_count - pre_check_count) % this->size_factor;
  117. uint32_t post_check_start = ((start_address + size - post_check_count * this->fat_sector_size) / this->fat_sector_size);
  118. // Calculate rest
  119. uint32_t rest_check_count = sectors_count - pre_check_count - post_check_count;
  120. if ((pre_check_count == this->size_factor) && (0 == pre_check_start)) {
  121. rest_check_count+=this->size_factor;
  122. pre_check_count = 0;
  123. }
  124. uint32_t rest_check_start = start_address + pre_check_count * this->fat_sector_size;
  125. // Here we will clear pre_check_count amount of sectors
  126. if (pre_check_count != 0) {
  127. result = this->erase_sector_fit(start_address / this->fat_sector_size, pre_check_count);
  128. WL_EXT_RESULT_CHECK(result);
  129. }
  130. ESP_LOGV(TAG, "%s rest_check_start = %i, pre_check_count=%i, rest_check_count=%i, post_check_count=%i\n", __func__, rest_check_start, pre_check_count, rest_check_count, post_check_count);
  131. if (rest_check_count > 0) {
  132. rest_check_count = rest_check_count / this->size_factor;
  133. size_t start_sector = rest_check_start / this->flash_sector_size;
  134. for (size_t i = 0; i < rest_check_count; i++) {
  135. result = WL_Flash::erase_sector(start_sector + i);
  136. WL_EXT_RESULT_CHECK(result);
  137. }
  138. }
  139. if (post_check_count != 0) {
  140. result = this->erase_sector_fit(post_check_start, post_check_count);
  141. WL_EXT_RESULT_CHECK(result);
  142. }
  143. return ESP_OK;
  144. }