]> mj.ucw.cz Git - home-hw.git/blob - bsb/firmware/main.c
BSB: Switching to DFU mode
[home-hw.git] / bsb / firmware / main.c
1 /*
2  *      Boiler System Bus Gateway
3  *
4  *      (c) 2020 Martin Mareš <mj@ucw.cz>
5  */
6
7 #include "util.h"
8
9 #include <libopencm3/cm3/cortex.h>
10 #include <libopencm3/cm3/nvic.h>
11 #include <libopencm3/cm3/systick.h>
12 #include <libopencm3/cm3/scb.h>
13 #include <libopencm3/stm32/rcc.h>
14 #include <libopencm3/stm32/desig.h>
15 #include <libopencm3/stm32/gpio.h>
16 #include <libopencm3/stm32/timer.h>
17 #include <libopencm3/stm32/usart.h>
18 #include <libopencm3/usb/dfu.h>
19 #include <libopencm3/usb/usbd.h>
20
21 #include <string.h>
22
23 static void clock_init(void)
24 {
25         rcc_clock_setup_in_hse_8mhz_out_72mhz();
26
27         rcc_periph_clock_enable(RCC_GPIOA);
28         rcc_periph_clock_enable(RCC_GPIOB);
29         rcc_periph_clock_enable(RCC_GPIOC);
30         rcc_periph_clock_enable(RCC_USART1);
31         rcc_periph_clock_enable(RCC_USB);
32
33         rcc_periph_reset_pulse(RST_GPIOA);
34         rcc_periph_reset_pulse(RST_GPIOB);
35         rcc_periph_reset_pulse(RST_GPIOC);
36         rcc_periph_reset_pulse(RST_USART1);
37         rcc_periph_reset_pulse(RST_USB);
38 }
39
40 static void gpio_init(void)
41 {
42         // PA9 = TXD1 for debugging console
43         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
44
45         // PA10 = RXD1 for debugging console
46         gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
47
48         // PC13 = BluePill LED
49         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
50         gpio_clear(GPIOC, GPIO13);
51 }
52
53 static volatile u32 ms_ticks;
54
55 void sys_tick_handler(void)
56 {
57         ms_ticks++;
58 }
59
60 static void tick_init(void)
61 {
62         systick_set_frequency(1000, 72000000);
63         systick_counter_enable();
64         systick_interrupt_enable();
65 }
66
67 static void delay_ms(uint ms)
68 {
69         u32 start_ticks = ms_ticks;
70         while (ms_ticks - start_ticks < ms)
71                 ;
72 }
73
74 static void usart_init(void)
75 {
76         usart_set_baudrate(USART1, 115200);
77         usart_set_databits(USART1, 8);
78         usart_set_stopbits(USART1, USART_STOPBITS_1);
79         usart_set_mode(USART1, USART_MODE_TX_RX);
80         usart_set_parity(USART1, USART_PARITY_NONE);
81         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
82
83         usart_enable(USART1);
84 }
85
86 static const struct usb_device_descriptor device = {
87         .bLength = USB_DT_DEVICE_SIZE,
88         .bDescriptorType = USB_DT_DEVICE,
89         .bcdUSB = 0x0200,
90         .bDeviceClass = 0xFF,
91         .bDeviceSubClass = 0,
92         .bDeviceProtocol = 0,
93         .bMaxPacketSize0 = 64,
94         .idVendor = 0x4242,
95         .idProduct = 0x0003,
96         .bcdDevice = 0x0200,
97         .iManufacturer = 1,
98         .iProduct = 2,
99         .iSerialNumber = 3,
100         .bNumConfigurations = 1,
101 };
102
103 static const struct usb_endpoint_descriptor endpoints[] = {{
104         .bLength = USB_DT_ENDPOINT_SIZE,
105         .bDescriptorType = USB_DT_ENDPOINT,
106         .bEndpointAddress = 0x81,
107         .bmAttributes = USB_ENDPOINT_ATTR_BULK,
108         .wMaxPacketSize = 64,
109         .bInterval = 1,
110 },{
111         .bLength = USB_DT_ENDPOINT_SIZE,
112         .bDescriptorType = USB_DT_ENDPOINT,
113         .bEndpointAddress = 0x82,
114         .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
115         .wMaxPacketSize = 64,
116         .bInterval = 1,
117 }};
118
119 static const struct usb_interface_descriptor iface = {
120         .bLength = USB_DT_INTERFACE_SIZE,
121         .bDescriptorType = USB_DT_INTERFACE,
122         .bInterfaceNumber = 0,
123         .bAlternateSetting = 0,
124         .bNumEndpoints = 2,
125         .bInterfaceClass = 0xFF,
126         .bInterfaceSubClass = 0,
127         .bInterfaceProtocol = 0,
128         .iInterface = 0,
129         .endpoint = endpoints,
130 };
131
132 static const struct usb_dfu_descriptor dfu_function = {
133         .bLength = sizeof(struct usb_dfu_descriptor),
134         .bDescriptorType = DFU_FUNCTIONAL,
135         .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
136         .wDetachTimeout = 255,
137         .wTransferSize = 1024,
138         .bcdDFUVersion = 0x0100,
139 };
140
141 static const struct usb_interface_descriptor dfu_iface = {
142         .bLength = USB_DT_INTERFACE_SIZE,
143         .bDescriptorType = USB_DT_INTERFACE,
144         .bInterfaceNumber = 1,
145         .bAlternateSetting = 0,
146         .bNumEndpoints = 0,
147         .bInterfaceClass = 0xFE,
148         .bInterfaceSubClass = 1,
149         .bInterfaceProtocol = 1,
150         .iInterface = 0,
151
152         .extra = &dfu_function,
153         .extralen = sizeof(dfu_function),
154 };
155
156 static const struct usb_interface ifaces[] = {{
157         .num_altsetting = 1,
158         .altsetting = &iface,
159 }, {
160         .num_altsetting = 1,
161         .altsetting = &dfu_iface,
162 }};
163
164 static const struct usb_config_descriptor config = {
165         .bLength = USB_DT_CONFIGURATION_SIZE,
166         .bDescriptorType = USB_DT_CONFIGURATION,
167         .wTotalLength = 0,
168         .bNumInterfaces = 2,
169         .bConfigurationValue = 1,
170         .iConfiguration = 0,
171         .bmAttributes = 0x80,
172         .bMaxPower = 50,        // multiplied by 2 mA
173         .interface = ifaces,
174 };
175
176 static char usb_serial_number[13];
177
178 static const char *usb_strings[] = {
179         "United Computer Wizards",
180         "BSB Gateway",
181         usb_serial_number,
182 };
183
184 static byte usb_configured;
185 static uint8_t usbd_control_buffer[64];
186
187 static enum usbd_request_return_codes control_cb(usbd_device *usbd_dev,
188         struct usb_setup_data *req,
189         uint8_t **buf,
190         uint16_t *len,
191         void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req) UNUSED)
192 {
193         if (req->bmRequestType != 0xc0 || req->bRequest != 0x00)
194                 return USBD_REQ_NOTSUPP;
195
196         (void) usbd_dev;
197         (void) buf;
198         (void) len;
199         (void) complete;
200         debug_printf("USB: Control request\n");
201
202         byte *b = *buf;
203         b[0] = 0x12;
204         b[1] = 0x34;
205         b[2] = 0x56;
206         b[3] = 0x78;
207         if (*len > 4)
208                 *len = 4;
209
210         return USBD_REQ_HANDLED;
211 }
212
213 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
214 {
215         // Reset to bootloader, which implements the rest of DFU
216         scb_reset_core();
217 }
218
219 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
220         struct usb_setup_data *req,
221         uint8_t **buf UNUSED,
222         uint16_t *len UNUSED,
223         void (**complete)(usbd_device *dev, struct usb_setup_data *req))
224 {
225         if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
226                 return USBD_REQ_NOTSUPP;
227
228         *complete = dfu_detach_complete;
229         return USBD_REQ_HANDLED;
230 }
231
232 static void ep81_cb(usbd_device *usbd_dev, uint8_t ep UNUSED)
233 {
234         byte buf[4];
235         put_u32_be(buf, ms_ticks);
236         usbd_ep_write_packet(usbd_dev, 0x81, buf, sizeof(buf));
237         debug_printf("USB: Bulk write\n");
238 }
239
240 static void set_config_cb(usbd_device *usbd_dev, uint16_t wValue UNUSED)
241 {
242         usbd_register_control_callback(usbd_dev, USB_REQ_TYPE_VENDOR, USB_REQ_TYPE_TYPE, control_cb);
243         usbd_register_control_callback(usbd_dev,
244                 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
245                 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
246                 dfu_control_cb);
247         usbd_ep_setup(usbd_dev, 0x81, USB_ENDPOINT_ATTR_BULK, 64, ep81_cb);
248         usbd_ep_setup(usbd_dev, 0x82, USB_ENDPOINT_ATTR_BULK, 64, NULL);
249         ep81_cb(usbd_dev, 0);
250         usb_configured = 1;
251 }
252
253 static void reset_cb(void)
254 {
255         debug_printf("USB: Reset\n");
256         usb_configured = 0;
257 }
258
259 int main(void)
260 {
261         clock_init();
262         gpio_init();
263         tick_init();
264         usart_init();
265         desig_get_unique_id_as_dfu(usb_serial_number);
266
267         debug_printf("Hello, kitty!\n");
268
269         // Simulate USB disconnect
270         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
271         gpio_clear(GPIOA, GPIO11 | GPIO12);
272         delay_ms(1000);
273
274         usbd_device *usbd_dev = usbd_init(&st_usbfs_v1_usb_driver, &device, &config, usb_strings, ARRAY_SIZE(usb_strings), usbd_control_buffer, sizeof(usbd_control_buffer));
275         usbd_register_reset_callback(usbd_dev, reset_cb);
276         usbd_register_set_config_callback(usbd_dev, set_config_cb);
277         u32 last_ds_step = 0;
278
279         for (;;) {
280                 if (ms_ticks - last_ds_step >= 100) {
281                         debug_led_toggle();
282                         last_ds_step = ms_ticks;
283                         if (usb_configured) {
284                                 byte x[4];
285                                 put_u32_be(x, ms_ticks);
286                                 usbd_ep_write_packet(usbd_dev, 0x82, x, 4);
287                         }
288                 }
289
290                 // XXX: libopencm3 usbd does not use interrupts at the moment, need to poll...
291                 // wait_for_interrupt();
292                 usbd_poll(usbd_dev);
293         }
294
295         return 0;
296 }