Переглянути джерело

Simplify usb_desc.h configuration - do not require computing descriptor sizes/offsets

teensy4-core
PaulStoffregen 10 роки тому
джерело
коміт
8111996bdc
3 змінених файлів з 156 додано та 62 видалено
  1. +86
    -9
      teensy3/usb_desc.c
  2. +53
    -35
      teensy3/usb_desc.h
  3. +17
    -18
      teensy3/usb_inst.cpp

+ 86
- 9
teensy3/usb_desc.c Переглянути файл

@@ -301,6 +301,86 @@ static uint8_t flightsim_report_desc[] = {
#endif


// **************************************************************
// USB Descriptor Sizes
// **************************************************************

// pre-compute the size and position of everything in the config descriptor
//
#define CONFIG_HEADER_DESCRIPTOR_SIZE 9

#define CDC_IAD_DESCRIPTOR_POS CONFIG_HEADER_DESCRIPTOR_SIZE
#ifdef CDC_IAD_DESCRIPTOR
#define CDC_IAD_DESCRIPTOR_SIZE 8
#else
#define CDC_IAD_DESCRIPTOR_SIZE 0
#endif

#define CDC_DATA_INTERFACE_DESC_POS CDC_IAD_DESCRIPTOR_POS+CDC_IAD_DESCRIPTOR_SIZE
#ifdef CDC_DATA_INTERFACE
#define CDC_DATA_INTERFACE_DESC_SIZE 9+5+5+4+5+7+9+7+7
#else
#define CDC_DATA_INTERFACE_DESC_SIZE 0
#endif

#define MIDI_INTERFACE_DESC_POS CDC_DATA_INTERFACE_DESC_POS+CDC_DATA_INTERFACE_DESC_SIZE
#ifdef MIDI_INTERFACE
#define MIDI_INTERFACE_DESC_SIZE 9+7+6+6+9+9+9+5+9+5
#else
#define MIDI_INTERFACE_DESC_SIZE 0
#endif

#define KEYBOARD_INTERFACE_DESC_POS MIDI_INTERFACE_DESC_POS+MIDI_INTERFACE_DESC_SIZE
#ifdef KEYBOARD_INTERFACE
#define KEYBOARD_INTERFACE_DESC_SIZE 9+9+7
#define KEYBOARD_HID_DESC_OFFSET KEYBOARD_INTERFACE_DESC_POS+9
#else
#define KEYBOARD_INTERFACE_DESC_SIZE 0
#endif

#define MOUSE_INTERFACE_DESC_POS KEYBOARD_INTERFACE_DESC_POS+KEYBOARD_INTERFACE_DESC_SIZE
#ifdef MOUSE_INTERFACE
#define MOUSE_INTERFACE_DESC_SIZE 9+9+7
#define MOUSE_HID_DESC_OFFSET MOUSE_INTERFACE_DESC_POS+9
#else
#define MOUSE_INTERFACE_DESC_SIZE 0
#endif

#define RAWHID_INTERFACE_DESC_POS MOUSE_INTERFACE_DESC_POS+MOUSE_INTERFACE_DESC_SIZE
#ifdef RAWHID_INTERFACE
#define RAWHID_INTERFACE_DESC_SIZE 9+9+7+7
#define RAWHID_HID_DESC_OFFSET RAWHID_INTERFACE_DESC_POS+9
#else
#define RAWHID_INTERFACE_DESC_SIZE 0
#endif

#define FLIGHTSIM_INTERFACE_DESC_POS RAWHID_INTERFACE_DESC_POS+RAWHID_INTERFACE_DESC_SIZE
#ifdef FLIGHTSIM_INTERFACE
#define FLIGHTSIM_INTERFACE_DESC_SIZE 9+9+7+7
#define FLIGHTSIM_HID_DESC_OFFSET FLIGHTSIM_INTERFACE_DESC_POS+9
#else
#define FLIGHTSIM_INTERFACE_DESC_SIZE 0
#endif

#define SEREMU_INTERFACE_DESC_POS FLIGHTSIM_INTERFACE_DESC_POS+FLIGHTSIM_INTERFACE_DESC_SIZE
#ifdef SEREMU_INTERFACE
#define SEREMU_INTERFACE_DESC_SIZE 9+9+7+7
#define SEREMU_HID_DESC_OFFSET SEREMU_INTERFACE_DESC_POS+9
#else
#define SEREMU_INTERFACE_DESC_SIZE 0
#endif

#define JOYSTICK_INTERFACE_DESC_POS SEREMU_INTERFACE_DESC_POS+SEREMU_INTERFACE_DESC_SIZE
#ifdef JOYSTICK_INTERFACE
#define JOYSTICK_INTERFACE_DESC_SIZE 9+9+7
#define JOYSTICK_HID_DESC_OFFSET JOYSTICK_INTERFACE_DESC_POS+9
#else
#define JOYSTICK_INTERFACE_DESC_SIZE 0
#endif

#define CONFIG_DESC_SIZE JOYSTICK_INTERFACE_DESC_POS+JOYSTICK_INTERFACE_DESC_SIZE



// **************************************************************
// USB Configuration
@@ -761,35 +841,32 @@ const usb_descriptor_list_t usb_descriptor_list[] = {
{0x0200, 0x0000, config_descriptor, sizeof(config_descriptor)},
#ifdef SEREMU_INTERFACE
{0x2200, SEREMU_INTERFACE, seremu_report_desc, sizeof(seremu_report_desc)},
{0x2100, SEREMU_INTERFACE, config_descriptor+SEREMU_DESC_OFFSET, 9},
{0x2100, SEREMU_INTERFACE, config_descriptor+SEREMU_HID_DESC_OFFSET, 9},
#endif
#ifdef KEYBOARD_INTERFACE
{0x2200, KEYBOARD_INTERFACE, keyboard_report_desc, sizeof(keyboard_report_desc)},
{0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_DESC_OFFSET, 9},
{0x2100, KEYBOARD_INTERFACE, config_descriptor+KEYBOARD_HID_DESC_OFFSET, 9},
#endif
#ifdef MOUSE_INTERFACE
{0x2200, MOUSE_INTERFACE, mouse_report_desc, sizeof(mouse_report_desc)},
{0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_DESC_OFFSET, 9},
{0x2100, MOUSE_INTERFACE, config_descriptor+MOUSE_HID_DESC_OFFSET, 9},
#endif
#ifdef JOYSTICK_INTERFACE
{0x2200, JOYSTICK_INTERFACE, joystick_report_desc, sizeof(joystick_report_desc)},
{0x2100, JOYSTICK_INTERFACE, config_descriptor+JOYSTICK_DESC_OFFSET, 9},
{0x2100, JOYSTICK_INTERFACE, config_descriptor+JOYSTICK_HID_DESC_OFFSET, 9},
#endif
#ifdef RAWHID_INTERFACE
{0x2200, RAWHID_INTERFACE, rawhid_report_desc, sizeof(rawhid_report_desc)},
{0x2100, RAWHID_INTERFACE, config_descriptor+RAWHID_DESC_OFFSET, 9},
{0x2100, RAWHID_INTERFACE, config_descriptor+RAWHID_HID_DESC_OFFSET, 9},
#endif
#ifdef FLIGHTSIM_INTERFACE
{0x2200, FLIGHTSIM_INTERFACE, flightsim_report_desc, sizeof(flightsim_report_desc)},
{0x2100, FLIGHTSIM_INTERFACE, config_descriptor+FLIGHTSIM_DESC_OFFSET, 9},
{0x2100, FLIGHTSIM_INTERFACE, config_descriptor+FLIGHTSIM_HID_DESC_OFFSET, 9},
#endif
{0x0300, 0x0000, (const uint8_t *)&string0, 0},
{0x0301, 0x0409, (const uint8_t *)&usb_string_manufacturer_name, 0},
{0x0302, 0x0409, (const uint8_t *)&usb_string_product_name, 0},
{0x0303, 0x0409, (const uint8_t *)&usb_string_serial_number, 0},
//{0x0301, 0x0409, (const uint8_t *)&string1, 0},
//{0x0302, 0x0409, (const uint8_t *)&string2, 0},
//{0x0303, 0x0409, (const uint8_t *)&string3, 0},
{0, 0, NULL, 0}
};


+ 53
- 35
teensy3/usb_desc.h Переглянути файл

@@ -47,30 +47,67 @@
#define ENDPOINT_TRANSMIT_AND_RECEIVE 0x1D

/*
To modify a USB Type to have different interfaces, start in this
file. 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.
Each group of #define lines below corresponds to one of the
settings in the Tools > USB Type menu. This file defines what
type of USB device is actually created for each of those menu
options.

Each "interface" is a set of functionality your PC or Mac will
use and treat as if it is a unique device. Within each interface,
the "endpoints" are the actual communication channels. Most
interfaces use 1, 2 or 3 endpoints. By editing only this file,
you can customize the USB Types to be any collection of interfaces.

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 number of interfaces.
reflect the total number of interfaces.

Next, assign unique endpoint numbers to all the endpoints across
all the interfaces your device has. You can reuse an endpoint
number for transmit and receive, but the same endpoint number must
not be used twice to transmit, or twice to receive.

Most endpoints also require their maximum size, and some also
need an interval specification (the number of milliseconds the
PC will check for data from that endpoint). For existing
interfaces, usually these other settings should not be changed.

Within each interface, make sure it uses a unique set of endpoints.
Edit NUM_ENDPOINTS to be at least the largest endpoint number used.
Then edit the ENDPOINT*_CONFIG lines so each endpoint is configured

Edit NUM_USB_BUFFERS to control how much memory the USB stack will
allocate. At least 2 should be used for each endpoint. More
memory will allow higher throughput for user programs that have
high latency (eg, spending time doing things other than interacting
with the USB).

Edit the ENDPOINT*_CONFIG lines so each endpoint is configured
the proper way (transmit, receive, or both).

The CONFIG_DESC_SIZE and any XYZ_DESC_OFFSET numbers must be
edited to the correct sizes. See usb_desc.c for the giant array
of bytes. Someday these may be done automatically..... (but how?)
If you are using existing interfaces (making your own device with
a different set of interfaces) the code in all other files should
automatically adapt to the new endpoints you specify here.

If you are using existing interfaces, the code in each file should
automatically adapt to the changes you specify. If you need to
create a new type of interface, you'll need to write the code which
sends and receives packets, and presents an API to the user.
However, the .h files for each interface, which define the API
visible to user programs, may need to be edited. At the top of
each .h file is #if defined(USB_XYZ). These may need to be edited
to make the interface API appear to your program.

Finally, edit usb_inst.cpp, which creats instances of the C++
objects for each combination.
If you need to create a new type of interface, you'll need to write
the code which sends and receives packets, and presents an API to
the user. Usually, a pair of files are added for the actual code,
and code is also added in usb_dev.c for any control transfers,
interrupt-level code, or other very low-level stuff not possible
from the packet send/receive functons. Code also is added in
usb_inst.c to create an instance of your C++ object.

You may edit the Vendor and Product ID numbers, and strings. If
the numbers are changed, Teensyduino may not be able to automatically
find and reboot your board when you click the Upload button in
the Arduino IDE. You will need to press the Program button on
Teensy to initiate programming.

Some operating systems, especially Windows, may cache USB device
info. Changes to the device name may not update on the same
@@ -82,7 +119,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
*/



#if defined(USB_SERIAL)
#define VENDOR_ID 0x16C0
#define PRODUCT_ID 0x0483
@@ -103,7 +139,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define CDC_ACM_SIZE 16
#define CDC_RX_SIZE 64
#define CDC_TX_SIZE 64
#define CONFIG_DESC_SIZE (9+9+5+5+4+5+7+9+7+7)
#define ENDPOINT2_CONFIG ENDPOINT_TRANSIMIT_ONLY
#define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_ONLY
#define ENDPOINT4_CONFIG ENDPOINT_TRANSIMIT_ONLY
@@ -138,11 +173,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define JOYSTICK_ENDPOINT 4
#define JOYSTICK_SIZE 16
#define JOYSTICK_INTERVAL 2
#define KEYBOARD_DESC_OFFSET (9 + 9)
#define MOUSE_DESC_OFFSET (9 + 9+9+7 + 9)
#define SEREMU_DESC_OFFSET (9 + 9+9+7 + 9+9+7 + 9)
#define JOYSTICK_DESC_OFFSET (9 + 9+9+7 + 9+9+7 + 9+9+7+7 + 9)
#define CONFIG_DESC_SIZE (9 + 9+9+7 + 9+9+7 + 9+9+7+7 + 9+9+7)
#define ENDPOINT1_CONFIG ENDPOINT_TRANSIMIT_ONLY
#define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_ONLY
#define ENDPOINT3_CONFIG ENDPOINT_TRANSIMIT_ONLY
@@ -184,10 +214,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define JOYSTICK_ENDPOINT 6
#define JOYSTICK_SIZE 16
#define JOYSTICK_INTERVAL 1
#define KEYBOARD_DESC_OFFSET (9+8 + 9+5+5+4+5+7+9+7+7 + 9)
#define MOUSE_DESC_OFFSET (9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9)
#define JOYSTICK_DESC_OFFSET (9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9+9+7 + 9)
#define CONFIG_DESC_SIZE (9+8 + 9+5+5+4+5+7+9+7+7 + 9+9+7 + 9+9+7 + 9+9+7)
#define ENDPOINT1_CONFIG ENDPOINT_TRANSIMIT_ONLY
#define ENDPOINT2_CONFIG ENDPOINT_TRANSIMIT_ONLY
#define ENDPOINT3_CONFIG ENDPOINT_RECEIVE_ONLY
@@ -218,8 +244,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define MIDI_TX_SIZE 64
#define MIDI_RX_ENDPOINT 4
#define MIDI_RX_SIZE 64
#define SEREMU_DESC_OFFSET (9 + 9+7+6+6+9+9+9+5+9+5 + 9)
#define CONFIG_DESC_SIZE (9 + 9+7+6+6+9+9+9+5+9+5 + 9+9+7+7)
#define ENDPOINT1_CONFIG ENDPOINT_TRANSIMIT_ONLY
#define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_ONLY
#define ENDPOINT3_CONFIG ENDPOINT_TRANSIMIT_ONLY
@@ -252,9 +276,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define SEREMU_RX_ENDPOINT 2
#define SEREMU_RX_SIZE 32
#define SEREMU_RX_INTERVAL 2
#define RAWHID_DESC_OFFSET (9 + 9)
#define SEREMU_DESC_OFFSET (9 + 9+9+7+7 + 9)
#define CONFIG_DESC_SIZE (9 + 9+9+7+7 + 9+9+7+7)
#define ENDPOINT1_CONFIG ENDPOINT_TRANSIMIT_ONLY
#define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_ONLY
#define ENDPOINT3_CONFIG ENDPOINT_TRANSIMIT_ONLY
@@ -285,9 +306,6 @@ let me know? http://forum.pjrc.com/forums/4-Suggestions-amp-Bug-Reports
#define SEREMU_RX_ENDPOINT 2
#define SEREMU_RX_SIZE 32
#define SEREMU_RX_INTERVAL 2
#define FLIGHTSIM_DESC_OFFSET (9 + 9)
#define SEREMU_DESC_OFFSET (9 + 9+9+7+7 + 9)
#define CONFIG_DESC_SIZE (9 + 9+9+7+7 + 9+9+7+7)
#define ENDPOINT1_CONFIG ENDPOINT_TRANSIMIT_ONLY
#define ENDPOINT2_CONFIG ENDPOINT_RECEIVE_ONLY
#define ENDPOINT3_CONFIG ENDPOINT_TRANSIMIT_ONLY

+ 17
- 18
teensy3/usb_inst.cpp Переглянути файл

@@ -29,44 +29,43 @@
*/

#include "WProgram.h"
#include "usb_desc.h"

#if F_CPU >= 20000000

#ifdef USB_SERIAL
#ifdef CDC_DATA_INTERFACE
usb_serial_class Serial;
#endif

#ifdef USB_HID
usb_keyboard_class Keyboard;
usb_mouse_class Mouse;
usb_joystick_class Joystick;
uint8_t usb_joystick_class::manual_mode = 0;
usb_seremu_class Serial;
#ifdef MIDI_INTERFACE
usb_midi_class usbMIDI;
#endif

#ifdef USB_SERIAL_HID
usb_serial_class Serial;
#ifdef KEYBOARD_INTERFACE
usb_keyboard_class Keyboard;
usb_mouse_class Mouse;
usb_joystick_class Joystick;
uint8_t usb_joystick_class::manual_mode = 0;
#endif

#ifdef USB_MIDI
usb_midi_class usbMIDI;
usb_seremu_class Serial;
#ifdef MOUSE_INTERFACE
usb_mouse_class Mouse;
#endif

#ifdef USB_RAWHID
#ifdef RAWHID_INTERFACE
usb_rawhid_class RawHID;
usb_seremu_class Serial;
#endif

#ifdef USB_FLIGHTSIM
#ifdef FLIGHTSIM_INTERFACE
FlightSimClass FlightSim;
#endif

#ifdef SEREMU_INTERFACE
usb_seremu_class Serial;
#endif

#ifdef JOYSTICK_INTERFACE
usb_joystick_class Joystick;
uint8_t usb_joystick_class::manual_mode = 0;
#endif


#else // F_CPU < 20 MHz


Завантаження…
Відмінити
Зберегти