diskio_impl.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
  3. /*-----------------------------------------------------------------------*/
  4. /* If a working storage control module is available, it should be */
  5. /* attached to the FatFs via a glue function rather than modifying it. */
  6. /* This is an example of glue functions to attach various exsisting */
  7. /* storage control modules to the FatFs module with a defined API. */
  8. /*-----------------------------------------------------------------------*/
  9. #include "luat_base.h"
  10. #include "luat_spi.h"
  11. #include "luat_timer.h"
  12. #include "luat_gpio.h"
  13. #include "lauxlib.h"
  14. #include "ff.h" /* Obtains integer types */
  15. #include "diskio.h" /* Declarations of disk functions */
  16. #define LUAT_LOG_TAG "luat.fatfs"
  17. #include "luat_log.h"
  18. BYTE FATFS_DEBUG = 0; // debug log, 0 -- disable , 1 -- enable
  19. static block_disk_t disks[FF_VOLUMES+1] = {0};
  20. DSTATUS disk_initialize (BYTE pdrv) {
  21. if (FATFS_DEBUG)
  22. LLOGD("disk_initialize >> %d", pdrv);
  23. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  24. LLOGD("NOTRDY >> %d", pdrv);
  25. return RES_NOTRDY;
  26. }
  27. return disks[pdrv].opts->initialize(disks[pdrv].userdata);
  28. }
  29. DSTATUS disk_status (BYTE pdrv) {
  30. if (FATFS_DEBUG)
  31. LLOGD("disk_status >> %d", pdrv);
  32. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  33. return RES_NOTRDY;
  34. }
  35. return disks[pdrv].opts->status(disks[pdrv].userdata);
  36. }
  37. DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count) {
  38. if (FATFS_DEBUG)
  39. LLOGD("disk_read >> %d", pdrv);
  40. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  41. return RES_NOTRDY;
  42. }
  43. return disks[pdrv].opts->read(disks[pdrv].userdata, buff, sector, count);
  44. }
  45. DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count) {
  46. //if (FATFS_DEBUG)
  47. // LLOGD("disk_write >> %d", pdrv);
  48. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  49. return RES_NOTRDY;
  50. }
  51. return disks[pdrv].opts->write(disks[pdrv].userdata, buff, sector, count);
  52. }
  53. DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff) {
  54. if (FATFS_DEBUG)
  55. LLOGD("disk_ioctl >> %d %d", pdrv, cmd);
  56. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  57. return RES_NOTRDY;
  58. }
  59. return disks[pdrv].opts->ioctl(disks[pdrv].userdata, cmd, buff);
  60. }
  61. DRESULT diskio_open(BYTE pdrv, block_disk_t * disk) {
  62. if (FATFS_DEBUG)
  63. LLOGD("disk_open >> %d", pdrv);
  64. if (pdrv >= FF_VOLUMES || disks[pdrv].opts != NULL) {
  65. return RES_NOTRDY;
  66. }
  67. disks[pdrv].opts = disk->opts;
  68. disks[pdrv].userdata = disk->userdata;
  69. return RES_OK;
  70. }
  71. #include "luat_malloc.h"
  72. DRESULT diskio_close(BYTE pdrv) {
  73. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  74. return RES_NOTRDY;
  75. }
  76. disks[pdrv].opts = NULL;
  77. if (disks[pdrv].userdata)
  78. luat_heap_free(disks[pdrv].userdata);
  79. disks[pdrv].userdata = NULL;
  80. return RES_OK;
  81. }
  82. DWORD get_fattime (void) {
  83. return ((2020UL-1980) << 25) /* Year = 2010 */
  84. | (1UL << 21) /* Month = 11 */
  85. | (1UL << 16) /* Day = 2 */
  86. | (1U << 11) /* Hour = 15 */
  87. | (0U << 5) /* Min = 0 */
  88. | (0U >> 1) /* Sec = 0 */
  89. ;
  90. }