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)
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"
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
37 // In these states we run at the initialization 400kHz clockspeed:
38 SDCARD_STATE_NOT_PRESENT
= 0,
40 SDCARD_STATE_CARD_INIT_IN_PROGRESS
,
41 SDCARD_STATE_INITIALIZATION_RECEIVE_CID
,
43 // In these states we run at full clock speed
46 SDCARD_STATE_SENDING_WRITE
,
47 SDCARD_STATE_WAITING_FOR_WRITE
,
48 SDCARD_STATE_WRITING_MULTIPLE_BLOCKS
,
49 SDCARD_STATE_STOPPING_MULTIPLE_BLOCK_WRITE
,
52 typedef struct sdcard_t
{
58 sdcard_operationCompleteCallback_c callback
;
59 uint32_t callbackData
;
62 uint32_t operationStartTime
;
69 uint32_t multiWriteNextBlock
;
70 uint32_t multiWriteBlocksRemain
;
73 sdcardMetadata_t metadata
;
78 #if defined(USE_SDCARD_SPI)
82 #if defined(USE_SDCARD_SDIO)
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
{
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
);
101 bool (*isFunctional
)(void);
102 bool (*isInitialized
)(void);
103 const sdcardMetadata_t
* (*getMetadata
)(void);
106 #ifdef USE_SDCARD_SPI
107 extern sdcardVTable_t sdcardSpiVTable
;
110 #ifdef USE_SDCARD_SDIO
111 extern sdcardVTable_t sdcardSdioVTable
;