Use the cached value of useDshotTelemetry to ensure consistent runtime use if dshot_b...
[betaflight.git] / src / main / drivers / flash.h
blobda75304bb6e90364fdb30946947b5753bc2ebf22
1 /*
2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
8 * any later version.
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
21 #pragma once
23 #include <stdint.h>
25 #include "pg/flash.h"
26 #include "drivers/io.h"
28 // Maximum page size of all supported SPI flash devices.
29 // Used to detect flashfs allocation size being too small.
30 #define FLASH_MAX_PAGE_SIZE 2048
32 #define SPIFLASH_INSTRUCTION_RDID 0x9F
34 typedef enum {
35 FLASH_TYPE_NOR = 0,
36 FLASH_TYPE_NAND
37 } flashType_e;
39 typedef uint16_t flashSector_t;
41 typedef struct flashGeometry_s {
42 flashSector_t sectors; // Count of the number of erasable blocks on the device
43 uint16_t pageSize; // In bytes
44 uint32_t sectorSize; // This is just pagesPerSector * pageSize
45 uint32_t totalSize; // This is just sectorSize * sectors
46 uint16_t pagesPerSector;
47 flashType_e flashType;
48 uint32_t jedecId;
49 } flashGeometry_t;
52 typedef enum {
54 * When set it indicates the system was booted in memory mapped mode, flash chip is already configured by
55 * the bootloader and does not need re-configuration.
56 * When un-set the system was booted normally and the flash chip needs configuration before use.
58 FLASH_CF_SYSTEM_IS_MEMORY_MAPPED = (1 << 0),
59 } flashConfigurationFlags_e;
61 void flashPreInit(const flashConfig_t *flashConfig);
62 bool flashInit(const flashConfig_t *flashConfig);
64 bool flashIsReady(void);
65 bool flashWaitForReady(void);
66 void flashEraseSector(uint32_t address);
67 void flashEraseCompletely(void);
68 void flashPageProgramBegin(uint32_t address, void (*callback)(uint32_t arg));
69 uint32_t flashPageProgramContinue(const uint8_t **buffers, uint32_t *bufferSizes, uint32_t bufferCount);
70 void flashPageProgramFinish(void);
71 void flashPageProgram(uint32_t address, const uint8_t *data, uint32_t length, void (*callback)(uint32_t length));
72 int flashReadBytes(uint32_t address, uint8_t *buffer, uint32_t length);
73 void flashFlush(void);
74 const flashGeometry_t *flashGetGeometry(void);
76 void flashMemoryMappedModeDisable(void);
77 void flashMemoryMappedModeEnable(void);
81 // flash partitioning api
84 typedef struct flashPartition_s {
85 uint8_t type;
86 flashSector_t startSector;
87 flashSector_t endSector;
88 } flashPartition_t;
90 #define FLASH_PARTITION_SECTOR_COUNT(partition) (partition->endSector + 1 - partition->startSector) // + 1 for inclusive, start and end sector can be the same sector.
92 // Must be in sync with flashPartitionTypeNames[]
93 // Should not be deleted or reordered once the code is writing a table to a flash.
94 typedef enum {
95 FLASH_PARTITION_TYPE_UNKNOWN = 0,
96 FLASH_PARTITION_TYPE_PARTITION_TABLE,
97 FLASH_PARTITION_TYPE_FLASHFS,
98 FLASH_PARTITION_TYPE_BADBLOCK_MANAGEMENT,
99 FLASH_PARTITION_TYPE_FIRMWARE,
100 FLASH_PARTITION_TYPE_CONFIG,
101 FLASH_MAX_PARTITIONS
102 } flashPartitionType_e;
104 typedef struct flashPartitionTable_s {
105 flashPartition_t partitions[FLASH_MAX_PARTITIONS];
106 } flashPartitionTable_t;
108 void flashPartitionSet(uint8_t index, uint32_t startSector, uint32_t endSector);
109 flashPartition_t *flashPartitionFindByType(flashPartitionType_e type);
110 const flashPartition_t *flashPartitionFindByIndex(uint8_t index);
111 const char *flashPartitionGetTypeName(flashPartitionType_e type);
112 int flashPartitionCount(void);