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_TIM3);
34 rcc_periph_clock_enable(RCC_USART1);
35 rcc_periph_clock_enable(RCC_USART3);
36 rcc_periph_clock_enable(RCC_USB);
38 rcc_periph_reset_pulse(RST_GPIOA);
39 rcc_periph_reset_pulse(RST_GPIOB);
40 rcc_periph_reset_pulse(RST_GPIOC);
41 rcc_periph_reset_pulse(RST_TIM3);
42 rcc_periph_reset_pulse(RST_USART1);
43 rcc_periph_reset_pulse(RST_USART3);
44 rcc_periph_reset_pulse(RST_USB);
47 static void gpio_init(void)
49 // PA9 = TXD1 for debugging console
50 // PA10 = RXD1 for debugging console
51 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
52 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
54 // PC13 = BluePill LED
55 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
56 gpio_clear(GPIOC, GPIO13);
58 // PB10 = TXD3 for DMX
59 // PB11 = RXD3 for DMX
60 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO10);
61 gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO11);
63 // PB1 = TX enable for DMX (always on)
64 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO1);
65 gpio_set(GPIOB, GPIO1);
68 static void usart_init(void)
70 usart_set_baudrate(USART1, 115200);
71 usart_set_databits(USART1, 8);
72 usart_set_stopbits(USART1, USART_STOPBITS_1);
73 usart_set_mode(USART1, USART_MODE_TX);
74 usart_set_parity(USART1, USART_PARITY_NONE);
75 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
80 /*** System ticks ***/
82 static volatile u32 ms_ticks;
84 void sys_tick_handler(void)
89 static void tick_init(void)
91 systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
92 systick_counter_enable();
93 systick_interrupt_enable();
96 static void delay_ms(uint ms)
98 u32 start_ticks = ms_ticks;
99 while (ms_ticks - start_ticks < ms)
105 #define DMX_MAX_PACKET_SIZE 64
107 static volatile byte dmx_state;
108 static byte dmx_packet[DMX_MAX_PACKET_SIZE];
109 static byte dmx_pos, dmx_len;
111 static byte dmx_next_packet[DMX_MAX_PACKET_SIZE];
112 static byte dmx_next_len;
116 #define DMX_DEBUG(ch) debug_putc(ch)
118 #define DMX_DEBUG(ch) do { } while (0)
130 static void dmx_init(void)
132 usart_set_baudrate(USART3, 250000);
133 usart_set_databits(USART3, 8);
134 usart_set_stopbits(USART3, USART_STOPBITS_2);
135 usart_set_mode(USART3, USART_MODE_TX);
136 usart_set_parity(USART3, USART_PARITY_NONE);
137 usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE);
138 nvic_enable_irq(NVIC_USART3_IRQ);
140 usart_enable(USART3);
142 timer_set_prescaler(TIM3, CPU_CLOCK_MHZ - 1); // 1 MHz
143 timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
144 timer_update_on_overflow(TIM3);
145 timer_disable_preload(TIM3);
146 timer_one_shot_mode(TIM3);
147 timer_enable_irq(TIM3, TIM_DIER_UIE);
148 nvic_enable_irq(NVIC_TIM3_IRQ);
150 // Until we get data from USB, send "turn everything off" packets
151 memset(dmx_next_packet, 0, DMX_MAX_PACKET_SIZE);
152 dmx_next_len = DMX_MAX_PACKET_SIZE;
157 if (TIM_SR(TIM3) & TIM_SR_UIF) {
158 TIM_SR(TIM3) &= ~TIM_SR_UIF;
160 case DMX_STATE_BREAK:
161 // Send mark for 50 μs
163 dmx_state = DMX_STATE_MARK;
164 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO10);
165 timer_set_period(TIM3, 199);
166 timer_generate_event(TIM3, TIM_EGR_UG);
167 timer_enable_counter(TIM3);
170 // Start sending data bytes
172 dmx_state = DMX_STATE_DATA;
173 usart_enable_tx_interrupt(USART3);
177 dmx_state = DMX_STATE_IDLE;
183 void usart3_isr(void)
185 u32 status = USART_SR(USART3);
187 if (dmx_state == DMX_STATE_DATA) {
188 if (status & USART_SR_TXE) {
189 if (dmx_pos < dmx_len) {
191 usart_send(USART3, dmx_packet[dmx_pos++]);
193 // The transmitter is double-buffered, so at this moment, it is transmitting
194 // the last byte of the frame. Wait until transfer is completed.
196 usart_disable_tx_interrupt(USART3);
197 USART_CR1(USART3) |= USART_CR1_TCIE;
198 dmx_state = DMX_STATE_LAST_BYTE;
201 } else if (dmx_state == DMX_STATE_LAST_BYTE) {
202 // Transfer of the last byte is complete, but we have to wait
203 // before the start of the next packet: minimum time between two
204 // breaks must be at least 1204 μs. We already sent a 200μs break,
205 // 200μs mark, and dmx_len 44μs data slots.
206 USART_CR1(USART3) &= ~USART_CR1_TCIE;
207 uint sent = 200 + 200 + 44*dmx_len;
210 dmx_state = DMX_STATE_GAP;
211 timer_set_period(TIM3, 1300 - sent - 1);
212 timer_generate_event(TIM3, TIM_EGR_UG);
213 timer_enable_counter(TIM3);
216 dmx_state = DMX_STATE_IDLE;
221 static void dmx_send(void)
223 if (dmx_state != DMX_STATE_IDLE)
231 // Send break for 200 μs
233 dmx_state = DMX_STATE_BREAK;
234 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO10);
235 gpio_clear(GPIOB, GPIO10);
237 timer_set_period(TIM3, 199);
238 timer_generate_event(TIM3, TIM_EGR_UG);
239 timer_enable_counter(TIM3);
244 static usbd_device *usbd_dev;
247 STR_MANUFACTURER = 1,
252 static char usb_serial_number[13];
254 static const char *usb_strings[] = {
255 "United Computer Wizards",
260 static const struct usb_device_descriptor device = {
261 .bLength = USB_DT_DEVICE_SIZE,
262 .bDescriptorType = USB_DT_DEVICE,
264 .bDeviceClass = 0xFF,
265 .bDeviceSubClass = 0,
266 .bDeviceProtocol = 0,
267 .bMaxPacketSize0 = 64,
268 .idVendor = DMX_USB_VENDOR,
269 .idProduct = DMX_USB_PRODUCT,
270 .bcdDevice = DMX_USB_VERSION,
271 .iManufacturer = STR_MANUFACTURER,
272 .iProduct = STR_PRODUCT,
273 .iSerialNumber = STR_SERIAL,
274 .bNumConfigurations = 1,
277 static const struct usb_endpoint_descriptor endpoints[] = {{
278 // Bulk end-point for sending values to DMX
279 .bLength = USB_DT_ENDPOINT_SIZE,
280 .bDescriptorType = USB_DT_ENDPOINT,
281 .bEndpointAddress = 0x01,
282 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
283 .wMaxPacketSize = 64,
287 static const struct usb_interface_descriptor iface = {
288 .bLength = USB_DT_INTERFACE_SIZE,
289 .bDescriptorType = USB_DT_INTERFACE,
290 .bInterfaceNumber = 0,
291 .bAlternateSetting = 0,
293 .bInterfaceClass = 0xFF,
294 .bInterfaceSubClass = 0,
295 .bInterfaceProtocol = 0,
297 .endpoint = endpoints,
300 static const struct usb_dfu_descriptor dfu_function = {
301 .bLength = sizeof(struct usb_dfu_descriptor),
302 .bDescriptorType = DFU_FUNCTIONAL,
303 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
304 .wDetachTimeout = 255,
305 .wTransferSize = 1024,
306 .bcdDFUVersion = 0x0100,
309 static const struct usb_interface_descriptor dfu_iface = {
310 .bLength = USB_DT_INTERFACE_SIZE,
311 .bDescriptorType = USB_DT_INTERFACE,
312 .bInterfaceNumber = 1,
313 .bAlternateSetting = 0,
315 .bInterfaceClass = 0xFE,
316 .bInterfaceSubClass = 1,
317 .bInterfaceProtocol = 1,
320 .extra = &dfu_function,
321 .extralen = sizeof(dfu_function),
324 static const struct usb_interface ifaces[] = {{
326 .altsetting = &iface,
329 .altsetting = &dfu_iface,
332 static const struct usb_config_descriptor config = {
333 .bLength = USB_DT_CONFIGURATION_SIZE,
334 .bDescriptorType = USB_DT_CONFIGURATION,
337 .bConfigurationValue = 1,
339 .bmAttributes = 0x80,
340 .bMaxPower = 50, // multiplied by 2 mA
344 static byte usb_configured;
345 static uint8_t usbd_control_buffer[64];
347 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
349 // Reset to bootloader, which implements the rest of DFU
350 debug_printf("Switching to DFU\n");
355 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
356 struct usb_setup_data *req,
357 uint8_t **buf UNUSED,
358 uint16_t *len UNUSED,
359 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
361 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
362 return USBD_REQ_NOTSUPP;
364 *complete = dfu_detach_complete;
365 return USBD_REQ_HANDLED;
368 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
370 // We received a frame from the USB host
371 uint len = usbd_ep_read_packet(dev, 0x01, dmx_next_packet, DMX_MAX_PACKET_SIZE);
372 debug_printf("USB: Host sent %u bytes\n", len);
376 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
378 usbd_register_control_callback(
380 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
381 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
383 usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
387 static void reset_cb(void)
389 debug_printf("USB: Reset\n");
393 static volatile bool usb_event_pending;
395 void usb_lp_can_rx0_isr(void)
398 * We handle USB in the main loop to avoid race conditions between
399 * USB interrupts and other code. However, we need an interrupt to
400 * up the main loop from sleep.
402 * We set up only the low-priority ISR, because high-priority ISR handles
403 * only double-buffered bulk transfers and isochronous transfers.
405 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
406 usb_event_pending = 1;
409 static void usb_init(void)
411 // Simulate USB disconnect
412 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
413 gpio_clear(GPIOA, GPIO11 | GPIO12);
416 usbd_dev = usbd_init(
417 &st_usbfs_v1_usb_driver,
421 ARRAY_SIZE(usb_strings),
423 sizeof(usbd_control_buffer)
425 usbd_register_reset_callback(usbd_dev, reset_cb);
426 usbd_register_set_config_callback(usbd_dev, set_config_cb);
427 usb_event_pending = 1;
438 desig_get_unique_id_as_dfu(usb_serial_number);
440 debug_printf("Hail, Lord Damian! Thy DMX interface is ready.\n");
449 if (ms_ticks - last_blink >= 100) {
451 last_blink = ms_ticks;
455 if (usb_event_pending) {
457 usb_event_pending = 0;
458 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
459 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
462 if (dmx_state == DMX_STATE_IDLE) {
464 // We have a new packet to send
465 debug_printf("Sending new packet: %d bytes\n", dmx_next_len);
466 memcpy(dmx_packet, dmx_next_packet, dmx_next_len);
467 dmx_len = dmx_next_len;
470 last_send = ms_ticks;
471 } else if (ms_ticks - last_send >= 100) {
472 // Re-send every 100 ms
473 // debug_printf("Re-send: %d bytes\n", dmx_len);
475 last_send = ms_ticks;
479 wait_for_interrupt();