2 * This file is part of Cleanflight and Betaflight.
4 * 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)
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)
26 /* Include(s) -------------------------------------------------------------------------------------------------------*/
33 #ifdef USE_SDCARD_SDIO
35 #include "drivers/sdmmc_sdio.h"
36 #include "stm32f7xx.h"
40 #include "drivers/io.h"
41 #include "drivers/io_impl.h"
42 #include "drivers/nvic.h"
43 #include "drivers/time.h"
44 #include "drivers/rcc.h"
45 #include "drivers/dma.h"
46 #include "drivers/light_led.h"
48 #include "build/debug.h"
50 #define BLOCK_SIZE ((uint32_t)(512))
52 #define IFCR_CLEAR_MASK_STREAM3 (DMA_LIFCR_CTCIF3 | DMA_LIFCR_CHTIF3 | DMA_LIFCR_CTEIF3 | DMA_LIFCR_CDMEIF3 | DMA_LIFCR_CFEIF3)
53 #define IFCR_CLEAR_MASK_STREAM6 (DMA_HIFCR_CTCIF6 | DMA_HIFCR_CHTIF6 | DMA_HIFCR_CTEIF6 | DMA_HIFCR_CDMEIF6 | DMA_HIFCR_CFEIF6)
55 #define SDMMC_ICR_STATIC_FLAGS ((uint32_t)(SDMMC_ICR_CCRCFAILC | SDMMC_ICR_DCRCFAILC | SDMMC_ICR_CTIMEOUTC |\
56 SDMMC_ICR_DTIMEOUTC | SDMMC_ICR_TXUNDERRC | SDMMC_ICR_RXOVERRC |\
57 SDMMC_ICR_CMDRENDC | SDMMC_ICR_CMDSENTC | SDMMC_ICR_DATAENDC |\
60 #define SD_SOFTWARE_COMMAND_TIMEOUT ((uint32_t)0x00020000)
62 #define SD_OCR_ADDR_OUT_OF_RANGE ((uint32_t)0x80000000)
63 #define SD_OCR_ADDR_MISALIGNED ((uint32_t)0x40000000)
64 #define SD_OCR_BLOCK_LEN_ERR ((uint32_t)0x20000000)
65 #define SD_OCR_ERASE_SEQ_ERR ((uint32_t)0x10000000)
66 #define SD_OCR_BAD_ERASE_PARAM ((uint32_t)0x08000000)
67 #define SD_OCR_WRITE_PROT_VIOLATION ((uint32_t)0x04000000)
68 #define SD_OCR_LOCK_UNLOCK_FAILED ((uint32_t)0x01000000)
69 #define SD_OCR_COM_CRC_FAILED ((uint32_t)0x00800000)
70 #define SD_OCR_ILLEGAL_CMD ((uint32_t)0x00400000)
71 #define SD_OCR_CARD_ECC_FAILED ((uint32_t)0x00200000)
72 #define SD_OCR_CC_ERROR ((uint32_t)0x00100000)
73 #define SD_OCR_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00080000)
74 #define SD_OCR_STREAM_READ_UNDERRUN ((uint32_t)0x00040000)
75 #define SD_OCR_STREAM_WRITE_OVERRUN ((uint32_t)0x00020000)
76 #define SD_OCR_CID_CSD_OVERWRITE ((uint32_t)0x00010000)
77 #define SD_OCR_WP_ERASE_SKIP ((uint32_t)0x00008000)
78 #define SD_OCR_CARD_ECC_DISABLED ((uint32_t)0x00004000)
79 #define SD_OCR_ERASE_RESET ((uint32_t)0x00002000)
80 #define SD_OCR_AKE_SEQ_ERROR ((uint32_t)0x00000008)
81 #define SD_OCR_ERRORBITS ((uint32_t)0xFDFFE008)
83 #define SD_R6_GENERAL_UNKNOWN_ERROR ((uint32_t)0x00002000)
84 #define SD_R6_ILLEGAL_CMD ((uint32_t)0x00004000)
85 #define SD_R6_COM_CRC_FAILED ((uint32_t)0x00008000)
87 #define SD_VOLTAGE_WINDOW_SD ((uint32_t)0x80100000)
88 #define SD_RESP_HIGH_CAPACITY ((uint32_t)0x40000000)
89 #define SD_RESP_STD_CAPACITY ((uint32_t)0x00000000)
90 #define SD_CHECK_PATTERN ((uint32_t)0x000001AA)
92 #define SD_MAX_VOLT_TRIAL ((uint32_t)0x0000FFFF)
93 #define SD_ALLZERO ((uint32_t)0x00000000)
95 #define SD_WIDE_BUS_SUPPORT ((uint32_t)0x00040000)
96 #define SD_SINGLE_BUS_SUPPORT ((uint32_t)0x00010000)
97 #define SD_CARD_LOCKED ((uint32_t)0x02000000)
99 #define SD_0TO7BITS ((uint32_t)0x000000FF)
100 #define SD_8TO15BITS ((uint32_t)0x0000FF00)
101 #define SD_16TO23BITS ((uint32_t)0x00FF0000)
102 #define SD_24TO31BITS ((uint32_t)0xFF000000)
103 #define SD_MAX_DATA_LENGTH ((uint32_t)0x01FFFFFF)
105 #define SD_CCCC_ERASE ((uint32_t)0x00000020)
107 #define SD_SDMMC_SEND_IF_COND ((uint32_t)SD_CMD_HS_SEND_EXT_CSD)
109 #define SD_BUS_WIDE_1B ((uint32_t)0x00000000)
110 #define SD_BUS_WIDE_4B SDMMC_CLKCR_WIDBUS_0
111 #define SD_BUS_WIDE_8B SDMMC_CLKCR_WIDBUS_1
113 #define SD_CMD_RESPONSE_SHORT SDMMC_CMD_WAITRESP_0
114 #define SD_CMD_RESPONSE_LONG SDMMC_CMD_WAITRESP
116 #define SD_DATABLOCK_SIZE_8B (SDMMC_DCTRL_DBLOCKSIZE_0|SDMMC_DCTRL_DBLOCKSIZE_1)
117 #define SD_DATABLOCK_SIZE_64B (SDMMC_DCTRL_DBLOCKSIZE_1|SDMMC_DCTRL_DBLOCKSIZE_2)
118 #define SD_DATABLOCK_SIZE_512B (SDMMC_DCTRL_DBLOCKSIZE_0|SDMMC_DCTRL_DBLOCKSIZE_3)
120 #define CLKCR_CLEAR_MASK ((uint32_t)(SDMMC_CLKCR_CLKDIV | SDMMC_CLKCR_PWRSAV |\
121 SDMMC_CLKCR_BYPASS | SDMMC_CLKCR_WIDBUS |\
122 SDMMC_CLKCR_NEGEDGE | SDMMC_CLKCR_HWFC_EN))
124 #define DCTRL_CLEAR_MASK ((uint32_t)(SDMMC_DCTRL_DTEN | SDMMC_DCTRL_DTDIR |\
125 SDMMC_DCTRL_DTMODE | SDMMC_DCTRL_DBLOCKSIZE))
127 #define CMD_CLEAR_MASK ((uint32_t)(SDMMC_CMD_CMDINDEX | SDMMC_CMD_WAITRESP |\
128 SDMMC_CMD_WAITINT | SDMMC_CMD_WAITPEND |\
129 SDMMC_CMD_CPSMEN | SDMMC_CMD_SDMMC1SUSPEND))
131 #define SDMMC_INIT_CLK_DIV ((uint8_t)0x76)
132 #define SDMMC_CLK_DIV ((uint8_t)0x00)
134 #define SD_CMD_GO_IDLE_STATE ((uint8_t)0) // Resets the SD memory card.
135 #define SD_CMD_SEND_OP_COND ((uint8_t)1) // Sends host capacity support information and activates the card's initialization process.
136 #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.
137 #define SD_CMD_SET_REL_ADDR ((uint8_t)3) // Asks the card to publish a new relative address (RCA).
138 #define SD_CMD_HS_SWITCH ((uint8_t)6) // Checks switchable function (mode 0) and switch card function (mode 1).
139 #define SD_CMD_SEL_DESEL_CARD ((uint8_t)7) // Selects the card by its own relative address and gets deselected by any other address
140 #define SD_CMD_HS_SEND_EXT_CSD ((uint8_t)8) // Sends SD Memory Card interface condition, which includes host supply voltage information
141 // and asks the card whether card supports voltage.
142 #define SD_CMD_SEND_CSD ((uint8_t)9) // Addressed card sends its card specific data (CSD) on the CMD line.
143 #define SD_CMD_SEND_CID ((uint8_t)10) // Addressed card sends its card identification (CID) on the CMD line.
144 #define SD_CMD_STOP_TRANSMISSION ((uint8_t)12) // Forces the card to stop transmission.
145 #define SD_CMD_SEND_STATUS ((uint8_t)13) // Addressed card sends its status register.
146 #define SD_CMD_SET_BLOCKLEN ((uint8_t)16) // Sets the block length (in bytes for SDSC) for all following block commands
147 // (read, write, lock). Default block length is fixed to 512 Bytes. Not effective
148 // for SDHS and SDXC.
149 #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
150 // fixed 512 bytes in case of SDHC and SDXC.
151 #define SD_CMD_READ_MULT_BLOCK ((uint8_t)18) // Continuously transfers data blocks from card to host until interrupted by
152 // STOP_TRANSMISSION command.
153 #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
154 // fixed 512 bytes in case of SDHC and SDXC.
155 #define SD_CMD_WRITE_MULT_BLOCK ((uint8_t)25) // Continuously writes blocks of data until a STOP_TRANSMISSION follows.
156 #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).
157 #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.
158 // system set by switch function command (CMD6).
159 #define SD_CMD_ERASE ((uint8_t)38) // Reserved for SD security applications.
160 #define SD_CMD_FAST_IO ((uint8_t)39) // SD card doesn't support it (Reserved).
161 #define SD_CMD_APP_CMD ((uint8_t)55) // Indicates to the card that the next command is an application specific command rather
162 // than a standard command.
164 /* Following commands are SD Card Specific commands.
165 SDMMC_APP_CMD should be sent before sending these commands. */
166 #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
167 // widths are given in SCR register.
168 #define SD_CMD_SD_APP_STATUS ((uint8_t)13) // (ACMD13) Sends the SD status.
169 #define SD_CMD_SD_APP_OP_COND ((uint8_t)41) // (ACMD41) Sends host capacity support information (HCS) and asks the accessed card to
170 // send its operating condition register (OCR) content in the response on the CMD line.
171 #define SD_CMD_SD_APP_SEND_SCR ((uint8_t)51) // Reads the SD Configuration Register (SCR).
173 #define SDMMC_DIR_TX 1
174 #define SDMMC_DIR_RX 0
176 #define SDMMC_DMA_ST3 1
178 /* Typedef(s) -------------------------------------------------------------------------------------------------------*/
182 SD_SINGLE_BLOCK
= 0, // Single block operation
183 SD_MULTIPLE_BLOCK
= 1, // Multiple blocks operation
188 uint32_t CSD
[4]; // SD card specific data table
189 uint32_t CID
[4]; // SD card identification number table
190 volatile uint32_t TransferComplete
; // SD transfer complete flag in non blocking mode
191 volatile uint32_t TransferError
; // SD transfer error flag in non blocking mode
192 volatile uint32_t RXCplt
; // SD RX Complete is equal 0 when no transfer
193 volatile uint32_t TXCplt
; // SD TX Complete is equal 0 when no transfer
194 volatile uint32_t Operation
; // SD transfer operation (read/write)
199 SD_CARD_READY
= ((uint32_t)0x00000001), // Card state is ready
200 SD_CARD_IDENTIFICATION
= ((uint32_t)0x00000002), // Card is in identification state
201 SD_CARD_STANDBY
= ((uint32_t)0x00000003), // Card is in standby state
202 SD_CARD_TRANSFER
= ((uint32_t)0x00000004), // Card is in transfer state
203 SD_CARD_SENDING
= ((uint32_t)0x00000005), // Card is sending an operation
204 SD_CARD_RECEIVING
= ((uint32_t)0x00000006), // Card is receiving operation information
205 SD_CARD_PROGRAMMING
= ((uint32_t)0x00000007), // Card is in programming state
206 SD_CARD_DISCONNECTED
= ((uint32_t)0x00000008), // Card is disconnected
207 SD_CARD_ERROR
= ((uint32_t)0x000000FF) // Card is in error state
210 /* Variable(s) ------------------------------------------------------------------------------------------------------*/
212 static SD_Handle_t SD_Handle
;
213 SD_CardInfo_t SD_CardInfo
;
214 static uint32_t SD_Status
;
215 static uint32_t SD_CardRCA
;
216 SD_CardType_t SD_CardType
;
217 static volatile uint32_t TimeOut
;
218 DMA_Stream_TypeDef
*dmaStream
;
220 /* Private function(s) ----------------------------------------------------------------------------------------------*/
222 static void SD_DataTransferInit (uint32_t Size
, uint32_t DataBlockSize
, bool IsItReadFromCard
);
223 static SD_Error_t
SD_TransmitCommand (uint32_t Command
, uint32_t Argument
, int8_t ResponseType
);
224 static SD_Error_t
SD_CmdResponse (uint8_t SD_CMD
, int8_t ResponseType
);
225 static void SD_GetResponse (uint32_t* pResponse
);
226 static SD_Error_t
CheckOCR_Response (uint32_t Response_R1
);
227 static void SD_DMA_Complete (DMA_Stream_TypeDef
* pDMA_Stream
);
228 static SD_Error_t
SD_InitializeCard (void);
230 static SD_Error_t
SD_PowerON (void);
231 static SD_Error_t
SD_WideBusOperationConfig (uint32_t WideMode
);
232 static SD_Error_t
SD_FindSCR (uint32_t *pSCR
);
234 void SDMMC_DMA_ST3_IRQHandler(dmaChannelDescriptor_t
*dma
);
235 void SDMMC_DMA_ST6_IRQHandler(dmaChannelDescriptor_t
*dma
);
237 //static void SD_PowerOFF (void);
239 /** -----------------------------------------------------------------------------------------------------------------*/
242 * @brief Prepare the state machine for transfer
243 * @param SD_TransferType_e TransfertDir
244 * @param SD_CARD_BlockSize_e Size
246 static void SD_DataTransferInit(uint32_t Size
, uint32_t DataBlockSize
, bool IsItReadFromCard
)
250 SDMMC1
->DTIMER
= SD_DATATIMEOUT
; // Set the SDMMC1 Data TimeOut value
251 SDMMC1
->DLEN
= Size
; // Set the SDMMC1 DataLength value
252 Direction
= (IsItReadFromCard
== true) ? SDMMC_DCTRL_DTDIR
: 0;
253 SDMMC1
->DCTRL
|= (uint32_t)(DataBlockSize
| Direction
| SDMMC_DCTRL_DTEN
| 0x01);
257 /** -----------------------------------------------------------------------------------------------------------------*/
258 /** SD_TransmitCommand
260 * @brief Send the commande to SDMMC1
261 * @param uint32_t Command
262 * @param uint32_t Argument Must provide the response size
263 * @param uint8_t ResponseType
264 * @retval SD Card error state
266 static SD_Error_t
SD_TransmitCommand(uint32_t Command
, uint32_t Argument
, int8_t ResponseType
)
268 SD_Error_t ErrorState
;
270 WRITE_REG(SDMMC1
->ICR
, SDMMC_ICR_STATIC_FLAGS
); // Clear the Command Flags
271 WRITE_REG(SDMMC1
->ARG
, (uint32_t)Argument
); // Set the SDMMC1 Argument value
272 WRITE_REG(SDMMC1
->CMD
, (uint32_t)(Command
| SDMMC_CMD_CPSMEN
)); // Set SDMMC1 command parameters
273 if((Argument
== 0) && (ResponseType
== 0)) ResponseType
= -1; // Go idle command
274 ErrorState
= SD_CmdResponse(Command
& SDMMC_CMD_CMDINDEX
, ResponseType
);
275 WRITE_REG(SDMMC1
->ICR
, SDMMC_ICR_STATIC_FLAGS
); // Clear the Command Flags
280 /** -----------------------------------------------------------------------------------------------------------------*/
282 * @brief Checks for error conditions for any response.
283 * - R2 (CID or CSD) response.
284 * - R3 (OCR) response.
286 * @param SD_CMD: The sent command Index
287 * @retval SD Card error state
289 static SD_Error_t
SD_CmdResponse(uint8_t SD_CMD
, int8_t ResponseType
)
291 uint32_t Response_R1
;
295 if(ResponseType
== -1) {
296 Flag
= SDMMC_STA_CMDSENT
;
298 Flag
= SDMMC_STA_CCRCFAIL
| SDMMC_STA_CMDREND
| SDMMC_STA_CTIMEOUT
;
301 TimeOut
= SD_SOFTWARE_COMMAND_TIMEOUT
;
304 SD_Status
= SDMMC1
->STA
;
307 while(((SD_Status
& Flag
) == 0) && (TimeOut
> 0));
309 if(ResponseType
<= 0)
312 return SD_CMD_RSP_TIMEOUT
;
318 if((SDMMC1
->STA
& SDMMC_STA_CTIMEOUT
) != 0) {
319 return SD_CMD_RSP_TIMEOUT
;
321 if(ResponseType
== 3)
324 return SD_CMD_RSP_TIMEOUT
; // Card is not V2.0 compliant or card does not support the set voltage range
326 return SD_OK
; // Card is SD V2.0 compliant
330 if((SDMMC1
->STA
& SDMMC_STA_CCRCFAIL
) != 0) {
331 return SD_CMD_CRC_FAIL
;
333 if(ResponseType
== 2) {
336 if((uint8_t)SDMMC1
->RESPCMD
!= SD_CMD
) {
337 return SD_ILLEGAL_CMD
; // Check if response is of desired command
340 Response_R1
= SDMMC1
->RESP1
; // We have received response, retrieve it for analysis
342 if(ResponseType
== 1)
344 return CheckOCR_Response(Response_R1
);
346 else if(ResponseType
== 6)
348 if((Response_R1
& (SD_R6_GENERAL_UNKNOWN_ERROR
| SD_R6_ILLEGAL_CMD
| SD_R6_COM_CRC_FAILED
)) == SD_ALLZERO
)
350 SD_CardRCA
= Response_R1
;
352 if((Response_R1
& SD_R6_GENERAL_UNKNOWN_ERROR
) == SD_R6_GENERAL_UNKNOWN_ERROR
) {
353 return SD_GENERAL_UNKNOWN_ERROR
;
355 if((Response_R1
& SD_R6_ILLEGAL_CMD
) == SD_R6_ILLEGAL_CMD
) {
356 return SD_ILLEGAL_CMD
;
358 if((Response_R1
& SD_R6_COM_CRC_FAILED
) == SD_R6_COM_CRC_FAILED
) {
359 return SD_COM_CRC_FAILED
;
366 /** -----------------------------------------------------------------------------------------------------------------*/
368 * @brief Analyze the OCR response and return the appropriate error code
369 * @param Response_R1: OCR Response code
370 * @retval SD Card error state
372 static SD_Error_t
CheckOCR_Response(uint32_t Response_R1
)
374 if((Response_R1
& SD_OCR_ERRORBITS
) == SD_ALLZERO
) return SD_OK
;
375 if((Response_R1
& SD_OCR_ADDR_OUT_OF_RANGE
) == SD_OCR_ADDR_OUT_OF_RANGE
) return SD_ADDR_OUT_OF_RANGE
;
376 if((Response_R1
& SD_OCR_ADDR_MISALIGNED
) == SD_OCR_ADDR_MISALIGNED
) return SD_ADDR_MISALIGNED
;
377 if((Response_R1
& SD_OCR_BLOCK_LEN_ERR
) == SD_OCR_BLOCK_LEN_ERR
) return SD_BLOCK_LEN_ERR
;
378 if((Response_R1
& SD_OCR_ERASE_SEQ_ERR
) == SD_OCR_ERASE_SEQ_ERR
) return SD_ERASE_SEQ_ERR
;
379 if((Response_R1
& SD_OCR_BAD_ERASE_PARAM
) == SD_OCR_BAD_ERASE_PARAM
) return SD_BAD_ERASE_PARAM
;
380 if((Response_R1
& SD_OCR_WRITE_PROT_VIOLATION
) == SD_OCR_WRITE_PROT_VIOLATION
) return SD_WRITE_PROT_VIOLATION
;
381 if((Response_R1
& SD_OCR_LOCK_UNLOCK_FAILED
) == SD_OCR_LOCK_UNLOCK_FAILED
) return SD_LOCK_UNLOCK_FAILED
;
382 if((Response_R1
& SD_OCR_COM_CRC_FAILED
) == SD_OCR_COM_CRC_FAILED
) return SD_COM_CRC_FAILED
;
383 if((Response_R1
& SD_OCR_ILLEGAL_CMD
) == SD_OCR_ILLEGAL_CMD
) return SD_ILLEGAL_CMD
;
384 if((Response_R1
& SD_OCR_CARD_ECC_FAILED
) == SD_OCR_CARD_ECC_FAILED
) return SD_CARD_ECC_FAILED
;
385 if((Response_R1
& SD_OCR_CC_ERROR
) == SD_OCR_CC_ERROR
) return SD_CC_ERROR
;
386 if((Response_R1
& SD_OCR_GENERAL_UNKNOWN_ERROR
) == SD_OCR_GENERAL_UNKNOWN_ERROR
)return SD_GENERAL_UNKNOWN_ERROR
;
387 if((Response_R1
& SD_OCR_STREAM_READ_UNDERRUN
) == SD_OCR_STREAM_READ_UNDERRUN
) return SD_STREAM_READ_UNDERRUN
;
388 if((Response_R1
& SD_OCR_STREAM_WRITE_OVERRUN
) == SD_OCR_STREAM_WRITE_OVERRUN
) return SD_STREAM_WRITE_OVERRUN
;
389 if((Response_R1
& SD_OCR_CID_CSD_OVERWRITE
) == SD_OCR_CID_CSD_OVERWRITE
) return SD_CID_CSD_OVERWRITE
;
390 if((Response_R1
& SD_OCR_WP_ERASE_SKIP
) == SD_OCR_WP_ERASE_SKIP
) return SD_WP_ERASE_SKIP
;
391 if((Response_R1
& SD_OCR_CARD_ECC_DISABLED
) == SD_OCR_CARD_ECC_DISABLED
) return SD_CARD_ECC_DISABLED
;
392 if((Response_R1
& SD_OCR_ERASE_RESET
) == SD_OCR_ERASE_RESET
) return SD_ERASE_RESET
;
393 if((Response_R1
& SD_OCR_AKE_SEQ_ERROR
) == SD_OCR_AKE_SEQ_ERROR
) return SD_AKE_SEQ_ERROR
;
398 /** -----------------------------------------------------------------------------------------------------------------*/
401 * @brief Get response from SD device
402 * @param uint32_t* pResponse
404 static void SD_GetResponse(uint32_t* pResponse
)
406 pResponse
[0] = SDMMC1
->RESP1
;
407 pResponse
[1] = SDMMC1
->RESP2
;
408 pResponse
[2] = SDMMC1
->RESP3
;
409 pResponse
[3] = SDMMC1
->RESP4
;
412 /** -----------------------------------------------------------------------------------------------------------------*/
414 * @brief SD DMA transfer complete RX and TX callback.
415 * @param DMA_Stream_TypeDef* pDMA_Stream
417 static void SD_DMA_Complete(DMA_Stream_TypeDef
* pDMA_Stream
)
419 if (SD_Handle
.RXCplt
) {
420 if (SD_Handle
.Operation
== ((SDMMC_DIR_RX
<< 1) | SD_MULTIPLE_BLOCK
)) {
421 /* Send stop command in multiblock write */
422 SD_TransmitCommand((SD_CMD_STOP_TRANSMISSION
| SD_CMD_RESPONSE_SHORT
), 0, 1);
425 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
426 in the SD DCTRL register */
427 SDMMC1
->DCTRL
&= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN
);
429 /* Clear all the static flags */
430 SDMMC1
->ICR
= SDMMC_ICR_STATIC_FLAGS
;
433 SD_Handle
.RXCplt
= 0;
435 /* Disable the stream */
436 pDMA_Stream
->CR
&= ~DMA_SxCR_EN
;
438 /* Enable Dataend IE */
439 SDMMC1
->MASK
|= SDMMC_MASK_DATAENDIE
;
443 /** -----------------------------------------------------------------------------------------------------------------*/
445 * @brief Initializes all cards or single card as the case may be Card(s) come
446 * into standby state.
447 * @retval SD Card error state
449 static SD_Error_t
SD_InitializeCard(void)
451 SD_Error_t ErrorState
= SD_OK
;
453 if((SDMMC1
->POWER
& SDMMC_POWER_PWRCTRL
) != 0) // Power off
455 if(SD_CardType
!= SD_SECURE_DIGITAL_IO
)
457 // Send CMD2 ALL_SEND_CID
458 if((ErrorState
= SD_TransmitCommand((SD_CMD_ALL_SEND_CID
| SD_CMD_RESPONSE_LONG
), 0, 2)) != SD_OK
)
463 // Get Card identification number data
464 SD_GetResponse(SD_Handle
.CID
);
467 if((SD_CardType
== SD_STD_CAPACITY_V1_1
) || (SD_CardType
== SD_STD_CAPACITY_V2_0
) ||
468 (SD_CardType
== SD_SECURE_DIGITAL_IO_COMBO
) || (SD_CardType
== SD_HIGH_CAPACITY
))
470 // Send CMD3 SET_REL_ADDR with argument 0
471 // SD Card publishes its RCA.
472 if((ErrorState
= SD_TransmitCommand((SD_CMD_SET_REL_ADDR
| SD_CMD_RESPONSE_SHORT
), 0, 6)) != SD_OK
)
478 if(SD_CardType
!= SD_SECURE_DIGITAL_IO
)
480 // Send CMD9 SEND_CSD with argument as card's RCA
481 if((ErrorState
= SD_TransmitCommand((SD_CMD_SEND_CSD
| SD_CMD_RESPONSE_LONG
), SD_CardRCA
, 2)) == SD_OK
)
483 // Get Card Specific Data
484 SD_GetResponse(SD_Handle
.CSD
);
490 ErrorState
= SD_REQUEST_NOT_APPLICABLE
;
496 /** -----------------------------------------------------------------------------------------------------------------*/
498 * @brief Prepre the DMA transfer
499 * @param pDMA: DMA Stream to use for the DMA operation
500 * @param pBuffer: Pointer to the buffer that will contain the data to transmit
501 * @param BlockSize: The SD card Data block size
502 * @note BlockSize must be 512 bytes.
503 * @param NumberOfBlocks: Number of blocks to write
504 * @retval SD Card error state
506 static void SD_StartBlockTransfert(uint32_t* pBuffer
, uint32_t BlockSize
, uint32_t NumberOfBlocks
, uint8_t dir
)
508 DMA_Stream_TypeDef
*pDMA
= dmaStream
;
510 SDMMC1
->DCTRL
= 0; // Initialize data control register
511 SD_Handle
.TransferComplete
= 0; // Initialize handle flags
512 SD_Handle
.TransferError
= SD_OK
;
513 SD_Handle
.Operation
= (NumberOfBlocks
> 1) ? SD_MULTIPLE_BLOCK
: SD_SINGLE_BLOCK
; // Initialize SD Read operation
514 SD_Handle
.Operation
|= dir
<< 1;
516 if (dir
== SDMMC_DIR_RX
) {
517 SDMMC1
->MASK
|= (SDMMC_MASK_DCRCFAILIE
| SDMMC_MASK_DTIMEOUTIE
| // Enable transfer interrupts
518 SDMMC_MASK_DATAENDIE
| SDMMC_MASK_RXOVERRIE
);
520 SDMMC1
->MASK
|= (SDMMC_MASK_DCRCFAILIE
| SDMMC_MASK_DTIMEOUTIE
| // Enable transfer interrupts
521 SDMMC_MASK_TXUNDERRIE
);
523 if (dir
== SDMMC_DIR_TX
) {
524 SDMMC1
->DCTRL
|= SDMMC_DCTRL_DMAEN
; // Enable SDMMC1 DMA transfer
526 pDMA
->CR
&= ~DMA_SxCR_EN
; // Disable the Peripheral
527 while (pDMA
->CR
& DMA_SxCR_EN
);
528 pDMA
->NDTR
= (uint32_t) (BlockSize
* NumberOfBlocks
) / 4; // Configure DMA Stream data length
529 pDMA
->M0AR
= (uint32_t) pBuffer
; // Configure DMA Stream memory address
530 if (dir
== SDMMC_DIR_RX
) {
531 pDMA
->CR
&= ~(0x01U
<< 6U); // Sets peripheral to memory
533 pDMA
->CR
|= DMA_MEMORY_TO_PERIPH
; // Sets memory to peripheral
535 if (dmaStream
== DMA2_Stream3
) {
536 DMA2
->LIFCR
= DMA_LIFCR_CTEIF3
| DMA_LIFCR_CDMEIF3
|
537 DMA_LIFCR_CFEIF3
| DMA_LIFCR_CHTIF3
| DMA_LIFCR_CTCIF3
; // Clear the transfer error flag
539 DMA2
->HIFCR
= DMA_HIFCR_CTEIF6
| DMA_HIFCR_CDMEIF6
|
540 DMA_HIFCR_CFEIF6
| DMA_HIFCR_CHTIF6
| DMA_HIFCR_CTCIF6
; // Clear the transfer error flag
542 pDMA
->CR
|= DMA_SxCR_TCIE
| DMA_SxCR_HTIE
| DMA_SxCR_TEIE
| DMA_SxCR_DMEIE
; // Enable all interrupts
543 pDMA
->FCR
|= DMA_SxFCR_FEIE
;
544 pDMA
->CR
|= DMA_SxCR_EN
; // Enable the Peripheral
545 if (dir
== SDMMC_DIR_RX
) {
546 SDMMC1
->DCTRL
|= SDMMC_DCTRL_DMAEN
; // Enable SDMMC1 DMA transfer
550 /** -----------------------------------------------------------------------------------------------------------------*/
552 * @brief Reads block(s) from a specified address in a card. The Data transfer
553 * is managed by DMA mode.
554 * @note This API should be followed by the function SD_CheckOperation()
555 * to check the completion of the read process
556 * @param pReadBuffer: Pointer to the buffer that will contain the received data
557 * @param ReadAddr: Address from where data is to be read
558 * @param BlockSize: SD card Data block size
559 * @note BlockSize must be 512 bytes.
560 * @param NumberOfBlocks: Number of blocks to read.
561 * @retval SD Card error state
563 SD_Error_t
SD_ReadBlocks_DMA(uint64_t ReadAddress
, uint32_t *buffer
, uint32_t BlockSize
, uint32_t NumberOfBlocks
)
565 SD_Error_t ErrorState
;
567 SD_Handle
.RXCplt
= 1;
569 //printf("Reading at %ld into %p %ld blocks\n", (uint32_t)ReadAddress, (void*)buffer, NumberOfBlocks);
571 if(SD_CardType
!= SD_HIGH_CAPACITY
)
576 SD_StartBlockTransfert(buffer
, BlockSize
, NumberOfBlocks
, SDMMC_DIR_RX
);
578 // Configure the SD DPSM (Data Path State Machine)
579 SD_DataTransferInit(BlockSize
* NumberOfBlocks
, SD_DATABLOCK_SIZE_512B
, true);
581 // Set Block Size for Card
582 ErrorState
= SD_TransmitCommand((SD_CMD_SET_BLOCKLEN
| SD_CMD_RESPONSE_SHORT
), BlockSize
, 1);
584 // Send CMD18 READ_MULT_BLOCK with argument data address
585 // or send CMD17 READ_SINGLE_BLOCK depending on number of block
586 uint8_t retries
= 10;
587 CmdIndex
= (NumberOfBlocks
> 1) ? SD_CMD_READ_MULT_BLOCK
: SD_CMD_READ_SINGLE_BLOCK
;
589 ErrorState
= SD_TransmitCommand((CmdIndex
| SD_CMD_RESPONSE_SHORT
), (uint32_t)ReadAddress
, 1);
590 if (ErrorState
!= SD_OK
&& retries
--) {
591 ErrorState
= SD_TransmitCommand((SD_CMD_APP_CMD
| SD_CMD_RESPONSE_SHORT
), 0, 1);
593 } while (ErrorState
!= SD_OK
&& retries
);
595 if (ErrorState
!= SD_OK
) {
596 SD_Handle
.RXCplt
= 0;
599 // Update the SD transfer error in SD handle
600 SD_Handle
.TransferError
= ErrorState
;
605 /** -----------------------------------------------------------------------------------------------------------------*/
607 * @brief Writes block(s) to a specified address in a card. The Data transfer
608 * is managed by DMA mode.
609 * @note This API should be followed by the function SD_CheckOperation()
610 * to check the completion of the write process (by SD current status polling).
611 * @param pWriteBuffer: pointer to the buffer that will contain the data to transmit
612 * @param WriteAddress: Address from where data is to be read
613 * @param BlockSize: the SD card Data block size
614 * @note BlockSize must be 512 bytes.
615 * @param NumberOfBlocks: Number of blocks to write
616 * @retval SD Card error state
618 SD_Error_t
SD_WriteBlocks_DMA(uint64_t WriteAddress
, uint32_t *buffer
, uint32_t BlockSize
, uint32_t NumberOfBlocks
)
620 SD_Error_t ErrorState
;
622 SD_Handle
.TXCplt
= 1;
624 //printf("Reading at %ld into %p %ld blocks\n", (uint32_t)WriteAddress, (void*)buffer, NumberOfBlocks);
626 if(SD_CardType
!= SD_HIGH_CAPACITY
)
631 // Check number of blocks command
632 // Send CMD24 WRITE_SINGLE_BLOCK
633 // Send CMD25 WRITE_MULT_BLOCK with argument data address
634 CmdIndex
= (NumberOfBlocks
> 1) ? SD_CMD_WRITE_MULT_BLOCK
: SD_CMD_WRITE_SINGLE_BLOCK
;
636 // Set Block Size for Card
637 uint8_t retries
= 10;
639 ErrorState
= SD_TransmitCommand((CmdIndex
| SD_CMD_RESPONSE_SHORT
), (uint32_t)WriteAddress
, 1);
640 if (ErrorState
!= SD_OK
&& retries
--) {
641 ErrorState
= SD_TransmitCommand((SD_CMD_APP_CMD
| SD_CMD_RESPONSE_SHORT
), 0, 1);
643 } while(ErrorState
!= SD_OK
&& retries
);
645 if (ErrorState
!= SD_OK
) {
646 SD_Handle
.TXCplt
= 0;
650 SD_StartBlockTransfert(buffer
, BlockSize
, NumberOfBlocks
, SDMMC_DIR_TX
);
652 // Configure the SD DPSM (Data Path State Machine)
653 SD_DataTransferInit(BlockSize
* NumberOfBlocks
, SD_DATABLOCK_SIZE_512B
, false);
655 SD_Handle
.TransferError
= ErrorState
;
660 SD_Error_t
SD_CheckWrite(void)
662 if (SD_Handle
.TXCplt
!= 0) return SD_BUSY
;
666 SD_Error_t
SD_CheckRead(void)
668 if (SD_Handle
.RXCplt
!= 0) return SD_BUSY
;
672 /** -----------------------------------------------------------------------------------------------------------------*/
674 * @brief This function waits until the SD DMA data wirte or read transfer is finished.
675 * This should be called after WriteBlocks_DMA or SD_ReadBlocks_DMA() function
676 * to insure that all data sent is already transferred by the DMA controller.
677 * @retval SD Card error state
680 SD_Error_t SD_CheckOperation(uint32_t Flag)
682 SD_Error_t ErrorState = SD_OK;
688 // Wait for DMA/SD transfer end or SD error variables to be in SD handle
689 Temp1 = SD_Handle.DMA_XferComplete;
690 Temp2 = SD_Handle.TransferComplete;
691 Temp3 = (SD_Error_t)SD_Handle.TransferError;
693 if (((Temp1 & Temp2) == 0) && (Temp3 == SD_OK) && (TimeOut > 0))
695 Temp1 = SD_Handle.DMA_XferComplete;
696 Temp2 = SD_Handle.TransferComplete;
697 Temp3 = (SD_Error_t)SD_Handle.TransferError;
702 // Wait until the Rx transfer is no longer active
703 if (((SDMMC1->STA & Flag) != 0) && (TimeOut > 0))
709 // Send stop command in multi block read
710 if(SD_Handle.Operation & 0x01 == SD_MULTIPLE_BLOCK)
712 ErrorState = SD_TransmitCommand((SD_CMD_STOP_TRANSMISSION | SD_CMD_RESPONSE_SHORT), 0, 1);
715 if((TimeOut == 0) && (ErrorState == SD_OK))
717 ErrorState = SD_DATA_TIMEOUT;
720 // Return error state
721 if(SD_Handle.TransferError != SD_OK)
723 return (SD_Error_t)(SD_Handle.TransferError);
730 /** -----------------------------------------------------------------------------------------------------------------*/
732 * @brief Erases the specified memory area of the given SD card.
733 * @param StartAddress: Start byte address
734 * @param EndAddress: End byte address
735 * @retval SD Card error state
738 SD_Error_t SD_Erase(uint64_t StartAddress, uint64_t EndAddress)
740 SD_Error_t ErrorState;
745 // Check if the card command class supports erase command
746 if(((SD_Handle.CSD[1] >> 20) & SD_CCCC_ERASE) == 0)
748 return SD_REQUEST_NOT_APPLICABLE;
751 // Get max delay value
752 MaxDelay = 120000 / (((SDMMC1->CLKCR) & 0xFF) + 2);
754 if((SDMMC1->RESP1 & SD_CARD_LOCKED) == SD_CARD_LOCKED)
756 return SD_LOCK_UNLOCK_FAILED;
759 // Get start and end block for high capacity cards
760 if(SD_CardType == SD_HIGH_CAPACITY)
766 // According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33)
767 if ((SD_CardType == SD_STD_CAPACITY_V1_1) || (SD_CardType == SD_STD_CAPACITY_V2_0) ||
768 (SD_CardType == SD_HIGH_CAPACITY))
770 // Send CMD32 SD_ERASE_GRP_START with argument as addr
771 if((ErrorState = SD_TransmitCommand((SD_CMD_SD_ERASE_GRP_START | SDMMC_CMD_RESPONSE_SHORT), (uint32_t)StartAddress, 1)) != SD_OK)
776 // Send CMD33 SD_ERASE_GRP_END with argument as addr
777 if((ErrorState = SD_TransmitCommand((SD_CMD_SD_ERASE_GRP_END | SDMMC_CMD_RESPONSE_SHORT), (uint32_t)EndAddress, 1)) != SD_OK)
784 if((ErrorState = SD_TransmitCommand((SD_CMD_ERASE | SDMMC_CMD_RESPONSE_SHORT), 0, 1)) != SD_OK)
789 for(Delay = 0; Delay < MaxDelay; Delay++);
791 // Wait until the card is in programming state
792 ErrorState = SD_IsCardProgramming(&CardState);
794 Delay = SD_DATATIMEOUT;
795 while((Delay > 0) && (ErrorState == SD_OK) && ((CardState == SD_CARD_PROGRAMMING) || (CardState == SD_CARD_RECEIVING)))
797 ErrorState = SD_IsCardProgramming( &CardState);
805 /** -----------------------------------------------------------------------------------------------------------------*/
807 * @brief Returns information about specific card.
808 * contains all SD cardinformation
809 * @retval SD Card error state
811 SD_Error_t
SD_GetCardInfo(void)
813 SD_Error_t ErrorState
= SD_OK
;
817 Temp
= (SD_Handle
.CSD
[0] & 0xFF000000) >> 24;
818 SD_CardInfo
.SD_csd
.CSDStruct
= (uint8_t)((Temp
& 0xC0) >> 6);
819 SD_CardInfo
.SD_csd
.SysSpecVersion
= (uint8_t)((Temp
& 0x3C) >> 2);
820 SD_CardInfo
.SD_csd
.Reserved1
= Temp
& 0x03;
823 Temp
= (SD_Handle
.CSD
[0] & 0x00FF0000) >> 16;
824 SD_CardInfo
.SD_csd
.TAAC
= (uint8_t)Temp
;
827 Temp
= (SD_Handle
.CSD
[0] & 0x0000FF00) >> 8;
828 SD_CardInfo
.SD_csd
.NSAC
= (uint8_t)Temp
;
831 Temp
= SD_Handle
.CSD
[0] & 0x000000FF;
832 SD_CardInfo
.SD_csd
.MaxBusClkFrec
= (uint8_t)Temp
;
835 Temp
= (SD_Handle
.CSD
[1] & 0xFF000000) >> 24;
836 SD_CardInfo
.SD_csd
.CardComdClasses
= (uint16_t)(Temp
<< 4);
839 Temp
= (SD_Handle
.CSD
[1] & 0x00FF0000) >> 16;
840 SD_CardInfo
.SD_csd
.CardComdClasses
|= (uint16_t)((Temp
& 0xF0) >> 4);
841 SD_CardInfo
.SD_csd
.RdBlockLen
= (uint8_t)(Temp
& 0x0F);
844 Temp
= (SD_Handle
.CSD
[1] & 0x0000FF00) >> 8;
845 SD_CardInfo
.SD_csd
.PartBlockRead
= (uint8_t)((Temp
& 0x80) >> 7);
846 SD_CardInfo
.SD_csd
.WrBlockMisalign
= (uint8_t)((Temp
& 0x40) >> 6);
847 SD_CardInfo
.SD_csd
.RdBlockMisalign
= (uint8_t)((Temp
& 0x20) >> 5);
848 SD_CardInfo
.SD_csd
.DSRImpl
= (uint8_t)((Temp
& 0x10) >> 4);
849 SD_CardInfo
.SD_csd
.Reserved2
= 0; /*!< Reserved */
851 if((SD_CardType
== SD_STD_CAPACITY_V1_1
) || (SD_CardType
== SD_STD_CAPACITY_V2_0
))
853 SD_CardInfo
.SD_csd
.DeviceSize
= (Temp
& 0x03) << 10;
856 Temp
= (uint8_t)(SD_Handle
.CSD
[1] & 0x000000FF);
857 SD_CardInfo
.SD_csd
.DeviceSize
|= (Temp
) << 2;
860 Temp
= (uint8_t)((SD_Handle
.CSD
[2] & 0xFF000000) >> 24);
861 SD_CardInfo
.SD_csd
.DeviceSize
|= (Temp
& 0xC0) >> 6;
863 SD_CardInfo
.SD_csd
.MaxRdCurrentVDDMin
= (Temp
& 0x38) >> 3;
864 SD_CardInfo
.SD_csd
.MaxRdCurrentVDDMax
= (Temp
& 0x07);
867 Temp
= (uint8_t)((SD_Handle
.CSD
[2] & 0x00FF0000) >> 16);
868 SD_CardInfo
.SD_csd
.MaxWrCurrentVDDMin
= (Temp
& 0xE0) >> 5;
869 SD_CardInfo
.SD_csd
.MaxWrCurrentVDDMax
= (Temp
& 0x1C) >> 2;
870 SD_CardInfo
.SD_csd
.DeviceSizeMul
= (Temp
& 0x03) << 1;
873 Temp
= (uint8_t)((SD_Handle
.CSD
[2] & 0x0000FF00) >> 8);
874 SD_CardInfo
.SD_csd
.DeviceSizeMul
|= (Temp
& 0x80) >> 7;
876 SD_CardInfo
.CardCapacity
= (SD_CardInfo
.SD_csd
.DeviceSize
+ 1) ;
877 SD_CardInfo
.CardCapacity
*= (1 << (SD_CardInfo
.SD_csd
.DeviceSizeMul
+ 2));
878 SD_CardInfo
.CardBlockSize
= 1 << (SD_CardInfo
.SD_csd
.RdBlockLen
);
879 SD_CardInfo
.CardCapacity
= SD_CardInfo
.CardCapacity
* SD_CardInfo
.CardBlockSize
/ 512; // In 512 byte blocks
881 else if(SD_CardType
== SD_HIGH_CAPACITY
)
884 Temp
= (uint8_t)(SD_Handle
.CSD
[1] & 0x000000FF);
885 SD_CardInfo
.SD_csd
.DeviceSize
= (Temp
& 0x3F) << 16;
888 Temp
= (uint8_t)((SD_Handle
.CSD
[2] & 0xFF000000) >> 24);
890 SD_CardInfo
.SD_csd
.DeviceSize
|= (Temp
<< 8);
893 Temp
= (uint8_t)((SD_Handle
.CSD
[2] & 0x00FF0000) >> 16);
895 SD_CardInfo
.SD_csd
.DeviceSize
|= (Temp
);
898 Temp
= (uint8_t)((SD_Handle
.CSD
[2] & 0x0000FF00) >> 8);
900 SD_CardInfo
.CardCapacity
= ((uint64_t)SD_CardInfo
.SD_csd
.DeviceSize
+ 1) * 1024;
901 SD_CardInfo
.CardBlockSize
= 512;
905 // Not supported card type
906 ErrorState
= SD_ERROR
;
909 SD_CardInfo
.SD_csd
.EraseGrSize
= (Temp
& 0x40) >> 6;
910 SD_CardInfo
.SD_csd
.EraseGrMul
= (Temp
& 0x3F) << 1;
913 Temp
= (uint8_t)(SD_Handle
.CSD
[2] & 0x000000FF);
914 SD_CardInfo
.SD_csd
.EraseGrMul
|= (Temp
& 0x80) >> 7;
915 SD_CardInfo
.SD_csd
.WrProtectGrSize
= (Temp
& 0x7F);
918 Temp
= (uint8_t)((SD_Handle
.CSD
[3] & 0xFF000000) >> 24);
919 SD_CardInfo
.SD_csd
.WrProtectGrEnable
= (Temp
& 0x80) >> 7;
920 SD_CardInfo
.SD_csd
.ManDeflECC
= (Temp
& 0x60) >> 5;
921 SD_CardInfo
.SD_csd
.WrSpeedFact
= (Temp
& 0x1C) >> 2;
922 SD_CardInfo
.SD_csd
.MaxWrBlockLen
= (Temp
& 0x03) << 2;
925 Temp
= (uint8_t)((SD_Handle
.CSD
[3] & 0x00FF0000) >> 16);
926 SD_CardInfo
.SD_csd
.MaxWrBlockLen
|= (Temp
& 0xC0) >> 6;
927 SD_CardInfo
.SD_csd
.WriteBlockPaPartial
= (Temp
& 0x20) >> 5;
928 SD_CardInfo
.SD_csd
.Reserved3
= 0;
929 SD_CardInfo
.SD_csd
.ContentProtectAppli
= (Temp
& 0x01);
932 Temp
= (uint8_t)((SD_Handle
.CSD
[3] & 0x0000FF00) >> 8);
933 SD_CardInfo
.SD_csd
.FileFormatGrouop
= (Temp
& 0x80) >> 7;
934 SD_CardInfo
.SD_csd
.CopyFlag
= (Temp
& 0x40) >> 6;
935 SD_CardInfo
.SD_csd
.PermWrProtect
= (Temp
& 0x20) >> 5;
936 SD_CardInfo
.SD_csd
.TempWrProtect
= (Temp
& 0x10) >> 4;
937 SD_CardInfo
.SD_csd
.FileFormat
= (Temp
& 0x0C) >> 2;
938 SD_CardInfo
.SD_csd
.ECC
= (Temp
& 0x03);
941 Temp
= (uint8_t)(SD_Handle
.CSD
[3] & 0x000000FF);
942 SD_CardInfo
.SD_csd
.CSD_CRC
= (Temp
& 0xFE) >> 1;
943 SD_CardInfo
.SD_csd
.Reserved4
= 1;
946 Temp
= (uint8_t)((SD_Handle
.CID
[0] & 0xFF000000) >> 24);
947 SD_CardInfo
.SD_cid
.ManufacturerID
= Temp
;
950 Temp
= (uint8_t)((SD_Handle
.CID
[0] & 0x00FF0000) >> 16);
951 SD_CardInfo
.SD_cid
.OEM_AppliID
= Temp
<< 8;
954 Temp
= (uint8_t)((SD_Handle
.CID
[0] & 0x000000FF00) >> 8);
955 SD_CardInfo
.SD_cid
.OEM_AppliID
|= Temp
;
958 Temp
= (uint8_t)(SD_Handle
.CID
[0] & 0x000000FF);
959 SD_CardInfo
.SD_cid
.ProdName1
= Temp
<< 24;
962 Temp
= (uint8_t)((SD_Handle
.CID
[1] & 0xFF000000) >> 24);
963 SD_CardInfo
.SD_cid
.ProdName1
|= Temp
<< 16;
966 Temp
= (uint8_t)((SD_Handle
.CID
[1] & 0x00FF0000) >> 16);
967 SD_CardInfo
.SD_cid
.ProdName1
|= Temp
<< 8;
970 Temp
= (uint8_t)((SD_Handle
.CID
[1] & 0x0000FF00) >> 8);
971 SD_CardInfo
.SD_cid
.ProdName1
|= Temp
;
974 Temp
= (uint8_t)(SD_Handle
.CID
[1] & 0x000000FF);
975 SD_CardInfo
.SD_cid
.ProdName2
= Temp
;
978 Temp
= (uint8_t)((SD_Handle
.CID
[2] & 0xFF000000) >> 24);
979 SD_CardInfo
.SD_cid
.ProdRev
= Temp
;
982 Temp
= (uint8_t)((SD_Handle
.CID
[2] & 0x00FF0000) >> 16);
983 SD_CardInfo
.SD_cid
.ProdSN
= Temp
<< 24;
986 Temp
= (uint8_t)((SD_Handle
.CID
[2] & 0x0000FF00) >> 8);
987 SD_CardInfo
.SD_cid
.ProdSN
|= Temp
<< 16;
990 Temp
= (uint8_t)(SD_Handle
.CID
[2] & 0x000000FF);
991 SD_CardInfo
.SD_cid
.ProdSN
|= Temp
<< 8;
994 Temp
= (uint8_t)((SD_Handle
.CID
[3] & 0xFF000000) >> 24);
995 SD_CardInfo
.SD_cid
.ProdSN
|= Temp
;
998 Temp
= (uint8_t)((SD_Handle
.CID
[3] & 0x00FF0000) >> 16);
999 SD_CardInfo
.SD_cid
.Reserved1
|= (Temp
& 0xF0) >> 4;
1000 SD_CardInfo
.SD_cid
.ManufactDate
= (Temp
& 0x0F) << 8;
1003 Temp
= (uint8_t)((SD_Handle
.CID
[3] & 0x0000FF00) >> 8);
1004 SD_CardInfo
.SD_cid
.ManufactDate
|= Temp
;
1007 Temp
= (uint8_t)(SD_Handle
.CID
[3] & 0x000000FF);
1008 SD_CardInfo
.SD_cid
.CID_CRC
= (Temp
& 0xFE) >> 1;
1009 SD_CardInfo
.SD_cid
.Reserved2
= 1;
1014 /** -----------------------------------------------------------------------------------------------------------------*/
1016 * @brief Enables wide bus operation for the requested card if supported by
1018 * @param WideMode: Specifies the SD card wide bus mode
1019 * This parameter can be one of the following values:
1020 * @arg SD_BUS_WIDE_8B: 8-bit data transfer (Only for MMC)
1021 * @arg SD_BUS_WIDE_4B: 4-bit data transfer
1022 * @arg SD_BUS_WIDE_1B: 1-bit data transfer
1023 * @retval SD Card error state
1025 static SD_Error_t
SD_WideBusOperationConfig(uint32_t WideMode
)
1027 SD_Error_t ErrorState
= SD_OK
;
1029 uint32_t SCR
[2] = {0, 0};
1031 if((SD_CardType
== SD_STD_CAPACITY_V1_1
) || (SD_CardType
== SD_STD_CAPACITY_V2_0
) ||\
1032 (SD_CardType
== SD_HIGH_CAPACITY
))
1034 if(WideMode
== SD_BUS_WIDE_8B
)
1036 ErrorState
= SD_UNSUPPORTED_FEATURE
;
1038 else if((WideMode
== SD_BUS_WIDE_4B
) ||
1039 (WideMode
== SD_BUS_WIDE_1B
))
1041 if((SDMMC1
->RESP1
& SD_CARD_LOCKED
) != SD_CARD_LOCKED
)
1044 ErrorState
= SD_FindSCR(SCR
);
1045 if(ErrorState
== SD_OK
)
1047 Temp
= (WideMode
== SD_BUS_WIDE_4B
) ? SD_WIDE_BUS_SUPPORT
: SD_SINGLE_BUS_SUPPORT
;
1049 // If requested card supports wide bus operation
1050 if((SCR
[1] & Temp
) != SD_ALLZERO
)
1052 // Send CMD55 APP_CMD with argument as card's RCA.
1053 ErrorState
= SD_TransmitCommand((SD_CMD_APP_CMD
| SD_CMD_RESPONSE_SHORT
), SD_CardRCA
, 1);
1054 if(ErrorState
== SD_OK
)
1056 Temp
= (WideMode
== SD_BUS_WIDE_4B
) ? 2 : 0;
1058 // Send ACMD6 APP_CMD with argument as 2 for wide bus mode
1059 ErrorState
= SD_TransmitCommand((SD_CMD_APP_SD_SET_BUSWIDTH
| SD_CMD_RESPONSE_SHORT
), Temp
, 1);
1064 ErrorState
= SD_REQUEST_NOT_APPLICABLE
;
1070 ErrorState
= SD_LOCK_UNLOCK_FAILED
;
1075 ErrorState
= SD_INVALID_PARAMETER
; // WideMode is not a valid argument
1078 if(ErrorState
== SD_OK
)
1080 // Configure the SDMMC1 peripheral, we need this delay for some reason...
1081 while ((READ_REG(SDMMC1
->CLKCR
) & 0x800) != WideMode
) {
1082 MODIFY_REG(SDMMC1
->CLKCR
, CLKCR_CLEAR_MASK
, (uint32_t) WideMode
);
1087 ErrorState
= SD_UNSUPPORTED_FEATURE
;
1093 /** -----------------------------------------------------------------------------------------------------------------*/
1095 * @brief Switches the SD card to High Speed mode.
1096 * This API must be used after "Transfer State"
1097 * @retval SD Card error state
1100 SD_Error_t HAL_SD_HighSpeed(void)
1102 SD_Error_t ErrorState;
1103 uint8_t SD_hs[64] = {0};
1104 uint32_t SD_scr[2] = {0, 0};
1105 uint32_t SD_SPEC = 0;
1107 uint32_t* Buffer = (uint32_t *)SD_hs;
1109 // Initialize the Data control register
1113 if((ErrorState = SD_FindSCR(SD_scr)) != SD_OK)
1118 // Test the Version supported by the card
1119 SD_SPEC = (SD_scr[1] & 0x01000000) | (SD_scr[1] & 0x02000000);
1121 if(SD_SPEC != SD_ALLZERO)
1123 // Set Block Size for Card
1124 if((ErrorState = SD_TransmitCommand((SD_CMD_SET_BLOCKLEN | SDMMC_CMD_RESPONSE_SHORT), 64, 1)) != SD_OK)
1129 // Configure the SD DPSM (Data Path State Machine)
1130 SD_DataTransferInit(64, SDMMC_DATABLOCK_SIZE_64B, true);
1132 // Send CMD6 switch mode
1133 if((ErrorState =SD_TransmitCommand((SD_CMD_HS_SWITCH | SDMMC_CMD_RESPONSE_SHORT), 0x80FFFF01, 1)) != SD_OK)
1138 while((SDMMC1->STA & (SDMMC_STA_RXOVERR | SDMMC_STA_DCRCFAIL | SDMMC_STA_DTIMEOUT | SDMMC_STA_DBCKEND)) == 0)
1140 if((SDMMC1->STA & SDMMC_STA_RXFIFOHF) != 0)
1142 for(Count = 0; Count < 8; Count++)
1144 *(Buffer + Count) = SDMMC1->FIFO;
1151 if((SDMMC1->STA & SDMMC_STA_DTIMEOUT) != 0) return SD_DATA_TIMEOUT;
1152 else if((SDMMC1->STA & SDMMC_STA_DCRCFAIL) != 0) return SD_DATA_CRC_FAIL;
1153 else if((SDMMC1->STA & SDMMC_STA_RXOVERR) != 0) return SD_RX_OVERRUN;
1155 Count = SD_DATATIMEOUT;
1157 while(((SDMMC1->STA & SDMMC_STA_RXDAVL) != 0) && (Count > 0))
1159 *Buffer = SDMMC1->FIFO;
1164 // Test if the switch mode HS is ok
1165 if((SD_hs[13] & 2) != 2)
1167 ErrorState = SD_UNSUPPORTED_FEATURE;
1176 /** -----------------------------------------------------------------------------------------------------------------*/
1178 * @brief Gets the current card's data status.
1179 * @retval Data Transfer state
1181 static SD_Error_t
SD_GetStatus(void)
1183 SD_Error_t ErrorState
;
1185 SD_CardState_t CardState
;
1187 // Send Status command
1188 if((ErrorState
= SD_TransmitCommand((SD_CMD_SEND_STATUS
| SD_CMD_RESPONSE_SHORT
), SD_CardRCA
, 1)) == SD_OK
)
1190 Response1
= SDMMC1
->RESP1
;
1191 CardState
= (SD_CardState_t
)((Response1
>> 9) & 0x0F);
1193 // Find SD status according to card state
1194 if (CardState
== SD_CARD_TRANSFER
) ErrorState
= SD_OK
;
1195 else if(CardState
== SD_CARD_ERROR
) ErrorState
= SD_ERROR
;
1196 else ErrorState
= SD_BUSY
;
1200 ErrorState
= SD_ERROR
;
1206 /** -----------------------------------------------------------------------------------------------------------------*/
1208 * @brief Gets the SD card status.
1209 * @retval SD Card error state
1211 SD_Error_t
SD_GetCardStatus(SD_CardStatus_t
* pCardStatus
)
1213 SD_Error_t ErrorState
;
1215 uint32_t Status
[16];
1218 // Check SD response
1219 if((SDMMC1
->RESP1
& SD_CARD_LOCKED
) == SD_CARD_LOCKED
)
1221 return SD_LOCK_UNLOCK_FAILED
;
1224 // Set block size for card if it is not equal to current block size for card
1225 if((ErrorState
= SD_TransmitCommand((SD_CMD_SET_BLOCKLEN
| SD_CMD_RESPONSE_SHORT
), 64, 1)) != SD_OK
)
1231 if((ErrorState
= SD_TransmitCommand((SD_CMD_APP_CMD
| SD_CMD_RESPONSE_SHORT
), SD_CardRCA
, 1)) != SD_OK
)
1236 // Configure the SD DPSM (Data Path State Machine)
1237 SD_DataTransferInit(64, SD_DATABLOCK_SIZE_64B
, true);
1239 // Send ACMD13 (SD_APP_STAUS) with argument as card's RCA
1240 if((ErrorState
= SD_TransmitCommand((SD_CMD_SD_APP_STATUS
| SD_CMD_RESPONSE_SHORT
), 0, 1)) != SD_OK
)
1246 while((SDMMC1
->STA
& (SDMMC_STA_RXOVERR
| SDMMC_STA_DCRCFAIL
| SDMMC_STA_DTIMEOUT
| SDMMC_STA_DBCKEND
)) == 0)
1248 if((SDMMC1
->STA
& SDMMC_STA_RXFIFOHF
) != 0)
1250 for(Count
= 0; Count
< 8; Count
++)
1252 Status
[Count
] = SDMMC1
->FIFO
;
1257 if((SDMMC1
->STA
& SDMMC_STA_DTIMEOUT
) != 0) return SD_DATA_TIMEOUT
;
1258 else if((SDMMC1
->STA
& SDMMC_STA_DCRCFAIL
) != 0) return SD_DATA_CRC_FAIL
;
1259 else if((SDMMC1
->STA
& SDMMC_STA_RXOVERR
) != 0) return SD_RX_OVERRUN
;
1263 this part from the HAL is very strange has it is possible to overflow the provide buffer... and this originate from ST HAL
1265 Count = SD_DATATIMEOUT;
1266 while(((SDMMC1->STA & SDMMC_STA_RXDAVL) != 0) && (Count > 0))
1268 *pSDstatus = SDMMC1->FIFO;
1276 Temp
= (Status
[0] & 0xC0) >> 6;
1277 pCardStatus
->DAT_BUS_WIDTH
= (uint8_t)Temp
;
1280 Temp
= (Status
[0] & 0x20) >> 5;
1281 pCardStatus
->SECURED_MODE
= (uint8_t)Temp
;
1284 Temp
= (Status
[2] & 0xFF);
1285 pCardStatus
->SD_CARD_TYPE
= (uint8_t)(Temp
<< 8);
1288 Temp
= (Status
[3] & 0xFF);
1289 pCardStatus
->SD_CARD_TYPE
|= (uint8_t)Temp
;
1292 Temp
= (Status
[4] & 0xFF);
1293 pCardStatus
->SIZE_OF_PROTECTED_AREA
= (uint8_t)(Temp
<< 24);
1296 Temp
= (Status
[5] & 0xFF);
1297 pCardStatus
->SIZE_OF_PROTECTED_AREA
|= (uint8_t)(Temp
<< 16);
1300 Temp
= (Status
[6] & 0xFF);
1301 pCardStatus
->SIZE_OF_PROTECTED_AREA
|= (uint8_t)(Temp
<< 8);
1304 Temp
= (Status
[7] & 0xFF);
1305 pCardStatus
->SIZE_OF_PROTECTED_AREA
|= (uint8_t)Temp
;
1308 Temp
= (Status
[8] & 0xFF);
1309 pCardStatus
->SPEED_CLASS
= (uint8_t)Temp
;
1312 Temp
= (Status
[9] & 0xFF);
1313 pCardStatus
->PERFORMANCE_MOVE
= (uint8_t)Temp
;
1316 Temp
= (Status
[10] & 0xF0) >> 4;
1317 pCardStatus
->AU_SIZE
= (uint8_t)Temp
;
1320 Temp
= (Status
[11] & 0xFF);
1321 pCardStatus
->ERASE_SIZE
= (uint8_t)(Temp
<< 8);
1324 Temp
= (Status
[12] & 0xFF);
1325 pCardStatus
->ERASE_SIZE
|= (uint8_t)Temp
;
1328 Temp
= (Status
[13] & 0xFC) >> 2;
1329 pCardStatus
->ERASE_TIMEOUT
= (uint8_t)Temp
;
1332 Temp
= (Status
[13] & 0x3);
1333 pCardStatus
->ERASE_OFFSET
= (uint8_t)Temp
;
1338 /** -----------------------------------------------------------------------------------------------------------------*/
1340 * @brief Enquires cards about their operating voltage and configures clock
1341 * controls and stores SD information that will be needed in future
1343 * @retval SD Card error state
1345 static SD_Error_t
SD_PowerON(void)
1347 SD_Error_t ErrorState
;
1348 uint32_t Response
= 0;
1350 uint32_t ValidVoltage
;
1352 //uint32_t TickStart;
1356 SD_Type
= SD_RESP_STD_CAPACITY
;
1358 // Power ON Sequence -------------------------------------------------------
1359 SDMMC1
->CLKCR
&= ~SDMMC_CLKCR_CLKEN
; // Disable SDMMC1 Clock
1360 SDMMC1
->POWER
= SDMMC_POWER_PWRCTRL
; // Set Power State to ON
1362 // 1ms: required power up waiting time before starting the SD initialization sequence (make it 2 to be safe)
1365 SDMMC1
->CLKCR
|= SDMMC_CLKCR_CLKEN
; // Enable SDMMC1 Clock
1367 // CMD0: GO_IDLE_STATE -----------------------------------------------------
1368 // No CMD response required
1369 if((ErrorState
= SD_TransmitCommand(SD_CMD_GO_IDLE_STATE
, 0, 0)) != SD_OK
)
1371 // CMD Response Timeout (wait for CMDSENT flag)
1375 // CMD8: SEND_IF_COND ------------------------------------------------------
1376 // Send CMD8 to verify SD card interface operating condition
1377 // Argument: - [31:12]: Reserved (shall be set to '0')
1378 //- [11:8]: Supply Voltage (VHS) 0x1 (Range: 2.7-3.6 V)
1379 //- [7:0]: Check Pattern (recommended 0xAA)
1380 // CMD Response: R7 */
1381 if((ErrorState
= SD_TransmitCommand((SD_SDMMC_SEND_IF_COND
| SD_CMD_RESPONSE_SHORT
), SD_CHECK_PATTERN
, 7)) == SD_OK
)
1384 SD_CardType
= SD_STD_CAPACITY_V2_0
;
1385 SD_Type
= SD_RESP_HIGH_CAPACITY
;
1389 // If ErrorState is Command Timeout, it is a MMC card
1390 // If ErrorState is SD_OK it is a SD card: SD card 2.0 (voltage range mismatch) or SD card 1.x
1391 if((ErrorState
= SD_TransmitCommand((SD_CMD_APP_CMD
| SD_CMD_RESPONSE_SHORT
), 0, 1)) == SD_OK
)
1394 // Send ACMD41 SD_APP_OP_COND with Argument 0x80100000
1395 while((ValidVoltage
== 0) && (Count
< SD_MAX_VOLT_TRIAL
))
1397 // SEND CMD55 APP_CMD with RCA as 0
1398 if((ErrorState
= SD_TransmitCommand((SD_CMD_APP_CMD
| SD_CMD_RESPONSE_SHORT
), 0, 1)) != SD_OK
)
1404 if((ErrorState
= SD_TransmitCommand((SD_CMD_SD_APP_OP_COND
| SD_CMD_RESPONSE_SHORT
), SD_VOLTAGE_WINDOW_SD
| SD_Type
, 3)) != SD_OK
)
1409 Response
= SDMMC1
->RESP1
; // Get command response
1410 ValidVoltage
= (((Response
>> 31) == 1) ? 1 : 0); // Get operating voltage
1414 if(Count
>= SD_MAX_VOLT_TRIAL
)
1416 return SD_INVALID_VOLTRANGE
;
1419 if((Response
& SD_RESP_HIGH_CAPACITY
) == SD_RESP_HIGH_CAPACITY
)
1421 SD_CardType
= SD_HIGH_CAPACITY
;
1428 /** -----------------------------------------------------------------------------------------------------------------*/
1430 * @brief Turns the SDMMC1 output signals off.
1431 * @retval SD Card error state
1434 static void SD_PowerOFF(void)
1436 // Set Power State to OFF
1437 SDMMC1
->POWER
= (uint32_t)0;
1441 /** -----------------------------------------------------------------------------------------------------------------*/
1443 * @brief Finds the SD card SCR register value.
1444 * @param pSCR: pointer to the buffer that will contain the SCR value
1445 * @retval SD Card error state
1447 static SD_Error_t
SD_FindSCR(uint32_t *pSCR
)
1449 SD_Error_t ErrorState
;
1451 uint32_t tempscr
[2] = {0, 0};
1453 // Set Block Size To 8 Bytes
1454 // Send CMD55 APP_CMD with argument as card's RCA
1455 if((ErrorState
= SD_TransmitCommand((SD_CMD_SET_BLOCKLEN
| SD_CMD_RESPONSE_SHORT
), 8, 1)) == SD_OK
)
1457 // Send CMD55 APP_CMD with argument as card's RCA
1458 if((ErrorState
= SD_TransmitCommand((SD_CMD_APP_CMD
| SD_CMD_RESPONSE_SHORT
), SD_CardRCA
, 1)) == SD_OK
)
1460 SD_DataTransferInit(8, SD_DATABLOCK_SIZE_8B
, true);
1462 // Send ACMD51 SD_APP_SEND_SCR with argument as 0
1463 if((ErrorState
= SD_TransmitCommand((SD_CMD_SD_APP_SEND_SCR
| SD_CMD_RESPONSE_SHORT
), 0, 1)) == SD_OK
)
1465 while((SDMMC1
->STA
& (SDMMC_STA_RXOVERR
| SDMMC_STA_DCRCFAIL
| SDMMC_STA_DTIMEOUT
| SDMMC_STA_DBCKEND
)) == 0)
1467 if((SDMMC1
->STA
& SDMMC_STA_RXDAVL
) != 0)
1469 *(tempscr
+ Index
) = SDMMC1
->FIFO
;
1474 if ((SDMMC1
->STA
& SDMMC_STA_DTIMEOUT
) != 0) ErrorState
= SD_DATA_TIMEOUT
;
1475 else if((SDMMC1
->STA
& SDMMC_STA_DCRCFAIL
) != 0) ErrorState
= SD_DATA_CRC_FAIL
;
1476 else if((SDMMC1
->STA
& SDMMC_STA_RXOVERR
) != 0) ErrorState
= SD_RX_OVERRUN
;
1477 else if((SDMMC1
->STA
& SDMMC_STA_RXDAVL
) != 0) ErrorState
= SD_OUT_OF_BOUND
;
1480 *(pSCR
+ 1) = ((tempscr
[0] & SD_0TO7BITS
) << 24) | ((tempscr
[0] & SD_8TO15BITS
) << 8) |
1481 ((tempscr
[0] & SD_16TO23BITS
) >> 8) | ((tempscr
[0] & SD_24TO31BITS
) >> 24);
1483 *(pSCR
) = ((tempscr
[1] & SD_0TO7BITS
) << 24) | ((tempscr
[1] & SD_8TO15BITS
) << 8) |
1484 ((tempscr
[1] & SD_16TO23BITS
) >> 8) | ((tempscr
[1] & SD_24TO31BITS
) >> 24);
1494 /** -----------------------------------------------------------------------------------------------------------------*/
1496 * @brief Checks if the SD card is in programming state.
1497 * @param pStatus: pointer to the variable that will contain the SD card state
1498 * @retval SD Card error state
1501 static SD_Error_t SD_IsCardProgramming(uint8_t *pStatus)
1503 uint32_t Response_R1;
1505 SD_TransmitCommand((SD_CMD_SEND_STATUS | SDMMC_CMD_RESPONSE_SHORT), SD_CardRCA, 0);
1506 if((SDMMC1->STA & SDMMC_STA_CTIMEOUT) != 0) return SD_CMD_RSP_TIMEOUT;
1507 else if((SDMMC1->STA & SDMMC_STA_CCRCFAIL) != 0) return SD_CMD_CRC_FAIL;
1508 if((uint32_t)SDMMC1->RESPCMD != SD_CMD_SEND_STATUS) return SD_ILLEGAL_CMD; // Check if is of desired command
1509 Response_R1 = SDMMC1->RESP1; // We have received response, retrieve it for analysis
1510 *pStatus = (uint8_t)((Response_R1 >> 9) & 0x0000000F); // Find out card status
1512 return CheckOCR_Response(Response_R1);
1516 /** -----------------------------------------------------------------------------------------------------------------*/
1518 * @brief Initialize the SDMMC1 module, DMA, and IO
1520 bool SD_Initialize_LL(DMA_Stream_TypeDef
*dma
)
1522 const dmaIdentifier_e dmaIdentifier
= dmaGetIdentifier((dmaResource_t
*)dmaStream
);
1523 if (!(dma
== DMA2_Stream3
|| dma
== DMA2_Stream6
) || !dmaAllocate(dmaIdentifier
, OWNER_SDCARD
, 0)) {
1527 // Reset SDMMC1 Module
1528 RCC
->APB2RSTR
|= RCC_APB2RSTR_SDMMC1RST
;
1530 RCC
->APB2RSTR
&= ~RCC_APB2RSTR_SDMMC1RST
;
1533 // Enable SDMMC1 clock
1534 RCC
->APB2ENR
|= RCC_APB2ENR_SDMMC1EN
;
1536 // Enable DMA2 clocks
1537 RCC
->AHB1ENR
|= RCC_AHB1ENR_DMA2EN
;
1540 RCC
->AHB1ENR
|= RCC_AHB1ENR_GPIOCEN
| RCC_AHB1ENR_GPIODEN
;
1542 uint8_t is4BitWidth
= sdioConfig()->use4BitWidth
;
1544 const IO_t d0
= IOGetByTag(IO_TAG(PC8
));
1545 const IO_t d1
= IOGetByTag(IO_TAG(PC9
));
1546 const IO_t d2
= IOGetByTag(IO_TAG(PC10
));
1547 const IO_t d3
= IOGetByTag(IO_TAG(PC11
));
1548 const IO_t clk
= IOGetByTag(IO_TAG(PC12
));
1549 const IO_t cmd
= IOGetByTag(IO_TAG(PD2
));
1551 IOInit(d0
, OWNER_SDCARD
, 0);
1553 IOInit(d1
, OWNER_SDCARD
, 0);
1554 IOInit(d2
, OWNER_SDCARD
, 0);
1555 IOInit(d3
, OWNER_SDCARD
, 0);
1557 IOInit(clk
, OWNER_SDCARD
, 0);
1558 IOInit(cmd
, OWNER_SDCARD
, 0);
1560 #define SDMMC_DATA IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
1561 #define SDMMC_CMD IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
1562 #define SDMMC_CLK IO_CONFIG(GPIO_MODE_AF_PP, GPIO_SPEED_FREQ_VERY_HIGH, GPIO_NOPULL)
1564 IOConfigGPIOAF(d0
, SDMMC_DATA
, GPIO_AF12_SDMMC1
);
1566 IOConfigGPIOAF(d1
, SDMMC_DATA
, GPIO_AF12_SDMMC1
);
1567 IOConfigGPIOAF(d2
, SDMMC_DATA
, GPIO_AF12_SDMMC1
);
1568 IOConfigGPIOAF(d3
, SDMMC_DATA
, GPIO_AF12_SDMMC1
);
1570 IOConfigGPIOAF(clk
, SDMMC_CLK
, GPIO_AF12_SDMMC1
);
1571 IOConfigGPIOAF(cmd
, SDMMC_CMD
, GPIO_AF12_SDMMC1
);
1573 uint32_t PriorityGroup
= NVIC_GetPriorityGrouping();
1575 // NVIC configuration for SDIO interrupts
1576 NVIC_SetPriority(SDMMC1_IRQn
, NVIC_EncodePriority(PriorityGroup
, 1, 0));
1577 NVIC_EnableIRQ(SDMMC1_IRQn
);
1580 RCC
->AHB1ENR
|= RCC_AHB1ENR_DMA2EN
;
1583 dmaStream
->CR
= 0; // Reset DMA Stream control register
1584 dmaStream
->PAR
= (uint32_t)&SDMMC1
->FIFO
;
1585 if (dmaStream
== DMA2_Stream3
) {
1586 DMA2
->LIFCR
= IFCR_CLEAR_MASK_STREAM3
; // Clear all interrupt flags
1588 DMA2
->HIFCR
= IFCR_CLEAR_MASK_STREAM6
; // Clear all interrupt flags
1590 dmaStream
->CR
= (DMA_CHANNEL_4
| DMA_SxCR_PFCTRL
| // Prepare the DMA Stream configuration
1591 DMA_MINC_ENABLE
| DMA_PDATAALIGN_WORD
| // And write to DMA Stream CR register
1592 DMA_MDATAALIGN_WORD
| DMA_PRIORITY_VERY_HIGH
|
1593 DMA_MBURST_INC4
| DMA_PBURST_INC4
|
1594 DMA_MEMORY_TO_PERIPH
);
1595 dmaStream
->FCR
= (DMA_SxFCR_DMDIS
| DMA_SxFCR_FTH
); // Configuration FIFO control register
1596 dmaEnable(dmaIdentifier
);
1597 if (dmaStream
== DMA2_Stream3
) {
1598 dmaSetHandler(dmaIdentifier
, SDMMC_DMA_ST3_IRQHandler
, 1, 0);
1600 dmaSetHandler(dmaIdentifier
, SDMMC_DMA_ST6_IRQHandler
, 1, 0);
1606 /** -----------------------------------------------------------------------------------------------------------------*/
1607 bool SD_GetState(void)
1609 // Check SDCARD status
1610 if(SD_GetStatus() == SD_OK
) return true;
1614 /** -----------------------------------------------------------------------------------------------------------------*/
1615 static SD_Error_t
SD_DoInit(void)
1617 SD_Error_t errorState
;
1619 __HAL_RCC_SDMMC1_CLK_ENABLE();
1621 // Initialize SDMMC1 peripheral interface with default configuration for SD card initialization.
1622 MODIFY_REG(SDMMC1
->CLKCR
, CLKCR_CLEAR_MASK
, (uint32_t) SDMMC_INIT_CLK_DIV
);
1624 // Identify card operating voltage.
1625 errorState
= SD_PowerON();
1626 if (errorState
!= SD_OK
) {
1630 // Initialize the present card and put them in idle state.
1631 errorState
= SD_InitializeCard();
1632 if (errorState
!= SD_OK
) {
1636 // Read CSD/CID MSD registers.
1637 errorState
= SD_GetCardInfo();
1638 if (errorState
!= SD_OK
) {
1642 // Select the Card - Send CMD7 SDMMC_SEL_DESEL_CARD.
1643 errorState
= SD_TransmitCommand((SD_CMD_SEL_DESEL_CARD
| SD_CMD_RESPONSE_SHORT
), SD_CardRCA
, 1);
1644 // Configure SDMMC1 peripheral interface.
1645 MODIFY_REG(SDMMC1
->CLKCR
, CLKCR_CLEAR_MASK
, (uint32_t) SDMMC_CLK_DIV
);
1647 // Configure SD Bus width.
1648 if (errorState
== SD_OK
)
1650 // Enable wide operation.
1651 if (sdioConfig()->use4BitWidth
) {
1652 errorState
= SD_WideBusOperationConfig(SD_BUS_WIDE_4B
);
1654 errorState
= SD_WideBusOperationConfig(SD_BUS_WIDE_1B
);
1661 SD_Error_t
SD_Init(void)
1663 static bool sdInitAttempted
= false;
1664 static SD_Error_t result
= SD_ERROR
;
1666 if (sdInitAttempted
) {
1670 sdInitAttempted
= true;
1672 result
= SD_DoInit();
1677 /** -----------------------------------------------------------------------------------------------------------------*/
1679 * @brief This function handles SD card interrupt request.
1681 void SDMMC1_IRQHandler(void)
1683 // Check for SDMMC1 interrupt flags
1684 if ((SDMMC1
->STA
& SDMMC_STA_DATAEND
) != 0) {
1685 SDMMC1
->ICR
= SDMMC_ICR_DATAENDC
;
1686 SDMMC1
->ICR
= SDMMC_ICR_STATIC_FLAGS
;
1687 SDMMC1
->MASK
&= ~(SDMMC_MASK_DATAENDIE
| SDMMC_MASK_DCRCFAILIE
| SDMMC_MASK_DTIMEOUTIE
| \
1688 SDMMC_MASK_TXUNDERRIE
| SDMMC_MASK_RXOVERRIE
| SDMMC_MASK_TXFIFOHEIE
| SDMMC_MASK_RXFIFOHFIE
);
1690 /* Currently doesn't implement multiple block write handling */
1691 if ((SD_Handle
.Operation
& 0x02) == (SDMMC_DIR_TX
<< 1)) {
1692 /* Disable the stream */
1693 dmaStream
->CR
&= ~DMA_SxCR_EN
;
1694 SDMMC1
->DCTRL
&= ~(SDMMC_DCTRL_DMAEN
);
1695 /* Transfer is complete */
1696 SD_Handle
.TXCplt
= 0;
1697 if ((SD_Handle
.Operation
& 0x01) == SD_MULTIPLE_BLOCK
) {
1698 /* Send stop command in multiblock write */
1699 SD_TransmitCommand((SD_CMD_STOP_TRANSMISSION
| SD_CMD_RESPONSE_SHORT
), 0, 1);
1702 SD_Handle
.TransferComplete
= 1;
1703 SD_Handle
.TransferError
= SD_OK
; // No transfer error
1705 else if ((SDMMC1
->STA
& SDMMC_STA_DCRCFAIL
) != 0)
1706 SD_Handle
.TransferError
= SD_DATA_CRC_FAIL
;
1707 else if ((SDMMC1
->STA
& SDMMC_STA_DTIMEOUT
) != 0)
1708 SD_Handle
.TransferError
= SD_DATA_TIMEOUT
;
1709 else if ((SDMMC1
->STA
& SDMMC_STA_RXOVERR
) != 0)
1710 SD_Handle
.TransferError
= SD_RX_OVERRUN
;
1711 else if ((SDMMC1
->STA
& SDMMC_STA_TXUNDERR
) != 0)
1712 SD_Handle
.TransferError
= SD_TX_UNDERRUN
;
1714 SDMMC1
->ICR
= SDMMC_ICR_STATIC_FLAGS
;
1716 // Disable all SDMMC1 peripheral interrupt sources
1717 SDMMC1
->MASK
&= ~(SDMMC_MASK_DCRCFAILIE
| SDMMC_MASK_DTIMEOUTIE
1718 | SDMMC_MASK_DATAENDIE
|
1719 SDMMC_MASK_TXFIFOHEIE
| SDMMC_MASK_RXFIFOHFIE
| SDMMC_MASK_TXUNDERRIE
|
1720 SDMMC_MASK_RXOVERRIE
);
1723 /** -----------------------------------------------------------------------------------------------------------------*/
1725 * @brief This function handles DMA2 Stream 3 interrupt request.
1727 void SDMMC_DMA_ST3_IRQHandler(dmaChannelDescriptor_t
*dma
)
1730 // Transfer Error Interrupt management
1731 if((DMA2
->LISR
& DMA_LISR_TEIF3
) != 0)
1733 if((DMA2_Stream3
->CR
& DMA_SxCR_TEIE
) != 0)
1735 DMA2_Stream3
->CR
&= ~DMA_SxCR_TEIE
; // Disable the transfer error interrupt
1736 DMA2
->LIFCR
= DMA_LIFCR_CTEIF3
; // Clear the transfer error flag
1740 // FIFO Error Interrupt management
1741 if((DMA2
->LISR
& DMA_LISR_FEIF3
) != 0)
1743 if((DMA2_Stream3
->FCR
& DMA_SxFCR_FEIE
) != 0)
1745 DMA2_Stream3
->FCR
&= ~DMA_SxFCR_FEIE
; // Disable the FIFO Error interrupt
1746 DMA2
->LIFCR
= DMA_LIFCR_CFEIF3
; // Clear the FIFO error flag
1750 // Direct Mode Error Interrupt management
1751 if((DMA2
->LISR
& DMA_LISR_DMEIF3
) != 0)
1753 if((DMA2_Stream3
->CR
& DMA_SxCR_DMEIE
) != 0)
1755 DMA2_Stream3
->CR
&= ~DMA_SxCR_DMEIE
; // Disable the direct mode Error interrupt
1756 DMA2
->LIFCR
= DMA_LIFCR_CDMEIF3
; // Clear the FIFO error flag
1760 // Half Transfer Complete Interrupt management
1761 if((DMA2
->LISR
& DMA_LISR_HTIF3
) != 0)
1763 if((DMA2_Stream3
->CR
& DMA_SxCR_HTIE
) != 0)
1765 if(((DMA2_Stream3
->CR
) & (uint32_t)(DMA_SxCR_DBM
)) != 0) // Multi_Buffering mode enabled
1767 DMA2
->LIFCR
= DMA_LIFCR_CHTIF3
; // Clear the half transfer complete flag
1771 if((DMA2_Stream3
->CR
& DMA_SxCR_CIRC
) == 0) // Disable the half transfer interrupt if the DMA mode is not CIRCULAR
1773 DMA2_Stream3
->CR
&= ~DMA_SxCR_HTIE
; // Disable the half transfer interrupt
1776 DMA2
->LIFCR
= DMA_LIFCR_CHTIF3
; // Clear the half transfer complete flag
1781 // Transfer Complete Interrupt management
1782 if((DMA2
->LISR
& DMA_LISR_TCIF3
) != 0)
1784 if((DMA2_Stream3
->CR
& DMA_SxCR_TCIE
) != 0)
1786 if((DMA2_Stream3
->CR
& (uint32_t)(DMA_SxCR_DBM
)) != 0)
1788 DMA2
->LIFCR
= DMA_LIFCR_CTCIF3
; // Clear the transfer complete flag
1790 else //Disable the transfer complete interrupt if the DMA mode is not CIRCULAR
1792 if((DMA2_Stream3
->CR
& DMA_SxCR_CIRC
) == 0)
1794 DMA2_Stream3
->CR
&= ~DMA_SxCR_TCIE
; // Disable the transfer complete interrupt
1797 DMA2
->LIFCR
= DMA_LIFCR_CTCIF3
; // Clear the transfer complete flag
1798 SD_DMA_Complete(DMA2_Stream3
);
1804 /** -----------------------------------------------------------------------------------------------------------------*/
1806 * @brief This function handles DMA2 Stream 6 interrupt request.
1808 void SDMMC_DMA_ST6_IRQHandler(dmaChannelDescriptor_t
*dma
)
1811 // Transfer Error Interrupt management
1812 if((DMA2
->HISR
& DMA_HISR_TEIF6
) != 0)
1814 if((DMA2_Stream6
->CR
& DMA_SxCR_TEIE
) != 0)
1816 DMA2_Stream6
->CR
&= ~DMA_SxCR_TEIE
; // Disable the transfer error interrupt
1817 DMA2
->HIFCR
= DMA_HIFCR_CTEIF6
; // Clear the transfer error flag
1821 // FIFO Error Interrupt management
1822 if((DMA2
->HISR
& DMA_HISR_FEIF6
) != 0)
1824 if((DMA2_Stream6
->FCR
& DMA_SxFCR_FEIE
) != 0)
1826 DMA2_Stream6
->FCR
&= ~DMA_SxFCR_FEIE
; // Disable the FIFO Error interrupt
1827 DMA2
->HIFCR
= DMA_HIFCR_CFEIF6
; // Clear the FIFO error flag
1831 // Direct Mode Error Interrupt management
1832 if((DMA2
->HISR
& DMA_HISR_DMEIF6
) != 0)
1834 if((DMA2_Stream6
->CR
& DMA_SxCR_DMEIE
) != 0)
1836 DMA2_Stream6
->CR
&= ~DMA_SxCR_DMEIE
; // Disable the direct mode Error interrupt
1837 DMA2
->HIFCR
= DMA_HIFCR_CDMEIF6
; // Clear the FIFO error flag
1841 // Half Transfer Complete Interrupt management
1842 if((DMA2
->HISR
& DMA_HISR_HTIF6
) != 0)
1844 if((DMA2_Stream6
->CR
& DMA_SxCR_HTIE
) != 0)
1846 if(((DMA2_Stream6
->CR
) & (uint32_t)(DMA_SxCR_DBM
)) != 0) // Multi_Buffering mode enabled
1848 DMA2
->HIFCR
= DMA_HIFCR_CHTIF6
; // Clear the half transfer complete flag
1852 if((DMA2_Stream6
->CR
& DMA_SxCR_CIRC
) == 0) // Disable the half transfer interrupt if the DMA mode is not CIRCULAR
1854 DMA2_Stream6
->CR
&= ~DMA_SxCR_HTIE
; // Disable the half transfer interrupt
1857 DMA2
->HIFCR
= DMA_HIFCR_CHTIF6
; // Clear the half transfer complete flag
1862 // Transfer Complete Interrupt management
1863 if((DMA2
->HISR
& DMA_HISR_TCIF6
) != 0)
1865 if((DMA2_Stream6
->CR
& DMA_SxCR_TCIE
) != 0)
1867 if((DMA2_Stream6
->CR
& (uint32_t)(DMA_SxCR_DBM
)) != 0)
1869 DMA2
->HIFCR
= DMA_HIFCR_CTCIF6
; // Clear the transfer complete flag
1871 else //Disable the transfer complete interrupt if the DMA mode is not CIRCULAR
1873 if((DMA2_Stream6
->CR
& DMA_SxCR_CIRC
) == 0)
1875 DMA2_Stream6
->CR
&= ~DMA_SxCR_TCIE
; // Disable the transfer complete interrupt
1878 DMA2
->HIFCR
= DMA_HIFCR_CTCIF6
; // Clear the transfer complete flag
1879 SD_DMA_Complete(DMA2_Stream6
);
1885 /* ------------------------------------------------------------------------------------------------------------------*/