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