2 * Boiler System Bus Gateway
4 * (c) 2020 Martin Mareš <mj@ucw.cz>
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>
20 static void clock_init(void)
22 rcc_clock_setup_in_hse_8mhz_out_72mhz();
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);
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);
37 static void gpio_init(void)
39 // PA9 = TXD1 for debugging console
40 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
42 // PA10 = RXD1 for debugging console
43 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
45 // PC13 = BluePill LED
46 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
47 gpio_clear(GPIOC, GPIO13);
50 static volatile u32 ms_ticks;
52 void sys_tick_handler(void)
57 static void tick_init(void)
59 systick_set_frequency(1000, 72000000);
60 systick_counter_enable();
61 systick_interrupt_enable();
64 static void delay_ms(uint ms)
66 u32 start_ticks = ms_ticks;
67 while (ms_ticks - start_ticks < ms)
71 static void usart_init(void)
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);
83 static const struct usb_device_descriptor dev = {
84 .bLength = USB_DT_DEVICE_SIZE,
85 .bDescriptorType = USB_DT_DEVICE,
90 .bMaxPacketSize0 = 64,
97 .bNumConfigurations = 1,
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,
108 .bLength = USB_DT_ENDPOINT_SIZE,
109 .bDescriptorType = USB_DT_ENDPOINT,
110 .bEndpointAddress = 0x82,
111 .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
112 .wMaxPacketSize = 64,
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,
122 .bInterfaceClass = 0xFF,
123 .bInterfaceSubClass = 0,
124 .bInterfaceProtocol = 0,
126 .endpoint = endpoints,
129 static const struct usb_interface ifaces[] = {{
131 .altsetting = &iface,
134 static const struct usb_config_descriptor config = {
135 .bLength = USB_DT_CONFIGURATION_SIZE,
136 .bDescriptorType = USB_DT_CONFIGURATION,
139 .bConfigurationValue = 1,
141 .bmAttributes = 0x80,
142 .bMaxPower = 50, // multiplied by 2 mA
146 static char usb_serial_number[13];
148 static const char *usb_strings[] = {
149 "United Computer Wizards",
154 static byte usb_configured;
155 static uint8_t usbd_control_buffer[64];
157 static enum usbd_request_return_codes control_cb(usbd_device *usbd_dev,
158 struct usb_setup_data *req,
161 void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req) UNUSED)
163 if (req->bmRequestType != 0xc0 || req->bRequest != 0x00)
164 return USBD_REQ_NOTSUPP;
170 debug_printf("USB: Control request\n");
180 return USBD_REQ_HANDLED;
183 static void ep81_cb(usbd_device *usbd_dev, uint8_t ep UNUSED)
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");
191 static void set_config_cb(usbd_device *usbd_dev, uint16_t wValue UNUSED)
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);
200 static void reset_cb(void)
202 debug_printf("USB: Reset\n");
212 desig_get_unique_id_as_dfu(usb_serial_number);
214 debug_printf("Hello, kitty!\n");
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);
221 usbd_device *usbd_dev = usbd_init(&st_usbfs_v1_usb_driver, &dev, &config, usb_strings, 3, 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;
227 if (ms_ticks - last_ds_step >= 100) {
229 last_ds_step = ms_ticks;
230 if (usb_configured) {
232 put_u32_be(x, ms_ticks);
233 usbd_ep_write_packet(usbd_dev, 0x82, x, 4);
237 // XXX: libopencm3 usbd does not use interrupts at the moment, need to poll...
238 // wait_for_interrupt();