2 ******************************************************************************
3 * @file stm32f7xx_hal_sd.c
4 * @author MCD Application Team
7 * @brief SD card HAL module driver.
8 * This file provides firmware functions to manage the following
9 * functionalities of the Secure Digital (SD) peripheral:
10 * + Initialization and de-initialization functions
11 * + IO operation functions
12 * + Peripheral Control functions
13 * + SD card Control functions
16 ==============================================================================
17 ##### How to use this driver #####
18 ==============================================================================
20 This driver implements a high level communication layer for read and write from/to
21 this memory. The needed STM32 hardware resources (SDMMC and GPIO) are performed by
22 the user in HAL_SD_MspInit() function (MSP layer).
23 Basically, the MSP layer configuration should be the same as we provide in the
25 You can easily tailor this configuration according to hardware resources.
28 This driver is a generic layered driver for SDMMC memories which uses the HAL
29 SDMMC driver functions to interface with SD and uSD cards devices.
30 It is used as follows:
32 (#)Initialize the SDMMC low level resources by implement the HAL_SD_MspInit() API:
33 (##) Enable the SDMMC interface clock using __HAL_RCC_SDMMC_CLK_ENABLE();
34 (##) SDMMC pins configuration for SD card
35 (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
36 (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
37 and according to your pin assignment;
38 (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
39 and HAL_SD_WriteBlocks_DMA() APIs).
40 (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE();
41 (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled.
42 (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
43 (+++) Configure the SDMMC and DMA interrupt priorities using functions
44 HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
45 (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
46 (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
47 and __HAL_SD_DISABLE_IT() inside the communication process.
48 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
49 and __HAL_SD_CLEAR_IT()
50 (##) NVIC configuration if you need to use interrupt process (HAL_SD_ReadBlocks_IT()
51 and HAL_SD_WriteBlocks_IT() APIs).
52 (+++) Configure the SDMMC interrupt priorities using function
53 HAL_NVIC_SetPriority();
54 (+++) Enable the NVIC SDMMC IRQs using function HAL_NVIC_EnableIRQ()
55 (+++) SDMMC interrupts are managed using the macros __HAL_SD_ENABLE_IT()
56 and __HAL_SD_DISABLE_IT() inside the communication process.
57 (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_GET_IT()
58 and __HAL_SD_CLEAR_IT()
59 (#) At this stage, you can perform SD read/write/erase operations after SD card initialization
62 *** SD Card Initialization and configuration ***
63 ================================================
65 To initialize the SD Card, use the HAL_SD_Init() function. It Initializes
66 SDMMC IP (STM32 side) and the SD Card, and put it into StandBy State (Ready for data transfer).
67 This function provide the following operations:
69 (#) Initialize the SDMMC peripheral interface with defaullt configuration.
70 The initialization process is done at 400KHz. You can change or adapt
71 this frequency by adjusting the "ClockDiv" field.
72 The SD Card frequency (SDMMC_CK) is computed as follows:
74 SDMMC_CK = SDMMCCLK / (ClockDiv + 2)
76 In initialization mode and according to the SD Card standard,
77 make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
79 This phase of initialization is done through SDMMC_Init() and
80 SDMMC_PowerState_ON() SDMMC low level APIs.
82 (#) Initialize the SD card. The API used is HAL_SD_InitCard().
83 This phase allows the card initialization and identification
84 and check the SD Card type (Standard Capacity or High Capacity)
85 The initialization flow is compatible with SD standard.
87 This API (HAL_SD_InitCard()) could be used also to reinitialize the card in case
90 (#) Configure the SD Card Data transfer frequency. By Default, the card transfer
91 frequency is set to 24MHz. You can change or adapt this frequency by adjusting
93 In transfer mode and according to the SD Card standard, make sure that the
94 SDMMC_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
95 To be able to use a frequency higher than 24MHz, you should use the SDMMC
96 peripheral in bypass mode. Refer to the corresponding reference manual
99 (#) Select the corresponding SD Card according to the address read with the step 2.
101 (#) Configure the SD Card in wide bus mode: 4-bits data.
103 *** SD Card Read operation ***
104 ==============================
106 (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks().
107 This function allows the read of 512 bytes blocks.
108 You can choose either one block read operation or multiple block read operation
109 by adjusting the "NumberOfBlocks" parameter.
110 After this, you have to ensure that the transfer is done correctly. The check is done
111 through HAL_SD_GetCardState() function for SD card state.
113 (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
114 This function allows the read of 512 bytes blocks.
115 You can choose either one block read operation or multiple block read operation
116 by adjusting the "NumberOfBlocks" parameter.
117 After this, you have to ensure that the transfer is done correctly. The check is done
118 through HAL_SD_GetCardState() function for SD card state.
119 You could also check the DMA transfer process through the SD Rx interrupt event.
121 (+) You can read from SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT().
122 This function allows the read of 512 bytes blocks.
123 You can choose either one block read operation or multiple block read operation
124 by adjusting the "NumberOfBlocks" parameter.
125 After this, you have to ensure that the transfer is done correctly. The check is done
126 through HAL_SD_GetCardState() function for SD card state.
127 You could also check the IT transfer process through the SD Rx interrupt event.
129 *** SD Card Write operation ***
130 ===============================
132 (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks().
133 This function allows the read of 512 bytes blocks.
134 You can choose either one block read operation or multiple block read operation
135 by adjusting the "NumberOfBlocks" parameter.
136 After this, you have to ensure that the transfer is done correctly. The check is done
137 through HAL_SD_GetCardState() function for SD card state.
139 (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
140 This function allows the read of 512 bytes blocks.
141 You can choose either one block read operation or multiple block read operation
142 by adjusting the "NumberOfBlocks" parameter.
143 After this, you have to ensure that the transfer is done correctly. The check is done
144 through HAL_SD_GetCardState() function for SD card state.
145 You could also check the DMA transfer process through the SD Tx interrupt event.
147 (+) You can write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT().
148 This function allows the read of 512 bytes blocks.
149 You can choose either one block read operation or multiple block read operation
150 by adjusting the "NumberOfBlocks" parameter.
151 After this, you have to ensure that the transfer is done correctly. The check is done
152 through HAL_SD_GetCardState() function for SD card state.
153 You could also check the IT transfer process through the SD Tx interrupt event.
155 *** SD card status ***
156 ======================
158 (+) The SD Status contains status bits that are related to the SD Memory
159 Card proprietary features. To get SD card status use the HAL_SD_GetCardStatus().
161 *** SD card information ***
162 ===========================
164 (+) To get SD card information, you can use the function HAL_SD_GetCardInfo().
165 It returns useful information about the SD card such as block size, card type,
168 *** SD card CSD register ***
169 ============================
171 (+) The HAL_SD_GetCardCSD() API allows to get the parameters of the CSD register.
172 Some of the CSD parameters are useful for card initialization and identification.
174 *** SD card CID register ***
175 ============================
177 (+) The HAL_SD_GetCardCID() API allows to get the parameters of the CID register.
178 Some of the CSD parameters are useful for card initialization and identification.
180 *** SD HAL driver macros list ***
181 ==================================
183 Below the list of most used macros in SD HAL driver.
185 (+) __HAL_SD_ENABLE : Enable the SD device
186 (+) __HAL_SD_DISABLE : Disable the SD device
187 (+) __HAL_SD_DMA_ENABLE: Enable the SDMMC DMA transfer
188 (+) __HAL_SD_DMA_DISABLE: Disable the SDMMC DMA transfer
189 (+) __HAL_SD_ENABLE_IT: Enable the SD device interrupt
190 (+) __HAL_SD_DISABLE_IT: Disable the SD device interrupt
191 (+) __HAL_SD_GET_FLAG:Check whether the specified SD flag is set or not
192 (+) __HAL_SD_CLEAR_FLAG: Clear the SD's pending flags
195 (@) You can refer to the SD HAL driver header file for more useful macros
198 ******************************************************************************
201 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
203 * Redistribution and use in source and binary forms, with or without modification,
204 * are permitted provided that the following conditions are met:
205 * 1. Redistributions of source code must retain the above copyright notice,
206 * this list of conditions and the following disclaimer.
207 * 2. Redistributions in binary form must reproduce the above copyright notice,
208 * this list of conditions and the following disclaimer in the documentation
209 * and/or other materials provided with the distribution.
210 * 3. Neither the name of STMicroelectronics nor the names of its contributors
211 * may be used to endorse or promote products derived from this software
212 * without specific prior written permission.
214 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
215 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
216 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
217 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
218 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
219 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
220 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
221 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
222 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
223 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
225 ******************************************************************************
228 /* Includes ------------------------------------------------------------------*/
229 #include "stm32f7xx_hal.h"
231 /** @addtogroup STM32F7xx_HAL_Driver
239 #ifdef HAL_SD_MODULE_ENABLED
241 /* Private typedef -----------------------------------------------------------*/
242 /* Private define ------------------------------------------------------------*/
243 /** @addtogroup SD_Private_Defines
251 /* Private macro -------------------------------------------------------------*/
252 /* Private variables ---------------------------------------------------------*/
253 /* Private function prototypes -----------------------------------------------*/
254 /* Private functions ---------------------------------------------------------*/
255 /** @defgroup SD_Private_Functions SD Private Functions
258 static uint32_t SD_InitCard(SD_HandleTypeDef
*hsd
);
259 static uint32_t SD_PowerON(SD_HandleTypeDef
*hsd
);
260 static uint32_t SD_SendSDStatus(SD_HandleTypeDef
*hsd
, uint32_t *pSDstatus
);
261 static uint32_t SD_SendStatus(SD_HandleTypeDef
*hsd
, uint32_t *pCardStatus
);
262 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef
*hsd
);
263 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef
*hsd
);
264 static uint32_t SD_FindSCR(SD_HandleTypeDef
*hsd
, uint32_t *pSCR
);
265 static HAL_StatusTypeDef
SD_PowerOFF(SD_HandleTypeDef
*hsd
);
266 static HAL_StatusTypeDef
SD_Write_IT(SD_HandleTypeDef
*hsd
);
267 static HAL_StatusTypeDef
SD_Read_IT(SD_HandleTypeDef
*hsd
);
268 static void SD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
269 static void SD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
270 static void SD_DMAError(DMA_HandleTypeDef
*hdma
);
271 static void SD_DMATxAbort(DMA_HandleTypeDef
*hdma
);
272 static void SD_DMARxAbort(DMA_HandleTypeDef
*hdma
);
277 /* Exported functions --------------------------------------------------------*/
278 /** @addtogroup SD_Exported_Functions
282 /** @addtogroup SD_Exported_Functions_Group1
283 * @brief Initialization and de-initialization functions
286 ==============================================================================
287 ##### Initialization and de-initialization functions #####
288 ==============================================================================
290 This section provides functions allowing to initialize/de-initialize the SD
291 card device to be ready for use.
298 * @brief Initializes the SD according to the specified parameters in the
299 SD_HandleTypeDef and create the associated handle.
300 * @param hsd: Pointer to the SD handle
303 HAL_StatusTypeDef
HAL_SD_Init(SD_HandleTypeDef
*hsd
)
305 /* Check the SD handle allocation */
311 /* Check the parameters */
312 assert_param(IS_SDMMC_ALL_INSTANCE(hsd
->Instance
));
313 assert_param(IS_SDMMC_CLOCK_EDGE(hsd
->Init
.ClockEdge
));
314 assert_param(IS_SDMMC_CLOCK_BYPASS(hsd
->Init
.ClockBypass
));
315 assert_param(IS_SDMMC_CLOCK_POWER_SAVE(hsd
->Init
.ClockPowerSave
));
316 assert_param(IS_SDMMC_BUS_WIDE(hsd
->Init
.BusWide
));
317 assert_param(IS_SDMMC_HARDWARE_FLOW_CONTROL(hsd
->Init
.HardwareFlowControl
));
318 assert_param(IS_SDMMC_CLKDIV(hsd
->Init
.ClockDiv
));
320 if(hsd
->State
== HAL_SD_STATE_RESET
)
322 /* Allocate lock resource and initialize it */
323 hsd
->Lock
= HAL_UNLOCKED
;
324 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
328 hsd
->State
= HAL_SD_STATE_BUSY
;
330 /* Initialize the Card parameters */
331 HAL_SD_InitCard(hsd
);
333 /* Initialize the error code */
334 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
336 /* Initialize the SD operation */
337 hsd
->Context
= SD_CONTEXT_NONE
;
339 /* Initialize the SD state */
340 hsd
->State
= HAL_SD_STATE_READY
;
346 * @brief Initializes the SD Card.
347 * @param hsd: Pointer to SD handle
348 * @note This function initializes the SD card. It could be used when a card
349 re-initialization is needed.
352 HAL_StatusTypeDef
HAL_SD_InitCard(SD_HandleTypeDef
*hsd
)
354 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
357 /* Default SDMMC peripheral configuration for SD card initialization */
358 Init
.ClockEdge
= SDMMC_CLOCK_EDGE_RISING
;
359 Init
.ClockBypass
= SDMMC_CLOCK_BYPASS_DISABLE
;
360 Init
.ClockPowerSave
= SDMMC_CLOCK_POWER_SAVE_DISABLE
;
361 Init
.BusWide
= SDMMC_BUS_WIDE_1B
;
362 Init
.HardwareFlowControl
= SDMMC_HARDWARE_FLOW_CONTROL_DISABLE
;
363 Init
.ClockDiv
= SDMMC_INIT_CLK_DIV
;
365 /* Initialize SDMMC peripheral interface with default configuration */
366 SDMMC_Init(hsd
->Instance
, Init
);
368 /* Disable SDMMC Clock */
369 __HAL_SD_DISABLE(hsd
);
371 /* Set Power State to ON */
372 SDMMC_PowerState_ON(hsd
->Instance
);
374 /* Enable SDMMC Clock */
375 __HAL_SD_ENABLE(hsd
);
377 /* Required power up waiting time before starting the SD initialization sequence */
380 /* Identify card operating voltage */
381 errorstate
= SD_PowerON(hsd
);
382 if(errorstate
!= HAL_SD_ERROR_NONE
)
384 hsd
->State
= HAL_SD_STATE_READY
;
385 hsd
->ErrorCode
|= errorstate
;
389 /* Card initialization */
390 errorstate
= SD_InitCard(hsd
);
391 if(errorstate
!= HAL_SD_ERROR_NONE
)
393 hsd
->State
= HAL_SD_STATE_READY
;
394 hsd
->ErrorCode
|= errorstate
;
402 * @brief De-Initializes the SD card.
403 * @param hsd: Pointer to SD handle
406 HAL_StatusTypeDef
HAL_SD_DeInit(SD_HandleTypeDef
*hsd
)
408 /* Check the SD handle allocation */
414 /* Check the parameters */
415 assert_param(IS_SDMMC_ALL_INSTANCE(hsd
->Instance
));
417 hsd
->State
= HAL_SD_STATE_BUSY
;
419 /* Set SD power state to off */
422 /* De-Initialize the MSP layer */
423 HAL_SD_MspDeInit(hsd
);
425 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
426 hsd
->State
= HAL_SD_STATE_RESET
;
433 * @brief Initializes the SD MSP.
434 * @param hsd: Pointer to SD handle
437 __weak
void HAL_SD_MspInit(SD_HandleTypeDef
*hsd
)
439 /* Prevent unused argument(s) compilation warning */
442 /* NOTE : This function Should not be modified, when the callback is needed,
443 the HAL_SD_MspInit could be implemented in the user file
448 * @brief De-Initialize SD MSP.
449 * @param hsd: Pointer to SD handle
452 __weak
void HAL_SD_MspDeInit(SD_HandleTypeDef
*hsd
)
454 /* Prevent unused argument(s) compilation warning */
457 /* NOTE : This function Should not be modified, when the callback is needed,
458 the HAL_SD_MspDeInit could be implemented in the user file
466 /** @addtogroup SD_Exported_Functions_Group2
467 * @brief Data transfer functions
470 ==============================================================================
471 ##### IO operation functions #####
472 ==============================================================================
474 This subsection provides a set of functions allowing to manage the data
475 transfer from/to SD card.
482 * @brief Reads block(s) from a specified address in a card. The Data transfer
483 * is managed by polling mode.
484 * @note This API should be followed by a check on the card state through
485 * HAL_SD_GetCardState().
486 * @param hsd: Pointer to SD handle
487 * @param pData: pointer to the buffer that will contain the received data
488 * @param BlockAdd: Block Address from where data is to be read
489 * @param NumberOfBlocks: Number of SD blocks to read
490 * @param Timeout: Specify timeout value
493 HAL_StatusTypeDef
HAL_SD_ReadBlocks(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
, uint32_t Timeout
)
495 SDMMC_DataInitTypeDef config
;
496 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
497 uint32_t tickstart
= HAL_GetTick();
498 uint32_t count
= 0, *tempbuff
= (uint32_t *)pData
;
502 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
506 if(hsd
->State
== HAL_SD_STATE_READY
)
508 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
510 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
512 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
516 hsd
->State
= HAL_SD_STATE_BUSY
;
518 /* Initialize data control register */
519 hsd
->Instance
->DCTRL
= 0;
521 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
526 /* Set Block Size for Card */
527 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
528 if(errorstate
!= HAL_SD_ERROR_NONE
)
530 /* Clear all the static flags */
531 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
532 hsd
->ErrorCode
|= errorstate
;
533 hsd
->State
= HAL_SD_STATE_READY
;
537 /* Configure the SD DPSM (Data Path State Machine) */
538 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
539 config
.DataLength
= NumberOfBlocks
* BLOCKSIZE
;
540 config
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_512B
;
541 config
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
542 config
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
543 config
.DPSM
= SDMMC_DPSM_ENABLE
;
544 SDMMC_ConfigData(hsd
->Instance
, &config
);
546 /* Read block(s) in polling mode */
547 if(NumberOfBlocks
> 1)
549 hsd
->Context
= SD_CONTEXT_READ_MULTIPLE_BLOCK
;
551 /* Read Multi Block command */
552 errorstate
= SDMMC_CmdReadMultiBlock(hsd
->Instance
, BlockAdd
);
556 hsd
->Context
= SD_CONTEXT_READ_SINGLE_BLOCK
;
558 /* Read Single Block command */
559 errorstate
= SDMMC_CmdReadSingleBlock(hsd
->Instance
, BlockAdd
);
561 if(errorstate
!= HAL_SD_ERROR_NONE
)
563 /* Clear all the static flags */
564 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
565 hsd
->ErrorCode
|= errorstate
;
566 hsd
->State
= HAL_SD_STATE_READY
;
570 /* Poll on SDMMC flags */
571 while(!__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DATAEND
))
573 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXFIFOHF
))
575 /* Read data from SDMMC Rx FIFO */
576 for(count
= 0U; count
< 8U; count
++)
578 *(tempbuff
+ count
) = SDMMC_ReadFIFO(hsd
->Instance
);
583 if((Timeout
== 0U)||((HAL_GetTick()-tickstart
) >= Timeout
))
585 /* Clear all the static flags */
586 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
587 hsd
->ErrorCode
|= HAL_SD_ERROR_TIMEOUT
;
588 hsd
->State
= HAL_SD_STATE_READY
;
593 /* Send stop transmission command in case of multiblock read */
594 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DATAEND
) && (NumberOfBlocks
> 1U))
596 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
598 /* Send stop transmission command */
599 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
600 if(errorstate
!= HAL_SD_ERROR_NONE
)
602 /* Clear all the static flags */
603 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
604 hsd
->ErrorCode
|= errorstate
;
605 hsd
->State
= HAL_SD_STATE_READY
;
611 /* Get error state */
612 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
))
614 /* Clear all the static flags */
615 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
616 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_TIMEOUT
;
617 hsd
->State
= HAL_SD_STATE_READY
;
620 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
))
622 /* Clear all the static flags */
623 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
624 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_CRC_FAIL
;
625 hsd
->State
= HAL_SD_STATE_READY
;
628 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
))
630 /* Clear all the static flags */
631 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
632 hsd
->ErrorCode
|= HAL_SD_ERROR_RX_OVERRUN
;
633 hsd
->State
= HAL_SD_STATE_READY
;
637 /* Empty FIFO if there is still any data */
638 while ((__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXDAVL
)))
640 *tempbuff
= SDMMC_ReadFIFO(hsd
->Instance
);
643 if((Timeout
== 0U)||((HAL_GetTick()-tickstart
) >= Timeout
))
645 /* Clear all the static flags */
646 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
647 hsd
->ErrorCode
|= HAL_SD_ERROR_TIMEOUT
;
648 hsd
->State
= HAL_SD_STATE_READY
;
653 /* Clear all the static flags */
654 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
656 hsd
->State
= HAL_SD_STATE_READY
;
662 hsd
->ErrorCode
|= HAL_SD_ERROR_BUSY
;
668 * @brief Allows to write block(s) to a specified address in a card. The Data
669 * transfer is managed by polling mode.
670 * @note This API should be followed by a check on the card state through
671 * HAL_SD_GetCardState().
672 * @param hsd: Pointer to SD handle
673 * @param pData: pointer to the buffer that will contain the data to transmit
674 * @param BlockAdd: Block Address where data will be written
675 * @param NumberOfBlocks: Number of SD blocks to write
676 * @param Timeout: Specify timeout value
679 HAL_StatusTypeDef
HAL_SD_WriteBlocks(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
, uint32_t Timeout
)
681 SDMMC_DataInitTypeDef config
;
682 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
683 uint32_t tickstart
= HAL_GetTick();
685 uint32_t *tempbuff
= (uint32_t *)pData
;
689 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
693 if(hsd
->State
== HAL_SD_STATE_READY
)
695 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
697 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
699 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
703 hsd
->State
= HAL_SD_STATE_BUSY
;
705 /* Initialize data control register */
706 hsd
->Instance
->DCTRL
= 0;
708 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
713 /* Set Block Size for Card */
714 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
715 if(errorstate
!= HAL_SD_ERROR_NONE
)
717 /* Clear all the static flags */
718 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
719 hsd
->ErrorCode
|= errorstate
;
720 hsd
->State
= HAL_SD_STATE_READY
;
724 /* Write Blocks in Polling mode */
725 if(NumberOfBlocks
> 1U)
727 hsd
->Context
= SD_CONTEXT_WRITE_MULTIPLE_BLOCK
;
729 /* Write Multi Block command */
730 errorstate
= SDMMC_CmdWriteMultiBlock(hsd
->Instance
, BlockAdd
);
734 hsd
->Context
= SD_CONTEXT_WRITE_SINGLE_BLOCK
;
736 /* Write Single Block command */
737 errorstate
= SDMMC_CmdWriteSingleBlock(hsd
->Instance
, BlockAdd
);
739 if(errorstate
!= HAL_SD_ERROR_NONE
)
741 /* Clear all the static flags */
742 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
743 hsd
->ErrorCode
|= errorstate
;
744 hsd
->State
= HAL_SD_STATE_READY
;
748 /* Configure the SD DPSM (Data Path State Machine) */
749 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
750 config
.DataLength
= NumberOfBlocks
* BLOCKSIZE
;
751 config
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_512B
;
752 config
.TransferDir
= SDMMC_TRANSFER_DIR_TO_CARD
;
753 config
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
754 config
.DPSM
= SDMMC_DPSM_ENABLE
;
755 SDMMC_ConfigData(hsd
->Instance
, &config
);
757 /* Write block(s) in polling mode */
758 while(!__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_TXUNDERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DATAEND
))
760 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_TXFIFOHE
))
762 /* Write data to SDMMC Tx FIFO */
763 for(count
= 0U; count
< 8U; count
++)
765 SDMMC_WriteFIFO(hsd
->Instance
, (tempbuff
+ count
));
770 if((Timeout
== 0U)||((HAL_GetTick()-tickstart
) >= Timeout
))
772 /* Clear all the static flags */
773 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
774 hsd
->ErrorCode
|= errorstate
;
775 hsd
->State
= HAL_SD_STATE_READY
;
780 /* Send stop transmission command in case of multiblock write */
781 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DATAEND
) && (NumberOfBlocks
> 1U))
783 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
785 /* Send stop transmission command */
786 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
787 if(errorstate
!= HAL_SD_ERROR_NONE
)
789 /* Clear all the static flags */
790 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
791 hsd
->ErrorCode
|= errorstate
;
792 hsd
->State
= HAL_SD_STATE_READY
;
798 /* Get error state */
799 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
))
801 /* Clear all the static flags */
802 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
803 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_TIMEOUT
;
804 hsd
->State
= HAL_SD_STATE_READY
;
807 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
))
809 /* Clear all the static flags */
810 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
811 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_CRC_FAIL
;
812 hsd
->State
= HAL_SD_STATE_READY
;
815 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_TXUNDERR
))
817 /* Clear all the static flags */
818 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
819 hsd
->ErrorCode
|= HAL_SD_ERROR_TX_UNDERRUN
;
820 hsd
->State
= HAL_SD_STATE_READY
;
824 /* Clear all the static flags */
825 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
827 hsd
->State
= HAL_SD_STATE_READY
;
833 hsd
->ErrorCode
|= HAL_SD_ERROR_BUSY
;
839 * @brief Reads block(s) from a specified address in a card. The Data transfer
840 * is managed in interrupt mode.
841 * @note This API should be followed by a check on the card state through
842 * HAL_SD_GetCardState().
843 * @note You could also check the IT transfer process through the SD Rx
845 * @param hsd: Pointer to SD handle
846 * @param pData: Pointer to the buffer that will contain the received data
847 * @param BlockAdd: Block Address from where data is to be read
848 * @param NumberOfBlocks: Number of blocks to read.
851 HAL_StatusTypeDef
HAL_SD_ReadBlocks_IT(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
)
853 SDMMC_DataInitTypeDef config
;
854 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
858 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
862 if(hsd
->State
== HAL_SD_STATE_READY
)
864 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
866 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
868 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
872 hsd
->State
= HAL_SD_STATE_BUSY
;
874 /* Initialize data control register */
875 hsd
->Instance
->DCTRL
= 0U;
877 hsd
->pRxBuffPtr
= (uint32_t *)pData
;
878 hsd
->RxXferSize
= BLOCKSIZE
* NumberOfBlocks
;
880 __HAL_SD_ENABLE_IT(hsd
, (SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
| SDMMC_IT_RXOVERR
| SDMMC_IT_DATAEND
| SDMMC_FLAG_RXFIFOHF
));
882 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
887 /* Configure the SD DPSM (Data Path State Machine) */
888 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
889 config
.DataLength
= BLOCKSIZE
* NumberOfBlocks
;
890 config
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_512B
;
891 config
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
892 config
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
893 config
.DPSM
= SDMMC_DPSM_ENABLE
;
894 SDMMC_ConfigData(hsd
->Instance
, &config
);
896 /* Set Block Size for Card */
897 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
898 if(errorstate
!= HAL_SD_ERROR_NONE
)
900 /* Clear all the static flags */
901 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
902 hsd
->ErrorCode
|= errorstate
;
903 hsd
->State
= HAL_SD_STATE_READY
;
907 /* Read Blocks in IT mode */
908 if(NumberOfBlocks
> 1U)
910 hsd
->Context
= (SD_CONTEXT_READ_MULTIPLE_BLOCK
| SD_CONTEXT_IT
);
912 /* Read Multi Block command */
913 errorstate
= SDMMC_CmdReadMultiBlock(hsd
->Instance
, BlockAdd
);
917 hsd
->Context
= (SD_CONTEXT_READ_SINGLE_BLOCK
| SD_CONTEXT_IT
);
919 /* Read Single Block command */
920 errorstate
= SDMMC_CmdReadSingleBlock(hsd
->Instance
, BlockAdd
);
922 if(errorstate
!= HAL_SD_ERROR_NONE
)
924 /* Clear all the static flags */
925 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
926 hsd
->ErrorCode
|= errorstate
;
927 hsd
->State
= HAL_SD_STATE_READY
;
940 * @brief Writes block(s) to a specified address in a card. The Data transfer
941 * is managed in interrupt mode.
942 * @note This API should be followed by a check on the card state through
943 * HAL_SD_GetCardState().
944 * @note You could also check the IT transfer process through the SD Tx
946 * @param hsd: Pointer to SD handle
947 * @param pData: Pointer to the buffer that will contain the data to transmit
948 * @param BlockAdd: Block Address where data will be written
949 * @param NumberOfBlocks: Number of blocks to write
952 HAL_StatusTypeDef
HAL_SD_WriteBlocks_IT(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
)
954 SDMMC_DataInitTypeDef config
;
955 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
959 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
963 if(hsd
->State
== HAL_SD_STATE_READY
)
965 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
967 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
969 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
973 hsd
->State
= HAL_SD_STATE_BUSY
;
975 /* Initialize data control register */
976 hsd
->Instance
->DCTRL
= 0U;
978 hsd
->pTxBuffPtr
= (uint32_t *)pData
;
979 hsd
->TxXferSize
= BLOCKSIZE
* NumberOfBlocks
;
981 /* Enable transfer interrupts */
982 __HAL_SD_ENABLE_IT(hsd
, (SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
| SDMMC_IT_TXUNDERR
| SDMMC_IT_DATAEND
| SDMMC_FLAG_TXFIFOHE
));
984 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
989 /* Set Block Size for Card */
990 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
991 if(errorstate
!= HAL_SD_ERROR_NONE
)
993 /* Clear all the static flags */
994 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
995 hsd
->ErrorCode
|= errorstate
;
996 hsd
->State
= HAL_SD_STATE_READY
;
1000 /* Write Blocks in Polling mode */
1001 if(NumberOfBlocks
> 1U)
1003 hsd
->Context
= (SD_CONTEXT_WRITE_MULTIPLE_BLOCK
| SD_CONTEXT_IT
);
1005 /* Write Multi Block command */
1006 errorstate
= SDMMC_CmdWriteMultiBlock(hsd
->Instance
, BlockAdd
);
1010 hsd
->Context
= (SD_CONTEXT_WRITE_SINGLE_BLOCK
| SD_CONTEXT_IT
);
1012 /* Write Single Block command */
1013 errorstate
= SDMMC_CmdWriteSingleBlock(hsd
->Instance
, BlockAdd
);
1015 if(errorstate
!= HAL_SD_ERROR_NONE
)
1017 /* Clear all the static flags */
1018 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1019 hsd
->ErrorCode
|= errorstate
;
1020 hsd
->State
= HAL_SD_STATE_READY
;
1024 /* Configure the SD DPSM (Data Path State Machine) */
1025 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
1026 config
.DataLength
= BLOCKSIZE
* NumberOfBlocks
;
1027 config
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_512B
;
1028 config
.TransferDir
= SDMMC_TRANSFER_DIR_TO_CARD
;
1029 config
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
1030 config
.DPSM
= SDMMC_DPSM_ENABLE
;
1031 SDMMC_ConfigData(hsd
->Instance
, &config
);
1042 * @brief Reads block(s) from a specified address in a card. The Data transfer
1043 * is managed by DMA mode.
1044 * @note This API should be followed by a check on the card state through
1045 * HAL_SD_GetCardState().
1046 * @note You could also check the DMA transfer process through the SD Rx
1048 * @param hsd: Pointer SD handle
1049 * @param pData: Pointer to the buffer that will contain the received data
1050 * @param BlockAdd: Block Address from where data is to be read
1051 * @param NumberOfBlocks: Number of blocks to read.
1052 * @retval HAL status
1054 HAL_StatusTypeDef
HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
)
1056 SDMMC_DataInitTypeDef config
;
1057 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1061 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
1065 if(hsd
->State
== HAL_SD_STATE_READY
)
1067 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
1069 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
1071 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
1075 hsd
->State
= HAL_SD_STATE_BUSY
;
1077 /* Initialize data control register */
1078 hsd
->Instance
->DCTRL
= 0U;
1080 __HAL_SD_ENABLE_IT(hsd
, (SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
| SDMMC_IT_RXOVERR
| SDMMC_IT_DATAEND
));
1082 /* Set the DMA transfer complete callback */
1083 hsd
->hdmarx
->XferCpltCallback
= SD_DMAReceiveCplt
;
1085 /* Set the DMA error callback */
1086 hsd
->hdmarx
->XferErrorCallback
= SD_DMAError
;
1088 /* Set the DMA Abort callback */
1089 hsd
->hdmarx
->XferAbortCallback
= NULL
;
1091 /* Enable the DMA Channel */
1092 HAL_DMA_Start_IT(hsd
->hdmarx
, (uint32_t)&hsd
->Instance
->FIFO
, (uint32_t)pData
, (uint32_t)(BLOCKSIZE
* NumberOfBlocks
)/4);
1094 /* Enable SD DMA transfer */
1095 __HAL_SD_DMA_ENABLE(hsd
);
1097 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
1102 /* Configure the SD DPSM (Data Path State Machine) */
1103 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
1104 config
.DataLength
= BLOCKSIZE
* NumberOfBlocks
;
1105 config
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_512B
;
1106 config
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
1107 config
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
1108 config
.DPSM
= SDMMC_DPSM_ENABLE
;
1109 SDMMC_ConfigData(hsd
->Instance
, &config
);
1111 /* Set Block Size for Card */
1112 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
1113 if(errorstate
!= HAL_SD_ERROR_NONE
)
1115 /* Clear all the static flags */
1116 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1117 hsd
->ErrorCode
|= errorstate
;
1118 hsd
->State
= HAL_SD_STATE_READY
;
1122 /* Read Blocks in DMA mode */
1123 if(NumberOfBlocks
> 1U)
1125 hsd
->Context
= (SD_CONTEXT_READ_MULTIPLE_BLOCK
| SD_CONTEXT_DMA
);
1127 /* Read Multi Block command */
1128 errorstate
= SDMMC_CmdReadMultiBlock(hsd
->Instance
, BlockAdd
);
1132 hsd
->Context
= (SD_CONTEXT_READ_SINGLE_BLOCK
| SD_CONTEXT_DMA
);
1134 /* Read Single Block command */
1135 errorstate
= SDMMC_CmdReadSingleBlock(hsd
->Instance
, BlockAdd
);
1137 if(errorstate
!= HAL_SD_ERROR_NONE
)
1139 /* Clear all the static flags */
1140 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1141 hsd
->ErrorCode
|= errorstate
;
1142 hsd
->State
= HAL_SD_STATE_READY
;
1155 * @brief Writes block(s) to a specified address in a card. The Data transfer
1156 * is managed by DMA mode.
1157 * @note This API should be followed by a check on the card state through
1158 * HAL_SD_GetCardState().
1159 * @note You could also check the DMA transfer process through the SD Tx
1161 * @param hsd: Pointer to SD handle
1162 * @param pData: Pointer to the buffer that will contain the data to transmit
1163 * @param BlockAdd: Block Address where data will be written
1164 * @param NumberOfBlocks: Number of blocks to write
1165 * @retval HAL status
1167 HAL_StatusTypeDef
HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
)
1169 SDMMC_DataInitTypeDef config
;
1170 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1174 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
1178 if(hsd
->State
== HAL_SD_STATE_READY
)
1180 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
1182 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
1184 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
1188 hsd
->State
= HAL_SD_STATE_BUSY
;
1190 /* Initialize data control register */
1191 hsd
->Instance
->DCTRL
= 0U;
1193 /* Enable SD Error interrupts */
1194 __HAL_SD_ENABLE_IT(hsd
, (SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
| SDMMC_IT_TXUNDERR
));
1196 /* Set the DMA transfer complete callback */
1197 hsd
->hdmatx
->XferCpltCallback
= SD_DMATransmitCplt
;
1199 /* Set the DMA error callback */
1200 hsd
->hdmatx
->XferErrorCallback
= SD_DMAError
;
1202 /* Set the DMA Abort callback */
1203 hsd
->hdmatx
->XferAbortCallback
= NULL
;
1205 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
1210 /* Set Block Size for Card */
1211 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
1212 if(errorstate
!= HAL_SD_ERROR_NONE
)
1214 /* Clear all the static flags */
1215 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1216 hsd
->ErrorCode
|= errorstate
;
1217 hsd
->State
= HAL_SD_STATE_READY
;
1221 /* Write Blocks in Polling mode */
1222 if(NumberOfBlocks
> 1U)
1224 hsd
->Context
= (SD_CONTEXT_WRITE_MULTIPLE_BLOCK
| SD_CONTEXT_DMA
);
1226 /* Write Multi Block command */
1227 errorstate
= SDMMC_CmdWriteMultiBlock(hsd
->Instance
, BlockAdd
);
1231 hsd
->Context
= (SD_CONTEXT_WRITE_SINGLE_BLOCK
| SD_CONTEXT_DMA
);
1233 /* Write Single Block command */
1234 errorstate
= SDMMC_CmdWriteSingleBlock(hsd
->Instance
, BlockAdd
);
1236 if(errorstate
!= HAL_SD_ERROR_NONE
)
1238 /* Clear all the static flags */
1239 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1240 hsd
->ErrorCode
|= errorstate
;
1241 hsd
->State
= HAL_SD_STATE_READY
;
1245 /* Enable SDMMC DMA transfer */
1246 __HAL_SD_DMA_ENABLE(hsd
);
1248 /* Enable the DMA Channel */
1249 HAL_DMA_Start_IT(hsd
->hdmatx
, (uint32_t)pData
, (uint32_t)&hsd
->Instance
->FIFO
, (uint32_t)(BLOCKSIZE
* NumberOfBlocks
)/4);
1251 /* Configure the SD DPSM (Data Path State Machine) */
1252 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
1253 config
.DataLength
= BLOCKSIZE
* NumberOfBlocks
;
1254 config
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_512B
;
1255 config
.TransferDir
= SDMMC_TRANSFER_DIR_TO_CARD
;
1256 config
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
1257 config
.DPSM
= SDMMC_DPSM_ENABLE
;
1258 SDMMC_ConfigData(hsd
->Instance
, &config
);
1269 * @brief Erases the specified memory area of the given SD card.
1270 * @note This API should be followed by a check on the card state through
1271 * HAL_SD_GetCardState().
1272 * @param hsd: Pointer to SD handle
1273 * @param BlockStartAdd: Start Block address
1274 * @param BlockEndAdd: End Block address
1275 * @retval HAL status
1277 HAL_StatusTypeDef
HAL_SD_Erase(SD_HandleTypeDef
*hsd
, uint32_t BlockStartAdd
, uint32_t BlockEndAdd
)
1279 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1281 if(hsd
->State
== HAL_SD_STATE_READY
)
1283 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
1285 if(BlockEndAdd
< BlockStartAdd
)
1287 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
1291 if(BlockEndAdd
> (hsd
->SdCard
.LogBlockNbr
))
1293 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
1297 hsd
->State
= HAL_SD_STATE_BUSY
;
1299 /* Check if the card command class supports erase command */
1300 if(((hsd
->SdCard
.Class
) & SDMMC_CCCC_ERASE
) == 0U)
1302 /* Clear all the static flags */
1303 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1304 hsd
->ErrorCode
|= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
1305 hsd
->State
= HAL_SD_STATE_READY
;
1309 if((SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
1311 /* Clear all the static flags */
1312 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1313 hsd
->ErrorCode
|= HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
1314 hsd
->State
= HAL_SD_STATE_READY
;
1318 /* Get start and end block for high capacity cards */
1319 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
1321 BlockStartAdd
*= 512U;
1322 BlockEndAdd
*= 512U;
1325 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1326 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
1328 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1329 errorstate
= SDMMC_CmdSDEraseStartAdd(hsd
->Instance
, BlockStartAdd
);
1330 if(errorstate
!= HAL_SD_ERROR_NONE
)
1332 /* Clear all the static flags */
1333 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1334 hsd
->ErrorCode
|= errorstate
;
1335 hsd
->State
= HAL_SD_STATE_READY
;
1339 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1340 errorstate
= SDMMC_CmdSDEraseEndAdd(hsd
->Instance
, BlockEndAdd
);
1341 if(errorstate
!= HAL_SD_ERROR_NONE
)
1343 /* Clear all the static flags */
1344 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1345 hsd
->ErrorCode
|= errorstate
;
1346 hsd
->State
= HAL_SD_STATE_READY
;
1351 /* Send CMD38 ERASE */
1352 errorstate
= SDMMC_CmdErase(hsd
->Instance
);
1353 if(errorstate
!= HAL_SD_ERROR_NONE
)
1355 /* Clear all the static flags */
1356 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1357 hsd
->ErrorCode
|= errorstate
;
1358 hsd
->State
= HAL_SD_STATE_READY
;
1362 hsd
->State
= HAL_SD_STATE_READY
;
1373 * @brief This function handles SD card interrupt request.
1374 * @param hsd: Pointer to SD handle
1377 void HAL_SD_IRQHandler(SD_HandleTypeDef
*hsd
)
1379 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1381 /* Check for SDMMC interrupt flags */
1382 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_IT_DATAEND
) != RESET
)
1384 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_FLAG_DATAEND
);
1386 __HAL_SD_DISABLE_IT(hsd
, SDMMC_IT_DATAEND
| SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
|\
1387 SDMMC_IT_TXUNDERR
| SDMMC_IT_RXOVERR
);
1389 if((hsd
->Context
& SD_CONTEXT_IT
) != RESET
)
1391 if(((hsd
->Context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) != RESET
) || ((hsd
->Context
& SD_CONTEXT_WRITE_MULTIPLE_BLOCK
) != RESET
))
1393 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
1394 if(errorstate
!= HAL_SD_ERROR_NONE
)
1396 hsd
->ErrorCode
|= errorstate
;
1397 HAL_SD_ErrorCallback(hsd
);
1401 /* Clear all the static flags */
1402 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1404 hsd
->State
= HAL_SD_STATE_READY
;
1405 if(((hsd
->Context
& SD_CONTEXT_READ_SINGLE_BLOCK
) != RESET
) || ((hsd
->Context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) != RESET
))
1407 HAL_SD_RxCpltCallback(hsd
);
1411 HAL_SD_TxCpltCallback(hsd
);
1414 else if((hsd
->Context
& SD_CONTEXT_DMA
) != RESET
)
1416 if((hsd
->Context
& SD_CONTEXT_WRITE_MULTIPLE_BLOCK
) != RESET
)
1418 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
1419 if(errorstate
!= HAL_SD_ERROR_NONE
)
1421 hsd
->ErrorCode
|= errorstate
;
1422 HAL_SD_ErrorCallback(hsd
);
1425 if(((hsd
->Context
& SD_CONTEXT_READ_SINGLE_BLOCK
) == RESET
) && ((hsd
->Context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) == RESET
))
1427 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1428 in the SD DCTRL register */
1429 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN
);
1431 hsd
->State
= HAL_SD_STATE_READY
;
1433 HAL_SD_TxCpltCallback(hsd
);
1438 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_IT_TXFIFOHE
) != RESET
)
1440 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_FLAG_TXFIFOHE
);
1445 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_IT_RXFIFOHF
) != RESET
)
1447 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_FLAG_RXFIFOHF
);
1452 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
| SDMMC_IT_RXOVERR
| SDMMC_IT_TXUNDERR
) != RESET
)
1454 /* Set Error code */
1455 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_IT_DCRCFAIL
) != RESET
)
1457 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_CRC_FAIL
;
1459 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_IT_DTIMEOUT
) != RESET
)
1461 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_TIMEOUT
;
1463 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_IT_RXOVERR
) != RESET
)
1465 hsd
->ErrorCode
|= HAL_SD_ERROR_RX_OVERRUN
;
1467 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_IT_TXUNDERR
) != RESET
)
1469 hsd
->ErrorCode
|= HAL_SD_ERROR_TX_UNDERRUN
;
1472 /* Clear All flags */
1473 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1475 /* Disable all interrupts */
1476 __HAL_SD_DISABLE_IT(hsd
, SDMMC_IT_DATAEND
| SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
|\
1477 SDMMC_IT_TXUNDERR
| SDMMC_IT_RXOVERR
);
1479 if((hsd
->Context
& SD_CONTEXT_DMA
) != RESET
)
1481 /* Abort the SD DMA Streams */
1482 if(hsd
->hdmatx
!= NULL
)
1484 /* Set the DMA Tx abort callback */
1485 hsd
->hdmatx
->XferAbortCallback
= SD_DMATxAbort
;
1486 /* Abort DMA in IT mode */
1487 if(HAL_DMA_Abort_IT(hsd
->hdmatx
) != HAL_OK
)
1489 SD_DMATxAbort(hsd
->hdmatx
);
1492 else if(hsd
->hdmarx
!= NULL
)
1494 /* Set the DMA Rx abort callback */
1495 hsd
->hdmarx
->XferAbortCallback
= SD_DMARxAbort
;
1496 /* Abort DMA in IT mode */
1497 if(HAL_DMA_Abort_IT(hsd
->hdmarx
) != HAL_OK
)
1499 SD_DMARxAbort(hsd
->hdmarx
);
1504 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
1505 hsd
->State
= HAL_SD_STATE_READY
;
1506 HAL_SD_AbortCallback(hsd
);
1509 else if((hsd
->Context
& SD_CONTEXT_IT
) != RESET
)
1511 /* Set the SD state to ready to be able to start again the process */
1512 hsd
->State
= HAL_SD_STATE_READY
;
1513 HAL_SD_ErrorCallback(hsd
);
1519 * @brief return the SD state
1520 * @param hsd: Pointer to sd handle
1523 HAL_SD_StateTypeDef
HAL_SD_GetState(SD_HandleTypeDef
*hsd
)
1529 * @brief Return the SD error code
1530 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1531 * the configuration information.
1532 * @retval SD Error Code
1534 uint32_t HAL_SD_GetError(SD_HandleTypeDef
*hsd
)
1536 return hsd
->ErrorCode
;
1540 * @brief Tx Transfer completed callbacks
1541 * @param hsd: Pointer to SD handle
1544 __weak
void HAL_SD_TxCpltCallback(SD_HandleTypeDef
*hsd
)
1546 /* Prevent unused argument(s) compilation warning */
1549 /* NOTE : This function should not be modified, when the callback is needed,
1550 the HAL_SD_TxCpltCallback can be implemented in the user file
1555 * @brief Rx Transfer completed callbacks
1556 * @param hsd: Pointer SD handle
1559 __weak
void HAL_SD_RxCpltCallback(SD_HandleTypeDef
*hsd
)
1561 /* Prevent unused argument(s) compilation warning */
1564 /* NOTE : This function should not be modified, when the callback is needed,
1565 the HAL_SD_RxCpltCallback can be implemented in the user file
1570 * @brief SD error callbacks
1571 * @param hsd: Pointer SD handle
1574 __weak
void HAL_SD_ErrorCallback(SD_HandleTypeDef
*hsd
)
1576 /* Prevent unused argument(s) compilation warning */
1579 /* NOTE : This function should not be modified, when the callback is needed,
1580 the HAL_SD_ErrorCallback can be implemented in the user file
1585 * @brief SD Abort callbacks
1586 * @param hsd: Pointer SD handle
1589 __weak
void HAL_SD_AbortCallback(SD_HandleTypeDef
*hsd
)
1591 /* Prevent unused argument(s) compilation warning */
1594 /* NOTE : This function should not be modified, when the callback is needed,
1595 the HAL_SD_ErrorCallback can be implemented in the user file
1604 /** @addtogroup SD_Exported_Functions_Group3
1605 * @brief management functions
1608 ==============================================================================
1609 ##### Peripheral Control functions #####
1610 ==============================================================================
1612 This subsection provides a set of functions allowing to control the SD card
1613 operations and get the related information
1620 * @brief Returns information the information of the card which are stored on
1622 * @param hsd: Pointer to SD handle
1623 * @param pCID: Pointer to a HAL_SD_CardCIDTypeDef structure that
1624 * contains all CID register parameters
1625 * @retval HAL status
1627 HAL_StatusTypeDef
HAL_SD_GetCardCID(SD_HandleTypeDef
*hsd
, HAL_SD_CardCIDTypeDef
*pCID
)
1632 tmp
= (uint8_t)((hsd
->CID
[0] & 0xFF000000U
) >> 24);
1633 pCID
->ManufacturerID
= tmp
;
1636 tmp
= (uint8_t)((hsd
->CID
[0] & 0x00FF0000) >> 16);
1637 pCID
->OEM_AppliID
= tmp
<< 8;
1640 tmp
= (uint8_t)((hsd
->CID
[0] & 0x000000FF00) >> 8);
1641 pCID
->OEM_AppliID
|= tmp
;
1644 tmp
= (uint8_t)(hsd
->CID
[0] & 0x000000FF);
1645 pCID
->ProdName1
= tmp
<< 24;
1648 tmp
= (uint8_t)((hsd
->CID
[1] & 0xFF000000U
) >> 24);
1649 pCID
->ProdName1
|= tmp
<< 16;
1652 tmp
= (uint8_t)((hsd
->CID
[1] & 0x00FF0000) >> 16);
1653 pCID
->ProdName1
|= tmp
<< 8;
1656 tmp
= (uint8_t)((hsd
->CID
[1] & 0x0000FF00) >> 8);
1657 pCID
->ProdName1
|= tmp
;
1660 tmp
= (uint8_t)(hsd
->CID
[1] & 0x000000FF);
1661 pCID
->ProdName2
= tmp
;
1664 tmp
= (uint8_t)((hsd
->CID
[2] & 0xFF000000U
) >> 24);
1665 pCID
->ProdRev
= tmp
;
1668 tmp
= (uint8_t)((hsd
->CID
[2] & 0x00FF0000) >> 16);
1669 pCID
->ProdSN
= tmp
<< 24;
1672 tmp
= (uint8_t)((hsd
->CID
[2] & 0x0000FF00) >> 8);
1673 pCID
->ProdSN
|= tmp
<< 16;
1676 tmp
= (uint8_t)(hsd
->CID
[2] & 0x000000FF);
1677 pCID
->ProdSN
|= tmp
<< 8;
1680 tmp
= (uint8_t)((hsd
->CID
[3] & 0xFF000000U
) >> 24);
1681 pCID
->ProdSN
|= tmp
;
1684 tmp
= (uint8_t)((hsd
->CID
[3] & 0x00FF0000) >> 16);
1685 pCID
->Reserved1
|= (tmp
& 0xF0) >> 4;
1686 pCID
->ManufactDate
= (tmp
& 0x0F) << 8;
1689 tmp
= (uint8_t)((hsd
->CID
[3] & 0x0000FF00) >> 8);
1690 pCID
->ManufactDate
|= tmp
;
1693 tmp
= (uint8_t)(hsd
->CID
[3] & 0x000000FF);
1694 pCID
->CID_CRC
= (tmp
& 0xFE) >> 1;
1695 pCID
->Reserved2
= 1;
1701 * @brief Returns information the information of the card which are stored on
1703 * @param hsd: Pointer to SD handle
1704 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
1705 * contains all CSD register parameters
1706 * @retval HAL status
1708 HAL_StatusTypeDef
HAL_SD_GetCardCSD(SD_HandleTypeDef
*hsd
, HAL_SD_CardCSDTypeDef
*pCSD
)
1713 tmp
= (hsd
->CSD
[0] & 0xFF000000U
) >> 24;
1714 pCSD
->CSDStruct
= (uint8_t)((tmp
& 0xC0) >> 6);
1715 pCSD
->SysSpecVersion
= (uint8_t)((tmp
& 0x3C) >> 2);
1716 pCSD
->Reserved1
= tmp
& 0x03;
1719 tmp
= (hsd
->CSD
[0] & 0x00FF0000) >> 16;
1720 pCSD
->TAAC
= (uint8_t)tmp
;
1723 tmp
= (hsd
->CSD
[0] & 0x0000FF00) >> 8;
1724 pCSD
->NSAC
= (uint8_t)tmp
;
1727 tmp
= hsd
->CSD
[0] & 0x000000FF;
1728 pCSD
->MaxBusClkFrec
= (uint8_t)tmp
;
1731 tmp
= (hsd
->CSD
[1] & 0xFF000000U
) >> 24;
1732 pCSD
->CardComdClasses
= (uint16_t)(tmp
<< 4);
1735 tmp
= (hsd
->CSD
[1] & 0x00FF0000U
) >> 16;
1736 pCSD
->CardComdClasses
|= (uint16_t)((tmp
& 0xF0) >> 4);
1737 pCSD
->RdBlockLen
= (uint8_t)(tmp
& 0x0F);
1740 tmp
= (hsd
->CSD
[1] & 0x0000FF00U
) >> 8;
1741 pCSD
->PartBlockRead
= (uint8_t)((tmp
& 0x80) >> 7);
1742 pCSD
->WrBlockMisalign
= (uint8_t)((tmp
& 0x40) >> 6);
1743 pCSD
->RdBlockMisalign
= (uint8_t)((tmp
& 0x20) >> 5);
1744 pCSD
->DSRImpl
= (uint8_t)((tmp
& 0x10) >> 4);
1745 pCSD
->Reserved2
= 0; /*!< Reserved */
1747 if(hsd
->SdCard
.CardType
== CARD_SDSC
)
1749 pCSD
->DeviceSize
= (tmp
& 0x03) << 10;
1752 tmp
= (uint8_t)(hsd
->CSD
[1] & 0x000000FFU
);
1753 pCSD
->DeviceSize
|= (tmp
) << 2;
1756 tmp
= (uint8_t)((hsd
->CSD
[2] & 0xFF000000U
) >> 24);
1757 pCSD
->DeviceSize
|= (tmp
& 0xC0) >> 6;
1759 pCSD
->MaxRdCurrentVDDMin
= (tmp
& 0x38) >> 3;
1760 pCSD
->MaxRdCurrentVDDMax
= (tmp
& 0x07);
1763 tmp
= (uint8_t)((hsd
->CSD
[2] & 0x00FF0000U
) >> 16);
1764 pCSD
->MaxWrCurrentVDDMin
= (tmp
& 0xE0) >> 5;
1765 pCSD
->MaxWrCurrentVDDMax
= (tmp
& 0x1C) >> 2;
1766 pCSD
->DeviceSizeMul
= (tmp
& 0x03) << 1;
1768 tmp
= (uint8_t)((hsd
->CSD
[2] & 0x0000FF00U
) >> 8);
1769 pCSD
->DeviceSizeMul
|= (tmp
& 0x80) >> 7;
1771 hsd
->SdCard
.BlockNbr
= (pCSD
->DeviceSize
+ 1) ;
1772 hsd
->SdCard
.BlockNbr
*= (1 << (pCSD
->DeviceSizeMul
+ 2));
1773 hsd
->SdCard
.BlockSize
= 1 << (pCSD
->RdBlockLen
);
1775 hsd
->SdCard
.LogBlockNbr
= (hsd
->SdCard
.BlockNbr
) * ((hsd
->SdCard
.BlockSize
) / 512);
1776 hsd
->SdCard
.LogBlockSize
= 512;
1778 else if(hsd
->SdCard
.CardType
== CARD_SDHC_SDXC
)
1781 tmp
= (uint8_t)(hsd
->CSD
[1] & 0x000000FFU
);
1782 pCSD
->DeviceSize
= (tmp
& 0x3F) << 16;
1785 tmp
= (uint8_t)((hsd
->CSD
[2] & 0xFF000000U
) >> 24);
1787 pCSD
->DeviceSize
|= (tmp
<< 8);
1790 tmp
= (uint8_t)((hsd
->CSD
[2] & 0x00FF0000U
) >> 16);
1792 pCSD
->DeviceSize
|= (tmp
);
1795 tmp
= (uint8_t)((hsd
->CSD
[2] & 0x0000FF00U
) >> 8);
1797 hsd
->SdCard
.LogBlockNbr
= hsd
->SdCard
.BlockNbr
= (((uint64_t)pCSD
->DeviceSize
+ 1) * 1024);
1798 hsd
->SdCard
.LogBlockSize
= hsd
->SdCard
.BlockSize
= 512;
1802 /* Clear all the static flags */
1803 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1804 hsd
->ErrorCode
|= HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
1805 hsd
->State
= HAL_SD_STATE_READY
;
1809 pCSD
->EraseGrSize
= (tmp
& 0x40) >> 6;
1810 pCSD
->EraseGrMul
= (tmp
& 0x3F) << 1;
1813 tmp
= (uint8_t)(hsd
->CSD
[2] & 0x000000FF);
1814 pCSD
->EraseGrMul
|= (tmp
& 0x80) >> 7;
1815 pCSD
->WrProtectGrSize
= (tmp
& 0x7F);
1818 tmp
= (uint8_t)((hsd
->CSD
[3] & 0xFF000000U
) >> 24);
1819 pCSD
->WrProtectGrEnable
= (tmp
& 0x80) >> 7;
1820 pCSD
->ManDeflECC
= (tmp
& 0x60) >> 5;
1821 pCSD
->WrSpeedFact
= (tmp
& 0x1C) >> 2;
1822 pCSD
->MaxWrBlockLen
= (tmp
& 0x03) << 2;
1825 tmp
= (uint8_t)((hsd
->CSD
[3] & 0x00FF0000) >> 16);
1826 pCSD
->MaxWrBlockLen
|= (tmp
& 0xC0) >> 6;
1827 pCSD
->WriteBlockPaPartial
= (tmp
& 0x20) >> 5;
1828 pCSD
->Reserved3
= 0;
1829 pCSD
->ContentProtectAppli
= (tmp
& 0x01);
1832 tmp
= (uint8_t)((hsd
->CSD
[3] & 0x0000FF00) >> 8);
1833 pCSD
->FileFormatGrouop
= (tmp
& 0x80) >> 7;
1834 pCSD
->CopyFlag
= (tmp
& 0x40) >> 6;
1835 pCSD
->PermWrProtect
= (tmp
& 0x20) >> 5;
1836 pCSD
->TempWrProtect
= (tmp
& 0x10) >> 4;
1837 pCSD
->FileFormat
= (tmp
& 0x0C) >> 2;
1838 pCSD
->ECC
= (tmp
& 0x03);
1841 tmp
= (uint8_t)(hsd
->CSD
[3] & 0x000000FF);
1842 pCSD
->CSD_CRC
= (tmp
& 0xFE) >> 1;
1843 pCSD
->Reserved4
= 1;
1849 * @brief Gets the SD status info.
1850 * @param hsd: Pointer to SD handle
1851 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
1852 * will contain the SD card status information
1853 * @retval HAL status
1855 HAL_StatusTypeDef
HAL_SD_GetCardStatus(SD_HandleTypeDef
*hsd
, HAL_SD_CardStatusTypeDef
*pStatus
)
1858 uint32_t sd_status
[16];
1859 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1861 errorstate
= SD_SendSDStatus(hsd
, sd_status
);
1862 if(errorstate
!= HAL_OK
)
1864 /* Clear all the static flags */
1865 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
1866 hsd
->ErrorCode
|= errorstate
;
1867 hsd
->State
= HAL_SD_STATE_READY
;
1873 tmp
= (sd_status
[0] & 0xC0) >> 6;
1874 pStatus
->DataBusWidth
= (uint8_t)tmp
;
1877 tmp
= (sd_status
[0] & 0x20) >> 5;
1878 pStatus
->SecuredMode
= (uint8_t)tmp
;
1881 tmp
= (sd_status
[0] & 0x00FF0000U
) >> 16;
1882 pStatus
->CardType
= (uint16_t)(tmp
<< 8);
1885 tmp
= (sd_status
[0] & 0xFF000000U
) >> 24;
1886 pStatus
->CardType
|= (uint16_t)tmp
;
1889 tmp
= (sd_status
[1] & 0xFF);
1890 pStatus
->ProtectedAreaSize
= (uint32_t)(tmp
<< 24);
1893 tmp
= (sd_status
[1] & 0xFF00) >> 8;
1894 pStatus
->ProtectedAreaSize
|= (uint32_t)(tmp
<< 16);
1897 tmp
= (sd_status
[1] & 0xFF0000) >> 16;
1898 pStatus
->ProtectedAreaSize
|= (uint32_t)(tmp
<< 8);
1901 tmp
= (sd_status
[1] & 0xFF000000U
) >> 24;
1902 pStatus
->ProtectedAreaSize
|= (uint32_t)tmp
;
1905 tmp
= (sd_status
[2] & 0xFF);
1906 pStatus
->SpeedClass
= (uint8_t)tmp
;
1909 tmp
= (sd_status
[2] & 0xFF00) >> 8;
1910 pStatus
->PerformanceMove
= (uint8_t)tmp
;
1913 tmp
= (sd_status
[2] & 0xF00000) >> 20;
1914 pStatus
->AllocationUnitSize
= (uint8_t)tmp
;
1917 tmp
= (sd_status
[2] & 0xFF000000U
) >> 24;
1918 pStatus
->EraseSize
= (uint16_t)(tmp
<< 8);
1921 tmp
= (sd_status
[3] & 0xFF);
1922 pStatus
->EraseSize
|= (uint16_t)tmp
;
1925 tmp
= (sd_status
[3] & 0xFC00) >> 10;
1926 pStatus
->EraseTimeout
= (uint8_t)tmp
;
1929 tmp
= (sd_status
[3] & 0x0300) >> 8;
1930 pStatus
->EraseOffset
= (uint8_t)tmp
;
1937 * @brief Gets the SD card info.
1938 * @param hsd: Pointer to SD handle
1939 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
1940 * will contain the SD card status information
1941 * @retval HAL status
1943 HAL_StatusTypeDef
HAL_SD_GetCardInfo(SD_HandleTypeDef
*hsd
, HAL_SD_CardInfoTypeDef
*pCardInfo
)
1945 pCardInfo
->CardType
= (uint32_t)(hsd
->SdCard
.CardType
);
1946 pCardInfo
->CardVersion
= (uint32_t)(hsd
->SdCard
.CardVersion
);
1947 pCardInfo
->Class
= (uint32_t)(hsd
->SdCard
.Class
);
1948 pCardInfo
->RelCardAdd
= (uint32_t)(hsd
->SdCard
.RelCardAdd
);
1949 pCardInfo
->BlockNbr
= (uint32_t)(hsd
->SdCard
.BlockNbr
);
1950 pCardInfo
->BlockSize
= (uint32_t)(hsd
->SdCard
.BlockSize
);
1951 pCardInfo
->LogBlockNbr
= (uint32_t)(hsd
->SdCard
.LogBlockNbr
);
1952 pCardInfo
->LogBlockSize
= (uint32_t)(hsd
->SdCard
.LogBlockSize
);
1958 * @brief Enables wide bus operation for the requested card if supported by
1960 * @param hsd: Pointer to SD handle
1961 * @param WideMode: Specifies the SD card wide bus mode
1962 * This parameter can be one of the following values:
1963 * @arg SDMMC_BUS_WIDE_8B: 8-bit data transfer
1964 * @arg SDMMC_BUS_WIDE_4B: 4-bit data transfer
1965 * @arg SDMMC_BUS_WIDE_1B: 1-bit data transfer
1966 * @retval HAL status
1968 HAL_StatusTypeDef
HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef
*hsd
, uint32_t WideMode
)
1970 SDMMC_InitTypeDef Init
;
1971 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1973 /* Check the parameters */
1974 assert_param(IS_SDMMC_BUS_WIDE(WideMode
));
1977 hsd
->State
= HAL_SD_STATE_BUSY
;
1979 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
1981 if(WideMode
== SDMMC_BUS_WIDE_8B
)
1983 hsd
->ErrorCode
|= HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
1985 else if(WideMode
== SDMMC_BUS_WIDE_4B
)
1987 errorstate
= SD_WideBus_Enable(hsd
);
1989 hsd
->ErrorCode
|= errorstate
;
1991 else if(WideMode
== SDMMC_BUS_WIDE_1B
)
1993 errorstate
= SD_WideBus_Disable(hsd
);
1995 hsd
->ErrorCode
|= errorstate
;
1999 /* WideMode is not a valid argument*/
2000 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
2005 /* MMC Card does not support this feature */
2006 hsd
->ErrorCode
|= HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2009 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2011 /* Clear all the static flags */
2012 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2013 hsd
->State
= HAL_SD_STATE_READY
;
2018 /* Configure the SDMMC peripheral */
2019 Init
.ClockEdge
= hsd
->Init
.ClockEdge
;
2020 Init
.ClockBypass
= hsd
->Init
.ClockBypass
;
2021 Init
.ClockPowerSave
= hsd
->Init
.ClockPowerSave
;
2022 Init
.BusWide
= WideMode
;
2023 Init
.HardwareFlowControl
= hsd
->Init
.HardwareFlowControl
;
2024 Init
.ClockDiv
= hsd
->Init
.ClockDiv
;
2025 SDMMC_Init(hsd
->Instance
, Init
);
2029 hsd
->State
= HAL_SD_STATE_READY
;
2036 * @brief Gets the current sd card data state.
2037 * @param hsd: pointer to SD handle
2038 * @retval Card state
2040 HAL_SD_CardStateTypeDef
HAL_SD_GetCardState(SD_HandleTypeDef
*hsd
)
2042 HAL_SD_CardStateTypeDef cardstate
= HAL_SD_CARD_TRANSFER
;
2043 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2046 errorstate
= SD_SendStatus(hsd
, &resp1
);
2047 if(errorstate
!= HAL_OK
)
2049 hsd
->ErrorCode
|= errorstate
;
2052 cardstate
= (HAL_SD_CardStateTypeDef
)((resp1
>> 9) & 0x0F);
2058 * @brief Abort the current transfer and disable the SD.
2059 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2060 * the configuration information for SD module.
2061 * @retval HAL status
2063 HAL_StatusTypeDef
HAL_SD_Abort(SD_HandleTypeDef
*hsd
)
2065 HAL_SD_CardStateTypeDef CardState
;
2067 /* DIsable All interrupts */
2068 __HAL_SD_DISABLE_IT(hsd
, SDMMC_IT_DATAEND
| SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
|\
2069 SDMMC_IT_TXUNDERR
| SDMMC_IT_RXOVERR
);
2071 /* Clear All flags */
2072 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2074 if((hsd
->hdmatx
!= NULL
) || (hsd
->hdmarx
!= NULL
))
2076 /* Disable the SD DMA request */
2077 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN
);
2079 /* Abort the SD DMA Tx Stream */
2080 if(hsd
->hdmatx
!= NULL
)
2082 HAL_DMA_Abort(hsd
->hdmatx
);
2084 /* Abort the SD DMA Rx Stream */
2085 if(hsd
->hdmarx
!= NULL
)
2087 HAL_DMA_Abort(hsd
->hdmarx
);
2091 hsd
->State
= HAL_SD_STATE_READY
;
2092 CardState
= HAL_SD_GetCardState(hsd
);
2093 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2095 hsd
->ErrorCode
= SDMMC_CmdStopTransfer(hsd
->Instance
);
2097 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2105 * @brief Abort the current transfer and disable the SD (IT mode).
2106 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2107 * the configuration information for SD module.
2108 * @retval HAL status
2110 HAL_StatusTypeDef
HAL_SD_Abort_IT(SD_HandleTypeDef
*hsd
)
2112 HAL_SD_CardStateTypeDef CardState
;
2114 /* DIsable All interrupts */
2115 __HAL_SD_DISABLE_IT(hsd
, SDMMC_IT_DATAEND
| SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
|\
2116 SDMMC_IT_TXUNDERR
| SDMMC_IT_RXOVERR
);
2118 /* Clear All flags */
2119 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2121 if((hsd
->hdmatx
!= NULL
) || (hsd
->hdmarx
!= NULL
))
2123 /* Disable the SD DMA request */
2124 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN
);
2126 /* Abort the SD DMA Tx Stream */
2127 if(hsd
->hdmatx
!= NULL
)
2129 hsd
->hdmatx
->XferAbortCallback
= SD_DMATxAbort
;
2130 if(HAL_DMA_Abort_IT(hsd
->hdmatx
) != HAL_OK
)
2135 /* Abort the SD DMA Rx Stream */
2136 if(hsd
->hdmarx
!= NULL
)
2138 hsd
->hdmarx
->XferAbortCallback
= SD_DMARxAbort
;
2139 if(HAL_DMA_Abort_IT(hsd
->hdmarx
) != HAL_OK
)
2146 /* No transfer ongoing on both DMA channels*/
2147 if((hsd
->hdmatx
== NULL
) && (hsd
->hdmarx
== NULL
))
2149 CardState
= HAL_SD_GetCardState(hsd
);
2150 hsd
->State
= HAL_SD_STATE_READY
;
2151 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2153 hsd
->ErrorCode
= SDMMC_CmdStopTransfer(hsd
->Instance
);
2155 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2161 HAL_SD_AbortCallback(hsd
);
2176 /* Private function ----------------------------------------------------------*/
2177 /** @addtogroup SD_Private_Functions
2182 * @brief DMA SD transmit process complete callback
2183 * @param hdma: DMA handle
2186 static void SD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2188 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2190 /* Enable DATAEND Interrupt */
2191 __HAL_SD_ENABLE_IT(hsd
, (SDMMC_IT_DATAEND
));
2195 * @brief DMA SD receive process complete callback
2196 * @param hdma: DMA handle
2199 static void SD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
2201 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2202 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2204 /* Send stop command in multiblock write */
2205 if(hsd
->Context
== (SD_CONTEXT_READ_MULTIPLE_BLOCK
| SD_CONTEXT_DMA
))
2207 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
2208 if(errorstate
!= HAL_SD_ERROR_NONE
)
2210 hsd
->ErrorCode
|= errorstate
;
2211 HAL_SD_ErrorCallback(hsd
);
2215 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2216 in the SD DCTRL register */
2217 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDMMC_DCTRL_DMAEN
);
2219 /* Clear all the static flags */
2220 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2222 hsd
->State
= HAL_SD_STATE_READY
;
2224 HAL_SD_RxCpltCallback(hsd
);
2228 * @brief DMA SD communication error callback
2229 * @param hdma: DMA handle
2232 static void SD_DMAError(DMA_HandleTypeDef
*hdma
)
2234 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2235 HAL_SD_CardStateTypeDef CardState
;
2237 /* if DMA error is FIFO error ignore it */
2238 if(HAL_DMA_GetError(hdma
) != HAL_DMA_ERROR_FE
)
2240 if((hsd
->hdmarx
->ErrorCode
== HAL_DMA_ERROR_TE
) || (hsd
->hdmatx
->ErrorCode
== HAL_DMA_ERROR_TE
))
2242 /* Clear All flags */
2243 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2245 /* Disable All interrupts */
2246 __HAL_SD_DISABLE_IT(hsd
, SDMMC_IT_DATAEND
| SDMMC_IT_DCRCFAIL
| SDMMC_IT_DTIMEOUT
|\
2247 SDMMC_IT_TXUNDERR
| SDMMC_IT_RXOVERR
);
2249 hsd
->ErrorCode
|= HAL_SD_ERROR_DMA
;
2250 CardState
= HAL_SD_GetCardState(hsd
);
2251 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2253 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
2256 hsd
->State
= HAL_SD_STATE_READY
;
2258 HAL_SD_ErrorCallback(hsd
);
2263 * @brief DMA SD Tx Abort callback
2264 * @param hdma: DMA handle
2267 static void SD_DMATxAbort(DMA_HandleTypeDef
*hdma
)
2269 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2270 HAL_SD_CardStateTypeDef CardState
;
2272 if(hsd
->hdmatx
!= NULL
)
2277 /* All DMA channels are aborted */
2278 if(hsd
->hdmarx
== NULL
)
2280 CardState
= HAL_SD_GetCardState(hsd
);
2281 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
2282 hsd
->State
= HAL_SD_STATE_READY
;
2283 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2285 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
2287 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2289 HAL_SD_AbortCallback(hsd
);
2293 HAL_SD_ErrorCallback(hsd
);
2300 * @brief DMA SD Rx Abort callback
2301 * @param hdma: DMA handle
2304 static void SD_DMARxAbort(DMA_HandleTypeDef
*hdma
)
2306 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2307 HAL_SD_CardStateTypeDef CardState
;
2309 if(hsd
->hdmarx
!= NULL
)
2314 /* All DMA channels are aborted */
2315 if(hsd
->hdmatx
== NULL
)
2317 CardState
= HAL_SD_GetCardState(hsd
);
2318 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
2319 hsd
->State
= HAL_SD_STATE_READY
;
2320 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2322 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
2324 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2326 HAL_SD_AbortCallback(hsd
);
2330 HAL_SD_ErrorCallback(hsd
);
2338 * @brief Initializes the sd card.
2339 * @param hsd: Pointer to SD handle
2340 * @retval SD Card error state
2342 static uint32_t SD_InitCard(SD_HandleTypeDef
*hsd
)
2344 HAL_SD_CardCSDTypeDef CSD
;
2345 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2346 uint16_t sd_rca
= 1;
2348 /* Check the power State */
2349 if(SDMMC_GetPowerState(hsd
->Instance
) == 0)
2352 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
2355 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2357 /* Send CMD2 ALL_SEND_CID */
2358 errorstate
= SDMMC_CmdSendCID(hsd
->Instance
);
2359 if(errorstate
!= HAL_SD_ERROR_NONE
)
2365 /* Get Card identification number data */
2366 hsd
->CID
[0] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2367 hsd
->CID
[1] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP2
);
2368 hsd
->CID
[2] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP3
);
2369 hsd
->CID
[3] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP4
);
2373 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2375 /* Send CMD3 SET_REL_ADDR with argument 0 */
2376 /* SD Card publishes its RCA. */
2377 errorstate
= SDMMC_CmdSetRelAdd(hsd
->Instance
, &sd_rca
);
2378 if(errorstate
!= HAL_SD_ERROR_NONE
)
2383 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2385 /* Get the SD card RCA */
2386 hsd
->SdCard
.RelCardAdd
= sd_rca
;
2388 /* Send CMD9 SEND_CSD with argument as card's RCA */
2389 errorstate
= SDMMC_CmdSendCSD(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
2390 if(errorstate
!= HAL_SD_ERROR_NONE
)
2396 /* Get Card Specific Data */
2397 hsd
->CSD
[0U] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2398 hsd
->CSD
[1U] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP2
);
2399 hsd
->CSD
[2U] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP3
);
2400 hsd
->CSD
[3U] = SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP4
);
2404 /* Get the Card Class */
2405 hsd
->SdCard
.Class
= (SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP2
) >> 20);
2407 /* Get CSD parameters */
2408 HAL_SD_GetCardCSD(hsd
, &CSD
);
2410 /* Select the Card */
2411 errorstate
= SDMMC_CmdSelDesel(hsd
->Instance
, (uint32_t)(((uint32_t)hsd
->SdCard
.RelCardAdd
) << 16));
2412 if(errorstate
!= HAL_SD_ERROR_NONE
)
2417 /* Configure SDMMC peripheral interface */
2418 SDMMC_Init(hsd
->Instance
, hsd
->Init
);
2420 /* All cards are initialized */
2421 return HAL_SD_ERROR_NONE
;
2425 * @brief Enquires cards about their operating voltage and configures clock
2426 * controls and stores SD information that will be needed in future
2428 * @param hsd: Pointer to SD handle
2429 * @retval error state
2431 static uint32_t SD_PowerON(SD_HandleTypeDef
*hsd
)
2433 __IO
uint32_t count
= 0;
2434 uint32_t response
= 0, validvoltage
= 0;
2435 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2437 /* CMD0: GO_IDLE_STATE */
2438 errorstate
= SDMMC_CmdGoIdleState(hsd
->Instance
);
2439 if(errorstate
!= HAL_SD_ERROR_NONE
)
2444 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2445 errorstate
= SDMMC_CmdOperCond(hsd
->Instance
);
2446 if(errorstate
!= HAL_SD_ERROR_NONE
)
2448 hsd
->SdCard
.CardVersion
= CARD_V1_X
;
2450 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2451 while(validvoltage
== 0)
2453 if(count
++ == SDMMC_MAX_VOLT_TRIAL
)
2455 return HAL_SD_ERROR_INVALID_VOLTRANGE
;
2458 /* SEND CMD55 APP_CMD with RCA as 0 */
2459 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, 0);
2460 if(errorstate
!= HAL_SD_ERROR_NONE
)
2462 return HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2466 errorstate
= SDMMC_CmdAppOperCommand(hsd
->Instance
, SDMMC_STD_CAPACITY
);
2467 if(errorstate
!= HAL_SD_ERROR_NONE
)
2469 return HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2472 /* Get command response */
2473 response
= SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2475 /* Get operating voltage*/
2476 validvoltage
= (((response
>> 31) == 1) ? 1 : 0);
2478 /* Card type is SDSC */
2479 hsd
->SdCard
.CardType
= CARD_SDSC
;
2483 hsd
->SdCard
.CardVersion
= CARD_V2_X
;
2485 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2486 while(validvoltage
== 0)
2488 if(count
++ == SDMMC_MAX_VOLT_TRIAL
)
2490 return HAL_SD_ERROR_INVALID_VOLTRANGE
;
2493 /* SEND CMD55 APP_CMD with RCA as 0 */
2494 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, 0);
2495 if(errorstate
!= HAL_SD_ERROR_NONE
)
2501 errorstate
= SDMMC_CmdAppOperCommand(hsd
->Instance
, SDMMC_HIGH_CAPACITY
);
2502 if(errorstate
!= HAL_SD_ERROR_NONE
)
2507 /* Get command response */
2508 response
= SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2510 /* Get operating voltage*/
2511 validvoltage
= (((response
>> 31) == 1) ? 1 : 0);
2514 if((response
& SDMMC_HIGH_CAPACITY
) == SDMMC_HIGH_CAPACITY
) /* (response &= SD_HIGH_CAPACITY) */
2516 hsd
->SdCard
.CardType
= CARD_SDHC_SDXC
;
2520 hsd
->SdCard
.CardType
= CARD_SDSC
;
2524 return HAL_SD_ERROR_NONE
;
2528 * @brief Turns the SDMMC output signals off.
2529 * @param hsd: Pointer to SD handle
2530 * @retval HAL status
2532 static HAL_StatusTypeDef
SD_PowerOFF(SD_HandleTypeDef
*hsd
)
2534 /* Set Power State to OFF */
2535 SDMMC_PowerState_OFF(hsd
->Instance
);
2541 * @brief Send Status info command.
2542 * @param hsd: pointer to SD handle
2543 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
2544 * SD Status register)
2545 * @retval error state
2547 static uint32_t SD_SendSDStatus(SD_HandleTypeDef
*hsd
, uint32_t *pSDstatus
)
2549 SDMMC_DataInitTypeDef config
;
2550 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2551 uint32_t tickstart
= HAL_GetTick();
2554 /* Check SD response */
2555 if((SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
2557 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
2560 /* Set block size for card if it is not equal to current block size for card */
2561 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, 64);
2562 if(errorstate
!= HAL_SD_ERROR_NONE
)
2564 hsd
->ErrorCode
|= HAL_SD_ERROR_NONE
;
2569 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16));
2570 if(errorstate
!= HAL_SD_ERROR_NONE
)
2572 hsd
->ErrorCode
|= HAL_SD_ERROR_NONE
;
2576 /* Configure the SD DPSM (Data Path State Machine) */
2577 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
2578 config
.DataLength
= 64;
2579 config
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_64B
;
2580 config
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
2581 config
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
2582 config
.DPSM
= SDMMC_DPSM_ENABLE
;
2583 SDMMC_ConfigData(hsd
->Instance
, &config
);
2585 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2586 errorstate
= SDMMC_CmdStatusRegister(hsd
->Instance
);
2587 if(errorstate
!= HAL_SD_ERROR_NONE
)
2589 hsd
->ErrorCode
|= HAL_SD_ERROR_NONE
;
2593 /* Get status data */
2594 while(!__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DBCKEND
))
2596 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXFIFOHF
))
2598 for(count
= 0; count
< 8; count
++)
2600 *(pSDstatus
+ count
) = SDMMC_ReadFIFO(hsd
->Instance
);
2606 if((HAL_GetTick() - tickstart
) >= SDMMC_DATATIMEOUT
)
2608 return HAL_SD_ERROR_TIMEOUT
;
2612 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
))
2614 return HAL_SD_ERROR_DATA_TIMEOUT
;
2616 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
))
2618 return HAL_SD_ERROR_DATA_CRC_FAIL
;
2620 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
))
2622 return HAL_SD_ERROR_RX_OVERRUN
;
2625 while ((__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXDAVL
)))
2627 *pSDstatus
= SDMMC_ReadFIFO(hsd
->Instance
);
2630 if((HAL_GetTick() - tickstart
) >= SDMMC_DATATIMEOUT
)
2632 return HAL_SD_ERROR_TIMEOUT
;
2636 /* Clear all the static status flags*/
2637 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2639 return HAL_SD_ERROR_NONE
;
2643 * @brief Returns the current card's status.
2644 * @param hsd: Pointer to SD handle
2645 * @param pCardStatus: pointer to the buffer that will contain the SD card
2646 * status (Card Status register)
2647 * @retval error state
2649 static uint32_t SD_SendStatus(SD_HandleTypeDef
*hsd
, uint32_t *pCardStatus
)
2651 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2653 if(pCardStatus
== NULL
)
2655 return HAL_SD_ERROR_PARAM
;
2658 /* Send Status command */
2659 errorstate
= SDMMC_CmdSendStatus(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16));
2660 if(errorstate
!= HAL_OK
)
2665 /* Get SD card status */
2666 *pCardStatus
= SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
);
2668 return HAL_SD_ERROR_NONE
;
2672 * @brief Enables the SDMMC wide bus mode.
2673 * @param hsd: pointer to SD handle
2674 * @retval error state
2676 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef
*hsd
)
2678 uint32_t scr
[2] = {0, 0};
2679 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2681 if((SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
2683 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
2686 /* Get SCR Register */
2687 errorstate
= SD_FindSCR(hsd
, scr
);
2688 if(errorstate
!= HAL_OK
)
2693 /* If requested card supports wide bus operation */
2694 if((scr
[1] & SDMMC_WIDE_BUS_SUPPORT
) != SDMMC_ALLZERO
)
2696 /* Send CMD55 APP_CMD with argument as card's RCA.*/
2697 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16));
2698 if(errorstate
!= HAL_OK
)
2703 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2704 errorstate
= SDMMC_CmdBusWidth(hsd
->Instance
, 2);
2705 if(errorstate
!= HAL_OK
)
2710 return HAL_SD_ERROR_NONE
;
2714 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
2719 * @brief Disables the SDMMC wide bus mode.
2720 * @param hsd: Pointer to SD handle
2721 * @retval error state
2723 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef
*hsd
)
2725 uint32_t scr
[2] = {0, 0};
2726 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2728 if((SDMMC_GetResponse(hsd
->Instance
, SDMMC_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
2730 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
2733 /* Get SCR Register */
2734 errorstate
= SD_FindSCR(hsd
, scr
);
2735 if(errorstate
!= HAL_OK
)
2740 /* If requested card supports 1 bit mode operation */
2741 if((scr
[1] & SDMMC_SINGLE_BUS_SUPPORT
) != SDMMC_ALLZERO
)
2743 /* Send CMD55 APP_CMD with argument as card's RCA */
2744 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16));
2745 if(errorstate
!= HAL_OK
)
2750 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
2751 errorstate
= SDMMC_CmdBusWidth(hsd
->Instance
, 0);
2752 if(errorstate
!= HAL_OK
)
2757 return HAL_SD_ERROR_NONE
;
2761 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
2767 * @brief Finds the SD card SCR register value.
2768 * @param hsd: Pointer to SD handle
2769 * @param pSCR: pointer to the buffer that will contain the SCR value
2770 * @retval error state
2772 static uint32_t SD_FindSCR(SD_HandleTypeDef
*hsd
, uint32_t *pSCR
)
2774 SDMMC_DataInitTypeDef config
;
2775 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2776 uint32_t tickstart
= HAL_GetTick();
2778 uint32_t tempscr
[2] = {0, 0};
2780 /* Set Block Size To 8 Bytes */
2781 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, 8);
2782 if(errorstate
!= HAL_OK
)
2787 /* Send CMD55 APP_CMD with argument as card's RCA */
2788 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)((hsd
->SdCard
.RelCardAdd
) << 16));
2789 if(errorstate
!= HAL_OK
)
2794 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
2795 config
.DataLength
= 8;
2796 config
.DataBlockSize
= SDMMC_DATABLOCK_SIZE_8B
;
2797 config
.TransferDir
= SDMMC_TRANSFER_DIR_TO_SDMMC
;
2798 config
.TransferMode
= SDMMC_TRANSFER_MODE_BLOCK
;
2799 config
.DPSM
= SDMMC_DPSM_ENABLE
;
2800 SDMMC_ConfigData(hsd
->Instance
, &config
);
2802 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
2803 errorstate
= SDMMC_CmdSendSCR(hsd
->Instance
);
2804 if(errorstate
!= HAL_OK
)
2809 while(!__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
| SDMMC_FLAG_DCRCFAIL
| SDMMC_FLAG_DTIMEOUT
| SDMMC_FLAG_DBCKEND
))
2811 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXDAVL
))
2813 *(tempscr
+ index
) = SDMMC_ReadFIFO(hsd
->Instance
);
2817 if((HAL_GetTick() - tickstart
) >= SDMMC_DATATIMEOUT
)
2819 return HAL_SD_ERROR_TIMEOUT
;
2823 if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
))
2825 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_FLAG_DTIMEOUT
);
2827 return HAL_SD_ERROR_DATA_TIMEOUT
;
2829 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
))
2831 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_FLAG_DCRCFAIL
);
2833 return HAL_SD_ERROR_DATA_CRC_FAIL
;
2835 else if(__HAL_SD_GET_FLAG(hsd
, SDMMC_FLAG_RXOVERR
))
2837 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_FLAG_RXOVERR
);
2839 return HAL_SD_ERROR_RX_OVERRUN
;
2843 /* No error flag set */
2844 /* Clear all the static flags */
2845 __HAL_SD_CLEAR_FLAG(hsd
, SDMMC_STATIC_FLAGS
);
2847 *(pSCR
+ 1) = ((tempscr
[0] & SDMMC_0TO7BITS
) << 24) | ((tempscr
[0] & SDMMC_8TO15BITS
) << 8) |\
2848 ((tempscr
[0] & SDMMC_16TO23BITS
) >> 8) | ((tempscr
[0] & SDMMC_24TO31BITS
) >> 24);
2850 *(pSCR
) = ((tempscr
[1] & SDMMC_0TO7BITS
) << 24) | ((tempscr
[1] & SDMMC_8TO15BITS
) << 8) |\
2851 ((tempscr
[1] & SDMMC_16TO23BITS
) >> 8) | ((tempscr
[1] & SDMMC_24TO31BITS
) >> 24);
2854 return HAL_SD_ERROR_NONE
;
2858 * @brief Wrap up reading in non-blocking mode.
2859 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2860 * the configuration information.
2861 * @retval HAL status
2863 static HAL_StatusTypeDef
SD_Read_IT(SD_HandleTypeDef
*hsd
)
2868 tmp
= (uint32_t*)hsd
->pRxBuffPtr
;
2870 /* Read data from SDMMC Rx FIFO */
2871 for(count
= 0; count
< 8; count
++)
2873 *(tmp
+ count
) = SDMMC_ReadFIFO(hsd
->Instance
);
2876 hsd
->pRxBuffPtr
+= 8;
2882 * @brief Wrap up writing in non-blocking mode.
2883 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2884 * the configuration information.
2885 * @retval HAL status
2887 static HAL_StatusTypeDef
SD_Write_IT(SD_HandleTypeDef
*hsd
)
2892 tmp
= (uint32_t*)hsd
->pTxBuffPtr
;
2894 /* Write data to SDMMC Tx FIFO */
2895 for(count
= 0; count
< 8; count
++)
2897 SDMMC_WriteFIFO(hsd
->Instance
, (tmp
+ count
));
2900 hsd
->pTxBuffPtr
+= 8;
2909 #endif /* HAL_SD_MODULE_ENABLED */
2919 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/