update tinyusb lib to 0.5.x

This commit is contained in:
hathach 2019-07-24 16:46:31 +07:00
parent 82ab998cd5
commit c921f6637f
8 changed files with 90 additions and 109 deletions

@ -1 +1 @@
Subproject commit 0848c462b3e431a9da42e96537d2b597a4579636
Subproject commit 1ee9ef4f2b7c6acfab6c398a4f57ca22036958f7

View File

@ -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

View File

@ -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"));
}
}
@ -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;
@ -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 ) {

View File

@ -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();
}

View File

@ -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

View File

@ -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;
}

View File

@ -1,43 +0,0 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2018 hathach for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#ifndef USB_DESC_H_
#define USB_DESC_H_
#include "lib/tinyusb/src/tusb.h"
#ifdef __cplusplus
extern "C" {
#endif
// Descriptors set used by tinyusb stack
extern tud_desc_set_t tud_desc_set;
#ifdef __cplusplus
}
#endif
#endif /* USB_DESC_H_ */

View File

@ -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);
@ -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);
}
@ -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;
}