Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / sdcard / sdcard_impl.h
blob9fcb9a469025fdecc4204a874556085593a2adf9
1 /*
2 * This file is part of Cleanflight, INAV and Betaflight.
4 * Cleanflight, INAV 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 "drivers/nvic.h"
24 #include "drivers/io.h"
26 #include "drivers/bus.h"
27 #include "drivers/bus_spi.h"
28 #include "drivers/time.h"
30 #include "drivers/sdcard/sdcard.h"
31 #include "drivers/sdcard/sdcard_standard.h"
33 #define SDCARD_TIMEOUT_INIT_MILLIS 200
34 #define SDCARD_MAX_CONSECUTIVE_FAILURES 8
36 typedef enum {
37 // In these states we run at the initialization 400kHz clockspeed:
38 SDCARD_STATE_NOT_PRESENT = 0,
39 SDCARD_STATE_RESET,
40 SDCARD_STATE_CARD_INIT_IN_PROGRESS,
41 SDCARD_STATE_INITIALIZATION_RECEIVE_CID,
43 // In these states we run at full clock speed
44 SDCARD_STATE_READY,
45 SDCARD_STATE_READING,
46 SDCARD_STATE_SENDING_WRITE,
47 SDCARD_STATE_WAITING_FOR_WRITE,
48 SDCARD_STATE_WRITING_MULTIPLE_BLOCKS,
49 SDCARD_STATE_STOPPING_MULTIPLE_BLOCK_WRITE,
50 } sdcardState_e;
52 typedef struct sdcard_t {
53 struct {
54 uint8_t *buffer;
55 uint32_t blockIndex;
56 uint8_t chunkIndex;
58 sdcard_operationCompleteCallback_c callback;
59 uint32_t callbackData;
60 } pendingOperation;
62 uint32_t operationStartTime;
64 uint8_t failureCount;
66 uint8_t version;
67 bool highCapacity;
69 uint32_t multiWriteNextBlock;
70 uint32_t multiWriteBlocksRemain;
72 sdcardState_e state;
73 sdcardMetadata_t metadata;
74 sdcardCSD_t csd;
76 IO_t cardDetectPin;
78 #if defined(USE_SDCARD_SPI)
79 busDevice_t * dev;
80 #endif
82 #if defined(USE_SDCARD_SDIO)
83 DMA_t dma;
84 #endif
85 } sdcard_t;
87 extern sdcard_t sdcard;
89 STATIC_ASSERT(sizeof(sdcardCSD_t) == 16, sdcard_csd_bitfields_didnt_pack_properly);
91 void sdcardInsertionDetectInit(void);
92 void sdcardInsertionDetectDeinit(void);
93 bool sdcard_isInserted(void);
95 typedef struct sdcardVTable_s {
96 void (*init)(void);
97 bool (*readBlock)(uint32_t blockIndex, uint8_t *buffer, sdcard_operationCompleteCallback_c callback, uint32_t callbackData);
98 sdcardOperationStatus_e (*beginWriteBlocks)(uint32_t blockIndex, uint32_t blockCount);
99 sdcardOperationStatus_e (*writeBlock)(uint32_t blockIndex, uint8_t *buffer, sdcard_operationCompleteCallback_c callback, uint32_t callbackData);
100 bool (*poll)(void);
101 bool (*isFunctional)(void);
102 bool (*isInitialized)(void);
103 const sdcardMetadata_t* (*getMetadata)(void);
104 } sdcardVTable_t;
106 #ifdef USE_SDCARD_SPI
107 extern sdcardVTable_t sdcardSpiVTable;
108 #endif
110 #ifdef USE_SDCARD_SDIO
111 extern sdcardVTable_t sdcardSdioVTable;
112 #endif