Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / flash.h
blob4dfa8cb19539dda738f6f14aa8f12f176d90be6d
1 /*
2 * This file is part of INAV.
4 * INAV is 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 * INAV is distributed in the hope that it
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 "common/time.h"
27 //#ifdef USE_FLASHFS
29 typedef uint16_t flashSector_t;
31 typedef enum {
32 FLASH_TYPE_NOR = 0,
33 FLASH_TYPE_NAND
34 } flashType_e;
36 typedef struct flashGeometry_s {
37 flashSector_t sectors; // Count of the number of erasable blocks on the device
38 uint16_t pageSize; // In bytes
39 uint32_t sectorSize; // This is just pagesPerSector * pageSize
40 uint32_t totalSize; // This is just sectorSize * sectors
41 uint16_t pagesPerSector;
42 flashType_e flashType;
43 } flashGeometry_t;
45 typedef struct
47 bool (*init)(int flashNumToUse);
48 bool (*isReady)(void);
49 bool (*waitForReady)(timeMs_t timeoutMillis);
50 void (*eraseSector)(uint32_t address);
51 void (*eraseCompletely)(void);
52 uint32_t (*pageProgram)(uint32_t address, const uint8_t *data, int length);
53 int (*readBytes)(uint32_t address, uint8_t *buffer, int length);
54 void (*flush)(void);
55 const flashGeometry_t *(*getGeometry)(void);
56 } flashDriver_t;
58 bool flashInit(void);
60 bool flashIsReady(void);
61 bool flashWaitForReady(timeMs_t timeoutMillis);
62 void flashEraseSector(uint32_t address);
63 void flashEraseCompletely(void);
64 uint32_t flashPageProgram(uint32_t address, const uint8_t *data, int length);
65 int flashReadBytes(uint32_t address, uint8_t *buffer, int length);
66 void flashFlush(void);
67 const flashGeometry_t *flashGetGeometry(void);
70 // flash partitioning api
73 typedef struct flashPartition_s {
74 uint8_t type;
75 flashSector_t startSector;
76 flashSector_t endSector;
77 } flashPartition_t;
79 #define FLASH_PARTITION_SECTOR_COUNT(partition) (partition->endSector + 1 - partition->startSector) // + 1 for inclusive, start and end sector can be the same sector.
81 // Must be in sync with flashPartitionTypeNames[]
82 // Should not be deleted or reordered once the code is writing a table to a flash.
83 typedef enum {
84 FLASH_PARTITION_TYPE_UNKNOWN = 0,
85 FLASH_PARTITION_TYPE_PARTITION_TABLE,
86 FLASH_PARTITION_TYPE_FLASHFS,
87 FLASH_PARTITION_TYPE_BADBLOCK_MANAGEMENT,
88 FLASH_PARTITION_TYPE_FIRMWARE,
89 FLASH_PARTITION_TYPE_CONFIG,
90 FLASH_PARTITION_TYPE_FULL_BACKUP,
91 FLASH_PARTITION_TYPE_FIRMWARE_UPDATE_META,
92 FLASH_PARTITION_TYPE_UPDATE_FIRMWARE,
93 FLASH_MAX_PARTITIONS
94 } flashPartitionType_e;
96 typedef struct flashPartitionTable_s {
97 flashPartition_t partitions[FLASH_MAX_PARTITIONS];
98 } flashPartitionTable_t;
100 void flashPartitionSet(uint8_t index, uint32_t startSector, uint32_t endSector);
101 flashPartition_t *flashPartitionFindByType(flashPartitionType_e type);
102 const flashPartition_t *flashPartitionFindByIndex(uint8_t index);
103 const char *flashPartitionGetTypeName(flashPartitionType_e type);
104 int flashPartitionCount(void);
105 uint32_t flashPartitionSize(flashPartition_t *partition);
106 void flashPartitionErase(flashPartition_t *partition);
108 //#endif [> USE_FLASHFS <]