WL_Ext_Perf.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "WL_Ext_Perf.h"
  7. #include "Partition.h"
  8. #include <stdlib.h>
  9. #include <inttypes.h>
  10. #include "esp_log.h"
  11. static const char *TAG = "wl_ext_perf";
  12. #define WL_EXT_RESULT_CHECK(result) \
  13. if (result != ESP_OK) { \
  14. ESP_LOGE(TAG,"%s(%d): result = 0x%08" PRIx32, __FUNCTION__, __LINE__, (uint32_t) result); \
  15. return (result); \
  16. }
  17. WL_Ext_Perf::WL_Ext_Perf(): WL_Flash()
  18. {
  19. this->sector_buffer = NULL;
  20. }
  21. WL_Ext_Perf::~WL_Ext_Perf()
  22. {
  23. free(this->sector_buffer);
  24. }
  25. esp_err_t WL_Ext_Perf::config(WL_Config_s *cfg, Partition *partition)
  26. {
  27. wl_ext_cfg_t *ext_cfg = (wl_ext_cfg_t *)cfg;
  28. this->flash_sector_size = ext_cfg->flash_sector_size;
  29. this->fat_sector_size = ext_cfg->fat_sector_size;
  30. /*when flash and fat sector sizes are not equal, (where flash_sector_size >= fat_sector_size)
  31. this flash_fat_sector_size_factor will be used while flash sector erase or read-write operation */
  32. this->flash_fat_sector_size_factor = this->flash_sector_size / this->fat_sector_size;
  33. if (this->flash_fat_sector_size_factor < 1) {
  34. return ESP_ERR_INVALID_ARG;
  35. }
  36. this->sector_buffer = (uint32_t *)malloc(ext_cfg->flash_sector_size);
  37. if (this->sector_buffer == NULL) {
  38. return ESP_ERR_NO_MEM;
  39. }
  40. return WL_Flash::config(cfg, partition);
  41. }
  42. esp_err_t WL_Ext_Perf::init()
  43. {
  44. return WL_Flash::init();
  45. }
  46. size_t WL_Ext_Perf::get_flash_size()
  47. {
  48. return WL_Flash::get_flash_size();
  49. }
  50. size_t WL_Ext_Perf::get_sector_size()
  51. {
  52. return this->fat_sector_size;
  53. }
  54. esp_err_t WL_Ext_Perf::erase_sector(size_t sector)
  55. {
  56. return WL_Flash::erase_sector(sector);
  57. }
  58. /*
  59. erase_sector_fit function is needed in case flash_sector_size != fat_sector_size and
  60. sector to be erased is not multiple of flash_fat_sector_size_factor
  61. */
  62. esp_err_t WL_Ext_Perf::erase_sector_fit(uint32_t first_erase_sector, uint32_t count)
  63. {
  64. // This method works with one flash device sector and able to erase "count" of fatfs sectors from this first_erase_sector
  65. esp_err_t result = ESP_OK;
  66. ESP_LOGV(TAG, "%s begin, first_erase_sector = 0x%08" PRIx32 ", count = %" PRIu32, __func__, first_erase_sector, count);
  67. uint32_t flash_sector_base_addr = first_erase_sector / this->flash_fat_sector_size_factor;
  68. uint32_t pre_check_start = first_erase_sector % this->flash_fat_sector_size_factor;
  69. // Except pre check and post check data area, read and store all other data to sector_buffer
  70. for (int i = 0; i < this->flash_fat_sector_size_factor; i++) {
  71. if ((i < pre_check_start) || (i >= count + pre_check_start)) {
  72. result = this->read(flash_sector_base_addr * this->flash_sector_size + i * this->fat_sector_size,
  73. &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)],
  74. this->fat_sector_size);
  75. WL_EXT_RESULT_CHECK(result);
  76. }
  77. }
  78. //erase complete flash sector which includes pre and post check data area
  79. result = WL_Flash::erase_sector(flash_sector_base_addr);
  80. WL_EXT_RESULT_CHECK(result);
  81. /* Restore data which was previously stored to sector_buffer
  82. back to data area which was not part of pre and post check data */
  83. for (int i = 0; i < this->flash_fat_sector_size_factor; i++) {
  84. if ((i < pre_check_start) || (i >= count + pre_check_start)) {
  85. result = this->write(flash_sector_base_addr * this->flash_sector_size + i * this->fat_sector_size,
  86. &this->sector_buffer[i * this->fat_sector_size / sizeof(uint32_t)],
  87. this->fat_sector_size);
  88. WL_EXT_RESULT_CHECK(result);
  89. }
  90. }
  91. return ESP_OK;
  92. }
  93. esp_err_t WL_Ext_Perf::erase_range(size_t start_address, size_t size)
  94. {
  95. esp_err_t result = ESP_OK;
  96. //start_address as well as size should be aligned to fat_sector_size
  97. if ((start_address % this->fat_sector_size) != 0) {
  98. result = ESP_ERR_INVALID_ARG;
  99. }
  100. if (((size % this->fat_sector_size) != 0) || (size == 0)) {
  101. result = ESP_ERR_INVALID_SIZE;
  102. }
  103. WL_EXT_RESULT_CHECK(result);
  104. // The range to erase could be allocated in any possible way
  105. // ---------------------------------------------------------
  106. // | | | | |
  107. // |0|0|x|x|x|x|x|x|x|x|x|x|x|x|0|0|
  108. // | pre | rest | rest | post | <- check ranges
  109. //
  110. // Pre check - the data that does not fit in the full sector at the begining of the erased block
  111. // Post check - the data that does not fit in the full sector at the end of the erased block
  112. // rest check - the data that fits in the full sector at the middle of the erased block
  113. //
  114. // In case of pre and post check situations, the data of the area which should not be erased
  115. // as part of pre and post check data area have to read first, store at other location,
  116. // erase complete sector and restore data of area which should not be erased.
  117. // For the rest check area, this operation not needed because complete flash device sector will be erased.
  118. ESP_LOGV(TAG, "%s begin, addr = 0x%08" PRIx32 ", size = %" PRIu32, __func__, (uint32_t) start_address, (uint32_t) size);
  119. uint32_t sectors_count = size / this->fat_sector_size;
  120. // Calculate pre check values
  121. uint32_t pre_check_start = (start_address / this->fat_sector_size) % this->flash_fat_sector_size_factor;
  122. uint32_t pre_check_count = (this->flash_fat_sector_size_factor - pre_check_start);
  123. //maximum sectors count that need to be erased is sectors_count
  124. if (pre_check_count > sectors_count) {
  125. pre_check_count = sectors_count;
  126. }
  127. // Calculate post check values
  128. uint32_t post_check_count = (sectors_count - pre_check_count) % this->flash_fat_sector_size_factor;
  129. uint32_t post_check_start = ((start_address + size - post_check_count * this->fat_sector_size) / this->fat_sector_size);
  130. // Calculate rest check values
  131. uint32_t rest_check_count = sectors_count - pre_check_count - post_check_count;
  132. if ((pre_check_count == this->flash_fat_sector_size_factor) && (0 == pre_check_start)) {
  133. rest_check_count+=this->flash_fat_sector_size_factor;
  134. pre_check_count = 0;
  135. }
  136. uint32_t rest_check_start = start_address + pre_check_count * this->fat_sector_size;
  137. // Clear pre_check_count sectors
  138. if (pre_check_count != 0) {
  139. result = this->erase_sector_fit(start_address / this->fat_sector_size, pre_check_count);
  140. WL_EXT_RESULT_CHECK(result);
  141. }
  142. ESP_LOGV(TAG, "%s rest_check_start = %" PRIu32 ", pre_check_count=%" PRIu32 ", rest_check_count=%" PRIu32 ", post_check_count=%" PRIu32, __func__, rest_check_start, pre_check_count, rest_check_count, post_check_count);
  143. // Clear rest_check_count sectors
  144. if (rest_check_count > 0) {
  145. rest_check_count = rest_check_count / this->flash_fat_sector_size_factor;
  146. size_t start_sector = rest_check_start / this->flash_sector_size;
  147. for (size_t i = 0; i < rest_check_count; i++) {
  148. result = WL_Flash::erase_sector(start_sector + i);
  149. WL_EXT_RESULT_CHECK(result);
  150. }
  151. }
  152. // Clear post_check_count sectors
  153. if (post_check_count != 0) {
  154. result = this->erase_sector_fit(post_check_start, post_check_count);
  155. WL_EXT_RESULT_CHECK(result);
  156. }
  157. return ESP_OK;
  158. }