stm32_eval_spi_sd.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32_eval_spi_sd.c
00004   * @author  MCD Application Team
00005   * @version V4.3.0
00006   * @date    10/15/2010
00007   * @brief   This file provides a set of functions needed to manage the SPI SD 
00008   *          Card memory mounted on STM32xx-EVAL board (refer to stm32_eval.h
00009   *          to know about the boards supporting this memory). 
00010   *          It implements a high level communication layer for read and write 
00011   *          from/to this memory. The needed STM32 hardware resources (SPI and 
00012   *          GPIO) are defined in stm32xx_eval.h file, and the initialization is 
00013   *          performed in SD_LowLevel_Init() function declared in stm32xx_eval.c 
00014   *          file.
00015   *          You can easily tailor this driver to any other development board, 
00016   *          by just adapting the defines for hardware resources and 
00017   *          SD_LowLevel_Init() function.
00018   *            
00019   *          +-------------------------------------------------------+
00020   *          |                     Pin assignment                    |
00021   *          +-------------------------+---------------+-------------+
00022   *          |  STM32 SPI Pins         |     SD        |    Pin      |
00023   *          +-------------------------+---------------+-------------+
00024   *          | SD_SPI_CS_PIN           |   ChipSelect  |    1        |
00025   *          | SD_SPI_MOSI_PIN / MOSI  |   DataIn      |    2        |
00026   *          |                         |   GND         |    3 (0 V)  |
00027   *          |                         |   VDD         |    4 (3.3 V)|
00028   *          | SD_SPI_SCK_PIN / SCLK   |   Clock       |    5        |
00029   *          |                         |   GND         |    6 (0 V)  |
00030   *          | SD_SPI_MISO_PIN / MISO  |   DataOut     |    7        |
00031   *          +-------------------------+---------------+-------------+
00032   ******************************************************************************
00033   * @copy
00034   *
00035   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
00036   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
00037   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
00038   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
00039   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
00040   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
00041   *
00042   * <h2><center>&copy; COPYRIGHT 2010 STMicroelectronics</center></h2>
00043   */
00044 
00045 /* Includes ------------------------------------------------------------------*/
00046 #include "stm32_eval_spi_sd.h"
00047 
00048 /** @addtogroup Utilities
00049   * @{
00050   */
00051   
00052 /** @addtogroup STM32_EVAL
00053   * @{
00054   */ 
00055 
00056 /** @addtogroup Common
00057   * @{
00058   */
00059   
00060 /** @addtogroup STM32_EVAL_SPI_SD
00061   * @brief      This file includes the SD card driver of STM32-EVAL boards.
00062   * @{
00063   */ 
00064 
00065 /** @defgroup STM32_EVAL_SPI_SD_Private_Types
00066   * @{
00067   */ 
00068 /**
00069   * @}
00070   */ 
00071 
00072 
00073 /** @defgroup STM32_EVAL_SPI_SD_Private_Defines
00074   * @{
00075   */ 
00076 /**
00077   * @}
00078   */ 
00079 
00080 /** @defgroup STM32_EVAL_SPI_SD_Private_Macros
00081   * @{
00082   */
00083 /**
00084   * @}
00085   */ 
00086   
00087 
00088 /** @defgroup STM32_EVAL_SPI_SD_Private_Variables
00089   * @{
00090   */ 
00091 /**
00092   * @}
00093   */ 
00094 
00095 
00096 /** @defgroup STM32_EVAL_SPI_SD_Private_Function_Prototypes
00097   * @{
00098   */
00099 /**
00100   * @}
00101   */ 
00102 
00103 
00104 /** @defgroup STM32_EVAL_SPI_SD_Private_Functions
00105   * @{
00106   */ 
00107 
00108 /**
00109   * @brief  DeInitializes the SD/SD communication.
00110   * @param  None
00111   * @retval None
00112   */
00113 void SD_DeInit(void)
00114 {
00115   SD_LowLevel_DeInit();
00116 }
00117 
00118 /**
00119   * @brief  Initializes the SD/SD communication.
00120   * @param  None
00121   * @retval The SD Response: 
00122   *         - SD_RESPONSE_FAILURE: Sequence failed
00123   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00124   */
00125 SD_Error SD_Init(void)
00126 {
00127   uint32_t i = 0;
00128 
00129   /*!< Initialize SD_SPI */
00130   SD_LowLevel_Init(); 
00131 
00132   /*!< SD chip select high */
00133   SD_CS_HIGH();
00134 
00135   /*!< Send dummy byte 0xFF, 10 times with CS high */
00136   /*!< Rise CS and MOSI for 80 clocks cycles */
00137   for (i = 0; i <= 9; i++)
00138   {
00139     /*!< Send dummy byte 0xFF */
00140     SD_WriteByte(SD_DUMMY_BYTE);
00141   }
00142   /*------------Put SD in SPI mode--------------*/
00143   /*!< SD initialized and set to SPI mode properly */
00144   return (SD_GoIdleState());
00145 }
00146 
00147 /**
00148  * @brief  Detect if SD card is correctly plugged in the memory slot.
00149  * @param  None
00150  * @retval Return if SD is detected or not
00151  */
00152 uint8_t SD_Detect(void)
00153 {
00154   __IO uint8_t status = SD_PRESENT;
00155 
00156   /*!< Check GPIO to detect SD */
00157   if (GPIO_ReadInputData(SD_DETECT_GPIO_PORT) & SD_DETECT_PIN)
00158   {
00159     status = SD_NOT_PRESENT;
00160   }
00161   return status;
00162 }
00163 
00164 /**
00165   * @brief  Returns information about specific card.
00166   * @param  cardinfo: pointer to a SD_CardInfo structure that contains all SD 
00167   *         card information.
00168   * @retval The SD Response:
00169   *         - SD_RESPONSE_FAILURE: Sequence failed
00170   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00171   */
00172 SD_Error SD_GetCardInfo(SD_CardInfo *cardinfo)
00173 {
00174   SD_Error status = SD_RESPONSE_FAILURE;
00175 
00176   status = SD_GetCSDRegister(&(cardinfo->SD_csd));
00177   status = SD_GetCIDRegister(&(cardinfo->SD_cid));
00178   cardinfo->CardCapacity = (cardinfo->SD_csd.DeviceSize + 1) ;
00179   cardinfo->CardCapacity *= (1 << (cardinfo->SD_csd.DeviceSizeMul + 2));
00180   cardinfo->CardBlockSize = 1 << (cardinfo->SD_csd.RdBlockLen);
00181   cardinfo->CardCapacity *= cardinfo->CardBlockSize;
00182 
00183   /*!< Returns the reponse */
00184   return status;
00185 }
00186 
00187 /**
00188   * @brief  Reads a block of data from the SD.
00189   * @param  pBuffer: pointer to the buffer that receives the data read from the 
00190   *                  SD.
00191   * @param  ReadAddr: SD's internal address to read from.
00192   * @param  BlockSize: the SD card Data block size.
00193   * @retval The SD Response:
00194   *         - SD_RESPONSE_FAILURE: Sequence failed
00195   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00196   */
00197 SD_Error SD_ReadBlock(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize)
00198 {
00199   uint32_t i = 0;
00200   SD_Error rvalue = SD_RESPONSE_FAILURE;
00201 
00202   /*!< SD chip select low */
00203   SD_CS_LOW();
00204   
00205   /*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
00206   SD_SendCmd(SD_CMD_READ_SINGLE_BLOCK, ReadAddr, 0xFF);
00207   
00208   /*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
00209   if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
00210   {
00211     /*!< Now look for the data token to signify the start of the data */
00212     if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
00213     {
00214       /*!< Read the SD block data : read NumByteToRead data */
00215       for (i = 0; i < BlockSize; i++)
00216       {
00217         /*!< Save the received data */
00218         *pBuffer = SD_ReadByte();
00219        
00220         /*!< Point to the next location where the byte read will be saved */
00221         pBuffer++;
00222       }
00223       /*!< Get CRC bytes (not really needed by us, but required by SD) */
00224       SD_ReadByte();
00225       SD_ReadByte();
00226       /*!< Set response value to success */
00227       rvalue = SD_RESPONSE_NO_ERROR;
00228     }
00229   }
00230   /*!< SD chip select high */
00231   SD_CS_HIGH();
00232   
00233   /*!< Send dummy byte: 8 Clock pulses of delay */
00234   SD_WriteByte(SD_DUMMY_BYTE);
00235   
00236   /*!< Returns the reponse */
00237   return rvalue;
00238 }
00239 
00240 /**
00241   * @brief  Reads multiple block of data from the SD.
00242   * @param  pBuffer: pointer to the buffer that receives the data read from the 
00243   *                  SD.
00244   * @param  ReadAddr: SD's internal address to read from.
00245   * @param  BlockSize: the SD card Data block size.
00246   * @param  NumberOfBlocks: number of blocks to be read.
00247   * @retval The SD Response:
00248   *         - SD_RESPONSE_FAILURE: Sequence failed
00249   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00250   */
00251 SD_Error SD_ReadMultiBlocks(uint8_t* pBuffer, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
00252 {
00253   uint32_t i = 0, Offset = 0;
00254   SD_Error rvalue = SD_RESPONSE_FAILURE;
00255   
00256   /*!< SD chip select low */
00257   SD_CS_LOW();
00258   /*!< Data transfer */
00259   while (NumberOfBlocks--)
00260   {
00261     /*!< Send CMD17 (SD_CMD_READ_SINGLE_BLOCK) to read one block */
00262     SD_SendCmd (SD_CMD_READ_SINGLE_BLOCK, ReadAddr + Offset, 0xFF);
00263     /*!< Check if the SD acknowledged the read block command: R1 response (0x00: no errors) */
00264     if (SD_GetResponse(SD_RESPONSE_NO_ERROR))
00265     {
00266       return  SD_RESPONSE_FAILURE;
00267     }
00268     /*!< Now look for the data token to signify the start of the data */
00269     if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
00270     {
00271       /*!< Read the SD block data : read NumByteToRead data */
00272       for (i = 0; i < BlockSize; i++)
00273       {
00274         /*!< Read the pointed data */
00275         *pBuffer = SD_ReadByte();
00276         /*!< Point to the next location where the byte read will be saved */
00277         pBuffer++;
00278       }
00279       /*!< Set next read address*/
00280       Offset += 512;
00281       /*!< get CRC bytes (not really needed by us, but required by SD) */
00282       SD_ReadByte();
00283       SD_ReadByte();
00284       /*!< Set response value to success */
00285       rvalue = SD_RESPONSE_NO_ERROR;
00286     }
00287     else
00288     {
00289       /*!< Set response value to failure */
00290       rvalue = SD_RESPONSE_FAILURE;
00291     }
00292   }
00293   /*!< SD chip select high */
00294   SD_CS_HIGH();
00295   /*!< Send dummy byte: 8 Clock pulses of delay */
00296   SD_WriteByte(SD_DUMMY_BYTE);
00297   /*!< Returns the reponse */
00298   return rvalue;
00299 }
00300 
00301 /**
00302   * @brief  Writes a block on the SD
00303   * @param  pBuffer: pointer to the buffer containing the data to be written on 
00304   *                  the SD.
00305   * @param  WriteAddr: address to write on.
00306   * @param  BlockSize: the SD card Data block size.
00307   * @retval The SD Response: 
00308   *         - SD_RESPONSE_FAILURE: Sequence failed
00309   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00310   */
00311 SD_Error SD_WriteBlock(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize)
00312 {
00313   uint32_t i = 0;
00314   SD_Error rvalue = SD_RESPONSE_FAILURE;
00315 
00316   /*!< SD chip select low */
00317   SD_CS_LOW();
00318 
00319   /*!< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write multiple block */
00320   SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr, 0xFF);
00321   
00322   /*!< Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
00323   if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
00324   {
00325     /*!< Send a dummy byte */
00326     SD_WriteByte(SD_DUMMY_BYTE);
00327 
00328     /*!< Send the data token to signify the start of the data */
00329     SD_WriteByte(0xFE);
00330 
00331     /*!< Write the block data to SD : write count data by block */
00332     for (i = 0; i < BlockSize; i++)
00333     {
00334       /*!< Send the pointed byte */
00335       SD_WriteByte(*pBuffer);
00336       /*!< Point to the next location where the byte read will be saved */
00337       pBuffer++;
00338     }
00339     /*!< Put CRC bytes (not really needed by us, but required by SD) */
00340     SD_ReadByte();
00341     SD_ReadByte();
00342 
00343     /*!< Read data response */
00344     if (SD_GetDataResponse() == SD_DATA_OK)
00345     {
00346       rvalue = SD_RESPONSE_NO_ERROR;
00347     }
00348   }
00349   /*!< SD chip select high */
00350   SD_CS_HIGH();
00351   /*!< Send dummy byte: 8 Clock pulses of delay */
00352   SD_WriteByte(SD_DUMMY_BYTE);
00353 
00354   /*!< Returns the reponse */
00355   return rvalue;
00356 }
00357 
00358 /**
00359   * @brief  Writes many blocks on the SD
00360   * @param  pBuffer: pointer to the buffer containing the data to be written on 
00361   *                  the SD.
00362   * @param  WriteAddr: address to write on.
00363   * @param  BlockSize: the SD card Data block size.
00364   * @param  NumberOfBlocks: number of blocks to be written.
00365   * @retval The SD Response: 
00366   *         - SD_RESPONSE_FAILURE: Sequence failed
00367   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00368   */
00369 SD_Error SD_WriteMultiBlocks(uint8_t* pBuffer, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks)
00370 {
00371   uint32_t i = 0, Offset = 0;
00372   SD_Error rvalue = SD_RESPONSE_FAILURE;
00373 
00374   /*!< SD chip select low */
00375   SD_CS_LOW();
00376   /*!< Data transfer */
00377   while (NumberOfBlocks--)
00378   {
00379     /*!< Send CMD24 (SD_CMD_WRITE_SINGLE_BLOCK) to write blocks */
00380     SD_SendCmd(SD_CMD_WRITE_SINGLE_BLOCK, WriteAddr + Offset, 0xFF);
00381     /*!< Check if the SD acknowledged the write block command: R1 response (0x00: no errors) */
00382     if (SD_GetResponse(SD_RESPONSE_NO_ERROR))
00383     {
00384       return SD_RESPONSE_FAILURE;
00385     }
00386     /*!< Send dummy byte */
00387     SD_WriteByte(SD_DUMMY_BYTE);
00388     /*!< Send the data token to signify the start of the data */
00389     SD_WriteByte(SD_START_DATA_SINGLE_BLOCK_WRITE);
00390     /*!< Write the block data to SD : write count data by block */
00391     for (i = 0; i < BlockSize; i++)
00392     {
00393       /*!< Send the pointed byte */
00394       SD_WriteByte(*pBuffer);
00395       /*!< Point to the next location where the byte read will be saved */
00396       pBuffer++;
00397     }
00398     /*!< Set next write address */
00399     Offset += 512;
00400     /*!< Put CRC bytes (not really needed by us, but required by SD) */
00401     SD_ReadByte();
00402     SD_ReadByte();
00403     /*!< Read data response */
00404     if (SD_GetDataResponse() == SD_DATA_OK)
00405     {
00406       /*!< Set response value to success */
00407       rvalue = SD_RESPONSE_NO_ERROR;
00408     }
00409     else
00410     {
00411       /*!< Set response value to failure */
00412       rvalue = SD_RESPONSE_FAILURE;
00413     }
00414   }
00415   /*!< SD chip select high */
00416   SD_CS_HIGH();
00417   /*!< Send dummy byte: 8 Clock pulses of delay */
00418   SD_WriteByte(SD_DUMMY_BYTE);
00419   /*!< Returns the reponse */
00420   return rvalue;
00421 }
00422 
00423 /**
00424   * @brief  Read the CSD card register.
00425   *         Reading the contents of the CSD register in SPI mode is a simple 
00426   *         read-block transaction.
00427   * @param  SD_csd: pointer on an SCD register structure
00428   * @retval The SD Response: 
00429   *         - SD_RESPONSE_FAILURE: Sequence failed
00430   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00431   */
00432 SD_Error SD_GetCSDRegister(SD_CSD* SD_csd)
00433 {
00434   uint32_t i = 0;
00435   SD_Error rvalue = SD_RESPONSE_FAILURE;
00436   uint8_t CSD_Tab[16];
00437 
00438   /*!< SD chip select low */
00439   SD_CS_LOW();
00440   /*!< Send CMD9 (CSD register) or CMD10(CSD register) */
00441   SD_SendCmd(SD_CMD_SEND_CSD, 0, 0xFF);
00442   /*!< Wait for response in the R1 format (0x00 is no errors) */
00443   if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
00444   {
00445     if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
00446     {
00447       for (i = 0; i < 16; i++)
00448       {
00449         /*!< Store CSD register value on CSD_Tab */
00450         CSD_Tab[i] = SD_ReadByte();
00451       }
00452     }
00453     /*!< Get CRC bytes (not really needed by us, but required by SD) */
00454     SD_WriteByte(SD_DUMMY_BYTE);
00455     SD_WriteByte(SD_DUMMY_BYTE);
00456     /*!< Set response value to success */
00457     rvalue = SD_RESPONSE_NO_ERROR;
00458   }
00459   /*!< SD chip select high */
00460   SD_CS_HIGH();
00461   /*!< Send dummy byte: 8 Clock pulses of delay */
00462   SD_WriteByte(SD_DUMMY_BYTE);
00463 
00464   /*!< Byte 0 */
00465   SD_csd->CSDStruct = (CSD_Tab[0] & 0xC0) >> 6;
00466   SD_csd->SysSpecVersion = (CSD_Tab[0] & 0x3C) >> 2;
00467   SD_csd->Reserved1 = CSD_Tab[0] & 0x03;
00468 
00469   /*!< Byte 1 */
00470   SD_csd->TAAC = CSD_Tab[1];
00471 
00472   /*!< Byte 2 */
00473   SD_csd->NSAC = CSD_Tab[2];
00474 
00475   /*!< Byte 3 */
00476   SD_csd->MaxBusClkFrec = CSD_Tab[3];
00477 
00478   /*!< Byte 4 */
00479   SD_csd->CardComdClasses = CSD_Tab[4] << 4;
00480 
00481   /*!< Byte 5 */
00482   SD_csd->CardComdClasses |= (CSD_Tab[5] & 0xF0) >> 4;
00483   SD_csd->RdBlockLen = CSD_Tab[5] & 0x0F;
00484 
00485   /*!< Byte 6 */
00486   SD_csd->PartBlockRead = (CSD_Tab[6] & 0x80) >> 7;
00487   SD_csd->WrBlockMisalign = (CSD_Tab[6] & 0x40) >> 6;
00488   SD_csd->RdBlockMisalign = (CSD_Tab[6] & 0x20) >> 5;
00489   SD_csd->DSRImpl = (CSD_Tab[6] & 0x10) >> 4;
00490   SD_csd->Reserved2 = 0; /*!< Reserved */
00491 
00492   SD_csd->DeviceSize = (CSD_Tab[6] & 0x03) << 10;
00493 
00494   /*!< Byte 7 */
00495   SD_csd->DeviceSize |= (CSD_Tab[7]) << 2;
00496 
00497   /*!< Byte 8 */
00498   SD_csd->DeviceSize |= (CSD_Tab[8] & 0xC0) >> 6;
00499 
00500   SD_csd->MaxRdCurrentVDDMin = (CSD_Tab[8] & 0x38) >> 3;
00501   SD_csd->MaxRdCurrentVDDMax = (CSD_Tab[8] & 0x07);
00502 
00503   /*!< Byte 9 */
00504   SD_csd->MaxWrCurrentVDDMin = (CSD_Tab[9] & 0xE0) >> 5;
00505   SD_csd->MaxWrCurrentVDDMax = (CSD_Tab[9] & 0x1C) >> 2;
00506   SD_csd->DeviceSizeMul = (CSD_Tab[9] & 0x03) << 1;
00507   /*!< Byte 10 */
00508   SD_csd->DeviceSizeMul |= (CSD_Tab[10] & 0x80) >> 7;
00509     
00510   SD_csd->EraseGrSize = (CSD_Tab[10] & 0x40) >> 6;
00511   SD_csd->EraseGrMul = (CSD_Tab[10] & 0x3F) << 1;
00512 
00513   /*!< Byte 11 */
00514   SD_csd->EraseGrMul |= (CSD_Tab[11] & 0x80) >> 7;
00515   SD_csd->WrProtectGrSize = (CSD_Tab[11] & 0x7F);
00516 
00517   /*!< Byte 12 */
00518   SD_csd->WrProtectGrEnable = (CSD_Tab[12] & 0x80) >> 7;
00519   SD_csd->ManDeflECC = (CSD_Tab[12] & 0x60) >> 5;
00520   SD_csd->WrSpeedFact = (CSD_Tab[12] & 0x1C) >> 2;
00521   SD_csd->MaxWrBlockLen = (CSD_Tab[12] & 0x03) << 2;
00522 
00523   /*!< Byte 13 */
00524   SD_csd->MaxWrBlockLen |= (CSD_Tab[13] & 0xC0) >> 6;
00525   SD_csd->WriteBlockPaPartial = (CSD_Tab[13] & 0x20) >> 5;
00526   SD_csd->Reserved3 = 0;
00527   SD_csd->ContentProtectAppli = (CSD_Tab[13] & 0x01);
00528 
00529   /*!< Byte 14 */
00530   SD_csd->FileFormatGrouop = (CSD_Tab[14] & 0x80) >> 7;
00531   SD_csd->CopyFlag = (CSD_Tab[14] & 0x40) >> 6;
00532   SD_csd->PermWrProtect = (CSD_Tab[14] & 0x20) >> 5;
00533   SD_csd->TempWrProtect = (CSD_Tab[14] & 0x10) >> 4;
00534   SD_csd->FileFormat = (CSD_Tab[14] & 0x0C) >> 2;
00535   SD_csd->ECC = (CSD_Tab[14] & 0x03);
00536 
00537   /*!< Byte 15 */
00538   SD_csd->CSD_CRC = (CSD_Tab[15] & 0xFE) >> 1;
00539   SD_csd->Reserved4 = 1;
00540 
00541   /*!< Return the reponse */
00542   return rvalue;
00543 }
00544 
00545 /**
00546   * @brief  Read the CID card register.
00547   *         Reading the contents of the CID register in SPI mode is a simple 
00548   *         read-block transaction.
00549   * @param  SD_cid: pointer on an CID register structure
00550   * @retval The SD Response: 
00551   *         - SD_RESPONSE_FAILURE: Sequence failed
00552   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00553   */
00554 SD_Error SD_GetCIDRegister(SD_CID* SD_cid)
00555 {
00556   uint32_t i = 0;
00557   SD_Error rvalue = SD_RESPONSE_FAILURE;
00558   uint8_t CID_Tab[16];
00559   
00560   /*!< SD chip select low */
00561   SD_CS_LOW();
00562   
00563   /*!< Send CMD10 (CID register) */
00564   SD_SendCmd(SD_CMD_SEND_CID, 0, 0xFF);
00565   
00566   /*!< Wait for response in the R1 format (0x00 is no errors) */
00567   if (!SD_GetResponse(SD_RESPONSE_NO_ERROR))
00568   {
00569     if (!SD_GetResponse(SD_START_DATA_SINGLE_BLOCK_READ))
00570     {
00571       /*!< Store CID register value on CID_Tab */
00572       for (i = 0; i < 16; i++)
00573       {
00574         CID_Tab[i] = SD_ReadByte();
00575       }
00576     }
00577     /*!< Get CRC bytes (not really needed by us, but required by SD) */
00578     SD_WriteByte(SD_DUMMY_BYTE);
00579     SD_WriteByte(SD_DUMMY_BYTE);
00580     /*!< Set response value to success */
00581     rvalue = SD_RESPONSE_NO_ERROR;
00582   }
00583   /*!< SD chip select high */
00584   SD_CS_HIGH();
00585   /*!< Send dummy byte: 8 Clock pulses of delay */
00586   SD_WriteByte(SD_DUMMY_BYTE);
00587 
00588   /*!< Byte 0 */
00589   SD_cid->ManufacturerID = CID_Tab[0];
00590 
00591   /*!< Byte 1 */
00592   SD_cid->OEM_AppliID = CID_Tab[1] << 8;
00593 
00594   /*!< Byte 2 */
00595   SD_cid->OEM_AppliID |= CID_Tab[2];
00596 
00597   /*!< Byte 3 */
00598   SD_cid->ProdName1 = CID_Tab[3] << 24;
00599 
00600   /*!< Byte 4 */
00601   SD_cid->ProdName1 |= CID_Tab[4] << 16;
00602 
00603   /*!< Byte 5 */
00604   SD_cid->ProdName1 |= CID_Tab[5] << 8;
00605 
00606   /*!< Byte 6 */
00607   SD_cid->ProdName1 |= CID_Tab[6];
00608 
00609   /*!< Byte 7 */
00610   SD_cid->ProdName2 = CID_Tab[7];
00611 
00612   /*!< Byte 8 */
00613   SD_cid->ProdRev = CID_Tab[8];
00614 
00615   /*!< Byte 9 */
00616   SD_cid->ProdSN = CID_Tab[9] << 24;
00617 
00618   /*!< Byte 10 */
00619   SD_cid->ProdSN |= CID_Tab[10] << 16;
00620 
00621   /*!< Byte 11 */
00622   SD_cid->ProdSN |= CID_Tab[11] << 8;
00623 
00624   /*!< Byte 12 */
00625   SD_cid->ProdSN |= CID_Tab[12];
00626 
00627   /*!< Byte 13 */
00628   SD_cid->Reserved1 |= (CID_Tab[13] & 0xF0) >> 4;
00629   SD_cid->ManufactDate = (CID_Tab[13] & 0x0F) << 8;
00630 
00631   /*!< Byte 14 */
00632   SD_cid->ManufactDate |= CID_Tab[14];
00633 
00634   /*!< Byte 15 */
00635   SD_cid->CID_CRC = (CID_Tab[15] & 0xFE) >> 1;
00636   SD_cid->Reserved2 = 1;
00637 
00638   /*!< Return the reponse */
00639   return rvalue;
00640 }
00641 
00642 /**
00643   * @brief  Send 5 bytes command to the SD card.
00644   * @param  Cmd: The user expected command to send to SD card.
00645   * @param  Arg: The command argument.
00646   * @param  Crc: The CRC.
00647   * @retval None
00648   */
00649 void SD_SendCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc)
00650 {
00651   uint32_t i = 0x00;
00652   
00653   uint8_t Frame[6];
00654   
00655   Frame[0] = (Cmd | 0x40); /*!< Construct byte 1 */
00656   
00657   Frame[1] = (uint8_t)(Arg >> 24); /*!< Construct byte 2 */
00658   
00659   Frame[2] = (uint8_t)(Arg >> 16); /*!< Construct byte 3 */
00660   
00661   Frame[3] = (uint8_t)(Arg >> 8); /*!< Construct byte 4 */
00662   
00663   Frame[4] = (uint8_t)(Arg); /*!< Construct byte 5 */
00664   
00665   Frame[5] = (Crc); /*!< Construct CRC: byte 6 */
00666   
00667   for (i = 0; i < 6; i++)
00668   {
00669     SD_WriteByte(Frame[i]); /*!< Send the Cmd bytes */
00670   }
00671 }
00672 
00673 /**
00674   * @brief  Get SD card data response.
00675   * @param  None
00676   * @retval The SD status: Read data response xxx0<status>1
00677   *         - status 010: Data accecpted
00678   *         - status 101: Data rejected due to a crc error
00679   *         - status 110: Data rejected due to a Write error.
00680   *         - status 111: Data rejected due to other error.
00681   */
00682 uint8_t SD_GetDataResponse(void)
00683 {
00684   uint32_t i = 0;
00685   uint8_t response, rvalue;
00686 
00687   while (i <= 64)
00688   {
00689     /*!< Read resonse */
00690     response = SD_ReadByte();
00691     /*!< Mask unused bits */
00692     response &= 0x1F;
00693     switch (response)
00694     {
00695       case SD_DATA_OK:
00696       {
00697         rvalue = SD_DATA_OK;
00698         break;
00699       }
00700       case SD_DATA_CRC_ERROR:
00701         return SD_DATA_CRC_ERROR;
00702       case SD_DATA_WRITE_ERROR:
00703         return SD_DATA_WRITE_ERROR;
00704       default:
00705       {
00706         rvalue = SD_DATA_OTHER_ERROR;
00707         break;
00708       }
00709     }
00710     /*!< Exit loop in case of data ok */
00711     if (rvalue == SD_DATA_OK)
00712       break;
00713     /*!< Increment loop counter */
00714     i++;
00715   }
00716 
00717   /*!< Wait null data */
00718   while (SD_ReadByte() == 0);
00719 
00720   /*!< Return response */
00721   return response;
00722 }
00723 
00724 /**
00725   * @brief  Returns the SD response.
00726   * @param  None
00727   * @retval The SD Response: 
00728   *         - SD_RESPONSE_FAILURE: Sequence failed
00729   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00730   */
00731 SD_Error SD_GetResponse(uint8_t Response)
00732 {
00733   uint32_t Count = 0xFFF;
00734 
00735   /*!< Check if response is got or a timeout is happen */
00736   while ((SD_ReadByte() != Response) && Count)
00737   {
00738     Count--;
00739   }
00740   if (Count == 0)
00741   {
00742     /*!< After time out */
00743     return SD_RESPONSE_FAILURE;
00744   }
00745   else
00746   {
00747     /*!< Right response got */
00748     return SD_RESPONSE_NO_ERROR;
00749   }
00750 }
00751 
00752 /**
00753   * @brief  Returns the SD status.
00754   * @param  None
00755   * @retval The SD status.
00756   */
00757 uint16_t SD_GetStatus(void)
00758 {
00759   uint16_t Status = 0;
00760 
00761   /*!< SD chip select low */
00762   SD_CS_LOW();
00763 
00764   /*!< Send CMD13 (SD_SEND_STATUS) to get SD status */
00765   SD_SendCmd(SD_CMD_SEND_STATUS, 0, 0xFF);
00766 
00767   Status = SD_ReadByte();
00768   Status |= (uint16_t)(SD_ReadByte() << 8);
00769 
00770   /*!< SD chip select high */
00771   SD_CS_HIGH();
00772 
00773   /*!< Send dummy byte 0xFF */
00774   SD_WriteByte(SD_DUMMY_BYTE);
00775 
00776   return Status;
00777 }
00778 
00779 /**
00780   * @brief  Put SD in Idle state.
00781   * @param  None
00782   * @retval The SD Response: 
00783   *         - SD_RESPONSE_FAILURE: Sequence failed
00784   *         - SD_RESPONSE_NO_ERROR: Sequence succeed
00785   */
00786 SD_Error SD_GoIdleState(void)
00787 {
00788   /*!< SD chip select low */
00789   SD_CS_LOW();
00790   
00791   /*!< Send CMD0 (SD_CMD_GO_IDLE_STATE) to put SD in SPI mode */
00792   SD_SendCmd(SD_CMD_GO_IDLE_STATE, 0, 0x95);
00793   
00794   /*!< Wait for In Idle State Response (R1 Format) equal to 0x01 */
00795   if (SD_GetResponse(SD_IN_IDLE_STATE))
00796   {
00797     /*!< No Idle State Response: return response failue */
00798     return SD_RESPONSE_FAILURE;
00799   }
00800   /*----------Activates the card initialization process-----------*/
00801   do
00802   {
00803     /*!< SD chip select high */
00804     SD_CS_HIGH();
00805     
00806     /*!< Send Dummy byte 0xFF */
00807     SD_WriteByte(SD_DUMMY_BYTE);
00808     
00809     /*!< SD chip select low */
00810     SD_CS_LOW();
00811     
00812     /*!< Send CMD1 (Activates the card process) until response equal to 0x0 */
00813     SD_SendCmd(SD_CMD_SEND_OP_COND, 0, 0xFF);
00814     /*!< Wait for no error Response (R1 Format) equal to 0x00 */
00815   }
00816   while (SD_GetResponse(SD_RESPONSE_NO_ERROR));
00817   
00818   /*!< SD chip select high */
00819   SD_CS_HIGH();
00820   
00821   /*!< Send dummy byte 0xFF */
00822   SD_WriteByte(SD_DUMMY_BYTE);
00823   
00824   return SD_RESPONSE_NO_ERROR;
00825 }
00826 
00827 /**
00828   * @brief  Write a byte on the SD.
00829   * @param  Data: byte to send.
00830   * @retval None
00831   */
00832 uint8_t SD_WriteByte(uint8_t Data)
00833 {
00834   /*!< Wait until the transmit buffer is empty */
00835   while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET)
00836   {
00837   }
00838   
00839   /*!< Send the byte */
00840   SPI_I2S_SendData(SD_SPI, Data);
00841   
00842   /*!< Wait to receive a byte*/
00843   while(SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
00844   {
00845   }
00846   
00847   /*!< Return the byte read from the SPI bus */ 
00848   return SPI_I2S_ReceiveData(SD_SPI);
00849 }
00850 
00851 /**
00852   * @brief  Read a byte from the SD.
00853   * @param  None
00854   * @retval The received byte.
00855   */
00856 uint8_t SD_ReadByte(void)
00857 {
00858   uint8_t Data = 0;
00859   
00860   /*!< Wait until the transmit buffer is empty */
00861   while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_TXE) == RESET)
00862   {
00863   }
00864   /*!< Send the byte */
00865   SPI_I2S_SendData(SD_SPI, SD_DUMMY_BYTE);
00866 
00867   /*!< Wait until a data is received */
00868   while (SPI_I2S_GetFlagStatus(SD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
00869   {
00870   }
00871   /*!< Get the received data */
00872   Data = SPI_I2S_ReceiveData(SD_SPI);
00873 
00874   /*!< Return the shifted data */
00875   return Data;
00876 }
00877 
00878 /**
00879   * @}
00880   */
00881 
00882 
00883 /**
00884   * @}
00885   */
00886 
00887 
00888 /**
00889   * @}
00890   */
00891 
00892 /**
00893   * @}
00894   */
00895 
00896 /**
00897   * @}
00898   */  
00899 
00900 /******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/
STM32F10x Standard Peripherals Library: Footer

 

 

 

      For complete documentation on STM32(CORTEX M3) 32-bit Microcontrollers platform visit  www.st.com/STM32