New SPI API supporting DMA
[betaflight.git] / src / main / drivers / flash_impl.h
blob4d0aa1e30fe64a3fb6b025d766900c154cd13431
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"
29 struct flashVTable_s;
31 typedef enum {
32 FLASHIO_NONE = 0,
33 FLASHIO_SPI,
34 FLASHIO_QUADSPI
35 } flashDeviceIoMode_e;
37 typedef struct flashDeviceIO_s {
38 union {
39 extDevice_t *dev; // Device interface dependent handle (spi/i2c)
40 #ifdef USE_QUADSPI
41 QUADSPI_TypeDef *quadSpi;
42 #endif
43 } handle;
44 flashDeviceIoMode_e mode;
45 } flashDeviceIO_t;
47 typedef struct flashDevice_s {
48 const struct flashVTable_s *vTable;
49 flashGeometry_t geometry;
50 uint32_t currentWriteAddress;
51 bool isLargeFlash;
52 // Whether we've performed an action that could have made the device busy
53 // for writes. This allows us to avoid polling for writable status
54 // when it is definitely ready already.
55 bool couldBeBusy;
56 uint32_t timeoutAt;
57 flashDeviceIO_t io;
58 void (*callback)(uint32_t arg);
59 uint32_t callbackArg;
60 } flashDevice_t;
62 typedef struct flashVTable_s {
63 bool (*isReady)(flashDevice_t *fdevice);
64 bool (*waitForReady)(flashDevice_t *fdevice);
65 void (*eraseSector)(flashDevice_t *fdevice, uint32_t address);
66 void (*eraseCompletely)(flashDevice_t *fdevice);
67 void (*pageProgramBegin)(flashDevice_t *fdevice, uint32_t address);
68 void (*pageProgramContinue)(flashDevice_t *fdevice, const uint8_t *data, int length);
69 void (*pageProgramFinish)(flashDevice_t *fdevice);
70 void (*pageProgram)(flashDevice_t *fdevice, uint32_t address, const uint8_t *data, int length);
71 void (*flush)(flashDevice_t *fdevice);
72 int (*readBytes)(flashDevice_t *fdevice, uint32_t address, uint8_t *buffer, int length);
73 const flashGeometry_t *(*getGeometry)(flashDevice_t *fdevice);
74 } flashVTable_t;