Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / sdcard / sdcard.c
blobff8cc4fa850bbd1d6f9edd8cb50427eed8d50a83
1 /*
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/>.
18 #include <stdbool.h>
19 #include <stdint.h>
21 #include "platform.h"
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
41 #endif
43 sdcard_t sdcard;
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);
65 /**
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)
73 return !result;
74 #else
75 return result;
76 #endif
78 else {
79 return true;
83 /**
84 * Dispatch
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;
94 #endif
96 if (sdcardVTable) {
97 sdcardVTable->init();
101 bool sdcard_readBlock(uint32_t blockIndex, uint8_t *buffer, sdcard_operationCompleteCallback_c callback, uint32_t callbackData)
103 if (sdcardVTable) {
104 return sdcardVTable->readBlock(blockIndex, buffer, callback, callbackData);
105 } else {
106 return false;
110 sdcardOperationStatus_e sdcard_beginWriteBlocks(uint32_t blockIndex, uint32_t blockCount)
112 if (sdcardVTable) {
113 return sdcardVTable->beginWriteBlocks(blockIndex, blockCount);
114 } else {
115 return false;
119 sdcardOperationStatus_e sdcard_writeBlock(uint32_t blockIndex, uint8_t *buffer, sdcard_operationCompleteCallback_c callback, uint32_t callbackData)
121 if (sdcardVTable) {
122 return sdcardVTable->writeBlock(blockIndex, buffer, callback, callbackData);
123 } else {
124 return false;
128 bool sdcard_poll(void)
130 if (sdcardVTable) {
131 return sdcardVTable->poll();
132 } else {
133 return false;
137 bool sdcard_isFunctional(void)
139 if (sdcardVTable) {
140 return sdcardVTable->isFunctional();
141 } else {
142 return false;
146 bool sdcard_isInitialized(void)
148 if (sdcardVTable) {
149 return sdcardVTable->isInitialized();
150 } else {
151 return false;
155 const sdcardMetadata_t* sdcard_getMetadata(void)
157 if (sdcardVTable) {
158 return sdcardVTable->getMetadata();
160 else {
161 return NULL;
165 #endif