diskio_impl.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module SKELETON for FatFs (C)ChaN, 2019 */
  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. #ifdef __LUATOS__
  14. #include "lauxlib.h"
  15. #endif
  16. #include "ff.h" /* Obtains integer types */
  17. #include "diskio.h" /* Declarations of disk functions */
  18. #define LUAT_LOG_TAG "luat.fatfs"
  19. #include "luat_log.h"
  20. uint16_t FATFS_POWER_DELAY = 1;
  21. BYTE FATFS_DEBUG = 0; // debug log, 0 -- disable , 1 -- enable
  22. BYTE FATFS_POWER_PIN = 0xff;
  23. uint8_t FATFS_NO_CRC_CHECK = 0;
  24. uint16_t FATFS_WRITE_TO = 100;
  25. static block_disk_t disks[FF_VOLUMES+1] = {0};
  26. DSTATUS disk_initialize (BYTE pdrv) {
  27. if (FATFS_DEBUG)
  28. LLOGD("disk_initialize >> %d", pdrv);
  29. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  30. LLOGD("NOTRDY >> %d", pdrv);
  31. return RES_NOTRDY;
  32. }
  33. if (FATFS_POWER_PIN != 0xff)
  34. {
  35. luat_gpio_mode(FATFS_POWER_PIN, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, 0);
  36. luat_timer_mdelay(FATFS_POWER_DELAY);
  37. luat_gpio_mode(FATFS_POWER_PIN, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, 1);
  38. }
  39. return disks[pdrv].opts->initialize(disks[pdrv].userdata);
  40. }
  41. DSTATUS disk_status (BYTE pdrv) {
  42. if (FATFS_DEBUG)
  43. LLOGD("disk_status >> %d", pdrv);
  44. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  45. return RES_NOTRDY;
  46. }
  47. return disks[pdrv].opts->status(disks[pdrv].userdata);
  48. }
  49. DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count) {
  50. if (FATFS_DEBUG)
  51. LLOGD("disk_read >> %d", pdrv);
  52. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  53. return RES_NOTRDY;
  54. }
  55. return disks[pdrv].opts->read(disks[pdrv].userdata, buff, sector, count);
  56. }
  57. DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count) {
  58. //if (FATFS_DEBUG)
  59. // LLOGD("disk_write >> %d", pdrv);
  60. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  61. return RES_NOTRDY;
  62. }
  63. return disks[pdrv].opts->write(disks[pdrv].userdata, buff, sector, count);
  64. }
  65. DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff) {
  66. if (FATFS_DEBUG)
  67. LLOGD("disk_ioctl >> %d %d", pdrv, cmd);
  68. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  69. return RES_NOTRDY;
  70. }
  71. return disks[pdrv].opts->ioctl(disks[pdrv].userdata, cmd, buff);
  72. }
  73. DRESULT diskio_open(BYTE pdrv, block_disk_t * disk) {
  74. if (FATFS_DEBUG)
  75. LLOGD("disk_open >> %d", pdrv);
  76. if (pdrv >= FF_VOLUMES || disks[pdrv].opts != NULL) {
  77. return RES_NOTRDY;
  78. }
  79. disks[pdrv].opts = disk->opts;
  80. disks[pdrv].userdata = disk->userdata;
  81. return RES_OK;
  82. }
  83. #include "luat_mem.h"
  84. DRESULT diskio_close(BYTE pdrv) {
  85. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  86. return RES_NOTRDY;
  87. }
  88. disks[pdrv].opts = NULL;
  89. if (disks[pdrv].userdata)
  90. luat_heap_free(disks[pdrv].userdata);
  91. disks[pdrv].userdata = NULL;
  92. return RES_OK;
  93. }
  94. #include "time.h"
  95. DWORD get_fattime (void) {
  96. struct tm *tm;
  97. time_t t;
  98. time(&t);
  99. tm = gmtime(&t);
  100. return ( ((tm->tm_year-80) & 0x3F) << 25)
  101. | ((tm->tm_mon + 1) << 21)
  102. | (tm->tm_mday << 16)
  103. | (tm->tm_hour << 11)
  104. | (tm->tm_min << 5)
  105. | ((tm->tm_sec % 60) >> 1)
  106. ;
  107. }