123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342 |
- #include "hal.h"
- SerialUSBDriver SDU1;
- #define USBD2_DATA_REQUEST_EP 1
- #define USBD2_DATA_AVAILABLE_EP 1
- #define USBD2_INTERRUPT_REQUEST_EP 2
- static const uint8_t vcom_device_descriptor_data[18] = {
- USB_DESC_DEVICE (0x0110,
- 0x02,
- 0x00,
- 0x00,
- 0x40,
- 0x0483,
- 0x5740,
- 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 (USBD2_INTERRUPT_REQUEST_EP|0x80,
- 0x03,
- 0x0008,
- 0xFF),
-
- USB_DESC_INTERFACE (0x01,
- 0x00,
- 0x02,
- 0x0A,
- 0x00,
- 0x00,
- 0x00),
-
- USB_DESC_ENDPOINT (USBD2_DATA_AVAILABLE_EP,
- 0x02,
- 0x0040,
- 0x00),
-
- USB_DESC_ENDPOINT (USBD2_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 const uint8_t vcom_string1[] = {
- USB_DESC_BYTE(38),
- USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
- 'S', 0, 'T', 0, 'M', 0, 'i', 0, 'c', 0, 'r', 0, 'o', 0, 'e', 0,
- 'l', 0, 'e', 0, 'c', 0, 't', 0, 'r', 0, 'o', 0, 'n', 0, 'i', 0,
- 'c', 0, 's', 0
- };
- static const uint8_t vcom_string2[] = {
- USB_DESC_BYTE(56),
- USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
- 'C', 0, 'h', 0, 'i', 0, 'b', 0, 'i', 0, 'O', 0, 'S', 0, '/', 0,
- 'R', 0, 'T', 0, ' ', 0, 'V', 0, 'i', 0, 'r', 0, 't', 0, 'u', 0,
- 'a', 0, 'l', 0, ' ', 0, 'C', 0, 'O', 0, 'M', 0, ' ', 0, 'P', 0,
- 'o', 0, 'r', 0, 't', 0
- };
- static const uint8_t vcom_string3[] = {
- USB_DESC_BYTE(8),
- USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
- '0' + CH_KERNEL_MAJOR, 0,
- '0' + CH_KERNEL_MINOR, 0,
- '0' + CH_KERNEL_PATCH, 0
- };
- static const USBDescriptor vcom_strings[] = {
- {sizeof vcom_string0, vcom_string0},
- {sizeof vcom_string1, vcom_string1},
- {sizeof vcom_string2, vcom_string2},
- {sizeof vcom_string3, vcom_string3}
- };
- 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, USBD2_DATA_REQUEST_EP, &ep1config);
- usbInitEndpointI(usbp, USBD2_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 serusbcfg = {
- &USBD2,
- USBD2_DATA_REQUEST_EP,
- USBD2_DATA_AVAILABLE_EP,
- USBD2_INTERRUPT_REQUEST_EP
- };
|