27 #include "hal_serial_usb.h"
30 #if HAL_USE_SERIAL_USB
33 #define SERIAL_USB USBD1
36 #ifndef USBD1_DATA_REQUEST_EP
37 #define USBD1_DATA_REQUEST_EP 1
39 #ifndef USBD1_DATA_AVAILABLE_EP
40 #define USBD1_DATA_AVAILABLE_EP 1
42 #ifndef USBD1_INTERRUPT_REQUEST_EP
43 #define USBD1_INTERRUPT_REQUEST_EP 2
46 #if (CH_KERNEL_MAJOR == 2)
47 static EventListener inputAvailEL = {NULL, NULL, 0, 0};
49 static event_listener_t inputAvailEL = {NULL, NULL, 0, 0, 0};
60 static const uint8_t vcom_device_descriptor_data[18] = {
61 USB_DESC_DEVICE (0x0110,
78 static const USBDescriptor vcom_device_descriptor = {
79 sizeof vcom_device_descriptor_data,
80 vcom_device_descriptor_data
84 static const uint8_t vcom_configuration_descriptor_data[67] = {
86 USB_DESC_CONFIGURATION(67,
93 USB_DESC_INTERFACE (0x00,
106 USB_DESC_BYTE (0x24),
107 USB_DESC_BYTE (0x00),
109 USB_DESC_BCD (0x0110),
112 USB_DESC_BYTE (0x24),
113 USB_DESC_BYTE (0x01),
115 USB_DESC_BYTE (0x00),
116 USB_DESC_BYTE (0x01),
119 USB_DESC_BYTE (0x24),
120 USB_DESC_BYTE (0x02),
122 USB_DESC_BYTE (0x02),
125 USB_DESC_BYTE (0x24),
126 USB_DESC_BYTE (0x06),
128 USB_DESC_BYTE (0x00),
130 USB_DESC_BYTE (0x01),
133 USB_DESC_ENDPOINT (USBD1_INTERRUPT_REQUEST_EP|0x80,
138 USB_DESC_INTERFACE (0x01,
149 USB_DESC_ENDPOINT (USBD1_DATA_AVAILABLE_EP,
154 USB_DESC_ENDPOINT (USBD1_DATA_REQUEST_EP|0x80,
163 static const USBDescriptor vcom_configuration_descriptor = {
164 sizeof vcom_configuration_descriptor_data,
165 vcom_configuration_descriptor_data
171 static const uint8_t vcom_string0[] = {
173 USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
174 USB_DESC_WORD(0x0409)
180 static const uint8_t vcom_string1[] = {
182 USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
183 'P', 0,
'a', 0,
'p', 0,
'a', 0,
'r', 0,
'a', 0,
'z', 0,
'z', 0,
184 'i', 0,
' ', 0,
'U', 0,
'A', 0,
'V', 0
190 static const uint8_t vcom_string2[] = {
192 USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
193 'C', 0,
'D', 0,
'C', 0,
' ', 0,
'S', 0,
'e', 0,
'r', 0,
'i', 0,
194 'a', 0,
'l', 0,
' ', 0,
'S', 0,
'T', 0,
'M', 0,
'3', 0,
'2', 0
200 static const uint8_t vcom_string3[] = {
202 USB_DESC_BYTE(USB_DESCRIPTOR_STRING),
203 '0' + CH_KERNEL_MAJOR, 0,
204 '0' + CH_KERNEL_MINOR, 0,
205 '0' + CH_KERNEL_PATCH, 0
211 static const USBDescriptor vcom_strings[] = {
212 {
sizeof vcom_string0, vcom_string0},
213 {
sizeof vcom_string1, vcom_string1},
214 {
sizeof vcom_string2, vcom_string2},
215 {
sizeof vcom_string3, vcom_string3}
219 static SerialUSBDriver SDU;
225 static const USBDescriptor *get_descriptor(USBDriver *usbp,
233 case USB_DESCRIPTOR_DEVICE:
234 return &vcom_device_descriptor;
235 case USB_DESCRIPTOR_CONFIGURATION:
236 return &vcom_configuration_descriptor;
237 case USB_DESCRIPTOR_STRING:
239 return &vcom_strings[dindex];
248 static USBInEndpointState ep1instate;
253 static USBOutEndpointState ep1outstate;
258 static const USBEndpointConfig ep1config = {
259 USB_EP_MODE_TYPE_BULK,
274 static USBInEndpointState ep2instate;
279 static const USBEndpointConfig ep2config = {
280 USB_EP_MODE_TYPE_INTR,
282 sduInterruptTransmitted,
297 static void usb_event(USBDriver *usbp, usbevent_t event) {
300 case USB_EVENT_RESET:
302 case USB_EVENT_ADDRESS:
304 case USB_EVENT_CONFIGURED:
310 usbInitEndpointI(usbp, USBD1_DATA_REQUEST_EP, &ep1config);
311 usbInitEndpointI(usbp, USBD1_INTERRUPT_REQUEST_EP, &ep2config);
314 sduConfigureHookI(&SDU);
316 chSysUnlockFromISR();
318 case USB_EVENT_UNCONFIGURED:
320 case USB_EVENT_SUSPEND:
324 sduSuspendHookI(&SDU);
326 chSysUnlockFromISR();
328 case USB_EVENT_WAKEUP:
330 case USB_EVENT_STALLED:
339 static void sof_handler(USBDriver *usbp)
345 chSysUnlockFromISR();
351 static const USBConfig usbcfg = {
358 static SerialUSBConfig serusbcfg ;
360 USBDriver *usbGetDriver (
void)
362 return serusbcfg.usbp;
367 bool isUsbConnected(
void)
369 static bool hasEverReceive =
FALSE;
371 #if (CH_KERNEL_MAJOR <= 3)
372 if (inputAvailEL.el_listener == NULL)
374 if (inputAvailEL.listener == NULL)
381 return ((hasEverReceive = chEvtGetAndClearFlags(&inputAvailEL)) != 0);
387 void usbSerialReset(SerialUSBDriver *sdu)
389 usbStop(serusbcfg.usbp);
390 usbDisconnectBus(serusbcfg.usbp);
393 chThdSleepMilliseconds(10);
394 sduStart(sdu, &serusbcfg);
395 usbDisconnectBus(serusbcfg.usbp);
396 chThdSleepMilliseconds(900);
397 usbStart(serusbcfg.usbp, &usbcfg);
398 usbConnectBus(serusbcfg.usbp);
410 long *
fd __attribute__((unused)),
417 long fd __attribute__((unused)),
420 streamPut(&SDU,
byte);
424 long fd __attribute__((unused)),
427 streamWrite(&SDU, data, len);
438 if(
p->nb_bytes > 0) {
445 msg_t res = ibqGetTimeout(&SDU.ibqueue, TIME_IMMEDIATE);
448 p->rx_buf[write_idx] = (
uint8_t) res;
459 if(
p->nb_bytes > 0) {
466 return streamGet(&SDU);
478 serusbcfg.usbp = &SERIAL_USB;
479 serusbcfg.bulk_in = USBD1_DATA_REQUEST_EP;
480 serusbcfg.bulk_out = USBD1_DATA_AVAILABLE_EP;
481 serusbcfg.int_in = USBD1_INTERRUPT_REQUEST_EP;
484 sduStart(&SDU, &serusbcfg);
486 usbDisconnectBus(serusbcfg.usbp);
487 chThdSleepMilliseconds(1000);
488 usbStart(serusbcfg.usbp, &usbcfg);
489 usbConnectBus(serusbcfg.usbp);
491 chEvtRegisterMask(chnGetEventSource(&SDU), &inputAvailEL, CHN_INPUT_AVAILABLE);
511 if(!isUsbConnected()) {
static uint8_t mode
mode holds the current sonar mode mode = 0 used at high altitude, uses 16 wave patterns mode = 1 used...
void VCOM_event(void)
Poll usb (required by libopencm3).
int VCOM_getchar(void)
Reads one character from VCOM port.
int VCOM_putchar(int c)
Writes one character to VCOM port fifo.
static int usb_serial_check_free_space(struct usb_serial_periph *p, long *fd, uint16_t len)
static void usb_serial_transmit_buffer(struct usb_serial_periph *p, long fd, uint8_t *data, uint16_t len)
static void usb_serial_transmit(struct usb_serial_periph *p, long fd, uint8_t byte)
int VCOM_check_available(void)
Checks if data available in VCOM buffer.
int VCOM_peekchar(int ofs)
Reads one character from VCOM port without removing it from the queue.
void VCOM_send_message(void)
Send data from fifo right now.
struct usb_serial_periph usb_serial
static uint8_t usb_serial_getch(struct usb_serial_periph *p)
static void usb_serial_send(struct usb_serial_periph *p, long fd)
static int usb_serial_char_available(struct usb_serial_periph *p)
bool VCOM_check_free_space(uint16_t len)
Checks if buffer free in VCOM buffer.
void VCOM_set_linecoding(uint8_t mode)
struct link_device device
Generic device interface.
void VCOM_allow_linecoding(uint8_t mode)
#define USB_RX_BUFFER_SIZE
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.