1 #include <string.h>
2 #include <stdlib.h>
3
4 #include "SpiFlash.h"
5 #include "spi_flash_mmap.h"
6 #include "esp_err.h"
7 #include "esp_rom_spiflash.h"
8 #include "esp_flash.h"
9 #include "bsd_strings.h"
10
11 SpiFlash spiflash = SpiFlash();
12
13 esp_rom_spiflash_chip_t g_rom_flashchip;
14
convert_chip_size_string(const char * chip_size_str)15 size_t convert_chip_size_string(const char* chip_size_str)
16 {
17 int size = 0;
18 if (strcmp(chip_size_str, "1MB") == 0) {
19 size = 0x100000;
20 }
21 else if (strcmp(chip_size_str, "2MB") == 0) {
22 size = 0x200000;
23 }
24 else if (strcmp(chip_size_str, "4MB") == 0) {
25 size = 0x400000;
26 }
27 else if (strcmp(chip_size_str, "8MB") == 0) {
28 size = 0x800000;
29 }
30 else if (strcmp(chip_size_str, "16MB") == 0) {
31 size = 0x1000000;
32 } else {
33 size = 0;
34 }
35
36 return size;
37 }
38
_spi_flash_init(const char * chip_size,size_t block_size,size_t sector_size,size_t page_size,const char * partitions_bin)39 extern "C" void _spi_flash_init(const char* chip_size, size_t block_size, size_t sector_size, size_t page_size, const char* partitions_bin)
40 {
41 size_t size = convert_chip_size_string(chip_size);
42
43 assert(size != 0);
44
45 spiflash.init(size, block_size, sector_size, page_size, partitions_bin);
46
47 g_rom_flashchip.chip_size = size;
48 g_rom_flashchip.block_size = block_size;
49 g_rom_flashchip.sector_size = sector_size;
50 g_rom_flashchip.page_size = page_size;
51 }
52
spi_flash_mmap(size_t src_addr,size_t size,spi_flash_mmap_memory_t memory,const void ** out_ptr,spi_flash_mmap_handle_t * out_handle)53 extern "C" esp_err_t spi_flash_mmap(size_t src_addr, size_t size, spi_flash_mmap_memory_t memory,
54 const void** out_ptr, spi_flash_mmap_handle_t* out_handle)
55 {
56 *out_handle = 0;
57 *out_ptr = (void*)spiflash.get_memory_ptr(src_addr);
58
59 return ESP_OK;
60 }
61
spi_flash_munmap(spi_flash_mmap_handle_t handle)62 extern "C" void spi_flash_munmap(spi_flash_mmap_handle_t handle)
63 {
64 return;
65 }
66
spi_flash_get_total_erase_cycles(void)67 extern "C" int spi_flash_get_total_erase_cycles(void)
68 {
69 return spiflash.get_total_erase_cycles();
70 }
71
spi_flash_get_erase_cycles(size_t sector)72 extern "C" int spi_flash_get_erase_cycles(size_t sector)
73 {
74 return spiflash.get_erase_cycles(sector);
75 }
76
bootloader_flash_unlock(void)77 extern "C" esp_err_t bootloader_flash_unlock(void)
78 {
79 return ESP_OK;
80 }
81
esp_rom_spiflash_read(uint32_t target,uint32_t * dest,int32_t len)82 esp_rom_spiflash_result_t esp_rom_spiflash_read(uint32_t target, uint32_t *dest, int32_t len)
83 {
84 return spiflash.read(target, dest, len);
85 }
86
esp_flash_read(esp_flash_t * chip,void * buffer,uint32_t address,uint32_t length)87 extern "C" esp_err_t esp_flash_read(esp_flash_t *chip, void *buffer, uint32_t address, uint32_t length)
88 {
89 return spiflash.read(address, buffer, length);
90 }
91
esp_rom_spiflash_erase_block(uint32_t block)92 esp_rom_spiflash_result_t esp_rom_spiflash_erase_block(uint32_t block)
93 {
94 return spiflash.erase_block(block);
95 }
96
esp_rom_spiflash_erase_sector(uint32_t sector)97 esp_rom_spiflash_result_t esp_rom_spiflash_erase_sector(uint32_t sector)
98 {
99 return spiflash.erase_sector(sector);
100 }
101
esp_rom_spiflash_erase_page(uint32_t page)102 esp_rom_spiflash_result_t esp_rom_spiflash_erase_page(uint32_t page)
103 {
104 return spiflash.erase_page(page);
105 }
106
esp_flash_erase_region(esp_flash_t * chip,uint32_t start_addr,uint32_t size)107 extern "C" esp_err_t esp_flash_erase_region(esp_flash_t *chip, uint32_t start_addr, uint32_t size)
108 {
109 size_t start = start_addr / SPI_FLASH_SEC_SIZE;
110 size_t end = start + size / SPI_FLASH_SEC_SIZE;
111 const size_t sectors_per_block = 65536 / SPI_FLASH_SEC_SIZE;
112 esp_rom_spiflash_result_t rc = ESP_ROM_SPIFLASH_RESULT_OK;
113 for (size_t sector = start; sector != end && rc == ESP_ROM_SPIFLASH_RESULT_OK; ) {
114 rc = spiflash.erase_sector(sector);
115 ++sector;
116 }
117 return rc;
118 }
119
esp_rom_spiflash_write(uint32_t target,const uint32_t * src,int32_t len)120 esp_rom_spiflash_result_t esp_rom_spiflash_write(uint32_t target, const uint32_t *src, int32_t len)
121 {
122 return spiflash.write(target, src, len);
123 }
124
esp_flash_write(esp_flash_t * chip,const void * buffer,uint32_t address,uint32_t length)125 extern "C" esp_err_t esp_flash_write(esp_flash_t *chip, const void *buffer, uint32_t address, uint32_t length)
126 {
127 return spiflash.write(address, buffer, length);
128 }
129
esp_rom_spiflash_write_encrypted(uint32_t flash_addr,uint32_t * data,uint32_t len)130 esp_rom_spiflash_result_t esp_rom_spiflash_write_encrypted(uint32_t flash_addr, uint32_t *data, uint32_t len)
131 {
132 return spiflash.write(flash_addr, data, len);
133 }
134
heap_caps_malloc(size_t size,uint32_t caps)135 void *heap_caps_malloc( size_t size, uint32_t caps )
136 {
137 return NULL;
138 }
139
esp_rom_spiflash_write_disable(void)140 esp_rom_spiflash_result_t esp_rom_spiflash_write_disable(void)
141 {
142 return ESP_ROM_SPIFLASH_RESULT_OK;
143 }
144
145 esp_flash_t* esp_flash_default_chip = NULL;
146