HAL SD How to Use

group SD_How_To_Use

How to use this driver

The HAL SD driver can be used as follows:

SD memory card system Specifications are available through the SD card association website at www.sdcard.org This driver is compliant with SD memory card specification version 6.0 and full compliant with previous version.

This HAL SD driver is a layered driver on top of the SDMMC core driver (allowing to handle SD and MMC memories). The HAL SD driver allows to interface with SD and uSD cards devices.

This driver implements a high level communication layer for read and write from/to this memory.

This driver provides different set of APIs that allows to :

  1. Initialize and de-initialize the logical SD object :

    • To initialize the SD Card, use the HAL_SD_Init() function to

      • to associate physical instance to logical object.

      • Enable the SDMMC peripheral Clock:

        • Either by calling the appropriate HAL RCC function HAL_RCC_SDMMCx_EnableClock

        • Either by Setting the USE_HAL_SD_CLK_ENABLE_MODEL to HAL_CLK_ENABLE_PERIPH_ONLY through stm32tnxx_hal_conf.h module, in this case the SDMMC peripheral clock must be automatically enabled by the HAL_SD_Init() .

    • To de-initiliaze the SD card, use the HAL_SD_DeInit() function that must turn off the SD Card and set the HAL SD handle state to RESET

  2. Set and Get SD configuration:

    • Configure the SD card, use the HAL_SD_SetConfig() to apply the SD card configuration process, the clock frequency must be less than 400Khz. then, this phase allows the card identification and Get all card information (CSD, CID, Card Status Register) to ensure that the card has been correctly identified and then, Apply the user parameters. In this stage, the SD card moved from the IDENTIFICATION STATE to TRANSFER STATE and put the globale state at the IDLE state. In this case, we can perform any SD card Erase, read and write operation.

    • To get the SD card configuration, use the HAL_SD_GetConfig() to retrieve the current SD configuration.

    • When the GPIO Pin detects a removal of the SD card from the bus, use the HAL_SD_NotifyCardRemoval() to apply the SDMMC default configuration and wait for an insertion of the SD card.

    • When the GPIO Pin detects an insertion of the SD card, use the HAL_SD_NotifyCardInsertion() to re-apply the user configuration.

    • According to the SD card specification (speed, size and class), the timeout values available through define can be used also by user to fill the configuration structure at the SetConfig call:

      • HAL_SD_DATA_MAX_TIMEOUT (0xFFFFFFFFU) : Max data timeout

      • HAL_SD_STOP_XFER_MAX_TIMEOUT (500U) : Max stop transfer timeout 500 ms

      • HAL_SD_ERASE_MAX_TIMEOUT (63000U) : Max erase Timeout 63 seconds It can modify the timeout process values:

      • For the data_timeout_cycle : Use HAL_SD_SetDataTimeout() to update it with the user definition

      • For the stopxfer_timeout_ms : Use HAL_SD_SetStopXferTimeout() to update it with the user definition

      • for the erase_timeout_ms : Use HAL_SD_SetEraseTimeout() to update it with the user definition

  3. Input and Output operations:

    • Blocking mode: Polling

      • Read from SD card in polling mode by using function HAL_SD_ReadBlocks() . This function supports only 512-bytes block length (the block size must be chosen as 512 bytes). Choose either one block read operation or multiple block read operation by adjusting blocks_nbr. After this, ensure that the transfer is done correctly. The check is done through HAL_SD_GetCardState() function of SD card state.

      • Write to SD card in polling mode by using function HAL_SD_WriteBlocks() . This function supports only 512-bytes block length (the block size must be chosen as 512 bytes). Choose either one block read operation or multiple block read operation by adjusting blocks_nbr. After this, ensure that the transfer is done correctly. The check is done through HAL_SD_GetCardState() function of the SD card state.

    • Non-Blocking mode: IT

      • Read from the SD card in Interrupt mode by using function HAL_SD_ReadBlocks_IT() . This function supports only 512-bytes block length (the block size must be chosen as 512 bytes). Choose either one block read operation or multiple block read operation by adjusting blocks_nbr. After this, ensure that the transfer is done correctly by using the Xfer complete callbacks to check the end of the process.

      • Write to SD card in Interrupt mode by using function HAL_SD_WriteBlocks_IT() . This function supports only 512-bytes block length (the block size must be chosen as 512 bytes). Choose either one block read operation or multiple block read operation by adjusting blocks_nbr. After this, ensure that the transfer is done correctly by using the Xfer complete callbacks to check the end of the process.

    • Non-Blocking mode: DMA in normal mode

      • Read from the SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA() . This function supports only 512-bytes block length (the block size must be chosen as 512 bytes). Choose either one block read operation or multiple block read operation by adjusting blocks_nbr. After this, ensure that the transfer is done correctly by checking the IT transfer process through the SD xfer interrupt event.

      • Write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA() . This function supports only 512-bytes block length (the block size must be chosen as 512 bytes). Choose either one block read operation or multiple block read operation by adjusting blocks_nbr. After this, ensure that the transfer is done correctly by using the Xfer complete callbacks to check the end of the process.

    • Non-Blocking mode: DMA in linked-list mode Use the Linked list feature by enabling the USE_HAL_SD_LINKEDLIST compilation flag through stm32tnxx_hal_conf.h module. At this stage, Build a linked-list Q using the module Q.

      • Read from the SD card in linked-list mode by using the function HAL_SD_ReadBlocks_LinkedList() . This function supports only 512-bytes block length (the block size must be chosen as 512 bytes). After this, ensure that the transfer is done correctly by using the Xfer complete callbacks to check the end of the process.

      • Write to the SD card in linked-list mode by using the function HAL_SD_WriteBlocks_LinkedList() . This function supports only 512-bytes block length (the block size must be chosen as 512 bytes). After this, ensure that the transfer is done correctly by using the Xfer complete callbacks to check the end of the process.

    • Abort operation

      • Abort any transfer to/from the SD card by using the HAL_SD_Abort() to stop the transfer in polling mode

      • Abort any transfer to/from the SD card by using the HAL_SD_Abort_IT() to stop the transfer in IT mode

  4. IRQHandler and Callbacks Functions:

  5. SD Card related information functions:

    • To get the globale global_state, use the HAL_SD_GetState() .

    • To get the internal state of the SD card, use the HAL_SD_GetCardState() .

    • To get SD card status use the function HAL_SD_GetCardStatus() to retrieve the status bits that are related to the SD memory card proprietary features.

    • To get SD card information, use the function HAL_SD_GetCardInfo() . It returns useful information about the SD card such as block number, block size type, logical block number, logical block size and the relative card address.

    • To switch the speed mode of the SD card, use the HAL_SD_SwitchSpeedMode() .

    • To retrieve the SD card specification version, use the HAL_SD_GetCardSpecVersion() .

    • To get the SD card identification register value, use the HAL_SD_GetCardCID() function.

    • To retrieve the last error code detecting by the SD card, Set the USE_HAL_SD_GET_LAST_ERRORS to 1U the compilation flag through stm32tnxx_hal_conf.h module to allow the user to get the last error code.

  6. SD Card Interrupt and flags Inline functions:

  7. SD card linked-list queue functions: This subsection provides a set of functions allowing to manage the SD linked-list node and queue:

  8. SD Delay Block functions: The delay block (DLYB) is used to generate an output clock that is dephased from the input clock.