diskio_impl.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. static block_disk_t disks[FF_VOLUMES+1] = {0};
  24. DSTATUS disk_initialize (BYTE pdrv) {
  25. if (FATFS_DEBUG)
  26. LLOGD("disk_initialize >> %d", pdrv);
  27. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  28. LLOGD("NOTRDY >> %d", pdrv);
  29. return RES_NOTRDY;
  30. }
  31. if (FATFS_POWER_PIN != 0xff)
  32. {
  33. luat_gpio_mode(FATFS_POWER_PIN, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, 0);
  34. luat_timer_mdelay(FATFS_POWER_DELAY);
  35. luat_gpio_mode(FATFS_POWER_PIN, Luat_GPIO_OUTPUT, Luat_GPIO_DEFAULT, 1);
  36. }
  37. return disks[pdrv].opts->initialize(disks[pdrv].userdata);
  38. }
  39. DSTATUS disk_status (BYTE pdrv) {
  40. if (FATFS_DEBUG)
  41. LLOGD("disk_status >> %d", pdrv);
  42. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  43. return RES_NOTRDY;
  44. }
  45. return disks[pdrv].opts->status(disks[pdrv].userdata);
  46. }
  47. DRESULT disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count) {
  48. if (FATFS_DEBUG)
  49. LLOGD("disk_read >> %d", pdrv);
  50. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  51. return RES_NOTRDY;
  52. }
  53. return disks[pdrv].opts->read(disks[pdrv].userdata, buff, sector, count);
  54. }
  55. DRESULT disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count) {
  56. //if (FATFS_DEBUG)
  57. // LLOGD("disk_write >> %d", pdrv);
  58. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  59. return RES_NOTRDY;
  60. }
  61. return disks[pdrv].opts->write(disks[pdrv].userdata, buff, sector, count);
  62. }
  63. DRESULT disk_ioctl (BYTE pdrv, BYTE cmd, void* buff) {
  64. if (FATFS_DEBUG)
  65. LLOGD("disk_ioctl >> %d %d", pdrv, cmd);
  66. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  67. return RES_NOTRDY;
  68. }
  69. return disks[pdrv].opts->ioctl(disks[pdrv].userdata, cmd, buff);
  70. }
  71. DRESULT diskio_open(BYTE pdrv, block_disk_t * disk) {
  72. if (FATFS_DEBUG)
  73. LLOGD("disk_open >> %d", pdrv);
  74. if (pdrv >= FF_VOLUMES || disks[pdrv].opts != NULL) {
  75. return RES_NOTRDY;
  76. }
  77. disks[pdrv].opts = disk->opts;
  78. disks[pdrv].userdata = disk->userdata;
  79. return RES_OK;
  80. }
  81. #include "luat_mem.h"
  82. DRESULT diskio_close(BYTE pdrv) {
  83. if (pdrv >= FF_VOLUMES || disks[pdrv].opts == NULL) {
  84. return RES_NOTRDY;
  85. }
  86. disks[pdrv].opts = NULL;
  87. if (disks[pdrv].userdata)
  88. luat_heap_free(disks[pdrv].userdata);
  89. disks[pdrv].userdata = NULL;
  90. return RES_OK;
  91. }
  92. DWORD get_fattime (void) {
  93. return ((2020UL-1980) << 25) /* Year = 2010 */
  94. | (1UL << 21) /* Month = 11 */
  95. | (1UL << 16) /* Day = 2 */
  96. | (1U << 11) /* Hour = 15 */
  97. | (0U << 5) /* Min = 0 */
  98. | (0U >> 1) /* Sec = 0 */
  99. ;
  100. }