WILLY BAYOT / stm32_adafruit

Dependents:   TDEMNucleo

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers sd_diskio.c Source File

sd_diskio.c

Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    sd_diskio.c
00004   * @author  MCD Application Team
00005   * @version V1.1.0
00006   * @date    22-April-2014
00007   * @brief   SD Disk I/O driver
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
00012   *
00013   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
00014   * You may not use this file except in compliance with the License.
00015   * You may obtain a copy of the License at:
00016   *
00017   *        http://www.st.com/software_license_agreement_liberty_v2
00018   *
00019   * Unless required by applicable law or agreed to in writing, software 
00020   * distributed under the License is distributed on an "AS IS" BASIS, 
00021   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00022   * See the License for the specific language governing permissions and
00023   * limitations under the License.
00024   *
00025   ******************************************************************************
00026   */ 
00027 
00028 /* Includes ------------------------------------------------------------------*/
00029 #include <string.h>
00030 #include "ff_gen_drv.h"
00031 
00032 /* Private typedef -----------------------------------------------------------*/
00033 /* Private define ------------------------------------------------------------*/
00034 /* Block Size in Bytes */
00035 #define BLOCK_SIZE                512
00036 
00037 /* Private variables ---------------------------------------------------------*/
00038 /* Disk status */
00039 static volatile DSTATUS Stat = STA_NOINIT;
00040 
00041 /* Private function prototypes -----------------------------------------------*/
00042 DSTATUS SD_initialize (void);
00043 DSTATUS SD_status (void);
00044 DRESULT SD_read (BYTE*, DWORD, BYTE);
00045 #if _USE_WRITE == 1
00046   DRESULT SD_write (const BYTE*, DWORD, BYTE);
00047 #endif /* _USE_WRITE == 1 */
00048 #if _USE_IOCTL == 1
00049   DRESULT SD_ioctl (BYTE, void*);
00050 #endif  /* _USE_IOCTL == 1 */
00051   
00052 Diskio_drvTypeDef  SD_Driver =
00053 {
00054   SD_initialize,
00055   SD_status,
00056   SD_read, 
00057 #if  _USE_WRITE == 1
00058   SD_write,
00059 #endif /* _USE_WRITE == 1 */
00060   
00061 #if  _USE_IOCTL == 1
00062   SD_ioctl,
00063 #endif /* _USE_IOCTL == 1 */
00064 };
00065 
00066 /* Private functions ---------------------------------------------------------*/
00067 
00068 /**
00069   * @brief  Initializes a Drive
00070   * @param  None
00071   * @retval DSTATUS: Operation status
00072   */
00073 DSTATUS SD_initialize(void)
00074 {
00075   Stat = STA_NOINIT;
00076   
00077   /* Configure the uSD device */
00078   if(BSP_SD_Init() == MSD_OK)
00079   {
00080     Stat &= ~STA_NOINIT;
00081   }
00082 
00083   return Stat;
00084 }
00085 
00086 /**
00087   * @brief  Gets Disk Status
00088   * @param  None
00089   * @retval DSTATUS: Operation status
00090   */
00091 DSTATUS SD_status(void)
00092 {
00093   Stat = STA_NOINIT;
00094 
00095   if(BSP_SD_GetStatus() == MSD_OK)
00096   {
00097     Stat &= ~STA_NOINIT;
00098   }
00099   
00100   return Stat;
00101 }
00102 
00103 /**
00104   * @brief  Reads Sector(s)
00105   * @param  *buff: Data buffer to store read data
00106   * @param  sector: Sector address (LBA)
00107   * @param  count: Number of sectors to read (1..128)
00108   * @retval DRESULT: Operation result
00109   */
00110 DRESULT SD_read(BYTE *buff, DWORD sector, BYTE count)
00111 {
00112   DRESULT res = RES_OK;
00113   
00114   if(BSP_SD_ReadBlocks((uint32_t*)buff, 
00115                        (uint64_t) (sector * BLOCK_SIZE), 
00116                        BLOCK_SIZE, 
00117                        count) != MSD_OK)
00118   {
00119     res = RES_ERROR;
00120   }
00121   
00122   return res;
00123 }
00124 
00125 /**
00126   * @brief  Writes Sector(s)
00127   * @param  *buff: Data to be written
00128   * @param  sector: Sector address (LBA)
00129   * @param  count: Number of sectors to write (1..128)
00130   * @retval DRESULT: Operation result
00131   */
00132 #if _USE_WRITE == 1
00133 DRESULT SD_write(const BYTE *buff, DWORD sector, BYTE count)
00134 {
00135   DRESULT res = RES_OK;
00136   
00137   if(BSP_SD_WriteBlocks((uint32_t*)buff, 
00138                         (uint64_t)(sector * BLOCK_SIZE), 
00139                         BLOCK_SIZE, count) != MSD_OK)
00140   {
00141     res = RES_ERROR;
00142   }
00143   
00144   return res;
00145 }
00146 #endif /* _USE_WRITE == 1 */
00147 
00148 /**
00149   * @brief  I/O control operation
00150   * @param  cmd: Control code
00151   * @param  *buff: Buffer to send/receive control data
00152   * @retval DRESULT: Operation result
00153   */
00154 #if _USE_IOCTL == 1
00155 DRESULT SD_ioctl(BYTE cmd, void *buff)
00156 {
00157   DRESULT res = RES_ERROR;
00158   SD_CardInfo CardInfo;
00159   
00160   if (Stat & STA_NOINIT) return RES_NOTRDY;
00161   
00162   switch (cmd)
00163   {
00164   /* Make sure that no pending write process */
00165   case CTRL_SYNC :
00166     res = RES_OK;
00167     break;
00168   
00169   /* Get number of sectors on the disk (DWORD) */
00170   case GET_SECTOR_COUNT :
00171     BSP_SD_GetCardInfo(&CardInfo);
00172     *(DWORD*)buff = CardInfo.CardCapacity / BLOCK_SIZE;
00173     res = RES_OK;
00174     break;
00175   
00176   /* Get R/W sector size (WORD) */
00177   case GET_SECTOR_SIZE :
00178     *(WORD*)buff = BLOCK_SIZE;
00179     res = RES_OK;
00180     break;
00181   
00182   /* Get erase block size in unit of sector (DWORD) */
00183   case GET_BLOCK_SIZE :
00184     *(DWORD*)buff = BLOCK_SIZE;
00185     break;
00186   
00187   default:
00188     res = RES_PARERR;
00189   }
00190   
00191   return res;
00192 }
00193 #endif /* _USE_IOCTL == 1 */
00194   
00195 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00196