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 <esp32/rom/spi_flash.h>
16 #include <esp_app_format.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #include <user_config.h>
23 
31 #define SPI_FLASH_RESULT_OK 0
32 
34 #define INTERNAL_FLASH_WRITE_UNIT_SIZE 4
35 #define INTERNAL_FLASH_READ_UNIT_SIZE 4
36 
37 #define FLASH_TOTAL_SEC_COUNT (flashmem_get_size_sectors())
38 
40 #define SYS_PARAM_SEC_COUNT 4
41 #define FLASH_WORK_SEC_COUNT (FLASH_TOTAL_SEC_COUNT - SYS_PARAM_SEC_COUNT)
42 
43 #define INTERNAL_FLASH_SECTOR_SIZE SPI_FLASH_SEC_SIZE
44 #define INTERNAL_FLASH_SIZE ((FLASH_WORK_SEC_COUNT)*INTERNAL_FLASH_SECTOR_SIZE)
45 
46 typedef enum {
47  MODE_QIO = ESP_IMAGE_SPI_MODE_QIO,
48  MODE_QOUT = ESP_IMAGE_SPI_MODE_QOUT,
49  MODE_DIO = ESP_IMAGE_SPI_MODE_DIO,
50  MODE_DOUT = ESP_IMAGE_SPI_MODE_DOUT,
51  MODE_FAST_READ = ESP_IMAGE_SPI_MODE_FAST_READ,
52  MODE_SLOW_READ = ESP_IMAGE_SPI_MODE_SLOW_READ,
53 } SPIFlashMode;
54 
55 typedef enum {
56  SPEED_40MHZ = ESP_IMAGE_SPI_SPEED_40M,
57  SPEED_26MHZ = ESP_IMAGE_SPI_SPEED_26M,
58  SPEED_20MHZ = ESP_IMAGE_SPI_SPEED_20M,
59  SPEED_80MHZ = ESP_IMAGE_SPI_SPEED_80M,
61 
62 typedef enum {
63  SIZE_1MBIT = ESP_IMAGE_FLASH_SIZE_1MB,
64  SIZE_2MBIT = ESP_IMAGE_FLASH_SIZE_2MB,
65  SIZE_4MBIT = ESP_IMAGE_FLASH_SIZE_4MB,
66  SIZE_8MBIT = ESP_IMAGE_FLASH_SIZE_8MB,
67  SIZE_16MBIT = ESP_IMAGE_FLASH_SIZE_16MB,
68  SIZE_32MBIT = 0xFF,
69 } SPIFlashSize;
70 
76 typedef struct {
77  SPIFlashMode mode;
78  SPIFlashSpeed speed;
80 } SPIFlashInfo;
81 
89 uint32_t flashmem_get_address(const void* memptr);
90 
98 uint32_t flashmem_write(const void* from, uint32_t toaddr, uint32_t size);
99 
107 uint32_t flashmem_read(void* to, uint32_t fromaddr, uint32_t size);
108 
113 bool flashmem_erase_sector(uint32_t sector_id);
114 
119 
123 uint8_t flashmem_get_size_type();
124 
128 uint32_t flashmem_get_size_bytes();
129 
134 
142 uint32_t flashmem_find_sector(uint32_t address, uint32_t* pstart, uint32_t* pend);
143 
148 uint32_t flashmem_get_sector_of_address(uint32_t addr);
149 
152 uint32_t spi_flash_get_id(void);
153 
154 #ifdef __cplusplus
155 }
156 #endif
SPIFlashInfo flashmem_get_info()
Get flash memory information block.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:65
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:51
uint32_t flashmem_get_size_bytes()
get the total flash memory size
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:64
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:49
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:63
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:50
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:56
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:59
uint16_t flashmem_get_size_sectors()
Get the total number of flash sectors.
uint32_t flashmem_get_address(const void *memptr)
Obtain the flash memory address for a memory pointer.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:57
SPIFlashSize
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:62
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:47
uint32_t spi_flash_get_id(void)
bool flashmem_erase_sector(uint32_t sector_id)
Erase a single flash sector.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:52
Not listed.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:68
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.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:66
SPIFlashMode
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:46
SPIFlashSize size
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:79
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:58
SPIFlashSpeed
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:55
SPIFlashMode mode
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:77
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. Copied from bootloader header. See esp_image_header_t.
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:76
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:48
SPIFlashSpeed speed
Definition: Esp32/Components/spi_flash/include/esp_spi_flash.h:78
uint32_t flashmem_read(void *to, uint32_t fromaddr, uint32_t size)
Read a block of data from flash.