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_ALTFN_OPENDRAIN, GPIO10);
61 gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO11);
64 static void usart_init(void)
66 usart_set_baudrate(USART1, 115200);
67 usart_set_databits(USART1, 8);
68 usart_set_stopbits(USART1, USART_STOPBITS_1);
69 usart_set_mode(USART1, USART_MODE_TX);
70 usart_set_parity(USART1, USART_PARITY_NONE);
71 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
76 /*** System ticks ***/
78 static volatile u32 ms_ticks;
80 void sys_tick_handler(void)
85 static void tick_init(void)
87 systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
88 systick_counter_enable();
89 systick_interrupt_enable();
92 static void delay_ms(uint ms)
94 u32 start_ticks = ms_ticks;
95 while (ms_ticks - start_ticks < ms)
101 static volatile byte dmx_state;
102 static byte dmx_packet[64];
103 static byte dmx_pos, dmx_len;
107 #define DMX_DEBUG(ch) debug_putc(ch)
109 #define DMX_DEBUG(ch) do { } while (0)
121 static void dmx_init(void)
123 usart_set_baudrate(USART3, 250000);
124 usart_set_databits(USART3, 8);
125 usart_set_stopbits(USART3, USART_STOPBITS_1);
126 usart_set_mode(USART3, USART_MODE_TX);
127 usart_set_parity(USART3, USART_PARITY_NONE);
128 usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE);
129 nvic_enable_irq(NVIC_USART3_IRQ);
131 usart_enable(USART3);
133 timer_set_prescaler(TIM3, CPU_CLOCK_MHZ - 1); // 1 MHz
134 timer_set_mode(TIM3, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
135 timer_update_on_overflow(TIM3);
136 timer_disable_preload(TIM3);
137 timer_one_shot_mode(TIM3);
138 timer_enable_irq(TIM3, TIM_DIER_UIE);
139 nvic_enable_irq(NVIC_TIM3_IRQ);
144 if (TIM_SR(TIM3) & TIM_SR_UIF) {
145 TIM_SR(TIM3) &= ~TIM_SR_UIF;
147 case DMX_STATE_BREAK:
148 // Send mark for 50 μs
150 dmx_state = DMX_STATE_MARK;
151 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO10);
152 timer_set_period(TIM3, 199);
153 timer_generate_event(TIM3, TIM_EGR_UG);
154 timer_enable_counter(TIM3);
157 // Start sending data bytes
159 dmx_state = DMX_STATE_DATA;
160 usart_enable_tx_interrupt(USART3);
164 dmx_state = DMX_STATE_IDLE;
170 void usart3_isr(void)
172 u32 status = USART_SR(USART3);
174 if (dmx_state == DMX_STATE_DATA) {
175 if (status & USART_SR_TXE) {
176 if (dmx_pos < dmx_len) {
178 usart_send(USART3, dmx_packet[dmx_pos++]);
180 // The transmitter is double-buffered, so at this moment, it is transmitting
181 // the last byte of the frame. Wait until transfer is completed.
183 usart_disable_tx_interrupt(USART3);
184 USART_CR1(USART3) |= USART_CR1_TCIE;
185 dmx_state = DMX_STATE_LAST_BYTE;
188 } else if (dmx_state == DMX_STATE_LAST_BYTE) {
189 // Transfer of the last byte is complete, but we have to wait
190 // before the start of the next packet: minimum time between two
191 // breaks must be at least 1204 μs. We already sent a 200μs break,
192 // 200μs mark, and dmx_len 40μs data slots.
193 USART_CR1(USART3) &= ~USART_CR1_TCIE;
194 uint sent = 200 + 200 + 40*dmx_len;
197 dmx_state = DMX_STATE_GAP;
198 timer_set_period(TIM3, 1300 - sent - 1);
199 timer_generate_event(TIM3, TIM_EGR_UG);
200 timer_enable_counter(TIM3);
203 dmx_state = DMX_STATE_IDLE;
208 static void dmx_send(void)
210 if (dmx_state != DMX_STATE_IDLE)
214 dmx_packet[1] = 0xff; // warm
215 dmx_packet[2] = 0xff; // cold
221 // Send break for 200 μs
223 dmx_state = DMX_STATE_BREAK;
224 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO10);
225 gpio_clear(GPIOB, GPIO10);
227 timer_set_period(TIM3, 199);
228 timer_generate_event(TIM3, TIM_EGR_UG);
229 timer_enable_counter(TIM3);
234 static usbd_device *usbd_dev;
237 STR_MANUFACTURER = 1,
242 static char usb_serial_number[13];
244 static const char *usb_strings[] = {
245 "United Computer Wizards",
250 static const struct usb_device_descriptor device = {
251 .bLength = USB_DT_DEVICE_SIZE,
252 .bDescriptorType = USB_DT_DEVICE,
254 .bDeviceClass = 0xFF,
255 .bDeviceSubClass = 0,
256 .bDeviceProtocol = 0,
257 .bMaxPacketSize0 = 64,
258 .idVendor = DMX_USB_VENDOR,
259 .idProduct = DMX_USB_PRODUCT,
260 .bcdDevice = DMX_USB_VERSION,
261 .iManufacturer = STR_MANUFACTURER,
262 .iProduct = STR_PRODUCT,
263 .iSerialNumber = STR_SERIAL,
264 .bNumConfigurations = 1,
267 static const struct usb_endpoint_descriptor endpoints[] = {{
268 // Bulk end-point for sending values to DMX
269 .bLength = USB_DT_ENDPOINT_SIZE,
270 .bDescriptorType = USB_DT_ENDPOINT,
271 .bEndpointAddress = 0x01,
272 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
273 .wMaxPacketSize = 64,
277 static const struct usb_interface_descriptor iface = {
278 .bLength = USB_DT_INTERFACE_SIZE,
279 .bDescriptorType = USB_DT_INTERFACE,
280 .bInterfaceNumber = 0,
281 .bAlternateSetting = 0,
283 .bInterfaceClass = 0xFF,
284 .bInterfaceSubClass = 0,
285 .bInterfaceProtocol = 0,
287 .endpoint = endpoints,
290 static const struct usb_dfu_descriptor dfu_function = {
291 .bLength = sizeof(struct usb_dfu_descriptor),
292 .bDescriptorType = DFU_FUNCTIONAL,
293 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
294 .wDetachTimeout = 255,
295 .wTransferSize = 1024,
296 .bcdDFUVersion = 0x0100,
299 static const struct usb_interface_descriptor dfu_iface = {
300 .bLength = USB_DT_INTERFACE_SIZE,
301 .bDescriptorType = USB_DT_INTERFACE,
302 .bInterfaceNumber = 1,
303 .bAlternateSetting = 0,
305 .bInterfaceClass = 0xFE,
306 .bInterfaceSubClass = 1,
307 .bInterfaceProtocol = 1,
310 .extra = &dfu_function,
311 .extralen = sizeof(dfu_function),
314 static const struct usb_interface ifaces[] = {{
316 .altsetting = &iface,
319 .altsetting = &dfu_iface,
322 static const struct usb_config_descriptor config = {
323 .bLength = USB_DT_CONFIGURATION_SIZE,
324 .bDescriptorType = USB_DT_CONFIGURATION,
327 .bConfigurationValue = 1,
329 .bmAttributes = 0x80,
330 .bMaxPower = 50, // multiplied by 2 mA
334 static byte usb_configured;
335 static uint8_t usbd_control_buffer[64];
337 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
339 // Reset to bootloader, which implements the rest of DFU
340 debug_printf("Switching to DFU\n");
345 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
346 struct usb_setup_data *req,
347 uint8_t **buf UNUSED,
348 uint16_t *len UNUSED,
349 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
351 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
352 return USBD_REQ_NOTSUPP;
354 *complete = dfu_detach_complete;
355 return USBD_REQ_HANDLED;
358 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
360 // We received a frame from the USB host
362 uint len = usbd_ep_read_packet(dev, 0x01, buf, sizeof(buf));
363 debug_printf("USB: Host sent %u bytes\n", len);
366 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
368 usbd_register_control_callback(
370 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
371 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
373 usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
377 static void reset_cb(void)
379 debug_printf("USB: Reset\n");
383 static volatile bool usb_event_pending;
385 void usb_lp_can_rx0_isr(void)
388 * We handle USB in the main loop to avoid race conditions between
389 * USB interrupts and other code. However, we need an interrupt to
390 * up the main loop from sleep.
392 * We set up only the low-priority ISR, because high-priority ISR handles
393 * only double-buffered bulk transfers and isochronous transfers.
395 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
396 usb_event_pending = 1;
399 static void usb_init(void)
401 // Simulate USB disconnect
402 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
403 gpio_clear(GPIOA, GPIO11 | GPIO12);
406 usbd_dev = usbd_init(
407 &st_usbfs_v1_usb_driver,
411 ARRAY_SIZE(usb_strings),
413 sizeof(usbd_control_buffer)
415 usbd_register_reset_callback(usbd_dev, reset_cb);
416 usbd_register_set_config_callback(usbd_dev, set_config_cb);
417 usb_event_pending = 1;
428 desig_get_unique_id_as_dfu(usb_serial_number);
430 debug_printf("Hail, Lord Damian! Thy DMX interface is ready.\n");
438 if (ms_ticks - last_blink >= 100) {
440 last_blink = ms_ticks;
444 if (usb_event_pending) {
446 usb_event_pending = 0;
447 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
448 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
451 wait_for_interrupt();