summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorsakumisu <[email protected]>2022-02-23 17:47:24 +0800
committersakumisu <[email protected]>2022-02-23 17:47:24 +0800
commitc053a656bda0872c3e12fbcd6c10d62aac435372 (patch)
tree25237130126e6b7b0066bd4c3555a082d90edd30
parent4db9c34d908c8df0e012d3901bf896a24e90bc9e (diff)
add msc port for fatfs
-rw-r--r--third_party/fatfs-0.14/source/port/fatfs_usbd.c60
-rw-r--r--third_party/fatfs-0.14/source/port/fatfs_usbh.c58
2 files changed, 118 insertions, 0 deletions
diff --git a/third_party/fatfs-0.14/source/port/fatfs_usbd.c b/third_party/fatfs-0.14/source/port/fatfs_usbd.c
new file mode 100644
index 00000000..e34e7405
--- /dev/null
+++ b/third_party/fatfs-0.14/source/port/fatfs_usbd.c
@@ -0,0 +1,60 @@
+#include "diskio.h"
+#include "string.h"
+#include "hal_flash.h"
+
+#define FLASH_START_ADDR 0x00040000 /*addr start from 256k */
+#define FLASH_BLOCK_SIZE 4096
+#define FLASH_BLOCK_COUNT 64
+
+extern const char *FR_Table[];
+
+int USB_disk_status(void)
+{
+ return 0;
+}
+int USB_disk_initialize(void)
+{
+ return RES_OK;
+}
+int USB_disk_read(BYTE *buff, LBA_t sector, UINT count)
+{
+ flash_read(FLASH_START_ADDR + sector * FLASH_BLOCK_SIZE, (uint8_t *)buff, count * FLASH_BLOCK_SIZE);
+ return 0;
+}
+int USB_disk_write(const BYTE *buff, LBA_t sector, UINT count)
+{
+ flash_erase(FLASH_START_ADDR + sector * FLASH_BLOCK_SIZE, 4096);
+ flash_write(FLASH_START_ADDR + sector * FLASH_BLOCK_SIZE, (uint8_t *)buff, count * FLASH_BLOCK_SIZE);
+ return 0;
+}
+int USB_disk_ioctl(BYTE cmd, void *buff)
+{
+ int result = 0;
+
+ switch (cmd) {
+ case CTRL_SYNC:
+ result = RES_OK;
+ break;
+
+ case GET_SECTOR_SIZE:
+ *(WORD *)buff = FLASH_BLOCK_SIZE;
+ result = RES_OK;
+ break;
+
+ case GET_BLOCK_SIZE:
+ *(DWORD *)buff = 1;
+ result = RES_OK;
+ break;
+
+ case GET_SECTOR_COUNT:
+ *(DWORD *)buff = FLASH_BLOCK_COUNT;
+ result = RES_OK;
+ break;
+
+ default:
+ result = RES_PARERR;
+ break;
+ }
+
+ return result;
+} \ No newline at end of file
diff --git a/third_party/fatfs-0.14/source/port/fatfs_usbh.c b/third_party/fatfs-0.14/source/port/fatfs_usbh.c
new file mode 100644
index 00000000..b3104b6e
--- /dev/null
+++ b/third_party/fatfs-0.14/source/port/fatfs_usbh.c
@@ -0,0 +1,58 @@
+#include "diskio.h"
+#include "usbh_core.h"
+#include "usbh_msc.h"
+
+struct usbh_msc *active_msc_class;
+
+int USB_disk_status(void)
+{
+ return 0;
+}
+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_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;
+
+ switch (cmd) {
+ case CTRL_SYNC:
+ result = RES_OK;
+ break;
+
+ case GET_SECTOR_SIZE:
+ *(WORD *)buff = active_msc_class->blocksize;
+ result = RES_OK;
+ break;
+
+ case GET_BLOCK_SIZE:
+ *(DWORD *)buff = 1;
+ result = RES_OK;
+ break;
+
+ case GET_SECTOR_COUNT:
+ *(DWORD *)buff = active_msc_class->blocknum;
+ result = RES_OK;
+ break;
+
+ default:
+ result = RES_PARERR;
+ break;
+ }
+
+ return result;
+} \ No newline at end of file