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/>.
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)
31 #if defined(USE_SDCARD_SDIO)
34 #include "drivers/dma.h"
40 / 1 2 3 4 5 6 7 8 | NR |SDIO INTERFACE
41 / | |NAME STM32F746 DESCRIPTION
44 | | 1 |CD/DAT3 PC11 - Connector data line 3
45 | | 2 |CMD PD2 PD2 Command/Response line
46 | | 3 |VSS1 GND GND GND
47 | SD CARD Pinout | 4 |VDD 3.3V 3.3V 3.3V Power supply
48 | | 5 |CLK PC12 PC12 Clock
49 | | 6 |VSS2 GND GND GND
50 | | 7 |DAT0 PC8 PC8 Connector data line 0
51 | | 8 |DAT1 PC9 - Connector data line 1
52 |___________________| 9 |DAT2 PC10 - Connector data line 2
56 /* Define(s) --------------------------------------------------------------------------------------------------------*/
57 #define SD_DATATIMEOUT ((uint32_t)100000000)
59 /* Structure(s) -----------------------------------------------------------------------------------------------------*/
63 // SD specific error defines
64 SD_CMD_CRC_FAIL
= (1), // Command response received (but CRC check failed)
65 SD_DATA_CRC_FAIL
= (2), // Data block sent/received (CRC check failed)
66 SD_CMD_RSP_TIMEOUT
= (3), // Command response TimeOut
67 SD_DATA_TIMEOUT
= (4), // Data TimeOut
68 SD_TX_UNDERRUN
= (5), // Transmit FIFO underrun
69 SD_RX_OVERRUN
= (6), // Receive FIFO overrun
70 SD_START_BIT_ERR
= (7), // Start bit not detected on all data signals in wide bus mode
71 SD_CMD_OUT_OF_RANGE
= (8), // Command's argument was out of range.
72 SD_ADDR_MISALIGNED
= (9), // Misaligned address
73 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
74 SD_ERASE_SEQ_ERR
= (11), // An error in the sequence of erase command occurs.
75 SD_BAD_ERASE_PARAM
= (12), // An invalid selection for erase groups
76 SD_WRITE_PROT_VIOLATION
= (13), // Attempt to program a write protect block
77 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
78 SD_COM_CRC_FAILED
= (15), // CRC check of the previous command failed
79 SD_ILLEGAL_CMD
= (16), // Command is not legal for the card state
80 SD_CARD_ECC_FAILED
= (17), // Card internal ECC was applied but failed to correct the data
81 SD_CC_ERROR
= (18), // Internal card controller error
82 SD_GENERAL_UNKNOWN_ERROR
= (19), // General or unknown error
83 SD_STREAM_READ_UNDERRUN
= (20), // The card could not sustain data transfer in stream read operation.
84 SD_STREAM_WRITE_OVERRUN
= (21), // The card could not sustain data programming in stream mode
85 SD_CID_CSD_OVERWRITE
= (22), // CID/CSD overwrite error
86 SD_WP_ERASE_SKIP
= (23), // Only partial address space was erased
87 SD_CARD_ECC_DISABLED
= (24), // Command has been executed without using internal ECC
88 SD_ERASE_RESET
= (25), // Erase sequence was cleared before executing because an out of erase sequence command was received
89 SD_AKE_SEQ_ERROR
= (26), // Error in sequence of authentication.
90 SD_INVALID_VOLTRANGE
= (27),
91 SD_ADDR_OUT_OF_RANGE
= (28),
92 SD_SWITCH_ERROR
= (29),
93 SD_SDMMC_DISABLED
= (30),
94 SD_SDMMC_FUNCTION_BUSY
= (31),
95 SD_SDMMC_FUNCTION_FAILED
= (32),
96 SD_SDMMC_UNKNOWN_FUNCTION
= (33),
97 SD_OUT_OF_BOUND
= (34),
100 // Standard error defines
101 SD_INTERNAL_ERROR
= (35),
102 SD_NOT_CONFIGURED
= (36),
103 SD_REQUEST_PENDING
= (37),
104 SD_REQUEST_NOT_APPLICABLE
= (38),
105 SD_INVALID_PARAMETER
= (39),
106 SD_UNSUPPORTED_FEATURE
= (40),
107 SD_UNSUPPORTED_HW
= (41),
115 uint8_t DAT_BUS_WIDTH
; // Shows the currently defined data bus width
116 uint8_t SECURED_MODE
; // Card is in secured mode of operation
117 uint16_t SD_CARD_TYPE
; // Carries information about card type
118 uint32_t SIZE_OF_PROTECTED_AREA
; // Carries information about the capacity of protected area
119 uint8_t SPEED_CLASS
; // Carries information about the speed class of the card
120 uint8_t PERFORMANCE_MOVE
; // Carries information about the card's performance move
121 uint8_t AU_SIZE
; // Carries information about the card's allocation unit size
122 uint16_t ERASE_SIZE
; // Determines the number of AUs to be erased in one operation
123 uint8_t ERASE_TIMEOUT
; // Determines the TimeOut for any number of AU erase
124 uint8_t ERASE_OFFSET
; // Carries information about the erase offset
129 uint8_t CSDStruct
; // CSD structure
130 uint8_t SysSpecVersion
; // System specification version
131 uint8_t Reserved1
; // Reserved
132 uint8_t TAAC
; // Data read access time 1
133 uint8_t NSAC
; // Data read access time 2 in CLK cycles
134 uint8_t MaxBusClkFrec
; // Max. bus clock frequency
135 uint16_t CardComdClasses
; // Card command classes
136 uint8_t RdBlockLen
; // Max. read data block length
137 uint8_t PartBlockRead
; // Partial blocks for read allowed
138 uint8_t WrBlockMisalign
; // Write block misalignment
139 uint8_t RdBlockMisalign
; // Read block misalignment
140 uint8_t DSRImpl
; // DSR implemented
141 uint8_t Reserved2
; // Reserved
142 uint32_t DeviceSize
; // Device Size
143 uint8_t MaxRdCurrentVDDMin
; // Max. read current @ VDD min
144 uint8_t MaxRdCurrentVDDMax
; // Max. read current @ VDD max
145 uint8_t MaxWrCurrentVDDMin
; // Max. write current @ VDD min
146 uint8_t MaxWrCurrentVDDMax
; // Max. write current @ VDD max
147 uint8_t DeviceSizeMul
; // Device size multiplier
148 uint8_t EraseGrSize
; // Erase group size
149 uint8_t EraseGrMul
; // Erase group size multiplier
150 uint8_t WrProtectGrSize
; // Write protect group size
151 uint8_t WrProtectGrEnable
; // Write protect group enable
152 uint8_t ManDeflECC
; // Manufacturer default ECC
153 uint8_t WrSpeedFact
; // Write speed factor
154 uint8_t MaxWrBlockLen
; // Max. write data block length
155 uint8_t WriteBlockPaPartial
; // Partial blocks for write allowed
156 uint8_t Reserved3
; // Reserved
157 uint8_t ContentProtectAppli
; // Content protection application
158 uint8_t FileFormatGrouop
; // File format group
159 uint8_t CopyFlag
; // Copy flag (OTP)
160 uint8_t PermWrProtect
; // Permanent write protection
161 uint8_t TempWrProtect
; // Temporary write protection
162 uint8_t FileFormat
; // File format
163 uint8_t ECC
; // ECC code
164 uint8_t CSD_CRC
; // CSD CRC
165 uint8_t Reserved4
; // Always 1
170 uint8_t ManufacturerID
; // Manufacturer ID
171 uint16_t OEM_AppliID
; // OEM/Application ID
172 uint32_t ProdName1
; // Product Name part1
173 uint8_t ProdName2
; // Product Name part2
174 uint8_t ProdRev
; // Product Revision
175 uint32_t ProdSN
; // Product Serial Number
176 uint8_t Reserved1
; // Reserved1
177 uint16_t ManufactDate
; // Manufacturing Date
178 uint8_t CID_CRC
; // CID CRC
179 uint8_t Reserved2
; // Always 1
184 SD_STD_CAPACITY_V1_1
= 0,
185 SD_STD_CAPACITY_V2_0
= 1,
186 SD_HIGH_CAPACITY
= 2,
188 SD_SECURE_DIGITAL_IO
= 4,
189 SD_HIGH_SPEED_MULTIMEDIA
= 5,
190 SD_SECURE_DIGITAL_IO_COMBO
= 6,
191 SD_HIGH_CAPACITY_MMC
= 7,
196 volatile SD_CSD_t SD_csd
; // SD card specific data register
197 volatile SD_CID_t SD_cid
; // SD card identification number register
198 uint64_t CardCapacity
; // Card capacity
199 uint32_t CardBlockSize
; // Card block size
202 /* Prototype(s) -----------------------------------------------------------------------------------------------------*/
204 extern SD_CardInfo_t SD_CardInfo
;
205 extern SD_CardType_t SD_CardType
;
208 // TODO:AT32 TARGES NOT USE SD CARD ANT TF CARD FOR NOW
209 void SD_Initialize_LL (DMA_t dma
);
211 bool SD_Initialize_LL (DMA_t dma
);
214 bool SD_IsDetected(void);
215 bool SD_GetState(void);
217 SD_Error_t
SD_GetCardInfo(void);
219 SD_Error_t
SD_ReadBlocks_DMA(uint64_t ReadAddress
, uint32_t *buffer
, uint32_t BlockSize
, uint32_t NumberOfBlocks
);
220 SD_Error_t
SD_CheckRead(void);
221 SD_Error_t
SD_WriteBlocks_DMA(uint64_t WriteAddress
, uint32_t *buffer
, uint32_t BlockSize
, uint32_t NumberOfBlocks
);
222 SD_Error_t
SD_CheckWrite(void);
224 SD_Error_t
SD_Erase(uint64_t StartAddress
, uint64_t EndAddress
);
225 SD_Error_t
SD_GetCardStatus(SD_CardStatus_t
* pCardStatus
);