Add firmware
This commit is contained in:
47
firmware/base/tusb/tusb_config.h
Normal file
47
firmware/base/tusb/tusb_config.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_MS_OS_20_DESCRIPTOR
|
||||
#undef PICO_STDIO_USB_RESET_INTERFACE_SUPPORT_MS_OS_20_DESCRIPTOR
|
||||
#endif
|
||||
|
||||
#define CFG_TUSB_RHPORT0_MODE (OPT_MODE_DEVICE | OPT_MODE_FULL_SPEED)
|
||||
#ifndef BOARD_TUD_RHPORT
|
||||
#define BOARD_TUD_RHPORT 0
|
||||
#endif
|
||||
|
||||
#ifndef BOARD_TUD_MAX_SPEED
|
||||
#define BOARD_TUD_MAX_SPEED OPT_MODE_DEFAULT_SPEED
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUSB_MCU
|
||||
#error CFG_TUSB_MCU must be defined
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUSB_OS
|
||||
#define CFG_TUSB_OS OPT_OS_NONE
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_ENABLED
|
||||
#define CFG_TUD_ENABLED 1
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_MAX_SPEED
|
||||
#define CFG_TUD_MAX_SPEED BOARD_TUD_MAX_SPEED
|
||||
#endif
|
||||
|
||||
#ifndef CFG_TUD_ENDPOINT0_SIZE
|
||||
#define CFG_TUD_ENDPOINT0_SIZE 64
|
||||
#endif
|
||||
|
||||
#define CFG_TUD_CDC 2
|
||||
#define CFG_TUD_MSC 0
|
||||
#define CFG_TUD_HID 0
|
||||
#define CFG_TUD_MIDI 0
|
||||
#define CFG_TUD_VENDOR 0
|
||||
|
||||
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
|
||||
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
|
||||
|
||||
#define CFG_TUD_CDC_EP_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
|
||||
|
||||
#define CFG_TUD_MSC_EP_BUFSIZE 512
|
||||
143
firmware/base/tusb/tusb_descriptors.c
Normal file
143
firmware/base/tusb/tusb_descriptors.c
Normal file
@@ -0,0 +1,143 @@
|
||||
#include "bsp/board_api.h"
|
||||
#include "tusb.h"
|
||||
|
||||
#define _PID_MAP(itf,n) ( (CFG_TUD_##itf) << (n) )
|
||||
#define USB_PID (0x4d47 | _PID_MAP(CDC, 0) | _PID_MAP(MSC, 1)| _PID_MAP(HID, 2) | \
|
||||
_PID_MAP(MIDI, 3) | _PID_MAP(VENDOR, 4) )
|
||||
|
||||
#define USB_VID 0x1209
|
||||
#define USB_BCD 0x0200
|
||||
|
||||
tusb_desc_device_t const desc_device = {
|
||||
.bLength = sizeof(tusb_desc_device_t),
|
||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||
.bcdUSB = USB_BCD,
|
||||
|
||||
.bDeviceClass = TUSB_CLASS_MISC,
|
||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
|
||||
.bDeviceProtocol = MISC_PROTOCOL_IAD,
|
||||
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
|
||||
|
||||
.idVendor = USB_VID,
|
||||
.idProduct = USB_PID,
|
||||
.bcdDevice = 0x0100,
|
||||
|
||||
.iManufacturer = 0x01,
|
||||
.iProduct = 0x02,
|
||||
.iSerialNumber = 0x03,
|
||||
|
||||
.bNumConfigurations = 0x01
|
||||
};
|
||||
|
||||
|
||||
uint8_t const * tud_descriptor_device_cb(void) {
|
||||
return (uint8_t const *) &desc_device;
|
||||
}
|
||||
|
||||
enum
|
||||
{
|
||||
ITF_NUM_CDC_0 = 0,
|
||||
ITF_NUM_CDC_0_DATA,
|
||||
ITF_NUM_CDC_1,
|
||||
ITF_NUM_CDC_1_DATA,
|
||||
ITF_NUM_TOTAL,
|
||||
};
|
||||
|
||||
|
||||
#define CONFIG_TOTAL_LEN (TUD_CONFIG_DESC_LEN + CFG_TUD_CDC * TUD_CDC_DESC_LEN)
|
||||
|
||||
#define EPNUM_CDC_NOTIF 0x81
|
||||
#define EPNUM_CDC_OUT 0x02
|
||||
#define EPNUM_CDC_IN 0x82
|
||||
|
||||
#define EPNUM_CDC_1_NOTIF 0x84
|
||||
#define EPNUM_CDC_1_OUT 0x05
|
||||
#define EPNUM_CDC_1_IN 0x85
|
||||
|
||||
|
||||
uint8_t const desc_fs_configuration[] =
|
||||
{
|
||||
TUD_CONFIG_DESCRIPTOR(1, ITF_NUM_TOTAL, 0, CONFIG_TOTAL_LEN, 0x00, 250),
|
||||
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_0, 4, EPNUM_CDC_NOTIF, 8, EPNUM_CDC_OUT, EPNUM_CDC_IN, 64),
|
||||
TUD_CDC_DESCRIPTOR(ITF_NUM_CDC_1, 4, EPNUM_CDC_1_NOTIF, 8, EPNUM_CDC_1_OUT, EPNUM_CDC_1_IN, 64)
|
||||
};
|
||||
|
||||
|
||||
tusb_desc_device_qualifier_t const desc_device_qualifier =
|
||||
{
|
||||
.bLength = sizeof(tusb_desc_device_qualifier_t),
|
||||
.bDescriptorType = TUSB_DESC_DEVICE,
|
||||
.bcdUSB = USB_BCD,
|
||||
|
||||
.bDeviceClass = TUSB_CLASS_MISC,
|
||||
.bDeviceSubClass = MISC_SUBCLASS_COMMON,
|
||||
.bDeviceProtocol = MISC_PROTOCOL_IAD,
|
||||
|
||||
.bMaxPacketSize0 = CFG_TUD_ENDPOINT0_SIZE,
|
||||
.bNumConfigurations = 0x01
|
||||
};
|
||||
|
||||
|
||||
uint8_t const* tud_descriptor_device_qualifier_cb(void) {
|
||||
return (uint8_t const*) &desc_device_qualifier;
|
||||
}
|
||||
|
||||
|
||||
uint8_t const* tud_descriptor_configuration_cb(uint8_t index) {
|
||||
(void) index;
|
||||
|
||||
return desc_fs_configuration;
|
||||
}
|
||||
|
||||
enum {
|
||||
STRID_LANGID = 0,
|
||||
STRID_MANUFACTURER,
|
||||
STRID_PRODUCT,
|
||||
STRID_SERIAL,
|
||||
STRID_CDC_0,
|
||||
STRID_CDC_1
|
||||
};
|
||||
|
||||
char const *string_desc_arr[] = {
|
||||
(const char[]) { 0x09, 0x04},
|
||||
"Mg Robotics",
|
||||
"Magrob Odometry MCU",
|
||||
NULL,
|
||||
"Odometry CDC",
|
||||
"Stepper CDC"
|
||||
};
|
||||
|
||||
static uint16_t _desc_str[32+1];
|
||||
|
||||
uint16_t const *tud_descriptor_string_cb(uint8_t index, uint16_t langid) {
|
||||
(void) langid;
|
||||
size_t chr_count;
|
||||
|
||||
switch (index) {
|
||||
case STRID_LANGID:
|
||||
memcpy(&_desc_str[1], string_desc_arr[0], 2);
|
||||
chr_count = 1;
|
||||
break;
|
||||
|
||||
case STRID_SERIAL:
|
||||
chr_count = board_usb_get_serial(_desc_str + 1, 32);
|
||||
break;
|
||||
|
||||
default:
|
||||
if( !(index < sizeof(string_desc_arr) / sizeof(string_desc_arr[0])) ) return NULL;
|
||||
|
||||
const char *str = string_desc_arr[index];
|
||||
|
||||
chr_count = strlen(str);
|
||||
size_t const max_count = sizeof(_desc_str) / sizeof(_desc_str[0]) - 1;
|
||||
if ( chr_count > max_count ) chr_count = max_count;
|
||||
|
||||
for(size_t i = 0; i < chr_count; i++) {
|
||||
_desc_str[i + 1] = str[i];
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
_desc_str[0] = (uint16_t) ((TUSB_DESC_STRING << 8) | (2 * chr_count + 2));
|
||||
return _desc_str;
|
||||
}
|
||||
130
firmware/base/tusb/tusb_stdio_driver.c
Normal file
130
firmware/base/tusb/tusb_stdio_driver.c
Normal file
@@ -0,0 +1,130 @@
|
||||
#include "tusb_stdio_driver.h"
|
||||
#include "tusb.h"
|
||||
|
||||
#include "class/cdc/cdc_device.h"
|
||||
#include "device/usbd.h"
|
||||
#include "pico/sem.h"
|
||||
#include "pico/stdio/driver.h"
|
||||
#include "pico/time.h"
|
||||
|
||||
|
||||
#include "hardware/irq.h"
|
||||
#include "hardware/timer.h"
|
||||
|
||||
#define STDIO_DUAL_USB_STDOUT_TIMEOUT_US 10000
|
||||
|
||||
semaphore_t stdio_dual_usb_mutex;
|
||||
static uint8_t low_priority_irq_num;
|
||||
|
||||
bool stdio_dual_usb_connected() {
|
||||
const int itf = get_core_num();
|
||||
return tud_cdc_n_connected(itf);
|
||||
}
|
||||
|
||||
static void low_priority_irq_worker(void) {
|
||||
if (sem_try_acquire(&stdio_dual_usb_mutex)) {
|
||||
tud_task();
|
||||
sem_release(&stdio_dual_usb_mutex);
|
||||
}
|
||||
}
|
||||
|
||||
static void stdio_dual_usb_out_chars(const char *buf, int length) {
|
||||
static uint64_t last_avail_time[2] = {};
|
||||
const int itf = get_core_num();
|
||||
// if (!sem_acquire_timeout_ms(&stdio_dual_usb_mutex, 500)) {
|
||||
// return;
|
||||
// }
|
||||
sem_acquire_blocking(&stdio_dual_usb_mutex);
|
||||
if (stdio_dual_usb_connected()) {
|
||||
for (int i = 0; i < length;) {
|
||||
int n = length - i;
|
||||
int avail = (int)tud_cdc_n_write_available(itf);
|
||||
if (n > avail)
|
||||
n = avail;
|
||||
if (n) {
|
||||
int n2 = (int)tud_cdc_n_write(itf, buf + i, (uint32_t)n);
|
||||
tud_task();
|
||||
tud_cdc_n_write_flush(itf);
|
||||
i += n2;
|
||||
last_avail_time[itf] = time_us_64();
|
||||
} else {
|
||||
tud_task();
|
||||
tud_cdc_n_write_flush(itf);
|
||||
if (!stdio_dual_usb_connected() ||
|
||||
(!tud_cdc_write_available() &&
|
||||
time_us_64() >
|
||||
last_avail_time[itf] + STDIO_DUAL_USB_STDOUT_TIMEOUT_US)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
last_avail_time[itf] = 0;
|
||||
tud_task();
|
||||
}
|
||||
sem_release(&stdio_dual_usb_mutex);
|
||||
}
|
||||
|
||||
static void stdio_dual_usb_out_flush(void) {
|
||||
const int itf = get_core_num();
|
||||
// if (!sem_acquire_timeout_ms(&stdio_dual_usb_mutex, 500)) {
|
||||
// return;
|
||||
// }
|
||||
sem_acquire_blocking(&stdio_dual_usb_mutex);
|
||||
int last_time = time_us_64();
|
||||
do {
|
||||
tud_task();
|
||||
} while (tud_cdc_n_write_flush(itf) && time_us_64() > last_time + STDIO_DUAL_USB_STDOUT_TIMEOUT_US);
|
||||
sem_release(&stdio_dual_usb_mutex);
|
||||
}
|
||||
|
||||
int stdio_dual_usb_in_chars(char *buf, int length) {
|
||||
const int itf = get_core_num();
|
||||
int rc = PICO_ERROR_NO_DATA;
|
||||
// if (!sem_acquire_timeout_ms(&stdio_dual_usb_mutex, 500)) {
|
||||
// return rc;
|
||||
// }
|
||||
sem_acquire_blocking(&stdio_dual_usb_mutex);
|
||||
if (stdio_dual_usb_connected() && tud_cdc_n_available(itf)) {
|
||||
int count = (int)tud_cdc_n_read(itf, buf, (uint32_t)length);
|
||||
rc = count ? count : PICO_ERROR_NO_DATA;
|
||||
} else {
|
||||
tud_task();
|
||||
}
|
||||
sem_release(&stdio_dual_usb_mutex);
|
||||
return rc;
|
||||
}
|
||||
|
||||
void irq_handler() { irq_set_pending(low_priority_irq_num); }
|
||||
|
||||
stdio_driver_t stdio_dual_usb = {
|
||||
.out_chars = stdio_dual_usb_out_chars,
|
||||
.out_flush = stdio_dual_usb_out_flush,
|
||||
.in_chars = stdio_dual_usb_in_chars,
|
||||
.crlf_enabled = PICO_STDIO_DEFAULT_CRLF,
|
||||
};
|
||||
|
||||
bool stdio_dual_usb_init(void) {
|
||||
if (get_core_num() != alarm_pool_core_num(alarm_pool_get_default())) {
|
||||
assert(false);
|
||||
return false;
|
||||
}
|
||||
|
||||
assert(tud_inited());
|
||||
|
||||
sem_init(&stdio_dual_usb_mutex, 1,1);
|
||||
// low_priority_irq_num = user_irq_claim_unused(true);
|
||||
// irq_set_exclusive_handler(low_priority_irq_num, low_priority_irq_worker);
|
||||
// irq_set_enabled(low_priority_irq_num, true);
|
||||
|
||||
// if (irq_has_handler(USBCTRL_IRQ)) {
|
||||
// irq_add_shared_handler(USBCTRL_IRQ, irq_handler,
|
||||
// PICO_SHARED_IRQ_HANDLER_LOWEST_ORDER_PRIORITY);
|
||||
// } else {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
stdio_set_driver_enabled(&stdio_dual_usb, true);
|
||||
|
||||
return true;
|
||||
}
|
||||
7
firmware/base/tusb/tusb_stdio_driver.h
Normal file
7
firmware/base/tusb/tusb_stdio_driver.h
Normal file
@@ -0,0 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "pico.h"
|
||||
#include "pico/sem.h"
|
||||
|
||||
extern semaphore_t stdio_dual_usb_mutex;
|
||||
bool stdio_dual_usb_init(void);
|
||||
Reference in New Issue
Block a user