CARME-M4 BSP  V1.5
stm32f4_sdio_sd.c File Reference

This file provides a set of functions needed to manage the SDIO SD Card memory mounted on STM32F4 Discovery board. More...

#include "stm32f4_sdio_sd.h"
+ Include dependency graph for stm32f4_sdio_sd.c:

Go to the source code of this file.

Macros

#define NULL   0
 SDIO Static flags, TimeOut, FIFO Address.
 
#define SDIO_STATIC_FLAGS   ((uint32_t)0x000005FF)
 
#define SDIO_CMD0TIMEOUT   ((uint32_t)0x00010000)
 
#define SD_OCR_ADDR_OUT_OF_RANGE   ((uint32_t)0x80000000)
 Mask for errors Card Status R1 (OCR Register)
 
#define SD_OCR_ADDR_MISALIGNED   ((uint32_t)0x40000000)
 
#define SD_OCR_BLOCK_LEN_ERR   ((uint32_t)0x20000000)
 
#define SD_OCR_ERASE_SEQ_ERR   ((uint32_t)0x10000000)
 
#define SD_OCR_BAD_ERASE_PARAM   ((uint32_t)0x08000000)
 
#define SD_OCR_WRITE_PROT_VIOLATION   ((uint32_t)0x04000000)
 
#define SD_OCR_LOCK_UNLOCK_FAILED   ((uint32_t)0x01000000)
 
#define SD_OCR_COM_CRC_FAILED   ((uint32_t)0x00800000)
 
#define SD_OCR_ILLEGAL_CMD   ((uint32_t)0x00400000)
 
#define SD_OCR_CARD_ECC_FAILED   ((uint32_t)0x00200000)
 
#define SD_OCR_CC_ERROR   ((uint32_t)0x00100000)
 
#define SD_OCR_GENERAL_UNKNOWN_ERROR   ((uint32_t)0x00080000)
 
#define SD_OCR_STREAM_READ_UNDERRUN   ((uint32_t)0x00040000)
 
#define SD_OCR_STREAM_WRITE_OVERRUN   ((uint32_t)0x00020000)
 
#define SD_OCR_CID_CSD_OVERWRIETE   ((uint32_t)0x00010000)
 
#define SD_OCR_WP_ERASE_SKIP   ((uint32_t)0x00008000)
 
#define SD_OCR_CARD_ECC_DISABLED   ((uint32_t)0x00004000)
 
#define SD_OCR_ERASE_RESET   ((uint32_t)0x00002000)
 
#define SD_OCR_AKE_SEQ_ERROR   ((uint32_t)0x00000008)
 
#define SD_OCR_ERRORBITS   ((uint32_t)0xFDFFE008)
 
#define SD_R6_GENERAL_UNKNOWN_ERROR   ((uint32_t)0x00002000)
 Masks for R6 Response.
 
#define SD_R6_ILLEGAL_CMD   ((uint32_t)0x00004000)
 
#define SD_R6_COM_CRC_FAILED   ((uint32_t)0x00008000)
 
#define SD_VOLTAGE_WINDOW_SD   ((uint32_t)0x80100000)
 
#define SD_HIGH_CAPACITY   ((uint32_t)0x40000000)
 
#define SD_STD_CAPACITY   ((uint32_t)0x00000000)
 
#define SD_CHECK_PATTERN   ((uint32_t)0x000001AA)
 
#define SD_MAX_VOLT_TRIAL   ((uint32_t)0x0000FFFF)
 
#define SD_ALLZERO   ((uint32_t)0x00000000)
 
#define SD_WIDE_BUS_SUPPORT   ((uint32_t)0x00040000)
 
#define SD_SINGLE_BUS_SUPPORT   ((uint32_t)0x00010000)
 
#define SD_CARD_LOCKED   ((uint32_t)0x02000000)
 
#define SD_DATATIMEOUT   ((uint32_t)0xFFFFFFFF)
 
#define SD_0TO7BITS   ((uint32_t)0x000000FF)
 
#define SD_8TO15BITS   ((uint32_t)0x0000FF00)
 
#define SD_16TO23BITS   ((uint32_t)0x00FF0000)
 
#define SD_24TO31BITS   ((uint32_t)0xFF000000)
 
#define SD_MAX_DATA_LENGTH   ((uint32_t)0x01FFFFFF)
 
#define SD_HALFFIFO   ((uint32_t)0x00000008)
 
#define SD_HALFFIFOBYTES   ((uint32_t)0x00000020)
 
#define SD_CCCC_LOCK_UNLOCK   ((uint32_t)0x00000080)
 Command Class Supported.
 
#define SD_CCCC_WRITE_PROT   ((uint32_t)0x00000040)
 
#define SD_CCCC_ERASE   ((uint32_t)0x00000020)
 
#define SDIO_SEND_IF_COND   ((uint32_t)0x00000008)
 Following commands are SD Card Specific commands. SDIO_APP_CMD should be sent before sending these commands.
 

Functions

static SD_Error CmdError (void)
 Checks for error conditions for CMD0. More...
 
static SD_Error CmdResp1Error (uint8_t cmd)
 Checks for error conditions for R1 response. More...
 
static SD_Error CmdResp7Error (void)
 Checks for error conditions for R7 response. More...
 
static SD_Error CmdResp3Error (void)
 Checks for error conditions for R3 (OCR) response. More...
 
static SD_Error CmdResp2Error (void)
 Checks for error conditions for R2 (CID or CSD) response. More...
 
static SD_Error CmdResp6Error (uint8_t cmd, uint16_t *prca)
 Checks for error conditions for R6 (RCA) response. More...
 
static SD_Error SDEnWideBus (FunctionalState NewState)
 Enables or disables the SDIO wide bus mode. More...
 
static SD_Error IsCardProgramming (uint8_t *pstatus)
 Checks if the SD card is in programming state. More...
 
static SD_Error FindSCR (uint16_t rca, uint32_t *pscr)
 Find the SD card SCR register value. More...
 
uint8_t convert_from_bytes_to_power_of_two (uint16_t NumberOfBytes)
 Converts the number of bytes in power of two and returns the power. More...
 
void SD_DeInit (void)
 DeInitializes the SDIO interface. More...
 
SD_Error SD_Init (void)
 Initializes the SD Card and put it into StandBy State (Ready for data transfer). More...
 
SDTransferState SD_GetStatus (void)
 Gets the cuurent sd card data transfer status. More...
 
SDCardState SD_GetState (void)
 Returns the current card's state. More...
 
uint8_t SD_Detect (void)
 Detect if SD card is correctly plugged in the memory slot. More...
 
SD_Error SD_PowerON (void)
 Enquires cards about their operating voltage and configures clock controls. More...
 
SD_Error SD_PowerOFF (void)
 Turns the SDIO output signals off. More...
 
SD_Error SD_InitializeCards (void)
 Intialises all cards or single card as the case may be Card(s) come into standby state. More...
 
SD_Error SD_GetCardInfo (SD_CardInfo *cardinfo)
 Returns information about specific card. More...
 
SD_Error SD_GetCardStatus (SD_CardStatus *cardstatus)
 Enables wide bus opeartion for the requeseted card if supported by card. More...
 
SD_Error SD_EnableWideBusOperation (uint32_t WideMode)
 Enables wide bus opeartion for the requeseted card if supported by card. More...
 
SD_Error SD_SelectDeselect (uint32_t addr)
 Selects od Deselects the corresponding card. More...
 
SD_Error SD_ReadBlock (uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)
 Allows to read one block from a specified address in a card. The Data transfer can be managed by DMA mode or Polling mode. More...
 
SD_Error SD_ReadMultiBlocks (uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
 Allows to read blocks from a specified address in a card. The Data transfer can be managed by DMA mode or Polling mode. More...
 
SD_Error SD_ReadMultiBlocksFIXED (uint8_t *readbuff, uint32_t ReadAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
 
SD_Error SD_WaitReadOperation (void)
 This function waits until the SDIO DMA data transfer is finished. This function should be called after SDIO_ReadMultiBlocks() function to insure that all data sent by the card are already transferred by the DMA controller. More...
 
SD_Error SD_WriteBlock (uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize)
 Allows to write one block starting from a specified address in a card. The Data transfer can be managed by DMA mode or Polling mode. More...
 
SD_Error SD_WriteMultiBlocks (uint8_t *writebuff, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
 Allows to write blocks starting from a specified address in a card. The Data transfer can be managed by DMA mode only. More...
 
SD_Error SD_WriteMultiBlocksFIXED (uint8_t *writebuff, uint32_t WriteAddr, uint32_t BlockSize, uint32_t NumberOfBlocks)
 
SD_Error SD_WaitWriteOperation (void)
 This function waits until the SDIO DMA data transfer is finished. This function should be called after SDIO_WriteBlock() and SDIO_WriteMultiBlocks() function to insure that all data sent by the card are already transferred by the DMA controller. More...
 
SDTransferState SD_GetTransferState (void)
 Gets the cuurent data transfer state. More...
 
SD_Error SD_StopTransfer (void)
 Aborts an ongoing data transfer. More...
 
SD_Error SD_Erase (uint32_t startaddr, uint32_t endaddr)
 Allows to erase memory area specified for the given card. More...
 
SD_Error SD_SendStatus (uint32_t *pcardstatus)
 Returns the current card's status. More...
 
SD_Error SD_SendSDStatus (uint32_t *psdstatus)
 Returns the current SD card's status. More...
 
SD_Error SD_ProcessIRQSrc (void)
 Allows to process all the interrupts that are high. More...
 
void SD_ProcessDMAIRQ (void)
 This function waits until the SDIO DMA data transfer is finished. More...
 

Variables

static uint32_t CardType = SDIO_STD_CAPACITY_SD_CARD_V1_1
 
static uint32_t CSD_Tab [4]
 
static uint32_t CID_Tab [4]
 
static uint32_t RCA = 0
 
static uint8_t SDSTATUS_Tab [16]
 
__IO uint32_t StopCondition = 0
 
__IO SD_Error TransferError = SD_OK
 
__IO uint32_t TransferEnd = 0
 
__IO uint32_t DMAEndOfTransfer = 0
 
SD_CardInfo SDCardInfo
 
SDIO_InitTypeDef SDIO_InitStructure
 
SDIO_CmdInitTypeDef SDIO_CmdInitStructure
 
SDIO_DataInitTypeDef SDIO_DataInitStructure
 

Detailed Description

This file provides a set of functions needed to manage the SDIO SD Card memory mounted on STM32F4 Discovery board.

Author
MCD Application Team
Version
V1.0.2
Date
05-March-2012
*
*          ===================================================================
*                                   How to use this driver
*          ===================================================================
*          It implements a high level communication layer for read and write
*          from/to this memory. The needed STM32 hardware resources (SDIO and
*          GPIO) are defined in stm32f4_discovery.h file, and the initialization is
*          performed in SD_LowLevel_Init() function declared in stm32f4_discovery.c
*          file.
*          You can easily tailor this driver to any other development board,
*          by just adapting the defines for hardware resources and
*          SD_LowLevel_Init() function.
*
*          A - SD Card Initialization and configuration
*          ============================================
*            - To initialize the SD Card, use the SD_Init() function.  It
*              Initializes the SD Card and put it into StandBy State (Ready
*              for data transfer). This function provide the following operations:
*
*              1 - 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
*                  "SDIO_INIT_CLK_DIV" define inside the stm32f4_discovery.h file.
*                  The SD Card frequency (SDIO_CK) is computed as follows:
*
*                     +---------------------------------------------+
*                     | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
*                     +---------------------------------------------+
*
*                  In initialization mode and according to the SD Card standard,
*                  make sure that the SDIO_CK frequency don't exceed 400KHz.
*
*              2 - 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.
*
*              3 - 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 "SDIO_TRANSFER_CLK_DIV"
*                  define inside the stm32f4_discovery.h file.
*                  The SD Card frequency (SDIO_CK) is computed as follows:
*
*                     +---------------------------------------------+
*                     | SDIO_CK = SDIOCLK / (SDIO_INIT_CLK_DIV + 2) |
*                     +---------------------------------------------+
*
*                  In transfer mode and according to the SD Card standard,
*                  make sure that the SDIO_CK frequency don't exceed 25MHz
*                  and 50MHz in High-speed mode switch.
*                  To be able to use a frequency higher than 24MHz, you should
*                  use the SDIO peripheral in bypass mode. Refer to the
*                  corresponding reference manual for more details.
*
*              4 -  Select the corresponding SD Card according to the address
*                   read with the step 2.
*
*              5 -  Configure the SD Card in wide bus mode: 4-bits data.
*
*          B - SD Card Read operation
*          ==========================
*           - You can read SD card by using two function: SD_ReadBlock() and
*             SD_ReadMultiBlocks() functions. These functions support only
*             512-byte block length.
*           - The SD_ReadBlock() function read only one block (512-byte). This
*             function can transfer the data using DMA controller or using
*             polling mode. To select between DMA or polling mode refer to
*             "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm32f4_discovery_sdio_sd.h
*             file and uncomment the corresponding line. By default the SD DMA
*             mode is selected
*           - The SD_ReadMultiBlocks() function read only mutli blocks (multiple
*             of 512-byte).
*           - Any read operation should be followed by two functions to check
*             if the DMA Controller and SD Card status.
*              - SD_ReadWaitOperation(): this function insure that the DMA
*                controller has finished all data transfer.
*              - SD_GetStatus(): to check that the SD Card has finished the
*                data transfer and it is ready for data.
*
*           - The DMA transfer is finished by the SDIO Data End interrupt.
*             User has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler()
*             and SD_ProcessDMAIRQ() function inside the DMA2_Streamx_IRQHandler().
*             Don't forget to enable the SDIO_IRQn and DMA2_Stream3_IRQn or
*             DMA2_Stream6_IRQn interrupts using the NVIC controller.
*
*          C - SD Card Write operation
*          ===========================
*           - You can write SD card by using two function: SD_WriteBlock() and
*             SD_WriteMultiBlocks() functions. These functions support only
*             512-byte block length.
*           - The SD_WriteBlock() function write only one block (512-byte). This
*             function can transfer the data using DMA controller or using
*             polling mode. To select between DMA or polling mode refer to
*             "SD_DMA_MODE" or "SD_POLLING_MODE" inside the stm32f4_discovery_sdio_sd.h
*             file and uncomment the corresponding line. By default the SD DMA
*             mode is selected
*           - The SD_WriteMultiBlocks() function write only mutli blocks (multiple
*             of 512-byte).
*           - Any write operation should be followed by two functions to check
*             if the DMA Controller and SD Card status.
*              - SD_ReadWaitOperation(): this function insure that the DMA
*                controller has finished all data transfer.
*              - SD_GetStatus(): to check that the SD Card has finished the
*                data transfer and it is ready for data.
*
*           - The DMA transfer is finished by the SDIO Data End interrupt.
*             User has to call the SD_ProcessIRQ() function inside the SDIO_IRQHandler()
*             and SD_ProcessDMAIRQ() function inside the DMA2_Streamx_IRQHandler().
*             Don't forget to enable the SDIO_IRQn and DMA2_Stream3_IRQn or
*             DMA2_Stream6_IRQn interrupts using the NVIC controller.
*
*
*          D - SD card status
*          ==================
*           - At any time, you can check the SD Card status and get the SD card
*             state by using the 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
*             SD_SendSDStatus() function.
*
*          E - Programming Model (Selecting DMA for SDIO data Transfer)
*          ============================================================
*             Status = SD_Init(); // Initialization Step as described in section A
*
*             // SDIO Interrupt ENABLE
*             NVIC_InitStructure.NVIC_IRQChannel = SDIO_IRQn;
*             NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
*             NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
*             NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
*             NVIC_Init(&NVIC_InitStructure);
*             // DMA2 STREAMx Interrupt ENABLE
*             NVIC_InitStructure.NVIC_IRQChannel = SD_SDIO_DMA_IRQn;
*             NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
*             NVIC_Init(&NVIC_InitStructure);
*
*             // Write operation as described in Section C
*             Status = SD_WriteBlock(buffer, address, 512);
*             Status = SD_WaitWriteOperation();
*             while(SD_GetStatus() != SD_TRANSFER_OK);
*
*             Status = SD_WriteMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
*             Status = SD_WaitWriteOperation();
*             while(SD_GetStatus() != SD_TRANSFER_OK);
*
*             // Read operation as described in Section B
*             Status = SD_ReadBlock(buffer, address, 512);
*             Status = SD_WaitReadOperation();
*             while(SD_GetStatus() != SD_TRANSFER_OK);
*
*             Status = SD_ReadMultiBlocks(buffer, address, 512, NUMBEROFBLOCKS);
*             Status = SD_WaitReadOperation();
*             while(SD_GetStatus() != SD_TRANSFER_OK);
*
*             - Add the SDIO and DMA2 StreamX (3 or 6) IRQ Handlers:
*                 void SDIO_IRQHandler(void)
*                 {
*                   SD_ProcessIRQ();
*                 }
*                 void SD_SDIO_DMA_IRQHANDLER(void)
*                 {
*                   SD_ProcessDMAIRQ();
*                 }
*
*          F - Programming Model (Selecting Polling for SDIO data Transfer)
*          ================================================================
*            //Only SD Card Single Block operation are managed.
*            Status = SD_Init(); // Initialization Step as described in section
*
*            // Write operation as described in Section C
*            Status = SD_WriteBlock(buffer, address, 512);
*
*            // Read operation as described in Section B
*            Status = SD_ReadBlock(buffer, address, 512);
*
*          STM32 SDIO Pin assignment
*          =========================
*          +-----------------------------------------------------------+
*          |                     Pin assignment                        |
*          +-----------------------------+---------------+-------------+
*          |  STM32 SDIO Pins            |     SD        |    Pin      |
*          +-----------------------------+---------------+-------------+
*          |      SDIO D2                |   D2          |    1        |
*          |      SDIO D3                |   D3          |    2        |
*          |      SDIO CMD               |   CMD         |    3        |
*          |                             |   VCC         |    4 (3.3 V)|
*          |      SDIO CLK               |   CLK         |    5        |
*          |                             |   GND         |    6 (0 V)  |
*          |      SDIO D0                |   D0          |    7        |
*          |      SDIO D1                |   D1          |    8        |
*          +-----------------------------+---------------+-------------+
*
*  
Attention

© COPYRIGHT 2012 STMicroelectronics

Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); You may not use this file except in compliance with the License. You may obtain a copy of the License at:

   http://www.st.com/software_license_agreement_liberty_v2

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

PATCHED BY CLIVE SOURC.nosp@m.ER32.nosp@m.@GMAI.nosp@m.L.CO.nosp@m.M TO SUPPORT MEDIA >4GB 21-JUN-2012

Definition in file stm32f4_sdio_sd.c.