luat_sdio_air101.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. #include "luat_base.h"
  2. #include "luat_sdio.h"
  3. #define LUAT_LOG_TAG "sdio"
  4. #include "luat_log.h"
  5. #include "wm_include.h"
  6. #include "wm_gpio_afsel.h"
  7. #include "wm_sdio_host.h"
  8. #include "luat_fs.h"
  9. #include "ff.h"
  10. #include "diskio.h"
  11. FATFS fs;
  12. BYTE work[FF_MAX_SS];
  13. #ifdef LUAT_USE_FS_VFS
  14. extern const struct luat_vfs_filesystem vfs_fs_fatfs;
  15. #endif
  16. int luat_sdio_init(int id){
  17. if (id == 0) {
  18. wm_sdio_host_config(0);
  19. return 0;
  20. }
  21. // #ifdef AIR103
  22. else if (id == 1) {
  23. wm_sdio_host_config(1);
  24. return 0;
  25. }
  26. // #endif
  27. return -1;
  28. }
  29. int luat_sdio_sd_read(int id, int rca, char* buff, size_t offset, size_t len){
  30. return wm_sd_card_blocks_read(rca, offset, buff, len);
  31. }
  32. int luat_sdio_sd_write(int id, int rca, char* buff, size_t offset, size_t len){
  33. return wm_sd_card_blocks_write(rca, offset, buff, len);
  34. }
  35. int luat_sdio_sd_mount(int id, int *rca, char* path,int auto_format){
  36. FRESULT res_sd;
  37. res_sd = f_mount(&fs, "/", 1);
  38. if (res_sd) {
  39. LLOGD("mount ret = %d", res_sd);
  40. if (res_sd == FR_NO_FILESYSTEM && auto_format) {
  41. res_sd = f_mkfs("/", 0, work, sizeof(work));
  42. if(res_sd == FR_OK){
  43. res_sd = f_mount(NULL, "/", 1);
  44. res_sd = f_mount(&fs, "/", 1);
  45. }
  46. else {
  47. LLOGD("format ret = %d", res_sd);
  48. }
  49. }
  50. }
  51. if (res_sd != FR_OK) {
  52. return res_sd;
  53. }
  54. #ifdef LUAT_USE_FS_VFS
  55. luat_vfs_reg(&vfs_fs_fatfs);
  56. luat_fs_conf_t conf = {
  57. .busname = &fs,
  58. .type = "fatfs",
  59. .filesystem = "fatfs",
  60. .mount_point = path,
  61. };
  62. luat_fs_mount(&conf);
  63. #endif
  64. return 0;
  65. }
  66. int luat_sdio_sd_unmount(int id, int rca){
  67. return f_mount(NULL, "/", 1);
  68. }
  69. int luat_sdio_sd_format(int id, int rca){
  70. f_mkfs("/", 0, work, sizeof(work));
  71. return 0;
  72. }
  73. #include "wm_sdio_host.h"
  74. #include <string.h>
  75. #include "wm_include.h"
  76. extern int wm_sd_card_set_blocklen(uint32_t blocklen);
  77. #define BLOCK_SIZE 512
  78. #define TRY_COUNT 10
  79. static uint32_t fs_rca;
  80. DSTATUS sdhc_sdio_initialize (
  81. luat_fatfs_sdio_t* userdata
  82. )
  83. {
  84. int ret;
  85. luat_sdio_init(0);
  86. ret= sdh_card_init(&fs_rca);
  87. if(ret)
  88. goto end;
  89. ret = wm_sd_card_set_bus_width(fs_rca, 2);
  90. if(ret)
  91. goto end;
  92. ret = wm_sd_card_set_blocklen(BLOCK_SIZE); //512
  93. if(ret)
  94. goto end;
  95. end:
  96. return ret;
  97. return 0;
  98. }
  99. DSTATUS sdhc_sdio_status (
  100. luat_fatfs_sdio_t* userdata
  101. )
  102. {
  103. //if (drv) return STA_NOINIT;
  104. return 0;
  105. }
  106. DRESULT sdhc_sdio_read (
  107. luat_fatfs_sdio_t* userdata,
  108. BYTE *buff, /* Pointer to the data buffer to store read data */
  109. DWORD sector, /* Start sector number (LBA) */
  110. UINT count /* Sector count (1..128) */
  111. )
  112. {
  113. int ret, i;
  114. int buflen = BLOCK_SIZE*count;
  115. BYTE *rdbuff = buff;
  116. if (((u32)buff)&0x3) /*non aligned 4*/
  117. {
  118. rdbuff = tls_mem_alloc(buflen);
  119. if (rdbuff == NULL)
  120. {
  121. return -1;
  122. }
  123. }
  124. for( i=0; i<TRY_COUNT; i++ )
  125. {
  126. if(count == 1)
  127. {
  128. ret = wm_sd_card_block_read(fs_rca, sector, (char *)rdbuff);
  129. }
  130. else if(count > 1)
  131. {
  132. ret = wm_sd_card_blocks_read(fs_rca, sector, (char *)rdbuff, buflen);
  133. }
  134. if( ret == 0 )
  135. break;
  136. }
  137. if(rdbuff != buff)
  138. {
  139. if(ret == 0)
  140. {
  141. memcpy(buff, rdbuff, buflen);
  142. }
  143. tls_mem_free(rdbuff);
  144. }
  145. return ret;
  146. return 0;
  147. }
  148. DRESULT sdhc_sdio_write (
  149. luat_fatfs_sdio_t* userdata,
  150. const BYTE *buff, /* Pointer to the data to be written */
  151. DWORD sector, /* Start sector number (LBA) */
  152. UINT count /* Sector count (1..128) */
  153. )
  154. {
  155. int ret, i;
  156. int buflen = BLOCK_SIZE*count;
  157. BYTE *wrbuff = buff;
  158. if (((u32)buff)&0x3)
  159. {
  160. wrbuff = tls_mem_alloc(buflen);
  161. if (wrbuff == NULL) /*non aligned 4*/
  162. {
  163. return -1;
  164. }
  165. memcpy(wrbuff, buff, buflen);
  166. }
  167. for( i = 0; i < TRY_COUNT; i++ )
  168. {
  169. if(count == 1)
  170. {
  171. ret = wm_sd_card_block_write(fs_rca, sector, (char *)wrbuff);
  172. }
  173. else if(count > 1)
  174. {
  175. ret = wm_sd_card_blocks_write(fs_rca, sector, (char *)wrbuff, buflen);
  176. }
  177. if( ret == 0 )
  178. {
  179. break;
  180. }
  181. }
  182. if(wrbuff != buff)
  183. {
  184. tls_mem_free(wrbuff);
  185. }
  186. return ret;
  187. return 0;
  188. }
  189. DRESULT sdhc_sdio_ioctl (
  190. luat_fatfs_sdio_t* userdata,
  191. BYTE ctrl, /* Control code */
  192. void *buff /* Buffer to send/receive control data */
  193. )
  194. {
  195. int res;
  196. // Process of the command for the MMC/SD card
  197. switch(ctrl)
  198. {
  199. #if (FF_FS_READONLY == 0)
  200. case CTRL_SYNC:
  201. res = RES_OK;
  202. break;
  203. #endif
  204. case GET_SECTOR_COUNT:
  205. *(DWORD*)buff = SDCardInfo.CardCapacity / BLOCK_SIZE;
  206. res = RES_OK;
  207. break;
  208. case GET_SECTOR_SIZE:
  209. *(WORD*)buff = BLOCK_SIZE;
  210. res = RES_OK;
  211. break;
  212. case GET_BLOCK_SIZE:
  213. *(DWORD*)buff = SDCardInfo.CardBlockSize;
  214. res = RES_OK;
  215. break;
  216. #if (FF_USE_TRIM == 1)
  217. case CTRL_TRIM:
  218. break;
  219. #endif
  220. default:
  221. break;
  222. }
  223. return res;
  224. return 0;
  225. }
  226. static const block_disk_opts_t sdhc_sdio_disk_opts = {
  227. .initialize = sdhc_sdio_initialize,
  228. .status = sdhc_sdio_status,
  229. .read = sdhc_sdio_read,
  230. .write = sdhc_sdio_write,
  231. .ioctl = sdhc_sdio_ioctl,
  232. };
  233. void luat_sdio_set_sdhc_ctrl(block_disk_t *disk){
  234. disk->opts = &sdhc_sdio_disk_opts;
  235. }