4 * (c) 2020 Martin Mareš <mj@ucw.cz>
10 #include <libopencm3/cm3/cortex.h>
11 #include <libopencm3/cm3/nvic.h>
12 #include <libopencm3/cm3/systick.h>
13 #include <libopencm3/cm3/scb.h>
14 #include <libopencm3/stm32/rcc.h>
15 #include <libopencm3/stm32/desig.h>
16 #include <libopencm3/stm32/gpio.h>
17 #include <libopencm3/stm32/timer.h>
18 #include <libopencm3/stm32/usart.h>
19 #include <libopencm3/usb/dfu.h>
20 #include <libopencm3/usb/usbd.h>
24 /*** Hardware init ***/
26 static void clock_init(void)
28 rcc_clock_setup_in_hse_8mhz_out_72mhz();
30 rcc_periph_clock_enable(RCC_GPIOA);
31 rcc_periph_clock_enable(RCC_GPIOB);
32 rcc_periph_clock_enable(RCC_GPIOC);
33 rcc_periph_clock_enable(RCC_USART1);
34 rcc_periph_clock_enable(RCC_USART3);
35 rcc_periph_clock_enable(RCC_USB);
37 rcc_periph_reset_pulse(RST_GPIOA);
38 rcc_periph_reset_pulse(RST_GPIOB);
39 rcc_periph_reset_pulse(RST_GPIOC);
40 rcc_periph_reset_pulse(RST_USART1);
41 rcc_periph_reset_pulse(RST_USART3);
42 rcc_periph_reset_pulse(RST_USB);
45 static void gpio_init(void)
47 // PA9 = TXD1 for debugging console
48 // PA10 = RXD1 for debugging console
49 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
50 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
52 // PC13 = BluePill LED
53 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
54 gpio_clear(GPIOC, GPIO13);
56 // PB10 = TXD3 for DMX
57 // PB11 = RXD3 for DMX
58 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO10);
59 gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO11);
62 static void usart_init(void)
64 usart_set_baudrate(USART1, 115200);
65 usart_set_databits(USART1, 8);
66 usart_set_stopbits(USART1, USART_STOPBITS_1);
67 usart_set_mode(USART1, USART_MODE_TX);
68 usart_set_parity(USART1, USART_PARITY_NONE);
69 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
74 /*** System ticks ***/
76 static volatile u32 ms_ticks;
78 void sys_tick_handler(void)
83 static void tick_init(void)
85 systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
86 systick_counter_enable();
87 systick_interrupt_enable();
90 static void delay_ms(uint ms)
92 u32 start_ticks = ms_ticks;
93 while (ms_ticks - start_ticks < ms)
99 static void dmx_init(void)
101 usart_set_baudrate(USART3, 250000);
102 usart_set_databits(USART3, 8);
103 usart_set_stopbits(USART3, USART_STOPBITS_1);
104 usart_set_mode(USART3, USART_MODE_TX);
105 usart_set_parity(USART3, USART_PARITY_NONE);
106 usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE);
108 usart_enable(USART3);
111 static void dmx_send(void)
116 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO10);
117 gpio_clear(GPIOB, GPIO10);
121 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO10);
124 usart_send_blocking(USART3, 0x00);
125 usart_send_blocking(USART3, 0x00);
126 usart_send_blocking(USART3, 0xff); // warm
127 usart_send_blocking(USART3, 0x00);
128 usart_send_blocking(USART3, 0x00); // cold
133 static usbd_device *usbd_dev;
136 STR_MANUFACTURER = 1,
141 static char usb_serial_number[13];
143 static const char *usb_strings[] = {
144 "United Computer Wizards",
149 static const struct usb_device_descriptor device = {
150 .bLength = USB_DT_DEVICE_SIZE,
151 .bDescriptorType = USB_DT_DEVICE,
153 .bDeviceClass = 0xFF,
154 .bDeviceSubClass = 0,
155 .bDeviceProtocol = 0,
156 .bMaxPacketSize0 = 64,
157 .idVendor = DMX_USB_VENDOR,
158 .idProduct = DMX_USB_PRODUCT,
159 .bcdDevice = DMX_USB_VERSION,
160 .iManufacturer = STR_MANUFACTURER,
161 .iProduct = STR_PRODUCT,
162 .iSerialNumber = STR_SERIAL,
163 .bNumConfigurations = 1,
166 static const struct usb_endpoint_descriptor endpoints[] = {{
167 // Bulk end-point for sending values to DMX
168 .bLength = USB_DT_ENDPOINT_SIZE,
169 .bDescriptorType = USB_DT_ENDPOINT,
170 .bEndpointAddress = 0x01,
171 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
172 .wMaxPacketSize = 64,
176 static const struct usb_interface_descriptor iface = {
177 .bLength = USB_DT_INTERFACE_SIZE,
178 .bDescriptorType = USB_DT_INTERFACE,
179 .bInterfaceNumber = 0,
180 .bAlternateSetting = 0,
182 .bInterfaceClass = 0xFF,
183 .bInterfaceSubClass = 0,
184 .bInterfaceProtocol = 0,
186 .endpoint = endpoints,
189 static const struct usb_dfu_descriptor dfu_function = {
190 .bLength = sizeof(struct usb_dfu_descriptor),
191 .bDescriptorType = DFU_FUNCTIONAL,
192 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
193 .wDetachTimeout = 255,
194 .wTransferSize = 1024,
195 .bcdDFUVersion = 0x0100,
198 static const struct usb_interface_descriptor dfu_iface = {
199 .bLength = USB_DT_INTERFACE_SIZE,
200 .bDescriptorType = USB_DT_INTERFACE,
201 .bInterfaceNumber = 1,
202 .bAlternateSetting = 0,
204 .bInterfaceClass = 0xFE,
205 .bInterfaceSubClass = 1,
206 .bInterfaceProtocol = 1,
209 .extra = &dfu_function,
210 .extralen = sizeof(dfu_function),
213 static const struct usb_interface ifaces[] = {{
215 .altsetting = &iface,
218 .altsetting = &dfu_iface,
221 static const struct usb_config_descriptor config = {
222 .bLength = USB_DT_CONFIGURATION_SIZE,
223 .bDescriptorType = USB_DT_CONFIGURATION,
226 .bConfigurationValue = 1,
228 .bmAttributes = 0x80,
229 .bMaxPower = 50, // multiplied by 2 mA
233 static byte usb_configured;
234 static uint8_t usbd_control_buffer[64];
236 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
238 // Reset to bootloader, which implements the rest of DFU
239 debug_printf("Switching to DFU\n");
244 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
245 struct usb_setup_data *req,
246 uint8_t **buf UNUSED,
247 uint16_t *len UNUSED,
248 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
250 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
251 return USBD_REQ_NOTSUPP;
253 *complete = dfu_detach_complete;
254 return USBD_REQ_HANDLED;
257 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
259 // We received a frame from the USB host
261 uint len = usbd_ep_read_packet(dev, 0x01, buf, sizeof(buf));
262 debug_printf("USB: Host sent %u bytes\n", len);
265 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
267 usbd_register_control_callback(
269 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
270 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
272 usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
276 static void reset_cb(void)
278 debug_printf("USB: Reset\n");
282 static volatile bool usb_event_pending;
284 void usb_lp_can_rx0_isr(void)
287 * We handle USB in the main loop to avoid race conditions between
288 * USB interrupts and other code. However, we need an interrupt to
289 * up the main loop from sleep.
291 * We set up only the low-priority ISR, because high-priority ISR handles
292 * only double-buffered bulk transfers and isochronous transfers.
294 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
295 usb_event_pending = 1;
298 static void usb_init(void)
300 // Simulate USB disconnect
301 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
302 gpio_clear(GPIOA, GPIO11 | GPIO12);
305 usbd_dev = usbd_init(
306 &st_usbfs_v1_usb_driver,
310 ARRAY_SIZE(usb_strings),
312 sizeof(usbd_control_buffer)
314 usbd_register_reset_callback(usbd_dev, reset_cb);
315 usbd_register_set_config_callback(usbd_dev, set_config_cb);
316 usb_event_pending = 1;
327 desig_get_unique_id_as_dfu(usb_serial_number);
329 debug_printf("Hail, Lord Damian! Thy DMX interface is ready.\n");
337 if (ms_ticks - last_blink >= 100) {
339 last_blink = ms_ticks;
343 if (usb_event_pending) {
345 usb_event_pending = 0;
346 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
347 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
350 wait_for_interrupt();