1 /* This program is free software: you can redistribute it and/or modify
2 * it under the terms of the GNU General Public License as published by
3 * the Free Software Foundation, either version 3 of the License, or
4 * (at your option) any later version.
6 * This program is distributed in the hope that it will be useful,
7 * but WITHOUT ANY WARRANTY; without even the implied warranty of
8 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
9 * GNU General Public License for more details.
11 * You should have received a copy of the GNU General Public License
12 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 /** library for USB CDC ACM communication (code)
17 * @author King Kévin <kingkevin@cuvoodoo.info>
21 /* standard libraries */
22 #include <stdint.h> // standard integer types
23 #include <stdio.h> // standard I/O facilities
24 #include <stdlib.h> // general utilities
26 /* STM32 (including CM3) libraries */
27 #include <libopencm3/stm32/rcc.h> // real-time control clock library
28 #include <libopencm3/stm32/gpio.h> // general purpose input output library
29 #include <libopencm3/cm3/nvic.h> // interrupt handler
30 #include <libopencm3/cm3/scb.h> // reset utilities
31 #include <libopencmsis/core_cm3.h> // Cortex M3 utilities
32 #include <libopencm3/usb/usbd.h> // USB library
33 #include <libopencm3/usb/cdc.h> // USB CDC library
34 #include <libopencm3/cm3/sync.h> // synchronisation utilities
37 #include "usb_cdcacm.h" // USB CDC ACM header and definitions
39 /** USB CDC ACM device descriptor
40 * @note as defined in USB CDC specification section 5
42 static const struct usb_device_descriptor device_descriptor = {
43 .bLength = USB_DT_DEVICE_SIZE, // the size of this header in bytes, 18
44 .bDescriptorType = USB_DT_DEVICE, // a value of 1 indicates that this is a device descriptor
45 .bcdUSB = 0x0200, // this device supports USB 2.0
46 .bDeviceClass = USB_CLASS_CDC, // use the CDC device class
47 .bDeviceSubClass = 0, // unused
48 .bDeviceProtocol = 0, // unused
49 .bMaxPacketSize0 = 64, // packet size for endpoint zero in bytes
50 .idVendor = 0xc440, // Vendor ID (CuVo...)
51 .idProduct = 0x0d00, // product ID within the Vendor ID space (...odoo)
52 .bcdDevice = 0x0100, // version number for the device
53 .iManufacturer = 1, // the index of the string in the string table that represents the name of the manufacturer of this device.
54 .iProduct = 2, // the index of the string in the string table that represents the name of the product
55 .iSerialNumber = 3, // the index of the string in the string table that represents the serial number of this item in string form.
56 .bNumConfigurations = 1, // the number of possible configurations this device has
59 /** USB CDC ACM data endpoints
60 * @note as defined in USB CDC specification section 5
62 static const struct usb_endpoint_descriptor data_endpoints[] = {{
63 .bLength = USB_DT_ENDPOINT_SIZE, // the size of the endpoint descriptor in bytes
64 .bDescriptorType = USB_DT_ENDPOINT, // a value of 5 indicates that this describes an endpoint
65 .bEndpointAddress = 0x01, // OUT (from host) direction (0<<7), endpoint 1
66 .bmAttributes = USB_ENDPOINT_ATTR_BULK, // bulk mode
67 .wMaxPacketSize = 64, // maximum packet size
68 .bInterval = 1, // the frequency, in number of frames, that we're going to be sending data
70 .bLength = USB_DT_ENDPOINT_SIZE, // the size of the endpoint descriptor in bytes
71 .bDescriptorType = USB_DT_ENDPOINT, // a value of 5 indicates that this describes an endpoint
72 .bEndpointAddress = 0x82, // IN (to host) direction (1<<7), endpoint 2
73 .bmAttributes = USB_ENDPOINT_ATTR_BULK, // bulk mode
74 .wMaxPacketSize = 64, // maximum packet size
75 .bInterval = 1, // the frequency, in number of frames, that we're going to be sending data
78 /** USB CDC ACM communication endpoints
79 * @note This notification endpoint isn't implemented. According to CDC spec its optional, but its absence causes a NULL pointer dereference in Linux cdc_acm driver
81 static const struct usb_endpoint_descriptor communication_endpoints[] = {{
82 .bLength = USB_DT_ENDPOINT_SIZE, // the size of the endpoint descriptor in bytes
83 .bDescriptorType = USB_DT_ENDPOINT, // a value of 5 indicates that this describes an endpoint
84 .bEndpointAddress = 0x83, // IN (to host) direction (1<<7), endpoint 3
85 .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT, // interrupt mode
86 .wMaxPacketSize = 16, // maximum packet size
87 .bInterval = 255, // the frequency, in number of frames, that we're going to be sending data
90 /** USB CDC ACM functional descriptor
92 * @note as defined in USB CDC specification section 5.2.3
95 struct usb_cdc_header_descriptor header; /**< header */
96 struct usb_cdc_call_management_descriptor call_mgmt; /**< call management descriptor */
97 struct usb_cdc_acm_descriptor acm; /**< descriptor */
98 struct usb_cdc_union_descriptor cdc_union; /**< descriptor */
99 } __attribute__((packed)) cdcacm_functional_descriptors = {
101 .bFunctionLength = sizeof(struct usb_cdc_header_descriptor), /**< descriptor length */
102 .bDescriptorType = CS_INTERFACE, /**< descriptor type */
103 .bDescriptorSubtype = USB_CDC_TYPE_HEADER, /**< descriptor subtype */
104 .bcdCDC = 0x0110, /**< CDC value */
107 .bFunctionLength = sizeof(struct usb_cdc_call_management_descriptor), /**< descriptor length */
108 .bDescriptorType = CS_INTERFACE, /**< descriptor type */
109 .bDescriptorSubtype = USB_CDC_TYPE_CALL_MANAGEMENT, /**< descriptor subtype */
110 .bmCapabilities = 0, /**< capabilities */
111 .bDataInterface = 1, /**< data interface */
114 .bFunctionLength = sizeof(struct usb_cdc_acm_descriptor), /**< descriptor length */
115 .bDescriptorType = CS_INTERFACE, /**< descriptor type */
116 .bDescriptorSubtype = USB_CDC_TYPE_ACM, /**< descriptor subtype */
117 .bmCapabilities = 0, /**< capabilities */
120 .bFunctionLength = sizeof(struct usb_cdc_union_descriptor), /**< descriptor length */
121 .bDescriptorType = CS_INTERFACE, /**< descriptor type */
122 .bDescriptorSubtype = USB_CDC_TYPE_UNION, /**< descriptor subtype */
123 .bControlInterface = 0, /**< control interface */
124 .bSubordinateInterface0 = 1, /**< subordinate interface */
128 /** USB CDC interface descriptor
129 * @note as defined in USB CDC specification section 5.1.3
131 static const struct usb_interface_descriptor communication_interface[] = {{
132 .bLength = USB_DT_INTERFACE_SIZE,
133 .bDescriptorType = USB_DT_INTERFACE,
134 .bInterfaceNumber = 0,
135 .bAlternateSetting = 0,
137 .bInterfaceClass = USB_CLASS_CDC,
138 .bInterfaceSubClass = USB_CDC_SUBCLASS_ACM,
139 .bInterfaceProtocol = USB_CDC_PROTOCOL_NONE,
142 .endpoint = communication_endpoints,
144 .extra = &cdcacm_functional_descriptors,
145 .extralen = sizeof(cdcacm_functional_descriptors),
148 /** USB CDC ACM data class interface descriptor
149 * @note as defined in USB CDC specification section 5.1.3
151 static const struct usb_interface_descriptor data_interface[] = {{
152 .bLength = USB_DT_INTERFACE_SIZE,
153 .bDescriptorType = USB_DT_INTERFACE,
154 .bInterfaceNumber = 1,
155 .bAlternateSetting = 0,
157 .bInterfaceClass = USB_CLASS_DATA,
158 .bInterfaceSubClass = 0,
159 .bInterfaceProtocol = 0,
162 .endpoint = data_endpoints,
165 /** USB CDC ACM interface descriptor */
166 static const struct usb_interface interfaces[] = {{
168 .altsetting = communication_interface,
171 .altsetting = data_interface,
174 /** USB CDC ACM configuration descriptor */
175 static const struct usb_config_descriptor config = {
176 .bLength = USB_DT_CONFIGURATION_SIZE, // the length of this header in bytes
177 .bDescriptorType = USB_DT_CONFIGURATION, // a value of 2 indicates that this is a configuration descriptor
178 .wTotalLength = 0, // this should hold the total size of the configuration descriptor including all sub interfaces. it is automatically filled in by the USB stack in libopencm3
179 .bNumInterfaces = 2, // the number of interfaces in this configuration
180 .bConfigurationValue = 1, // the index of this configuration
181 .iConfiguration = 0, // a string index describing this configuration (zero means not provided)
182 .bmAttributes = 0x80, // self powered (0<<6), supports remote wakeup (0<<5)
183 .bMaxPower = 0x32, // the maximum amount of current that this device will draw in 2mA units
185 .interface = interfaces, // pointer to an array of interfaces
189 * @note starting with index 1
191 static const char *usb_strings[] = {
197 static uint8_t usbd_control_buffer[128] = {0}; /**< buffer to be used for control requests */
198 static usbd_device *usb_device = NULL; /**< structure holding all the info related to the USB device */
199 static bool connected = false; /**< is the USB device is connected to a host */
201 /* input and output ring buffer, indexes, and available memory */
202 static uint8_t rx_buffer[CDCACM_BUFFER] = {0}; /**< ring buffer for received data */
203 static volatile uint8_t rx_i = 0; /**< current position of read received data */
204 static volatile uint8_t rx_used = 0; /**< how much data has been received and not red */
205 mutex_t rx_lock = MUTEX_UNLOCKED; /**< lock to update rx_i or rx_used */
206 static uint8_t tx_buffer[CDCACM_BUFFER] = {0}; /**< ring buffer for data to transmit */
207 static volatile uint8_t tx_i = 0; /**< current position if transmitted data */
208 static volatile uint8_t tx_used = 0; /**< how much data needs to be transmitted */
209 mutex_t tx_lock = MUTEX_UNLOCKED; /**< lock to update tx_i or tx_used */
210 volatile uint8_t cdcacm_received = 0; // same as rx_used, but since the user can write this variable we don't rely on it
212 /** disconnect USB by pulling down D+ to for re-enumerate */
213 static void usb_disconnect(void)
215 /* short USB disconnect to force re-enumerate */
216 #if defined(SYSTEM_BOARD) || defined(BLUE_PILL)
217 // pull USB D+ low for a short while
218 rcc_periph_clock_enable(RCC_GPIOA);
219 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO12);
220 gpio_clear(GPIOA, GPIO12);
221 for (uint32_t i = 0; i < 0x2000; i++) {
224 #elif defined(MAPLE_MINI)
225 // disconnect USB D+ using dedicated DISC line/circuit on PB9
226 rcc_periph_clock_enable(RCC_GPIOB);
227 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_2_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO9);
228 gpio_set(GPIOB, GPIO9);
229 for (uint32_t i = 0; i < 0x2000; i++) {
232 gpio_clear(GPIOB, GPIO9);
236 /** incoming USB CDC ACM control request
237 * @param[in] usbd_dev USB device descriptor
238 * @param[in] req control request information
239 * @param[in] buf control request data
240 * @param[in] len control request data length
241 * @param[in] complete not used
242 * @return 0 if succeeded, error else
243 * @note resets device when configured with 5 bits
245 static enum usbd_request_return_codes 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))
251 switch (req->bRequest) {
252 case USB_CDC_REQ_SET_CONTROL_LINE_STATE:
253 connected = req->wValue ? true : false; // check if terminal is open
254 //bool dtr = (req->wValue & (1 << 0)) ? true : false;
255 //bool rts = (req->wValue & (1 << 1)) ? true : false;
256 /* this Linux cdc_acm driver requires this to be implemented
257 * even though it's optional in the CDC spec, and we don't
258 * advertise it in the ACM functional descriptor.
260 uint8_t reply[10] = {0};
261 struct usb_cdc_notification *notif = (void *)reply;
262 /* we echo signals back to host as notification. */
263 notif->bmRequestType = 0xA1;
264 notif->bNotification = USB_CDC_NOTIFY_SERIAL_STATE;
268 reply[8] = req->wValue & 3;
270 usbd_ep_write_packet(usbd_dev, 0x83, reply, LENGTH(reply));
271 return USBD_REQ_HANDLED;
273 case USB_CDC_REQ_SET_LINE_CODING:
274 // ignore if length is wrong
275 if (*len < sizeof(struct usb_cdc_line_coding)) {
276 return USBD_REQ_NOTSUPP;
278 // get the line coding
279 struct usb_cdc_line_coding *coding = (struct usb_cdc_line_coding *)*buf;
280 /* reset device is the data bits is set to 5
281 * this is used to allowing rebooting the device in DFU mode for reflashing
282 * to reset the device from the host you can use stty --file /dev/ttyACM0 115200 raw cs5
284 if (coding->bDataBits==5) {
285 usb_disconnect(); // force re-enumerate after reset
286 scb_reset_system(); // reset device
287 while (true); // wait for the reset to happen
289 return USBD_REQ_HANDLED;
292 return USBD_REQ_HANDLED;
294 return USBD_REQ_NOTSUPP;
297 /** USB CDC ACM data received callback
298 * @param[in] usbd_dev USB device descriptor
299 * @param[in] ep endpoint where data came in
301 static void cdcacm_data_rx_cb(usbd_device *usbd_dev, uint8_t ep)
305 char usb_data[64] = {0}; // buffer to read data
306 uint16_t usb_length = 0; // length of incoming data
309 usb_length = usbd_ep_read_packet(usbd_dev, 0x01, usb_data, sizeof(usb_data));
310 if (usb_length) { // copy received data
311 for (uint16_t i=0; i<usb_length && rx_used<LENGTH(rx_buffer); i++) { // only until buffer is full
312 rx_buffer[(rx_i+rx_used)%LENGTH(rx_buffer)] = usb_data[i]; // put character in buffer
313 rx_used++; // update used buffer
315 cdcacm_received = rx_used; // update available data
319 /** USB CDC ACM data transmitted callback
320 * @param[in] usbd_dev USB device descriptor
321 * @param[in] ep endpoint where data came in
323 static void cdcacm_data_tx_cb(usbd_device *usbd_dev, uint8_t ep)
328 if (!usbd_dev || !connected || !tx_used) { // verify if we can send and there is something to send
331 if (mutex_trylock(&tx_lock)) { // try to get lock
332 uint8_t usb_length = (tx_used > 64 ? 64 : tx_used); // length of data to be transmitted (respect max packet size)
333 usb_length = (usb_length > (LENGTH(tx_buffer)-tx_i) ? LENGTH(tx_buffer)-tx_i : usb_length); // since here we use the source array not as ring buffer, only go up to the end
334 while (usb_length != usbd_ep_write_packet(usb_device, 0x82, (void*)(&tx_buffer[tx_i]), usb_length)); // ensure data is written into transmit buffer
335 tx_i = (tx_i+usb_length)%LENGTH(tx_buffer); // update location on buffer
336 tx_used -= usb_length; // update used size
337 mutex_unlock(&tx_lock); // release lock
339 usbd_ep_write_packet(usb_device, 0x82, NULL, 0); // trigger empty tx for a later callback
341 usbd_poll(usb_device); // ensure the data gets sent
344 /** set USB CDC ACM configuration
345 * @param[in] usbd_dev USB device descriptor
346 * @param[in] wValue not used
348 static void cdcacm_set_config(usbd_device *usbd_dev, uint16_t wValue)
352 usbd_ep_setup(usbd_dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_rx_cb);
353 usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, cdcacm_data_tx_cb);
354 usbd_ep_setup(usbd_dev, 0x83, USB_ENDPOINT_ATTR_INTERRUPT, 16, NULL);
356 usbd_register_control_callback( usbd_dev, USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE, USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT, cdcacm_control_request);
359 void cdcacm_setup(void)
361 connected = false; // start with USB not connected
362 usb_disconnect(); // force re-enumerate (useful after a restart or if there is a bootloader using another USB profile)
365 usb_device = usbd_init(&st_usbfs_v1_usb_driver, &device_descriptor, &config, usb_strings, 3, usbd_control_buffer, sizeof(usbd_control_buffer));
366 usbd_register_set_config_callback(usb_device, cdcacm_set_config);
368 /* enable interrupts (to not have to poll all the time) */
369 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ); // without this USB isn't detected by the host
371 /* reset buffer states */
374 mutex_unlock(&rx_lock);
378 mutex_unlock(&tx_lock);
381 char cdcacm_getchar(void)
383 while (!rx_used) { // idle until data is available
384 __WFI(); // sleep until interrupt (not sure if it's a good idea here)
386 char to_return = rx_buffer[rx_i]; // get the next available character
387 rx_i = (rx_i+1)%LENGTH(rx_buffer); // update used buffer
388 rx_used--; // update used buffer
389 cdcacm_received = rx_used; // update available data
393 void cdcacm_putchar(char c)
395 if (!usb_device || !connected) {
398 mutex_lock(&tx_lock); // get lock to prevent race condition
399 if (tx_used<LENGTH(tx_buffer)) { // buffer not full
400 tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // put character in buffer
401 tx_used++; // update used buffer
402 } else { // buffer full (might be that no terminal is connected to this serial)
403 tx_i = (tx_i+1)%LENGTH(tx_buffer); // shift start
404 tx_buffer[(tx_i+tx_used)%LENGTH(tx_buffer)] = c; // overwrite old data
406 mutex_unlock(&tx_lock); // release lock
407 if (tx_used==1) { // to buffer is not empty anymore
408 usbd_ep_write_packet(usb_device, 0x82, NULL, 0); // trigger tx callback
412 /** USB interrupt service routine called when data is received */
413 void usb_lp_can_rx0_isr(void) {
414 usbd_poll(usb_device);