2 * This file is part of INAV.
4 * INAV is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * INAV is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with INAV. If not, see <http://www.gnu.org/licenses/>.
23 #if defined(USE_SDCARD)
25 #include "build/debug.h"
26 #include "common/utils.h"
28 #include "drivers/time.h"
29 #include "drivers/nvic.h"
30 #include "drivers/io.h"
31 #include "drivers/bus.h"
33 #include "drivers/sdcard/sdcard.h"
34 #include "drivers/sdcard/sdcard_impl.h"
35 #include "drivers/sdcard/sdcard_standard.h"
37 #include "scheduler/protothreads.h"
39 #ifndef SDCARD_DETECT_PIN
40 #define SDCARD_DETECT_PIN NONE
45 void sdcardInsertionDetectDeinit(void)
47 sdcard
.cardDetectPin
= IOGetByTag(IO_TAG(SDCARD_DETECT_PIN
));
49 if (sdcard
.cardDetectPin
) {
50 IOInit(sdcard
.cardDetectPin
, OWNER_FREE
, RESOURCE_NONE
, 0);
51 IOConfigGPIO(sdcard
.cardDetectPin
, IOCFG_IN_FLOATING
);
55 void sdcardInsertionDetectInit(void)
57 sdcard
.cardDetectPin
= IOGetByTag(IO_TAG(SDCARD_DETECT_PIN
));
59 if (sdcard
.cardDetectPin
) {
60 IOInit(sdcard
.cardDetectPin
, OWNER_SDCARD
, RESOURCE_INPUT
, 0);
61 IOConfigGPIO(sdcard
.cardDetectPin
, IOCFG_IPU
);
66 * Returns true if the card is physically inserted into the slot
68 bool sdcard_isInserted(void)
70 if (sdcard
.cardDetectPin
) {
71 bool result
= (IORead(sdcard
.cardDetectPin
) != 0);
72 #if defined(SDCARD_DETECT_INVERTED)
86 sdcardVTable_t
*sdcardVTable
= NULL
;
88 void sdcard_init(void)
90 #if defined(USE_SDCARD_SPI)
91 sdcardVTable
= &sdcardSpiVTable
;
92 #elif defined(USE_SDCARD_SDIO)
93 sdcardVTable
= &sdcardSdioVTable
;
101 bool sdcard_readBlock(uint32_t blockIndex
, uint8_t *buffer
, sdcard_operationCompleteCallback_c callback
, uint32_t callbackData
)
104 return sdcardVTable
->readBlock(blockIndex
, buffer
, callback
, callbackData
);
110 sdcardOperationStatus_e
sdcard_beginWriteBlocks(uint32_t blockIndex
, uint32_t blockCount
)
113 return sdcardVTable
->beginWriteBlocks(blockIndex
, blockCount
);
119 sdcardOperationStatus_e
sdcard_writeBlock(uint32_t blockIndex
, uint8_t *buffer
, sdcard_operationCompleteCallback_c callback
, uint32_t callbackData
)
122 return sdcardVTable
->writeBlock(blockIndex
, buffer
, callback
, callbackData
);
128 bool sdcard_poll(void)
131 return sdcardVTable
->poll();
137 bool sdcard_isFunctional(void)
140 return sdcardVTable
->isFunctional();
146 bool sdcard_isInitialized(void)
149 return sdcardVTable
->isInitialized();
155 const sdcardMetadata_t
* sdcard_getMetadata(void)
158 return sdcardVTable
->getMetadata();