STM32 Notes--SDIO (SD card reading)

Publisher:熙风细雨Latest update time:2018-12-26 Source: eefocusKeywords:STM32 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

To learn here, you must first understand the basic information of SD card:




The SD card protocol has too many parts and is quite complicated. The recommended document is: "Simplified_Physical_Layer_Spec.pdf". Its main complexity lies in the numerous commands. There are 64 commands in total. You can check the document for what each command represents.




Most of the time here you just need to be familiar with the functions and call the library directly, but you still need to read each function. There will be examples of the use of specific functions later, which will be supplemented at that time.


The SD card library provided by ARM is generally used in our code, but there is a problem in the library, SD_Error SD_ReadBlock() is missing code: (marked, can be directly replaced)


SD_Error SD_ReadBlock(uint8_t *readbuff, uint32_t ReadAddr, uint16_t BlockSize)

{

  SD_Error errorstatus = SD_OK;

#if defined (SD_POLLING_MODE) 

  uint32_t count = 0, *tempbuff = (uint32_t *)readbuff;

#endif

 

  TransferError = SD_OK;

  TransferEnd = 0; //Set the transfer end flag, set to 1 in the interrupt service

  StopCondition = 0; //How to use it?

  

  SDIO->DCTRL = 0x0;

 

  

  if (CardType == SDIO_HIGH_CAPACITY_SD_CARD)

  {

    BlockSize = 512;

    ReadAddr /= 512;

  }

  /*******************add, without this section, it is easy to get stuck in DMA detection****************************************/

  /* Set Block Size for Card,cmd16,

* If it is a sdsc card, it can be used to set the block size.

* If it is an SDHC card, the block size is 512 bytes and is not affected by cmd16 

*/

  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t) BlockSize;

  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_SET_BLOCKLEN;

  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;   //r1

  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;

  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;

  SDIO_SendCommand(&SDIO_CmdInitStructure);

 

  errorstatus = CmdResp1Error(SD_CMD_SET_BLOCKLEN);

 

  if (SD_OK != errorstatus)

  {

    return(errorstatus);

  }

 /*********************************************************************************/

  SDIO_DataInitStructure.SDIO_DataTimeOut = SD_DATATIMEOUT;

  SDIO_DataInitStructure.SDIO_DataLength = BlockSize;

  SDIO_DataInitStructure.SDIO_DataBlockSize = (uint32_t) 9 << 4;

  SDIO_DataInitStructure.SDIO_TransferDir = SDIO_TransferDir_ToSDIO;

  SDIO_DataInitStructure.SDIO_TransferMode = SDIO_TransferMode_Block;

  SDIO_DataInitStructure.SDIO_DPSM = SDIO_DPSM_Enable;

  SDIO_DataConfig(&SDIO_DataInitStructure);

 

  /*!< Send CMD17 READ_SINGLE_BLOCK */

  SDIO_CmdInitStructure.SDIO_Argument = (uint32_t)ReadAddr;

  SDIO_CmdInitStructure.SDIO_CmdIndex = SD_CMD_READ_SINGLE_BLOCK;

  SDIO_CmdInitStructure.SDIO_Response = SDIO_Response_Short;

  SDIO_CmdInitStructure.SDIO_Wait = SDIO_Wait_No;

  SDIO_CmdInitStructure.SDIO_CPSM = SDIO_CPSM_Enable;

  SDIO_SendCommand(&SDIO_CmdInitStructure);

 

  errorstatus = CmdResp1Error(SD_CMD_READ_SINGLE_BLOCK);

 

  if (errorstatus != SD_OK)

  {

    return(errorstatus);

  }

 

#if defined (SD_POLLING_MODE)  

  /*!< In case of single block transfer, no need of stop transfer at all.*/

  /*!< Polling mode */

  while (!(SDIO->STA &(SDIO_FLAG_RXOVERR | SDIO_FLAG_DCRCFAIL | SDIO_FLAG_DTIMEOUT | SDIO_FLAG_DBCKEND | SDIO_FLAG_STBITERR)))

  {

    if (SDIO_GetFlagStatus(SDIO_FLAG_RXFIFOHF) != RESET)

    {

      for (count = 0; count < 8; count++)

      {

        *(tempbuff + count) = SDIO_ReadData();

      }

      tempbuff += 8;

    }

  }

 

  if (SDIO_GetFlagStatus(SDIO_FLAG_DTIMEOUT) != RESET)

  {

    SDIO_ClearFlag(SDIO_FLAG_DTIMEOUT);

    errorstatus = SD_DATA_TIMEOUT;

    return(errorstatus);

  }

  else if (SDIO_GetFlagStatus(SDIO_FLAG_DCRCFAIL) != RESET)

  {

    SDIO_ClearFlag(SDIO_FLAG_DCRCFAIL);

    errorstatus = SD_DATA_CRC_FAIL;

    return(errorstatus);

  }

  else if (SDIO_GetFlagStatus(SDIO_FLAG_RXOVERR) != RESET)

  {

    SDIO_ClearFlag(SDIO_FLAG_RXOVERR);

    errorstatus = SD_RX_OVERRUN;

    return(errorstatus);

  }

  else if (SDIO_GetFlagStatus(SDIO_FLAG_STBITERR) != RESET)

  {

    SDIO_ClearFlag(SDIO_FLAG_STBITERR);

    errorstatus = SD_START_BIT_ERR;

    return(errorstatus);

  }

  while (SDIO_GetFlagStatus(SDIO_FLAG_RXDAVL) != RESET)

  {

    *tempbuff = SDIO_ReadData();

    tempbuff++;

  }

  

  /*!< Clear all the static flags */

  SDIO_ClearFlag(SDIO_STATIC_FLAGS);

 

#elif defined (SD_DMA_MODE)

    SDIO_ITConfig(SDIO_IT_DATAEND, ENABLE);

    SDIO_DMACmd(ENABLE);

    SD_DMA_RxConfig((uint32_t *)readbuff, BlockSize);

#endif

 

  return(errorstatus);

}

Keywords:STM32 Reference address:STM32 Notes--SDIO (SD card reading)

Previous article:Some thoughts and conclusions on the communication between STM32 and SD card
Next article:Summary of the process of writing serial port program for stm32f407

Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号