2018-07-17 10:24:49 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the Micro Python 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.
|
|
|
|
*/
|
|
|
|
|
2019-07-31 00:30:24 -04:00
|
|
|
#include "py/objstr.h"
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "shared-bindings/microcontroller/Processor.h"
|
2018-11-16 20:04:42 -05:00
|
|
|
#include "shared-module/usb_midi/__init__.h"
|
2020-07-07 12:14:43 -04:00
|
|
|
#include "supervisor/background_callback.h"
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "supervisor/port.h"
|
2020-09-03 14:36:15 -04:00
|
|
|
#include "supervisor/serial.h"
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "supervisor/usb.h"
|
2020-11-19 17:47:12 -05:00
|
|
|
#include "supervisor/shared/workflow.h"
|
2018-07-17 10:24:49 -04:00
|
|
|
#include "lib/utils/interrupt_char.h"
|
2018-07-31 03:28:34 -04:00
|
|
|
#include "lib/mp-readline/readline.h"
|
2018-07-17 10:24:49 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
#include "tusb.h"
|
2018-07-17 10:24:49 -04:00
|
|
|
|
2021-01-25 21:37:58 -05:00
|
|
|
#if CIRCUITPY_USB_VENDOR
|
|
|
|
#include "genhdr/autogen_usb_descriptor.h"
|
|
|
|
|
|
|
|
// The WebUSB support being conditionally added to this file is based on the
|
|
|
|
// tinyusb demo examples/device/webusb_serial.
|
|
|
|
|
|
|
|
extern const tusb_desc_webusb_url_t desc_webusb_url;
|
|
|
|
|
|
|
|
static bool web_serial_connected = false;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
// Serial number as hex characters. This writes directly to the USB
|
|
|
|
// descriptor.
|
|
|
|
extern uint16_t usb_serial_number[1 + COMMON_HAL_MCU_PROCESSOR_UID_LENGTH * 2];
|
2018-07-17 12:57:49 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
void load_serial_number(void) {
|
2018-08-23 03:00:54 -04:00
|
|
|
// create serial number based on device unique id
|
2018-10-19 21:46:22 -04:00
|
|
|
uint8_t raw_id[COMMON_HAL_MCU_PROCESSOR_UID_LENGTH];
|
|
|
|
common_hal_mcu_processor_get_uid(raw_id);
|
|
|
|
|
|
|
|
for (int i = 0; i < COMMON_HAL_MCU_PROCESSOR_UID_LENGTH; i++) {
|
|
|
|
for (int j = 0; j < 2; j++) {
|
|
|
|
uint8_t nibble = (raw_id[i] >> (j * 4)) & 0xf;
|
|
|
|
// Strings are UTF-16-LE encoded.
|
2019-07-31 00:30:24 -04:00
|
|
|
usb_serial_number[1 + i * 2 + j] = nibble_to_hex_upper[nibble];
|
2018-10-19 21:46:22 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 03:00:54 -04:00
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
bool usb_enabled(void) {
|
2019-01-29 08:47:01 -05:00
|
|
|
return tusb_inited();
|
2018-10-19 21:46:22 -04:00
|
|
|
}
|
2018-08-23 03:00:54 -04:00
|
|
|
|
2021-01-21 14:33:13 -05:00
|
|
|
MP_WEAK void post_usb_init(void) {}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
void usb_init(void) {
|
|
|
|
init_usb_hardware();
|
|
|
|
load_serial_number();
|
2018-08-23 03:00:54 -04:00
|
|
|
|
2018-07-17 10:24:49 -04:00
|
|
|
tusb_init();
|
2018-07-31 03:28:34 -04:00
|
|
|
|
2021-01-21 14:33:13 -05:00
|
|
|
post_usb_init();
|
|
|
|
|
2018-07-31 03:28:34 -04:00
|
|
|
#if MICROPY_KBD_EXCEPTION
|
2020-07-20 09:45:31 -04:00
|
|
|
// Set Ctrl+C as wanted char, tud_cdc_rx_wanted_cb() usb_callback will be invoked when Ctrl+C is received
|
|
|
|
// This usb_callback always got invoked regardless of mp_interrupt_char value since we only set it once here
|
2018-07-31 03:28:34 -04:00
|
|
|
tud_cdc_set_wanted_char(CHAR_CTRL_C);
|
|
|
|
#endif
|
2018-11-16 20:04:42 -05:00
|
|
|
|
2019-04-12 18:25:48 -04:00
|
|
|
#if CIRCUITPY_USB_MIDI
|
2018-11-16 20:04:42 -05:00
|
|
|
usb_midi_init();
|
2019-04-12 18:25:48 -04:00
|
|
|
#endif
|
2018-07-17 10:24:49 -04:00
|
|
|
}
|
|
|
|
|
2020-07-28 17:52:56 -04:00
|
|
|
void usb_disconnect(void) {
|
|
|
|
tud_disconnect();
|
|
|
|
}
|
|
|
|
|
2018-10-19 21:46:22 -04:00
|
|
|
void usb_background(void) {
|
2019-01-30 02:13:07 -05:00
|
|
|
if (usb_enabled()) {
|
2020-04-17 19:23:28 -04:00
|
|
|
#if CFG_TUSB_OS == OPT_OS_NONE
|
2018-11-16 20:04:42 -05:00
|
|
|
tud_task();
|
2020-04-17 19:23:28 -04:00
|
|
|
#endif
|
2018-10-19 21:46:22 -04:00
|
|
|
tud_cdc_write_flush();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-20 09:45:31 -04:00
|
|
|
static background_callback_t usb_callback;
|
2020-07-07 12:14:43 -04:00
|
|
|
static void usb_background_do(void* unused) {
|
|
|
|
usb_background();
|
|
|
|
}
|
|
|
|
|
2021-02-04 04:00:14 -05:00
|
|
|
void usb_background_schedule(void)
|
|
|
|
{
|
2020-07-20 09:45:31 -04:00
|
|
|
background_callback_add(&usb_callback, usb_background_do, NULL);
|
2020-07-07 12:14:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void usb_irq_handler(void) {
|
|
|
|
tud_int_handler(0);
|
2021-02-04 04:00:14 -05:00
|
|
|
usb_background_schedule();
|
2020-07-07 12:14:43 -04:00
|
|
|
}
|
|
|
|
|
2018-07-17 10:24:49 -04:00
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// tinyusb callbacks
|
|
|
|
//--------------------------------------------------------------------+
|
2018-08-21 04:15:44 -04:00
|
|
|
|
|
|
|
// Invoked when device is mounted
|
2018-07-17 10:24:49 -04:00
|
|
|
void tud_mount_cb(void) {
|
2019-12-17 22:01:03 -05:00
|
|
|
usb_msc_mount();
|
2018-07-17 10:24:49 -04:00
|
|
|
}
|
|
|
|
|
2018-08-21 04:15:44 -04:00
|
|
|
// Invoked when device is unmounted
|
2018-07-17 10:24:49 -04:00
|
|
|
void tud_umount_cb(void) {
|
2019-12-17 22:01:03 -05:00
|
|
|
usb_msc_umount();
|
2018-07-17 10:24:49 -04:00
|
|
|
}
|
|
|
|
|
2019-04-02 02:04:02 -04:00
|
|
|
// Invoked when usb bus is suspended
|
|
|
|
// remote_wakeup_en : if host allows us to perform remote wakeup
|
|
|
|
// USB Specs: Within 7ms, device must draw an average current less than 2.5 mA from bus
|
|
|
|
void tud_suspend_cb(bool remote_wakeup_en) {
|
2018-07-17 10:24:49 -04:00
|
|
|
}
|
|
|
|
|
2019-04-02 02:04:02 -04:00
|
|
|
// Invoked when usb bus is resumed
|
|
|
|
void tud_resume_cb(void) {
|
|
|
|
}
|
2018-08-21 04:15:44 -04:00
|
|
|
|
|
|
|
// Invoked when cdc when line state changed e.g connected/disconnected
|
|
|
|
// Use to reset to DFU when disconnect with 1200 bps
|
|
|
|
void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
|
|
|
|
(void) itf; // interface ID, not used
|
|
|
|
|
2018-09-04 07:36:08 -04:00
|
|
|
// DTR = false is counted as disconnected
|
|
|
|
if ( !dtr )
|
2018-08-21 04:15:44 -04:00
|
|
|
{
|
|
|
|
cdc_line_coding_t coding;
|
|
|
|
tud_cdc_get_line_coding(&coding);
|
|
|
|
|
|
|
|
if ( coding.bit_rate == 1200 )
|
|
|
|
{
|
2018-10-19 21:46:22 -04:00
|
|
|
reset_to_bootloader();
|
2018-08-21 04:15:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-25 21:37:58 -05:00
|
|
|
#if CIRCUITPY_USB_VENDOR
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
// WebUSB use vendor class
|
|
|
|
//--------------------------------------------------------------------+
|
|
|
|
|
|
|
|
bool tud_vendor_connected(void)
|
|
|
|
{
|
|
|
|
return web_serial_connected;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Invoked when a control transfer occurred on an interface of this class
|
|
|
|
// Driver response accordingly to the request and the transfer stage (setup/data/ack)
|
|
|
|
// return false to stall control endpoint (e.g unsupported request)
|
|
|
|
bool tud_vendor_control_xfer_cb(uint8_t rhport, uint8_t stage, tusb_control_request_t const * request)
|
|
|
|
{
|
|
|
|
// nothing to with DATA & ACK stage
|
|
|
|
if (stage != CONTROL_STAGE_SETUP ) return true;
|
|
|
|
|
|
|
|
switch (request->bRequest)
|
|
|
|
{
|
|
|
|
case VENDOR_REQUEST_WEBUSB:
|
|
|
|
// match vendor request in BOS descriptor
|
|
|
|
// Get landing page url
|
|
|
|
return tud_control_xfer(rhport, request, (void*) &desc_webusb_url, desc_webusb_url.bLength);
|
|
|
|
|
|
|
|
case VENDOR_REQUEST_MICROSOFT:
|
|
|
|
if ( request->wIndex == 7 )
|
|
|
|
{
|
|
|
|
// Get Microsoft OS 2.0 compatible descriptor
|
|
|
|
uint16_t total_len;
|
|
|
|
memcpy(&total_len, desc_ms_os_20+8, 2);
|
|
|
|
|
|
|
|
return tud_control_xfer(rhport, request, (void*) desc_ms_os_20, total_len);
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
case 0x22:
|
|
|
|
// Webserial simulate the CDC_REQUEST_SET_CONTROL_LINE_STATE (0x22) to
|
|
|
|
// connect and disconnect.
|
|
|
|
web_serial_connected = (request->wValue != 0);
|
|
|
|
|
|
|
|
// response with status OK
|
|
|
|
return tud_control_status(rhport, request);
|
|
|
|
|
|
|
|
default:
|
|
|
|
// stall unknown request
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif CIRCUITPY_USB_VENDOR
|
|
|
|
|
|
|
|
|
2018-07-17 10:24:49 -04:00
|
|
|
#if MICROPY_KBD_EXCEPTION
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Callback invoked when received an "wanted" char.
|
|
|
|
* @param itf Interface index (for multiple cdc interfaces)
|
|
|
|
* @param wanted_char The wanted char (set previously)
|
|
|
|
*/
|
|
|
|
void tud_cdc_rx_wanted_cb(uint8_t itf, char wanted_char)
|
|
|
|
{
|
|
|
|
(void) itf; // not used
|
|
|
|
|
2018-07-31 03:28:34 -04:00
|
|
|
// Workaround for using lib/utils/interrupt_char.c
|
|
|
|
// Compare mp_interrupt_char with wanted_char and ignore if not matched
|
2018-07-17 10:24:49 -04:00
|
|
|
if (mp_interrupt_char == wanted_char) {
|
2018-07-17 10:52:20 -04:00
|
|
|
tud_cdc_read_flush(); // flush read fifo
|
2018-07-17 10:24:49 -04:00
|
|
|
mp_keyboard_interrupt();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|