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