ADAFRUIT CLD & SD lib
sd_diskio.c@0:4db361f2e6d5, 2015-01-04 (annotated)
- Committer:
- willybayot
- Date:
- Sun Jan 04 13:25:59 2015 +0000
- Revision:
- 0:4db361f2e6d5
ADAFRUIT revision
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
willybayot | 0:4db361f2e6d5 | 1 | /** |
willybayot | 0:4db361f2e6d5 | 2 | ****************************************************************************** |
willybayot | 0:4db361f2e6d5 | 3 | * @file sd_diskio.c |
willybayot | 0:4db361f2e6d5 | 4 | * @author MCD Application Team |
willybayot | 0:4db361f2e6d5 | 5 | * @version V1.1.0 |
willybayot | 0:4db361f2e6d5 | 6 | * @date 22-April-2014 |
willybayot | 0:4db361f2e6d5 | 7 | * @brief SD Disk I/O driver |
willybayot | 0:4db361f2e6d5 | 8 | ****************************************************************************** |
willybayot | 0:4db361f2e6d5 | 9 | * @attention |
willybayot | 0:4db361f2e6d5 | 10 | * |
willybayot | 0:4db361f2e6d5 | 11 | * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2> |
willybayot | 0:4db361f2e6d5 | 12 | * |
willybayot | 0:4db361f2e6d5 | 13 | * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License"); |
willybayot | 0:4db361f2e6d5 | 14 | * You may not use this file except in compliance with the License. |
willybayot | 0:4db361f2e6d5 | 15 | * You may obtain a copy of the License at: |
willybayot | 0:4db361f2e6d5 | 16 | * |
willybayot | 0:4db361f2e6d5 | 17 | * http://www.st.com/software_license_agreement_liberty_v2 |
willybayot | 0:4db361f2e6d5 | 18 | * |
willybayot | 0:4db361f2e6d5 | 19 | * Unless required by applicable law or agreed to in writing, software |
willybayot | 0:4db361f2e6d5 | 20 | * distributed under the License is distributed on an "AS IS" BASIS, |
willybayot | 0:4db361f2e6d5 | 21 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
willybayot | 0:4db361f2e6d5 | 22 | * See the License for the specific language governing permissions and |
willybayot | 0:4db361f2e6d5 | 23 | * limitations under the License. |
willybayot | 0:4db361f2e6d5 | 24 | * |
willybayot | 0:4db361f2e6d5 | 25 | ****************************************************************************** |
willybayot | 0:4db361f2e6d5 | 26 | */ |
willybayot | 0:4db361f2e6d5 | 27 | |
willybayot | 0:4db361f2e6d5 | 28 | /* Includes ------------------------------------------------------------------*/ |
willybayot | 0:4db361f2e6d5 | 29 | #include <string.h> |
willybayot | 0:4db361f2e6d5 | 30 | #include "ff_gen_drv.h" |
willybayot | 0:4db361f2e6d5 | 31 | |
willybayot | 0:4db361f2e6d5 | 32 | /* Private typedef -----------------------------------------------------------*/ |
willybayot | 0:4db361f2e6d5 | 33 | /* Private define ------------------------------------------------------------*/ |
willybayot | 0:4db361f2e6d5 | 34 | /* Block Size in Bytes */ |
willybayot | 0:4db361f2e6d5 | 35 | #define BLOCK_SIZE 512 |
willybayot | 0:4db361f2e6d5 | 36 | |
willybayot | 0:4db361f2e6d5 | 37 | /* Private variables ---------------------------------------------------------*/ |
willybayot | 0:4db361f2e6d5 | 38 | /* Disk status */ |
willybayot | 0:4db361f2e6d5 | 39 | static volatile DSTATUS Stat = STA_NOINIT; |
willybayot | 0:4db361f2e6d5 | 40 | |
willybayot | 0:4db361f2e6d5 | 41 | /* Private function prototypes -----------------------------------------------*/ |
willybayot | 0:4db361f2e6d5 | 42 | DSTATUS SD_initialize (void); |
willybayot | 0:4db361f2e6d5 | 43 | DSTATUS SD_status (void); |
willybayot | 0:4db361f2e6d5 | 44 | DRESULT SD_read (BYTE*, DWORD, BYTE); |
willybayot | 0:4db361f2e6d5 | 45 | #if _USE_WRITE == 1 |
willybayot | 0:4db361f2e6d5 | 46 | DRESULT SD_write (const BYTE*, DWORD, BYTE); |
willybayot | 0:4db361f2e6d5 | 47 | #endif /* _USE_WRITE == 1 */ |
willybayot | 0:4db361f2e6d5 | 48 | #if _USE_IOCTL == 1 |
willybayot | 0:4db361f2e6d5 | 49 | DRESULT SD_ioctl (BYTE, void*); |
willybayot | 0:4db361f2e6d5 | 50 | #endif /* _USE_IOCTL == 1 */ |
willybayot | 0:4db361f2e6d5 | 51 | |
willybayot | 0:4db361f2e6d5 | 52 | Diskio_drvTypeDef SD_Driver = |
willybayot | 0:4db361f2e6d5 | 53 | { |
willybayot | 0:4db361f2e6d5 | 54 | SD_initialize, |
willybayot | 0:4db361f2e6d5 | 55 | SD_status, |
willybayot | 0:4db361f2e6d5 | 56 | SD_read, |
willybayot | 0:4db361f2e6d5 | 57 | #if _USE_WRITE == 1 |
willybayot | 0:4db361f2e6d5 | 58 | SD_write, |
willybayot | 0:4db361f2e6d5 | 59 | #endif /* _USE_WRITE == 1 */ |
willybayot | 0:4db361f2e6d5 | 60 | |
willybayot | 0:4db361f2e6d5 | 61 | #if _USE_IOCTL == 1 |
willybayot | 0:4db361f2e6d5 | 62 | SD_ioctl, |
willybayot | 0:4db361f2e6d5 | 63 | #endif /* _USE_IOCTL == 1 */ |
willybayot | 0:4db361f2e6d5 | 64 | }; |
willybayot | 0:4db361f2e6d5 | 65 | |
willybayot | 0:4db361f2e6d5 | 66 | /* Private functions ---------------------------------------------------------*/ |
willybayot | 0:4db361f2e6d5 | 67 | |
willybayot | 0:4db361f2e6d5 | 68 | /** |
willybayot | 0:4db361f2e6d5 | 69 | * @brief Initializes a Drive |
willybayot | 0:4db361f2e6d5 | 70 | * @param None |
willybayot | 0:4db361f2e6d5 | 71 | * @retval DSTATUS: Operation status |
willybayot | 0:4db361f2e6d5 | 72 | */ |
willybayot | 0:4db361f2e6d5 | 73 | DSTATUS SD_initialize(void) |
willybayot | 0:4db361f2e6d5 | 74 | { |
willybayot | 0:4db361f2e6d5 | 75 | Stat = STA_NOINIT; |
willybayot | 0:4db361f2e6d5 | 76 | |
willybayot | 0:4db361f2e6d5 | 77 | /* Configure the uSD device */ |
willybayot | 0:4db361f2e6d5 | 78 | if(BSP_SD_Init() == MSD_OK) |
willybayot | 0:4db361f2e6d5 | 79 | { |
willybayot | 0:4db361f2e6d5 | 80 | Stat &= ~STA_NOINIT; |
willybayot | 0:4db361f2e6d5 | 81 | } |
willybayot | 0:4db361f2e6d5 | 82 | |
willybayot | 0:4db361f2e6d5 | 83 | return Stat; |
willybayot | 0:4db361f2e6d5 | 84 | } |
willybayot | 0:4db361f2e6d5 | 85 | |
willybayot | 0:4db361f2e6d5 | 86 | /** |
willybayot | 0:4db361f2e6d5 | 87 | * @brief Gets Disk Status |
willybayot | 0:4db361f2e6d5 | 88 | * @param None |
willybayot | 0:4db361f2e6d5 | 89 | * @retval DSTATUS: Operation status |
willybayot | 0:4db361f2e6d5 | 90 | */ |
willybayot | 0:4db361f2e6d5 | 91 | DSTATUS SD_status(void) |
willybayot | 0:4db361f2e6d5 | 92 | { |
willybayot | 0:4db361f2e6d5 | 93 | Stat = STA_NOINIT; |
willybayot | 0:4db361f2e6d5 | 94 | |
willybayot | 0:4db361f2e6d5 | 95 | if(BSP_SD_GetStatus() == MSD_OK) |
willybayot | 0:4db361f2e6d5 | 96 | { |
willybayot | 0:4db361f2e6d5 | 97 | Stat &= ~STA_NOINIT; |
willybayot | 0:4db361f2e6d5 | 98 | } |
willybayot | 0:4db361f2e6d5 | 99 | |
willybayot | 0:4db361f2e6d5 | 100 | return Stat; |
willybayot | 0:4db361f2e6d5 | 101 | } |
willybayot | 0:4db361f2e6d5 | 102 | |
willybayot | 0:4db361f2e6d5 | 103 | /** |
willybayot | 0:4db361f2e6d5 | 104 | * @brief Reads Sector(s) |
willybayot | 0:4db361f2e6d5 | 105 | * @param *buff: Data buffer to store read data |
willybayot | 0:4db361f2e6d5 | 106 | * @param sector: Sector address (LBA) |
willybayot | 0:4db361f2e6d5 | 107 | * @param count: Number of sectors to read (1..128) |
willybayot | 0:4db361f2e6d5 | 108 | * @retval DRESULT: Operation result |
willybayot | 0:4db361f2e6d5 | 109 | */ |
willybayot | 0:4db361f2e6d5 | 110 | DRESULT SD_read(BYTE *buff, DWORD sector, BYTE count) |
willybayot | 0:4db361f2e6d5 | 111 | { |
willybayot | 0:4db361f2e6d5 | 112 | DRESULT res = RES_OK; |
willybayot | 0:4db361f2e6d5 | 113 | |
willybayot | 0:4db361f2e6d5 | 114 | if(BSP_SD_ReadBlocks((uint32_t*)buff, |
willybayot | 0:4db361f2e6d5 | 115 | (uint64_t) (sector * BLOCK_SIZE), |
willybayot | 0:4db361f2e6d5 | 116 | BLOCK_SIZE, |
willybayot | 0:4db361f2e6d5 | 117 | count) != MSD_OK) |
willybayot | 0:4db361f2e6d5 | 118 | { |
willybayot | 0:4db361f2e6d5 | 119 | res = RES_ERROR; |
willybayot | 0:4db361f2e6d5 | 120 | } |
willybayot | 0:4db361f2e6d5 | 121 | |
willybayot | 0:4db361f2e6d5 | 122 | return res; |
willybayot | 0:4db361f2e6d5 | 123 | } |
willybayot | 0:4db361f2e6d5 | 124 | |
willybayot | 0:4db361f2e6d5 | 125 | /** |
willybayot | 0:4db361f2e6d5 | 126 | * @brief Writes Sector(s) |
willybayot | 0:4db361f2e6d5 | 127 | * @param *buff: Data to be written |
willybayot | 0:4db361f2e6d5 | 128 | * @param sector: Sector address (LBA) |
willybayot | 0:4db361f2e6d5 | 129 | * @param count: Number of sectors to write (1..128) |
willybayot | 0:4db361f2e6d5 | 130 | * @retval DRESULT: Operation result |
willybayot | 0:4db361f2e6d5 | 131 | */ |
willybayot | 0:4db361f2e6d5 | 132 | #if _USE_WRITE == 1 |
willybayot | 0:4db361f2e6d5 | 133 | DRESULT SD_write(const BYTE *buff, DWORD sector, BYTE count) |
willybayot | 0:4db361f2e6d5 | 134 | { |
willybayot | 0:4db361f2e6d5 | 135 | DRESULT res = RES_OK; |
willybayot | 0:4db361f2e6d5 | 136 | |
willybayot | 0:4db361f2e6d5 | 137 | if(BSP_SD_WriteBlocks((uint32_t*)buff, |
willybayot | 0:4db361f2e6d5 | 138 | (uint64_t)(sector * BLOCK_SIZE), |
willybayot | 0:4db361f2e6d5 | 139 | BLOCK_SIZE, count) != MSD_OK) |
willybayot | 0:4db361f2e6d5 | 140 | { |
willybayot | 0:4db361f2e6d5 | 141 | res = RES_ERROR; |
willybayot | 0:4db361f2e6d5 | 142 | } |
willybayot | 0:4db361f2e6d5 | 143 | |
willybayot | 0:4db361f2e6d5 | 144 | return res; |
willybayot | 0:4db361f2e6d5 | 145 | } |
willybayot | 0:4db361f2e6d5 | 146 | #endif /* _USE_WRITE == 1 */ |
willybayot | 0:4db361f2e6d5 | 147 | |
willybayot | 0:4db361f2e6d5 | 148 | /** |
willybayot | 0:4db361f2e6d5 | 149 | * @brief I/O control operation |
willybayot | 0:4db361f2e6d5 | 150 | * @param cmd: Control code |
willybayot | 0:4db361f2e6d5 | 151 | * @param *buff: Buffer to send/receive control data |
willybayot | 0:4db361f2e6d5 | 152 | * @retval DRESULT: Operation result |
willybayot | 0:4db361f2e6d5 | 153 | */ |
willybayot | 0:4db361f2e6d5 | 154 | #if _USE_IOCTL == 1 |
willybayot | 0:4db361f2e6d5 | 155 | DRESULT SD_ioctl(BYTE cmd, void *buff) |
willybayot | 0:4db361f2e6d5 | 156 | { |
willybayot | 0:4db361f2e6d5 | 157 | DRESULT res = RES_ERROR; |
willybayot | 0:4db361f2e6d5 | 158 | SD_CardInfo CardInfo; |
willybayot | 0:4db361f2e6d5 | 159 | |
willybayot | 0:4db361f2e6d5 | 160 | if (Stat & STA_NOINIT) return RES_NOTRDY; |
willybayot | 0:4db361f2e6d5 | 161 | |
willybayot | 0:4db361f2e6d5 | 162 | switch (cmd) |
willybayot | 0:4db361f2e6d5 | 163 | { |
willybayot | 0:4db361f2e6d5 | 164 | /* Make sure that no pending write process */ |
willybayot | 0:4db361f2e6d5 | 165 | case CTRL_SYNC : |
willybayot | 0:4db361f2e6d5 | 166 | res = RES_OK; |
willybayot | 0:4db361f2e6d5 | 167 | break; |
willybayot | 0:4db361f2e6d5 | 168 | |
willybayot | 0:4db361f2e6d5 | 169 | /* Get number of sectors on the disk (DWORD) */ |
willybayot | 0:4db361f2e6d5 | 170 | case GET_SECTOR_COUNT : |
willybayot | 0:4db361f2e6d5 | 171 | BSP_SD_GetCardInfo(&CardInfo); |
willybayot | 0:4db361f2e6d5 | 172 | *(DWORD*)buff = CardInfo.CardCapacity / BLOCK_SIZE; |
willybayot | 0:4db361f2e6d5 | 173 | res = RES_OK; |
willybayot | 0:4db361f2e6d5 | 174 | break; |
willybayot | 0:4db361f2e6d5 | 175 | |
willybayot | 0:4db361f2e6d5 | 176 | /* Get R/W sector size (WORD) */ |
willybayot | 0:4db361f2e6d5 | 177 | case GET_SECTOR_SIZE : |
willybayot | 0:4db361f2e6d5 | 178 | *(WORD*)buff = BLOCK_SIZE; |
willybayot | 0:4db361f2e6d5 | 179 | res = RES_OK; |
willybayot | 0:4db361f2e6d5 | 180 | break; |
willybayot | 0:4db361f2e6d5 | 181 | |
willybayot | 0:4db361f2e6d5 | 182 | /* Get erase block size in unit of sector (DWORD) */ |
willybayot | 0:4db361f2e6d5 | 183 | case GET_BLOCK_SIZE : |
willybayot | 0:4db361f2e6d5 | 184 | *(DWORD*)buff = BLOCK_SIZE; |
willybayot | 0:4db361f2e6d5 | 185 | break; |
willybayot | 0:4db361f2e6d5 | 186 | |
willybayot | 0:4db361f2e6d5 | 187 | default: |
willybayot | 0:4db361f2e6d5 | 188 | res = RES_PARERR; |
willybayot | 0:4db361f2e6d5 | 189 | } |
willybayot | 0:4db361f2e6d5 | 190 | |
willybayot | 0:4db361f2e6d5 | 191 | return res; |
willybayot | 0:4db361f2e6d5 | 192 | } |
willybayot | 0:4db361f2e6d5 | 193 | #endif /* _USE_IOCTL == 1 */ |
willybayot | 0:4db361f2e6d5 | 194 | |
willybayot | 0:4db361f2e6d5 | 195 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |
willybayot | 0:4db361f2e6d5 | 196 |