diskio.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*-----------------------------------------------------------------------*/
  2. /* Low level disk I/O module skeleton for FatFs (C)ChaN, 2016 */
  3. /* ESP-IDF port Copyright 2016 Espressif Systems (Shanghai) PTE LTD */
  4. /*-----------------------------------------------------------------------*/
  5. /* If a working storage control module is available, it should be */
  6. /* attached to the FatFs via a glue function rather than modifying it. */
  7. /* This is an example of glue functions to attach various exsisting */
  8. /* storage control modules to the FatFs module with a defined API. */
  9. /*-----------------------------------------------------------------------*/
  10. #include <string.h>
  11. #include <time.h>
  12. #include <stdlib.h>
  13. #include <sys/time.h>
  14. #include "diskio_impl.h"
  15. #include "ffconf.h"
  16. #include "ff.h"
  17. static ff_diskio_impl_t * s_impls[FF_VOLUMES] = { NULL };
  18. #if FF_MULTI_PARTITION /* Multiple partition configuration */
  19. const PARTITION VolToPart[FF_VOLUMES] = {
  20. {0, 0}, /* Logical drive 0 ==> Physical drive 0, auto detection */
  21. #if FF_VOLUMES > 1
  22. {1, 0}, /* Logical drive 1 ==> Physical drive 1, auto detection */
  23. #endif
  24. #if FF_VOLUMES > 2
  25. {2, 0}, /* Logical drive 2 ==> Physical drive 2, auto detection */
  26. #endif
  27. #if FF_VOLUMES > 3
  28. {3, 0}, /* Logical drive 3 ==> Physical drive 3, auto detection */
  29. #endif
  30. #if FF_VOLUMES > 4
  31. {4, 0}, /* Logical drive 4 ==> Physical drive 4, auto detection */
  32. #endif
  33. #if FF_VOLUMES > 5
  34. {5, 0}, /* Logical drive 5 ==> Physical drive 5, auto detection */
  35. #endif
  36. #if FF_VOLUMES > 6
  37. {6, 0}, /* Logical drive 6 ==> Physical drive 6, auto detection */
  38. #endif
  39. #if FF_VOLUMES > 7
  40. {7, 0}, /* Logical drive 7 ==> Physical drive 7, auto detection */
  41. #endif
  42. #if FF_VOLUMES > 8
  43. {8, 0}, /* Logical drive 8 ==> Physical drive 8, auto detection */
  44. #endif
  45. #if FF_VOLUMES > 9
  46. {9, 0}, /* Logical drive 9 ==> Physical drive 9, auto detection */
  47. #endif
  48. };
  49. #endif
  50. esp_err_t ff_diskio_get_drive(BYTE* out_pdrv)
  51. {
  52. BYTE i;
  53. for(i=0; i<FF_VOLUMES; i++) {
  54. if (!s_impls[i]) {
  55. *out_pdrv = i;
  56. return ESP_OK;
  57. }
  58. }
  59. return ESP_ERR_NOT_FOUND;
  60. }
  61. void ff_diskio_register(BYTE pdrv, const ff_diskio_impl_t* discio_impl)
  62. {
  63. assert(pdrv < FF_VOLUMES);
  64. if (s_impls[pdrv]) {
  65. ff_diskio_impl_t* im = s_impls[pdrv];
  66. s_impls[pdrv] = NULL;
  67. free(im);
  68. }
  69. if (!discio_impl) {
  70. return;
  71. }
  72. ff_diskio_impl_t * impl = (ff_diskio_impl_t *)malloc(sizeof(ff_diskio_impl_t));
  73. assert(impl != NULL);
  74. memcpy(impl, discio_impl, sizeof(ff_diskio_impl_t));
  75. s_impls[pdrv] = impl;
  76. }
  77. DSTATUS ff_disk_initialize (BYTE pdrv)
  78. {
  79. return s_impls[pdrv]->init(pdrv);
  80. }
  81. DSTATUS ff_disk_status (BYTE pdrv)
  82. {
  83. return s_impls[pdrv]->status(pdrv);
  84. }
  85. DRESULT ff_disk_read (BYTE pdrv, BYTE* buff, LBA_t sector, UINT count)
  86. {
  87. return s_impls[pdrv]->read(pdrv, buff, sector, count);
  88. }
  89. DRESULT ff_disk_write (BYTE pdrv, const BYTE* buff, LBA_t sector, UINT count)
  90. {
  91. return s_impls[pdrv]->write(pdrv, buff, sector, count);
  92. }
  93. DRESULT ff_disk_ioctl (BYTE pdrv, BYTE cmd, void* buff)
  94. {
  95. return s_impls[pdrv]->ioctl(pdrv, cmd, buff);
  96. }
  97. DWORD get_fattime(void)
  98. {
  99. time_t t = time(NULL);
  100. struct tm tmr;
  101. localtime_r(&t, &tmr);
  102. int year = tmr.tm_year < 80 ? 0 : tmr.tm_year - 80;
  103. return ((DWORD)(year) << 25)
  104. | ((DWORD)(tmr.tm_mon + 1) << 21)
  105. | ((DWORD)tmr.tm_mday << 16)
  106. | (WORD)(tmr.tm_hour << 11)
  107. | (WORD)(tmr.tm_min << 5)
  108. | (WORD)(tmr.tm_sec >> 1);
  109. }