This repository has been archived on 2023-07-17. You can view files and clone it, but cannot push or open issues or pull requests.
bl_mcu_sdk/examples/fatfs/main.c

236 lines
6.9 KiB
C
Raw Normal View History

#include "bflb_mtimer.h"
#include "board.h"
#include "vlibc_stdio.h"
#include "ff.h"
#include "log.h"
FATFS fs;
__attribute((aligned(8))) static uint32_t workbuf[4096];
MKFS_PARM fs_para = {
.fmt = FM_FAT32, /* Format option (FM_FAT, FM_FAT32, FM_EXFAT and FM_SFD) */
.n_fat = 1, /* Number of FATs */
.align = 0, /* Data area alignment (sector) */
.n_root = 1, /* Number of root directory entries */
.au_size = 512 * 32, /* Cluster size (byte) */
};
void filesystem_init(void)
{
FRESULT ret;
board_sdh_gpio_init();
extern void fatfs_sdh_driver_register(void);
fatfs_sdh_driver_register();
ret = f_mount(&fs, "sd:/", 1);
if (ret == FR_NO_FILESYSTEM) {
LOG_W("No filesystem yet, try to be formatted...\r\n");
ret = f_mkfs("sd:/", &fs_para, workbuf, sizeof(workbuf));
if (ret != FR_OK) {
LOG_F("fail to make filesystem\r\n");
_CALL_ERROR();
}
if (ret == FR_OK) {
LOG_I("done with formatting.\r\n");
LOG_I("first start to unmount.\r\n");
ret = f_mount(NULL, "sd:/", 1);
LOG_I("then start to remount.\r\n");
}
} else if (ret != FR_OK) {
LOG_F("fail to mount filesystem,error= %d\r\n", ret);
LOG_F("SD card might fail to initialise.\r\n");
_CALL_ERROR();
} else {
LOG_D("Succeed to mount filesystem\r\n");
}
if (ret == FR_OK) {
LOG_I("FileSystem cluster size:%d-sectors (%d-Byte)\r\n", fs.csize, fs.csize * 512);
}
}
2022-12-08 11:03:19 +08:00
#define SDU_DATA_CHECK 1
char test_data[] =
"I've been reading books of old \r\n\
The legends and the myths \r\n\
Achilles and his gold \r\n\
Hercules and his gifts \r\n\
Spiderman's control \r\n\
And Batman with his fists\r\n\
And clearly I don't see myself upon that list\r\n\
But she said, where'd you wanna go?\r\n\
How much you wanna risk?\r\n\
I'm not looking for somebody\r\n\
With some superhuman gifts\r\n\
Some superhero\r\n\
Some fairytale bliss\r\n\
Just something I can turn to\r\n\
Somebody I can kiss\r\n\
I want something just like this\r\n\r\n";
BYTE RW_Buffer[32 * 1024] = { 0 };
#if SDU_DATA_CHECK
BYTE Check_Buffer[sizeof(RW_Buffer)] = { 0 };
#endif
void fatfs_write_read_test()
{
FRESULT ret;
FIL fnew;
UINT fnum;
uint32_t time_node, i, j;
/* full test data to buff */
for (uint32_t size = 0; size < (sizeof(RW_Buffer) - sizeof(test_data)); size += sizeof(test_data)) {
memcpy(&RW_Buffer[size], test_data, sizeof(test_data));
#if SDU_DATA_CHECK
memcpy(&Check_Buffer[size], test_data, sizeof(test_data));
#endif
}
/* write test */
LOG_I("\r\n******************** be about to write test... **********************\r\n");
ret = f_open(&fnew, "sd:test_file.txt", FA_CREATE_ALWAYS | FA_WRITE);
if (ret == FR_OK) {
time_node = (uint32_t)bflb_mtimer_get_time_ms();
/*write into file*/
ret = f_write(&fnew, RW_Buffer, 1024, &fnum);
for (i = 0; i < 1024; i++) {
ret = f_write(&fnew, RW_Buffer, sizeof(RW_Buffer), &fnum);
if (ret) {
break;
}
}
/* close file */
ret |= f_close(&fnew);
/* get time */
time_node = (uint32_t)bflb_mtimer_get_time_ms() - time_node;
if (ret == FR_OK) {
LOG_I("Write Test Succeed! \r\n");
LOG_I("Single data size:%d Byte, Write the number:%d, Total size:%d KB\r\n", sizeof(RW_Buffer), i, sizeof(RW_Buffer) * i >> 10);
LOG_I("Time:%dms, Write Speed:%d KB/s \r\n", time_node, ((sizeof(RW_Buffer) * i) >> 10) * 1000 / time_node);
} else {
LOG_F("Fail to write files(%d) num:%d\n", ret, i);
return;
}
} else {
LOG_F("Fail to open or create files.\r\n");
return;
}
/* read test */
LOG_I("\r\n******************** be about to read test... **********************\r\n");
ret = f_open(&fnew, "sd:test_file.txt", FA_OPEN_EXISTING | FA_READ);
if (ret == FR_OK) {
time_node = (uint32_t)bflb_mtimer_get_time_ms();
ret = f_read(&fnew, RW_Buffer, 1024, &fnum);
for (i = 0; i < 1024; i++) {
ret = f_read(&fnew, RW_Buffer, sizeof(RW_Buffer), &fnum);
if (ret) {
break;
}
}
/* close file */
ret |= f_close(&fnew);
/* get time */
time_node = (uint32_t)bflb_mtimer_get_time_ms() - time_node;
if (ret == FR_OK) {
LOG_I("Read Test Succeed! \r\n");
LOG_I("Single data size:%dByte, Read the number:%d, Total size:%d KB\r\n", sizeof(RW_Buffer), i, sizeof(RW_Buffer) * i >> 10);
LOG_I("Time:%dms, Read Speed:%d KB/s \r\n", time_node, ((sizeof(RW_Buffer) * i) >> 10) * 1000 / time_node);
} else {
LOG_F("Fail to read file: (%d), num:%d\n", ret, i);
return;
}
} else {
LOG_F("Fail to open files.\r\n");
return;
}
/* check data */
#if SDU_DATA_CHECK
LOG_I("\r\n******************** be about to check test... **********************\r\n");
ret = f_open(&fnew, "sd:test_file.txt", FA_OPEN_EXISTING | FA_READ);
if (ret == FR_OK) {
ret = f_read(&fnew, RW_Buffer, 1024, &fnum);
for (i = 0; i < 1024; i++) {
ret = f_read(&fnew, RW_Buffer, sizeof(RW_Buffer), &fnum);
if (ret) {
break;
}
for (j = 0; j < sizeof(RW_Buffer); j++) {
if (RW_Buffer[j] != Check_Buffer[j]) {
break;
}
}
if (j < sizeof(RW_Buffer)) {
break;
}
}
/* close file */
ret |= f_close(&fnew);
if (ret == FR_OK) {
if (i < 1024 || j < sizeof(RW_Buffer)) {
LOG_I("Check Test Error! \r\n");
LOG_I("Data Error! Num:%d/1024, Byte:%d/%d", i, j, sizeof(RW_Buffer));
} else {
LOG_I("Check Test Succeed! \r\n");
LOG_I("All Data Is Good! \r\n");
}
} else {
LOG_F("Fail to read file: (%d), num:%d\n", ret, i);
return;
}
} else {
LOG_F("Fail to open files.\r\n");
return;
}
#endif
}
int main(void)
{
board_init();
filesystem_init();
2022-12-08 11:03:19 +08:00
fatfs_write_read_test();
#ifdef CONFIG_VLIBC
LOG_I("\r\n******************** be about to vlibc test... **********************\r\n");
vlibc_file_t *fp = vlibc_fopen("sd:/test.txt", "w");
if (fp == NULL) {
2022-12-08 11:03:19 +08:00
LOG_F("open file error\r\n");
} else {
LOG_I("open file success\r\n");
for (uint8_t i = 0; i < 10; i++) {
vlibc_fprintf(fp, "hello world\r\n");
}
vlibc_fclose(fp);
LOG_I("close file success\r\n");
}
2022-12-08 11:03:19 +08:00
#endif
while (1) {
bflb_mtimer_delay_ms(200);
}
}