Rp2040/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 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #include <esp_system.h>
19 #include <sys/pgmspace.h>
20 
21 #define SPI_FLASH_SEC_SIZE 4096
22 
30 #define FLASH_TOTAL_SEC_COUNT PICO_FLASH_SIZE_BYTES
31 
32 /*
33  * TODO: Revise layout to use custom 2nd-stage bootloader with partition
34  * table support.
35  */
37 #define SYS_PARAM_SEC_COUNT 4
38 #define FLASH_WORK_SEC_COUNT (FLASH_TOTAL_SEC_COUNT - SYS_PARAM_SEC_COUNT)
39 
40 #define INTERNAL_FLASH_SECTOR_SIZE SPI_FLASH_SEC_SIZE
41 #define INTERNAL_FLASH_SIZE ((FLASH_WORK_SEC_COUNT)*INTERNAL_FLASH_SECTOR_SIZE)
42 #define INTERNAL_FLASH_START_ADDRESS FLASH_BASE
43 
44 typedef enum {
45  MODE_QIO = 0,
46  MODE_QOUT = 1,
47  MODE_DIO = 2,
48  MODE_DOUT = 15,
49  MODE_SLOW_READ = 0xFE,
50  MODE_FAST_READ = 0xFF,
51 } SPIFlashMode;
52 
53 typedef enum {
59 
60 typedef enum {
66  SIZE_1MBIT = 0xFF,
67 } SPIFlashSize;
68 
72 typedef struct {
73  uint8_t mode : 8;
74  uint8_t speed : 4;
75  uint8_t size : 4;
76 } SPIFlashInfo;
77 
85 static inline uint32_t flashmem_get_address(const void* memptr)
86 {
87  auto addr = uint32_t(memptr);
88  if(addr < XIP_BASE || addr >= XIP_NOALLOC_BASE) {
89  return 0;
90  }
91  return addr - XIP_BASE;
92 }
93 
101 uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size);
102 
110 uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size);
111 
116 bool flashmem_erase_sector(uint32_t sector_id);
117 
122 
127 
131 uint32_t flashmem_get_size_bytes();
132 
137 
145 uint32_t flashmem_find_sector(uint32_t address, uint32_t* pstart, uint32_t* pend);
146 
151 uint32_t flashmem_get_sector_of_address(uint32_t addr);
152 
153 /*
154  * @brief Returns the address of the first free block on flash
155  * @retval uint32_t The actual address on flash
156  */
158 
159 /*
160  * @brief Get unique 32-bit flash identification code
161  */
162 uint32_t spi_flash_get_id(void);
163 
164 /*
165  * @brief Read flash SFDP device information (standard for Serial Flash Discoverable Parameters)
166  * @param addr First memory location to read
167  * @param buffer Buffer for data
168  * @param count Number of bytes to read
169  */
170 void flashmem_sfdp_read(uint32_t addr, void* buffer, size_t count);
171 
174 #ifdef __cplusplus
175 }
176 #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: Rp2040/Components/spi_flash/include/esp_spi_flash.h:61
@ MODE_FAST_READ
Not supported.
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:50
uint32_t flashmem_get_size_bytes()
get the total flash memory size
@ SIZE_2MBIT
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:62
@ SIZE_1MBIT
Not supported.
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:66
@ MODE_DIO
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:47
@ MODE_DOUT
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:48
@ SPEED_80MHZ
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:57
SPIFlashInfo flashmem_get_info()
Get flash memory information block.
@ SPEED_26MHZ
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:55
@ SPEED_40MHZ
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:54
uint8_t size
SPIFlashSize.
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:75
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: Rp2040/Components/spi_flash/include/esp_spi_flash.h:44
@ MODE_QIO
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:45
bool flashmem_erase_sector(uint32_t sector_id)
Erase a single flash sector.
uint8_t speed
SPIFlashSpeed.
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:74
@ SIZE_32MBIT
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:65
@ MODE_SLOW_READ
Not supported.
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:49
uint8_t flashmem_get_size_type()
Returns a number indicating the size of flash memory chip.
@ SIZE_16MBIT
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:64
uint32_t flashmem_get_sector_of_address(uint32_t addr)
Get sector number containing the given address.
uint32_t flashmem_get_first_free_block_address()
@ SIZE_8MBIT
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:63
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: Rp2040/Components/spi_flash/include/esp_spi_flash.h:60
@ SPEED_20MHZ
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:56
@ MODE_QOUT
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:46
void flashmem_sfdp_read(uint32_t addr, void *buffer, size_t count)
uint8_t mode
SPIFlashMode.
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:73
uint32_t flashmem_read(void *to, uint32_t fromaddr, uint32_t size)
Read a block of data from flash.
SPIFlashSpeed
Definition: Rp2040/Components/spi_flash/include/esp_spi_flash.h:53