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/>.
23 #include "drivers/nvic.h"
24 #include "drivers/io.h"
27 #include "drivers/bus_spi.h"
28 #include "drivers/time.h"
31 #include "sdcard_standard.h"
33 #ifdef AFATFS_USE_INTROSPECTIVE_LOGGING
34 #define SDCARD_PROFILING
37 #define SDCARD_TIMEOUT_INIT_MILLIS 200
38 #define SDCARD_MAX_CONSECUTIVE_FAILURES 8
41 // In these states we run at the initialization 400kHz clockspeed:
42 SDCARD_STATE_NOT_PRESENT
= 0,
44 SDCARD_STATE_CARD_INIT_IN_PROGRESS
,
45 SDCARD_STATE_INITIALIZATION_RECEIVE_CID
,
47 // In these states we run at full clock speed
50 SDCARD_STATE_SENDING_WRITE
,
51 SDCARD_STATE_WAITING_FOR_WRITE
,
52 SDCARD_STATE_WRITING_MULTIPLE_BLOCKS
,
53 SDCARD_STATE_STOPPING_MULTIPLE_BLOCK_WRITE
56 typedef struct sdcard_t
{
62 sdcard_operationCompleteCallback_c callback
;
63 uint32_t callbackData
;
65 #ifdef SDCARD_PROFILING
66 uint32_t profileStartTime
;
70 uint32_t operationStartTime
;
77 uint32_t multiWriteNextBlock
;
78 uint32_t multiWriteBlocksRemain
;
82 sdcardMetadata_t metadata
;
85 #ifdef SDCARD_PROFILING
86 sdcard_profilerCallback_c profiler
;
89 bool detectionInverted
;
97 #ifdef USE_SDCARD_SDIO
98 dmaIdentifier_e dmaIdentifier
;
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
);
126 #ifdef USE_SDCARD_SPI
127 extern sdcardVTable_t sdcardSpiVTable
;
129 #ifdef USE_SDCARD_SDIO
130 extern sdcardVTable_t sdcardSdioVTable
;