Merge pull request #11297 from SteveCEvans/baro_state
[betaflight.git] / src / main / drivers / sdcard_impl.h
blobddf23d9d8149f4d9a88fc8f9178eed3a9ae522f0
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/>.
21 #pragma once
23 #include "drivers/nvic.h"
24 #include "drivers/io.h"
25 #include "dma.h"
27 #include "drivers/bus_spi.h"
28 #include "drivers/time.h"
30 #include "sdcard.h"
31 #include "sdcard_standard.h"
33 #ifdef AFATFS_USE_INTROSPECTIVE_LOGGING
34 #define SDCARD_PROFILING
35 #endif
37 #define SDCARD_TIMEOUT_INIT_MILLIS 200
38 #define SDCARD_MAX_CONSECUTIVE_FAILURES 8
40 typedef enum {
41 // In these states we run at the initialization 400kHz clockspeed:
42 SDCARD_STATE_NOT_PRESENT = 0,
43 SDCARD_STATE_RESET,
44 SDCARD_STATE_CARD_INIT_IN_PROGRESS,
45 SDCARD_STATE_INITIALIZATION_RECEIVE_CID,
47 // In these states we run at full clock speed
48 SDCARD_STATE_READY,
49 SDCARD_STATE_READING,
50 SDCARD_STATE_SENDING_WRITE,
51 SDCARD_STATE_WAITING_FOR_WRITE,
52 SDCARD_STATE_WRITING_MULTIPLE_BLOCKS,
53 SDCARD_STATE_STOPPING_MULTIPLE_BLOCK_WRITE
54 } sdcardState_e;
56 typedef struct sdcard_t {
57 struct {
58 uint8_t *buffer;
59 uint32_t blockIndex;
60 uint8_t chunkIndex;
62 sdcard_operationCompleteCallback_c callback;
63 uint32_t callbackData;
65 #ifdef SDCARD_PROFILING
66 uint32_t profileStartTime;
67 #endif
68 } pendingOperation;
70 uint32_t operationStartTime;
72 uint8_t failureCount;
74 uint8_t version;
75 bool highCapacity;
77 uint32_t multiWriteNextBlock;
78 uint32_t multiWriteBlocksRemain;
80 sdcardState_e state;
82 sdcardMetadata_t metadata;
83 sdcardCSD_t csd;
85 #ifdef SDCARD_PROFILING
86 sdcard_profilerCallback_c profiler;
87 #endif
88 bool enabled;
89 bool detectionInverted;
90 IO_t cardDetectPin;
92 #ifdef USE_SDCARD_SPI
93 extDevice_t dev;
94 uint8_t idleCount;
95 #endif
97 #ifdef USE_SDCARD_SDIO
98 dmaIdentifier_e dmaIdentifier;
99 uint8_t useCache;
100 #endif
102 uint8_t dmaChannel;
103 } sdcard_t;
105 extern sdcard_t sdcard;
107 STATIC_ASSERT(sizeof(sdcardCSD_t) == 16, sdcard_csd_bitfields_didnt_pack_properly);
109 bool sdcard_isInserted(void);
111 typedef struct sdcardVTable_s {
112 void (*sdcard_preInit)(const sdcardConfig_t *config);
113 void (*sdcard_init)(const sdcardConfig_t *config, const spiPinConfig_t *spiConfig);
114 bool (*sdcard_readBlock)(uint32_t blockIndex, uint8_t *buffer, sdcard_operationCompleteCallback_c callback, uint32_t callbackData);
115 sdcardOperationStatus_e (*sdcard_beginWriteBlocks)(uint32_t blockIndex, uint32_t blockCount);
116 sdcardOperationStatus_e (*sdcard_writeBlock)(uint32_t blockIndex, uint8_t *buffer, sdcard_operationCompleteCallback_c callback, uint32_t callbackData);
117 bool (*sdcard_poll)(void);
118 bool (*sdcard_isFunctional)(void);
119 bool (*sdcard_isInitialized)(void);
120 const sdcardMetadata_t* (*sdcard_getMetadata)(void);
121 #ifdef SDCARD_PROFILING
122 void (*sdcardSdio_setProfilerCallback)(sdcard_profilerCallback_c callback);
123 #endif
124 } sdcardVTable_t;
126 #ifdef USE_SDCARD_SPI
127 extern sdcardVTable_t sdcardSpiVTable;
128 #endif
129 #ifdef USE_SDCARD_SDIO
130 extern sdcardVTable_t sdcardSdioVTable;
131 #endif