123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364 |
- #include "hal.h"
- #include "hwdef.h"
- #include <stdlib.h>
- #include <string.h>
- #include "usbcfg.h"
- #if defined(HAL_USB_PRODUCT_ID) && !HAL_HAVE_DUAL_USB_CDC
- SerialUSBDriver SDU1;
- #define USBD1_DATA_REQUEST_EP 1
- #define USBD1_DATA_AVAILABLE_EP 1
- #define USBD1_INTERRUPT_REQUEST_EP 2
- static const uint8_t vcom_device_descriptor_data[18] = {
- USB_DESC_DEVICE(
- 0x0110,
- 0x02,
- 0x00,
- 0x00,
- 0x40,
- HAL_USB_VENDOR_ID,
- HAL_USB_PRODUCT_ID,
- 0x0200,
- 1,
- 2,
- 3,
- 1)
- };
- static const USBDescriptor vcom_device_descriptor = {
- sizeof vcom_device_descriptor_data,
- vcom_device_descriptor_data
- };
- static const uint8_t vcom_configuration_descriptor_data[67] = {
-
- USB_DESC_CONFIGURATION(67,
- 0x02,
- 0x01,
- 0,
- 0xC0,
- 50),
-
- USB_DESC_INTERFACE (0x00,
- 0x00,
- 0x01,
- 0x02,
- 0x02,
- 0x01,
- 0),
-
- USB_DESC_BYTE (5),
- USB_DESC_BYTE (0x24),
- USB_DESC_BYTE (0x00),
- USB_DESC_BCD (0x0110),
-
- USB_DESC_BYTE (5),
- USB_DESC_BYTE (0x24),
- USB_DESC_BYTE (0x01),
- USB_DESC_BYTE (0x00),
- USB_DESC_BYTE (0x01),
-
- USB_DESC_BYTE (4),
- USB_DESC_BYTE (0x24),
- USB_DESC_BYTE (0x02),
- USB_DESC_BYTE (0x02),
-
- USB_DESC_BYTE (5),
- USB_DESC_BYTE (0x24),
- USB_DESC_BYTE (0x06),
- USB_DESC_BYTE (0x00),
- USB_DESC_BYTE (0x01),
-
- USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80,
- 0x03,
- 0x0008,
- 0xFF),
-
- USB_DESC_INTERFACE (0x01,
- 0x00,
- 0x02,
- 0x0A,
- 0x00,
- 0x00,
- 0x00),
-
- USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP,
- 0x02,
- 0x0040,
- 0x00),
-
- USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80,
- 0x02,
- 0x0040,
- 0x00)
- };
- static const USBDescriptor vcom_configuration_descriptor = {
- sizeof vcom_configuration_descriptor_data,
- vcom_configuration_descriptor_data
- };
- static const uint8_t vcom_string0[] = {
- USB_DESC_BYTE(4),
- USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
- USB_DESC_WORD(0x0409)
- };
- static USBDescriptor vcom_strings[] = {
- {sizeof vcom_string0, vcom_string0},
- {0, NULL},
- {0, NULL},
- {0, NULL},
- };
- static uint8_t vcom_buffers[3][2+2*USB_DESC_MAX_STRLEN];
- static void setup_usb_string(USBDescriptor *desc, const char *str, uint8_t *b)
- {
- char str2[USB_DESC_MAX_STRLEN];
- string_substitute(str, str2);
- uint8_t len = strlen(str2);
- desc->ud_size = 2+2*len;
- desc->ud_string = (const uint8_t *)b;
- b[0] = USB_DESC_BYTE(desc->ud_size);
- b[1] = USB_DESC_BYTE(USB_DESCRIPTOR_STRING);
- uint8_t i;
- for (i=0; i<len; i++) {
- b[2+i*2] = str2[i];
- b[2+i*2+1] = 0;
- }
- }
- void setup_usb_strings(void)
- {
- setup_usb_string(&vcom_strings[1], HAL_USB_STRING_MANUFACTURER, vcom_buffers[0]);
- setup_usb_string(&vcom_strings[2], HAL_USB_STRING_PRODUCT, vcom_buffers[1]);
- setup_usb_string(&vcom_strings[3], HAL_USB_STRING_SERIAL, vcom_buffers[2]);
- }
- static const USBDescriptor *get_descriptor(USBDriver *usbp,
- uint8_t dtype,
- uint8_t dindex,
- uint16_t lang) {
- (void)usbp;
- (void)lang;
- switch (dtype) {
- case USB_DESCRIPTOR_DEVICE:
- return &vcom_device_descriptor;
- case USB_DESCRIPTOR_CONFIGURATION:
- return &vcom_configuration_descriptor;
- case USB_DESCRIPTOR_STRING:
- if (dindex < 4) {
- return &vcom_strings[dindex];
- }
- }
- return NULL;
- }
- static USBInEndpointState ep1instate;
- static USBOutEndpointState ep1outstate;
- static const USBEndpointConfig ep1config = {
- USB_EP_MODE_TYPE_BULK,
- NULL,
- sduDataTransmitted,
- sduDataReceived,
- 0x0040,
- 0x0040,
- &ep1instate,
- &ep1outstate,
- 2,
- NULL
- };
- static USBInEndpointState ep2instate;
- static const USBEndpointConfig ep2config = {
- USB_EP_MODE_TYPE_INTR,
- NULL,
- sduInterruptTransmitted,
- NULL,
- 0x0010,
- 0x0000,
- &ep2instate,
- NULL,
- 1,
- NULL
- };
- static void usb_event(USBDriver *usbp, usbevent_t event) {
- extern SerialUSBDriver SDU1;
- switch (event) {
- case USB_EVENT_ADDRESS:
- return;
- case USB_EVENT_CONFIGURED:
- chSysLockFromISR();
-
- usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config);
- usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config);
-
- sduConfigureHookI(&SDU1);
- chSysUnlockFromISR();
- return;
- case USB_EVENT_RESET:
-
- case USB_EVENT_UNCONFIGURED:
-
- case USB_EVENT_SUSPEND:
- chSysLockFromISR();
-
- sduSuspendHookI(&SDU1);
- chSysUnlockFromISR();
- return;
- case USB_EVENT_WAKEUP:
- chSysLockFromISR();
-
- sduWakeupHookI(&SDU1);
- chSysUnlockFromISR();
- return;
- case USB_EVENT_STALLED:
- return;
- }
- return;
- }
- static void sof_handler(USBDriver *usbp) {
- (void)usbp;
- osalSysLockFromISR();
- sduSOFHookI(&SDU1);
- osalSysUnlockFromISR();
- }
- const USBConfig usbcfg = {
- usb_event,
- get_descriptor,
- sduRequestsHook,
- sof_handler
- };
- const SerialUSBConfig serusbcfg1 = {
- &USBD1,
- USBD1_DATA_REQUEST_EP,
- USBD1_DATA_AVAILABLE_EP,
- USBD1_INTERRUPT_REQUEST_EP
- };
- #endif
|