diskio_impl.c 3.3 KB

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