FIX: Invalid references when neither DSHOT nor PWM_OUTPUT is defined. (#14135)
[betaflight.git] / src / main / drivers / flash / flash.h
blob964ba549c46da6c982663302d5a158dcef96be5c
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;
51 typedef enum {
53 * When set it indicates the system was booted in memory mapped mode, flash chip is already configured by
54 * the bootloader and does not need re-configuration.
55 * When un-set the system was booted normally and the flash chip needs configuration before use.
57 FLASH_CF_SYSTEM_IS_MEMORY_MAPPED = (1 << 0),
58 } flashConfigurationFlags_e;
60 void flashPreInit(const flashConfig_t *flashConfig);
61 bool flashInit(const flashConfig_t *flashConfig);
63 bool flashIsReady(void);
64 bool flashWaitForReady(void);
65 void flashEraseSector(uint32_t address);
66 void flashEraseCompletely(void);
67 void flashPageProgramBegin(uint32_t address, void (*callback)(uint32_t arg));
68 uint32_t flashPageProgramContinue(const uint8_t **buffers, uint32_t *bufferSizes, uint32_t bufferCount);
69 void flashPageProgramFinish(void);
70 void flashPageProgram(uint32_t address, const uint8_t *data, uint32_t length, void (*callback)(uint32_t length));
71 int flashReadBytes(uint32_t address, uint8_t *buffer, uint32_t length);
72 void flashFlush(void);
73 const flashGeometry_t *flashGetGeometry(void);
75 void flashMemoryMappedModeDisable(void);
76 void flashMemoryMappedModeEnable(void);
79 // flash partitioning api
82 typedef struct flashPartition_s {
83 uint8_t type;
84 flashSector_t startSector;
85 flashSector_t endSector;
86 } flashPartition_t;
88 #define FLASH_PARTITION_SECTOR_COUNT(partition) (partition->endSector + 1 - partition->startSector) // + 1 for inclusive, start and end sector can be the same sector.
90 // Must be in sync with flashPartitionTypeNames[]
91 // Should not be deleted or reordered once the code is writing a table to a flash.
92 typedef enum {
93 FLASH_PARTITION_TYPE_UNKNOWN = 0,
94 FLASH_PARTITION_TYPE_PARTITION_TABLE,
95 FLASH_PARTITION_TYPE_FLASHFS,
96 FLASH_PARTITION_TYPE_BADBLOCK_MANAGEMENT,
97 FLASH_PARTITION_TYPE_FIRMWARE,
98 FLASH_PARTITION_TYPE_CONFIG,
99 FLASH_MAX_PARTITIONS
100 } flashPartitionType_e;
102 typedef struct flashPartitionTable_s {
103 flashPartition_t partitions[FLASH_MAX_PARTITIONS];
104 } flashPartitionTable_t;
106 void flashPartitionSet(uint8_t index, uint32_t startSector, uint32_t endSector);
107 flashPartition_t *flashPartitionFindByType(flashPartitionType_e type);
108 const flashPartition_t *flashPartitionFindByIndex(uint8_t index);
109 const char *flashPartitionGetTypeName(flashPartitionType_e type);
110 int flashPartitionCount(void);