From 9c8717342be0117e7552eb90be8881cd2ec9efda Mon Sep 17 00:00:00 2001 From: "Zone.N" Date: Sat, 27 May 2023 23:04:09 +0800 Subject: [PATCH] test(fs): test fatfs, error occurred while writing Signed-off-by: Zone.N --- src/arch/riscv64/intr/intr.cpp | 2 +- src/kernel/test.cpp | 98 +++++++++++++++++++--------------- 2 files changed, 55 insertions(+), 45 deletions(-) diff --git a/src/arch/riscv64/intr/intr.cpp b/src/arch/riscv64/intr/intr.cpp index 12b9497da..189442eaf 100644 --- a/src/arch/riscv64/intr/intr.cpp +++ b/src/arch/riscv64/intr/intr.cpp @@ -42,7 +42,7 @@ trap_handler(uintptr_t _sepc, uintptr_t _stval, uintptr_t _scause, (void)_sstatus; (void)_satp; (void)_sscratch; -#define DEBUG +//#define DEBUG #ifdef DEBUG info("sepc: 0x%p, stval: 0x%p, scause: 0x%p, all_regs(sp): 0x%p, sie: " "0x%p\nsstatus: ", diff --git a/src/kernel/test.cpp b/src/kernel/test.cpp index dbec85b49..2101210fa 100644 --- a/src/kernel/test.cpp +++ b/src/kernel/test.cpp @@ -251,57 +251,67 @@ int test_device(void) { } int test_fatfs(void) { - // // FatFs work area needed for each volume - // FATFS FatFs; - // // File object needed for each open file - // FIL Fil; - // - // uint32_t bw; - // FRESULT fr; - // - // // Give a work area to the default drive - // fr = f_mount(&FatFs, "/", 1); - // info("f_mount:%d\n", fr); - // - // // Create a file - // fr = f_open(&Fil, "newfile.txt", FA_WRITE | FA_CREATE_ALWAYS); - // info("f_open:%d\n", fr); - // - // if (fr == FR_OK) { - // // Write data to the file - // f_write(&Fil, "It works!\r\n", 11, &bw); - // // Close the file - // fr = f_close(&Fil); - // info("f_close %d\n", fr); - // } + FATFS fatfs; + FRESULT status; + DIR dir; + FILINFO flinfo; + FIL fil; + uint8_t buf[COMMON::BUFFFER_SIZE]; + uint32_t size; - FATFS sdcard_fs; - FRESULT status; - DIR dj; - FILINFO fno; + status = f_mount(&fatfs, "", 0); + assert(status == FR_OK); - printf("/********************fs test*******************/\n"); - status = f_mount(&sdcard_fs, "", 0); - printf("mount sdcard: %d\n", status); - if (status != FR_OK) { - return status; - } - - /// @bug 找不到文件 - status = f_findfirst(&dj, &fno, "", "*"); + status = f_findfirst(&dir, &flinfo, "", "*"); + assert(status == FR_OK); - printf("printf filename %d [%s]\n", status, "fno.fname[0]"); - while (status == FR_OK && fno.fname[0]) { - if (fno.fattrib & AM_DIR) { - printf("dir: %s\n", fno.fname); + while (status == FR_OK && flinfo.fname[0]) { + if (flinfo.fattrib & AM_DIR) { + printf("dir: %s\n", flinfo.fname); } else { - printf("file: %s\n", fno.fname); + printf("file: %s\n", flinfo.fname); } - status = f_findnext(&dj, &fno); - printf("f_findnext %d [%s]\n", status, "fno.fname[0]"); + status = f_findnext(&dir, &flinfo); + } + f_closedir(&dir); + + status = f_open(&fil, "file1", FA_READ | FA_OPEN_EXISTING); + assert(status == FR_OK); + + bzero(buf, COMMON::BUFFFER_SIZE); + + status = f_read(&fil, buf, 15, &size); + assert(status == FR_OK); + f_close(&fil); + + printf("buf: [%s]\n", buf); + + status = f_open(&fil, "file1", FA_WRITE | FA_OPEN_APPEND); + assert(status == FR_OK); + + auto str = "Hello, World!\n"; + status = f_write(&fil, str, sizeof(str), &size); + assert(status == FR_OK); + f_close(&fil); + + assert(size == sizeof(str)); + printf("f_write: %d\n", size); + + status = f_open(&fil, "file1", FA_READ | FA_OPEN_EXISTING); + assert(status == FR_OK); + + bzero(buf, COMMON::BUFFFER_SIZE); + + status = f_read(&fil, buf, 15, &size); + assert(status == FR_OK); + f_close(&fil); + + printf("buf: %d [0x%X]\n", size, buf); + for (int i = 0; i < 15; i++) { + printf("[%c]", buf[i]); } - f_closedir(&dj); + printf("\n"); info("fatfs test done.\n"); return 0;