Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / drivers / sdcard / sdmmc_sdio_f7xx.c
blob20fe8ca8dd0f4ca1320298eb649abe469ab15b1e
1 /*
2 * This file is part of INAV, Cleanflight and Betaflight.
4 * INAV, Cleanflight 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 BF source: Chris Hockuba (https://github.com/conkerkh)
24 * Modified for INAV: Konstantin (https://github.com/digitalentity)
27 /* Include(s) -------------------------------------------------------------------------------------------------------*/
29 #include "stdbool.h"
30 #include <string.h>
32 #include "platform.h"
34 #ifdef USE_SDCARD_SDIO
36 #include "sdmmc_sdio.h"
37 #include "stm32f7xx.h"
39 #include "drivers/io.h"
40 #include "drivers/io_impl.h"
41 #include "drivers/nvic.h"
42 #include "drivers/time.h"
43 #include "drivers/rcc.h"
44 #include "drivers/dma.h"
46 #include "build/debug.h"
49 /* Define(s) --------------------------------------------------------------------------------------------------------*/
51 //#define DMA_CHANNEL_4 ((uint32_t)0x08000000)
52 #define DMA_MEMORY_TO_PERIPH ((uint32_t)DMA_SxCR_DIR_0)
54 //#define DMA_PERIPH_TO_MEMORY ((uint32_t)0x00)
55 #define DMA_MINC_ENABLE ((uint32_t)DMA_SxCR_MINC)
56 #define DMA_MDATAALIGN_WORD ((uint32_t)DMA_SxCR_MSIZE_1)
57 #define DMA_PDATAALIGN_WORD ((uint32_t)DMA_SxCR_PSIZE_1)
58 #define DMA_PRIORITY_LOW ((uint32_t)0x00000000U)
59 #define DMA_PRIORITY_MEDIUM ((uint32_t)DMA_SxCR_PL_0)
60 #define DMA_PRIORITY_HIGH ((uint32_t)DMA_SxCR_PL_1)
61 #define DMA_PRIORITY_VERY_HIGH ((uint32_t)DMA_SxCR_PL)
62 #define DMA_MBURST_INC4 ((uint32_t)DMA_SxCR_MBURST_0)
63 #define DMA_PBURST_INC4 ((uint32_t)DMA_SxCR_PBURST_0)
65 #define BLOCK_SIZE ((uint32_t)(512))
67 #define IFCR_CLEAR_MASK_STREAM3 (DMA_LIFCR_CTCIF3 | DMA_LIFCR_CHTIF3 | DMA_LIFCR_CTEIF3 | DMA_LIFCR_CDMEIF3 | DMA_LIFCR_CFEIF3)
68 #define IFCR_CLEAR_MASK_STREAM6 (DMA_HIFCR_CTCIF6 | DMA_HIFCR_CHTIF6 | DMA_HIFCR_CTEIF6 | DMA_HIFCR_CDMEIF6 | DMA_HIFCR_CFEIF6)
70 #define SDMMC_ICR_STATIC_FLAGS ((uint32_t)(SDMMC_ICR_CCRCFAILC | SDMMC_ICR_DCRCFAILC | SDMMC_ICR_CTIMEOUTC |\
71 SDMMC_ICR_DTIMEOUTC | SDMMC_ICR_TXUNDERRC | SDMMC_ICR_RXOVERRC |\
72 SDMMC_ICR_CMDRENDC | SDMMC_ICR_CMDSENTC | SDMMC_ICR_DATAENDC |\
73 SDMMC_ICR_DBCKENDC))
75 #define SD_SOFTWARE_COMMAND_TIMEOUT ((uint32_t)0x00020000)
77 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
78 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
79 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
80 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
81 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
82 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
83 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
84 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
85 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
86 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
87 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
88 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
89 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
90 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
91 #define SD_OCR_CID_CSD_OVERWRITE ((uint32_t)0x00010000)
92 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
93 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
94 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
95 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
96 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
98 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
99 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
100 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
102 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
103 #define SD_RESP_HIGH_CAPACITY ((uint32_t)0x40000000)
104 #define SD_RESP_STD_CAPACITY ((uint32_t)0x00000000)
105 #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
107 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
108 #define SD_ALLZERO ((uint32_t)0x00000000)
110 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
111 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
112 #define SD_CARD_LOCKED ((uint32_t)0x02000000)
114 #define SD_0TO7BITS ((uint32_t)0x000000FF)
115 #define SD_8TO15BITS ((uint32_t)0x0000FF00)
116 #define SD_16TO23BITS ((uint32_t)0x00FF0000)
117 #define SD_24TO31BITS ((uint32_t)0xFF000000)
118 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
120 #define SD_CCCC_ERASE ((uint32_t)0x00000020)
122 #define SD_SDMMC_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD)
126 #define SD_BUS_WIDE_1B ((uint32_t)0x00000000)
127 #define SD_BUS_WIDE_4B SDMMC_CLKCR_WIDBUS_0
128 #define SD_BUS_WIDE_8B SDMMC_CLKCR_WIDBUS_1
130 #define SD_CMD_RESPONSE_SHORT SDMMC_CMD_WAITRESP_0
131 #define SD_CMD_RESPONSE_LONG SDMMC_CMD_WAITRESP
133 #define SD_DATABLOCK_SIZE_8B (SDMMC_DCTRL_DBLOCKSIZE_0|SDMMC_DCTRL_DBLOCKSIZE_1)
134 #define SD_DATABLOCK_SIZE_64B (SDMMC_DCTRL_DBLOCKSIZE_1|SDMMC_DCTRL_DBLOCKSIZE_2)
135 #define SD_DATABLOCK_SIZE_512B (SDMMC_DCTRL_DBLOCKSIZE_0|SDMMC_DCTRL_DBLOCKSIZE_3)
137 #define CLKCR_CLEAR_MASK ((uint32_t)(SDMMC_CLKCR_CLKDIV | SDMMC_CLKCR_PWRSAV |\
138 SDMMC_CLKCR_BYPASS | SDMMC_CLKCR_WIDBUS |\
139 SDMMC_CLKCR_NEGEDGE | SDMMC_CLKCR_HWFC_EN))
141 #define DCTRL_CLEAR_MASK ((uint32_t)(SDMMC_DCTRL_DTEN | SDMMC_DCTRL_DTDIR |\
142 SDMMC_DCTRL_DTMODE | SDMMC_DCTRL_DBLOCKSIZE))
144 #define CMD_CLEAR_MASK ((uint32_t)(SDMMC_CMD_CMDINDEX | SDMMC_CMD_WAITRESP |\
145 SDMMC_CMD_WAITINT | SDMMC_CMD_WAITPEND |\
146 SDMMC_CMD_CPSMEN | SDMMC_CMD_SDMMC1SUSPEND))
148 #define SDMMC_INIT_CLK_DIV ((uint8_t)0x76)
149 #define SDMMC_CLK_DIV ((uint8_t)0x00)
152 #define SD_CMD_GO_IDLE_STATE ((uint8_t)0) // Resets the SD memory card.
153 #define SD_CMD_SEND_OP_COND ((uint8_t)1) // Sends host capacity support information and activates the card's initialization process.
154 #define SD_CMD_ALL_SEND_CID ((uint8_t)2) // Asks any card connected to the host to send the CID numbers on the CMD line.
155 #define SD_CMD_SET_REL_ADDR ((uint8_t)3) // Asks the card to publish a new relative address (RCA).
156 #define SD_CMD_HS_SWITCH ((uint8_t)6) // Checks switchable function (mode 0) and switch card function (mode 1).
157 #define SD_CMD_SEL_DESEL_CARD ((uint8_t)7) // Selects the card by its own relative address and gets deselected by any other address
158 #define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8) // Sends SD Memory Card interface condition, which includes host supply voltage information
159 // and asks the card whether card supports voltage.
160 #define SD_CMD_SEND_CSD ((uint8_t)9) // Addressed card sends its card specific data (CSD) on the CMD line.
161 #define SD_CMD_SEND_CID ((uint8_t)10) // Addressed card sends its card identification (CID) on the CMD line.
162 #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12) // Forces the card to stop transmission.
163 #define SD_CMD_SEND_STATUS ((uint8_t)13) // Addressed card sends its status register.
164 #define SD_CMD_SET_BLOCKLEN ((uint8_t)16) // Sets the block length (in bytes for SDSC) for all following block commands
165 // (read, write, lock). Default block length is fixed to 512 Bytes. Not effective
166 // for SDHS and SDXC.
167 #define SD_CMD_READ_SINGLE_BLOCK ((uint8_t)17) // Reads single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of
168 // fixed 512 bytes in case of SDHC and SDXC.
169 #define SD_CMD_READ_MULT_BLOCK ((uint8_t)18) // Continuously transfers data blocks from card to host until interrupted by
170 // STOP_TRANSMISSION command.
171 #define SD_CMD_WRITE_SINGLE_BLOCK ((uint8_t)24) // Writes single block of size selected by SET_BLOCKLEN in case of SDSC, and a block of
172 // fixed 512 bytes in case of SDHC and SDXC.
173 #define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25) // Continuously writes blocks of data until a STOP_TRANSMISSION follows.
174 #define SD_CMD_SD_ERASE_GRP_START ((uint8_t)32) // Sets the address of the first write block to be erased. (For SD card only).
175 #define SD_CMD_SD_ERASE_GRP_END ((uint8_t)33) // Sets the address of the last write block of the continuous range to be erased.
176 // system set by switch function command (CMD6).
177 #define SD_CMD_ERASE ((uint8_t)38) // Reserved for SD security applications.
178 #define SD_CMD_FAST_IO ((uint8_t)39) // SD card doesn't support it (Reserved).
179 #define SD_CMD_APP_CMD ((uint8_t)55) // Indicates to the card that the next command is an application specific command rather
180 // than a standard command.
182 /* Following commands are SD Card Specific commands.
183 SDMMC_APP_CMD should be sent before sending these commands. */
184 #define SD_CMD_APP_SD_SET_BUSWIDTH ((uint8_t)6) // (ACMD6) Defines the data bus width to be used for data transfer. The allowed data bus
185 // widths are given in SCR register.
186 #define SD_CMD_SD_APP_STATUS ((uint8_t)13) // (ACMD13) Sends the SD status.
187 #define SD_CMD_SD_APP_OP_COND ((uint8_t)41) // (ACMD41) Sends host capacity support information (HCS) and asks the accessed card to
188 // send its operating condition register (OCR) content in the response on the CMD line.
189 #define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51) // Reads the SD Configuration Register (SCR).
191 #define SDMMC_DIR_TX 1
192 #define SDMMC_DIR_RX 0
194 #define SDMMC_DMA_ST3 1
197 /* Typedef(s) -------------------------------------------------------------------------------------------------------*/
199 typedef enum
201 SD_SINGLE_BLOCK = 0, // Single block operation
202 SD_MULTIPLE_BLOCK = 1, // Multiple blocks operation
203 } SD_Operation_t;
205 typedef struct
207 uint32_t CSD[4]; // SD card specific data table
208 uint32_t CID[4]; // SD card identification number table
209 volatile uint32_t TransferComplete; // SD transfer complete flag in non blocking mode
210 volatile uint32_t TransferError; // SD transfer error flag in non blocking mode
211 volatile uint32_t RXCplt; // SD RX Complete is equal 0 when no transfer
212 volatile uint32_t TXCplt; // SD TX Complete is equal 0 when no transfer
213 volatile uint32_t Operation; // SD transfer operation (read/write)
214 } SD_Handle_t;
216 typedef enum
218 SD_CARD_READY = ((uint32_t)0x00000001), // Card state is ready
219 SD_CARD_IDENTIFICATION = ((uint32_t)0x00000002), // Card is in identification state
220 SD_CARD_STANDBY = ((uint32_t)0x00000003), // Card is in standby state
221 SD_CARD_TRANSFER = ((uint32_t)0x00000004), // Card is in transfer state
222 SD_CARD_SENDING = ((uint32_t)0x00000005), // Card is sending an operation
223 SD_CARD_RECEIVING = ((uint32_t)0x00000006), // Card is receiving operation information
224 SD_CARD_PROGRAMMING = ((uint32_t)0x00000007), // Card is in programming state
225 SD_CARD_DISCONNECTED = ((uint32_t)0x00000008), // Card is disconnected
226 SD_CARD_ERROR = ((uint32_t)0x000000FF) // Card is in error state
227 } SD_CardState_t;
229 /* Variable(s) ------------------------------------------------------------------------------------------------------*/
231 static SD_Handle_t SD_Handle;
232 SD_CardInfo_t SD_CardInfo;
233 static uint32_t SD_Status;
234 static uint32_t SD_CardRCA;
235 SD_CardType_t SD_CardType;
236 static volatile uint32_t TimeOut;
237 DMA_Stream_TypeDef *dma_stream;
239 /* Private function(s) ----------------------------------------------------------------------------------------------*/
241 static void SD_DataTransferInit (uint32_t Size, uint32_t DataBlockSize, bool IsItReadFromCard);
242 static SD_Error_t SD_TransmitCommand (uint32_t Command, uint32_t Argument, int8_t ResponseType);
243 static SD_Error_t SD_CmdResponse (uint8_t SD_CMD, int8_t ResponseType);
244 static void SD_GetResponse (uint32_t* pResponse);
245 static SD_Error_t CheckOCR_Response (uint32_t Response_R1);
246 static void SD_DMA_Complete (DMA_Stream_TypeDef* pDMA_Stream);
247 static SD_Error_t SD_InitializeCard (void);
249 static SD_Error_t SD_PowerON (void);
250 static SD_Error_t SD_WideBusOperationConfig (uint32_t WideMode);
251 static SD_Error_t SD_FindSCR (uint32_t *pSCR);
253 void SDMMC_DMA_ST3_IRQHandler(dmaChannelDescriptor_t *dma);
254 void SDMMC_DMA_ST6_IRQHandler(dmaChannelDescriptor_t *dma);
256 /** -----------------------------------------------------------------------------------------------------------------*/
257 /** DataTransferInit
259 * @brief Prepare the state machine for transfer
260 * @param SD_TransferType_e TransfertDir
261 * @param SD_CARD_BlockSize_e Size
263 static void SD_DataTransferInit(uint32_t Size, uint32_t DataBlockSize, bool IsItReadFromCard)
265 uint32_t Direction;
267 SDMMC1->DTIMER = SD_DATATIMEOUT; // Set the SDMMC1 Data TimeOut value
268 SDMMC1->DLEN = Size; // Set the SDMMC1 DataLength value
269 Direction = (IsItReadFromCard == true) ? SDMMC_DCTRL_DTDIR : 0;
270 SDMMC1->DCTRL |= (uint32_t)(DataBlockSize | Direction | SDMMC_DCTRL_DTEN | 0x01);
271 return;
275 /** -----------------------------------------------------------------------------------------------------------------*/
276 /** SD_TransmitCommand
278 * @brief Send the commande to SDMMC1
279 * @param uint32_t Command
280 * @param uint32_t Argument Must provide the response size
281 * @param uint8_t ResponseType
282 * @retval SD Card error state
284 static SD_Error_t SD_TransmitCommand(uint32_t Command, uint32_t Argument, int8_t ResponseType)
286 SD_Error_t ErrorState;
288 WRITE_REG(SDMMC1->ICR, SDMMC_ICR_STATIC_FLAGS); // Clear the Command Flags
289 WRITE_REG(SDMMC1->ARG, (uint32_t)Argument); // Set the SDMMC1 Argument value
290 WRITE_REG(SDMMC1->CMD, (uint32_t)(Command | SDMMC_CMD_CPSMEN)); // Set SDMMC1 command parameters
292 if ((Argument == 0) && (ResponseType == 0)) {
293 ResponseType = -1; // Go idle command
296 ErrorState = SD_CmdResponse(Command & SDMMC_CMD_CMDINDEX, ResponseType);
297 WRITE_REG(SDMMC1->ICR, SDMMC_ICR_STATIC_FLAGS); // Clear the Command Flags
299 return ErrorState;
303 /** -----------------------------------------------------------------------------------------------------------------*/
305 * @brief Checks for error conditions for any response.
306 * - R2 (CID or CSD) response.
307 * - R3 (OCR) response.
309 * @param SD_CMD: The sent command Index
310 * @retval SD Card error state
312 static SD_Error_t SD_CmdResponse(uint8_t SD_CMD, int8_t ResponseType)
314 uint32_t Response_R1;
315 uint32_t TimeOut;
316 uint32_t Flag;
318 if (ResponseType == -1) {
319 Flag = SDMMC_STA_CMDSENT;
321 else {
322 Flag = SDMMC_STA_CCRCFAIL | SDMMC_STA_CMDREND | SDMMC_STA_CTIMEOUT;
325 TimeOut = SD_SOFTWARE_COMMAND_TIMEOUT;
326 do {
327 SD_Status = SDMMC1->STA;
328 TimeOut--;
329 } while (((SD_Status & Flag) == 0) && (TimeOut > 0));
331 if (ResponseType <= 0) {
332 if (TimeOut == 0){
333 return SD_CMD_RSP_TIMEOUT;
335 else {
336 return SD_OK;
340 if ((SDMMC1->STA & SDMMC_STA_CTIMEOUT) != 0) {
341 return SD_CMD_RSP_TIMEOUT;
343 if (ResponseType == 3) {
344 if (TimeOut == 0) {
345 return SD_CMD_RSP_TIMEOUT; // Card is not V2.0 compliant or card does not support the set voltage range
347 else {
348 return SD_OK; // Card is SD V2.0 compliant
352 if ((SDMMC1->STA & SDMMC_STA_CCRCFAIL) != 0) {
353 return SD_CMD_CRC_FAIL;
355 if (ResponseType == 2) {
356 return SD_OK;
358 if ((uint8_t)SDMMC1->RESPCMD != SD_CMD) {
359 return SD_ILLEGAL_CMD; // Check if response is of desired command
362 Response_R1 = SDMMC1->RESP1; // We have received response, retrieve it for analysis
364 if (ResponseType == 1) {
365 return CheckOCR_Response(Response_R1);
367 else if (ResponseType == 6) {
368 if ((Response_R1 & (SD_R6_GENERAL_UNKNOWN_ERROR | SD_R6_ILLEGAL_CMD | SD_R6_COM_CRC_FAILED)) == SD_ALLZERO) {
369 SD_CardRCA = Response_R1;
371 if ((Response_R1 & SD_R6_GENERAL_UNKNOWN_ERROR) == SD_R6_GENERAL_UNKNOWN_ERROR) {
372 return SD_GENERAL_UNKNOWN_ERROR;
374 if ((Response_R1 & SD_R6_ILLEGAL_CMD) == SD_R6_ILLEGAL_CMD) {
375 return SD_ILLEGAL_CMD;
377 if ((Response_R1 & SD_R6_COM_CRC_FAILED) == SD_R6_COM_CRC_FAILED) {
378 return SD_COM_CRC_FAILED;
382 return SD_OK;
386 /** -----------------------------------------------------------------------------------------------------------------*/
388 * @brief Analyze the OCR response and return the appropriate error code
389 * @param Response_R1: OCR Response code
390 * @retval SD Card error state
392 static SD_Error_t CheckOCR_Response(uint32_t Response_R1)
394 if ((Response_R1 & SD_OCR_ERRORBITS) == SD_ALLZERO) return SD_OK;
395 if ((Response_R1 & SD_OCR_ADDR_OUT_OF_RANGE) == SD_OCR_ADDR_OUT_OF_RANGE) return SD_ADDR_OUT_OF_RANGE;
396 if ((Response_R1 & SD_OCR_ADDR_MISALIGNED) == SD_OCR_ADDR_MISALIGNED) return SD_ADDR_MISALIGNED;
397 if ((Response_R1 & SD_OCR_BLOCK_LEN_ERR) == SD_OCR_BLOCK_LEN_ERR) return SD_BLOCK_LEN_ERR;
398 if ((Response_R1 & SD_OCR_ERASE_SEQ_ERR) == SD_OCR_ERASE_SEQ_ERR) return SD_ERASE_SEQ_ERR;
399 if ((Response_R1 & SD_OCR_BAD_ERASE_PARAM) == SD_OCR_BAD_ERASE_PARAM) return SD_BAD_ERASE_PARAM;
400 if ((Response_R1 & SD_OCR_WRITE_PROT_VIOLATION) == SD_OCR_WRITE_PROT_VIOLATION) return SD_WRITE_PROT_VIOLATION;
401 if ((Response_R1 & SD_OCR_LOCK_UNLOCK_FAILED) == SD_OCR_LOCK_UNLOCK_FAILED) return SD_LOCK_UNLOCK_FAILED;
402 if ((Response_R1 & SD_OCR_COM_CRC_FAILED) == SD_OCR_COM_CRC_FAILED) return SD_COM_CRC_FAILED;
403 if ((Response_R1 & SD_OCR_ILLEGAL_CMD) == SD_OCR_ILLEGAL_CMD) return SD_ILLEGAL_CMD;
404 if ((Response_R1 & SD_OCR_CARD_ECC_FAILED) == SD_OCR_CARD_ECC_FAILED) return SD_CARD_ECC_FAILED;
405 if ((Response_R1 & SD_OCR_CC_ERROR) == SD_OCR_CC_ERROR) return SD_CC_ERROR;
406 if ((Response_R1 & SD_OCR_GENERAL_UNKNOWN_ERROR) == SD_OCR_GENERAL_UNKNOWN_ERROR)return SD_GENERAL_UNKNOWN_ERROR;
407 if ((Response_R1 & SD_OCR_STREAM_READ_UNDERRUN) == SD_OCR_STREAM_READ_UNDERRUN) return SD_STREAM_READ_UNDERRUN;
408 if ((Response_R1 & SD_OCR_STREAM_WRITE_OVERRUN) == SD_OCR_STREAM_WRITE_OVERRUN) return SD_STREAM_WRITE_OVERRUN;
409 if ((Response_R1 & SD_OCR_CID_CSD_OVERWRITE) == SD_OCR_CID_CSD_OVERWRITE) return SD_CID_CSD_OVERWRITE;
410 if ((Response_R1 & SD_OCR_WP_ERASE_SKIP) == SD_OCR_WP_ERASE_SKIP) return SD_WP_ERASE_SKIP;
411 if ((Response_R1 & SD_OCR_CARD_ECC_DISABLED) == SD_OCR_CARD_ECC_DISABLED) return SD_CARD_ECC_DISABLED;
412 if ((Response_R1 & SD_OCR_ERASE_RESET) == SD_OCR_ERASE_RESET) return SD_ERASE_RESET;
413 if ((Response_R1 & SD_OCR_AKE_SEQ_ERROR) == SD_OCR_AKE_SEQ_ERROR) return SD_AKE_SEQ_ERROR;
415 return SD_OK;
419 /** -----------------------------------------------------------------------------------------------------------------*/
420 /** GetResponse
422 * @brief Get response from SD device
423 * @param uint32_t* pResponse
425 static void SD_GetResponse(uint32_t* pResponse)
427 pResponse[0] = SDMMC1->RESP1;
428 pResponse[1] = SDMMC1->RESP2;
429 pResponse[2] = SDMMC1->RESP3;
430 pResponse[3] = SDMMC1->RESP4;
434 /** -----------------------------------------------------------------------------------------------------------------*/
436 * @brief SD DMA transfer complete RX and TX callback.
437 * @param DMA_Stream_TypeDef* pDMA_Stream
439 static void SD_DMA_Complete(DMA_Stream_TypeDef* pDMA_Stream)
441 if (SD_Handle.RXCplt) {
442 if (SD_Handle.Operation == ((SDMMC_DIR_RX << 1) | SD_MULTIPLE_BLOCK)) {
443 /* Send stop command in multiblock write */
444 SD_TransmitCommand((SD_CMD_STOP_TRANSMISSION | SD_CMD_RESPONSE_SHORT), 0, 1);
447 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
448 in the SD DCTRL register */
449 SDMMC1->DCTRL &= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN);
451 /* Clear all the static flags */
452 SDMMC1->ICR = SDMMC_ICR_STATIC_FLAGS;
454 /* Clear flag */
455 SD_Handle.RXCplt = 0;
457 /* Disable the stream */
458 pDMA_Stream->CR &= ~DMA_SxCR_EN;
460 else {
461 /* Enable Dataend IE */
462 SDMMC1->MASK |= SDMMC_MASK_DATAENDIE;
467 /** -----------------------------------------------------------------------------------------------------------------*/
469 * @brief Initializes all cards or single card as the case may be Card(s) come
470 * into standby state.
471 * @retval SD Card error state
473 static SD_Error_t SD_InitializeCard(void)
475 SD_Error_t ErrorState = SD_OK;
477 if ((SDMMC1->POWER & SDMMC_POWER_PWRCTRL) != 0) { // Power off
478 if (SD_CardType != SD_SECURE_DIGITAL_IO) {
479 // Send CMD2 ALL_SEND_CID
480 if ((ErrorState = SD_TransmitCommand((SD_CMD_ALL_SEND_CID | SD_CMD_RESPONSE_LONG), 0, 2)) != SD_OK) {
481 return ErrorState;
484 // Get Card identification number data
485 SD_GetResponse(SD_Handle.CID);
488 if ((SD_CardType == SD_STD_CAPACITY_V1_1) || (SD_CardType == SD_STD_CAPACITY_V2_0) ||
489 (SD_CardType == SD_SECURE_DIGITAL_IO_COMBO) || (SD_CardType == SD_HIGH_CAPACITY)) {
490 // Send CMD3 SET_REL_ADDR with argument 0
491 // SD Card publishes its RCA.
492 if ((ErrorState = SD_TransmitCommand((SD_CMD_SET_REL_ADDR | SD_CMD_RESPONSE_SHORT), 0, 6)) != SD_OK) {
493 return ErrorState;
497 if (SD_CardType != SD_SECURE_DIGITAL_IO) {
498 // Send CMD9 SEND_CSD with argument as card's RCA
499 if ((ErrorState = SD_TransmitCommand((SD_CMD_SEND_CSD | SD_CMD_RESPONSE_LONG), SD_CardRCA, 2)) == SD_OK) {
500 // Get Card Specific Data
501 SD_GetResponse(SD_Handle.CSD);
505 else {
506 ErrorState = SD_REQUEST_NOT_APPLICABLE;
509 return ErrorState;
513 /** -----------------------------------------------------------------------------------------------------------------*/
515 * @brief Prepre the DMA transfer
516 * @param pDMA: DMA Stream to use for the DMA operation
517 * @param pBuffer: Pointer to the buffer that will contain the data to transmit
518 * @param BlockSize: The SD card Data block size
519 * @note BlockSize must be 512 bytes.
520 * @param NumberOfBlocks: Number of blocks to write
521 * @retval SD Card error state
523 static void SD_StartBlockTransfert(uint32_t* pBuffer, uint32_t BlockSize, uint32_t NumberOfBlocks, uint8_t dir)
525 DMA_Stream_TypeDef *pDMA = dma_stream;
527 SDMMC1->DCTRL = 0; // Initialize data control register
528 SD_Handle.TransferComplete = 0; // Initialize handle flags
529 SD_Handle.TransferError = SD_OK;
530 SD_Handle.Operation = (NumberOfBlocks > 1) ? SD_MULTIPLE_BLOCK : SD_SINGLE_BLOCK; // Initialize SD Read operation
531 SD_Handle.Operation |= dir << 1;
532 SDMMC1->MASK = 0;
534 if (dir == SDMMC_DIR_RX) {
535 SDMMC1->MASK |= (SDMMC_MASK_DCRCFAILIE | SDMMC_MASK_DTIMEOUTIE | // Enable transfer interrupts
536 SDMMC_MASK_DATAENDIE | SDMMC_MASK_RXOVERRIE);
538 else {
539 SDMMC1->MASK |= (SDMMC_MASK_DCRCFAILIE | SDMMC_MASK_DTIMEOUTIE | // Enable transfer interrupts
540 SDMMC_MASK_TXUNDERRIE);
543 if (dir == SDMMC_DIR_TX) {
544 SDMMC1->DCTRL |= SDMMC_DCTRL_DMAEN; // Enable SDMMC1 DMA transfer
547 pDMA->CR &= ~DMA_SxCR_EN; // Disable the Peripheral
548 while (pDMA->CR & DMA_SxCR_EN);
550 pDMA->NDTR = (uint32_t) (BlockSize * NumberOfBlocks) / 4; // Configure DMA Stream data length
551 pDMA->M0AR = (uint32_t) pBuffer; // Configure DMA Stream memory address
553 if (dir == SDMMC_DIR_RX) {
554 pDMA->CR &= ~(0x01U << 6U); // Sets peripheral to memory
556 else {
557 pDMA->CR |= DMA_MEMORY_TO_PERIPH; // Sets memory to peripheral
560 // Clear the transfer error flag
561 if (dma_stream == DMA2_Stream3) {
562 DMA2->LIFCR = DMA_LIFCR_CTEIF3 | DMA_LIFCR_CDMEIF3 | DMA_LIFCR_CFEIF3 | DMA_LIFCR_CHTIF3 | DMA_LIFCR_CTCIF3;
564 else {
565 DMA2->HIFCR = DMA_HIFCR_CTEIF6 | DMA_HIFCR_CDMEIF6 | DMA_HIFCR_CFEIF6 | DMA_HIFCR_CHTIF6 | DMA_HIFCR_CTCIF6;
568 pDMA->CR |= DMA_SxCR_TCIE | DMA_SxCR_HTIE | DMA_SxCR_TEIE | DMA_SxCR_DMEIE; // Enable all interrupts
569 pDMA->FCR |= DMA_SxFCR_FEIE;
570 pDMA->CR |= DMA_SxCR_EN; // Enable the Peripheral
572 if (dir == SDMMC_DIR_RX) {
573 SDMMC1->DCTRL |= SDMMC_DCTRL_DMAEN; // Enable SDMMC1 DMA transfer
578 /** -----------------------------------------------------------------------------------------------------------------*/
580 * @brief Reads block(s) from a specified address in a card. The Data transfer
581 * is managed by DMA mode.
582 * @note This API should be followed by the function SD_CheckOperation()
583 * to check the completion of the read process
584 * @param pReadBuffer: Pointer to the buffer that will contain the received data
585 * @param ReadAddr: Address from where data is to be read
586 * @param BlockSize: SD card Data block size
587 * @note BlockSize must be 512 bytes.
588 * @param NumberOfBlocks: Number of blocks to read.
589 * @retval SD Card error state
591 SD_Error_t SD_ReadBlocks_DMA(uint64_t ReadAddress, uint32_t *buffer, uint32_t BlockSize, uint32_t NumberOfBlocks)
593 SD_Error_t ErrorState;
594 uint32_t CmdIndex;
595 SD_Handle.RXCplt = 1;
597 //printf("Reading at %ld into %p %ld blocks\n", (uint32_t)ReadAddress, (void*)buffer, NumberOfBlocks);
599 if (SD_CardType != SD_HIGH_CAPACITY) {
600 ReadAddress *= 512;
603 SD_StartBlockTransfert(buffer, BlockSize, NumberOfBlocks, SDMMC_DIR_RX);
605 // Configure the SD DPSM (Data Path State Machine)
606 SD_DataTransferInit(BlockSize * NumberOfBlocks, SD_DATABLOCK_SIZE_512B, true);
608 // Set Block Size for Card
609 ErrorState = SD_TransmitCommand((SD_CMD_SET_BLOCKLEN | SD_CMD_RESPONSE_SHORT), BlockSize, 1);
611 // Send CMD18 READ_MULT_BLOCK with argument data address
612 // or send CMD17 READ_SINGLE_BLOCK depending on number of block
613 uint8_t retries = 10;
614 CmdIndex = (NumberOfBlocks > 1) ? SD_CMD_READ_MULT_BLOCK : SD_CMD_READ_SINGLE_BLOCK;
616 do {
617 ErrorState = SD_TransmitCommand((CmdIndex | SD_CMD_RESPONSE_SHORT), (uint32_t)ReadAddress, 1);
618 if (ErrorState != SD_OK && retries--) {
619 ErrorState = SD_TransmitCommand((SD_CMD_APP_CMD | SD_CMD_RESPONSE_SHORT), 0, 1);
621 } while (ErrorState != SD_OK && retries);
623 if (ErrorState != SD_OK) {
624 SD_Handle.RXCplt = 0;
627 // Update the SD transfer error in SD handle
628 SD_Handle.TransferError = ErrorState;
630 return ErrorState;
634 /** -----------------------------------------------------------------------------------------------------------------*/
636 * @brief Writes block(s) to a specified address in a card. The Data transfer
637 * is managed by DMA mode.
638 * @note This API should be followed by the function SD_CheckOperation()
639 * to check the completion of the write process (by SD current status polling).
640 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
641 * @param WriteAddress: Address from where data is to be read
642 * @param BlockSize: the SD card Data block size
643 * @note BlockSize must be 512 bytes.
644 * @param NumberOfBlocks: Number of blocks to write
645 * @retval SD Card error state
647 SD_Error_t SD_WriteBlocks_DMA(uint64_t WriteAddress, uint32_t *buffer, uint32_t BlockSize, uint32_t NumberOfBlocks)
649 SD_Error_t ErrorState;
650 uint32_t CmdIndex;
651 SD_Handle.TXCplt = 1;
653 if (SD_CardType != SD_HIGH_CAPACITY) {
654 WriteAddress *= 512;
657 // Check number of blocks command
658 // Send CMD24 WRITE_SINGLE_BLOCK
659 // Send CMD25 WRITE_MULT_BLOCK with argument data address
660 CmdIndex = (NumberOfBlocks > 1) ? SD_CMD_WRITE_MULT_BLOCK : SD_CMD_WRITE_SINGLE_BLOCK;
662 // Set Block Size for Card
663 uint8_t retries = 10;
664 do {
665 ErrorState = SD_TransmitCommand((CmdIndex | SD_CMD_RESPONSE_SHORT), (uint32_t)WriteAddress, 1);
666 if (ErrorState != SD_OK && retries--) {
667 ErrorState = SD_TransmitCommand((SD_CMD_APP_CMD | SD_CMD_RESPONSE_SHORT), 0, 1);
669 } while (ErrorState != SD_OK && retries);
671 if (ErrorState != SD_OK) {
672 SD_Handle.TXCplt = 0;
673 return ErrorState;
676 SD_StartBlockTransfert(buffer, BlockSize, NumberOfBlocks, SDMMC_DIR_TX);
678 // Configure the SD DPSM (Data Path State Machine)
679 SD_DataTransferInit(BlockSize * NumberOfBlocks, SD_DATABLOCK_SIZE_512B, false);
681 SD_Handle.TransferError = ErrorState;
683 return ErrorState;
686 SD_Error_t SD_CheckWrite(void)
688 if (SD_Handle.TXCplt != 0) return SD_BUSY;
689 return SD_OK;
692 SD_Error_t SD_CheckRead(void)
694 if (SD_Handle.RXCplt != 0) return SD_BUSY;
695 return SD_OK;
698 /** -----------------------------------------------------------------------------------------------------------------*/
700 * @brief Returns information about specific card.
701 * contains all SD cardinformation
702 * @retval SD Card error state
704 SD_Error_t SD_GetCardInfo(void)
706 SD_Error_t ErrorState = SD_OK;
707 uint32_t Temp = 0;
709 // Byte 0
710 Temp = (SD_Handle.CSD[0] & 0xFF000000) >> 24;
711 SD_CardInfo.SD_csd.CSDStruct = (uint8_t)((Temp & 0xC0) >> 6);
712 SD_CardInfo.SD_csd.SysSpecVersion = (uint8_t)((Temp & 0x3C) >> 2);
713 SD_CardInfo.SD_csd.Reserved1 = Temp & 0x03;
715 // Byte 1
716 Temp = (SD_Handle.CSD[0] & 0x00FF0000) >> 16;
717 SD_CardInfo.SD_csd.TAAC = (uint8_t)Temp;
719 // Byte 2
720 Temp = (SD_Handle.CSD[0] & 0x0000FF00) >> 8;
721 SD_CardInfo.SD_csd.NSAC = (uint8_t)Temp;
723 // Byte 3
724 Temp = SD_Handle.CSD[0] & 0x000000FF;
725 SD_CardInfo.SD_csd.MaxBusClkFrec = (uint8_t)Temp;
727 // Byte 4
728 Temp = (SD_Handle.CSD[1] & 0xFF000000) >> 24;
729 SD_CardInfo.SD_csd.CardComdClasses = (uint16_t)(Temp << 4);
731 // Byte 5
732 Temp = (SD_Handle.CSD[1] & 0x00FF0000) >> 16;
733 SD_CardInfo.SD_csd.CardComdClasses |= (uint16_t)((Temp & 0xF0) >> 4);
734 SD_CardInfo.SD_csd.RdBlockLen = (uint8_t)(Temp & 0x0F);
736 // Byte 6
737 Temp = (SD_Handle.CSD[1] & 0x0000FF00) >> 8;
738 SD_CardInfo.SD_csd.PartBlockRead = (uint8_t)((Temp & 0x80) >> 7);
739 SD_CardInfo.SD_csd.WrBlockMisalign = (uint8_t)((Temp & 0x40) >> 6);
740 SD_CardInfo.SD_csd.RdBlockMisalign = (uint8_t)((Temp & 0x20) >> 5);
741 SD_CardInfo.SD_csd.DSRImpl = (uint8_t)((Temp & 0x10) >> 4);
742 SD_CardInfo.SD_csd.Reserved2 = 0; /*!< Reserved */
744 if ((SD_CardType == SD_STD_CAPACITY_V1_1) || (SD_CardType == SD_STD_CAPACITY_V2_0)) {
745 SD_CardInfo.SD_csd.DeviceSize = (Temp & 0x03) << 10;
747 // Byte 7
748 Temp = (uint8_t)(SD_Handle.CSD[1] & 0x000000FF);
749 SD_CardInfo.SD_csd.DeviceSize |= (Temp) << 2;
751 // Byte 8
752 Temp = (uint8_t)((SD_Handle.CSD[2] & 0xFF000000) >> 24);
753 SD_CardInfo.SD_csd.DeviceSize |= (Temp & 0xC0) >> 6;
755 SD_CardInfo.SD_csd.MaxRdCurrentVDDMin = (Temp & 0x38) >> 3;
756 SD_CardInfo.SD_csd.MaxRdCurrentVDDMax = (Temp & 0x07);
758 // Byte 9
759 Temp = (uint8_t)((SD_Handle.CSD[2] & 0x00FF0000) >> 16);
760 SD_CardInfo.SD_csd.MaxWrCurrentVDDMin = (Temp & 0xE0) >> 5;
761 SD_CardInfo.SD_csd.MaxWrCurrentVDDMax = (Temp & 0x1C) >> 2;
762 SD_CardInfo.SD_csd.DeviceSizeMul = (Temp & 0x03) << 1;
764 // Byte 10
765 Temp = (uint8_t)((SD_Handle.CSD[2] & 0x0000FF00) >> 8);
766 SD_CardInfo.SD_csd.DeviceSizeMul |= (Temp & 0x80) >> 7;
768 SD_CardInfo.CardCapacity = (SD_CardInfo.SD_csd.DeviceSize + 1) ;
769 SD_CardInfo.CardCapacity *= (1 << (SD_CardInfo.SD_csd.DeviceSizeMul + 2));
770 SD_CardInfo.CardBlockSize = 1 << (SD_CardInfo.SD_csd.RdBlockLen);
771 SD_CardInfo.CardCapacity *= SD_CardInfo.CardBlockSize;
773 else if (SD_CardType == SD_HIGH_CAPACITY) {
774 // Byte 7
775 Temp = (uint8_t)(SD_Handle.CSD[1] & 0x000000FF);
776 SD_CardInfo.SD_csd.DeviceSize = (Temp & 0x3F) << 16;
778 // Byte 8
779 Temp = (uint8_t)((SD_Handle.CSD[2] & 0xFF000000) >> 24);
781 SD_CardInfo.SD_csd.DeviceSize |= (Temp << 8);
783 // Byte 9
784 Temp = (uint8_t)((SD_Handle.CSD[2] & 0x00FF0000) >> 16);
786 SD_CardInfo.SD_csd.DeviceSize |= (Temp);
788 // Byte 10
789 Temp = (uint8_t)((SD_Handle.CSD[2] & 0x0000FF00) >> 8);
791 SD_CardInfo.CardCapacity = ((uint64_t)SD_CardInfo.SD_csd.DeviceSize + 1) * 1024;
792 SD_CardInfo.CardBlockSize = 512;
794 else {
795 // Not supported card type
796 ErrorState = SD_ERROR;
799 SD_CardInfo.SD_csd.EraseGrSize = (Temp & 0x40) >> 6;
800 SD_CardInfo.SD_csd.EraseGrMul = (Temp & 0x3F) << 1;
802 // Byte 11
803 Temp = (uint8_t)(SD_Handle.CSD[2] & 0x000000FF);
804 SD_CardInfo.SD_csd.EraseGrMul |= (Temp & 0x80) >> 7;
805 SD_CardInfo.SD_csd.WrProtectGrSize = (Temp & 0x7F);
807 // Byte 12
808 Temp = (uint8_t)((SD_Handle.CSD[3] & 0xFF000000) >> 24);
809 SD_CardInfo.SD_csd.WrProtectGrEnable = (Temp & 0x80) >> 7;
810 SD_CardInfo.SD_csd.ManDeflECC = (Temp & 0x60) >> 5;
811 SD_CardInfo.SD_csd.WrSpeedFact = (Temp & 0x1C) >> 2;
812 SD_CardInfo.SD_csd.MaxWrBlockLen = (Temp & 0x03) << 2;
814 // Byte 13
815 Temp = (uint8_t)((SD_Handle.CSD[3] & 0x00FF0000) >> 16);
816 SD_CardInfo.SD_csd.MaxWrBlockLen |= (Temp & 0xC0) >> 6;
817 SD_CardInfo.SD_csd.WriteBlockPaPartial = (Temp & 0x20) >> 5;
818 SD_CardInfo.SD_csd.Reserved3 = 0;
819 SD_CardInfo.SD_csd.ContentProtectAppli = (Temp & 0x01);
821 // Byte 14
822 Temp = (uint8_t)((SD_Handle.CSD[3] & 0x0000FF00) >> 8);
823 SD_CardInfo.SD_csd.FileFormatGrouop = (Temp & 0x80) >> 7;
824 SD_CardInfo.SD_csd.CopyFlag = (Temp & 0x40) >> 6;
825 SD_CardInfo.SD_csd.PermWrProtect = (Temp & 0x20) >> 5;
826 SD_CardInfo.SD_csd.TempWrProtect = (Temp & 0x10) >> 4;
827 SD_CardInfo.SD_csd.FileFormat = (Temp & 0x0C) >> 2;
828 SD_CardInfo.SD_csd.ECC = (Temp & 0x03);
830 // Byte 15
831 Temp = (uint8_t)(SD_Handle.CSD[3] & 0x000000FF);
832 SD_CardInfo.SD_csd.CSD_CRC = (Temp & 0xFE) >> 1;
833 SD_CardInfo.SD_csd.Reserved4 = 1;
835 // Byte 0
836 Temp = (uint8_t)((SD_Handle.CID[0] & 0xFF000000) >> 24);
837 SD_CardInfo.SD_cid.ManufacturerID = Temp;
839 // Byte 1
840 Temp = (uint8_t)((SD_Handle.CID[0] & 0x00FF0000) >> 16);
841 SD_CardInfo.SD_cid.OEM_AppliID = Temp << 8;
843 // Byte 2
844 Temp = (uint8_t)((SD_Handle.CID[0] & 0x000000FF00) >> 8);
845 SD_CardInfo.SD_cid.OEM_AppliID |= Temp;
847 // Byte 3
848 Temp = (uint8_t)(SD_Handle.CID[0] & 0x000000FF);
849 SD_CardInfo.SD_cid.ProdName1 = Temp << 24;
851 // Byte 4
852 Temp = (uint8_t)((SD_Handle.CID[1] & 0xFF000000) >> 24);
853 SD_CardInfo.SD_cid.ProdName1 |= Temp << 16;
855 // Byte 5
856 Temp = (uint8_t)((SD_Handle.CID[1] & 0x00FF0000) >> 16);
857 SD_CardInfo.SD_cid.ProdName1 |= Temp << 8;
859 // Byte 6
860 Temp = (uint8_t)((SD_Handle.CID[1] & 0x0000FF00) >> 8);
861 SD_CardInfo.SD_cid.ProdName1 |= Temp;
863 // Byte 7
864 Temp = (uint8_t)(SD_Handle.CID[1] & 0x000000FF);
865 SD_CardInfo.SD_cid.ProdName2 = Temp;
867 // Byte 8
868 Temp = (uint8_t)((SD_Handle.CID[2] & 0xFF000000) >> 24);
869 SD_CardInfo.SD_cid.ProdRev = Temp;
871 // Byte 9
872 Temp = (uint8_t)((SD_Handle.CID[2] & 0x00FF0000) >> 16);
873 SD_CardInfo.SD_cid.ProdSN = Temp << 24;
875 // Byte 10
876 Temp = (uint8_t)((SD_Handle.CID[2] & 0x0000FF00) >> 8);
877 SD_CardInfo.SD_cid.ProdSN |= Temp << 16;
879 // Byte 11
880 Temp = (uint8_t)(SD_Handle.CID[2] & 0x000000FF);
881 SD_CardInfo.SD_cid.ProdSN |= Temp << 8;
883 // Byte 12
884 Temp = (uint8_t)((SD_Handle.CID[3] & 0xFF000000) >> 24);
885 SD_CardInfo.SD_cid.ProdSN |= Temp;
887 // Byte 13
888 Temp = (uint8_t)((SD_Handle.CID[3] & 0x00FF0000) >> 16);
889 SD_CardInfo.SD_cid.Reserved1 |= (Temp & 0xF0) >> 4;
890 SD_CardInfo.SD_cid.ManufactDate = (Temp & 0x0F) << 8;
892 // Byte 14
893 Temp = (uint8_t)((SD_Handle.CID[3] & 0x0000FF00) >> 8);
894 SD_CardInfo.SD_cid.ManufactDate |= Temp;
896 // Byte 15
897 Temp = (uint8_t)(SD_Handle.CID[3] & 0x000000FF);
898 SD_CardInfo.SD_cid.CID_CRC = (Temp & 0xFE) >> 1;
899 SD_CardInfo.SD_cid.Reserved2 = 1;
901 return ErrorState;
905 /** -----------------------------------------------------------------------------------------------------------------*/
907 * @brief Enables wide bus operation for the requested card if supported by
908 * card.
909 * @param WideMode: Specifies the SD card wide bus mode
910 * This parameter can be one of the following values:
911 * @arg SD_BUS_WIDE_8B: 8-bit data transfer (Only for MMC)
912 * @arg SD_BUS_WIDE_4B: 4-bit data transfer
913 * @arg SD_BUS_WIDE_1B: 1-bit data transfer
914 * @retval SD Card error state
916 static SD_Error_t SD_WideBusOperationConfig(uint32_t WideMode)
918 SD_Error_t ErrorState = SD_OK;
919 uint32_t Temp;
920 uint32_t SCR[2] = {0, 0};
922 if ((SD_CardType == SD_STD_CAPACITY_V1_1) || (SD_CardType == SD_STD_CAPACITY_V2_0) || (SD_CardType == SD_HIGH_CAPACITY)) {
923 if (WideMode == SD_BUS_WIDE_8B) {
924 ErrorState = SD_UNSUPPORTED_FEATURE;
926 else if ((WideMode == SD_BUS_WIDE_4B) || (WideMode == SD_BUS_WIDE_1B)) {
927 if ((SDMMC1->RESP1 & SD_CARD_LOCKED) != SD_CARD_LOCKED) {
928 // Get SCR Register
929 ErrorState = SD_FindSCR(SCR);
931 if (ErrorState == SD_OK) {
932 Temp = (WideMode == SD_BUS_WIDE_4B) ? SD_WIDE_BUS_SUPPORT : SD_SINGLE_BUS_SUPPORT;
934 // If requested card supports wide bus operation
935 if ((SCR[1] & Temp) != SD_ALLZERO) {
936 // Send CMD55 APP_CMD with argument as card's RCA.
937 ErrorState = SD_TransmitCommand((SD_CMD_APP_CMD | SD_CMD_RESPONSE_SHORT), SD_CardRCA, 1);
938 if (ErrorState == SD_OK) {
939 Temp = (WideMode == SD_BUS_WIDE_4B) ? 2 : 0;
941 // Send ACMD6 APP_CMD with argument as 2 for wide bus mode
942 ErrorState = SD_TransmitCommand((SD_CMD_APP_SD_SET_BUSWIDTH | SD_CMD_RESPONSE_SHORT), Temp, 1);
945 else {
946 ErrorState = SD_REQUEST_NOT_APPLICABLE;
950 else {
951 ErrorState = SD_LOCK_UNLOCK_FAILED;
954 else {
955 ErrorState = SD_INVALID_PARAMETER; // WideMode is not a valid argument
958 if (ErrorState == SD_OK) {
959 // Configure the SDMMC1 peripheral, we need this delay for some reason...
960 while ((READ_REG(SDMMC1->CLKCR) & 0x800) != WideMode) {
961 MODIFY_REG(SDMMC1->CLKCR, CLKCR_CLEAR_MASK, (uint32_t) WideMode);
965 else {
966 ErrorState = SD_UNSUPPORTED_FEATURE;
970 return ErrorState;
974 /** -----------------------------------------------------------------------------------------------------------------*/
976 * @brief Gets the current card's data status.
977 * @retval Data Transfer state
979 SD_Error_t SD_GetStatus(void)
981 SD_Error_t ErrorState;
982 uint32_t Response1;
983 SD_CardState_t CardState;
986 // Send Status command
987 if ((ErrorState = SD_TransmitCommand((SD_CMD_SEND_STATUS | SD_CMD_RESPONSE_SHORT), SD_CardRCA, 1)) == SD_OK) {
988 Response1 = SDMMC1->RESP1;
989 CardState = (SD_CardState_t)((Response1 >> 9) & 0x0F);
991 // Find SD status according to card state
992 if (CardState == SD_CARD_TRANSFER) {
993 ErrorState = SD_OK;
995 else if (CardState == SD_CARD_ERROR) {
996 ErrorState = SD_ERROR;
998 else {
999 ErrorState = SD_BUSY;
1002 else {
1003 ErrorState = SD_ERROR;
1006 return ErrorState;
1010 /** -----------------------------------------------------------------------------------------------------------------*/
1012 * @brief Gets the SD card status.
1013 * @retval SD Card error state
1015 SD_Error_t SD_GetCardStatus(SD_CardStatus_t* pCardStatus)
1017 SD_Error_t ErrorState;
1018 uint32_t Temp = 0;
1019 uint32_t Status[16];
1020 uint32_t Count;
1022 // Check SD response
1023 if ((SDMMC1->RESP1 & SD_CARD_LOCKED) == SD_CARD_LOCKED) {
1024 return SD_LOCK_UNLOCK_FAILED;
1027 // Set block size for card if it is not equal to current block size for card
1028 if ((ErrorState = SD_TransmitCommand((SD_CMD_SET_BLOCKLEN | SD_CMD_RESPONSE_SHORT), 64, 1)) != SD_OK) {
1029 return ErrorState;
1032 // Send CMD55
1033 if ((ErrorState = SD_TransmitCommand((SD_CMD_APP_CMD | SD_CMD_RESPONSE_SHORT), SD_CardRCA, 1)) != SD_OK) {
1034 return ErrorState;
1037 // Configure the SD DPSM (Data Path State Machine)
1038 SD_DataTransferInit(64, SD_DATABLOCK_SIZE_64B, true);
1040 // Send ACMD13 (SD_APP_STAUS) with argument as card's RCA
1041 if ((ErrorState = SD_TransmitCommand((SD_CMD_SD_APP_STATUS | SD_CMD_RESPONSE_SHORT), 0, 1)) != SD_OK) {
1042 return ErrorState;
1045 // Get status data
1046 while ((SDMMC1->STA & (SDMMC_STA_RXOVERR | SDMMC_STA_DCRCFAIL | SDMMC_STA_DTIMEOUT | SDMMC_STA_DBCKEND)) == 0) {
1047 if ((SDMMC1->STA & SDMMC_STA_RXFIFOHF) != 0) {
1048 for(Count = 0; Count < 8; Count++) {
1049 Status[Count] = SDMMC1->FIFO;
1054 if ((SDMMC1->STA & SDMMC_STA_DTIMEOUT) != 0) {
1055 return SD_DATA_TIMEOUT;
1057 else if ((SDMMC1->STA & SDMMC_STA_DCRCFAIL) != 0) {
1058 return SD_DATA_CRC_FAIL;
1060 else if ((SDMMC1->STA & SDMMC_STA_RXOVERR) != 0) {
1061 return SD_RX_OVERRUN;
1063 else {
1064 Count = SD_DATATIMEOUT;
1065 while (((SDMMC1->STA & SDMMC_STA_RXDAVL) != 0) && (Count > 0)) {
1066 volatile uint8_t tmp = SDMMC1->FIFO;
1067 (void)tmp;
1068 Count--;
1072 // Byte 0
1073 Temp = (Status[0] & 0xC0) >> 6;
1074 pCardStatus->DAT_BUS_WIDTH = (uint8_t)Temp;
1076 // Byte 0
1077 Temp = (Status[0] & 0x20) >> 5;
1078 pCardStatus->SECURED_MODE = (uint8_t)Temp;
1080 // Byte 2
1081 Temp = (Status[2] & 0xFF);
1082 pCardStatus->SD_CARD_TYPE = (uint8_t)(Temp << 8);
1084 // Byte 3
1085 Temp = (Status[3] & 0xFF);
1086 pCardStatus->SD_CARD_TYPE |= (uint8_t)Temp;
1088 // Byte 4
1089 Temp = (Status[4] & 0xFF);
1090 pCardStatus->SIZE_OF_PROTECTED_AREA = (uint8_t)(Temp << 24);
1092 // Byte 5
1093 Temp = (Status[5] & 0xFF);
1094 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(Temp << 16);
1096 // Byte 6
1097 Temp = (Status[6] & 0xFF);
1098 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)(Temp << 8);
1100 // Byte 7
1101 Temp = (Status[7] & 0xFF);
1102 pCardStatus->SIZE_OF_PROTECTED_AREA |= (uint8_t)Temp;
1104 // Byte 8
1105 Temp = (Status[8] & 0xFF);
1106 pCardStatus->SPEED_CLASS = (uint8_t)Temp;
1108 // Byte 9
1109 Temp = (Status[9] & 0xFF);
1110 pCardStatus->PERFORMANCE_MOVE = (uint8_t)Temp;
1112 // Byte 10
1113 Temp = (Status[10] & 0xF0) >> 4;
1114 pCardStatus->AU_SIZE = (uint8_t)Temp;
1116 // Byte 11
1117 Temp = (Status[11] & 0xFF);
1118 pCardStatus->ERASE_SIZE = (uint8_t)(Temp << 8);
1120 // Byte 12
1121 Temp = (Status[12] & 0xFF);
1122 pCardStatus->ERASE_SIZE |= (uint8_t)Temp;
1124 // Byte 13
1125 Temp = (Status[13] & 0xFC) >> 2;
1126 pCardStatus->ERASE_TIMEOUT = (uint8_t)Temp;
1128 // Byte 13
1129 Temp = (Status[13] & 0x3);
1130 pCardStatus->ERASE_OFFSET = (uint8_t)Temp;
1132 return SD_OK;
1136 /** -----------------------------------------------------------------------------------------------------------------*/
1138 * @brief Enquires cards about their operating voltage and configures clock
1139 * controls and stores SD information that will be needed in future
1140 * in the SD handle.
1141 * @retval SD Card error state
1143 static SD_Error_t SD_PowerON(void)
1145 SD_Error_t ErrorState;
1146 uint32_t Response;
1147 uint32_t Count;
1148 uint32_t ValidVoltage;
1149 uint32_t SD_Type;
1150 //uint32_t TickStart;
1152 Count = 0;
1153 ValidVoltage = 0;
1154 SD_Type = SD_RESP_STD_CAPACITY;
1156 // Power ON Sequence -------------------------------------------------------
1157 SDMMC1->CLKCR &= ~SDMMC_CLKCR_CLKEN; // Disable SDMMC1 Clock
1158 SDMMC1->POWER = SDMMC_POWER_PWRCTRL; // Set Power State to ON
1160 // 1ms: required power up waiting time before starting the SD initialization sequence (make it 2 to be safe)
1161 delay(2);
1163 SDMMC1->CLKCR |= SDMMC_CLKCR_CLKEN; // Enable SDMMC1 Clock
1165 // CMD0: GO_IDLE_STATE -----------------------------------------------------
1166 // No CMD response required
1167 if ((ErrorState = SD_TransmitCommand(SD_CMD_GO_IDLE_STATE, 0, 0)) != SD_OK) {
1168 // CMD Response Timeout (wait for CMDSENT flag)
1169 return ErrorState;
1172 // CMD8: SEND_IF_COND ------------------------------------------------------
1173 // Send CMD8 to verify SD card interface operating condition
1174 // Argument: - [31:12]: Reserved (shall be set to '0')
1175 //- [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
1176 //- [7:0]: Check Pattern (recommended 0xAA)
1177 // CMD Response: R7 */
1178 if ((ErrorState = SD_TransmitCommand((SD_SDMMC_SEND_IF_COND | SD_CMD_RESPONSE_SHORT), SD_CHECK_PATTERN, 7)) == SD_OK) {
1179 // SD Card 2.0
1180 SD_CardType = SD_STD_CAPACITY_V2_0;
1181 SD_Type = SD_RESP_HIGH_CAPACITY;
1184 // Send CMD55
1185 // If ErrorState is Command Timeout, it is a MMC card
1186 // If ErrorState is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch) or SD card 1.x
1187 if ((ErrorState = SD_TransmitCommand((SD_CMD_APP_CMD | SD_CMD_RESPONSE_SHORT), 0, 1)) == SD_OK) {
1188 // SD CARD
1189 // Send ACMD41 SD_APP_OP_COND with Argument 0x80100000
1190 while ((ValidVoltage == 0) && (Count < SD_MAX_VOLT_TRIAL)) {
1191 // SEND CMD55 APP_CMD with RCA as 0
1192 if ((ErrorState = SD_TransmitCommand((SD_CMD_APP_CMD | SD_CMD_RESPONSE_SHORT), 0, 1)) != SD_OK) {
1193 return ErrorState;
1196 // Send CMD41
1197 if ((ErrorState = SD_TransmitCommand((SD_CMD_SD_APP_OP_COND | SD_CMD_RESPONSE_SHORT), SD_VOLTAGE_WINDOW_SD | SD_Type, 3)) != SD_OK) {
1198 return ErrorState;
1201 Response = SDMMC1->RESP1; // Get command response
1202 ValidVoltage = (((Response >> 31) == 1) ? 1 : 0); // Get operating voltage
1203 Count++;
1206 if (Count >= SD_MAX_VOLT_TRIAL) {
1207 return SD_INVALID_VOLTRANGE;
1210 if ((Response & SD_RESP_HIGH_CAPACITY) == SD_RESP_HIGH_CAPACITY) {
1211 SD_CardType = SD_HIGH_CAPACITY;
1213 } // else MMC Card
1215 return ErrorState;
1219 /** -----------------------------------------------------------------------------------------------------------------*/
1221 * @brief Finds the SD card SCR register value.
1222 * @param pSCR: pointer to the buffer that will contain the SCR value
1223 * @retval SD Card error state
1225 static SD_Error_t SD_FindSCR(uint32_t *pSCR)
1227 SD_Error_t ErrorState;
1228 uint32_t Index = 0;
1229 uint32_t tempscr[2] = {0, 0};
1231 // Set Block Size To 8 Bytes
1232 // Send CMD55 APP_CMD with argument as card's RCA
1233 if ((ErrorState = SD_TransmitCommand((SD_CMD_SET_BLOCKLEN | SD_CMD_RESPONSE_SHORT), 8, 1)) == SD_OK) {
1234 // Send CMD55 APP_CMD with argument as card's RCA
1235 if ((ErrorState = SD_TransmitCommand((SD_CMD_APP_CMD | SD_CMD_RESPONSE_SHORT), SD_CardRCA, 1)) == SD_OK) {
1236 SD_DataTransferInit(8, SD_DATABLOCK_SIZE_8B, true);
1238 // Send ACMD51 SD_APP_SEND_SCR with argument as 0
1239 if ((ErrorState = SD_TransmitCommand((SD_CMD_SD_APP_SEND_SCR | SD_CMD_RESPONSE_SHORT), 0, 1)) == SD_OK) {
1240 while ((SDMMC1->STA & (SDMMC_STA_RXOVERR | SDMMC_STA_DCRCFAIL | SDMMC_STA_DTIMEOUT | SDMMC_STA_DBCKEND)) == 0) {
1241 if ((SDMMC1->STA & SDMMC_STA_RXDAVL) != 0) {
1242 *(tempscr + Index) = SDMMC1->FIFO;
1243 Index++;
1247 if ((SDMMC1->STA & SDMMC_STA_DTIMEOUT) != 0) {
1248 ErrorState = SD_DATA_TIMEOUT;
1250 else if ((SDMMC1->STA & SDMMC_STA_DCRCFAIL) != 0) {
1251 ErrorState = SD_DATA_CRC_FAIL;
1253 else if ((SDMMC1->STA & SDMMC_STA_RXOVERR) != 0) {
1254 ErrorState = SD_RX_OVERRUN;
1256 else if ((SDMMC1->STA & SDMMC_STA_RXDAVL) != 0) {
1257 ErrorState = SD_OUT_OF_BOUND;
1259 else {
1260 *(pSCR + 1) = ((tempscr[0] & SD_0TO7BITS) << 24) | ((tempscr[0] & SD_8TO15BITS) << 8) |
1261 ((tempscr[0] & SD_16TO23BITS) >> 8) | ((tempscr[0] & SD_24TO31BITS) >> 24);
1263 *(pSCR) = ((tempscr[1] & SD_0TO7BITS) << 24) | ((tempscr[1] & SD_8TO15BITS) << 8) |
1264 ((tempscr[1] & SD_16TO23BITS) >> 8) | ((tempscr[1] & SD_24TO31BITS) >> 24);
1270 return ErrorState;
1274 /** -----------------------------------------------------------------------------------------------------------------*/
1276 * @brief Initialize the SDMMC1 module, DMA, and IO
1278 bool SD_Initialize_LL(DMA_Stream_TypeDef * dmaRef)
1280 // Sanity check DMA stread - we only support two possible
1281 if (((uint32_t)dmaRef != (uint32_t)DMA2_Stream3) && ((uint32_t)dmaRef != (uint32_t)DMA2_Stream6)) {
1282 return false;
1285 // Reset SDMMC1 Module
1286 RCC->APB2RSTR |= RCC_APB2RSTR_SDMMC1RST;
1287 delay(1);
1288 RCC->APB2RSTR &= ~RCC_APB2RSTR_SDMMC1RST;
1289 delay(1);
1291 // Enable SDMMC1 clock
1292 RCC->APB2ENR |= RCC_APB2ENR_SDMMC1EN;
1294 // Enable DMA2 clocks
1295 RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
1297 //Configure Pins
1298 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN | RCC_AHB1ENR_GPIODEN;
1300 const IO_t d0 = IOGetByTag(IO_TAG(PC8));
1301 const IO_t clk = IOGetByTag(IO_TAG(PC12));
1302 const IO_t cmd = IOGetByTag(IO_TAG(PD2));
1304 #define SDMMC_DATA IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
1305 #define SDMMC_CMD IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
1306 #define SDMMC_CLK IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
1308 IOInit(d0, OWNER_SDCARD, RESOURCE_NONE, 0);
1309 IOConfigGPIOAF(d0, SDMMC_DATA, GPIO_AF12_SDMMC1);
1311 #ifdef SDCARD_SDIO_4BIT
1312 const IO_t d1 = IOGetByTag(IO_TAG(PC9));
1313 const IO_t d2 = IOGetByTag(IO_TAG(PC10));
1314 const IO_t d3 = IOGetByTag(IO_TAG(PC11));
1316 IOInit(d1, OWNER_SDCARD, RESOURCE_NONE, 0);
1317 IOConfigGPIOAF(d1, SDMMC_DATA, GPIO_AF12_SDMMC1);
1319 IOInit(d2, OWNER_SDCARD, RESOURCE_NONE, 0);
1320 IOConfigGPIOAF(d2, SDMMC_DATA, GPIO_AF12_SDMMC1);
1322 IOInit(d3, OWNER_SDCARD, RESOURCE_NONE, 0);
1323 IOConfigGPIOAF(d3, SDMMC_DATA, GPIO_AF12_SDMMC1);
1324 #endif
1326 IOInit(clk, OWNER_SDCARD, RESOURCE_NONE, 0);
1327 IOConfigGPIOAF(clk, SDMMC_CLK, GPIO_AF12_SDMMC1);
1329 IOInit(cmd, OWNER_SDCARD, RESOURCE_NONE, 0);
1330 IOConfigGPIOAF(cmd, SDMMC_CMD, GPIO_AF12_SDMMC1);
1332 // NVIC configuration for SDIO interrupts
1333 NVIC_SetPriority(SDMMC1_IRQn, NVIC_PRIO_SDIO);
1334 NVIC_EnableIRQ(SDMMC1_IRQn);
1336 dma_stream = dmaRef;
1337 RCC->AHB1ENR |= RCC_AHB1ENR_DMA2EN;
1338 if ((uint32_t)dma_stream == (uint32_t)DMA2_Stream3) {
1339 // Initialize DMA2 channel 3
1340 DMA2_Stream3->CR = 0; // Reset DMA Stream control register
1341 DMA2_Stream3->PAR = (uint32_t)&SDMMC1->FIFO;
1342 DMA2->LIFCR = IFCR_CLEAR_MASK_STREAM3; // Clear all interrupt flags
1343 DMA2_Stream3->CR = (DMA_CHANNEL_4 | DMA_SxCR_PFCTRL | // Prepare the DMA Stream configuration
1344 DMA_MINC_ENABLE | DMA_PDATAALIGN_WORD | // And write to DMA Stream CR register
1345 DMA_MDATAALIGN_WORD | DMA_PRIORITY_VERY_HIGH |
1346 DMA_MBURST_INC4 | DMA_PBURST_INC4 |
1347 DMA_MEMORY_TO_PERIPH);
1348 DMA2_Stream3->FCR = (DMA_SxFCR_DMDIS | DMA_SxFCR_FTH); // Configuration FIFO control register
1349 dmaInit(dmaGetByRef(DMA2_Stream3), OWNER_SDCARD, 0);
1350 dmaSetHandler(dmaGetByRef(DMA2_Stream3), SDMMC_DMA_ST3_IRQHandler, 1, 0);
1352 else {
1353 // Initialize DMA2 channel 6
1354 DMA2_Stream6->CR = 0; // Reset DMA Stream control register
1355 DMA2_Stream6->PAR = (uint32_t)&SDMMC1->FIFO;
1356 DMA2->HIFCR = IFCR_CLEAR_MASK_STREAM6; // Clear all interrupt flags
1357 DMA2_Stream6->CR = (DMA_CHANNEL_4 | DMA_SxCR_PFCTRL | // Prepare the DMA Stream configuration
1358 DMA_MINC_ENABLE | DMA_PDATAALIGN_WORD | // And write to DMA Stream CR register
1359 DMA_MDATAALIGN_WORD | DMA_PRIORITY_VERY_HIGH |
1360 DMA_MBURST_INC4 | DMA_PBURST_INC4 |
1361 DMA_MEMORY_TO_PERIPH);
1362 DMA2_Stream6->FCR = (DMA_SxFCR_DMDIS | DMA_SxFCR_FTH); // Configuration FIFO control register
1363 dmaInit(dmaGetByRef(DMA2_Stream6), OWNER_SDCARD, 0);
1364 dmaSetHandler(dmaGetByRef(DMA2_Stream6), SDMMC_DMA_ST6_IRQHandler, 1, 0);
1367 return true;
1371 /** -----------------------------------------------------------------------------------------------------------------*/
1372 bool SD_GetState(void)
1374 // Check SDCARD status
1375 if (SD_GetStatus() == SD_OK) return true;
1376 return false;
1380 /** -----------------------------------------------------------------------------------------------------------------*/
1381 bool SD_Init(void)
1383 SD_Error_t ErrorState;
1385 // Initialize SDMMC1 peripheral interface with default configuration for SD card initialization
1386 MODIFY_REG(SDMMC1->CLKCR, CLKCR_CLEAR_MASK, (uint32_t) SDMMC_INIT_CLK_DIV);
1388 delay(100);
1390 // Identify card operating voltage
1391 if ((ErrorState = SD_PowerON()) == SD_OK) {
1392 delay(100);
1393 // Initialize the present card and put them in idle state
1394 if ((ErrorState = SD_InitializeCard()) == SD_OK) {
1395 delay(100);
1396 // Read CSD/CID MSD registers
1397 if ((ErrorState = SD_GetCardInfo()) == SD_OK) {
1398 // Select the Card - Send CMD7 SDMMC_SEL_DESEL_CARD
1399 ErrorState = SD_TransmitCommand((SD_CMD_SEL_DESEL_CARD | SD_CMD_RESPONSE_SHORT), SD_CardRCA, 1);
1400 delay(100);
1401 MODIFY_REG(SDMMC1->CLKCR, CLKCR_CLEAR_MASK, (uint32_t) SDMMC_CLK_DIV); // Configure SDMMC1 peripheral interface
1406 // Configure SD Bus width
1407 if (ErrorState == SD_OK) {
1408 delay(100);
1409 // Enable wide operation
1410 #ifdef SDCARD_SDIO_4BIT
1411 ErrorState = SD_WideBusOperationConfig(SD_BUS_WIDE_4B);
1412 #else
1413 ErrorState = SD_WideBusOperationConfig(SD_BUS_WIDE_1B);
1414 #endif
1415 delay(100);
1418 // Configure the SDCARD device
1419 return ErrorState;
1422 /** -----------------------------------------------------------------------------------------------------------------*/
1424 * @brief This function handles SD card interrupt request.
1426 void SDMMC1_IRQHandler(void)
1428 // Check for SDMMC1 interrupt flags
1429 if ((SDMMC1->STA & SDMMC_STA_DATAEND) != 0) {
1430 SDMMC1->ICR = SDMMC_ICR_DATAENDC;
1431 SDMMC1->ICR = SDMMC_ICR_STATIC_FLAGS;
1432 SDMMC1->MASK &= ~(SDMMC_MASK_DATAENDIE | SDMMC_MASK_DCRCFAILIE | SDMMC_MASK_DTIMEOUTIE |
1433 SDMMC_MASK_TXUNDERRIE | SDMMC_MASK_RXOVERRIE | SDMMC_MASK_TXFIFOHEIE | SDMMC_MASK_RXFIFOHFIE);
1435 /* Currently doesn't implement multiple block write handling */
1436 if ((SD_Handle.Operation & 0x02) == (SDMMC_DIR_TX << 1)) {
1437 /* Disable the stream */
1438 dma_stream->CR &= ~DMA_SxCR_EN;
1439 SDMMC1->DCTRL &= ~(SDMMC_DCTRL_DMAEN);
1440 /* Transfer is complete */
1441 SD_Handle.TXCplt = 0;
1442 if ((SD_Handle.Operation & 0x01) == SD_MULTIPLE_BLOCK) {
1443 /* Send stop command in multiblock write */
1444 SD_TransmitCommand((SD_CMD_STOP_TRANSMISSION | SD_CMD_RESPONSE_SHORT), 0, 1);
1447 SD_Handle.TransferComplete = 1;
1448 SD_Handle.TransferError = SD_OK; // No transfer error
1450 else if ((SDMMC1->STA & SDMMC_STA_DCRCFAIL) != 0) {
1451 SD_Handle.TransferError = SD_DATA_CRC_FAIL;
1453 else if ((SDMMC1->STA & SDMMC_STA_DTIMEOUT) != 0) {
1454 SD_Handle.TransferError = SD_DATA_TIMEOUT;
1456 else if ((SDMMC1->STA & SDMMC_STA_RXOVERR) != 0) {
1457 SD_Handle.TransferError = SD_RX_OVERRUN;
1459 else if ((SDMMC1->STA & SDMMC_STA_TXUNDERR) != 0) {
1460 SD_Handle.TransferError = SD_TX_UNDERRUN;
1463 SDMMC1->ICR = SDMMC_ICR_STATIC_FLAGS;
1465 // Disable all SDMMC1 peripheral interrupt sources
1466 SDMMC1->MASK &= ~(SDMMC_MASK_DCRCFAILIE | SDMMC_MASK_DTIMEOUTIE | SDMMC_MASK_DATAENDIE |
1467 SDMMC_MASK_TXFIFOHEIE | SDMMC_MASK_RXFIFOHFIE | SDMMC_MASK_TXUNDERRIE |
1468 SDMMC_MASK_RXOVERRIE);
1471 /** -----------------------------------------------------------------------------------------------------------------*/
1473 * @brief This function handles DMA2 Stream 3 interrupt request.
1475 void SDMMC_DMA_ST3_IRQHandler(dmaChannelDescriptor_t *dma)
1477 UNUSED(dma);
1478 // Transfer Error Interrupt management
1479 if ((DMA2->LISR & DMA_LISR_TEIF3) != 0) {
1480 if ((DMA2_Stream3->CR & DMA_SxCR_TEIE) != 0) {
1481 DMA2_Stream3->CR &= ~DMA_SxCR_TEIE; // Disable the transfer error interrupt
1482 DMA2->LIFCR = DMA_LIFCR_CTEIF3; // Clear the transfer error flag
1486 // FIFO Error Interrupt management
1487 if ((DMA2->LISR & DMA_LISR_FEIF3) != 0) {
1488 if ((DMA2_Stream3->FCR & DMA_SxFCR_FEIE) != 0) {
1489 DMA2_Stream3->FCR &= ~DMA_SxFCR_FEIE; // Disable the FIFO Error interrupt
1490 DMA2->LIFCR = DMA_LIFCR_CFEIF3; // Clear the FIFO error flag
1494 // Direct Mode Error Interrupt management
1495 if ((DMA2->LISR & DMA_LISR_DMEIF3) != 0) {
1496 if ((DMA2_Stream3->CR & DMA_SxCR_DMEIE) != 0) {
1497 DMA2_Stream3->CR &= ~DMA_SxCR_DMEIE; // Disable the direct mode Error interrupt
1498 DMA2->LIFCR = DMA_LIFCR_CDMEIF3; // Clear the FIFO error flag
1502 // Half Transfer Complete Interrupt management
1503 if ((DMA2->LISR & DMA_LISR_HTIF3) != 0) {
1504 if ((DMA2_Stream3->CR & DMA_SxCR_HTIE) != 0) {
1505 if (((DMA2_Stream3->CR) & (uint32_t)(DMA_SxCR_DBM)) != 0) { // Multi_Buffering mode enabled
1506 DMA2->LIFCR = DMA_LIFCR_CHTIF3; // Clear the half transfer complete flag
1508 else {
1509 if ((DMA2_Stream3->CR & DMA_SxCR_CIRC) == 0) { // Disable the half transfer interrupt if the DMA mode is not CIRCULAR
1510 DMA2_Stream3->CR &= ~DMA_SxCR_HTIE; // Disable the half transfer interrupt
1513 DMA2->LIFCR = DMA_LIFCR_CHTIF3; // Clear the half transfer complete flag
1518 // Transfer Complete Interrupt management
1519 if ((DMA2->LISR & DMA_LISR_TCIF3) != 0) {
1520 if ((DMA2_Stream3->CR & DMA_SxCR_TCIE) != 0) {
1521 if ((DMA2_Stream3->CR & (uint32_t)(DMA_SxCR_DBM)) != 0) {
1522 DMA2->LIFCR = DMA_LIFCR_CTCIF3; // Clear the transfer complete flag
1524 else {
1525 if ((DMA2_Stream3->CR & DMA_SxCR_CIRC) == 0) {
1526 DMA2_Stream3->CR &= ~DMA_SxCR_TCIE; // Disable the transfer complete interrupt
1529 DMA2->LIFCR = DMA_LIFCR_CTCIF3; // Clear the transfer complete flag
1530 SD_DMA_Complete(DMA2_Stream3);
1537 /** -----------------------------------------------------------------------------------------------------------------*/
1539 * @brief This function handles DMA2 Stream 6 interrupt request.
1541 void SDMMC_DMA_ST6_IRQHandler(dmaChannelDescriptor_t *dma)
1543 UNUSED(dma);
1544 // Transfer Error Interrupt management
1545 if ((DMA2->HISR & DMA_HISR_TEIF6) != 0) {
1546 if ((DMA2_Stream6->CR & DMA_SxCR_TEIE) != 0) {
1547 DMA2_Stream6->CR &= ~DMA_SxCR_TEIE; // Disable the transfer error interrupt
1548 DMA2->HIFCR = DMA_HIFCR_CTEIF6; // Clear the transfer error flag
1552 // FIFO Error Interrupt management
1553 if ((DMA2->HISR & DMA_HISR_FEIF6) != 0) {
1554 if ((DMA2_Stream6->FCR & DMA_SxFCR_FEIE) != 0) {
1555 DMA2_Stream6->FCR &= ~DMA_SxFCR_FEIE; // Disable the FIFO Error interrupt
1556 DMA2->HIFCR = DMA_HIFCR_CFEIF6; // Clear the FIFO error flag
1560 // Direct Mode Error Interrupt management
1561 if ((DMA2->HISR & DMA_HISR_DMEIF6) != 0) {
1562 if ((DMA2_Stream6->CR & DMA_SxCR_DMEIE) != 0) {
1563 DMA2_Stream6->CR &= ~DMA_SxCR_DMEIE; // Disable the direct mode Error interrupt
1564 DMA2->HIFCR = DMA_HIFCR_CDMEIF6; // Clear the FIFO error flag
1568 // Half Transfer Complete Interrupt management
1569 if ((DMA2->HISR & DMA_HISR_HTIF6) != 0) {
1570 if ((DMA2_Stream6->CR & DMA_SxCR_HTIE) != 0) {
1571 if (((DMA2_Stream6->CR) & (uint32_t)(DMA_SxCR_DBM)) != 0) { // Multi_Buffering mode enabled
1572 DMA2->HIFCR = DMA_HIFCR_CHTIF6; // Clear the half transfer complete flag
1574 else {
1575 if ((DMA2_Stream6->CR & DMA_SxCR_CIRC) == 0) { // Disable the half transfer interrupt if the DMA mode is not CIRCULAR
1576 DMA2_Stream6->CR &= ~DMA_SxCR_HTIE; // Disable the half transfer interrupt
1579 DMA2->HIFCR = DMA_HIFCR_CHTIF6; // Clear the half transfer complete flag
1584 // Transfer Complete Interrupt management
1585 if ((DMA2->HISR & DMA_HISR_TCIF6) != 0) {
1586 if ((DMA2_Stream6->CR & DMA_SxCR_TCIE) != 0) {
1587 if ((DMA2_Stream6->CR & (uint32_t)(DMA_SxCR_DBM)) != 0) {
1588 DMA2->HIFCR = DMA_HIFCR_CTCIF6; // Clear the transfer complete flag
1590 else {
1591 if ((DMA2_Stream6->CR & DMA_SxCR_CIRC) == 0) {
1592 DMA2_Stream6->CR &= ~DMA_SxCR_TCIE; // Disable the transfer complete interrupt
1595 DMA2->HIFCR = DMA_HIFCR_CTCIF6; // Clear the transfer complete flag
1596 SD_DMA_Complete(DMA2_Stream6);
1602 /* ------------------------------------------------------------------------------------------------------------------*/
1603 #endif