31 #include <libopencm3/stm32/rcc.h>
32 #include <libopencm3/stm32/gpio.h>
33 #include <libopencm3/cm3/nvic.h>
34 #include <libopencm3/cm3/systick.h>
35 #include <libopencm3/usb/usbd.h>
36 #include <libopencm3/usb/cdc.h>
37 #include <libopencm3/cm3/scb.h>
38 #include <libopencm3/stm32/desig.h>
39 #include <libopencm3/stm32/otg_fs.h>
43 #include "mcu_periph/sys_time_arch.h"
46 #define MAX_PACKET_SIZE 64
48 #define VCOM_FIFO_SIZE 256
50 #define TX_TIMEOUT_CNT 20 //TODO, make dynamic with event period
73 static const struct usb_device_descriptor
dev = {
74 .bLength = USB_DT_DEVICE_SIZE,
75 .bDescriptorType = USB_DT_DEVICE,
77 .bDeviceClass = USB_CLASS_CDC,
87 .bNumConfigurations = 1,
95 static const struct usb_endpoint_descriptor
comm_endp[] = {{
96 .bLength = USB_DT_ENDPOINT_SIZE,
97 .bDescriptorType = USB_DT_ENDPOINT,
98 .bEndpointAddress = 0x83,
99 .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
100 .wMaxPacketSize = 16,
105 static const struct usb_endpoint_descriptor
data_endp[] = {{
106 .bLength = USB_DT_ENDPOINT_SIZE,
107 .bDescriptorType = USB_DT_ENDPOINT,
108 .bEndpointAddress = 0x01,
109 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
113 .bLength = USB_DT_ENDPOINT_SIZE,
114 .bDescriptorType = USB_DT_ENDPOINT,
115 .bEndpointAddress = 0x82,
116 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
122 static const struct {
123 struct usb_cdc_header_descriptor header;
124 struct usb_cdc_call_management_descriptor call_mgmt;
125 struct usb_cdc_acm_descriptor acm;
126 struct usb_cdc_union_descriptor cdc_union;
129 .bFunctionLength =
sizeof(
struct usb_cdc_header_descriptor),
131 .bDescriptorSubtype = USB_CDC_TYPE_HEADER,
136 sizeof(
struct usb_cdc_call_management_descriptor),
138 .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT,
143 .bFunctionLength =
sizeof(
struct usb_cdc_acm_descriptor),
145 .bDescriptorSubtype = USB_CDC_TYPE_ACM,
149 .bFunctionLength =
sizeof(
struct usb_cdc_union_descriptor),
151 .bDescriptorSubtype = USB_CDC_TYPE_UNION,
152 .bControlInterface = 0,
153 .bSubordinateInterface0 = 1,
157 static const struct usb_interface_descriptor
comm_iface[] = {{
158 .bLength = USB_DT_INTERFACE_SIZE,
159 .bDescriptorType = USB_DT_INTERFACE,
160 .bInterfaceNumber = 0,
161 .bAlternateSetting = 0,
163 .bInterfaceClass = USB_CLASS_CDC,
164 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
165 .bInterfaceProtocol = USB_CDC_PROTOCOL_AT,
175 static const struct usb_interface_descriptor
data_iface[] = {{
176 .bLength = USB_DT_INTERFACE_SIZE,
177 .bDescriptorType = USB_DT_INTERFACE,
178 .bInterfaceNumber = 1,
179 .bAlternateSetting = 0,
181 .bInterfaceClass = USB_CLASS_DATA,
182 .bInterfaceSubClass = 0,
183 .bInterfaceProtocol = 0,
190 static const struct usb_interface
ifaces[] = {{
199 static const struct usb_config_descriptor
config = {
200 .bLength = USB_DT_CONFIGURATION_SIZE,
201 .bDescriptorType = USB_DT_CONFIGURATION,
204 .bConfigurationValue = 1,
206 .bmAttributes = 0x80,
233 uint16_t *len,
void (**complete)(usbd_device *usbd_dev,
struct usb_setup_data *req))
239 switch (req->bRequest) {
240 case USB_CDC_REQ_SET_CONTROL_LINE_STATE: {
247 struct usb_cdc_notification *notif = (
void *)local_buf;
250 notif->bmRequestType = 0xA1;
251 notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE;
255 local_buf[8] = req->wValue & 3;
257 usbd_ep_write_packet(usbd_dev, 0x83, local_buf, 10);
260 case USB_CDC_REQ_SET_LINE_CODING:
261 if (*len <
sizeof(
struct usb_cdc_line_coding)) {
284 for (
int i = 0; i < len; i++) {
295 usb_connected =
false;
307 usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK,
MAX_PACKET_SIZE, NULL);
308 usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
310 usbd_register_control_callback(usbd_dev,
311 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
312 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
316 usb_connected =
true;
317 usbd_register_suspend_callback(usbd_dev,
suspend_cb);
336 if (next == fifo->
tail) {
417 static int result = 0;
419 result =
fifo_get(&rxfifo, &c) ? c : -1;
491 #ifdef USE_USB_LINE_CODING
504 long *
fd __attribute__((unused)),
511 long fd __attribute__((unused)),
518 long fd __attribute__((unused)),
522 for (i = 0; i < len; i++) {
550 rcc_periph_clock_enable(RCC_GPIOA);
551 gpio_mode_setup(
GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE,
557 rcc_periph_clock_enable(RCC_OTGFS);
560 desig_get_unique_id_as_string(serial_no, 25);
565 usbd_control_buffer,
sizeof(usbd_control_buffer));
571 OTG_FS_GCCFG |= OTG_GCCFG_NOVBUSSENS | OTG_GCCFG_PWRDWN;
574 usb_connected =
false;
static bool usb_connected
static const struct usb_interface_descriptor comm_iface[]
static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue)
Set configuration and control callbacks for CDC device (from libopencm3 examples) ...
static uint8_t usb_serial_getch(struct usb_serial_periph *p)
static void suspend_cb(void)
static char serial_no[25]
static uint8_t rxdata[VCOM_FIFO_SIZE]
static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
RX callback for CDC device (from libopencm3 examples)
void VCOM_send_message(void)
static const struct @17 cdcacm_functional_descriptors
struct usb_serial_periph usb_serial
static void usb_serial_transmit_buffer(struct usb_serial_periph *p, long fd, uint8_t *data, uint16_t len)
static int usb_serial_check_free_space(struct usb_serial_periph *p, long *fd, uint16_t len)
uint8_t usbd_control_buffer[128]
static const struct usb_endpoint_descriptor data_endp[]
int fifo_free(fifo_t *fifo)
void fifo_init(fifo_t *fifo, U8 *buf)
void sys_time_usleep(uint32_t us)
sys_time_usleep(uint32_t us)
static void usb_serial_send(struct usb_serial_periph *p, long fd)
struct link_device device
Generic device interface.
bool VCOM_check_free_space(uint16_t len)
Checks if buffer free in VCOM buffer.
int VCOM_getchar(void)
Reads one character from VCOM port.
static const char * usb_strings[]
static const struct usb_device_descriptor dev
usbd_device * my_usbd_dev
static uint8_t mode
mode holds the current sonar mode mode = 0 used at high altitude, uses 16 wave patterns mode = 1 used...
BOOL fifo_put(fifo_t *fifo, U8 c)
static const struct usb_config_descriptor config
static void usb_serial_transmit(struct usb_serial_periph *p, long fd, uint8_t byte)
static const struct usb_endpoint_descriptor comm_endp[]
void VCOM_set_linecoding(uint8_t mode)
int VCOM_putchar(int c)
Writes one character to VCOM port.
static const struct usb_interface ifaces[]
static int usb_serial_char_available(struct usb_serial_periph *p)
static int cdcacm_control_request(usbd_device *usbd_dev, struct usb_setup_data *req, uint8_t **buf, uint16_t *len, void(**complete)(usbd_device *usbd_dev, struct usb_setup_data *req))
CDC device control request (from libopencm3 examples)
int fifo_avail(fifo_t *fifo)
BOOL fifo_get(fifo_t *fifo, U8 *pc)
void VCOM_allow_linecoding(uint8_t mode)
static uint8_t txdata[VCOM_FIFO_SIZE]
int VCOM_check_available(void)
Checks if data available in VCOM buffer.
static const struct usb_interface_descriptor data_iface[]