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 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef _SpiFlash_H_ 16 #define _SpiFlash_H_ 17 18 #include <stdbool.h> 19 20 #include "esp_err.h" 21 #include "esp32/rom/spi_flash.h" 22 23 /** 24 * @brief This class is used to emulate flash devices. 25 * 26 */ 27 class SpiFlash 28 { 29 30 public: 31 SpiFlash(); 32 ~SpiFlash(); 33 34 void init(uint32_t chip_size, uint32_t block_size, uint32_t sector_size, uint32_t page_size, const char* partitions_bin); 35 36 uint32_t get_chip_size(); 37 uint32_t get_block_size(); 38 uint32_t get_sector_size(); 39 uint32_t get_page_size(); 40 41 esp_rom_spiflash_result_t erase_block(uint32_t block); 42 esp_rom_spiflash_result_t erase_sector(uint32_t sector); 43 esp_rom_spiflash_result_t erase_page(uint32_t page); 44 45 esp_rom_spiflash_result_t write(uint32_t dest_addr, const void *src, uint32_t size); 46 esp_rom_spiflash_result_t read(uint32_t src_addr, void *dest, uint32_t size); 47 48 uint32_t get_erase_cycles(uint32_t sector); 49 uint32_t get_total_erase_cycles(); 50 51 void set_erase_cycles_limit(uint32_t limit); 52 void set_total_erase_cycles_limit(uint32_t limit); 53 54 void reset_erase_cycles(); 55 void reset_total_erase_cycles(); 56 57 uint8_t* get_memory_ptr(uint32_t src_address); 58 59 private: 60 uint32_t chip_size; 61 uint32_t block_size; 62 uint32_t sector_size; 63 uint32_t page_size; 64 65 uint32_t blocks; 66 uint32_t sectors; 67 uint32_t pages; 68 69 uint8_t* memory; 70 71 bool* erase_states; 72 73 uint32_t* erase_cycles; 74 uint32_t erase_cycles_limit; 75 uint32_t total_erase_cycles; 76 uint32_t total_erase_cycles_limit; 77 78 void deinit(); 79 }; 80 81 #endif // _SpiFlash_H_ 82