2 * Boiler System Bus Gateway
4 * (c) 2020 Martin Mareš <mj@ucw.cz>
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>
21 static void clock_init(void)
23 rcc_clock_setup_in_hse_8mhz_out_72mhz();
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);
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);
38 static void gpio_init(void)
40 // PA9 = TXD1 for debugging console
41 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
43 // PA10 = RXD1 for debugging console
44 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
46 // PC13 = BluePill LED
47 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
48 gpio_clear(GPIOC, GPIO13);
51 static volatile u32 ms_ticks;
53 void sys_tick_handler(void)
58 static void tick_init(void)
60 systick_set_frequency(1000, 72000000);
61 systick_counter_enable();
62 systick_interrupt_enable();
65 static void delay_ms(uint ms)
67 u32 start_ticks = ms_ticks;
68 while (ms_ticks - start_ticks < ms)
72 static void usart_init(void)
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);
84 static const struct usb_device_descriptor dev = {
85 .bLength = USB_DT_DEVICE_SIZE,
86 .bDescriptorType = USB_DT_DEVICE,
91 .bMaxPacketSize0 = 64,
98 .bNumConfigurations = 1,
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,
109 .bLength = USB_DT_ENDPOINT_SIZE,
110 .bDescriptorType = USB_DT_ENDPOINT,
111 .bEndpointAddress = 0x82,
112 .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
113 .wMaxPacketSize = 64,
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,
123 .bInterfaceClass = 0xFF,
124 .bInterfaceSubClass = 0,
125 .bInterfaceProtocol = 0,
127 .endpoint = endpoints,
130 static const struct usb_interface ifaces[] = {{
132 .altsetting = &iface,
135 static const struct usb_config_descriptor config = {
136 .bLength = USB_DT_CONFIGURATION_SIZE,
137 .bDescriptorType = USB_DT_CONFIGURATION,
140 .bConfigurationValue = 1,
142 .bmAttributes = 0x80,
143 .bMaxPower = 50, // multiplied by 2 mA
147 static char usb_serial_number[13];
149 static const char *usb_strings[] = {
150 "United Computer Wizards",
155 static byte usb_configured;
156 static uint8_t usbd_control_buffer[64];
158 static enum usbd_request_return_codes control_cb(usbd_device *usbd_dev,
159 struct usb_setup_data *req,
162 void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req) UNUSED)
164 if (req->bmRequestType != 0xc0 || req->bRequest != 0x00)
165 return USBD_REQ_NOTSUPP;
171 debug_printf("USB: Control request\n");
181 return USBD_REQ_HANDLED;
184 static void ep81_cb(usbd_device *usbd_dev, uint8_t ep UNUSED)
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");
192 static void set_config_cb(usbd_device *usbd_dev, uint16_t wValue UNUSED)
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);
201 static void reset_cb(void)
203 debug_printf("USB: Reset\n");
213 desig_get_unique_id_as_dfu(usb_serial_number);
215 debug_printf("Hello, kitty!\n");
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);
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;
228 if (ms_ticks - last_ds_step >= 100) {
230 last_ds_step = ms_ticks;
231 if (usb_configured) {
233 put_u32_be(x, ms_ticks);
234 usbd_ep_write_packet(usbd_dev, 0x82, x, 4);
238 // XXX: libopencm3 usbd does not use interrupts at the moment, need to poll...
239 // wait_for_interrupt();