Browse Source

improve msc device port

tfx2001 4 years ago
parent
commit
a7a9c668d1
2 changed files with 58 additions and 56 deletions
  1. 1 1
      rt-thread/SConscript
  2. 57 55
      rt-thread/port/msc_device_port.c

+ 1 - 1
rt-thread/SConscript

@@ -27,7 +27,7 @@ if GetDepend(["PKG_TINYUSB_DEVICE_CDC"]):
     src += ["../src/class/cdc/cdc_device.c"]
 
 if GetDepend(["PKG_TINYUSB_DEVICE_MSC"]):
-    src += ["../src/class/msc/msc_device.c", "port/msc_device.c"]
+    src += ["../src/class/msc/msc_device.c", "port/msc_device_port.c"]
 
 if GetDepend(["PKG_TINYUSB_DEVICE_EXAMPLE_CDC"]):
     src += ["example/cdc.c"]

+ 57 - 55
rt-thread/port/msc_device.c → rt-thread/port/msc_device_port.c

@@ -1,7 +1,7 @@
 /*
  * The MIT License (MIT)
  *
- * Copyright (c) 2019 Ha Thach (tinyusb.org)
+ * Copyright (c) 2021 tfx2001 (2479727366@qq.com)
  *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
@@ -23,114 +23,116 @@
  *
  */
 
-#ifdef __RTTHREAD__
+#include <tusb.h>
+#include <stdint.h>
 
 #include <board.h>
 #include <rtdevice.h>
 
-#include <tusb.h>
-#include <fal.h>
 
-// whether host does safe-eject
 static bool ejected = false;
 static rt_device_t flash_device;
+static struct rt_device_blk_geometry blk_geom;
+
+#ifdef __CC_ARM
+uint16_t __builtin_bswap16(uint16_t x)
+{
+    return (x << 8) | (x >> 8);
+}
+#endif
 
-// Invoked when received SCSI_CMD_INQUIRY
-// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively
-void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) {
+void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4])
+{
     (void) lun;
 
-    const char vid[] = "TinyUSB";
-    const char pid[] = "Mass Storage";
-    const char rev[] = "1.0";
+    const char vid[] = PKG_TINYUSB_DEVICE_MSC_VID;
+    const char pid[] = PKG_TINYUSB_DEVICE_MSC_PID;
+    const char rev[] = PKG_TINYUSB_DEVICE_MSC_REV;
 
     memcpy(vendor_id, vid, strlen(vid));
     memcpy(product_id, pid, strlen(pid));
     memcpy(product_rev, rev, strlen(rev));
 }
 
-// Invoked when received Test Unit Ready command.
-// return true allowing host to read/write this LUN e.g SD card inserted
-bool tud_msc_test_unit_ready_cb(uint8_t lun) {
+bool tud_msc_test_unit_ready_cb(uint8_t lun)
+{
     (void) lun;
 
-    // RAM disk is ready until ejected
-    if (ejected) {
+    if (ejected)
+    {
         tud_msc_set_sense(lun, SCSI_SENSE_NOT_READY, 0x3a, 0x00);
         return false;
     }
 
-    return true;
+    if (flash_device == NULL)
+    {
+        flash_device = rt_device_find(PKG_TINYUSB_DEVICE_MSC_NAME);
+    }
+    if (flash_device != NULL)
+    {
+        rt_device_open(flash_device, 0);
+        rt_device_control(flash_device, RT_DEVICE_CTRL_BLK_GETGEOME, &blk_geom);
+        return true;
+    }
+
+    return false;
 }
 
-// Invoked when received SCSI_CMD_READ_CAPACITY_10 and SCSI_CMD_READ_FORMAT_CAPACITY to determine the disk size
-// Application update block count and block size
-void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size) {
+void tud_msc_capacity_cb(uint8_t lun, uint32_t *block_count, uint16_t *block_size)
+{
     (void) lun;
-    struct rt_device_blk_geometry blk_geom;
-
-    if (!flash_device) {
-        flash_device = rt_device_find(PKG_TINYUSB_DEVICE_MSC_NAME);
-    }
-    rt_device_control(flash_device, RT_DEVICE_CTRL_BLK_GETGEOME, &blk_geom);
 
     *block_count = blk_geom.sector_count;
-    *block_size = blk_geom.bytes_per_sector;
+    *block_size  = blk_geom.bytes_per_sector;
 }
 
-// Invoked when received Start Stop Unit command
-// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage
-// - Start = 1 : active mode, if load_eject = 1 : load disk storage
-bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) {
+bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject)
+{
     (void) lun;
     (void) power_condition;
 
-    if (load_eject) {
-        if (start) {
-            // load disk storage
-            flash_device = rt_device_find(PKG_TINYUSB_DEVICE_MSC_NAME);
+    if (load_eject)
+    {
+        if (start)
+        {
+            ejected = false;
         } else {
             // unload disk storage
             ejected = true;
         }
-    }
+    } 
 
     return true;
 }
 
-// Callback invoked when received READ10 command.
-// Copy disk's data to buffer (up to bufsize) and return number of copied bytes.
-int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize) {
+int32_t tud_msc_read10_cb(uint8_t lun, uint32_t lba, uint32_t offset, void *buffer, uint32_t bufsize)
+{
     (void) lun;
     (void) offset;
     (void) bufsize;
 
-    return (int32_t) rt_device_read(flash_device, (rt_off_t) lba, buffer, 1) * 4096;
+    return (int32_t) rt_device_read(flash_device, (rt_off_t) lba, buffer, 1) * blk_geom.bytes_per_sector;
 }
 
-// Callback invoked when received WRITE10 command.
-// Process data in buffer to disk's storage and return number of written bytes
-int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize) {
+int32_t tud_msc_write10_cb(uint8_t lun, uint32_t lba, uint32_t offset, uint8_t *buffer, uint32_t bufsize)
+{
     (void) lun;
     (void) offset;
     (void) bufsize;
 
-    return (int32_t) rt_device_write(flash_device, (rt_off_t) lba, buffer, 1) * 4096;
+    return (int32_t) rt_device_write(flash_device, (rt_off_t) lba, buffer, 1) * blk_geom.bytes_per_sector;
 }
 
-// Callback invoked when received an SCSI command not in built-in list below
-// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
-// - READ10 and WRITE10 has their own callbacks
-int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize) {
-    // read10 & write10 has their own callback and MUST not be handled here
-
+int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, uint16_t bufsize)
+{
     void const *response = NULL;
     uint16_t resplen = 0;
 
     // most scsi handled is input
     bool in_xfer = true;
 
-    switch (scsi_cmd[0]) {
+    switch (scsi_cmd[0])
+    {
         case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
             // Host is about to read/write etc ... better not to disconnect disk
             resplen = 0;
@@ -148,8 +150,10 @@ int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, u
     // return resplen must not larger than bufsize
     if (resplen > bufsize) resplen = bufsize;
 
-    if (response && (resplen > 0)) {
-        if (in_xfer) {
+    if (response && (resplen > 0))
+    {
+        if (in_xfer)
+        {
             memcpy(buffer, response, resplen);
         } else {
             // SCSI output
@@ -158,5 +162,3 @@ int32_t tud_msc_scsi_cb(uint8_t lun, uint8_t const scsi_cmd[16], void *buffer, u
 
     return resplen;
 }
-
-#endif