ch_sdmmc_ff.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* ----------------------------------------------------------------------------
  2. * SAM Software Package License
  3. * ----------------------------------------------------------------------------
  4. * Copyright (c) 2015, Atmel Corporation
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the disclaimer below.
  13. *
  14. * Atmel's name may not be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  20. * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  23. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. * ----------------------------------------------------------------------------
  28. */
  29. /* ----------------------------------------------------------------------------
  30. * This file is based on the template source file named diskio.c,
  31. * part of the FatFs Module R0.10b:
  32. * Low level disk I/O module skeleton for FatFs (C)ChaN, 2014
  33. * If a working storage control module is available, it should be
  34. * attached to the FatFs via a glue function rather than modifying it.
  35. * This is an example of glue functions to attach various existing
  36. * storage control modules to the FatFs module with a defined API.
  37. * ----------------------------------------------------------------------------
  38. */
  39. #include "hal.h"
  40. #if (HAL_USE_SDMMC == TRUE)
  41. #include "sama_sdmmc_lld.h"
  42. #if SDMMC_USE_FF_LIB == 1
  43. #include "ccportab.h"
  44. #include "ffconf.h"
  45. #include "diskio.h"
  46. #include <string.h>
  47. #include "sama_sdmmc_lld.h"
  48. #include "ch_sdmmc_device.h"
  49. #include "ch_sdmmc_sd.h"
  50. #include "ch_sdmmc_sdio.h"
  51. #include "ch_sdmmc_trace.h"
  52. /*----------------------------------------------------------------------------
  53. * Definitions
  54. *----------------------------------------------------------------------------*/
  55. /**
  56. * \brief Access the SD/MMC Library instances owned by the application.
  57. * Used upon calls from the FatFs Module.
  58. *
  59. * Shall be implemented by the application.
  60. */
  61. extern bool CC_WEAK sdmmcGetInstance(uint8_t index, SdmmcDriver **sdmmcp) ;
  62. /*----------------------------------------------------------------------------
  63. * Exported functions
  64. *----------------------------------------------------------------------------*/
  65. /**
  66. * \brief Initialize a Drive.
  67. * \param slot Physical drive number (0..).
  68. * \return Drive status flags; STA_NOINIT if the specified drive does not exist.
  69. */
  70. DSTATUS disk_initialize(BYTE slot)
  71. {
  72. SdmmcDriver *sdmmcp = NULL;
  73. uint8_t rc;
  74. if (!sdmmcGetInstance(slot, &sdmmcp))
  75. return STA_NOINIT;
  76. rc = SD_GetStatus(sdmmcp);
  77. if (rc == SDMMC_NOT_SUPPORTED)
  78. return STA_NODISK | STA_NOINIT;
  79. #if 0
  80. if (sdmmcp->state != MCID_IDLE )
  81. sdmmc_device_deInit(sdmmcp);
  82. /* FIXME a delay with the bus held off may be required by the device */
  83. rc = sdmmc_device_start(sdmmcp);
  84. if (rc == SDMMC_OK) {
  85. rc = sdmmc_device_identify(sdmmcp);
  86. }
  87. #endif
  88. return rc == SDMMC_OK ? 0 : STA_NOINIT;
  89. }
  90. /**
  91. * \brief Get Drive Status.
  92. * \param slot Physical drive number (0..).
  93. * \return Drive status flags; STA_NODISK if there is currently no device in
  94. * the specified slot.
  95. */
  96. DSTATUS disk_status(BYTE slot)
  97. {
  98. SdmmcDriver *sdmmcp = NULL;
  99. uint8_t rc;
  100. if (!sdmmcGetInstance(slot, &sdmmcp))
  101. return STA_NODISK | STA_NOINIT;
  102. rc = SD_GetStatus(sdmmcp);
  103. if (rc == SDMMC_NOT_SUPPORTED)
  104. return STA_NODISK | STA_NOINIT;
  105. else if (rc != SDMMC_OK)
  106. return STA_NOINIT;
  107. /* Well, no restriction on this drive */
  108. return 0;
  109. }
  110. /**
  111. * \brief Read Sector(s).
  112. * \param slot Physical drive number (0..).
  113. * \param buff Data buffer to store read data.
  114. * \param sector Sector address in LBA.
  115. * \param count Number of sectors to read.
  116. * \return Result code; RES_OK if successful.
  117. */
  118. DRESULT disk_read(BYTE slot, BYTE* buff, DWORD sector, UINT count)
  119. {
  120. SdmmcDriver *sdmmcp = NULL;
  121. DRESULT res;
  122. uint32_t blk_size, addr = sector, len = count;
  123. uint8_t rc;
  124. if (!sdmmcGetInstance(slot, &sdmmcp))
  125. return RES_PARERR;
  126. blk_size = sdmmcp->card.wBlockSize;
  127. if (blk_size == 0)
  128. return RES_NOTRDY;
  129. if (blk_size < FF_MIN_SS) {
  130. if (FF_MIN_SS % blk_size)
  131. return RES_PARERR;
  132. addr = sector * (FF_MIN_SS / blk_size);
  133. len = count * (FF_MIN_SS / blk_size);
  134. }
  135. if (count <= 1)
  136. rc = SD_ReadBlocks(sdmmcp, addr, buff, len);
  137. else
  138. rc = SD_Read(sdmmcp, addr, buff, len);
  139. if (rc == SDMMC_OK || rc == SDMMC_CHANGED)
  140. res = RES_OK;
  141. else if (rc == SDMMC_ERR_IO || rc == SDMMC_ERR_RESP || rc == SDMMC_ERR)
  142. res = RES_ERROR;
  143. else if (rc == SDMMC_NO_RESPONSE || rc == SDMMC_BUSY
  144. || rc == SDMMC_NOT_INITIALIZED || rc == SDMMC_LOCKED
  145. || rc == SDMMC_STATE || rc == SDMMC_USER_CANCEL)
  146. res = RES_NOTRDY;
  147. else if (rc == SDMMC_PARAM || rc == SDMMC_NOT_SUPPORTED)
  148. res = RES_PARERR;
  149. else
  150. res = RES_ERROR;
  151. return res;
  152. }
  153. #if !FF_FS_READONLY
  154. /**
  155. * \brief Write Sector(s).
  156. *
  157. * \param slot Physical drive number (0..).
  158. * \param buff Data to be written.
  159. * \param sector Sector address in LBA.
  160. * \param count Number of sectors to write.
  161. * \return Result code; RES_OK if successful.
  162. *
  163. * \note The FatFs module will issue multiple sector transfer request
  164. * (count > 1) to the disk I/O layer. The disk function should process
  165. * the multiple sector transfer properly. Do not translate it into
  166. * multiple single sector transfers to the media, or the data read/write
  167. * performance may be drastically decreased.
  168. */
  169. DRESULT disk_write(BYTE slot, const BYTE* buff, DWORD sector, UINT count)
  170. {
  171. SdmmcDriver *sdmmcp = NULL;
  172. DRESULT res;
  173. uint32_t blk_size, addr = sector, len = count;
  174. uint8_t rc;
  175. if (!sdmmcGetInstance(slot, &sdmmcp))
  176. return RES_PARERR;
  177. blk_size = sdmmcp->card.wBlockSize;
  178. if (blk_size < FF_MIN_SS) {
  179. if (FF_MIN_SS % blk_size)
  180. return RES_PARERR;
  181. addr = sector * (FF_MIN_SS / blk_size);
  182. len = count * (FF_MIN_SS / blk_size);
  183. }
  184. if (count <= 1)
  185. rc = SD_WriteBlocks(sdmmcp, addr, buff, len);
  186. else
  187. rc = SD_Write(sdmmcp, addr, buff, len);
  188. if (rc == SDMMC_OK || rc == SDMMC_CHANGED)
  189. res = RES_OK;
  190. else if (rc == SDMMC_ERR_IO || rc == SDMMC_ERR_RESP || rc == SDMMC_ERR)
  191. res = RES_ERROR;
  192. else if (rc == SDMMC_NO_RESPONSE || rc == SDMMC_BUSY
  193. || rc == SDMMC_NOT_INITIALIZED || rc == SDMMC_LOCKED
  194. || rc == SDMMC_STATE || rc == SDMMC_USER_CANCEL)
  195. res = RES_NOTRDY;
  196. else if (rc == SDMMC_PARAM || rc == SDMMC_NOT_SUPPORTED)
  197. res = RES_PARERR;
  198. else
  199. res = RES_ERROR;
  200. return res;
  201. }
  202. #endif /* _FS_READONLY */
  203. /**
  204. * \brief Miscellaneous Functions.
  205. * \param slot Physical drive number (0..).
  206. * \param cmd Control code.
  207. * \param buff Buffer to send/receive control data.
  208. * \return Result code; RES_OK if successful.
  209. */
  210. DRESULT disk_ioctl(BYTE slot, BYTE cmd, void* buff)
  211. {
  212. SdmmcDriver *sdmmcp = NULL;
  213. DRESULT res;
  214. DWORD *param_u32 = (DWORD *)buff;
  215. WORD *param_u16 = (WORD *)buff;
  216. uint32_t blk_size, blk_count;
  217. if (!sdmmcGetInstance(slot, &sdmmcp))
  218. return RES_PARERR;
  219. switch (cmd)
  220. {
  221. case CTRL_SYNC:
  222. /* SD/MMC devices do not seem to cache data beyond completion
  223. * of the write commands. Note that if _FS_READONLY is enabled,
  224. * this command is not needed. */
  225. res = RES_OK;
  226. break;
  227. case GET_SECTOR_COUNT:
  228. if (!buff)
  229. return RES_PARERR;
  230. blk_size = sdmmcp->card.wBlockSize;
  231. blk_count = sdmmcp->card.dwNbBlocks;
  232. if (blk_size < FF_MIN_SS)
  233. {
  234. if (FF_MIN_SS % blk_size)
  235. return RES_PARERR;
  236. *param_u32 = blk_count / (FF_MIN_SS / blk_size);
  237. }
  238. else
  239. *param_u32 = blk_count;
  240. res = RES_OK;
  241. break;
  242. case GET_SECTOR_SIZE:
  243. /* Note that if _MAX_SS equals _MIN_SS i.e. the drive does not
  244. * have to support several sector sizes, this command is not
  245. * needed. */
  246. if (!buff)
  247. return RES_PARERR;
  248. blk_size = sdmmcp->card.wBlockSize;
  249. *param_u16 = blk_size >= FF_MIN_SS ? blk_size : FF_MIN_SS;
  250. res = RES_OK;
  251. break;
  252. case GET_BLOCK_SIZE:
  253. if (!buff)
  254. return RES_PARERR;
  255. /* On SD/MMC devices, erase block size is the same as write
  256. * block size.
  257. * Hence, erasing as little as one sector is allowed. */
  258. *param_u32 = 1;
  259. res = RES_OK;
  260. break;
  261. case CTRL_TRIM:
  262. /* This TRIM-like command is not implemented.
  263. * It would be required if _USE_TRIM was enabled. */
  264. res = RES_PARERR;
  265. break;
  266. default:
  267. res = RES_PARERR;
  268. break;
  269. }
  270. return res;
  271. }
  272. #endif
  273. #endif