From c54a4af55025593225ca3015de7b2d69047d1038 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?King=20K=C3=A9vin?= Date: Wed, 19 Jul 2017 23:27:54 +0200 Subject: [PATCH] application: implement fatfs example --- application.c | 220 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 220 insertions(+) diff --git a/application.c b/application.c index 97e6e0b..08536bf 100644 --- a/application.c +++ b/application.c @@ -44,6 +44,10 @@ #include "radio_gps.h" // GPS communication #include "flash_sdcard.h" // to read/write logs on SD card +/* FatFs library */ +#include "diskio.h" // disk access we have to implement to use FatFs +#include "ff.h" // FatFs library + #define WATCHDOG_PERIOD 10000 /**< watchdog period in ms */ /** @defgroup main_flags flag set in interrupts to be processed in main task @@ -168,6 +172,150 @@ error: return; } +#if !FF_FS_NORTC && !FF_FS_READONLY +/** get the current time + * @return current local time shall be returned as bit-fields packed into a DWORD value + * @note FatFs function to be implement by user + */ +DWORD get_fattime (void) +{ + time_rtc= rtc_get_counter_val(); // get time from internal RTC + time_tm = localtime(&time_rtc); // convert time + return ((1900+time_tm->tm_year-1800)<<25)+((time_tm->tm_mon)<<21)+((time_tm->tm_mday)<<16)+((time_tm->tm_hour)<<11)+((time_tm->tm_min)<<5)+((time_tm->tm_sec/2)<<0); // convert time to DWORD +} +#endif + +/** SD card status, used by FatFs */ +static DSTATUS flash_sdcard_status = STA_NOINIT; + +/** inquire the current drive status + * @param[in] pdrv physical drive number + * @return current drive status flags + * @note FatFs function to be implement by user + */ +DSTATUS disk_status (BYTE pdrv) +{ + if (0!=pdrv) { // drive 0 (sd card) is our only drive + return STA_NOINIT; + } + return flash_sdcard_status; +} + +/** initializes the storage device + * @param[in] pdrv physical drive number + * @return current drive status flags + * @note FatFs function to be implement by user + */ +DSTATUS disk_initialize (BYTE pdrv) +{ + if (0!=pdrv) { // drive 0 (sd card) is our only drive + return STA_NOINIT; + } + if (flash_sdcard_setup()) { + flash_sdcard_status &= ~(STA_NOINIT|STA_NODISK); // SD card initialized + } else { + flash_sdcard_status |= (STA_NOINIT|STA_NODISK); // SD card is not present or failed initialisation + } + return flash_sdcard_status; +} + +/** control device specific features and miscellaneous functions other than generic read/write + * @param[in] pdrv physical drive number + * @param[in] cmd control command code + * @param[io] buff pointer to the control data + * @return RES_* + * @note FatFs function to be implement by user + */ +DRESULT disk_ioctl (BYTE drv, BYTE cmd, void *buff) +{ + if (0!=drv) { // drive 0 (sd card) is our only drive + return RES_PARERR; + } + if (flash_sdcard_status&STA_NOINIT) { // SD card not initialized + return RES_NOTRDY; + } + + DRESULT to_return = RES_ERROR; + switch (cmd) { + case CTRL_SYNC: // nothing to flush (writes are complete) + to_return = RES_OK; + break; + case GET_SECTOR_COUNT: + *(DWORD*)buff = flash_sdcard_size()/512; + to_return = RES_OK; + break; + case GET_SECTOR_SIZE: + *(DWORD*)buff = 512; + to_return = RES_OK; + break; + case GET_BLOCK_SIZE: + *(DWORD*)buff = flash_sdcard_erase_size()/512; + to_return = RES_OK; + break; + default: + to_return = RES_ERROR; + } + return to_return;; +} + +/** read data from the sector(s) of storage device + * @param[in] pdrv physical drive number + * @param[out] buff pointer to the read data buffer + * @param[in] sector start sector number + * @param[in] count number of sectors to read + * @return RES_* + * @note FatFs function to be implement by user + */ +DRESULT disk_read (BYTE pdrv, BYTE* buff, DWORD sector, UINT count) +{ + if (0!=pdrv) { // drive 0 (sd card) is our only drive + return RES_PARERR; + } + if (flash_sdcard_status&STA_NOINIT) { // SD card not initialized + return RES_NOTRDY; + } + if (NULL==buff || 0==count) { // can't read no data + return RES_PARERR; + } + + for (UINT i=0; i