TUKS MCU Introductory course / TUKS-COURSE-THERMOMETER

Fork of TUKS-COURSE-TIMER by TUKS MCU Introductory course

Embed: (wiki syntax)

« Back to documentation index

stm32l4xx_hal_sd.c File Reference

stm32l4xx_hal_sd.c File Reference

SD card HAL module driver. This file provides firmware functions to manage the following functionalities of the Secure Digital (SD) peripheral: + Initialization and de-initialization functions + IO operation functions + Peripheral Control functions + Peripheral State functions. More...

Go to the source code of this file.

Functions

static HAL_SD_ErrorTypedef SD_Initialize_Cards (SD_HandleTypeDef *hsd)
 Initializes all cards or single card as the case may be Card(s) come into standby state.
static HAL_SD_ErrorTypedef SD_Select_Deselect (SD_HandleTypeDef *hsd, uint64_t addr)
 Selects or Deselects the corresponding card.
static HAL_SD_ErrorTypedef SD_PowerON (SD_HandleTypeDef *hsd)
 Enquires cards about their operating voltage and configures clock controls and stores SD information that will be needed in future in the SD handle.
static HAL_SD_ErrorTypedef SD_PowerOFF (SD_HandleTypeDef *hsd)
 Turns the SDMMC output signals off.
static HAL_SD_ErrorTypedef SD_SendStatus (SD_HandleTypeDef *hsd, uint32_t *pCardStatus)
 Returns the current card's status.
static HAL_SD_CardStateTypedef SD_GetState (SD_HandleTypeDef *hsd)
 Returns the SD current state.
static HAL_SD_ErrorTypedef SD_IsCardProgramming (SD_HandleTypeDef *hsd, uint8_t *pStatus)
 Checks if the SD card is in programming state.
static HAL_SD_ErrorTypedef SD_CmdError (SD_HandleTypeDef *hsd)
 Checks for error conditions for CMD0.
static HAL_SD_ErrorTypedef SD_CmdResp1Error (SD_HandleTypeDef *hsd, uint8_t SD_CMD)
 Checks for error conditions for R1 response.
static HAL_SD_ErrorTypedef SD_CmdResp7Error (SD_HandleTypeDef *hsd)
 Checks for error conditions for R7 response.
static HAL_SD_ErrorTypedef SD_CmdResp3Error (SD_HandleTypeDef *hsd)
 Checks for error conditions for R3 (OCR) response.
static HAL_SD_ErrorTypedef SD_CmdResp2Error (SD_HandleTypeDef *hsd)
 Checks for error conditions for R2 (CID or CSD) response.
static HAL_SD_ErrorTypedef SD_CmdResp6Error (SD_HandleTypeDef *hsd, uint8_t SD_CMD, uint16_t *pRCA)
 Checks for error conditions for R6 (RCA) response.
static HAL_SD_ErrorTypedef SD_WideBus_Enable (SD_HandleTypeDef *hsd)
 Enables the SDMMC wide bus mode.
static HAL_SD_ErrorTypedef SD_WideBus_Disable (SD_HandleTypeDef *hsd)
 Disables the SDMMC wide bus mode.
static HAL_SD_ErrorTypedef SD_FindSCR (SD_HandleTypeDef *hsd, uint32_t *pSCR)
 Finds the SD card SCR register value.
static void SD_DMA_RxCplt (DMA_HandleTypeDef *hdma)
 SD DMA transfer complete Rx callback.
static void SD_DMA_RxError (DMA_HandleTypeDef *hdma)
 SD DMA transfer Error Rx callback.
static void SD_DMA_TxCplt (DMA_HandleTypeDef *hdma)
 SD DMA transfer complete Tx callback.
static void SD_DMA_TxError (DMA_HandleTypeDef *hdma)
 SD DMA transfer Error Tx callback.
HAL_SD_ErrorTypedef HAL_SD_Init (SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *SDCardInfo)
 Initializes the SD card according to the specified parameters in the SD_HandleTypeDef and initialize the associated handle.
HAL_StatusTypeDef HAL_SD_DeInit (SD_HandleTypeDef *hsd)
 De-Initializes the SD card.
__weak void HAL_SD_MspInit (SD_HandleTypeDef *hsd)
 Initializes the SD MSP.
__weak void HAL_SD_MspDeInit (SD_HandleTypeDef *hsd)
 De-Initialize SD MSP.
HAL_SD_ErrorTypedef HAL_SD_ReadBlocks (SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
 Reads block(s) from a specified address in a card.
HAL_SD_ErrorTypedef HAL_SD_WriteBlocks (SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
 Allows to write block(s) to a specified address in a card.
HAL_SD_ErrorTypedef HAL_SD_ReadBlocks_DMA (SD_HandleTypeDef *hsd, uint32_t *pReadBuffer, uint64_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
 Reads block(s) from a specified address in a card.
HAL_SD_ErrorTypedef HAL_SD_WriteBlocks_DMA (SD_HandleTypeDef *hsd, uint32_t *pWriteBuffer, uint64_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
 Writes block(s) to a specified address in a card.
HAL_SD_ErrorTypedef HAL_SD_CheckReadOperation (SD_HandleTypeDef *hsd, uint32_t Timeout)
 This function waits until the SD DMA data read transfer is finished.
HAL_SD_ErrorTypedef HAL_SD_CheckWriteOperation (SD_HandleTypeDef *hsd, uint32_t Timeout)
 This function waits until the SD DMA data write transfer is finished.
HAL_SD_ErrorTypedef HAL_SD_Erase (SD_HandleTypeDef *hsd, uint64_t startaddr, uint64_t endaddr)
 Erases the specified memory area of the given SD card.
void HAL_SD_IRQHandler (SD_HandleTypeDef *hsd)
 This function handles SD card interrupt request.
__weak void HAL_SD_XferCpltCallback (SD_HandleTypeDef *hsd)
 SD end of transfer callback.
__weak void HAL_SD_XferErrorCallback (SD_HandleTypeDef *hsd)
 SD Transfer Error callback.
__weak void HAL_SD_DMA_RxCpltCallback (DMA_HandleTypeDef *hdma)
 SD Transfer complete Rx callback in non-blocking mode.
__weak void HAL_SD_DMA_RxErrorCallback (DMA_HandleTypeDef *hdma)
 SD DMA transfer complete Rx error callback.
__weak void HAL_SD_DMA_TxCpltCallback (DMA_HandleTypeDef *hdma)
 SD Transfer complete Tx callback in non-blocking mode.
__weak void HAL_SD_DMA_TxErrorCallback (DMA_HandleTypeDef *hdma)
 SD DMA transfer complete error Tx callback.
HAL_SD_ErrorTypedef HAL_SD_Get_CardInfo (SD_HandleTypeDef *hsd, HAL_SD_CardInfoTypedef *pCardInfo)
 Returns information about specific card.
HAL_SD_ErrorTypedef HAL_SD_WideBusOperation_Config (SD_HandleTypeDef *hsd, uint32_t WideMode)
 Enables wide bus operation for the requested card if supported by card.
HAL_SD_ErrorTypedef HAL_SD_StopTransfer (SD_HandleTypeDef *hsd)
 Aborts an ongoing data transfer.
HAL_SD_ErrorTypedef HAL_SD_HighSpeed (SD_HandleTypeDef *hsd)
 Switches the SD card to High Speed mode.
HAL_SD_ErrorTypedef HAL_SD_SendSDStatus (SD_HandleTypeDef *hsd, uint32_t *pSDstatus)
 Returns the current SD card's status.
HAL_SD_TransferStateTypedef HAL_SD_GetStatus (SD_HandleTypeDef *hsd)
 Gets the current sd card data status.
HAL_SD_ErrorTypedef HAL_SD_GetCardStatus (SD_HandleTypeDef *hsd, HAL_SD_CardStatusTypedef *pCardStatus)
 Gets the SD card status.

Detailed Description

SD card HAL module driver. This file provides firmware functions to manage the following functionalities of the Secure Digital (SD) peripheral: + Initialization and de-initialization functions + IO operation functions + Peripheral Control functions + Peripheral State functions.

Author:
MCD Application Team
Version:
V1.5.1
Date:
31-May-2016
  ==============================================================================
                        ##### How to use this driver #####
  ==============================================================================
  [..]
    This driver implements a high level communication layer for read and write from/to 
    this memory. The needed STM32 hardware resources (SDMMC1 and GPIO) are performed by 
    the user in HAL_SD_MspInit() function (MSP layer).                             
    Basically, the MSP layer configuration should be the same as we provide in the 
    examples.
    You can easily tailor this configuration according to hardware resources.

  [..]
    This driver is a generic layered driver for SDMMC memories which uses the HAL 
    SDMMC driver functions to interface with SD and uSD cards devices. 
    It is used as follows:
 
    (#)Initialize the SDMMC1 low level resources by implementing the HAL_SD_MspInit() API:
        (##) Call the function HAL_RCCEx_PeriphCLKConfig with RCC_PERIPHCLK_SDMMC1 for
        PeriphClockSelection and select SDMMC1 clock source (MSI, main PLL or PLLSAI1)
        (##) Enable the SDMMC1 interface clock using __HAL_RCC_SDMMC1_CLK_ENABLE(); 
        (##) SDMMC pins configuration for SD card
            (+++) Enable the clock for the SDMMC GPIOs using the functions __HAL_RCC_GPIOx_CLK_ENABLE();   
            (+++) Configure these SDMMC pins as alternate function pull-up using HAL_GPIO_Init()
                  and according to your pin assignment;
        (##) DMA Configuration if you need to use DMA process (HAL_SD_ReadBlocks_DMA()
             and HAL_SD_WriteBlocks_DMA() APIs).
            (+++) Enable the DMAx interface clock using __HAL_RCC_DMAx_CLK_ENABLE(); 
            (+++) Configure the DMA using the function HAL_DMA_Init() with predeclared and filled. 
        (##) NVIC configuration if you need to use interrupt process when using DMA transfer.
            (+++) Configure the SDMMC and DMA interrupt priorities using functions
                  HAL_NVIC_SetPriority(); DMA priority is superior to SDMMC's priority
            (+++) Enable the NVIC DMA and SDMMC IRQs using function HAL_NVIC_EnableIRQ()
            (+++) SDMMC interrupts are managed using the macros __HAL_SD_SDMMC_ENABLE_IT() 
                  and __HAL_SD_SDMMC_DISABLE_IT() inside the communication process.
            (+++) SDMMC interrupts pending bits are managed using the macros __HAL_SD_SDMMC_GET_IT()
                  and __HAL_SD_SDMMC_CLEAR_IT()
    (#) At this stage, you can perform SD read/write/erase operations after SD card initialization  

         
  *** SD Card Initialization and configuration ***
  ================================================    
  [..]
    To initialize the SD Card, use the HAL_SD_Init() function.  It Initializes 
    the SD Card and put it into StandBy State (Ready for data transfer). 
    This function provide the following operations:
  
    (#) Apply the SD Card initialization process at 400KHz and check the SD Card 
        type (Standard Capacity or High Capacity). You can change or adapt this 
        frequency by adjusting the "ClockDiv" field. 
        The SD Card frequency (SDMMC_CK) is computed as follows:
     (++)

           SDMMC_CK = SDMMCCLK / (ClockDiv + 2)

     -@@-  In initialization mode and according to the SD Card standard, 
          make sure that the SDMMC_CK frequency doesn't exceed 400KHz.
  
    (#) Get the SD CID and CSD data. All these information are managed by the SDCardInfo 
        structure. This structure provide also ready computed SD Card capacity 
        and Block size.
        
        -@- These information are stored in SD handle structure in case of future use.  
  
    (#) Configure the SD Card Data transfer frequency. By Default, the card transfer 
        frequency is set to 24MHz. You can change or adapt this frequency by adjusting 
        the "ClockDiv" field.
        In transfer mode and according to the SD Card standard, make sure that the 
        SDMMC_CK frequency doesn't exceed 25MHz and 50MHz in High-speed mode switch.
        To be able to use a frequency higher than 24MHz, you should use the SDMMC 
        peripheral in bypass mode. Refer to the corresponding reference manual 
        for more details.
  
    (#) Select the corresponding SD Card according to the address read with the step 2.
    
    (#) Configure the SD Card in wide bus mode: 4-bits data.
  
  *** SD Card Read operation ***
  ==============================
  [..] 
    (+) You can read from SD card in polling mode by using function HAL_SD_ReadBlocks(). 
        This function support only 512-bytes block length (the block size should be 
        chosen as 512 bytes).
        You can choose either one block read operation or multiple block read operation 
        by adjusting the "NumberOfBlocks" parameter.

    (+) You can read from SD card in DMA mode by using function HAL_SD_ReadBlocks_DMA().
        This function support only 512-bytes block length (the block size should be 
        chosen as 512 bytes).
        You can choose either one block read operation or multiple block read operation 
        by adjusting the "NumberOfBlocks" parameter.
        After this, you have to call the function HAL_SD_CheckReadOperation(), to insure
        that the read transfer is done correctly in both DMA and SD sides.
  
  *** SD Card Write operation ***
  =============================== 
  [..] 
    (+) You can write to SD card in polling mode by using function HAL_SD_WriteBlocks(). 
        This function support only 512-bytes block length (the block size should be 
        chosen as 512 bytes).
        You can choose either one block read operation or multiple block read operation 
        by adjusting the "NumberOfBlocks" parameter.

    (+) You can write to SD card in DMA mode by using function HAL_SD_WriteBlocks_DMA().
        This function support only 512-bytes block length (the block size should be 
        chosen as 512 byte).
        You can choose either one block read operation or multiple block read operation 
        by adjusting the "NumberOfBlocks" parameter.
        After this, you have to call the function HAL_SD_CheckWriteOperation(), to insure
        that the write transfer is done correctly in both DMA and SD sides.  
  
  *** SD card status ***
  ====================== 
  [..]
    (+) At any time, you can check the SD Card status and get the SD card state 
        by using the HAL_SD_GetStatus() function. This function checks first if the 
        SD card is still connected and then get the internal SD Card transfer state.     
    (+) You can also get the SD card SD Status register by using the HAL_SD_SendSDStatus() 
        function.    

  *** SD HAL driver macros list ***
  ==================================
  [..]
    Below the list of most used macros in SD HAL driver.
       
    (+) __HAL_SD_SDMMC_ENABLE : Enable the SD device
    (+) __HAL_SD_SDMMC_DISABLE : Disable the SD device
    (+) __HAL_SD_SDMMC_DMA_ENABLE: Enable the SDMMC DMA transfer
    (+) __HAL_SD_SDMMC_DMA_DISABLE: Disable the SDMMC DMA transfer
    (+) __HAL_SD_SDMMC_ENABLE_IT: Enable the SD device interrupt
    (+) __HAL_SD_SDMMC_DISABLE_IT: Disable the SD device interrupt
    (+) __HAL_SD_SDMMC_GET_FLAG:Check whether the specified SD flag is set or not
    (+) __HAL_SD_SDMMC_CLEAR_FLAG: Clear the SD's pending flags
    [..]  
    (@) You can refer to the SD HAL driver header file for more useful macros 
      
  
Attention:

© COPYRIGHT(c) 2016 STMicroelectronics

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of STMicroelectronics nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

Definition in file stm32l4xx_hal_sd.c.