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 #ifndef _WL_Flash_H_
15 #define _WL_Flash_H_
16 
17 #include "esp_err.h"
18 #include "Flash_Access.h"
19 #include "WL_Config.h"
20 #include "WL_State.h"
21 
22 /**
23 * @brief This class is used to make wear levelling for flash devices. Class implements Flash_Access interface
24 *
25 */
26 class WL_Flash : public Flash_Access
27 {
28 public :
29     WL_Flash();
30     ~WL_Flash() override;
31 
32     virtual esp_err_t config(wl_config_t *cfg, Flash_Access *flash_drv);
33     virtual esp_err_t init();
34 
35     size_t chip_size() override;
36     size_t sector_size() override;
37 
38 
39     esp_err_t erase_sector(size_t sector) override;
40     esp_err_t erase_range(size_t start_address, size_t size) override;
41 
42     esp_err_t write(size_t dest_addr, const void *src, size_t size) override;
43     esp_err_t read(size_t src_addr, void *dest, size_t size) override;
44 
45     esp_err_t flush() override;
46 
47     Flash_Access *get_drv();
48     wl_config_t *get_cfg();
49 
50 protected:
51     bool configured = false;
52     bool initialized = false;
53     wl_state_t state;
54     wl_config_t cfg;
55     Flash_Access *flash_drv = NULL;
56 
57     size_t addr_cfg;
58     size_t addr_state1;
59     size_t addr_state2;
60     size_t index_state1;
61     size_t index_state2;
62 
63     size_t flash_size;
64     uint32_t state_size;
65     uint32_t cfg_size;
66     uint8_t *temp_buff = NULL;
67     size_t dummy_addr;
68     uint32_t pos_data[4];
69 
70     esp_err_t initSections();
71     esp_err_t updateWL();
72     esp_err_t recoverPos();
73     size_t calcAddr(size_t addr);
74 
75     esp_err_t updateVersion();
76     esp_err_t updateV1_V2();
77     void fillOkBuff(int n);
78     bool OkBuffSet(int n);
79 };
80 
81 #endif // _WL_Flash_H_
82