application: implement fatfs example
This commit is contained in:
parent
8014fd1a62
commit
c54a4af550
220
application.c
220
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<count; i++) {
|
||||
if (!flash_sdcard_read_data(sector+i, &((uint8_t*)buff)[i*512])) {
|
||||
return RES_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
/** write data to the sector(s) of storage device.
|
||||
* @param[in] pdrv physical drive number
|
||||
* @param[out] buff pointer to the data to be written
|
||||
* @param[in] sector sector number to write from
|
||||
* @param[in] count number of sectors to write
|
||||
* @return RES_*
|
||||
* @note FatFs function to be implement by user
|
||||
*/
|
||||
DRESULT disk_write (BYTE drv, const BYTE* buff, DWORD sector, UINT count)
|
||||
{
|
||||
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;
|
||||
}
|
||||
if (NULL==buff || 0==count) { // can't write no data
|
||||
return RES_PARERR;
|
||||
}
|
||||
|
||||
for (UINT i=0; i<count; i++) {
|
||||
if (!flash_sdcard_write_data(sector+i, &((uint8_t*)buff)[i*512])) {
|
||||
return RES_ERROR;
|
||||
}
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
/** program entry point
|
||||
* this is the firmware function started by the micro-controller
|
||||
*/
|
||||
|
@ -219,6 +367,7 @@ void main(void)
|
|||
radio_gps_setup();
|
||||
printf("OK\n");
|
||||
|
||||
/*
|
||||
// setup SD card
|
||||
printf("setup SD card: ");
|
||||
if (flash_sdcard_setup()) {
|
||||
|
@ -228,6 +377,77 @@ void main(void)
|
|||
}
|
||||
printf("card size: %U bytes\n", flash_sdcard_size());
|
||||
printf("erase size: %u bytes\n", flash_sdcard_erase_size());
|
||||
uint8_t data_in[512] = {0xaa};
|
||||
for (uint16_t i=0; i<sizeof(data_in); i++) {
|
||||
data_in[i]=0xaa;
|
||||
}
|
||||
uint8_t data_out[512] = {0x00};
|
||||
if (!flash_sdcard_write_data(10000, data_in)) {
|
||||
printf("could not write data\n");
|
||||
} else if (!flash_sdcard_read_data(10000, data_out)) {
|
||||
printf("could not read data\n");
|
||||
} else {
|
||||
bool prog_ok = true;
|
||||
for (uint16_t i=0; i<sizeof(data_in) && i<sizeof(data_out); i++) {
|
||||
prog_ok = (prog_ok&&(data_in[i]==data_out[i]));
|
||||
}
|
||||
if (prog_ok) {
|
||||
printf("programming succeeded\n");
|
||||
} else {
|
||||
printf("programming failed\n");
|
||||
}
|
||||
}
|
||||
*/
|
||||
|
||||
printf("setup SD card file system: ");
|
||||
FATFS card_fs;
|
||||
FRESULT result = f_mount(&card_fs, "", 0);
|
||||
if (FR_OK!=result) {
|
||||
printf("failed (result=%u)\n", result);
|
||||
} else {
|
||||
printf("OK\n");
|
||||
DIR directory;
|
||||
result = f_opendir(&directory, "");
|
||||
if (FR_OK!=result) {
|
||||
printf("failed to open directory (result=%u)\n", result);
|
||||
} else {
|
||||
printf("directory opened\n");
|
||||
FILINFO info;
|
||||
result = f_findfirst(&directory, &info, "", "*");
|
||||
if (FR_OK!=result) {
|
||||
printf("failed to find item (result=%u)\n", result);
|
||||
} else {
|
||||
printf("item found: %s\n", info.fname);
|
||||
FIL file;
|
||||
result = f_open(&file, info.fname, FA_READ);
|
||||
if (FR_OK!=result) {
|
||||
printf("failed to open file (result=%u)\n", result);
|
||||
} else {
|
||||
printf("file opened\n");
|
||||
printf("file content:\n");
|
||||
char line[100];
|
||||
while (f_gets(line, sizeof(line), &file)) {
|
||||
printf(line);
|
||||
}
|
||||
f_close(&file);
|
||||
FIL file2;
|
||||
result = f_open(&file2, info.fname, FA_OPEN_APPEND | FA_WRITE);
|
||||
if (FR_OK!=result) {
|
||||
printf("failed to open file for appending (result=%u)\n", result);
|
||||
} else {
|
||||
int chars = f_puts("hello me\n", &file2);
|
||||
if (-1==chars) {
|
||||
printf("failed to append file\n");
|
||||
} else {
|
||||
printf("file appended by %d characters\n", chars);
|
||||
}
|
||||
f_close(&file2);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// print time
|
||||
time_rtc= rtc_get_counter_val(); // get time from internal RTC
|
||||
|
|
Loading…
Reference in New Issue