Merge pull request #11939 from blckmn/flash-fix
[betaflight.git] / src / main / drivers / flash_impl.h
blobafa0fdd1a6dbf4f470a140434e2d681ebebdda23
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/>.
22 * Author: jflyper
25 #pragma once
27 #include "drivers/bus.h"
28 #include "drivers/dma.h"
30 struct flashVTable_s;
32 typedef enum {
33 FLASHIO_NONE = 0,
34 FLASHIO_SPI,
35 FLASHIO_QUADSPI
36 } flashDeviceIoMode_e;
38 typedef struct flashDeviceIO_s {
39 union {
40 extDevice_t *dev; // Device interface dependent handle (spi/i2c)
41 #ifdef USE_QUADSPI
42 QUADSPI_TypeDef *quadSpi;
43 #endif
44 } handle;
45 flashDeviceIoMode_e mode;
46 } flashDeviceIO_t;
48 typedef struct flashDevice_s {
49 const struct flashVTable_s *vTable;
50 flashGeometry_t geometry;
51 uint32_t currentWriteAddress;
52 bool isLargeFlash;
53 // Whether we've performed an action that could have made the device busy
54 // for writes. This allows us to avoid polling for writable status
55 // when it is definitely ready already.
56 bool couldBeBusy;
57 uint32_t timeoutAt;
58 flashDeviceIO_t io;
59 void (*callback)(uint32_t arg);
60 uint32_t callbackArg;
61 } flashDevice_t;
63 typedef struct flashVTable_s {
64 bool (*isReady)(flashDevice_t *fdevice);
65 bool (*waitForReady)(flashDevice_t *fdevice);
66 void (*eraseSector)(flashDevice_t *fdevice, uint32_t address);
67 void (*eraseCompletely)(flashDevice_t *fdevice);
68 void (*pageProgramBegin)(flashDevice_t *fdevice, uint32_t address, void (*callback)(uint32_t length));
69 uint32_t (*pageProgramContinue)(flashDevice_t *fdevice, uint8_t const **buffers, uint32_t *bufferSizes, uint32_t bufferCount);
70 void (*pageProgramFinish)(flashDevice_t *fdevice);
71 void (*pageProgram)(flashDevice_t *fdevice, uint32_t address, const uint8_t *data, uint32_t length, void (*callback)(uint32_t length));
72 void (*flush)(flashDevice_t *fdevice);
73 int (*readBytes)(flashDevice_t *fdevice, uint32_t address, uint8_t *buffer, uint32_t length);
74 const flashGeometry_t *(*getGeometry)(flashDevice_t *fdevice);
75 } flashVTable_t;