Skip to content
18 changes: 9 additions & 9 deletions teensy4/usb.c → teensy4/usb.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "usb_dev.h"
#define USB_DESC_LIST_DEFINE
#include "usb_desc.h"
#include "core_pins.h" // for delay()
#include "usb_serial.h"
#include "usb_seremu.h"
#include "usb_rawhid.h"
Expand All @@ -12,7 +13,6 @@
#include "usb_midi.h"
#include "usb_audio.h"
#include "usb_mtp.h"
#include "core_pins.h" // for delay()
#include "avr/pgmspace.h"
#include <string.h>
#include "debug/printf.h"
Expand Down Expand Up @@ -95,13 +95,13 @@ static uint8_t sof_usage = 0;
static uint8_t usb_reboot_timer = 0;

extern uint8_t usb_descriptor_buffer[]; // defined in usb_desc.c
extern const uint8_t usb_config_descriptor_480[];
extern const uint8_t usb_config_descriptor_12[];
extern "C" const uint8_t usb_config_descriptor_480[];
extern "C" const uint8_t usb_config_descriptor_12[];

void (*usb_timer0_callback)(void) = NULL;
void (*usb_timer1_callback)(void) = NULL;

void usb_isr(void);
extern "C" void usb_isr(void);
static void endpoint0_setup(uint64_t setupdata);
static void endpoint0_transmit(const void *data, uint32_t len, int notify);
static void endpoint0_receive(void *data, uint32_t len, int notify);
Expand Down Expand Up @@ -352,7 +352,7 @@ void usb_isr(void)
//printf("sof %d\n", usb_reboot_timer);
if (usb_reboot_timer) {
if (--usb_reboot_timer == 0) {
usb_stop_sof_interrupts(NUM_INTERFACE);
usb_stop_sof_interrupts(NUM_INTERFACE());
_reboot_Teensyduino_();
}
}
Expand Down Expand Up @@ -809,7 +809,7 @@ static void endpoint0_complete(void)
memcpy(usb_cdc_line_coding, endpoint0_buffer, 7);
printf("usb_cdc_line_coding, baud=%u\n", usb_cdc_line_coding[0]);
if (usb_cdc_line_coding[0] == 134) {
usb_start_sof_interrupts(NUM_INTERFACE);
usb_start_sof_interrupts(NUM_INTERFACE());
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
}
}
Expand All @@ -819,7 +819,7 @@ static void endpoint0_complete(void)
memcpy(usb_cdc2_line_coding, endpoint0_buffer, 7);
printf("usb_cdc2_line_coding, baud=%u\n", usb_cdc2_line_coding[0]);
if (usb_cdc2_line_coding[0] == 134) {
usb_start_sof_interrupts(NUM_INTERFACE);
usb_start_sof_interrupts(NUM_INTERFACE());
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
}
}
Expand All @@ -829,7 +829,7 @@ static void endpoint0_complete(void)
memcpy(usb_cdc3_line_coding, endpoint0_buffer, 7);
printf("usb_cdc3_line_coding, baud=%u\n", usb_cdc3_line_coding[0]);
if (usb_cdc3_line_coding[0] == 134) {
usb_start_sof_interrupts(NUM_INTERFACE);
usb_start_sof_interrupts(NUM_INTERFACE());
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
}
}
Expand All @@ -845,7 +845,7 @@ static void endpoint0_complete(void)
if (endpoint0_buffer[0] == 0xA9 && endpoint0_buffer[1] == 0x45
&& endpoint0_buffer[2] == 0xC2 && endpoint0_buffer[3] == 0x6B) {
printf("seremu reboot request\n");
usb_start_sof_interrupts(NUM_INTERFACE);
usb_start_sof_interrupts(NUM_INTERFACE());
usb_reboot_timer = 80; // TODO: 10 if only 12 Mbit/sec
} else {
// any other feature report means Arduino Serial Monitor is open
Expand Down
18 changes: 9 additions & 9 deletions teensy4/usb_desc.c → teensy4/usb_desc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -668,13 +668,13 @@ static uint8_t microsoft_os_compatible_id_desc[] = {
// USB Configuration Descriptor. This huge descriptor tells all
// of the devices capabilities.

PROGMEM const uint8_t usb_config_descriptor_480[CONFIG_DESC_SIZE] = {
extern "C" PROGMEM const uint8_t usb_config_descriptor_480[CONFIG_DESC_SIZE] = {
// configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
9, // bLength;
2, // bDescriptorType;
LSB(CONFIG_DESC_SIZE), // wTotalLength
MSB(CONFIG_DESC_SIZE),
NUM_INTERFACE, // bNumInterfaces
NUM_INTERFACE(), // bNumInterfaces
1, // bConfigurationValue
0, // iConfiguration
0xC0, // bmAttributes
Expand Down Expand Up @@ -1682,13 +1682,13 @@ PROGMEM const uint8_t usb_config_descriptor_480[CONFIG_DESC_SIZE] = {
};


PROGMEM const uint8_t usb_config_descriptor_12[CONFIG_DESC_SIZE] = {
extern "C" PROGMEM const uint8_t usb_config_descriptor_12[CONFIG_DESC_SIZE] = {
// configuration descriptor, USB spec 9.6.3, page 264-266, Table 9-10
9, // bLength;
2, // bDescriptorType;
LSB(CONFIG_DESC_SIZE), // wTotalLength
MSB(CONFIG_DESC_SIZE),
NUM_INTERFACE, // bNumInterfaces
NUM_INTERFACE(), // bNumInterfaces
1, // bConfigurationValue
0, // iConfiguration
0xC0, // bmAttributes
Expand Down Expand Up @@ -2719,9 +2719,9 @@ struct usb_string_descriptor_struct {
};
*/

extern struct usb_string_descriptor_struct usb_string_manufacturer_name
extern const struct usb_string_descriptor_struct usb_string_manufacturer_name
__attribute__ ((weak, alias("usb_string_manufacturer_name_default")));
extern struct usb_string_descriptor_struct usb_string_product_name
extern const struct usb_string_descriptor_struct usb_string_product_name
__attribute__ ((weak, alias("usb_string_product_name_default")));
extern struct usb_string_descriptor_struct usb_string_serial_number
__attribute__ ((weak, alias("usb_string_serial_number_default")));
Expand All @@ -2732,12 +2732,12 @@ PROGMEM const struct usb_string_descriptor_struct string0 = {
{0x0409}
};

PROGMEM const struct usb_string_descriptor_struct usb_string_manufacturer_name_default = {
extern "C" PROGMEM const struct usb_string_descriptor_struct usb_string_manufacturer_name_default = {
2 + MANUFACTURER_NAME_LEN * 2,
3,
MANUFACTURER_NAME
};
PROGMEM const struct usb_string_descriptor_struct usb_string_product_name_default = {
extern "C" PROGMEM const struct usb_string_descriptor_struct usb_string_product_name_default = {
2 + PRODUCT_NAME_LEN * 2,
3,
PRODUCT_NAME
Expand All @@ -2755,7 +2755,7 @@ PROGMEM const struct usb_string_descriptor_struct usb_string_mtp = {
};
#endif

void usb_init_serialnumber(void)
extern "C" void usb_init_serialnumber(void)
{
char buf[11];
uint32_t i, num;
Expand Down
75 changes: 50 additions & 25 deletions teensy4/usb_desc.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ To modify a USB Type, delete the XYZ_INTERFACE lines for any
interfaces you wish to remove, and copy them from another USB Type
for any you want to add.

Give each interface a unique number, and edit NUM_INTERFACE to
reflect the total number of interfaces.
Give each interface a unique number.

Next, assign unique endpoint numbers to all the endpoints across
all the interfaces your device has. You can reuse an endpoint
Expand Down Expand Up @@ -121,7 +120,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_USB_BUFFERS 12
#define NUM_INTERFACE 3
#define CDC_IAD_DESCRIPTOR 1 // Serial
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1
Expand All @@ -147,7 +145,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 11
#define EP0_SIZE 64
#define NUM_ENDPOINTS 5
#define NUM_INTERFACE 4
#define CDC_IAD_DESCRIPTOR 1 // Serial
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1
Expand Down Expand Up @@ -178,7 +175,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 13
#define EP0_SIZE 64
#define NUM_ENDPOINTS 7
#define NUM_INTERFACE 6
#define CDC_IAD_DESCRIPTOR 1 // Serial
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1
Expand Down Expand Up @@ -217,7 +213,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_USB_BUFFERS 14
#define NUM_INTERFACE 3
#define SEREMU_INTERFACE 1 // Serial emulation
#define SEREMU_TX_ENDPOINT 2
#define SEREMU_TX_SIZE 64
Expand Down Expand Up @@ -247,7 +242,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define EP0_SIZE 64
#define NUM_ENDPOINTS 6
#define NUM_USB_BUFFERS 24
#define NUM_INTERFACE 5
#define SEREMU_INTERFACE 2 // Serial emulation
#define SEREMU_TX_ENDPOINT 2
#define SEREMU_TX_SIZE 64
Expand Down Expand Up @@ -289,7 +283,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 30
#define EP0_SIZE 64
#define NUM_ENDPOINTS 7
#define NUM_INTERFACE 6
#define CDC_IAD_DESCRIPTOR 1
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1 // Serial
Expand Down Expand Up @@ -333,7 +326,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 20
#define EP0_SIZE 64
#define NUM_ENDPOINTS 5
#define NUM_INTERFACE 4
#define SEREMU_INTERFACE 1 // Serial emulation
#define SEREMU_TX_ENDPOINT 2
#define SEREMU_TX_SIZE 64
Expand Down Expand Up @@ -367,7 +359,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 26
#define EP0_SIZE 64
#define NUM_ENDPOINTS 6
#define NUM_INTERFACE 5
#define SEREMU_INTERFACE 2 // Serial emulation
#define SEREMU_TX_ENDPOINT 2
#define SEREMU_TX_SIZE 64
Expand Down Expand Up @@ -406,7 +397,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 11
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_INTERFACE 2
#define SEREMU_INTERFACE 1 // Serial emulation
#define SEREMU_TX_ENDPOINT 2
#define SEREMU_TX_SIZE 64
Expand Down Expand Up @@ -435,7 +425,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 13
#define EP0_SIZE 64
#define NUM_ENDPOINTS 3
#define NUM_INTERFACE 2
#define SEREMU_INTERFACE 1 // Serial emulation
#define SEREMU_TX_ENDPOINT 2
#define SEREMU_TX_SIZE 64
Expand Down Expand Up @@ -464,7 +453,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 14
#define EP0_SIZE 64
#define NUM_ENDPOINTS 3
#define NUM_INTERFACE 2
#define SEREMU_INTERFACE 1 // Serial emulation
#define SEREMU_TX_ENDPOINT 2
#define SEREMU_TX_SIZE 64
Expand Down Expand Up @@ -492,7 +480,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 11
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_INTERFACE 3
#define CDC_IAD_DESCRIPTOR 1
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1 // Serial
Expand Down Expand Up @@ -526,7 +513,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 13
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_INTERFACE 3
#define CDC_IAD_DESCRIPTOR 1
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1 // Serial
Expand Down Expand Up @@ -560,7 +546,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 14
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_INTERFACE 3
#define CDC_IAD_DESCRIPTOR 1
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1 // Serial
Expand Down Expand Up @@ -595,7 +580,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 18
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_INTERFACE 2
#define RAWHID_INTERFACE 0 // RawHID
#define RAWHID_TX_ENDPOINT 3
#define RAWHID_TX_SIZE 64
Expand Down Expand Up @@ -623,7 +607,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 26
#define EP0_SIZE 64
#define NUM_ENDPOINTS 3
#define NUM_INTERFACE 2
#define FLIGHTSIM_INTERFACE 0 // Flight Sim Control
#define FLIGHTSIM_TX_ENDPOINT 3
#define FLIGHTSIM_TX_SIZE 64
Expand Down Expand Up @@ -651,7 +634,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 26
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_INTERFACE 3
#define FLIGHTSIM_INTERFACE 0 // Flight Sim Control
#define FLIGHTSIM_TX_ENDPOINT 3
#define FLIGHTSIM_TX_SIZE 64
Expand Down Expand Up @@ -683,7 +665,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 15
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_INTERFACE 2
#define MTP_INTERFACE 1 // MTP Disk
#define MTP_TX_ENDPOINT 3
#define MTP_TX_SIZE_12 64
Expand Down Expand Up @@ -715,7 +696,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 15
#define EP0_SIZE 64
#define NUM_ENDPOINTS 5
#define NUM_INTERFACE 3
#define CDC_IAD_DESCRIPTOR 1
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1 // Serial
Expand Down Expand Up @@ -752,7 +732,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 12
#define EP0_SIZE 64
#define NUM_ENDPOINTS 4
#define NUM_INTERFACE 4
#define SEREMU_INTERFACE 0 // Serial emulation
#define SEREMU_TX_ENDPOINT 2
#define SEREMU_TX_SIZE 64
Expand All @@ -779,7 +758,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 17
#define EP0_SIZE 64
#define NUM_ENDPOINTS 6
#define NUM_INTERFACE 6
#define CDC_IAD_DESCRIPTOR 1
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1 // Serial
Expand Down Expand Up @@ -821,7 +799,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 20
#define EP0_SIZE 64
#define NUM_ENDPOINTS 8
#define NUM_INTERFACE 6
#define CDC_IAD_DESCRIPTOR 1
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1 // Serial
Expand Down Expand Up @@ -867,7 +844,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define PRODUCT_NAME_LEN 14
#define EP0_SIZE 64
#define NUM_ENDPOINTS 15
#define NUM_INTERFACE 13
#define CDC_IAD_DESCRIPTOR 1
#define CDC_STATUS_INTERFACE 0
#define CDC_DATA_INTERFACE 1 // Serial
Expand Down Expand Up @@ -967,3 +943,52 @@ extern const usb_descriptor_list_t usb_descriptor_list[];
#endif // NUM_ENDPOINTS
#endif // USB_DESC_LIST_DEFINE

constexpr uint8_t NUM_INTERFACE() {
uint8_t interfaceCount = 0;
#ifdef CDC_DATA_INTERFACE
interfaceCount += 2;
#endif
#ifdef CDC2_DATA_INTERFACE
interfaceCount += 2;
#endif
#ifdef CDC3_DATA_INTERFACE
interfaceCount += 2;
#endif
#ifdef MIDI_INTERFACE
interfaceCount += 1;
#endif
#ifdef KEYBOARD_INTERFACE
interfaceCount += 1;
#endif
#ifdef MOUSE_INTERFACE
interfaceCount += 1;
#endif
#ifdef RAWHID_INTERFACE
interfaceCount += 1;
#endif
#ifdef FLIGHTSIM_INTERFACE
interfaceCount += 1;
#endif
#ifdef SEREMU_INTERFACE
interfaceCount += 1;
#endif
#ifdef JOYSTICK_INTERFACE
interfaceCount += 1;
#endif
#ifdef MTP_INTERFACE
interfaceCount += 1;
#endif
#ifdef KEYMEDIA_INTERFACE
interfaceCount += 1;
#endif
#ifdef AUDIO_INTERFACE
interfaceCount += 3;
#endif
#ifdef MULTITOUCH_INTERFACE
interfaceCount += 1;
#endif
#ifdef EXPERIMENTAL_INTERFACE
interfaceCount += 1;
#endif
return interfaceCount;
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading