]> mj.ucw.cz Git - home-hw.git/blob - dmx/firmware/main.c
DMX: First steps
[home-hw.git] / dmx / firmware / main.c
1 /*
2  *      DMX512 Interface
3  *
4  *      (c) 2020 Martin Mareš <mj@ucw.cz>
5  */
6
7 #include "util.h"
8 #include "interface.h"
9
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>
21
22 #include <string.h>
23
24 /*** Hardware init ***/
25
26 static void clock_init(void)
27 {
28         rcc_clock_setup_in_hse_8mhz_out_72mhz();
29
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);
36
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);
43 }
44
45 static void gpio_init(void)
46 {
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);
51
52         // PC13 = BluePill LED
53         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
54         gpio_clear(GPIOC, GPIO13);
55
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);
60 }
61
62 static void usart_init(void)
63 {
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);
70
71         usart_enable(USART1);
72 }
73
74 /*** System ticks ***/
75
76 static volatile u32 ms_ticks;
77
78 void sys_tick_handler(void)
79 {
80         ms_ticks++;
81 }
82
83 static void tick_init(void)
84 {
85         systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
86         systick_counter_enable();
87         systick_interrupt_enable();
88 }
89
90 static void delay_ms(uint ms)
91 {
92         u32 start_ticks = ms_ticks;
93         while (ms_ticks - start_ticks < ms)
94                 ;
95 }
96
97 /*** DMX ***/
98
99 static void dmx_init(void)
100 {
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);
107
108         usart_enable(USART3);
109 }
110
111 static void dmx_send(void)
112 {
113         delay_ms(1);
114
115         // Send break
116         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO10);
117         gpio_clear(GPIOB, GPIO10);
118         delay_ms(1);
119
120         // Send space
121         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO10);
122         delay_ms(1);
123
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
129 }
130
131 /*** USB ***/
132
133 static usbd_device *usbd_dev;
134
135 enum usb_string {
136         STR_MANUFACTURER = 1,
137         STR_PRODUCT,
138         STR_SERIAL,
139 };
140
141 static char usb_serial_number[13];
142
143 static const char *usb_strings[] = {
144         "United Computer Wizards",
145         "DMX512 Interface",
146         usb_serial_number,
147 };
148
149 static const struct usb_device_descriptor device = {
150         .bLength = USB_DT_DEVICE_SIZE,
151         .bDescriptorType = USB_DT_DEVICE,
152         .bcdUSB = 0x0200,
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,
164 };
165
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,
173         .bInterval = 1,
174 }};
175
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,
181         .bNumEndpoints = 1,
182         .bInterfaceClass = 0xFF,
183         .bInterfaceSubClass = 0,
184         .bInterfaceProtocol = 0,
185         .iInterface = 0,
186         .endpoint = endpoints,
187 };
188
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,
196 };
197
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,
203         .bNumEndpoints = 0,
204         .bInterfaceClass = 0xFE,
205         .bInterfaceSubClass = 1,
206         .bInterfaceProtocol = 1,
207         .iInterface = 0,
208
209         .extra = &dfu_function,
210         .extralen = sizeof(dfu_function),
211 };
212
213 static const struct usb_interface ifaces[] = {{
214         .num_altsetting = 1,
215         .altsetting = &iface,
216 }, {
217         .num_altsetting = 1,
218         .altsetting = &dfu_iface,
219 }};
220
221 static const struct usb_config_descriptor config = {
222         .bLength = USB_DT_CONFIGURATION_SIZE,
223         .bDescriptorType = USB_DT_CONFIGURATION,
224         .wTotalLength = 0,
225         .bNumInterfaces = 2,
226         .bConfigurationValue = 1,
227         .iConfiguration = 0,
228         .bmAttributes = 0x80,
229         .bMaxPower = 50,        // multiplied by 2 mA
230         .interface = ifaces,
231 };
232
233 static byte usb_configured;
234 static uint8_t usbd_control_buffer[64];
235
236 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
237 {
238         // Reset to bootloader, which implements the rest of DFU
239         debug_printf("Switching to DFU\n");
240         debug_flush();
241         scb_reset_core();
242 }
243
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))
249 {
250         if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
251                 return USBD_REQ_NOTSUPP;
252
253         *complete = dfu_detach_complete;
254         return USBD_REQ_HANDLED;
255 }
256
257 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
258 {
259         // We received a frame from the USB host
260         byte buf[64];
261         uint len = usbd_ep_read_packet(dev, 0x01, buf, sizeof(buf));
262         debug_printf("USB: Host sent %u bytes\n", len);
263 }
264
265 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
266 {
267         usbd_register_control_callback(
268                 dev,
269                 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
270                 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
271                 dfu_control_cb);
272         usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
273         usb_configured = 1;
274 }
275
276 static void reset_cb(void)
277 {
278         debug_printf("USB: Reset\n");
279         usb_configured = 0;
280 }
281
282 static volatile bool usb_event_pending;
283
284 void usb_lp_can_rx0_isr(void)
285 {
286         /*
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.
290          *
291          *  We set up only the low-priority ISR, because high-priority ISR handles
292          *  only double-buffered bulk transfers and isochronous transfers.
293          */
294         nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
295         usb_event_pending = 1;
296 }
297
298 static void usb_init(void)
299 {
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);
303         delay_ms(100);
304
305         usbd_dev = usbd_init(
306                 &st_usbfs_v1_usb_driver,
307                 &device,
308                 &config,
309                 usb_strings,
310                 ARRAY_SIZE(usb_strings),
311                 usbd_control_buffer,
312                 sizeof(usbd_control_buffer)
313         );
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;
317 }
318
319 /*** Main ***/
320
321 int main(void)
322 {
323         clock_init();
324         gpio_init();
325         tick_init();
326         usart_init();
327         desig_get_unique_id_as_dfu(usb_serial_number);
328
329         debug_printf("Hail, Lord Damian! Thy DMX interface is ready.\n");
330
331         usb_init();
332         dmx_init();
333
334         u32 last_blink = 0;
335
336         for (;;) {
337                 if (ms_ticks - last_blink >= 100) {
338                         debug_led_toggle();
339                         last_blink = ms_ticks;
340                         dmx_send();
341                 }
342
343                 if (usb_event_pending) {
344                         usbd_poll(usbd_dev);
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);
348                 }
349
350                 wait_for_interrupt();
351         }
352
353         return 0;
354 }