WILLY BAYOT / stm32_adafruit

Dependents:   TDEMNucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers stm32_adafruit_sd.h Source File

stm32_adafruit_sd.h

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32_adafruit_sd.h
00004   * @author  MCD Application Team
00005   * @version V1.0.0
00006   * @date    22-April-2014
00007   * @brief   This file contains the common defines and functions prototypes for
00008   *          the stm32_adafruit_sd.c driver.
00009   ******************************************************************************
00010   * @attention
00011   *
00012   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
00013   *
00014   * Redistribution and use in source and binary forms, with or without modification,
00015   * are permitted provided that the following conditions are met:
00016   *   1. Redistributions of source code must retain the above copyright notice,
00017   *      this list of conditions and the following disclaimer.
00018   *   2. Redistributions in binary form must reproduce the above copyright notice,
00019   *      this list of conditions and the following disclaimer in the documentation
00020   *      and/or other materials provided with the distribution.
00021   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00022   *      may be used to endorse or promote products derived from this software
00023   *      without specific prior written permission.
00024   *
00025   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00026   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00027   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00028   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00029   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00030   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00031   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00032   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00033   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00034   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00035   *
00036   ******************************************************************************
00037   */ 
00038 
00039 /* Define to prevent recursive inclusion -------------------------------------*/
00040 #ifndef __STM32_ADAFRUIT_SD_H
00041 #define __STM32_ADAFRUIT_SD_H
00042 
00043 #ifdef __cplusplus
00044  extern "C" {
00045 #endif 
00046 
00047 /* Includes ------------------------------------------------------------------*/
00048 #include <stdint.h>
00049 
00050 /** @addtogroup BSP
00051   * @{
00052   */ 
00053 #define __IO    volatile   
00054    
00055 /** @addtogroup STM32_ADAFRUIT
00056   * @{
00057   */
00058     
00059 /** @defgroup STM32_ADAFRUIT_SD
00060   * @{
00061   */    
00062 
00063 /** @defgroup STM32_ADAFRUIT_SD_Exported_Types
00064   * @{
00065   */
00066    
00067 /** 
00068   * @brief  SD status structure definition  
00069   */     
00070 #define MSD_OK         0x00
00071 #define MSD_ERROR      0x01
00072    
00073 /** 
00074   * @brief  SD Transfer state enumeration structure   
00075   */   
00076 
00077 
00078 /**
00079   * @brief  SD reponses and error flags
00080   */
00081 typedef enum
00082 {
00083   SD_RESPONSE_NO_ERROR      = (0x00),
00084   SD_IN_IDLE_STATE          = (0x01),
00085   //SD_ERASE_RESET            = (0x02),
00086   SD_ILLEGAL_COMMAND        = (0x04),
00087   SD_COM_CRC_ERROR          = (0x08),
00088   SD_ERASE_SEQUENCE_ERROR   = (0x10),
00089   SD_ADDRESS_ERROR          = (0x20),
00090   SD_PARAMETER_ERROR        = (0x40),
00091   SD_RESPONSE_FAILURE       = (0xFF),
00092 
00093 /**
00094   * @brief  Data response error
00095   */
00096   SD_DATA_OK                = (0x05),
00097   SD_DATA_CRC_ERROR         = (0x0B),
00098   SD_DATA_WRITE_ERROR       = (0x0D),
00099   SD_DATA_OTHER_ERROR       = (0xFF)
00100 } SD_Error;
00101 
00102 /** 
00103   * @brief  Card Specific Data: CSD Register
00104   */ 
00105 typedef struct
00106 {
00107   __IO uint8_t  CSDStruct;            /* CSD structure */
00108   __IO uint8_t  SysSpecVersion;       /* System specification version */
00109   __IO uint8_t  Reserved1;            /* Reserved */
00110   __IO uint8_t  TAAC;                 /* Data read access-time 1 */
00111   __IO uint8_t  NSAC;                 /* Data read access-time 2 in CLK cycles */
00112   __IO uint8_t  MaxBusClkFrec;        /* Max. bus clock frequency */
00113   __IO uint16_t CardComdClasses;      /* Card command classes */
00114   __IO uint8_t  RdBlockLen;           /* Max. read data block length */
00115   __IO uint8_t  PartBlockRead;        /* Partial blocks for read allowed */
00116   __IO uint8_t  WrBlockMisalign;      /* Write block misalignment */
00117   __IO uint8_t  RdBlockMisalign;      /* Read block misalignment */
00118   __IO uint8_t  DSRImpl;              /* DSR implemented */
00119   __IO uint8_t  Reserved2;            /* Reserved */
00120   __IO uint32_t DeviceSize;           /* Device Size */
00121   __IO uint8_t  MaxRdCurrentVDDMin;   /* Max. read current @ VDD min */
00122   __IO uint8_t  MaxRdCurrentVDDMax;   /* Max. read current @ VDD max */
00123   __IO uint8_t  MaxWrCurrentVDDMin;   /* Max. write current @ VDD min */
00124   __IO uint8_t  MaxWrCurrentVDDMax;   /* Max. write current @ VDD max */
00125   __IO uint8_t  DeviceSizeMul;        /* Device size multiplier */
00126   __IO uint8_t  EraseGrSize;          /* Erase group size */
00127   __IO uint8_t  EraseGrMul;           /* Erase group size multiplier */
00128   __IO uint8_t  WrProtectGrSize;      /* Write protect group size */
00129   __IO uint8_t  WrProtectGrEnable;    /* Write protect group enable */
00130   __IO uint8_t  ManDeflECC;           /* Manufacturer default ECC */
00131   __IO uint8_t  WrSpeedFact;          /* Write speed factor */
00132   __IO uint8_t  MaxWrBlockLen;        /* Max. write data block length */
00133   __IO uint8_t  WriteBlockPaPartial;  /* Partial blocks for write allowed */
00134   __IO uint8_t  Reserved3;            /* Reserded */
00135   __IO uint8_t  ContentProtectAppli;  /* Content protection application */
00136   __IO uint8_t  FileFormatGrouop;     /* File format group */
00137   __IO uint8_t  CopyFlag;             /* Copy flag (OTP) */
00138   __IO uint8_t  PermWrProtect;        /* Permanent write protection */
00139   __IO uint8_t  TempWrProtect;        /* Temporary write protection */
00140   __IO uint8_t  FileFormat;           /* File Format */
00141   __IO uint8_t  ECC;                  /* ECC code */
00142   __IO uint8_t  CSD_CRC;              /* CSD CRC */
00143   __IO uint8_t  Reserved4;            /* always 1*/
00144 } SD_CSD;
00145 
00146 /** 
00147   * @brief  Card Identification Data: CID Register   
00148   */
00149 typedef struct
00150 {
00151   __IO uint8_t  ManufacturerID;       /* ManufacturerID */
00152   __IO uint16_t OEM_AppliID;          /* OEM/Application ID */
00153   __IO uint32_t ProdName1;            /* Product Name part1 */
00154   __IO uint8_t  ProdName2;            /* Product Name part2*/
00155   __IO uint8_t  ProdRev;              /* Product Revision */
00156   __IO uint32_t ProdSN;               /* Product Serial Number */
00157   __IO uint8_t  Reserved1;            /* Reserved1 */
00158   __IO uint16_t ManufactDate;         /* Manufacturing Date */
00159   __IO uint8_t  CID_CRC;              /* CID CRC */
00160   __IO uint8_t  Reserved2;            /* always 1 */
00161 } SD_CID;
00162 
00163 /** 
00164   * @brief SD Card information 
00165   */
00166 typedef struct
00167 {
00168   SD_CSD Csd;
00169   SD_CID Cid;
00170   uint32_t CardCapacity;  /* Card Capacity */
00171   uint32_t CardBlockSize; /* Card Block Size */
00172 } SD_CardInfo;
00173 
00174 /**
00175   * @}
00176   */
00177   
00178 /** @defgroup STM32_ADAFRUIT_SPI_SD_Exported_Constants
00179   * @{
00180   */ 
00181   
00182 /**
00183   * @brief  Block Size
00184   */
00185 #define SD_BLOCK_SIZE    0x200
00186 
00187 /**
00188   * @brief  Start Data tokens:
00189   *         Tokens (necessary because at nop/idle (and CS active) only 0xff is 
00190   *         on the data/command line)  
00191   */ 
00192 #define SD_START_DATA_SINGLE_BLOCK_READ    0xFE  /* Data token start byte, Start Single Block Read */
00193 #define SD_START_DATA_MULTIPLE_BLOCK_READ  0xFE  /* Data token start byte, Start Multiple Block Read */
00194 #define SD_START_DATA_SINGLE_BLOCK_WRITE   0xFE  /* Data token start byte, Start Single Block Write */
00195 #define SD_START_DATA_MULTIPLE_BLOCK_WRITE 0xFD  /* Data token start byte, Start Multiple Block Write */
00196 #define SD_STOP_DATA_MULTIPLE_BLOCK_WRITE  0xFD  /* Data toke stop byte, Stop Multiple Block Write */
00197 
00198 /**
00199   * @brief  SD detection on its memory slot
00200   */
00201 #define SD_PRESENT               ((uint8_t)0x01)
00202 #define SD_NOT_PRESENT           ((uint8_t)0x00)
00203    
00204 /**
00205   * @brief  Commands: CMDxx = CMD-number | 0x40
00206   */
00207 #define SD_CMD_GO_IDLE_STATE          0   /* CMD0 = 0x40 */
00208 #define SD_CMD_SEND_OP_COND           1   /* CMD1 = 0x41 */
00209 #define SD_CMD_SEND_CSD               9   /* CMD9 = 0x49 */
00210 #define SD_CMD_SEND_CID               10  /* CMD10 = 0x4A */
00211 #define SD_CMD_STOP_TRANSMISSION      12  /* CMD12 = 0x4C */
00212 #define SD_CMD_SEND_STATUS            13  /* CMD13 = 0x4D */
00213 #define SD_CMD_SET_BLOCKLEN           16  /* CMD16 = 0x50 */
00214 #define SD_CMD_READ_SINGLE_BLOCK      17  /* CMD17 = 0x51 */
00215 #define SD_CMD_READ_MULT_BLOCK        18  /* CMD18 = 0x52 */
00216 #define SD_CMD_SET_BLOCK_COUNT        23  /* CMD23 = 0x57 */
00217 #define SD_CMD_WRITE_SINGLE_BLOCK     24  /* CMD24 = 0x58 */
00218 #define SD_CMD_WRITE_MULT_BLOCK       25  /* CMD25 = 0x59 */
00219 #define SD_CMD_PROG_CSD               27  /* CMD27 = 0x5B */
00220 #define SD_CMD_SET_WRITE_PROT         28  /* CMD28 = 0x5C */
00221 #define SD_CMD_CLR_WRITE_PROT         29  /* CMD29 = 0x5D */
00222 #define SD_CMD_SEND_WRITE_PROT        30  /* CMD30 = 0x5E */
00223 #define SD_CMD_SD_ERASE_GRP_START     32  /* CMD32 = 0x60 */
00224 #define SD_CMD_SD_ERASE_GRP_END       33  /* CMD33 = 0x61 */
00225 #define SD_CMD_UNTAG_SECTOR           34  /* CMD34 = 0x62 */
00226 #define SD_CMD_ERASE_GRP_START        35  /* CMD35 = 0x63 */
00227 #define SD_CMD_ERASE_GRP_END          36  /* CMD36 = 0x64 */
00228 #define SD_CMD_UNTAG_ERASE_GROUP      37  /* CMD37 = 0x65 */
00229 #define SD_CMD_ERASE                  38  /* CMD38 = 0x66 */
00230 
00231 /**
00232   * @}
00233   */
00234   
00235 /** @defgroup STM32_ADAFRUIT_SD_Exported_Macro
00236   * @{
00237   */ 
00238 
00239 /**
00240   * @}
00241   */ 
00242 
00243 /** @defgroup STM32_ADAFRUIT_SD_Exported_Functions
00244   * @{
00245   */   
00246 uint8_t BSP_SD_Init(void);
00247 uint8_t BSP_SD_IsDetected(void);
00248 uint8_t BSP_SD_ReadBlocks(uint32_t *pData, uint32_t ReadAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
00249 uint8_t BSP_SD_WriteBlocks(uint32_t *pData, uint32_t WriteAddr, uint16_t BlockSize, uint32_t NumberOfBlocks);
00250 uint8_t BSP_SD_Erase(uint32_t StartAddr, uint32_t EndAddr);
00251 uint8_t BSP_SD_GetStatus(void);
00252 uint8_t BSP_SD_GetCardInfo(SD_CardInfo *pCardInfo);
00253    
00254 /* Link functions for SD Card peripheral*/
00255 void    SD_IO_Init(void); 
00256 void    SD_IO_WriteByte(uint8_t Data);
00257 uint8_t SD_IO_ReadByte(void);
00258 uint8_t SD_IO_WriteCmd(uint8_t Cmd, uint32_t Arg, uint8_t Crc, uint8_t Response);
00259 uint8_t SD_IO_WaitResponse(uint8_t Response);
00260 void    SD_IO_WriteDummy(void);
00261 
00262 #ifdef __cplusplus
00263 }
00264 #endif
00265 
00266 #endif /* __STM32_ADAFRUIT_SD_H */
00267 
00268 /**
00269   * @}
00270   */ 
00271 
00272 /**
00273   * @}
00274   */ 
00275 
00276 /**
00277   * @}
00278   */ 
00279 
00280 /**
00281   * @}
00282   */ 
00283 
00284 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/