Huseyin Buyukesmeli / FatFS

Dependents:   DISCO-F746NG_uSD Active-WolfMan_V2-5-All-Frank-Board-Functions_copy DISCO-F746NG_uSD DISCO-F746NG_uSD

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.4.0
00006   * @date    09-September-2016
00007   * @brief   SD Disk I/O driver
00008   ******************************************************************************
00009   * @attention
00010   *
00011   * <h2><center>&copy; COPYRIGHT 2016 STMicroelectronics</center></h2>
00012   *
00013   * Redistribution and use in source and binary forms, with or without 
00014   * modification, are permitted, provided that the following conditions are met:
00015   *
00016   * 1. Redistribution 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 other 
00022   *    contributors to this software may be used to endorse or promote products 
00023   *    derived from this software without specific written permission.
00024   * 4. This software, including modifications and/or derivative works of this 
00025   *    software, must execute solely and exclusively on microcontroller or
00026   *    microprocessor devices manufactured by or for STMicroelectronics.
00027   * 5. Redistribution and use of this software other than as permitted under 
00028   *    this license is void and will automatically terminate your rights under 
00029   *    this license. 
00030   *
00031   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS" 
00032   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT 
00033   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A 
00034   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
00035   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT 
00036   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00037   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00038   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
00039   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 
00040   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 
00041   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00042   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00043   *
00044   ******************************************************************************
00045   */ 
00046 
00047 /* Includes ------------------------------------------------------------------*/
00048 #include <string.h>
00049 #include "ff_gen_drv.h"
00050 
00051 /* Private typedef -----------------------------------------------------------*/
00052 /* Private define ------------------------------------------------------------*/
00053 /* Private variables ---------------------------------------------------------*/
00054 /* Disk status */
00055 static volatile DSTATUS Stat = STA_NOINIT;
00056 
00057 /* Private function prototypes -----------------------------------------------*/
00058 DSTATUS SD_initialize (BYTE);
00059 DSTATUS SD_status (BYTE);
00060 DRESULT SD_read (BYTE, BYTE*, DWORD, UINT);
00061 #if _USE_WRITE == 1
00062   DRESULT SD_write (BYTE, const BYTE*, DWORD, UINT);
00063 #endif /* _USE_WRITE == 1 */
00064 #if _USE_IOCTL == 1
00065   DRESULT SD_ioctl (BYTE, BYTE, void*);
00066 #endif  /* _USE_IOCTL == 1 */
00067   
00068 const Diskio_drvTypeDef  SD_Driver =
00069 {
00070   SD_initialize,
00071   SD_status,
00072   SD_read, 
00073 #if  _USE_WRITE == 1
00074   SD_write,
00075 #endif /* _USE_WRITE == 1 */
00076   
00077 #if  _USE_IOCTL == 1
00078   SD_ioctl,
00079 #endif /* _USE_IOCTL == 1 */
00080 };
00081 
00082 /* Private functions ---------------------------------------------------------*/
00083 
00084 /**
00085   * @brief  Initializes a Drive
00086   * @param  lun : not used 
00087   * @retval DSTATUS: Operation status
00088   */
00089 DSTATUS SD_initialize(BYTE lun)
00090 {
00091   Stat = STA_NOINIT;
00092   
00093   /* Configure the uSD device */
00094   if(BSP_SD_Init() == MSD_OK)
00095   {
00096     Stat &= ~STA_NOINIT;
00097   }
00098 
00099   return Stat;
00100 }
00101 
00102 /**
00103   * @brief  Gets Disk Status
00104   * @param  lun : not used
00105   * @retval DSTATUS: Operation status
00106   */
00107 DSTATUS SD_status(BYTE lun)
00108 {
00109   Stat = STA_NOINIT;
00110 
00111   if(BSP_SD_GetCardState() == MSD_OK)
00112   {
00113     Stat &= ~STA_NOINIT;
00114   }
00115   
00116   return Stat;
00117 }
00118 
00119 /**
00120   * @brief  Reads Sector(s)
00121   * @param  lun : not used
00122   * @param  *buff: Data buffer to store read data
00123   * @param  sector: Sector address (LBA)
00124   * @param  count: Number of sectors to read (1..128)
00125   * @retval DRESULT: Operation result
00126   */
00127 DRESULT SD_read(BYTE lun, BYTE *buff, DWORD sector, UINT count)
00128 {
00129   DRESULT res = RES_ERROR;
00130   uint32_t timeout = 100000;
00131 
00132   if(BSP_SD_ReadBlocks((uint32_t*)buff, 
00133                        (uint32_t) (sector), 
00134                        count, SD_DATATIMEOUT) == MSD_OK)
00135   {
00136     while(BSP_SD_GetCardState()!= MSD_OK)
00137     {
00138       if (timeout-- == 0)
00139       {
00140         return RES_ERROR;
00141       }
00142     }
00143     res = RES_OK;
00144   }
00145   
00146   return res;
00147 }
00148 
00149 /**
00150   * @brief  Writes Sector(s)
00151   * @param  lun : not used
00152   * @param  *buff: Data to be written
00153   * @param  sector: Sector address (LBA)
00154   * @param  count: Number of sectors to write (1..128)
00155   * @retval DRESULT: Operation result
00156   */
00157 #if _USE_WRITE == 1
00158 DRESULT SD_write(BYTE lun, const BYTE *buff, DWORD sector, UINT count)
00159 {
00160   DRESULT res = RES_ERROR;
00161   uint32_t timeout = 100000;
00162 
00163   if(BSP_SD_WriteBlocks((uint32_t*)buff, 
00164                         (uint32_t)(sector), 
00165                         count, SD_DATATIMEOUT) == MSD_OK)
00166   {
00167     while(BSP_SD_GetCardState()!= MSD_OK)
00168     {
00169       if (timeout-- == 0)
00170       {
00171         return RES_ERROR;
00172       }
00173     }    
00174     res = RES_OK;
00175   }
00176   
00177   return res;
00178 }
00179 #endif /* _USE_WRITE == 1 */
00180 
00181 /**
00182   * @brief  I/O control operation
00183   * @param  lun : not used
00184   * @param  cmd: Control code
00185   * @param  *buff: Buffer to send/receive control data
00186   * @retval DRESULT: Operation result
00187   */
00188 #if _USE_IOCTL == 1
00189 DRESULT SD_ioctl(BYTE lun, BYTE cmd, void *buff)
00190 {
00191   DRESULT res = RES_ERROR;
00192   BSP_SD_CardInfo CardInfo;
00193   
00194   if (Stat & STA_NOINIT) return RES_NOTRDY;
00195   
00196   switch (cmd)
00197   {
00198   /* Make sure that no pending write process */
00199   case CTRL_SYNC :
00200     res = RES_OK;
00201     break;
00202   
00203   /* Get number of sectors on the disk (DWORD) */
00204   case GET_SECTOR_COUNT :
00205     BSP_SD_GetCardInfo(&CardInfo);
00206     *(DWORD*)buff = CardInfo.LogBlockNbr;
00207     res = RES_OK;
00208     break;
00209   
00210   /* Get R/W sector size (WORD) */
00211   case GET_SECTOR_SIZE :
00212     BSP_SD_GetCardInfo(&CardInfo);
00213     *(WORD*)buff = CardInfo.LogBlockSize;
00214     res = RES_OK;
00215     break;
00216   
00217   /* Get erase block size in unit of sector (DWORD) */
00218   case GET_BLOCK_SIZE :
00219     BSP_SD_GetCardInfo(&CardInfo);
00220     *(DWORD*)buff = CardInfo.LogBlockSize;
00221     break;
00222   
00223   default:
00224     res = RES_PARERR;
00225   }
00226   
00227   return res;
00228 }
00229 #endif /* _USE_IOCTL == 1 */
00230   
00231 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
00232