Files
kunlun/dtest/fatfs_test/fatfs_test.c
2024-09-28 14:24:04 +08:00

277 lines
6.7 KiB
C
Executable File

/****************************************************************************
Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
be copied by any method or incorporated into another program without
the express written consent of Aerospace C.Power. This Information or any portion
thereof remains the property of Aerospace C.Power. The Information contained herein
is believed to be accurate and Aerospace C.Power assumes no responsibility or
liability for its use in any way and conveys no license or title under
any patent or copyright and makes no representation or warranty that this
Information is free from patent or copyright infringement.
****************************************************************************/
/* os shim includes */
#include "os_types.h"
#include "os_task.h"
#include "os_lock.h"
#include "os_mem.h"
#include "os_utils.h"
/* common includes */
#include "iot_io.h"
#include "iot_clock.h"
#include "dbg_io.h"
/* driver includes */
#include "ahb.h"
#include "uart.h"
#include "iot_uart.h"
/* fatfs includes */
#include "ff.h"
#include "integer.h"
extern int platform_init();
static FATFS fs;
static FIL fp;
static FILINFO fno;
#define RECV_MAX_SZ (8 * 1024 * 1024)
#define RECV_START_ADDR (0x10000000)
uint8_t w_buf[] = "Hello1, World!\r\n";
uint8_t w_buf2[] = "hahaha\r\n";
uint8_t r_buf[256] = {0};
uint8_t *check_buf = (uint8_t *) (RECV_START_ADDR + RECV_MAX_SZ);
void fatfs_dir_access()
{
FRESULT res;
DIR dp;
// test directory function
iot_printf("\n{fatfs directory access test}\n");
iot_printf("-- test get filename by directory --\n");
res = f_opendir(&dp, "0:/");
if (res) {
iot_printf("[error] f_opendir failed, res: %d\n", res);
} else {
iot_printf("f_opendir result: %d\n", res);
}
while(1) {
res = f_readdir(&dp, &fno);
if (res != FR_OK || fno.fname[0] == 0) {
break;
} else {
iot_printf("get file name: %s\n", fno.fname);
// open file
res = f_open(&fp, fno.fname, FA_OPEN_EXISTING | FA_READ);
if (res) {
iot_printf("[error] f_open failed, res: %d\n", res);
} else {
iot_printf("f_open result: %d\n", res);
}
UINT br;
FSIZE_t size;
// get file size
size = f_size(&fp);
iot_printf("size: %d\n", size);
if (size == 0) {
continue;
}
res = f_read(&fp, check_buf, size, &br);
if (res) {
iot_printf("[error] f_read failed, res: %d\n", res);
} else {
iot_printf("f_read result: %d\n", res);
iot_printf("f_read len: %d\n", br);
}
f_close(&fp);
}
}
f_closedir(&dp);
iot_printf("= fatfs directory access test =\n");
}
void fatfs_file_access()
{
FRESULT res;
iot_printf("\n{fatfs file access test}\n");
// test f_write function
iot_printf("-- test write file --\n");
#if 1
res = f_open(&fp, "test.bin", FA_OPEN_ALWAYS | FA_WRITE);
if (res) {
iot_printf("[error] f_open failed, res: %d\n", res);
} else {
iot_printf("f_open result: %d\n", res);
}
UINT bw;
res = f_write(&fp, (uint8_t *)RECV_START_ADDR, 4096, &bw);
if (res) {
iot_printf("[error] f_write failed, res: %d\n", res);
} else {
iot_printf("f_write result: %d\n", res);
iot_printf("f_write len: %d, write buf: %s\n", bw, w_buf);
}
res = f_write(&fp, (uint8_t *)RECV_START_ADDR, sizeof(w_buf2), &bw);
if (res) {
iot_printf("[error] f_write failed, res: %d\n", res);
} else {
iot_printf("f_write result: %d\n", res);
iot_printf("f_write len: %d, write buf: %s\n", bw, w_buf2);
}
iot_printf("close: %d\n", f_close(&fp));
#endif
// test f_read function
iot_printf("-- test read --\n");
res = f_open(&fp, "test.bin", FA_OPEN_EXISTING | FA_READ);
if (res) {
iot_printf("[error] f_open failed, res: %d\n", res);
} else {
iot_printf("f_open result: %d\n", res);
}
UINT br;
res = f_read(&fp, r_buf, 25, &br);
if (res) {
iot_printf("[error] f_read failed, res: %d\n", res);
} else {
iot_printf("f_read result: %d\n", res);
iot_printf("f_read len: %d, buf: %s\n", br, r_buf);
iot_printf("f_read read buf: %s\n", r_buf);
}
f_close(&fp);
// test f_size function
iot_printf("-- test get file size --\n");
res = f_open(&fp, "test.bin", FA_OPEN_EXISTING | FA_READ);
if (res) {
iot_printf("[error] f_open failed, res: %d\n", res);
} else {
iot_printf("f_open result: %d\n", res);
}
FSIZE_t size;
size = f_size(&fp);
iot_printf("f_size size: %d\n", size);
f_close(&fp);
iot_printf("= fatfs file access test =\n");
}
#if 1
static uint8_t work[FF_MAX_SS];
#endif
void iot_task_1(void *arg)
{
FRESULT res;
// create fat volume, and format partitions
iot_printf("{fatfs mount access test}\n");
#if 1
iot_printf("\n-- test make fs --\n");
res = f_mkfs("0:", FM_ANY, 0, work, sizeof(work));
if (res) {
iot_printf("[error] f_mkfs failed, res: %d\n", res);
} else {
iot_printf("f_mkfs result: %d\n", res);
}
#endif
// test f_mount function
iot_printf("-- test mount fs --\n");
res = f_mount(&fs, "0:", 1);
if (res) {
iot_printf("[error] f_mount failed, res: %d\n", res);
} else {
iot_printf("f_mount result: %d\n", res);
}
iot_printf("= fatfs mount access test =\n");
fatfs_file_access();
fatfs_dir_access();
}
int32_t iot_task_init()
{
os_task_h handle;
/* start plc lib task */
handle = os_create_task(iot_task_1, NULL, 6);
//create the tasks;
if(handle != NULL) {
iot_printf("task 1 init successfully...\r\n");
}
return 0;
}
int32_t iot_task_start()
{
//start the tasks;
os_start_kernel();
return 0;
}
static int32_t iot_platform_init()
{
/*platform intialization*/
platform_init();
//resource initializations;
system_clock_init();
system_uart_init();
dbg_uart_init();
return 0;
}
int32_t iot_module_init(void)
{
//platform intialization;
iot_platform_init();
//create all the tasks;
iot_task_init();
iot_printf("\r\nstarting...\r\n");
return 0;
}
int32_t iot_module_start(void)
{
int32_t res = 0;
res = iot_task_start();
return res;
}
int main(void)
{
//module init;
iot_module_init();
//module start;
iot_module_start();
return 0;
}