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_next <esp_spi_flash.h>
15 #include <rom/spi_flash.h>
16 #include <esp_app_format.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
29 #define SPI_FLASH_RESULT_OK 0
30 
32 #define INTERNAL_FLASH_WRITE_UNIT_SIZE 4
33 #define INTERNAL_FLASH_READ_UNIT_SIZE 4
34 
35 #define FLASH_TOTAL_SEC_COUNT (flashmem_get_size_sectors())
36 
38 #define SYS_PARAM_SEC_COUNT 4
39 #define FLASH_WORK_SEC_COUNT (FLASH_TOTAL_SEC_COUNT - SYS_PARAM_SEC_COUNT)
40 
41 #define INTERNAL_FLASH_SECTOR_SIZE SPI_FLASH_SEC_SIZE
42 #define INTERNAL_FLASH_SIZE ((FLASH_WORK_SEC_COUNT)*INTERNAL_FLASH_SECTOR_SIZE)
43 
44 typedef enum {
45  MODE_QIO = ESP_IMAGE_SPI_MODE_QIO,
46  MODE_QOUT = ESP_IMAGE_SPI_MODE_QOUT,
47  MODE_DIO = ESP_IMAGE_SPI_MODE_DIO,
48  MODE_DOUT = ESP_IMAGE_SPI_MODE_DOUT,
49  MODE_FAST_READ = ESP_IMAGE_SPI_MODE_FAST_READ,
50  MODE_SLOW_READ = ESP_IMAGE_SPI_MODE_SLOW_READ,
51 } SPIFlashMode;
52 
53 typedef enum {
54  SPEED_40MHZ = ESP_IMAGE_SPI_SPEED_40M,
55  SPEED_26MHZ = ESP_IMAGE_SPI_SPEED_26M,
56  SPEED_20MHZ = ESP_IMAGE_SPI_SPEED_20M,
57  SPEED_80MHZ = ESP_IMAGE_SPI_SPEED_80M,
59 
60 typedef enum {
61  SIZE_1MBIT = ESP_IMAGE_FLASH_SIZE_1MB,
62  SIZE_2MBIT = ESP_IMAGE_FLASH_SIZE_2MB,
63  SIZE_4MBIT = ESP_IMAGE_FLASH_SIZE_4MB,
64  SIZE_8MBIT = ESP_IMAGE_FLASH_SIZE_8MB,
65  SIZE_16MBIT = ESP_IMAGE_FLASH_SIZE_16MB,
66  SIZE_32MBIT = 0xFF,
67 } SPIFlashSize;
68 
74 typedef struct {
75  SPIFlashMode mode;
76  SPIFlashSpeed speed;
78 } SPIFlashInfo;
79 
87 static inline uint32_t flashmem_get_address(const void* memptr)
88 {
89  auto phys = spi_flash_cache2phys(memptr);
90  return (phys == SPI_FLASH_CACHE2PHYS_FAIL) ? 0 : phys;
91 }
92 
100 uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size);
101 
109 uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size);
110 
115 bool flashmem_erase_sector(uint32_t sector_id);
116 
121 
126 
130 uint32_t flashmem_get_size_bytes();
131 
136 
144 uint32_t flashmem_find_sector(uint32_t address, uint32_t* pstart, uint32_t* pend);
145 
150 uint32_t flashmem_get_sector_of_address(uint32_t addr);
151 
152 /*
153  * @brief Get unique 32-bit flash identification code
154  */
155 uint32_t spi_flash_get_id(void);
156 
159 #ifdef __cplusplus
160 }
161 #endif
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
SPIFlashInfo flashmem_get_info()
Get flash memory information block.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:63
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:49
uint32_t flashmem_get_size_bytes()
get the total flash memory size
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:62
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:47
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:61
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:48
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:54
uint8_t flashmem_get_size_type()
Returns a number indicating the size of flash memory chip.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:57
uint16_t flashmem_get_size_sectors()
Get the total number of flash sectors.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:55
SPIFlashSize
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:60
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:45
bool flashmem_erase_sector(uint32_t sector_id)
Erase a single flash sector.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:50
Not listed.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:66
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:65
uint32_t spi_flash_get_id(void)
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.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:64
SPIFlashMode
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:44
SPIFlashSize size
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:77
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:56
SPIFlashSpeed
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:53
SPIFlashMode mode
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:75
uint32_t flashmem_write(const void *from, uint32_t toaddr, uint32_t size)
Write a block of data to flash.
SPI Flash memory information block. Stored at the beginning of flash memory.
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:72
uint32_t flashmem_get_sector_of_address(uint32_t addr)
Get sector number containing the given address.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:46
SPIFlashSpeed speed
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:76
uint32_t flashmem_read(void *to, uint32_t fromaddr, uint32_t size)
Read a block of data from flash.