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> // 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>
19 static void clock_init(void)
21 rcc_clock_setup_in_hse_8mhz_out_72mhz();
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);
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);
36 static void gpio_init(void)
38 // PA9 = TXD1 for debugging console
39 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
41 // PA10 = RXD1 for debugging console
42 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
44 // PC13 = BluePill LED
45 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
46 gpio_clear(GPIOC, GPIO13);
49 static volatile u32 ms_ticks;
51 void sys_tick_handler(void)
56 static void tick_init(void)
58 systick_set_frequency(1000, 72000000);
59 systick_counter_enable();
60 systick_interrupt_enable();
63 static void delay_ms(uint ms)
65 u32 start_ticks = ms_ticks;
66 while (ms_ticks - start_ticks < ms)
70 static void usart_init(void)
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);
82 static const struct usb_device_descriptor dev = {
83 .bLength = USB_DT_DEVICE_SIZE,
84 .bDescriptorType = USB_DT_DEVICE,
89 .bMaxPacketSize0 = 64,
96 .bNumConfigurations = 1,
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,
107 .bLength = USB_DT_ENDPOINT_SIZE,
108 .bDescriptorType = USB_DT_ENDPOINT,
109 .bEndpointAddress = 0x82,
110 .bmAttributes = USB_ENDPOINT_ATTR_INTERRUPT,
111 .wMaxPacketSize = 64,
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,
121 .bInterfaceClass = 0xFF,
122 .bInterfaceSubClass = 0,
123 .bInterfaceProtocol = 0,
125 .endpoint = endpoints,
128 static const struct usb_interface ifaces[] = {{
130 .altsetting = &iface,
133 static const struct usb_config_descriptor config = {
134 .bLength = USB_DT_CONFIGURATION_SIZE,
135 .bDescriptorType = USB_DT_CONFIGURATION,
138 .bConfigurationValue = 1,
140 .bmAttributes = 0x80,
141 .bMaxPower = 50, // multiplied by 2 mA
145 static const char *usb_strings[] = {
146 "United Computer Wizards",
151 static byte usb_configured;
152 static uint8_t usbd_control_buffer[64];
154 static enum usbd_request_return_codes control_cb(usbd_device *usbd_dev,
155 struct usb_setup_data *req,
158 void (**complete)(usbd_device *usbd_dev, struct usb_setup_data *req) UNUSED)
160 if (req->bmRequestType != 0xc0 || req->bRequest != 0x00)
161 return USBD_REQ_NOTSUPP;
167 debug_printf("USB: Control request\n");
177 return USBD_REQ_HANDLED;
180 static void ep81_cb(usbd_device *usbd_dev, uint8_t ep UNUSED)
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");
188 static void set_config_cb(usbd_device *usbd_dev, uint16_t wValue UNUSED)
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);
197 static void reset_cb(void)
199 debug_printf("USB: Reset\n");
210 debug_printf("Hello, kitty!\n");
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);
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;
223 if (ms_ticks - last_ds_step >= 100) {
225 last_ds_step = ms_ticks;
226 if (usb_configured) {
228 put_u32_be(x, ms_ticks);
229 usbd_ep_write_packet(usbd_dev, 0x82, x, 4);
233 // XXX: libopencm3 usbd does not use interrupts at the moment, need to poll...
234 // wait_for_interrupt();