Esp32/Components/spi_flash/include/esp_spi_flash.h
Go to the documentation of this file.
1 /****
2  * Sming Framework Project - Open Source framework for high efficiency native ESP8266 development.
3  * Created 2015 by Skurydin Alexey
4  * http://github.com/SmingHub/Sming
5  * All files of the Sming Core are provided under the LGPL v3 license.
6  *
7  * Based on NodeMCU platform_flash
8  * https://github.com/nodemcu/nodemcu-firmware
9  *
10  ****/
11 
12 #pragma once
13 
14 #include <esp_idf_version.h>
15 #if ESP_IDF_VERSION_MAJOR < 5
16 #include_next <esp_spi_flash.h>
17 #else
18 #include <spi_flash_mmap.h>
19 #endif
20 #include <rom/spi_flash.h>
21 #include <esp_app_format.h>
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
34 #define INTERNAL_FLASH_WRITE_UNIT_SIZE 4
36 #define INTERNAL_FLASH_READ_UNIT_SIZE 4
37 
38 #define FLASH_TOTAL_SEC_COUNT (flashmem_get_size_sectors())
39 
41 #define SYS_PARAM_SEC_COUNT 4
42 #define FLASH_WORK_SEC_COUNT (FLASH_TOTAL_SEC_COUNT - SYS_PARAM_SEC_COUNT)
43 
44 #define INTERNAL_FLASH_SECTOR_SIZE SPI_FLASH_SEC_SIZE
45 #define INTERNAL_FLASH_SIZE ((FLASH_WORK_SEC_COUNT)*INTERNAL_FLASH_SECTOR_SIZE)
46 
47 typedef enum {
48  MODE_QIO = ESP_IMAGE_SPI_MODE_QIO,
49  MODE_QOUT = ESP_IMAGE_SPI_MODE_QOUT,
50  MODE_DIO = ESP_IMAGE_SPI_MODE_DIO,
51  MODE_DOUT = ESP_IMAGE_SPI_MODE_DOUT,
52  MODE_FAST_READ = ESP_IMAGE_SPI_MODE_FAST_READ,
53  MODE_SLOW_READ = ESP_IMAGE_SPI_MODE_SLOW_READ,
54 } SPIFlashMode;
55 
56 typedef enum {
57  SPEED_40MHZ = 0, // ESP_IMAGE_SPI_SPEED_40M
58  SPEED_26MHZ = 1, // ESP_IMAGE_SPI_SPEED_26M
59  SPEED_20MHZ = 2, // ESP_IMAGE_SPI_SPEED_20M
60  SPEED_80MHZ = 0x0f, // ESP_IMAGE_SPI_SPEED_80M
62 
63 typedef enum {
64  SIZE_1MBIT = ESP_IMAGE_FLASH_SIZE_1MB,
65  SIZE_2MBIT = ESP_IMAGE_FLASH_SIZE_2MB,
66  SIZE_4MBIT = ESP_IMAGE_FLASH_SIZE_4MB,
67  SIZE_8MBIT = ESP_IMAGE_FLASH_SIZE_8MB,
68  SIZE_16MBIT = ESP_IMAGE_FLASH_SIZE_16MB,
69  SIZE_32MBIT = 0xFF,
70 } SPIFlashSize;
71 
77 typedef struct {
81 } SPIFlashInfo;
82 
90 static inline uint32_t flashmem_get_address(const void* memptr)
91 {
92  auto phys = spi_flash_cache2phys(memptr);
93  return (phys == SPI_FLASH_CACHE2PHYS_FAIL) ? 0 : phys;
94 }
95 
103 uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size);
104 
112 uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size);
113 
118 bool flashmem_erase_sector(uint32_t sector_id);
119 
124 
129 
133 uint32_t flashmem_get_size_bytes();
134 
139 
147 uint32_t flashmem_find_sector(uint32_t address, uint32_t* pstart, uint32_t* pend);
148 
153 uint32_t flashmem_get_sector_of_address(uint32_t addr);
154 
155 /*
156  * @brief Get unique 32-bit flash identification code
157  */
158 uint32_t spi_flash_get_id(void);
159 
162 #ifdef __cplusplus
163 }
164 #endif
uint32_t spi_flash_get_id(void)
uint32_t flashmem_get_address(const void *memptr)
Obtain the flash memory address for a memory pointer.
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:85
@ SIZE_4MBIT
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:66
SPIFlashSize size
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:80
@ MODE_FAST_READ
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:52
uint32_t flashmem_get_size_bytes()
get the total flash memory size
SPIFlashSpeed speed
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:79
@ SIZE_2MBIT
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:65
@ SIZE_1MBIT
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:64
@ MODE_DIO
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:50
@ MODE_DOUT
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:51
@ SPEED_80MHZ
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:60
SPIFlashInfo flashmem_get_info()
Get flash memory information block.
@ SPEED_26MHZ
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:58
@ SPEED_40MHZ
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:57
SPI Flash memory information block. Stored at the beginning of flash memory.
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:72
uint16_t flashmem_get_size_sectors()
Get the total number of flash sectors.
SPIFlashMode
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:47
@ MODE_QIO
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:48
SPIFlashMode mode
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:78
bool flashmem_erase_sector(uint32_t sector_id)
Erase a single flash sector.
@ SIZE_32MBIT
Not listed.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:69
@ MODE_SLOW_READ
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:53
uint8_t flashmem_get_size_type()
Returns a number indicating the size of flash memory chip.
@ SIZE_16MBIT
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:68
uint32_t flashmem_get_sector_of_address(uint32_t addr)
Get sector number containing the given address.
@ SIZE_8MBIT
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:67
uint32_t flashmem_find_sector(uint32_t address, uint32_t *pstart, uint32_t *pend)
Helper function: find the flash sector in which an address resides.
uint32_t flashmem_write(const void *from, uint32_t toaddr, uint32_t size)
Write a block of data to flash.
SPIFlashSize
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:63
@ SPEED_20MHZ
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:59
@ MODE_QOUT
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:49
uint32_t flashmem_read(void *to, uint32_t fromaddr, uint32_t size)
Read a block of data from flash.
SPIFlashSpeed
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:56