luat_sdio_air101.c 4.9 KB

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