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