Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / sdcard / sdmmc_sdio.h
blobf488d334f9681efeaf04b5107744e27757f4d59a
1 /*
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)
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/>.
22 * Original author: Alain (https://github.com/aroyer-qc)
23 * Modified for F4 and BF source: Chris Hockuba (https://github.com/conkerkh)
24 * Adapted for INAV: Konstantin (https://github.com/digitalentity)
27 #pragma once
29 #include <stdint.h>
31 #if defined(USE_SDCARD_SDIO)
33 #include "platform.h"
35 #ifdef STM32F4
36 #include "stm32f4xx.h"
37 #endif
39 #ifdef STM32F7
40 #include "stm32f7xx.h"
41 #endif
43 #ifdef STM32H7
44 #include "stm32h7xx.h"
45 #endif
47 #ifdef AT32F43x
48 #include "at32f435_437.h"
49 #endif
51 /* SDCARD pinouts
53 * SD CARD PINS
54 _________________
55 / 1 2 3 4 5 6 7 8 | NR |SDIO INTERFACE
56 / | |NAME STM32F746 DESCRIPTION
57 / 9 | | 4-BIT 1-BIT
58 | | |
59 | | 1 |CD/DAT3 PC11 - Connector data line 3
60 | | 2 |CMD PD2 PD2 Command/Response line
61 | | 3 |VSS1 GND GND GND
62 | SD CARD Pinout | 4 |VDD 3.3V 3.3V 3.3V Power supply
63 | | 5 |CLK PC12 PC12 Clock
64 | | 6 |VSS2 GND GND GND
65 | | 7 |DAT0 PC8 PC8 Connector data line 0
66 | | 8 |DAT1 PC9 - Connector data line 1
67 |___________________| 9 |DAT2 PC10 - Connector data line 2
71 /* Define(s) --------------------------------------------------------------------------------------------------------*/
72 #define SD_DATATIMEOUT ((uint32_t)100000000)
74 /* Structure(s) -----------------------------------------------------------------------------------------------------*/
76 typedef enum
78 // SD specific error defines
79 SD_CMD_CRC_FAIL = (1), // Command response received (but CRC check failed)
80 SD_DATA_CRC_FAIL = (2), // Data block sent/received (CRC check failed)
81 SD_CMD_RSP_TIMEOUT = (3), // Command response TimeOut
82 SD_DATA_TIMEOUT = (4), // Data TimeOut
83 SD_TX_UNDERRUN = (5), // Transmit FIFO underrun
84 SD_RX_OVERRUN = (6), // Receive FIFO overrun
85 SD_START_BIT_ERR = (7), // Start bit not detected on all data signals in wide bus mode
86 SD_CMD_OUT_OF_RANGE = (8), // Command's argument was out of range.
87 SD_ADDR_MISALIGNED = (9), // Misaligned address
88 SD_BLOCK_LEN_ERR = (10), // Transferred block length is not allowed for the card or the number of transferred bytes does not match the block length
89 SD_ERASE_SEQ_ERR = (11), // An error in the sequence of erase command occurs.
90 SD_BAD_ERASE_PARAM = (12), // An invalid selection for erase groups
91 SD_WRITE_PROT_VIOLATION = (13), // Attempt to program a write protect block
92 SD_LOCK_UNLOCK_FAILED = (14), // Sequence or password error has been detected in unlock command or if there was an attempt to access a locked card
93 SD_COM_CRC_FAILED = (15), // CRC check of the previous command failed
94 SD_ILLEGAL_CMD = (16), // Command is not legal for the card state
95 SD_CARD_ECC_FAILED = (17), // Card internal ECC was applied but failed to correct the data
96 SD_CC_ERROR = (18), // Internal card controller error
97 SD_GENERAL_UNKNOWN_ERROR = (19), // General or unknown error
98 SD_STREAM_READ_UNDERRUN = (20), // The card could not sustain data transfer in stream read operation.
99 SD_STREAM_WRITE_OVERRUN = (21), // The card could not sustain data programming in stream mode
100 SD_CID_CSD_OVERWRITE = (22), // CID/CSD overwrite error
101 SD_WP_ERASE_SKIP = (23), // Only partial address space was erased
102 SD_CARD_ECC_DISABLED = (24), // Command has been executed without using internal ECC
103 SD_ERASE_RESET = (25), // Erase sequence was cleared before executing because an out of erase sequence command was received
104 SD_AKE_SEQ_ERROR = (26), // Error in sequence of authentication.
105 SD_INVALID_VOLTRANGE = (27),
106 SD_ADDR_OUT_OF_RANGE = (28),
107 SD_SWITCH_ERROR = (29),
108 SD_SDMMC_DISABLED = (30),
109 SD_SDMMC_FUNCTION_BUSY = (31),
110 SD_SDMMC_FUNCTION_FAILED = (32),
111 SD_SDMMC_UNKNOWN_FUNCTION = (33),
112 SD_OUT_OF_BOUND = (34),
115 // Standard error defines
116 SD_INTERNAL_ERROR = (35),
117 SD_NOT_CONFIGURED = (36),
118 SD_REQUEST_PENDING = (37),
119 SD_REQUEST_NOT_APPLICABLE = (38),
120 SD_INVALID_PARAMETER = (39),
121 SD_UNSUPPORTED_FEATURE = (40),
122 SD_UNSUPPORTED_HW = (41),
123 SD_ERROR = (42),
124 SD_BUSY = (43),
125 SD_OK = (0)
126 } SD_Error_t;
128 typedef struct
130 uint8_t DAT_BUS_WIDTH; // Shows the currently defined data bus width
131 uint8_t SECURED_MODE; // Card is in secured mode of operation
132 uint16_t SD_CARD_TYPE; // Carries information about card type
133 uint32_t SIZE_OF_PROTECTED_AREA; // Carries information about the capacity of protected area
134 uint8_t SPEED_CLASS; // Carries information about the speed class of the card
135 uint8_t PERFORMANCE_MOVE; // Carries information about the card's performance move
136 uint8_t AU_SIZE; // Carries information about the card's allocation unit size
137 uint16_t ERASE_SIZE; // Determines the number of AUs to be erased in one operation
138 uint8_t ERASE_TIMEOUT; // Determines the TimeOut for any number of AU erase
139 uint8_t ERASE_OFFSET; // Carries information about the erase offset
140 } SD_CardStatus_t;
142 typedef struct
144 uint8_t CSDStruct; // CSD structure
145 uint8_t SysSpecVersion; // System specification version
146 uint8_t Reserved1; // Reserved
147 uint8_t TAAC; // Data read access time 1
148 uint8_t NSAC; // Data read access time 2 in CLK cycles
149 uint8_t MaxBusClkFrec; // Max. bus clock frequency
150 uint16_t CardComdClasses; // Card command classes
151 uint8_t RdBlockLen; // Max. read data block length
152 uint8_t PartBlockRead; // Partial blocks for read allowed
153 uint8_t WrBlockMisalign; // Write block misalignment
154 uint8_t RdBlockMisalign; // Read block misalignment
155 uint8_t DSRImpl; // DSR implemented
156 uint8_t Reserved2; // Reserved
157 uint32_t DeviceSize; // Device Size
158 uint8_t MaxRdCurrentVDDMin; // Max. read current @ VDD min
159 uint8_t MaxRdCurrentVDDMax; // Max. read current @ VDD max
160 uint8_t MaxWrCurrentVDDMin; // Max. write current @ VDD min
161 uint8_t MaxWrCurrentVDDMax; // Max. write current @ VDD max
162 uint8_t DeviceSizeMul; // Device size multiplier
163 uint8_t EraseGrSize; // Erase group size
164 uint8_t EraseGrMul; // Erase group size multiplier
165 uint8_t WrProtectGrSize; // Write protect group size
166 uint8_t WrProtectGrEnable; // Write protect group enable
167 uint8_t ManDeflECC; // Manufacturer default ECC
168 uint8_t WrSpeedFact; // Write speed factor
169 uint8_t MaxWrBlockLen; // Max. write data block length
170 uint8_t WriteBlockPaPartial; // Partial blocks for write allowed
171 uint8_t Reserved3; // Reserved
172 uint8_t ContentProtectAppli; // Content protection application
173 uint8_t FileFormatGrouop; // File format group
174 uint8_t CopyFlag; // Copy flag (OTP)
175 uint8_t PermWrProtect; // Permanent write protection
176 uint8_t TempWrProtect; // Temporary write protection
177 uint8_t FileFormat; // File format
178 uint8_t ECC; // ECC code
179 uint8_t CSD_CRC; // CSD CRC
180 uint8_t Reserved4; // Always 1
181 } SD_CSD_t;
183 typedef struct
185 uint8_t ManufacturerID; // Manufacturer ID
186 uint16_t OEM_AppliID; // OEM/Application ID
187 uint32_t ProdName1; // Product Name part1
188 uint8_t ProdName2; // Product Name part2
189 uint8_t ProdRev; // Product Revision
190 uint32_t ProdSN; // Product Serial Number
191 uint8_t Reserved1; // Reserved1
192 uint16_t ManufactDate; // Manufacturing Date
193 uint8_t CID_CRC; // CID CRC
194 uint8_t Reserved2; // Always 1
195 } SD_CID_t;
197 typedef enum
199 SD_STD_CAPACITY_V1_1 = 0,
200 SD_STD_CAPACITY_V2_0 = 1,
201 SD_HIGH_CAPACITY = 2,
202 SD_MULTIMEDIA = 3,
203 SD_SECURE_DIGITAL_IO = 4,
204 SD_HIGH_SPEED_MULTIMEDIA = 5,
205 SD_SECURE_DIGITAL_IO_COMBO = 6,
206 SD_HIGH_CAPACITY_MMC = 7,
207 } SD_CardType_t;
209 typedef struct
211 volatile SD_CSD_t SD_csd; // SD card specific data register
212 volatile SD_CID_t SD_cid; // SD card identification number register
213 uint64_t CardCapacity; // Card capacity
214 uint32_t CardBlockSize; // Card block size
215 } SD_CardInfo_t;
217 /* Prototype(s) -----------------------------------------------------------------------------------------------------*/
219 extern SD_CardInfo_t SD_CardInfo;
220 extern SD_CardType_t SD_CardType;
222 #ifdef AT32F43x
223 // TODO:AT32 TARGES NOT USE SD CARD ANT TF CARD FOR NOW
224 void SD_Initialize_LL (dma_channel_type *dma);
225 #else
226 bool SD_Initialize_LL (DMA_Stream_TypeDef *dma);
227 #endif
228 bool SD_Init(void);
229 bool SD_IsDetected(void);
230 bool SD_GetState(void);
232 SD_Error_t SD_GetCardInfo(void);
234 SD_Error_t SD_ReadBlocks_DMA(uint64_t ReadAddress, uint32_t *buffer, uint32_t BlockSize, uint32_t NumberOfBlocks);
235 SD_Error_t SD_CheckRead(void);
236 SD_Error_t SD_WriteBlocks_DMA(uint64_t WriteAddress, uint32_t *buffer, uint32_t BlockSize, uint32_t NumberOfBlocks);
237 SD_Error_t SD_CheckWrite(void);
239 SD_Error_t SD_Erase(uint64_t StartAddress, uint64_t EndAddress);
240 SD_Error_t SD_GetCardStatus(SD_CardStatus_t* pCardStatus);
242 #endif