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

313 lines
6.5 KiB
C
Executable File
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/****************************************************************************
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"
#include "flash.h"
extern int platform_init();
static FATFS fs;
static FIL fp;
char path[50];
uint8_t file_buf[1024];
void fatfs_file_access(int dev)
{
FRESULT res;
for (int i = 0; i < sizeof(file_buf); i++) {
file_buf[i] = i % 0xff;
}
iot_printf("-- test write file --\n");
iot_sprintf(path, "%d:/test.bin", dev);
#if 1 //写文件
res = f_open(&fp, path, FA_OPEN_ALWAYS | FA_WRITE);
if (res) {
iot_printf("[error] f_open %s failed, res: %d\n", path, res);
} else {
iot_printf("f_open %s ok %d\n", path);
}
UINT bw;
iot_printf("********write start*******\n");
int write_size = 0;
if (dev == 0) {
write_size = 100; // 1MB
} else if(dev == 2) {
write_size = 1024 * 1024;
} else {
iot_printf("unsupported write size *\n");
return ;
}
for (int i = 0; i < write_size; i++) {
res = f_write(&fp, file_buf, sizeof(file_buf), &bw);
if (res) {
iot_printf("[error] f_write failed, res: %d\n", res);
} else {
//iot_printf("f_write ok \n");
if (bw != sizeof(file_buf)) {
iot_printf("f_write len: %d\n", bw);
}
}
}
iot_printf("********write end*******\n");
f_close(&fp);
#endif
iot_printf("\n-- test read --\n\n");
res = f_open(&fp, path, FA_OPEN_ALWAYS | FA_WRITE | FA_READ);
if (res) {
iot_printf("[error] f_open %s failed, res: %d\n", path, res);
} else {
iot_printf("f_open %s ok \n", path);
}
UINT br;
res = f_read(&fp, file_buf, sizeof(file_buf), &br);
if (res) {
iot_printf("[error] f_read %s failed, res: %d\n", path, res);
} else {
iot_printf("f_read ok\n");
iot_printf("f_read len: %d\n", br);
iot_printf("f_read read buf: ");
for (int i = 0; i < br; i++) {
if (i % 16 == 0) {
iot_printf("\n");
}
iot_printf("%02x ", file_buf[i] & 0xff);
}
iot_printf("\n");
}
f_close(&fp);
// test f_size function
iot_printf("-- test get file size --\n");
res = f_open(&fp, path, FA_OPEN_ALWAYS | FA_WRITE);
if (res) {
iot_printf("[error] f_open %s failed, res: %d\n", path, res);
} else {
iot_printf("f_open %s ok %d\n", path);
}
FSIZE_t size;
size = f_size(&fp);
iot_printf("f_size size: %d\n", size);
f_close(&fp);
}
static uint8_t work[FF_MAX_SS];
void fatfs_test(int dev)
{
FRESULT res;
// create fat volume, and format partitions
iot_printf("-- test mount fs --\n");
iot_sprintf(path, "%d:", dev);
#if 1 //格式化选项传FM_ANY以防毕竟小的flash格式化失败
iot_printf("\n-- test make fs --\n");
res = f_mkfs(path, FM_ANY, 0, work, sizeof(work));
if (res) {
iot_printf("[error] f_mkfs \"%s\" failed, res: %d\n", path, res);
return ;
} else {
iot_printf("f_mkfs %s ok result: %d\n", path, res);
}
#else
(void) work;
#endif
// test f_mount function
res = f_mount(&fs, path, 1);
if (res) {
iot_printf("[error] f_mount %s failed, res: %d\n", path, res);
return ;
} else {
iot_printf("f_mount %s ok \n", path);
}
fatfs_file_access(dev);
f_unmount(path);
}
void flash_test(void)
{
flash_write_param_t param = {0};
param.read_mode = MOD_SFC_READ_QUAD_IO_FAST;
param.write_mode = MOD_SFC_PROG_QUAD;
param.is_erase = 1;
param.sw_mode = MOD_SW_MODE_DIS;
for (int i = 0; i < sizeof(file_buf); i++) {
file_buf[i] = i % 0xff;
}
flash_init(1);
int size = flash_get_dev_size();
iot_printf("size=%d\n", size);
iot_printf("start ..\r\n");
flash_write(file_buf, 0, sizeof(file_buf), &param);
flash_read(file_buf, 0, sizeof(file_buf), MOD_SFC_READ_QUAD_IO_FAST);
int i;
for (i = 0; i < sizeof(file_buf); i++) {
if (file_buf[i] != i % 0xff) {
iot_printf("err at %d,read:%02x,write:%02x\r\n", i, file_buf[i] & 0xff, i % 0xff);
break;
}
}
if (i == sizeof(file_buf)) {
iot_printf("flash_write_read ok\r\n");
}
}
int main()
{
dbg_uart_init();
iot_printf("start\n");
fatfs_test(0); // 0表示flash2表示SD卡
//flash_test();
while(1) {
}
return 0;
}
#if 0
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;
}
#endif