diskio.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. PARTITION VolToPart[] = {
  20. {0, 0}, /* Logical drive 0 ==> Physical drive 0, auto detection */
  21. {1, 0} /* Logical drive 1 ==> Physical drive 1, auto detection */
  22. };
  23. #endif
  24. esp_err_t ff_diskio_get_drive(BYTE* out_pdrv)
  25. {
  26. BYTE i;
  27. for(i=0; i<FF_VOLUMES; i++) {
  28. if (!s_impls[i]) {
  29. *out_pdrv = i;
  30. return ESP_OK;
  31. }
  32. }
  33. return ESP_ERR_NOT_FOUND;
  34. }
  35. void ff_diskio_register(BYTE pdrv, const ff_diskio_impl_t* discio_impl)
  36. {
  37. assert(pdrv < FF_VOLUMES);
  38. if (s_impls[pdrv]) {
  39. ff_diskio_impl_t* im = s_impls[pdrv];
  40. s_impls[pdrv] = NULL;
  41. free(im);
  42. }
  43. if (!discio_impl) {
  44. return;
  45. }
  46. ff_diskio_impl_t * impl = (ff_diskio_impl_t *)malloc(sizeof(ff_diskio_impl_t));
  47. assert(impl != NULL);
  48. memcpy(impl, discio_impl, sizeof(ff_diskio_impl_t));
  49. s_impls[pdrv] = impl;
  50. }
  51. DSTATUS ff_disk_initialize (BYTE pdrv)
  52. {
  53. return s_impls[pdrv]->init(pdrv);
  54. }
  55. DSTATUS ff_disk_status (BYTE pdrv)
  56. {
  57. return s_impls[pdrv]->status(pdrv);
  58. }
  59. DRESULT ff_disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count)
  60. {
  61. return s_impls[pdrv]->read(pdrv, buff, sector, count);
  62. }
  63. DRESULT ff_disk_write (BYTE pdrv, const BYTE* buff, DWORD sector, UINT count)
  64. {
  65. return s_impls[pdrv]->write(pdrv, buff, sector, count);
  66. }
  67. DRESULT ff_disk_ioctl (BYTE pdrv, BYTE cmd, void* buff)
  68. {
  69. return s_impls[pdrv]->ioctl(pdrv, cmd, buff);
  70. }
  71. DWORD get_fattime(void)
  72. {
  73. time_t t = time(NULL);
  74. struct tm tmr;
  75. localtime_r(&t, &tmr);
  76. int year = tmr.tm_year < 80 ? 0 : tmr.tm_year - 80;
  77. return ((DWORD)(year) << 25)
  78. | ((DWORD)(tmr.tm_mon + 1) << 21)
  79. | ((DWORD)tmr.tm_mday << 16)
  80. | (WORD)(tmr.tm_hour << 11)
  81. | (WORD)(tmr.tm_min << 5)
  82. | (WORD)(tmr.tm_sec >> 1);
  83. }