SpiFlash.h 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. #ifndef _SpiFlash_H_
  14. #define _SpiFlash_H_
  15. #include <stdbool.h>
  16. #include "esp_err.h"
  17. #include "esp32/rom/spi_flash.h"
  18. /**
  19. * @brief This class is used to emulate flash devices.
  20. *
  21. */
  22. class SpiFlash
  23. {
  24. public:
  25. SpiFlash();
  26. ~SpiFlash();
  27. void init(uint32_t chip_size, uint32_t block_size, uint32_t sector_size, uint32_t page_size, const char* partitions_bin);
  28. uint32_t get_chip_size();
  29. uint32_t get_block_size();
  30. uint32_t get_sector_size();
  31. uint32_t get_page_size();
  32. esp_rom_spiflash_result_t erase_block(uint32_t block);
  33. esp_rom_spiflash_result_t erase_sector(uint32_t sector);
  34. esp_rom_spiflash_result_t erase_page(uint32_t page);
  35. esp_rom_spiflash_result_t write(uint32_t dest_addr, const void *src, uint32_t size);
  36. esp_rom_spiflash_result_t read(uint32_t src_addr, void *dest, uint32_t size);
  37. uint32_t get_erase_cycles(uint32_t sector);
  38. uint32_t get_total_erase_cycles();
  39. void set_erase_cycles_limit(uint32_t limit);
  40. void set_total_erase_cycles_limit(uint32_t limit);
  41. void reset_erase_cycles();
  42. void reset_total_erase_cycles();
  43. uint8_t* get_memory_ptr(uint32_t src_address);
  44. private:
  45. uint32_t chip_size;
  46. uint32_t block_size;
  47. uint32_t sector_size;
  48. uint32_t page_size;
  49. uint32_t blocks;
  50. uint32_t sectors;
  51. uint32_t pages;
  52. uint8_t* memory;
  53. bool* erase_states;
  54. uint32_t* erase_cycles;
  55. uint32_t erase_cycles_limit;
  56. uint32_t total_erase_cycles;
  57. uint32_t total_erase_cycles_limit;
  58. void deinit();
  59. };
  60. #endif // _SpiFlash_H_