Merge pull request #11198 from SteveCEvans/sce_rc2
[betaflight.git] / src / main / drivers / flash.h
blobbdbd6583a3e9c134525219c92a1c8de91c995d09
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 } flashGeometry_t;
50 void flashPreInit(const flashConfig_t *flashConfig);
51 bool flashInit(const flashConfig_t *flashConfig);
53 bool flashIsReady(void);
54 bool flashWaitForReady(void);
55 void flashEraseSector(uint32_t address);
56 void flashEraseCompletely(void);
57 void flashPageProgramBegin(uint32_t address, void (*callback)(uint32_t arg));
58 uint32_t flashPageProgramContinue(const uint8_t **buffers, uint32_t *bufferSizes, uint32_t bufferCount);
59 void flashPageProgramFinish(void);
60 void flashPageProgram(uint32_t address, const uint8_t *data, uint32_t length, void (*callback)(uint32_t length));
61 int flashReadBytes(uint32_t address, uint8_t *buffer, uint32_t length);
62 void flashFlush(void);
63 const flashGeometry_t *flashGetGeometry(void);
66 // flash partitioning api
69 typedef struct flashPartition_s {
70 uint8_t type;
71 flashSector_t startSector;
72 flashSector_t endSector;
73 } flashPartition_t;
75 #define FLASH_PARTITION_SECTOR_COUNT(partition) (partition->endSector + 1 - partition->startSector) // + 1 for inclusive, start and end sector can be the same sector.
77 // Must be in sync with flashPartitionTypeNames[]
78 // Should not be deleted or reordered once the code is writing a table to a flash.
79 typedef enum {
80 FLASH_PARTITION_TYPE_UNKNOWN = 0,
81 FLASH_PARTITION_TYPE_PARTITION_TABLE,
82 FLASH_PARTITION_TYPE_FLASHFS,
83 FLASH_PARTITION_TYPE_BADBLOCK_MANAGEMENT,
84 FLASH_PARTITION_TYPE_FIRMWARE,
85 FLASH_PARTITION_TYPE_CONFIG,
86 FLASH_MAX_PARTITIONS
87 } flashPartitionType_e;
89 typedef struct flashPartitionTable_s {
90 flashPartition_t partitions[FLASH_MAX_PARTITIONS];
91 } flashPartitionTable_t;
93 void flashPartitionSet(uint8_t index, uint32_t startSector, uint32_t endSector);
94 flashPartition_t *flashPartitionFindByType(flashPartitionType_e type);
95 const flashPartition_t *flashPartitionFindByIndex(uint8_t index);
96 const char *flashPartitionGetTypeName(flashPartitionType_e type);
97 int flashPartitionCount(void);