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)
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/>.
26 * Each flash chip should:
28 * * expose a public `identify` method.
29 * - return true if the driver supports the passed JEDEC ID and false otherwise.
30 * - configure the `geometry` member of the flashDevice_t or set all `geometry` members to 0 if driver doesn't support the JEDEC ID.
31 * - configure the `vTable` member, with an appropriate API.
32 * - configure remaining flashDevice_t members, as appropriate.
33 * * not reconfigure the bus or flash chip when in memory mapped mode.
34 * - the firmware is free to do whatever it wants when memory mapped mode is disabled
35 * - when memory mapped mode is restored, e.g. after saving config to external flash, it should be in
36 * the same state that firmware found it in before the firmware disabled memory mapped mode.
38 * When memory mapped mode is disabled the following applies to all flash chip drivers uses in a memory mapped system:
39 * - do not call any methods or use data from the flash chip. i.e. memory mapped code/data is INACCESSIBLE.
40 * i.e. when saving the config, *ALL* the code to erase a block and write data should be in RAM,
41 * this includes any `delay` methods.
42 * - not require the use of use any ISRs - interrupts are disabled during flash access when memory mapped mode is disabled.
44 * When compiling a driver for use in a memory mapped flash system the following applies:
45 * - the vTable must live in RAM so it can be used when memory mapped mode is disabled.
46 * - other constant data structures that usually live in flash memory must be stored in RAM.
47 * - methods used to erase sectors, write data and read data much live in RAM.
51 #include "drivers/bus.h"
52 #include "drivers/dma.h"
61 } flashDeviceIoMode_e
;
63 typedef struct flashDeviceIO_s
{
66 extDevice_t
*dev
; // Device interface dependent handle (spi/i2c)
68 #ifdef USE_FLASH_QUADSPI
69 QUADSPI_TypeDef
*quadSpi
;
71 #ifdef USE_FLASH_OCTOSPI
72 OCTOSPI_TypeDef
*octoSpi
;
75 flashDeviceIoMode_e mode
;
78 typedef struct flashDevice_s
{
80 // members to be configured by the flash chip implementation
83 const struct flashVTable_s
*vTable
;
84 flashGeometry_t geometry
;
85 uint32_t currentWriteAddress
;
87 // Whether we've performed an action that could have made the device busy
88 // for writes. This allows us to avoid polling for writable status
89 // when it is definitely ready already.
94 // members configured by the flash detection system, read-only from the flash chip implementation's perspective.
98 void (*callback
)(uint32_t arg
);
102 typedef struct flashVTable_s
{
103 void (*configure
)(flashDevice_t
*fdevice
, uint32_t configurationFlags
);
105 bool (*isReady
)(flashDevice_t
*fdevice
);
106 bool (*waitForReady
)(flashDevice_t
*fdevice
);
108 void (*eraseSector
)(flashDevice_t
*fdevice
, uint32_t address
);
109 void (*eraseCompletely
)(flashDevice_t
*fdevice
);
111 void (*pageProgramBegin
)(flashDevice_t
*fdevice
, uint32_t address
, void (*callback
)(uint32_t length
));
112 uint32_t (*pageProgramContinue
)(flashDevice_t
*fdevice
, uint8_t const **buffers
, uint32_t *bufferSizes
, uint32_t bufferCount
);
113 void (*pageProgramFinish
)(flashDevice_t
*fdevice
);
114 void (*pageProgram
)(flashDevice_t
*fdevice
, uint32_t address
, const uint8_t *data
, uint32_t length
, void (*callback
)(uint32_t length
));
116 void (*flush
)(flashDevice_t
*fdevice
);
118 int (*readBytes
)(flashDevice_t
*fdevice
, uint32_t address
, uint8_t *buffer
, uint32_t length
);
120 const flashGeometry_t
*(*getGeometry
)(flashDevice_t
*fdevice
);