Skip to content

Commit

Permalink
update tinyusb lib to 0.5.x
Browse files Browse the repository at this point in the history
  • Loading branch information
hathach committed Jul 24, 2019
1 parent 82ab998 commit c921f66
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 109 deletions.
2 changes: 1 addition & 1 deletion lib/tinyusb
Submodule tinyusb updated 496 files
1 change: 1 addition & 0 deletions ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ INC += -I./nrfx
INC += -I./nrfx/hal
INC += -I./nrfx/mdk
INC += -I./nrfx/drivers/include
INC += -I./nrfx/drivers/src
INC += -I./bluetooth
INC += -I./peripherals
INC += -I../../lib/mp-readline
Expand Down
10 changes: 5 additions & 5 deletions shared-module/usb_hid/Device.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t*

// Wait until interface is ready, timeout = 2 seconds
uint64_t end_ticks = ticks_ms + 2000;
while ( (ticks_ms < end_ticks) && !tud_hid_generic_ready() ) { }
while ( (ticks_ms < end_ticks) && !tud_hid_ready() ) { }

if ( !tud_hid_generic_ready() ) {
if ( !tud_hid_ready() ) {
mp_raise_msg(&mp_type_OSError, translate("USB Busy"));
}

memcpy(self->report_buffer, report, len);

if ( !tud_hid_generic_report(self->report_id, self->report_buffer, len) ) {
if ( !tud_hid_report(self->report_id, self->report_buffer, len) ) {
mp_raise_msg(&mp_type_OSError, translate("USB Error"));
}
}
Expand All @@ -70,7 +70,7 @@ static usb_hid_device_obj_t* get_hid_device(uint8_t report_id) {
}

// Callbacks invoked when receive Get_Report request through control endpoint
uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) {
uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) {
// only support Input Report
if ( report_type != HID_REPORT_TYPE_INPUT ) return 0;

Expand All @@ -80,7 +80,7 @@ uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t repo
}

// Callbacks invoked when receive Set_Report request through control endpoint
void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) {
usb_hid_device_obj_t* hid_device = get_hid_device(report_id);

if ( report_type == HID_REPORT_TYPE_OUTPUT ) {
Expand Down
2 changes: 1 addition & 1 deletion shared-module/usb_midi/PortOut.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@ size_t common_hal_usb_midi_portout_write(usb_midi_portout_obj_t *self, const uin
}

bool common_hal_usb_midi_portout_ready_to_tx(usb_midi_portout_obj_t *self) {
return tud_midi_connected();
return tud_midi_mounted();
}
2 changes: 1 addition & 1 deletion supervisor/shared/usb/tusb_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@
// USB RAM PLACEMENT
//--------------------------------------------------------------------+
#define CFG_TUSB_ATTR_USBRAM
#define CFG_TUSB_MEM_ALIGN ATTR_ALIGNED(4)
#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4)))


#ifdef __cplusplus
Expand Down
43 changes: 28 additions & 15 deletions supervisor/shared/usb/usb_desc.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,36 @@
* THE SOFTWARE.
*/

#include "supervisor/shared/usb/usb_desc.h"
#include "lib/tinyusb/src/tusb.h"
#include "shared-module/usb_hid/Device.h"

#include "genhdr/autogen_usb_descriptor.h"

// tud_desc_set is required by tinyusb stack
tud_desc_set_t tud_desc_set =
{
.device = &usb_desc_dev,
.config = &usb_desc_cfg,
.string_arr = (uint8_t const **) string_desc_arr,
.string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]),
// Invoked when received GET DEVICE DESCRIPTOR
// Application return pointer to descriptor
uint8_t const * tud_descriptor_device_cb(void) {
return usb_desc_dev;
}

.hid_report =
{
.generic = hid_report_descriptor,
.boot_keyboard = NULL,
.boot_mouse = NULL
}
};
// Invoked when received GET CONFIGURATION DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const * tud_descriptor_configuration_cb(uint8_t index) {
(void) index; // for multiple configurations
return usb_desc_cfg;
}


// Invoked when received GET HID REPORT DESCRIPTOR
// Application return pointer to descriptor
// Descriptor contents must exist long enough for transfer to complete
uint8_t const * tud_hid_descriptor_report_cb(void) {
return hid_report_descriptor;
}

// Invoked when received GET STRING DESCRIPTOR request
// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete
uint16_t const* tud_descriptor_string_cb(uint8_t index) {
uint8_t const max_index = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]);
return (index < max_index) ? string_desc_arr[index] : NULL;
}
43 changes: 0 additions & 43 deletions supervisor/shared/usb/usb_desc.h

This file was deleted.

96 changes: 53 additions & 43 deletions supervisor/shared/usb/usb_msc_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,59 +59,18 @@ static fs_user_mount_t* get_vfs(int lun) {
}

// Callback invoked when received an SCSI command not in built-in list below
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE
// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, TEST_UNIT_READY, START_STOP_UNIT, MODE_SENSE6, REQUEST_SENSE
// - READ10 and WRITE10 have their own callbacks
int32_t tud_msc_scsi_cb (uint8_t lun, const uint8_t scsi_cmd[16], void* buffer, uint16_t bufsize) {
const void* response = NULL;
int32_t resplen = 0;

switch ( scsi_cmd[0] ) {
case SCSI_CMD_TEST_UNIT_READY:
// Command that host uses to check our readiness before sending other commands
resplen = 0;
if (lun > 1) {
resplen = -1;
} else {
fs_user_mount_t* current_mount = get_vfs(lun);
if (current_mount == NULL) {
resplen = -1;
}
if (ejected[lun]) {
resplen = -1;
}
}
break;

case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL:
// Host is about to read/write etc ... better not to disconnect disk
resplen = 0;
break;

case SCSI_CMD_START_STOP_UNIT:
{
// Host try to eject/safe remove/poweroff us. We could safely disconnect with disk storage, or go into lower power
const scsi_start_stop_unit_t* start_stop = (const scsi_start_stop_unit_t*) scsi_cmd;
// Start bit = 0 : low power mode, if load_eject = 1 : unmount disk storage as well
// Start bit = 1 : Ready mode, if load_eject = 1 : mount disk storage
resplen = 0;
if (start_stop->load_eject == 1) {
if (lun > 1) {
resplen = -1;
} else {
fs_user_mount_t* current_mount = get_vfs(lun);
if (current_mount == NULL) {
resplen = -1;
}
if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) {
resplen = -1;
} else {
ejected[lun] = true;
}
}
}
}
break;

default:
// Set Sense = Invalid Command Operation
tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00);
Expand All @@ -127,7 +86,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, const uint8_t scsi_cmd[16], void* buffer,
}

// copy response to stack's buffer if any
if ( response && resplen ) {
if ( response && (resplen > 0) ) {
memcpy(buffer, response, resplen);
}

Expand Down Expand Up @@ -206,3 +165,54 @@ void tud_msc_write10_complete_cb (uint8_t lun) {
// This write is complete, start the autoreload clock.
autoreload_start();
}

// 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) lun;

memcpy(vendor_id , CFG_TUD_MSC_VENDOR , strlen(CFG_TUD_MSC_VENDOR));
memcpy(product_id , CFG_TUD_MSC_PRODUCT , strlen(CFG_TUD_MSC_PRODUCT));
memcpy(product_rev, CFG_TUD_MSC_PRODUCT_REV, strlen(CFG_TUD_MSC_PRODUCT_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) {
if (lun > 1) {
return false;
}

fs_user_mount_t* current_mount = get_vfs(lun);
if (current_mount == NULL) {
return false;
}
if (ejected[lun]) {
return false;
}

return true;
}

// 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) {
if (load_eject) {
if (lun > 1) {
return false;
} else {
fs_user_mount_t* current_mount = get_vfs(lun);
if (current_mount == NULL) {
return false;
}
if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) {
return false;
} else {
ejected[lun] = true;
}
}
}

return true;
}

0 comments on commit c921f66

Please sign in to comment.