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