2 ******************************************************************************
3 * @file stm32f4xx_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 (SDIO 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 SDIO memories which uses the HAL
29 SDIO driver functions to interface with SD and uSD cards devices.
30 It is used as follows:
32 (#)Initialize the SDIO low level resources by implement the HAL_SD_MspInit() API:
33 (##) Enable the SDIO interface clock using __HAL_RCC_SDIO_CLK_ENABLE();
34 (##) SDIO pins configuration for SD card
35 (+++) Enable the clock for the SDIO GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();
36 (+++) Configure these SDIO 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 SDIO and DMA interrupt priorities using functions
44 HAL_NVIC_SetPriority(); DMA priority is superior to SDIO's priority
45 (+++) Enable the NVIC DMA and SDIO IRQs using function HAL_NVIC_EnableIRQ()
46 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
47 and __HAL_SD_DISABLE_IT() inside the communication process.
48 (+++) SDIO 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 SDIO interrupt priorities using function
53 HAL_NVIC_SetPriority();
54 (+++) Enable the NVIC SDIO IRQs using function HAL_NVIC_EnableIRQ()
55 (+++) SDIO interrupts are managed using the macros __HAL_SD_ENABLE_IT()
56 and __HAL_SD_DISABLE_IT() inside the communication process.
57 (+++) SDIO 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 SDIO 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 SDIO 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 (SDIO_CK) is computed as follows:
74 SDIO_CK = SDIOCLK / (ClockDiv + 2)
76 In initialization mode and according to the SD Card standard,
77 make sure that the SDIO_CK frequency doesn't exceed 400KHz.
79 This phase of initialization is done through SDIO_Init() and
80 SDIO_PowerState_ON() SDIO 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 SDIO_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 SDIO
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 SDIO DMA transfer
188 (+) __HAL_SD_DMA_DISABLE: Disable the SDIO 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 "stm32f4xx_hal.h"
231 /** @addtogroup STM32F4xx_HAL_Driver
239 #ifdef HAL_SD_MODULE_ENABLED
241 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) || \
242 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) || \
243 defined(STM32F401xC) || defined(STM32F401xE) || defined(STM32F411xE) || defined(STM32F446xx) || \
244 defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \
245 defined(STM32F412Rx) || defined(STM32F412Cx) || defined(STM32F413xx) || defined(STM32F423xx)
247 /* Private typedef -----------------------------------------------------------*/
248 /* Private define ------------------------------------------------------------*/
249 /** @addtogroup SD_Private_Defines
257 /* Private macro -------------------------------------------------------------*/
258 /* Private variables ---------------------------------------------------------*/
259 /* Private function prototypes -----------------------------------------------*/
260 /* Private functions ---------------------------------------------------------*/
261 /** @defgroup SD_Private_Functions SD Private Functions
264 static uint32_t SD_InitCard(SD_HandleTypeDef
*hsd
);
265 static uint32_t SD_PowerON(SD_HandleTypeDef
*hsd
);
266 static uint32_t SD_SendSDStatus(SD_HandleTypeDef
*hsd
, uint32_t *pSDstatus
);
267 static uint32_t SD_SendStatus(SD_HandleTypeDef
*hsd
, uint32_t *pCardStatus
);
268 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef
*hsd
);
269 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef
*hsd
);
270 static uint32_t SD_FindSCR(SD_HandleTypeDef
*hsd
, uint32_t *pSCR
);
271 static HAL_StatusTypeDef
SD_PowerOFF(SD_HandleTypeDef
*hsd
);
272 static HAL_StatusTypeDef
SD_Write_IT(SD_HandleTypeDef
*hsd
);
273 static HAL_StatusTypeDef
SD_Read_IT(SD_HandleTypeDef
*hsd
);
274 static void SD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
);
275 static void SD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
);
276 static void SD_DMAError(DMA_HandleTypeDef
*hdma
);
277 static void SD_DMATxAbort(DMA_HandleTypeDef
*hdma
);
278 static void SD_DMARxAbort(DMA_HandleTypeDef
*hdma
);
283 /* Exported functions --------------------------------------------------------*/
284 /** @addtogroup SD_Exported_Functions
288 /** @addtogroup SD_Exported_Functions_Group1
289 * @brief Initialization and de-initialization functions
292 ==============================================================================
293 ##### Initialization and de-initialization functions #####
294 ==============================================================================
296 This section provides functions allowing to initialize/de-initialize the SD
297 card device to be ready for use.
304 * @brief Initializes the SD according to the specified parameters in the
305 SD_HandleTypeDef and create the associated handle.
306 * @param hsd: Pointer to the SD handle
309 HAL_StatusTypeDef
HAL_SD_Init(SD_HandleTypeDef
*hsd
)
311 /* Check the SD handle allocation */
317 /* Check the parameters */
318 assert_param(IS_SDIO_ALL_INSTANCE(hsd
->Instance
));
319 assert_param(IS_SDIO_CLOCK_EDGE(hsd
->Init
.ClockEdge
));
320 assert_param(IS_SDIO_CLOCK_BYPASS(hsd
->Init
.ClockBypass
));
321 assert_param(IS_SDIO_CLOCK_POWER_SAVE(hsd
->Init
.ClockPowerSave
));
322 assert_param(IS_SDIO_BUS_WIDE(hsd
->Init
.BusWide
));
323 assert_param(IS_SDIO_HARDWARE_FLOW_CONTROL(hsd
->Init
.HardwareFlowControl
));
324 assert_param(IS_SDIO_CLKDIV(hsd
->Init
.ClockDiv
));
326 if(hsd
->State
== HAL_SD_STATE_RESET
)
328 /* Allocate lock resource and initialize it */
329 hsd
->Lock
= HAL_UNLOCKED
;
330 /* Init the low level hardware : GPIO, CLOCK, CORTEX...etc */
334 hsd
->State
= HAL_SD_STATE_BUSY
;
336 /* Initialize the Card parameters */
337 HAL_SD_InitCard(hsd
);
339 /* Initialize the error code */
340 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
342 /* Initialize the SD operation */
343 hsd
->Context
= SD_CONTEXT_NONE
;
345 /* Initialize the SD state */
346 hsd
->State
= HAL_SD_STATE_READY
;
352 * @brief Initializes the SD Card.
353 * @param hsd: Pointer to SD handle
354 * @note This function initializes the SD card. It could be used when a card
355 re-initialization is needed.
358 HAL_StatusTypeDef
HAL_SD_InitCard(SD_HandleTypeDef
*hsd
)
360 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
363 /* Default SDIO peripheral configuration for SD card initialization */
364 Init
.ClockEdge
= SDIO_CLOCK_EDGE_RISING
;
365 Init
.ClockBypass
= SDIO_CLOCK_BYPASS_DISABLE
;
366 Init
.ClockPowerSave
= SDIO_CLOCK_POWER_SAVE_DISABLE
;
367 Init
.BusWide
= SDIO_BUS_WIDE_1B
;
368 Init
.HardwareFlowControl
= SDIO_HARDWARE_FLOW_CONTROL_DISABLE
;
369 Init
.ClockDiv
= SDIO_INIT_CLK_DIV
;
371 /* Initialize SDIO peripheral interface with default configuration */
372 SDIO_Init(hsd
->Instance
, Init
);
374 /* Disable SDIO Clock */
375 __HAL_SD_DISABLE(hsd
);
377 /* Set Power State to ON */
378 SDIO_PowerState_ON(hsd
->Instance
);
380 /* Enable SDIO Clock */
381 __HAL_SD_ENABLE(hsd
);
383 /* Required power up waiting time before starting the SD initialization
387 /* Identify card operating voltage */
388 errorstate
= SD_PowerON(hsd
);
389 if(errorstate
!= HAL_SD_ERROR_NONE
)
391 hsd
->State
= HAL_SD_STATE_READY
;
392 hsd
->ErrorCode
|= errorstate
;
396 /* Card initialization */
397 errorstate
= SD_InitCard(hsd
);
398 if(errorstate
!= HAL_SD_ERROR_NONE
)
400 hsd
->State
= HAL_SD_STATE_READY
;
401 hsd
->ErrorCode
|= errorstate
;
409 * @brief De-Initializes the SD card.
410 * @param hsd: Pointer to SD handle
413 HAL_StatusTypeDef
HAL_SD_DeInit(SD_HandleTypeDef
*hsd
)
415 /* Check the SD handle allocation */
421 /* Check the parameters */
422 assert_param(IS_SDIO_ALL_INSTANCE(hsd
->Instance
));
424 hsd
->State
= HAL_SD_STATE_BUSY
;
426 /* Set SD power state to off */
429 /* De-Initialize the MSP layer */
430 HAL_SD_MspDeInit(hsd
);
432 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
433 hsd
->State
= HAL_SD_STATE_RESET
;
440 * @brief Initializes the SD MSP.
441 * @param hsd: Pointer to SD handle
444 __weak
void HAL_SD_MspInit(SD_HandleTypeDef
*hsd
)
446 /* Prevent unused argument(s) compilation warning */
449 /* NOTE : This function Should not be modified, when the callback is needed,
450 the HAL_SD_MspInit could be implemented in the user file
455 * @brief De-Initialize SD MSP.
456 * @param hsd: Pointer to SD handle
459 __weak
void HAL_SD_MspDeInit(SD_HandleTypeDef
*hsd
)
461 /* Prevent unused argument(s) compilation warning */
464 /* NOTE : This function Should not be modified, when the callback is needed,
465 the HAL_SD_MspDeInit could be implemented in the user file
473 /** @addtogroup SD_Exported_Functions_Group2
474 * @brief Data transfer functions
477 ==============================================================================
478 ##### IO operation functions #####
479 ==============================================================================
481 This subsection provides a set of functions allowing to manage the data
482 transfer from/to SD card.
489 * @brief Reads block(s) from a specified address in a card. The Data transfer
490 * is managed by polling mode.
491 * @note This API should be followed by a check on the card state through
492 * HAL_SD_GetCardState().
493 * @param hsd: Pointer to SD handle
494 * @param pData: pointer to the buffer that will contain the received data
495 * @param BlockAdd: Block Address from where data is to be read
496 * @param NumberOfBlocks: Number of SD blocks to read
497 * @param Timeout: Specify timeout value
500 HAL_StatusTypeDef
HAL_SD_ReadBlocks(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
, uint32_t Timeout
)
502 SDIO_DataInitTypeDef config
;
503 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
504 uint32_t tickstart
= HAL_GetTick();
505 uint32_t count
= 0U, *tempbuff
= (uint32_t *)pData
;
509 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
513 if(hsd
->State
== HAL_SD_STATE_READY
)
515 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
517 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
519 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
523 hsd
->State
= HAL_SD_STATE_BUSY
;
525 /* Initialize data control register */
526 hsd
->Instance
->DCTRL
= 0U;
528 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
533 /* Set Block Size for Card */
534 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
535 if(errorstate
!= HAL_SD_ERROR_NONE
)
537 /* Clear all the static flags */
538 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
539 hsd
->ErrorCode
|= errorstate
;
540 hsd
->State
= HAL_SD_STATE_READY
;
544 /* Configure the SD DPSM (Data Path State Machine) */
545 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
546 config
.DataLength
= NumberOfBlocks
* BLOCKSIZE
;
547 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_512B
;
548 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_SDIO
;
549 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
550 config
.DPSM
= SDIO_DPSM_ENABLE
;
551 SDIO_ConfigData(hsd
->Instance
, &config
);
553 /* Read block(s) in polling mode */
554 if(NumberOfBlocks
> 1U)
556 hsd
->Context
= SD_CONTEXT_READ_MULTIPLE_BLOCK
;
558 /* Read Multi Block command */
559 errorstate
= SDMMC_CmdReadMultiBlock(hsd
->Instance
, BlockAdd
);
563 hsd
->Context
= SD_CONTEXT_READ_SINGLE_BLOCK
;
565 /* Read Single Block command */
566 errorstate
= SDMMC_CmdReadSingleBlock(hsd
->Instance
, BlockAdd
);
568 if(errorstate
!= HAL_SD_ERROR_NONE
)
570 /* Clear all the static flags */
571 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
572 hsd
->ErrorCode
|= errorstate
;
573 hsd
->State
= HAL_SD_STATE_READY
;
577 /* Poll on SDIO flags */
578 #ifdef SDIO_STA_STBITERR
579 while(!__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
| SDIO_FLAG_DCRCFAIL
| SDIO_FLAG_DTIMEOUT
| SDIO_FLAG_DATAEND
| SDIO_STA_STBITERR
))
580 #else /* SDIO_STA_STBITERR not defined */
581 while(!__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
| SDIO_FLAG_DCRCFAIL
| SDIO_FLAG_DTIMEOUT
| SDIO_FLAG_DATAEND
))
582 #endif /* SDIO_STA_STBITERR */
584 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXFIFOHF
))
586 /* Read data from SDIO Rx FIFO */
587 for(count
= 0U; count
< 8U; count
++)
589 *(tempbuff
+ count
) = SDIO_ReadFIFO(hsd
->Instance
);
594 if((Timeout
== 0U)||((HAL_GetTick()-tickstart
) >= Timeout
))
596 /* Clear all the static flags */
597 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
598 hsd
->ErrorCode
|= HAL_SD_ERROR_TIMEOUT
;
599 hsd
->State
= HAL_SD_STATE_READY
;
604 /* Send stop transmission command in case of multiblock read */
605 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DATAEND
) && (NumberOfBlocks
> 1U))
607 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
609 /* Send stop transmission command */
610 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
611 if(errorstate
!= HAL_SD_ERROR_NONE
)
613 /* Clear all the static flags */
614 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
615 hsd
->ErrorCode
|= errorstate
;
616 hsd
->State
= HAL_SD_STATE_READY
;
622 /* Get error state */
623 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DTIMEOUT
))
625 /* Clear all the static flags */
626 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
627 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_TIMEOUT
;
628 hsd
->State
= HAL_SD_STATE_READY
;
631 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
))
633 /* Clear all the static flags */
634 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
635 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_CRC_FAIL
;
636 hsd
->State
= HAL_SD_STATE_READY
;
639 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
))
641 /* Clear all the static flags */
642 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
643 hsd
->ErrorCode
|= HAL_SD_ERROR_RX_OVERRUN
;
644 hsd
->State
= HAL_SD_STATE_READY
;
648 /* Empty FIFO if there is still any data */
649 while ((__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXDAVL
)))
651 *tempbuff
= SDIO_ReadFIFO(hsd
->Instance
);
654 if((Timeout
== 0U)||((HAL_GetTick()-tickstart
) >= Timeout
))
656 /* Clear all the static flags */
657 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
658 hsd
->ErrorCode
|= HAL_SD_ERROR_TIMEOUT
;
659 hsd
->State
= HAL_SD_STATE_READY
;
664 /* Clear all the static flags */
665 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
667 hsd
->State
= HAL_SD_STATE_READY
;
673 hsd
->ErrorCode
|= HAL_SD_ERROR_BUSY
;
679 * @brief Allows to write block(s) to a specified address in a card. The Data
680 * transfer is managed by polling mode.
681 * @note This API should be followed by a check on the card state through
682 * HAL_SD_GetCardState().
683 * @param hsd: Pointer to SD handle
684 * @param pData: pointer to the buffer that will contain the data to transmit
685 * @param BlockAdd: Block Address where data will be written
686 * @param NumberOfBlocks: Number of SD blocks to write
687 * @param Timeout: Specify timeout value
690 HAL_StatusTypeDef
HAL_SD_WriteBlocks(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
, uint32_t Timeout
)
692 SDIO_DataInitTypeDef config
;
693 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
694 uint32_t tickstart
= HAL_GetTick();
696 uint32_t *tempbuff
= (uint32_t *)pData
;
700 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
704 if(hsd
->State
== HAL_SD_STATE_READY
)
706 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
708 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
710 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
714 hsd
->State
= HAL_SD_STATE_BUSY
;
716 /* Initialize data control register */
717 hsd
->Instance
->DCTRL
= 0U;
719 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
724 /* Set Block Size for Card */
725 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
726 if(errorstate
!= HAL_SD_ERROR_NONE
)
728 /* Clear all the static flags */
729 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
730 hsd
->ErrorCode
|= errorstate
;
731 hsd
->State
= HAL_SD_STATE_READY
;
735 /* Write Blocks in Polling mode */
736 if(NumberOfBlocks
> 1U)
738 hsd
->Context
= SD_CONTEXT_WRITE_MULTIPLE_BLOCK
;
740 /* Write Multi Block command */
741 errorstate
= SDMMC_CmdWriteMultiBlock(hsd
->Instance
, BlockAdd
);
745 hsd
->Context
= SD_CONTEXT_WRITE_SINGLE_BLOCK
;
747 /* Write Single Block command */
748 errorstate
= SDMMC_CmdWriteSingleBlock(hsd
->Instance
, BlockAdd
);
750 if(errorstate
!= HAL_SD_ERROR_NONE
)
752 /* Clear all the static flags */
753 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
754 hsd
->ErrorCode
|= errorstate
;
755 hsd
->State
= HAL_SD_STATE_READY
;
759 /* Configure the SD DPSM (Data Path State Machine) */
760 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
761 config
.DataLength
= NumberOfBlocks
* BLOCKSIZE
;
762 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_512B
;
763 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_CARD
;
764 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
765 config
.DPSM
= SDIO_DPSM_ENABLE
;
766 SDIO_ConfigData(hsd
->Instance
, &config
);
768 /* Write block(s) in polling mode */
769 #ifdef SDIO_STA_STBITERR
770 while(!__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_TXUNDERR
| SDIO_FLAG_DCRCFAIL
| SDIO_FLAG_DTIMEOUT
| SDIO_FLAG_DATAEND
| SDIO_FLAG_STBITERR
))
771 #else /* SDIO_STA_STBITERR not defined */
772 while(!__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_TXUNDERR
| SDIO_FLAG_DCRCFAIL
| SDIO_FLAG_DTIMEOUT
| SDIO_FLAG_DATAEND
))
773 #endif /* SDIO_STA_STBITERR */
775 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_TXFIFOHE
))
777 /* Write data to SDIO Tx FIFO */
778 for(count
= 0U; count
< 8U; count
++)
780 SDIO_WriteFIFO(hsd
->Instance
, (tempbuff
+ count
));
785 if((Timeout
== 0U)||((HAL_GetTick()-tickstart
) >= Timeout
))
787 /* Clear all the static flags */
788 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
789 hsd
->ErrorCode
|= errorstate
;
790 hsd
->State
= HAL_SD_STATE_READY
;
795 /* Send stop transmission command in case of multiblock write */
796 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DATAEND
) && (NumberOfBlocks
> 1U))
798 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
800 /* Send stop transmission command */
801 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
802 if(errorstate
!= HAL_SD_ERROR_NONE
)
804 /* Clear all the static flags */
805 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
806 hsd
->ErrorCode
|= errorstate
;
807 hsd
->State
= HAL_SD_STATE_READY
;
813 /* Get error state */
814 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DTIMEOUT
))
816 /* Clear all the static flags */
817 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
818 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_TIMEOUT
;
819 hsd
->State
= HAL_SD_STATE_READY
;
822 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
))
824 /* Clear all the static flags */
825 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
826 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_CRC_FAIL
;
827 hsd
->State
= HAL_SD_STATE_READY
;
830 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_TXUNDERR
))
832 /* Clear all the static flags */
833 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
834 hsd
->ErrorCode
|= HAL_SD_ERROR_TX_UNDERRUN
;
835 hsd
->State
= HAL_SD_STATE_READY
;
839 /* Clear all the static flags */
840 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
842 hsd
->State
= HAL_SD_STATE_READY
;
848 hsd
->ErrorCode
|= HAL_SD_ERROR_BUSY
;
854 * @brief Reads block(s) from a specified address in a card. The Data transfer
855 * is managed in interrupt mode.
856 * @note This API should be followed by a check on the card state through
857 * HAL_SD_GetCardState().
858 * @note You could also check the IT transfer process through the SD Rx
860 * @param hsd: Pointer to SD handle
861 * @param pData: Pointer to the buffer that will contain the received data
862 * @param BlockAdd: Block Address from where data is to be read
863 * @param NumberOfBlocks: Number of blocks to read.
866 HAL_StatusTypeDef
HAL_SD_ReadBlocks_IT(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
)
868 SDIO_DataInitTypeDef config
;
869 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
873 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
877 if(hsd
->State
== HAL_SD_STATE_READY
)
879 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
881 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
883 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
887 hsd
->State
= HAL_SD_STATE_BUSY
;
889 /* Initialize data control register */
890 hsd
->Instance
->DCTRL
= 0U;
892 hsd
->pRxBuffPtr
= (uint32_t *)pData
;
893 hsd
->RxXferSize
= BLOCKSIZE
* NumberOfBlocks
;
895 #ifdef SDIO_STA_STBITER
896 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_RXOVERR
| SDIO_IT_DATAEND
| SDIO_FLAG_RXFIFOHF
| SDIO_IT_STBITERR
));
897 #else /* SDIO_STA_STBITERR not defined */
898 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_RXOVERR
| SDIO_IT_DATAEND
| SDIO_FLAG_RXFIFOHF
));
899 #endif /* SDIO_STA_STBITERR */
901 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
906 /* Configure the SD DPSM (Data Path State Machine) */
907 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
908 config
.DataLength
= BLOCKSIZE
* NumberOfBlocks
;
909 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_512B
;
910 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_SDIO
;
911 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
912 config
.DPSM
= SDIO_DPSM_ENABLE
;
913 SDIO_ConfigData(hsd
->Instance
, &config
);
915 /* Set Block Size for Card */
916 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
917 if(errorstate
!= HAL_SD_ERROR_NONE
)
919 /* Clear all the static flags */
920 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
921 hsd
->ErrorCode
|= errorstate
;
922 hsd
->State
= HAL_SD_STATE_READY
;
926 /* Read Blocks in IT mode */
927 if(NumberOfBlocks
> 1U)
929 hsd
->Context
= (SD_CONTEXT_READ_MULTIPLE_BLOCK
| SD_CONTEXT_IT
);
931 /* Read Multi Block command */
932 errorstate
= SDMMC_CmdReadMultiBlock(hsd
->Instance
, BlockAdd
);
936 hsd
->Context
= (SD_CONTEXT_READ_SINGLE_BLOCK
| SD_CONTEXT_IT
);
938 /* Read Single Block command */
939 errorstate
= SDMMC_CmdReadSingleBlock(hsd
->Instance
, BlockAdd
);
941 if(errorstate
!= HAL_SD_ERROR_NONE
)
943 /* Clear all the static flags */
944 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
945 hsd
->ErrorCode
|= errorstate
;
946 hsd
->State
= HAL_SD_STATE_READY
;
959 * @brief Writes block(s) to a specified address in a card. The Data transfer
960 * is managed in interrupt mode.
961 * @note This API should be followed by a check on the card state through
962 * HAL_SD_GetCardState().
963 * @note You could also check the IT transfer process through the SD Tx
965 * @param hsd: Pointer to SD handle
966 * @param pData: Pointer to the buffer that will contain the data to transmit
967 * @param BlockAdd: Block Address where data will be written
968 * @param NumberOfBlocks: Number of blocks to write
971 HAL_StatusTypeDef
HAL_SD_WriteBlocks_IT(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
)
973 SDIO_DataInitTypeDef config
;
974 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
978 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
982 if(hsd
->State
== HAL_SD_STATE_READY
)
984 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
986 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
988 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
992 hsd
->State
= HAL_SD_STATE_BUSY
;
994 /* Initialize data control register */
995 hsd
->Instance
->DCTRL
= 0U;
997 hsd
->pTxBuffPtr
= (uint32_t *)pData
;
998 hsd
->TxXferSize
= BLOCKSIZE
* NumberOfBlocks
;
1000 /* Enable transfer interrupts */
1001 #ifdef SDIO_STA_STBITER
1002 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_TXUNDERR
| SDIO_IT_DATAEND
| SDIO_FLAG_TXFIFOHE
| SDIO_IT_STBITERR
));
1003 #else /* SDIO_STA_STBITERR not defined */
1004 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_TXUNDERR
| SDIO_IT_DATAEND
| SDIO_FLAG_TXFIFOHE
));
1005 #endif /* SDIO_STA_STBITERR */
1007 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
1012 /* Set Block Size for Card */
1013 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
1014 if(errorstate
!= HAL_SD_ERROR_NONE
)
1016 /* Clear all the static flags */
1017 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1018 hsd
->ErrorCode
|= errorstate
;
1019 hsd
->State
= HAL_SD_STATE_READY
;
1023 /* Write Blocks in Polling mode */
1024 if(NumberOfBlocks
> 1U)
1026 hsd
->Context
= (SD_CONTEXT_WRITE_MULTIPLE_BLOCK
| SD_CONTEXT_IT
);
1028 /* Write Multi Block command */
1029 errorstate
= SDMMC_CmdWriteMultiBlock(hsd
->Instance
, BlockAdd
);
1033 hsd
->Context
= (SD_CONTEXT_WRITE_SINGLE_BLOCK
| SD_CONTEXT_IT
);
1035 /* Write Single Block command */
1036 errorstate
= SDMMC_CmdWriteSingleBlock(hsd
->Instance
, BlockAdd
);
1038 if(errorstate
!= HAL_SD_ERROR_NONE
)
1040 /* Clear all the static flags */
1041 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1042 hsd
->ErrorCode
|= errorstate
;
1043 hsd
->State
= HAL_SD_STATE_READY
;
1047 /* Configure the SD DPSM (Data Path State Machine) */
1048 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
1049 config
.DataLength
= BLOCKSIZE
* NumberOfBlocks
;
1050 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_512B
;
1051 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_CARD
;
1052 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
1053 config
.DPSM
= SDIO_DPSM_ENABLE
;
1054 SDIO_ConfigData(hsd
->Instance
, &config
);
1065 * @brief Reads block(s) from a specified address in a card. The Data transfer
1066 * is managed by DMA mode.
1067 * @note This API should be followed by a check on the card state through
1068 * HAL_SD_GetCardState().
1069 * @note You could also check the DMA transfer process through the SD Rx
1071 * @param hsd: Pointer SD handle
1072 * @param pData: Pointer to the buffer that will contain the received data
1073 * @param BlockAdd: Block Address from where data is to be read
1074 * @param NumberOfBlocks: Number of blocks to read.
1075 * @retval HAL status
1077 HAL_StatusTypeDef
HAL_SD_ReadBlocks_DMA(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
)
1079 SDIO_DataInitTypeDef config
;
1080 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1084 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
1088 if(hsd
->State
== HAL_SD_STATE_READY
)
1090 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
1092 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
1094 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
1098 hsd
->State
= HAL_SD_STATE_BUSY
;
1100 /* Initialize data control register */
1101 hsd
->Instance
->DCTRL
= 0U;
1103 #ifdef SDIO_STA_STBITER
1104 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_RXOVERR
| SDIO_IT_DATAEND
| SDIO_IT_STBITERR
));
1105 #else /* SDIO_STA_STBITERR not defined */
1106 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_RXOVERR
| SDIO_IT_DATAEND
));
1107 #endif /* SDIO_STA_STBITERR */
1109 /* Set the DMA transfer complete callback */
1110 hsd
->hdmarx
->XferCpltCallback
= SD_DMAReceiveCplt
;
1112 /* Set the DMA error callback */
1113 hsd
->hdmarx
->XferErrorCallback
= SD_DMAError
;
1115 /* Set the DMA Abort callback */
1116 hsd
->hdmarx
->XferAbortCallback
= NULL
;
1118 /* Enable the DMA Channel */
1119 HAL_DMA_Start_IT(hsd
->hdmarx
, (uint32_t)&hsd
->Instance
->FIFO
, (uint32_t)pData
, (uint32_t)(BLOCKSIZE
* NumberOfBlocks
)/4);
1121 /* Enable SD DMA transfer */
1122 __HAL_SD_DMA_ENABLE(hsd
);
1124 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
1129 /* Configure the SD DPSM (Data Path State Machine) */
1130 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
1131 config
.DataLength
= BLOCKSIZE
* NumberOfBlocks
;
1132 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_512B
;
1133 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_SDIO
;
1134 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
1135 config
.DPSM
= SDIO_DPSM_ENABLE
;
1136 SDIO_ConfigData(hsd
->Instance
, &config
);
1138 /* Set Block Size for Card */
1139 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
1140 if(errorstate
!= HAL_SD_ERROR_NONE
)
1142 /* Clear all the static flags */
1143 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1144 hsd
->ErrorCode
|= errorstate
;
1145 hsd
->State
= HAL_SD_STATE_READY
;
1149 /* Read Blocks in DMA mode */
1150 if(NumberOfBlocks
> 1U)
1152 hsd
->Context
= (SD_CONTEXT_READ_MULTIPLE_BLOCK
| SD_CONTEXT_DMA
);
1154 /* Read Multi Block command */
1155 errorstate
= SDMMC_CmdReadMultiBlock(hsd
->Instance
, BlockAdd
);
1159 hsd
->Context
= (SD_CONTEXT_READ_SINGLE_BLOCK
| SD_CONTEXT_DMA
);
1161 /* Read Single Block command */
1162 errorstate
= SDMMC_CmdReadSingleBlock(hsd
->Instance
, BlockAdd
);
1164 if(errorstate
!= HAL_SD_ERROR_NONE
)
1166 /* Clear all the static flags */
1167 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1168 hsd
->ErrorCode
|= errorstate
;
1169 hsd
->State
= HAL_SD_STATE_READY
;
1182 * @brief Writes block(s) to a specified address in a card. The Data transfer
1183 * is managed by DMA mode.
1184 * @note This API should be followed by a check on the card state through
1185 * HAL_SD_GetCardState().
1186 * @note You could also check the DMA transfer process through the SD Tx
1188 * @param hsd: Pointer to SD handle
1189 * @param pData: Pointer to the buffer that will contain the data to transmit
1190 * @param BlockAdd: Block Address where data will be written
1191 * @param NumberOfBlocks: Number of blocks to write
1192 * @retval HAL status
1194 HAL_StatusTypeDef
HAL_SD_WriteBlocks_DMA(SD_HandleTypeDef
*hsd
, uint8_t *pData
, uint32_t BlockAdd
, uint32_t NumberOfBlocks
)
1196 SDIO_DataInitTypeDef config
;
1197 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1201 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
1205 if(hsd
->State
== HAL_SD_STATE_READY
)
1207 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
1209 if((BlockAdd
+ NumberOfBlocks
) > (hsd
->SdCard
.LogBlockNbr
))
1211 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
1215 hsd
->State
= HAL_SD_STATE_BUSY
;
1217 /* Initialize data control register */
1218 hsd
->Instance
->DCTRL
= 0U;
1220 /* Enable SD Error interrupts */
1221 #ifdef SDIO_STA_STBITER
1222 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_TXUNDERR
| SDIO_IT_STBITERR
));
1223 #else /* SDIO_STA_STBITERR not defined */
1224 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_TXUNDERR
));
1225 #endif /* SDIO_STA_STBITERR */
1227 /* Set the DMA transfer complete callback */
1228 hsd
->hdmatx
->XferCpltCallback
= SD_DMATransmitCplt
;
1230 /* Set the DMA error callback */
1231 hsd
->hdmatx
->XferErrorCallback
= SD_DMAError
;
1233 /* Set the DMA Abort callback */
1234 hsd
->hdmatx
->XferAbortCallback
= NULL
;
1236 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
1241 /* Set Block Size for Card */
1242 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, BLOCKSIZE
);
1243 if(errorstate
!= HAL_SD_ERROR_NONE
)
1245 /* Clear all the static flags */
1246 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1247 hsd
->ErrorCode
|= errorstate
;
1248 hsd
->State
= HAL_SD_STATE_READY
;
1252 /* Write Blocks in Polling mode */
1253 if(NumberOfBlocks
> 1U)
1255 hsd
->Context
= (SD_CONTEXT_WRITE_MULTIPLE_BLOCK
| SD_CONTEXT_DMA
);
1257 /* Write Multi Block command */
1258 errorstate
= SDMMC_CmdWriteMultiBlock(hsd
->Instance
, BlockAdd
);
1262 hsd
->Context
= (SD_CONTEXT_WRITE_SINGLE_BLOCK
| SD_CONTEXT_DMA
);
1264 /* Write Single Block command */
1265 errorstate
= SDMMC_CmdWriteSingleBlock(hsd
->Instance
, BlockAdd
);
1267 if(errorstate
!= HAL_SD_ERROR_NONE
)
1269 /* Clear all the static flags */
1270 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1271 hsd
->ErrorCode
|= errorstate
;
1272 hsd
->State
= HAL_SD_STATE_READY
;
1276 /* Enable SDIO DMA transfer */
1277 __HAL_SD_DMA_ENABLE(hsd
);
1279 /* Enable the DMA Channel */
1280 HAL_DMA_Start_IT(hsd
->hdmatx
, (uint32_t)pData
, (uint32_t)&hsd
->Instance
->FIFO
, (uint32_t)(BLOCKSIZE
* NumberOfBlocks
)/4);
1282 /* Configure the SD DPSM (Data Path State Machine) */
1283 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
1284 config
.DataLength
= BLOCKSIZE
* NumberOfBlocks
;
1285 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_512B
;
1286 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_CARD
;
1287 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
1288 config
.DPSM
= SDIO_DPSM_ENABLE
;
1289 SDIO_ConfigData(hsd
->Instance
, &config
);
1300 * @brief Erases the specified memory area of the given SD card.
1301 * @note This API should be followed by a check on the card state through
1302 * HAL_SD_GetCardState().
1303 * @param hsd: Pointer to SD handle
1304 * @param BlockStartAdd: Start Block address
1305 * @param BlockEndAdd: End Block address
1306 * @retval HAL status
1308 HAL_StatusTypeDef
HAL_SD_Erase(SD_HandleTypeDef
*hsd
, uint32_t BlockStartAdd
, uint32_t BlockEndAdd
)
1310 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1312 if(hsd
->State
== HAL_SD_STATE_READY
)
1314 hsd
->ErrorCode
= HAL_DMA_ERROR_NONE
;
1316 if(BlockEndAdd
< BlockStartAdd
)
1318 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
1322 if(BlockEndAdd
> (hsd
->SdCard
.LogBlockNbr
))
1324 hsd
->ErrorCode
|= HAL_SD_ERROR_ADDR_OUT_OF_RANGE
;
1328 hsd
->State
= HAL_SD_STATE_BUSY
;
1330 /* Check if the card command class supports erase command */
1331 if(((hsd
->SdCard
.Class
) & SDIO_CCCC_ERASE
) == 0U)
1333 /* Clear all the static flags */
1334 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1335 hsd
->ErrorCode
|= HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
1336 hsd
->State
= HAL_SD_STATE_READY
;
1340 if((SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
1342 /* Clear all the static flags */
1343 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1344 hsd
->ErrorCode
|= HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
1345 hsd
->State
= HAL_SD_STATE_READY
;
1349 /* Get start and end block for high capacity cards */
1350 if(hsd
->SdCard
.CardType
!= CARD_SDHC_SDXC
)
1352 BlockStartAdd
*= 512U;
1353 BlockEndAdd
*= 512U;
1356 /* According to sd-card spec 1.0 ERASE_GROUP_START (CMD32) and erase_group_end(CMD33) */
1357 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
1359 /* Send CMD32 SD_ERASE_GRP_START with argument as addr */
1360 errorstate
= SDMMC_CmdSDEraseStartAdd(hsd
->Instance
, BlockStartAdd
);
1361 if(errorstate
!= HAL_SD_ERROR_NONE
)
1363 /* Clear all the static flags */
1364 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1365 hsd
->ErrorCode
|= errorstate
;
1366 hsd
->State
= HAL_SD_STATE_READY
;
1370 /* Send CMD33 SD_ERASE_GRP_END with argument as addr */
1371 errorstate
= SDMMC_CmdSDEraseEndAdd(hsd
->Instance
, BlockEndAdd
);
1372 if(errorstate
!= HAL_SD_ERROR_NONE
)
1374 /* Clear all the static flags */
1375 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1376 hsd
->ErrorCode
|= errorstate
;
1377 hsd
->State
= HAL_SD_STATE_READY
;
1382 /* Send CMD38 ERASE */
1383 errorstate
= SDMMC_CmdErase(hsd
->Instance
);
1384 if(errorstate
!= HAL_SD_ERROR_NONE
)
1386 /* Clear all the static flags */
1387 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1388 hsd
->ErrorCode
|= errorstate
;
1389 hsd
->State
= HAL_SD_STATE_READY
;
1393 hsd
->State
= HAL_SD_STATE_READY
;
1404 * @brief This function handles SD card interrupt request.
1405 * @param hsd: Pointer to SD handle
1408 void HAL_SD_IRQHandler(SD_HandleTypeDef
*hsd
)
1410 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1412 /* Check for SDIO interrupt flags */
1413 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_DATAEND
) != RESET
)
1415 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_DATAEND
);
1417 #ifdef SDIO_STA_STBITERR
1418 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
1419 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
| SDIO_IT_STBITERR
);
1420 #else /* SDIO_STA_STBITERR not defined */
1421 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
1422 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
);
1425 if((hsd
->Context
& SD_CONTEXT_IT
) != RESET
)
1427 if(((hsd
->Context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) != RESET
) || ((hsd
->Context
& SD_CONTEXT_WRITE_MULTIPLE_BLOCK
) != RESET
))
1429 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
1430 if(errorstate
!= HAL_SD_ERROR_NONE
)
1432 hsd
->ErrorCode
|= errorstate
;
1433 HAL_SD_ErrorCallback(hsd
);
1437 /* Clear all the static flags */
1438 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1440 hsd
->State
= HAL_SD_STATE_READY
;
1441 if(((hsd
->Context
& SD_CONTEXT_READ_SINGLE_BLOCK
) != RESET
) || ((hsd
->Context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) != RESET
))
1443 HAL_SD_RxCpltCallback(hsd
);
1447 HAL_SD_TxCpltCallback(hsd
);
1450 else if((hsd
->Context
& SD_CONTEXT_DMA
) != RESET
)
1452 if((hsd
->Context
& SD_CONTEXT_WRITE_MULTIPLE_BLOCK
) != RESET
)
1454 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
1455 if(errorstate
!= HAL_SD_ERROR_NONE
)
1457 hsd
->ErrorCode
|= errorstate
;
1458 HAL_SD_ErrorCallback(hsd
);
1461 if(((hsd
->Context
& SD_CONTEXT_READ_SINGLE_BLOCK
) == RESET
) && ((hsd
->Context
& SD_CONTEXT_READ_MULTIPLE_BLOCK
) == RESET
))
1463 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
1464 in the SD DCTRL register */
1465 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN
);
1467 hsd
->State
= HAL_SD_STATE_READY
;
1469 HAL_SD_TxCpltCallback(hsd
);
1474 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_TXFIFOHE
) != RESET
)
1476 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_TXFIFOHE
);
1481 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_RXFIFOHF
) != RESET
)
1483 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_RXFIFOHF
);
1488 #ifdef SDIO_STA_STBITERR
1489 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_RXOVERR
| SDIO_IT_TXUNDERR
| SDIO_IT_STBITERR
) != RESET
)
1491 /* Set Error code */
1492 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_DCRCFAIL
) != RESET
)
1494 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_CRC_FAIL
;
1496 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_DTIMEOUT
) != RESET
)
1498 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_TIMEOUT
;
1500 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_RXOVERR
) != RESET
)
1502 hsd
->ErrorCode
|= HAL_SD_ERROR_RX_OVERRUN
;
1504 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_TXUNDERR
) != RESET
)
1506 hsd
->ErrorCode
|= HAL_SD_ERROR_TX_UNDERRUN
;
1508 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_STBITERR
) != RESET
)
1510 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_TIMEOUT
;
1513 /* Clear All flags */
1514 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
| SDIO_FLAG_STBITERR
);
1516 /* Disable all interrupts */
1517 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
1518 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
|SDIO_IT_STBITERR
);
1520 if((hsd
->Context
& SD_CONTEXT_DMA
) != RESET
)
1522 /* Abort the SD DMA Streams */
1523 if(hsd
->hdmatx
!= NULL
)
1525 /* Set the DMA Tx abort callback */
1526 hsd
->hdmatx
->XferAbortCallback
= SD_DMATxAbort
;
1527 /* Abort DMA in IT mode */
1528 if(HAL_DMA_Abort_IT(hsd
->hdmatx
) != HAL_OK
)
1530 SD_DMATxAbort(hsd
->hdmatx
);
1533 else if(hsd
->hdmarx
!= NULL
)
1535 /* Set the DMA Rx abort callback */
1536 hsd
->hdmarx
->XferAbortCallback
= SD_DMARxAbort
;
1537 /* Abort DMA in IT mode */
1538 if(HAL_DMA_Abort_IT(hsd
->hdmarx
) != HAL_OK
)
1540 SD_DMARxAbort(hsd
->hdmarx
);
1545 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
1546 hsd
->State
= HAL_SD_STATE_READY
;
1547 HAL_SD_AbortCallback(hsd
);
1550 else if((hsd
->Context
& SD_CONTEXT_IT
) != RESET
)
1552 /* Set the SD state to ready to be able to start again the process */
1553 hsd
->State
= HAL_SD_STATE_READY
;
1554 HAL_SD_ErrorCallback(hsd
);
1557 #else /* SDIO_STA_STBITERR not defined */
1558 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
| SDIO_IT_RXOVERR
| SDIO_IT_TXUNDERR
) != RESET
)
1560 /* Set Error code */
1561 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_DCRCFAIL
) != RESET
)
1563 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_CRC_FAIL
;
1565 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_DTIMEOUT
) != RESET
)
1567 hsd
->ErrorCode
|= HAL_SD_ERROR_DATA_TIMEOUT
;
1569 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_RXOVERR
) != RESET
)
1571 hsd
->ErrorCode
|= HAL_SD_ERROR_RX_OVERRUN
;
1573 if(__HAL_SD_GET_FLAG(hsd
, SDIO_IT_TXUNDERR
) != RESET
)
1575 hsd
->ErrorCode
|= HAL_SD_ERROR_TX_UNDERRUN
;
1578 /* Clear All flags */
1579 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1581 /* Disable all interrupts */
1582 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
1583 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
);
1585 if((hsd
->Context
& SD_CONTEXT_DMA
) != RESET
)
1587 /* Abort the SD DMA Streams */
1588 if(hsd
->hdmatx
!= NULL
)
1590 /* Set the DMA Tx abort callback */
1591 hsd
->hdmatx
->XferAbortCallback
= SD_DMATxAbort
;
1592 /* Abort DMA in IT mode */
1593 if(HAL_DMA_Abort_IT(hsd
->hdmatx
) != HAL_OK
)
1595 SD_DMATxAbort(hsd
->hdmatx
);
1598 else if(hsd
->hdmarx
!= NULL
)
1600 /* Set the DMA Rx abort callback */
1601 hsd
->hdmarx
->XferAbortCallback
= SD_DMARxAbort
;
1602 /* Abort DMA in IT mode */
1603 if(HAL_DMA_Abort_IT(hsd
->hdmarx
) != HAL_OK
)
1605 SD_DMARxAbort(hsd
->hdmarx
);
1610 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
1611 hsd
->State
= HAL_SD_STATE_READY
;
1612 HAL_SD_AbortCallback(hsd
);
1615 else if((hsd
->Context
& SD_CONTEXT_IT
) != RESET
)
1617 /* Set the SD state to ready to be able to start again the process */
1618 hsd
->State
= HAL_SD_STATE_READY
;
1619 HAL_SD_ErrorCallback(hsd
);
1626 * @brief return the SD state
1627 * @param hsd: Pointer to sd handle
1630 HAL_SD_StateTypeDef
HAL_SD_GetState(SD_HandleTypeDef
*hsd
)
1636 * @brief Return the SD error code
1637 * @param hsd : Pointer to a SD_HandleTypeDef structure that contains
1638 * the configuration information.
1639 * @retval SD Error Code
1641 uint32_t HAL_SD_GetError(SD_HandleTypeDef
*hsd
)
1643 return hsd
->ErrorCode
;
1647 * @brief Tx Transfer completed callbacks
1648 * @param hsd: Pointer to SD handle
1651 __weak
void HAL_SD_TxCpltCallback(SD_HandleTypeDef
*hsd
)
1653 /* Prevent unused argument(s) compilation warning */
1656 /* NOTE : This function should not be modified, when the callback is needed,
1657 the HAL_SD_TxCpltCallback can be implemented in the user file
1662 * @brief Rx Transfer completed callbacks
1663 * @param hsd: Pointer SD handle
1666 __weak
void HAL_SD_RxCpltCallback(SD_HandleTypeDef
*hsd
)
1668 /* Prevent unused argument(s) compilation warning */
1671 /* NOTE : This function should not be modified, when the callback is needed,
1672 the HAL_SD_RxCpltCallback can be implemented in the user file
1677 * @brief SD error callbacks
1678 * @param hsd: Pointer SD handle
1681 __weak
void HAL_SD_ErrorCallback(SD_HandleTypeDef
*hsd
)
1683 /* Prevent unused argument(s) compilation warning */
1686 /* NOTE : This function should not be modified, when the callback is needed,
1687 the HAL_SD_ErrorCallback can be implemented in the user file
1692 * @brief SD Abort callbacks
1693 * @param hsd: Pointer SD handle
1696 __weak
void HAL_SD_AbortCallback(SD_HandleTypeDef
*hsd
)
1698 /* Prevent unused argument(s) compilation warning */
1701 /* NOTE : This function should not be modified, when the callback is needed,
1702 the HAL_SD_ErrorCallback can be implemented in the user file
1711 /** @addtogroup SD_Exported_Functions_Group3
1712 * @brief management functions
1715 ==============================================================================
1716 ##### Peripheral Control functions #####
1717 ==============================================================================
1719 This subsection provides a set of functions allowing to control the SD card
1720 operations and get the related information
1727 * @brief Returns information the information of the card which are stored on
1729 * @param hsd: Pointer to SD handle
1730 * @param pCID: Pointer to a HAL_SD_CIDTypeDef structure that
1731 * contains all CID register parameters
1732 * @retval HAL status
1734 HAL_StatusTypeDef
HAL_SD_GetCardCID(SD_HandleTypeDef
*hsd
, HAL_SD_CardCIDTypeDef
*pCID
)
1739 tmp
= (uint8_t)((hsd
->CID
[0U] & 0xFF000000U
) >> 24U);
1740 pCID
->ManufacturerID
= tmp
;
1743 tmp
= (uint8_t)((hsd
->CID
[0U] & 0x00FF0000U
) >> 16U);
1744 pCID
->OEM_AppliID
= tmp
<< 8U;
1747 tmp
= (uint8_t)((hsd
->CID
[0U] & 0x000000FF00U
) >> 8U);
1748 pCID
->OEM_AppliID
|= tmp
;
1751 tmp
= (uint8_t)(hsd
->CID
[0U] & 0x000000FFU
);
1752 pCID
->ProdName1
= tmp
<< 24U;
1755 tmp
= (uint8_t)((hsd
->CID
[1U] & 0xFF000000U
) >> 24U);
1756 pCID
->ProdName1
|= tmp
<< 16;
1759 tmp
= (uint8_t)((hsd
->CID
[1U] & 0x00FF0000U
) >> 16U);
1760 pCID
->ProdName1
|= tmp
<< 8U;
1763 tmp
= (uint8_t)((hsd
->CID
[1U] & 0x0000FF00U
) >> 8U);
1764 pCID
->ProdName1
|= tmp
;
1767 tmp
= (uint8_t)(hsd
->CID
[1U] & 0x000000FFU
);
1768 pCID
->ProdName2
= tmp
;
1771 tmp
= (uint8_t)((hsd
->CID
[2U] & 0xFF000000U
) >> 24U);
1772 pCID
->ProdRev
= tmp
;
1775 tmp
= (uint8_t)((hsd
->CID
[2U] & 0x00FF0000U
) >> 16U);
1776 pCID
->ProdSN
= tmp
<< 24U;
1779 tmp
= (uint8_t)((hsd
->CID
[2U] & 0x0000FF00U
) >> 8U);
1780 pCID
->ProdSN
|= tmp
<< 16U;
1783 tmp
= (uint8_t)(hsd
->CID
[2U] & 0x000000FFU
);
1784 pCID
->ProdSN
|= tmp
<< 8U;
1787 tmp
= (uint8_t)((hsd
->CID
[3U] & 0xFF000000U
) >> 24U);
1788 pCID
->ProdSN
|= tmp
;
1791 tmp
= (uint8_t)((hsd
->CID
[3U] & 0x00FF0000U
) >> 16U);
1792 pCID
->Reserved1
|= (tmp
& 0xF0U
) >> 4U;
1793 pCID
->ManufactDate
= (tmp
& 0x0FU
) << 8U;
1796 tmp
= (uint8_t)((hsd
->CID
[3U] & 0x0000FF00U
) >> 8U);
1797 pCID
->ManufactDate
|= tmp
;
1800 tmp
= (uint8_t)(hsd
->CID
[3U] & 0x000000FFU
);
1801 pCID
->CID_CRC
= (tmp
& 0xFEU
) >> 1U;
1802 pCID
->Reserved2
= 1U;
1808 * @brief Returns information the information of the card which are stored on
1810 * @param hsd: Pointer to SD handle
1811 * @param pCSD: Pointer to a HAL_SD_CardCSDTypeDef structure that
1812 * contains all CSD register parameters
1813 * @retval HAL status
1815 HAL_StatusTypeDef
HAL_SD_GetCardCSD(SD_HandleTypeDef
*hsd
, HAL_SD_CardCSDTypeDef
*pCSD
)
1820 tmp
= (hsd
->CSD
[0U] & 0xFF000000U
) >> 24U;
1821 pCSD
->CSDStruct
= (uint8_t)((tmp
& 0xC0U
) >> 6U);
1822 pCSD
->SysSpecVersion
= (uint8_t)((tmp
& 0x3CU
) >> 2U);
1823 pCSD
->Reserved1
= tmp
& 0x03U
;
1826 tmp
= (hsd
->CSD
[0U] & 0x00FF0000U
) >> 16U;
1827 pCSD
->TAAC
= (uint8_t)tmp
;
1830 tmp
= (hsd
->CSD
[0U] & 0x0000FF00U
) >> 8U;
1831 pCSD
->NSAC
= (uint8_t)tmp
;
1834 tmp
= hsd
->CSD
[0U] & 0x000000FFU
;
1835 pCSD
->MaxBusClkFrec
= (uint8_t)tmp
;
1838 tmp
= (hsd
->CSD
[1U] & 0xFF000000U
) >> 24U;
1839 pCSD
->CardComdClasses
= (uint16_t)(tmp
<< 4U);
1842 tmp
= (hsd
->CSD
[1U] & 0x00FF0000U
) >> 16U;
1843 pCSD
->CardComdClasses
|= (uint16_t)((tmp
& 0xF0U
) >> 4U);
1844 pCSD
->RdBlockLen
= (uint8_t)(tmp
& 0x0FU
);
1847 tmp
= (hsd
->CSD
[1U] & 0x0000FF00U
) >> 8U;
1848 pCSD
->PartBlockRead
= (uint8_t)((tmp
& 0x80U
) >> 7U);
1849 pCSD
->WrBlockMisalign
= (uint8_t)((tmp
& 0x40U
) >> 6U);
1850 pCSD
->RdBlockMisalign
= (uint8_t)((tmp
& 0x20U
) >> 5U);
1851 pCSD
->DSRImpl
= (uint8_t)((tmp
& 0x10U
) >> 4U);
1852 pCSD
->Reserved2
= 0U; /*!< Reserved */
1854 if(hsd
->SdCard
.CardType
== CARD_SDSC
)
1856 pCSD
->DeviceSize
= (tmp
& 0x03U
) << 10U;
1859 tmp
= (uint8_t)(hsd
->CSD
[1U] & 0x000000FFU
);
1860 pCSD
->DeviceSize
|= (tmp
) << 2U;
1863 tmp
= (uint8_t)((hsd
->CSD
[2U] & 0xFF000000U
) >> 24U);
1864 pCSD
->DeviceSize
|= (tmp
& 0xC0U
) >> 6U;
1866 pCSD
->MaxRdCurrentVDDMin
= (tmp
& 0x38U
) >> 3U;
1867 pCSD
->MaxRdCurrentVDDMax
= (tmp
& 0x07U
);
1870 tmp
= (uint8_t)((hsd
->CSD
[2U] & 0x00FF0000U
) >> 16U);
1871 pCSD
->MaxWrCurrentVDDMin
= (tmp
& 0xE0U
) >> 5U;
1872 pCSD
->MaxWrCurrentVDDMax
= (tmp
& 0x1CU
) >> 2U;
1873 pCSD
->DeviceSizeMul
= (tmp
& 0x03U
) << 1U;
1875 tmp
= (uint8_t)((hsd
->CSD
[2U] & 0x0000FF00U
) >> 8U);
1876 pCSD
->DeviceSizeMul
|= (tmp
& 0x80U
) >> 7U;
1878 hsd
->SdCard
.BlockNbr
= (pCSD
->DeviceSize
+ 1U) ;
1879 hsd
->SdCard
.BlockNbr
*= (1U << (pCSD
->DeviceSizeMul
+ 2U));
1880 hsd
->SdCard
.BlockSize
= 1U << (pCSD
->RdBlockLen
);
1882 hsd
->SdCard
.LogBlockNbr
= (hsd
->SdCard
.BlockNbr
) * ((hsd
->SdCard
.BlockSize
) / 512U);
1883 hsd
->SdCard
.LogBlockSize
= 512U;
1885 else if(hsd
->SdCard
.CardType
== CARD_SDHC_SDXC
)
1888 tmp
= (uint8_t)(hsd
->CSD
[1U] & 0x000000FFU
);
1889 pCSD
->DeviceSize
= (tmp
& 0x3FU
) << 16U;
1892 tmp
= (uint8_t)((hsd
->CSD
[2U] & 0xFF000000U
) >> 24U);
1894 pCSD
->DeviceSize
|= (tmp
<< 8U);
1897 tmp
= (uint8_t)((hsd
->CSD
[2U] & 0x00FF0000U
) >> 16U);
1899 pCSD
->DeviceSize
|= (tmp
);
1902 tmp
= (uint8_t)((hsd
->CSD
[2U] & 0x0000FF00U
) >> 8U);
1904 hsd
->SdCard
.LogBlockNbr
= hsd
->SdCard
.BlockNbr
= (((uint64_t)pCSD
->DeviceSize
+ 1U) * 1024U);
1905 hsd
->SdCard
.LogBlockSize
= hsd
->SdCard
.BlockSize
= 512U;
1909 /* Clear all the static flags */
1910 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1911 hsd
->ErrorCode
|= HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
1912 hsd
->State
= HAL_SD_STATE_READY
;
1916 pCSD
->EraseGrSize
= (tmp
& 0x40U
) >> 6U;
1917 pCSD
->EraseGrMul
= (tmp
& 0x3FU
) << 1U;
1920 tmp
= (uint8_t)(hsd
->CSD
[2U] & 0x000000FFU
);
1921 pCSD
->EraseGrMul
|= (tmp
& 0x80U
) >> 7U;
1922 pCSD
->WrProtectGrSize
= (tmp
& 0x7FU
);
1925 tmp
= (uint8_t)((hsd
->CSD
[3U] & 0xFF000000U
) >> 24U);
1926 pCSD
->WrProtectGrEnable
= (tmp
& 0x80U
) >> 7U;
1927 pCSD
->ManDeflECC
= (tmp
& 0x60U
) >> 5U;
1928 pCSD
->WrSpeedFact
= (tmp
& 0x1CU
) >> 2U;
1929 pCSD
->MaxWrBlockLen
= (tmp
& 0x03U
) << 2U;
1932 tmp
= (uint8_t)((hsd
->CSD
[3U] & 0x00FF0000U
) >> 16U);
1933 pCSD
->MaxWrBlockLen
|= (tmp
& 0xC0U
) >> 6U;
1934 pCSD
->WriteBlockPaPartial
= (tmp
& 0x20U
) >> 5U;
1935 pCSD
->Reserved3
= 0U;
1936 pCSD
->ContentProtectAppli
= (tmp
& 0x01U
);
1939 tmp
= (uint8_t)((hsd
->CSD
[3U] & 0x0000FF00U
) >> 8U);
1940 pCSD
->FileFormatGrouop
= (tmp
& 0x80U
) >> 7U;
1941 pCSD
->CopyFlag
= (tmp
& 0x40U
) >> 6U;
1942 pCSD
->PermWrProtect
= (tmp
& 0x20U
) >> 5U;
1943 pCSD
->TempWrProtect
= (tmp
& 0x10U
) >> 4U;
1944 pCSD
->FileFormat
= (tmp
& 0x0CU
) >> 2U;
1945 pCSD
->ECC
= (tmp
& 0x03U
);
1948 tmp
= (uint8_t)(hsd
->CSD
[3U] & 0x000000FFU
);
1949 pCSD
->CSD_CRC
= (tmp
& 0xFEU
) >> 1U;
1950 pCSD
->Reserved4
= 1U;
1956 * @brief Gets the SD status info.
1957 * @param hsd: Pointer to SD handle
1958 * @param pStatus: Pointer to the HAL_SD_CardStatusTypeDef structure that
1959 * will contain the SD card status information
1960 * @retval HAL status
1962 HAL_StatusTypeDef
HAL_SD_GetCardStatus(SD_HandleTypeDef
*hsd
, HAL_SD_CardStatusTypeDef
*pStatus
)
1965 uint32_t sd_status
[16U];
1966 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
1968 errorstate
= SD_SendSDStatus(hsd
, sd_status
);
1969 if(errorstate
!= HAL_OK
)
1971 /* Clear all the static flags */
1972 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
1973 hsd
->ErrorCode
|= errorstate
;
1974 hsd
->State
= HAL_SD_STATE_READY
;
1980 tmp
= (sd_status
[0U] & 0xC0U
) >> 6U;
1981 pStatus
->DataBusWidth
= (uint8_t)tmp
;
1984 tmp
= (sd_status
[0U] & 0x20U
) >> 5U;
1985 pStatus
->SecuredMode
= (uint8_t)tmp
;
1988 tmp
= (sd_status
[0U] & 0x00FF0000U
) >> 16U;
1989 pStatus
->CardType
= (uint16_t)(tmp
<< 8U);
1992 tmp
= (sd_status
[0U] & 0xFF000000U
) >> 24U;
1993 pStatus
->CardType
|= (uint16_t)tmp
;
1996 tmp
= (sd_status
[1U] & 0xFFU
);
1997 pStatus
->ProtectedAreaSize
= (uint32_t)(tmp
<< 24U);
2000 tmp
= (sd_status
[1U] & 0xFF00U
) >> 8U;
2001 pStatus
->ProtectedAreaSize
|= (uint32_t)(tmp
<< 16U);
2004 tmp
= (sd_status
[1U] & 0xFF0000U
) >> 16U;
2005 pStatus
->ProtectedAreaSize
|= (uint32_t)(tmp
<< 8U);
2008 tmp
= (sd_status
[1U] & 0xFF000000U
) >> 24U;
2009 pStatus
->ProtectedAreaSize
|= (uint32_t)tmp
;
2012 tmp
= (sd_status
[2U] & 0xFFU
);
2013 pStatus
->SpeedClass
= (uint8_t)tmp
;
2016 tmp
= (sd_status
[2U] & 0xFF00U
) >> 8U;
2017 pStatus
->PerformanceMove
= (uint8_t)tmp
;
2020 tmp
= (sd_status
[2U] & 0xF00000U
) >> 20U;
2021 pStatus
->AllocationUnitSize
= (uint8_t)tmp
;
2024 tmp
= (sd_status
[2U] & 0xFF000000U
) >> 24U;
2025 pStatus
->EraseSize
= (uint16_t)(tmp
<< 8U);
2028 tmp
= (sd_status
[3U] & 0xFFU
);
2029 pStatus
->EraseSize
|= (uint16_t)tmp
;
2032 tmp
= (sd_status
[3U] & 0xFC00U
) >> 10U;
2033 pStatus
->EraseTimeout
= (uint8_t)tmp
;
2036 tmp
= (sd_status
[3U] & 0x0300U
) >> 8U;
2037 pStatus
->EraseOffset
= (uint8_t)tmp
;
2044 * @brief Gets the SD card info.
2045 * @param hsd: Pointer to SD handle
2046 * @param pCardInfo: Pointer to the HAL_SD_CardInfoTypeDef structure that
2047 * will contain the SD card status information
2048 * @retval HAL status
2050 HAL_StatusTypeDef
HAL_SD_GetCardInfo(SD_HandleTypeDef
*hsd
, HAL_SD_CardInfoTypeDef
*pCardInfo
)
2052 pCardInfo
->CardType
= (uint32_t)(hsd
->SdCard
.CardType
);
2053 pCardInfo
->CardVersion
= (uint32_t)(hsd
->SdCard
.CardVersion
);
2054 pCardInfo
->Class
= (uint32_t)(hsd
->SdCard
.Class
);
2055 pCardInfo
->RelCardAdd
= (uint32_t)(hsd
->SdCard
.RelCardAdd
);
2056 pCardInfo
->BlockNbr
= (uint32_t)(hsd
->SdCard
.BlockNbr
);
2057 pCardInfo
->BlockSize
= (uint32_t)(hsd
->SdCard
.BlockSize
);
2058 pCardInfo
->LogBlockNbr
= (uint32_t)(hsd
->SdCard
.LogBlockNbr
);
2059 pCardInfo
->LogBlockSize
= (uint32_t)(hsd
->SdCard
.LogBlockSize
);
2065 * @brief Enables wide bus operation for the requested card if supported by
2067 * @param hsd: Pointer to SD handle
2068 * @param WideMode: Specifies the SD card wide bus mode
2069 * This parameter can be one of the following values:
2070 * @arg SDIO_BUS_WIDE_8B: 8-bit data transfer
2071 * @arg SDIO_BUS_WIDE_4B: 4-bit data transfer
2072 * @arg SDIO_BUS_WIDE_1B: 1-bit data transfer
2073 * @retval HAL status
2075 HAL_StatusTypeDef
HAL_SD_ConfigWideBusOperation(SD_HandleTypeDef
*hsd
, uint32_t WideMode
)
2077 SDIO_InitTypeDef Init
;
2078 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2080 /* Check the parameters */
2081 assert_param(IS_SDIO_BUS_WIDE(WideMode
));
2084 hsd
->State
= HAL_SD_STATE_BUSY
;
2086 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2088 if(WideMode
== SDIO_BUS_WIDE_8B
)
2090 hsd
->ErrorCode
|= HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2092 else if(WideMode
== SDIO_BUS_WIDE_4B
)
2094 errorstate
= SD_WideBus_Enable(hsd
);
2096 hsd
->ErrorCode
|= errorstate
;
2098 else if(WideMode
== SDIO_BUS_WIDE_1B
)
2100 errorstate
= SD_WideBus_Disable(hsd
);
2102 hsd
->ErrorCode
|= errorstate
;
2106 /* WideMode is not a valid argument*/
2107 hsd
->ErrorCode
|= HAL_SD_ERROR_PARAM
;
2112 /* MMC Card does not support this feature */
2113 hsd
->ErrorCode
|= HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2116 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2118 /* Clear all the static flags */
2119 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2120 hsd
->State
= HAL_SD_STATE_READY
;
2125 /* Configure the SDIO peripheral */
2126 Init
.ClockEdge
= hsd
->Init
.ClockEdge
;
2127 Init
.ClockBypass
= hsd
->Init
.ClockBypass
;
2128 Init
.ClockPowerSave
= hsd
->Init
.ClockPowerSave
;
2129 Init
.BusWide
= WideMode
;
2130 Init
.HardwareFlowControl
= hsd
->Init
.HardwareFlowControl
;
2131 Init
.ClockDiv
= hsd
->Init
.ClockDiv
;
2132 SDIO_Init(hsd
->Instance
, Init
);
2136 hsd
->State
= HAL_SD_STATE_READY
;
2143 * @brief Gets the current sd card data state.
2144 * @param hsd: pointer to SD handle
2145 * @retval Card state
2147 HAL_SD_CardStateTypeDef
HAL_SD_GetCardState(SD_HandleTypeDef
*hsd
)
2149 HAL_SD_CardStateTypeDef cardstate
= HAL_SD_CARD_TRANSFER
;
2150 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2153 errorstate
= SD_SendStatus(hsd
, &resp1
);
2154 if(errorstate
!= HAL_OK
)
2156 hsd
->ErrorCode
|= errorstate
;
2159 cardstate
= (HAL_SD_CardStateTypeDef
)((resp1
>> 9U) & 0x0FU
);
2165 * @brief Abort the current transfer and disable the SD.
2166 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2167 * the configuration information for SD module.
2168 * @retval HAL status
2170 HAL_StatusTypeDef
HAL_SD_Abort(SD_HandleTypeDef
*hsd
)
2172 HAL_SD_CardStateTypeDef CardState
;
2174 /* DIsable All interrupts */
2175 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
2176 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
);
2178 /* Clear All flags */
2179 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2181 if((hsd
->hdmatx
!= NULL
) || (hsd
->hdmarx
!= NULL
))
2183 /* Disable the SD DMA request */
2184 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN
);
2186 /* Abort the SD DMA Tx Stream */
2187 if(hsd
->hdmatx
!= NULL
)
2189 HAL_DMA_Abort(hsd
->hdmatx
);
2191 /* Abort the SD DMA Rx Stream */
2192 if(hsd
->hdmarx
!= NULL
)
2194 HAL_DMA_Abort(hsd
->hdmarx
);
2198 hsd
->State
= HAL_SD_STATE_READY
;
2199 CardState
= HAL_SD_GetCardState(hsd
);
2200 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2202 hsd
->ErrorCode
= SDMMC_CmdStopTransfer(hsd
->Instance
);
2204 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2212 * @brief Abort the current transfer and disable the SD (IT mode).
2213 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2214 * the configuration information for SD module.
2215 * @retval HAL status
2217 HAL_StatusTypeDef
HAL_SD_Abort_IT(SD_HandleTypeDef
*hsd
)
2219 HAL_SD_CardStateTypeDef CardState
;
2221 /* DIsable All interrupts */
2222 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
2223 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
);
2225 /* Clear All flags */
2226 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2228 if((hsd
->hdmatx
!= NULL
) || (hsd
->hdmarx
!= NULL
))
2230 /* Disable the SD DMA request */
2231 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN
);
2233 /* Abort the SD DMA Tx Stream */
2234 if(hsd
->hdmatx
!= NULL
)
2236 hsd
->hdmatx
->XferAbortCallback
= SD_DMATxAbort
;
2237 if(HAL_DMA_Abort_IT(hsd
->hdmatx
) != HAL_OK
)
2242 /* Abort the SD DMA Rx Stream */
2243 if(hsd
->hdmarx
!= NULL
)
2245 hsd
->hdmarx
->XferAbortCallback
= SD_DMARxAbort
;
2246 if(HAL_DMA_Abort_IT(hsd
->hdmarx
) != HAL_OK
)
2253 /* No transfer ongoing on both DMA channels*/
2254 if((hsd
->hdmatx
== NULL
) && (hsd
->hdmarx
== NULL
))
2256 CardState
= HAL_SD_GetCardState(hsd
);
2257 hsd
->State
= HAL_SD_STATE_READY
;
2258 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2260 hsd
->ErrorCode
= SDMMC_CmdStopTransfer(hsd
->Instance
);
2262 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2268 HAL_SD_AbortCallback(hsd
);
2283 /* Private function ----------------------------------------------------------*/
2284 /** @addtogroup SD_Private_Functions
2289 * @brief DMA SD transmit process complete callback
2290 * @param hdma: DMA handle
2293 static void SD_DMATransmitCplt(DMA_HandleTypeDef
*hdma
)
2295 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2297 /* Enable DATAEND Interrupt */
2298 __HAL_SD_ENABLE_IT(hsd
, (SDIO_IT_DATAEND
));
2302 * @brief DMA SD receive process complete callback
2303 * @param hdma: DMA handle
2306 static void SD_DMAReceiveCplt(DMA_HandleTypeDef
*hdma
)
2308 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2309 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2311 /* Send stop command in multiblock write */
2312 if(hsd
->Context
== (SD_CONTEXT_READ_MULTIPLE_BLOCK
| SD_CONTEXT_DMA
))
2314 errorstate
= SDMMC_CmdStopTransfer(hsd
->Instance
);
2315 if(errorstate
!= HAL_SD_ERROR_NONE
)
2317 hsd
->ErrorCode
|= errorstate
;
2318 HAL_SD_ErrorCallback(hsd
);
2322 /* Disable the DMA transfer for transmit request by setting the DMAEN bit
2323 in the SD DCTRL register */
2324 hsd
->Instance
->DCTRL
&= (uint32_t)~((uint32_t)SDIO_DCTRL_DMAEN
);
2326 /* Clear all the static flags */
2327 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2329 hsd
->State
= HAL_SD_STATE_READY
;
2331 HAL_SD_RxCpltCallback(hsd
);
2335 * @brief DMA SD communication error callback
2336 * @param hdma: DMA handle
2339 static void SD_DMAError(DMA_HandleTypeDef
*hdma
)
2341 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2342 HAL_SD_CardStateTypeDef CardState
;
2344 if((hsd
->hdmarx
->ErrorCode
== HAL_DMA_ERROR_TE
) || (hsd
->hdmatx
->ErrorCode
== HAL_DMA_ERROR_TE
))
2346 /* Clear All flags */
2347 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2349 /* Disable All interrupts */
2350 __HAL_SD_DISABLE_IT(hsd
, SDIO_IT_DATAEND
| SDIO_IT_DCRCFAIL
| SDIO_IT_DTIMEOUT
|\
2351 SDIO_IT_TXUNDERR
| SDIO_IT_RXOVERR
);
2353 hsd
->ErrorCode
|= HAL_SD_ERROR_DMA
;
2354 CardState
= HAL_SD_GetCardState(hsd
);
2355 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2357 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
2360 hsd
->State
= HAL_SD_STATE_READY
;
2363 HAL_SD_ErrorCallback(hsd
);
2367 * @brief DMA SD Tx Abort callback
2368 * @param hdma: DMA handle
2371 static void SD_DMATxAbort(DMA_HandleTypeDef
*hdma
)
2373 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2374 HAL_SD_CardStateTypeDef CardState
;
2376 if(hsd
->hdmatx
!= NULL
)
2381 /* All DMA channels are aborted */
2382 if(hsd
->hdmarx
== NULL
)
2384 CardState
= HAL_SD_GetCardState(hsd
);
2385 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
2386 hsd
->State
= HAL_SD_STATE_READY
;
2387 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2389 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
2391 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2393 HAL_SD_AbortCallback(hsd
);
2397 HAL_SD_ErrorCallback(hsd
);
2404 * @brief DMA SD Rx Abort callback
2405 * @param hdma: DMA handle
2408 static void SD_DMARxAbort(DMA_HandleTypeDef
*hdma
)
2410 SD_HandleTypeDef
* hsd
= (SD_HandleTypeDef
* )(hdma
->Parent
);
2411 HAL_SD_CardStateTypeDef CardState
;
2413 if(hsd
->hdmarx
!= NULL
)
2418 /* All DMA channels are aborted */
2419 if(hsd
->hdmatx
== NULL
)
2421 CardState
= HAL_SD_GetCardState(hsd
);
2422 hsd
->ErrorCode
= HAL_SD_ERROR_NONE
;
2423 hsd
->State
= HAL_SD_STATE_READY
;
2424 if((CardState
== HAL_SD_CARD_RECEIVING
) || (CardState
== HAL_SD_CARD_SENDING
))
2426 hsd
->ErrorCode
|= SDMMC_CmdStopTransfer(hsd
->Instance
);
2428 if(hsd
->ErrorCode
!= HAL_SD_ERROR_NONE
)
2430 HAL_SD_AbortCallback(hsd
);
2434 HAL_SD_ErrorCallback(hsd
);
2442 * @brief Initializes the sd card.
2443 * @param hsd: Pointer to SD handle
2444 * @retval SD Card error state
2446 static uint32_t SD_InitCard(SD_HandleTypeDef
*hsd
)
2448 HAL_SD_CardCSDTypeDef CSD
;
2449 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2450 uint16_t sd_rca
= 1U;
2452 /* Check the power State */
2453 if(SDIO_GetPowerState(hsd
->Instance
) == 0U)
2456 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
2459 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2461 /* Send CMD2 ALL_SEND_CID */
2462 errorstate
= SDMMC_CmdSendCID(hsd
->Instance
);
2463 if(errorstate
!= HAL_SD_ERROR_NONE
)
2469 /* Get Card identification number data */
2470 hsd
->CID
[0U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
);
2471 hsd
->CID
[1U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP2
);
2472 hsd
->CID
[2U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP3
);
2473 hsd
->CID
[3U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP4
);
2477 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2479 /* Send CMD3 SET_REL_ADDR with argument 0 */
2480 /* SD Card publishes its RCA. */
2481 errorstate
= SDMMC_CmdSetRelAdd(hsd
->Instance
, &sd_rca
);
2482 if(errorstate
!= HAL_SD_ERROR_NONE
)
2487 if(hsd
->SdCard
.CardType
!= CARD_SECURED
)
2489 /* Get the SD card RCA */
2490 hsd
->SdCard
.RelCardAdd
= sd_rca
;
2492 /* Send CMD9 SEND_CSD with argument as card's RCA */
2493 errorstate
= SDMMC_CmdSendCSD(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
2494 if(errorstate
!= HAL_SD_ERROR_NONE
)
2500 /* Get Card Specific Data */
2501 hsd
->CSD
[0U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
);
2502 hsd
->CSD
[1U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP2
);
2503 hsd
->CSD
[2U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP3
);
2504 hsd
->CSD
[3U] = SDIO_GetResponse(hsd
->Instance
, SDIO_RESP4
);
2508 /* Get the Card Class */
2509 hsd
->SdCard
.Class
= (SDIO_GetResponse(hsd
->Instance
, SDIO_RESP2
) >> 20U);
2511 /* Get CSD parameters */
2512 HAL_SD_GetCardCSD(hsd
, &CSD
);
2514 /* Select the Card */
2515 errorstate
= SDMMC_CmdSelDesel(hsd
->Instance
, (uint32_t)(((uint32_t)hsd
->SdCard
.RelCardAdd
) << 16U));
2516 if(errorstate
!= HAL_SD_ERROR_NONE
)
2521 /* Configure SDIO peripheral interface */
2522 SDIO_Init(hsd
->Instance
, hsd
->Init
);
2524 /* All cards are initialized */
2525 return HAL_SD_ERROR_NONE
;
2529 * @brief Enquires cards about their operating voltage and configures clock
2530 * controls and stores SD information that will be needed in future
2532 * @param hsd: Pointer to SD handle
2533 * @retval error state
2535 static uint32_t SD_PowerON(SD_HandleTypeDef
*hsd
)
2537 __IO
uint32_t count
= 0U;
2538 uint32_t response
= 0U, validvoltage
= 0U;
2539 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2541 /* CMD0: GO_IDLE_STATE */
2542 errorstate
= SDMMC_CmdGoIdleState(hsd
->Instance
);
2543 if(errorstate
!= HAL_SD_ERROR_NONE
)
2548 /* CMD8: SEND_IF_COND: Command available only on V2.0 cards */
2549 errorstate
= SDMMC_CmdOperCond(hsd
->Instance
);
2550 if(errorstate
!= HAL_SD_ERROR_NONE
)
2552 hsd
->SdCard
.CardVersion
= CARD_V1_X
;
2554 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2555 while(validvoltage
== 0U)
2557 if(count
++ == SDMMC_MAX_VOLT_TRIAL
)
2559 return HAL_SD_ERROR_INVALID_VOLTRANGE
;
2562 /* SEND CMD55 APP_CMD with RCA as 0 */
2563 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, 0U);
2564 if(errorstate
!= HAL_SD_ERROR_NONE
)
2566 return HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2570 errorstate
= SDMMC_CmdAppOperCommand(hsd
->Instance
, SDMMC_STD_CAPACITY
);
2571 if(errorstate
!= HAL_SD_ERROR_NONE
)
2573 return HAL_SD_ERROR_UNSUPPORTED_FEATURE
;
2576 /* Get command response */
2577 response
= SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
);
2579 /* Get operating voltage*/
2580 validvoltage
= (((response
>> 31U) == 1U) ? 1U : 0U);
2582 /* Card type is SDSC */
2583 hsd
->SdCard
.CardType
= CARD_SDSC
;
2587 hsd
->SdCard
.CardVersion
= CARD_V2_X
;
2589 /* Send ACMD41 SD_APP_OP_COND with Argument 0x80100000 */
2590 while(validvoltage
== 0U)
2592 if(count
++ == SDMMC_MAX_VOLT_TRIAL
)
2594 return HAL_SD_ERROR_INVALID_VOLTRANGE
;
2597 /* SEND CMD55 APP_CMD with RCA as 0 */
2598 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, 0U);
2599 if(errorstate
!= HAL_SD_ERROR_NONE
)
2605 errorstate
= SDMMC_CmdAppOperCommand(hsd
->Instance
, SDMMC_HIGH_CAPACITY
);
2606 if(errorstate
!= HAL_SD_ERROR_NONE
)
2611 /* Get command response */
2612 response
= SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
);
2614 /* Get operating voltage*/
2615 validvoltage
= (((response
>> 31U) == 1U) ? 1U : 0U);
2618 if((response
& SDMMC_HIGH_CAPACITY
) == SDMMC_HIGH_CAPACITY
) /* (response &= SD_HIGH_CAPACITY) */
2620 hsd
->SdCard
.CardType
= CARD_SDHC_SDXC
;
2624 hsd
->SdCard
.CardType
= CARD_SDSC
;
2628 return HAL_SD_ERROR_NONE
;
2632 * @brief Turns the SDIO output signals off.
2633 * @param hsd: Pointer to SD handle
2634 * @retval HAL status
2636 static HAL_StatusTypeDef
SD_PowerOFF(SD_HandleTypeDef
*hsd
)
2638 /* Set Power State to OFF */
2639 SDIO_PowerState_OFF(hsd
->Instance
);
2645 * @brief Send Status info command.
2646 * @param hsd: pointer to SD handle
2647 * @param pSDstatus: Pointer to the buffer that will contain the SD card status
2648 * SD Status register)
2649 * @retval error state
2651 static uint32_t SD_SendSDStatus(SD_HandleTypeDef
*hsd
, uint32_t *pSDstatus
)
2653 SDIO_DataInitTypeDef config
;
2654 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2655 uint32_t tickstart
= HAL_GetTick();
2656 uint32_t count
= 0U;
2658 /* Check SD response */
2659 if((SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
2661 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
2664 /* Set block size for card if it is not equal to current block size for card */
2665 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, 64U);
2666 if(errorstate
!= HAL_SD_ERROR_NONE
)
2668 hsd
->ErrorCode
|= HAL_SD_ERROR_NONE
;
2673 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
2674 if(errorstate
!= HAL_SD_ERROR_NONE
)
2676 hsd
->ErrorCode
|= HAL_SD_ERROR_NONE
;
2680 /* Configure the SD DPSM (Data Path State Machine) */
2681 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
2682 config
.DataLength
= 64U;
2683 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_64B
;
2684 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_SDIO
;
2685 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
2686 config
.DPSM
= SDIO_DPSM_ENABLE
;
2687 SDIO_ConfigData(hsd
->Instance
, &config
);
2689 /* Send ACMD13 (SD_APP_STAUS) with argument as card's RCA */
2690 errorstate
= SDMMC_CmdStatusRegister(hsd
->Instance
);
2691 if(errorstate
!= HAL_SD_ERROR_NONE
)
2693 hsd
->ErrorCode
|= HAL_SD_ERROR_NONE
;
2697 /* Get status data */
2698 while(!__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
| SDIO_FLAG_DCRCFAIL
| SDIO_FLAG_DTIMEOUT
| SDIO_FLAG_DBCKEND
))
2700 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXFIFOHF
))
2702 for(count
= 0U; count
< 8U; count
++)
2704 *(pSDstatus
+ count
) = SDIO_ReadFIFO(hsd
->Instance
);
2710 if((HAL_GetTick() - tickstart
) >= SDMMC_DATATIMEOUT
)
2712 return HAL_SD_ERROR_TIMEOUT
;
2716 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DTIMEOUT
))
2718 return HAL_SD_ERROR_DATA_TIMEOUT
;
2720 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
))
2722 return HAL_SD_ERROR_DATA_CRC_FAIL
;
2724 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
))
2726 return HAL_SD_ERROR_RX_OVERRUN
;
2729 while ((__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXDAVL
)))
2731 *pSDstatus
= SDIO_ReadFIFO(hsd
->Instance
);
2734 if((HAL_GetTick() - tickstart
) >= SDMMC_DATATIMEOUT
)
2736 return HAL_SD_ERROR_TIMEOUT
;
2740 /* Clear all the static status flags*/
2741 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2743 return HAL_SD_ERROR_NONE
;
2747 * @brief Returns the current card's status.
2748 * @param hsd: Pointer to SD handle
2749 * @param pCardStatus: pointer to the buffer that will contain the SD card
2750 * status (Card Status register)
2751 * @retval error state
2753 static uint32_t SD_SendStatus(SD_HandleTypeDef
*hsd
, uint32_t *pCardStatus
)
2755 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2757 if(pCardStatus
== NULL
)
2759 return HAL_SD_ERROR_PARAM
;
2762 /* Send Status command */
2763 errorstate
= SDMMC_CmdSendStatus(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
2764 if(errorstate
!= HAL_OK
)
2769 /* Get SD card status */
2770 *pCardStatus
= SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
);
2772 return HAL_SD_ERROR_NONE
;
2776 * @brief Enables the SDIO wide bus mode.
2777 * @param hsd: pointer to SD handle
2778 * @retval error state
2780 static uint32_t SD_WideBus_Enable(SD_HandleTypeDef
*hsd
)
2782 uint32_t scr
[2U] = {0U, 0U};
2783 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2785 if((SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
2787 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
2790 /* Get SCR Register */
2791 errorstate
= SD_FindSCR(hsd
, scr
);
2792 if(errorstate
!= HAL_OK
)
2797 /* If requested card supports wide bus operation */
2798 if((scr
[1U] & SDMMC_WIDE_BUS_SUPPORT
) != SDMMC_ALLZERO
)
2800 /* Send CMD55 APP_CMD with argument as card's RCA.*/
2801 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
2802 if(errorstate
!= HAL_OK
)
2807 /* Send ACMD6 APP_CMD with argument as 2 for wide bus mode */
2808 errorstate
= SDMMC_CmdBusWidth(hsd
->Instance
, 2U);
2809 if(errorstate
!= HAL_OK
)
2814 return HAL_SD_ERROR_NONE
;
2818 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
2823 * @brief Disables the SDIO wide bus mode.
2824 * @param hsd: Pointer to SD handle
2825 * @retval error state
2827 static uint32_t SD_WideBus_Disable(SD_HandleTypeDef
*hsd
)
2829 uint32_t scr
[2U] = {0U, 0U};
2830 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2832 if((SDIO_GetResponse(hsd
->Instance
, SDIO_RESP1
) & SDMMC_CARD_LOCKED
) == SDMMC_CARD_LOCKED
)
2834 return HAL_SD_ERROR_LOCK_UNLOCK_FAILED
;
2837 /* Get SCR Register */
2838 errorstate
= SD_FindSCR(hsd
, scr
);
2839 if(errorstate
!= HAL_OK
)
2844 /* If requested card supports 1 bit mode operation */
2845 if((scr
[1U] & SDMMC_SINGLE_BUS_SUPPORT
) != SDMMC_ALLZERO
)
2847 /* Send CMD55 APP_CMD with argument as card's RCA */
2848 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)(hsd
->SdCard
.RelCardAdd
<< 16U));
2849 if(errorstate
!= HAL_OK
)
2854 /* Send ACMD6 APP_CMD with argument as 0 for single bus mode */
2855 errorstate
= SDMMC_CmdBusWidth(hsd
->Instance
, 0U);
2856 if(errorstate
!= HAL_OK
)
2861 return HAL_SD_ERROR_NONE
;
2865 return HAL_SD_ERROR_REQUEST_NOT_APPLICABLE
;
2871 * @brief Finds the SD card SCR register value.
2872 * @param hsd: Pointer to SD handle
2873 * @param pSCR: pointer to the buffer that will contain the SCR value
2874 * @retval error state
2876 static uint32_t SD_FindSCR(SD_HandleTypeDef
*hsd
, uint32_t *pSCR
)
2878 SDIO_DataInitTypeDef config
;
2879 uint32_t errorstate
= HAL_SD_ERROR_NONE
;
2880 uint32_t tickstart
= HAL_GetTick();
2881 uint32_t index
= 0U;
2882 uint32_t tempscr
[2U] = {0U, 0U};
2884 /* Set Block Size To 8 Bytes */
2885 errorstate
= SDMMC_CmdBlockLength(hsd
->Instance
, 8U);
2886 if(errorstate
!= HAL_OK
)
2891 /* Send CMD55 APP_CMD with argument as card's RCA */
2892 errorstate
= SDMMC_CmdAppCommand(hsd
->Instance
, (uint32_t)((hsd
->SdCard
.RelCardAdd
) << 16U));
2893 if(errorstate
!= HAL_OK
)
2898 config
.DataTimeOut
= SDMMC_DATATIMEOUT
;
2899 config
.DataLength
= 8U;
2900 config
.DataBlockSize
= SDIO_DATABLOCK_SIZE_8B
;
2901 config
.TransferDir
= SDIO_TRANSFER_DIR_TO_SDIO
;
2902 config
.TransferMode
= SDIO_TRANSFER_MODE_BLOCK
;
2903 config
.DPSM
= SDIO_DPSM_ENABLE
;
2904 SDIO_ConfigData(hsd
->Instance
, &config
);
2906 /* Send ACMD51 SD_APP_SEND_SCR with argument as 0 */
2907 errorstate
= SDMMC_CmdSendSCR(hsd
->Instance
);
2908 if(errorstate
!= HAL_OK
)
2913 while(!__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
| SDIO_FLAG_DCRCFAIL
| SDIO_FLAG_DTIMEOUT
| SDIO_FLAG_DBCKEND
))
2915 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXDAVL
))
2917 *(tempscr
+ index
) = SDIO_ReadFIFO(hsd
->Instance
);
2921 if((HAL_GetTick() - tickstart
) >= SDMMC_DATATIMEOUT
)
2923 return HAL_SD_ERROR_TIMEOUT
;
2927 if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DTIMEOUT
))
2929 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_DTIMEOUT
);
2931 return HAL_SD_ERROR_DATA_TIMEOUT
;
2933 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
))
2935 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_DCRCFAIL
);
2937 return HAL_SD_ERROR_DATA_CRC_FAIL
;
2939 else if(__HAL_SD_GET_FLAG(hsd
, SDIO_FLAG_RXOVERR
))
2941 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_FLAG_RXOVERR
);
2943 return HAL_SD_ERROR_RX_OVERRUN
;
2947 /* No error flag set */
2948 /* Clear all the static flags */
2949 __HAL_SD_CLEAR_FLAG(hsd
, SDIO_STATIC_FLAGS
);
2951 *(pSCR
+ 1U) = ((tempscr
[0U] & SDMMC_0TO7BITS
) << 24U) | ((tempscr
[0U] & SDMMC_8TO15BITS
) << 8U) |\
2952 ((tempscr
[0U] & SDMMC_16TO23BITS
) >> 8U) | ((tempscr
[0U] & SDMMC_24TO31BITS
) >> 24U);
2954 *(pSCR
) = ((tempscr
[1U] & SDMMC_0TO7BITS
) << 24U) | ((tempscr
[1U] & SDMMC_8TO15BITS
) << 8U) |\
2955 ((tempscr
[1U] & SDMMC_16TO23BITS
) >> 8U) | ((tempscr
[1U] & SDMMC_24TO31BITS
) >> 24U);
2958 return HAL_SD_ERROR_NONE
;
2962 * @brief Wrap up reading in non-blocking mode.
2963 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2964 * the configuration information.
2965 * @retval HAL status
2967 static HAL_StatusTypeDef
SD_Read_IT(SD_HandleTypeDef
*hsd
)
2969 uint32_t count
= 0U;
2972 tmp
= (uint32_t*)hsd
->pRxBuffPtr
;
2974 /* Read data from SDIO Rx FIFO */
2975 for(count
= 0U; count
< 8U; count
++)
2977 *(tmp
+ count
) = SDIO_ReadFIFO(hsd
->Instance
);
2980 hsd
->pRxBuffPtr
+= 8U;
2986 * @brief Wrap up writing in non-blocking mode.
2987 * @param hsd: pointer to a SD_HandleTypeDef structure that contains
2988 * the configuration information.
2989 * @retval HAL status
2991 static HAL_StatusTypeDef
SD_Write_IT(SD_HandleTypeDef
*hsd
)
2993 uint32_t count
= 0U;
2996 tmp
= (uint32_t*)hsd
->pTxBuffPtr
;
2998 /* Write data to SDIO Tx FIFO */
2999 for(count
= 0U; count
< 8U; count
++)
3001 SDIO_WriteFIFO(hsd
->Instance
, (tmp
+ count
));
3004 hsd
->pTxBuffPtr
+= 8U;
3013 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx || STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||
3014 STM32F401xC || STM32F401xE || STM32F411xE || STM32F446xx || STM32F469xx || STM32F479xx || STM32F412Zx || STM32F412Vx ||
3015 STM32F412Rx || STM32F412Cx || STM32F413xx || STM32F423xx */
3017 #endif /* HAL_SD_MODULE_ENABLED */
3027 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/