luat_sdio_air101.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. else if (id == 1) {
  22. wm_sdio_host_config(1);
  23. return 0;
  24. }
  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 0;
  95. }
  96. DSTATUS sdhc_sdio_status (
  97. luat_fatfs_sdio_t* userdata
  98. )
  99. {
  100. //if (drv) return STA_NOINIT;
  101. return 0;
  102. }
  103. DRESULT sdhc_sdio_read (
  104. luat_fatfs_sdio_t* userdata,
  105. BYTE *buff, /* Pointer to the data buffer to store read data */
  106. DWORD sector, /* Start sector number (LBA) */
  107. UINT count /* Sector count (1..128) */
  108. )
  109. {
  110. int ret = 0, i;
  111. int buflen = BLOCK_SIZE*count;
  112. BYTE *rdbuff = buff;
  113. if (((u32)buff)&0x3) /*non aligned 4*/
  114. {
  115. rdbuff = tls_mem_alloc(buflen);
  116. if (rdbuff == NULL)
  117. {
  118. return -1;
  119. }
  120. }
  121. for( i=0; i<TRY_COUNT; i++ )
  122. {
  123. if(count == 1)
  124. {
  125. ret = wm_sd_card_block_read(fs_rca, sector, (char *)rdbuff);
  126. }
  127. else if(count > 1)
  128. {
  129. ret = wm_sd_card_blocks_read(fs_rca, sector, (char *)rdbuff, buflen);
  130. }
  131. if( ret == 0 )
  132. break;
  133. }
  134. if(rdbuff != buff)
  135. {
  136. if(ret == 0)
  137. {
  138. memcpy(buff, rdbuff, buflen);
  139. }
  140. tls_mem_free(rdbuff);
  141. }
  142. return ret;
  143. }
  144. DRESULT sdhc_sdio_write (
  145. luat_fatfs_sdio_t* userdata,
  146. const BYTE *buff, /* Pointer to the data to be written */
  147. DWORD sector, /* Start sector number (LBA) */
  148. UINT count /* Sector count (1..128) */
  149. )
  150. {
  151. int ret = 0, i;
  152. int buflen = BLOCK_SIZE*count;
  153. BYTE *wrbuff = buff;
  154. if (((u32)buff)&0x3)
  155. {
  156. wrbuff = tls_mem_alloc(buflen);
  157. if (wrbuff == NULL) /*non aligned 4*/
  158. {
  159. return -1;
  160. }
  161. memcpy(wrbuff, buff, buflen);
  162. }
  163. for( i = 0; i < TRY_COUNT; i++ )
  164. {
  165. if(count == 1)
  166. {
  167. ret = wm_sd_card_block_write(fs_rca, sector, (char *)wrbuff);
  168. }
  169. else if(count > 1)
  170. {
  171. ret = wm_sd_card_blocks_write(fs_rca, sector, (char *)wrbuff, buflen);
  172. }
  173. if( ret == 0 )
  174. {
  175. break;
  176. }
  177. }
  178. if(wrbuff != buff)
  179. {
  180. tls_mem_free(wrbuff);
  181. }
  182. return ret;
  183. }
  184. DRESULT sdhc_sdio_ioctl (
  185. luat_fatfs_sdio_t* userdata,
  186. BYTE ctrl, /* Control code */
  187. void *buff /* Buffer to send/receive control data */
  188. )
  189. {
  190. int res;
  191. // Process of the command for the MMC/SD card
  192. switch(ctrl)
  193. {
  194. #if (FF_FS_READONLY == 0)
  195. case CTRL_SYNC:
  196. res = RES_OK;
  197. break;
  198. #endif
  199. case GET_SECTOR_COUNT:
  200. *(DWORD*)buff = SDCardInfo.CardCapacity / BLOCK_SIZE;
  201. res = RES_OK;
  202. break;
  203. case GET_SECTOR_SIZE:
  204. *(WORD*)buff = BLOCK_SIZE;
  205. res = RES_OK;
  206. break;
  207. case GET_BLOCK_SIZE:
  208. *(DWORD*)buff = SDCardInfo.CardBlockSize;
  209. res = RES_OK;
  210. break;
  211. #if (FF_USE_TRIM == 1)
  212. case CTRL_TRIM:
  213. break;
  214. #endif
  215. default:
  216. break;
  217. }
  218. return res;
  219. return 0;
  220. }
  221. static const block_disk_opts_t sdhc_sdio_disk_opts = {
  222. .initialize = sdhc_sdio_initialize,
  223. .status = sdhc_sdio_status,
  224. .read = sdhc_sdio_read,
  225. .write = sdhc_sdio_write,
  226. .ioctl = sdhc_sdio_ioctl,
  227. };
  228. void luat_sdio_set_sdhc_ctrl(block_disk_t *disk){
  229. disk->opts = &sdhc_sdio_disk_opts;
  230. }