From 1df32997eddb5b50c5a804cf7bfbd63866a402e2 Mon Sep 17 00:00:00 2001 From: sakumisu <1203593632@qq.com> Date: Mon, 12 Sep 2022 20:12:41 +0800 Subject: [PATCH] update fatfs demo and port --- demo/usb_host.c | 102 +++++++++++------- third_party/fatfs-0.14/source/diskio.c | 30 +++--- third_party/fatfs-0.14/source/ffconf.h | 4 +- .../fatfs-0.14/source/port/fatfs_usbh.c | 5 +- 4 files changed, 81 insertions(+), 60 deletions(-) diff --git a/demo/usb_host.c b/demo/usb_host.c index a79c580b..129752e6 100644 --- a/demo/usb_host.c +++ b/demo/usb_host.c @@ -74,6 +74,66 @@ int cdc_acm_test(void) } #if 0 #include "ff.h" + +USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t read_write_buffer[25 * 100]; + +FATFS fs; +FIL fnew; +UINT fnum; +FRESULT res_sd = 0; + +int usb_msc_fatfs_test() +{ + const char *tmp_data = "cherryusb fatfs demo...\r\n"; + + USB_LOG_RAW("data len:%d\r\n", strlen(tmp_data)); + for (uint32_t i = 0; i < 100; i++) { + memcpy(&read_write_buffer[i * 25], tmp_data, strlen(tmp_data)); + } + + res_sd = f_mount(&fs, "2:", 1); + if (res_sd != FR_OK) { + USB_LOG_RAW("mount fail,res:%d\r\n", res_sd); + return -1; + } + + USB_LOG_RAW("test fatfs write\r\n"); + res_sd = f_open(&fnew, "2:test.txt", FA_CREATE_ALWAYS | FA_WRITE); + if (res_sd == FR_OK) { + res_sd = f_write(&fnew, read_write_buffer, sizeof(read_write_buffer), &fnum); + if (res_sd == FR_OK) { + USB_LOG_RAW("write success, write len:%d\n", fnum); + } else { + USB_LOG_RAW("write fail\r\n"); + goto unmount; + } + f_close(&fnew); + } else { + USB_LOG_RAW("open fail\r\n"); + goto unmount; + } + USB_LOG_RAW("test fatfs read\r\n"); + + res_sd = f_open(&fnew, "2:test.txt", FA_OPEN_EXISTING | FA_READ); + if (res_sd == FR_OK) { + res_sd = f_read(&fnew, read_write_buffer, sizeof(read_write_buffer), &fnum); + if (res_sd == FR_OK) { + USB_LOG_RAW("read success, read len:%d\n", fnum); + } else { + USB_LOG_RAW("read fail\r\n"); + goto unmount; + } + f_close(&fnew); + } else { + USB_LOG_RAW("open fail\r\n"); + goto unmount; + } + f_mount(NULL, "2:", 1); + return 0; +unmount: + f_mount(NULL, "2:", 1); + return -1; +} #endif USB_NOCACHE_RAM_SECTION USB_MEM_ALIGNX uint8_t partition_table[512]; @@ -86,7 +146,7 @@ int msc_test(void) USB_LOG_RAW("do not find /dev/sda\r\n"); return -1; } -#if 1 +#if 0 /* get the partition table */ ret = usbh_msc_scsi_read10(msc_class, 0, partition_table, 1); if (ret < 0) { @@ -103,45 +163,7 @@ int msc_test(void) #endif #if 0 - uint8_t *msc_buffer = usb_iomalloc(8192); - ret = usbh_msc_scsi_read10(msc_class, 0, msc_buffer, 16); - usb_iofree(msc_buffer); - // for (uint32_t i = 0; i < 1024; i++) { - // if (i % 16 == 0) { - // USB_LOG_RAW("\r\n"); - // } - // USB_LOG_RAW("%02x ", msc_buffer[i]); - // } - // USB_LOG_RAW("\r\n"); -#endif - -#if 0 - - FATFS fs; - FIL fnew; - UINT fnum; - FRESULT res_sd = 0; - uint8_t *ReadBuffer; - - ReadBuffer = usb_iomalloc(512); - f_mount(&fs, "2:", 1); - res_sd = f_open(&fnew, "2:test.c", FA_OPEN_EXISTING | FA_READ); - if (res_sd == FR_OK) { - res_sd = f_read(&fnew, ReadBuffer, 512, &fnum); - for (uint32_t i = 0; i < fnum; i++) { - if (i % 16 == 0) { - USB_LOG_RAW("\r\n"); - } - USB_LOG_RAW("%02x ", ReadBuffer[i]); - } - USB_LOG_RAW("\r\n"); - f_close(&fnew); - /*unmount*/ - f_mount(NULL, "2:", 1); - } else { - USB_LOG_RAW("open error:%d\r\n", res_sd); - } - usb_iofree(ReadBuffer); + usb_msc_fatfs_test(); #endif return ret; } diff --git a/third_party/fatfs-0.14/source/diskio.c b/third_party/fatfs-0.14/source/diskio.c index 0d0b28fa..a73dabb3 100644 --- a/third_party/fatfs-0.14/source/diskio.c +++ b/third_party/fatfs-0.14/source/diskio.c @@ -29,25 +29,24 @@ DSTATUS disk_status ( ) { DSTATUS stat; - int result; switch (pdrv) { case DEV_RAM : - //result = RAM_disk_status(); + //stat = RAM_disk_status(); // translate the reslut code here return stat; case DEV_MMC : - //result = MMC_disk_status(); + //stat = MMC_disk_status(); // translate the reslut code here return stat; case DEV_USB : - result = USB_disk_status(); + stat = USB_disk_status(); // translate the reslut code here @@ -67,25 +66,24 @@ DSTATUS disk_initialize ( ) { DSTATUS stat; - int result; switch (pdrv) { case DEV_RAM : - //result = RAM_disk_initialize(); + //stat = RAM_disk_initialize(); // translate the reslut code here return stat; case DEV_MMC : - //result = MMC_disk_initialize(); + //stat = MMC_disk_initialize(); // translate the reslut code here return stat; case DEV_USB : - result = USB_disk_initialize(); + stat = USB_disk_initialize(); // translate the reslut code here @@ -108,13 +106,12 @@ DRESULT disk_read ( ) { DRESULT res; - int result; switch (pdrv) { case DEV_RAM : // translate the arguments here - //result = RAM_disk_read(buff, sector, count); + //res = RAM_disk_read(buff, sector, count); // translate the reslut code here @@ -123,7 +120,7 @@ DRESULT disk_read ( case DEV_MMC : // translate the arguments here - //result = MMC_disk_read(buff, sector, count); + //res = MMC_disk_read(buff, sector, count); // translate the reslut code here @@ -132,7 +129,7 @@ DRESULT disk_read ( case DEV_USB : // translate the arguments here - result = USB_disk_read(buff, sector, count); + res = USB_disk_read(buff, sector, count); // translate the reslut code here @@ -158,13 +155,12 @@ DRESULT disk_write ( ) { DRESULT res; - int result; switch (pdrv) { case DEV_RAM : // translate the arguments here - //result = RAM_disk_write(buff, sector, count); + //res = RAM_disk_write(buff, sector, count); // translate the reslut code here @@ -173,7 +169,7 @@ DRESULT disk_write ( case DEV_MMC : // translate the arguments here - //result = MMC_disk_write(buff, sector, count); + //res = MMC_disk_write(buff, sector, count); // translate the reslut code here @@ -182,7 +178,7 @@ DRESULT disk_write ( case DEV_USB : // translate the arguments here - result = USB_disk_write(buff, sector, count); + res = USB_disk_write(buff, sector, count); // translate the reslut code here @@ -224,7 +220,7 @@ DRESULT disk_ioctl ( case DEV_USB : // Process of the command the USB drive - USB_disk_ioctl(cmd,buff); + res = USB_disk_ioctl(cmd,buff); return res; } diff --git a/third_party/fatfs-0.14/source/ffconf.h b/third_party/fatfs-0.14/source/ffconf.h index 16c12748..b150ce6d 100644 --- a/third_party/fatfs-0.14/source/ffconf.h +++ b/third_party/fatfs-0.14/source/ffconf.h @@ -166,12 +166,12 @@ / Drive/Volume Configurations /---------------------------------------------------------------------------*/ -#define FF_VOLUMES 1 +#define FF_VOLUMES 3 /* Number of volumes (logical drives) to be used. (1-10) */ #define FF_STR_VOLUME_ID 0 -#define FF_VOLUME_STRS "RAM","NAND","CF","SD","SD2","USB","USB2","USB3" +#define FF_VOLUME_STRS "RAM","SD","USB" /* FF_STR_VOLUME_ID switches support for volume ID in arbitrary strings. / When FF_STR_VOLUME_ID is set to 1 or 2, arbitrary strings can be used as drive / number in the path name. FF_VOLUME_STRS defines the volume ID strings for each diff --git a/third_party/fatfs-0.14/source/port/fatfs_usbh.c b/third_party/fatfs-0.14/source/port/fatfs_usbh.c index f8d521ae..feab07e4 100644 --- a/third_party/fatfs-0.14/source/port/fatfs_usbh.c +++ b/third_party/fatfs-0.14/source/port/fatfs_usbh.c @@ -14,18 +14,21 @@ int USB_disk_initialize(void) active_msc_class = (struct usbh_msc *)usbh_find_class_instance("/dev/sda"); if (active_msc_class == NULL) { printf("do not find /dev/sda\r\n"); - return -1; + return RES_NOTRDY; } return RES_OK; } + int USB_disk_read(BYTE *buff, LBA_t sector, UINT count) { return usbh_msc_scsi_read10(active_msc_class, sector, buff, count); } + int USB_disk_write(const BYTE *buff, LBA_t sector, UINT count) { return usbh_msc_scsi_write10(active_msc_class, sector, buff, count); } + int USB_disk_ioctl(BYTE cmd, void *buff) { int result = 0;