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