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/cm3/scb.h>
13 #include <libopencm3/stm32/rcc.h>
14 #include <libopencm3/stm32/desig.h>
15 #include <libopencm3/stm32/gpio.h>
16 #include <libopencm3/stm32/usart.h>
17 #include <libopencm3/stm32/i2c.h>
18 #include <libopencm3/usb/dfu.h>
19 #include <libopencm3/usb/usbd.h>
23 /*** Hardware init ***/
25 static void clock_init(void)
27 rcc_clock_setup_in_hse_8mhz_out_72mhz();
29 rcc_periph_clock_enable(RCC_GPIOA);
30 rcc_periph_clock_enable(RCC_GPIOB);
31 rcc_periph_clock_enable(RCC_GPIOC);
32 rcc_periph_clock_enable(RCC_I2C1);
33 rcc_periph_clock_enable(RCC_USART1);
34 rcc_periph_clock_enable(RCC_USB);
36 rcc_periph_reset_pulse(RST_GPIOA);
37 rcc_periph_reset_pulse(RST_GPIOB);
38 rcc_periph_reset_pulse(RST_GPIOC);
39 rcc_periph_reset_pulse(RST_I2C1);
40 rcc_periph_reset_pulse(RST_USART1);
41 rcc_periph_reset_pulse(RST_USB);
44 static void gpio_init(void)
46 // PA9 = TXD1 for debugging console
47 // PA10 = RXD1 for debugging console
48 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
49 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
51 // PC13 = BluePill LED
52 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
53 gpio_clear(GPIOC, GPIO13);
56 static void usart_init(void)
58 usart_set_baudrate(USART1, 115200);
59 usart_set_databits(USART1, 8);
60 usart_set_stopbits(USART1, USART_STOPBITS_1);
61 usart_set_mode(USART1, USART_MODE_TX);
62 usart_set_parity(USART1, USART_PARITY_NONE);
63 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
68 /*** System ticks ***/
70 static volatile u32 ms_ticks;
72 void sys_tick_handler(void)
77 static void tick_init(void)
79 systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
80 systick_counter_enable();
81 systick_interrupt_enable();
84 static void delay_ms(uint ms)
86 u32 start_ticks = ms_ticks;
87 while (ms_ticks - start_ticks < ms)
93 static usbd_device *usbd_dev;
101 static char usb_serial_number[13];
103 static const char *usb_strings[] = {
104 "United Computer Wizards",
105 "Sinclair Air Conditioner",
109 static const struct usb_device_descriptor device = {
110 .bLength = USB_DT_DEVICE_SIZE,
111 .bDescriptorType = USB_DT_DEVICE,
113 .bDeviceClass = 0xFF,
114 .bDeviceSubClass = 0,
115 .bDeviceProtocol = 0,
116 .bMaxPacketSize0 = 64,
120 .iManufacturer = STR_MANUFACTURER,
121 .iProduct = STR_PRODUCT,
122 .iSerialNumber = STR_SERIAL,
123 .bNumConfigurations = 1,
126 static const struct usb_endpoint_descriptor endpoints[] = {{
127 // Bulk end-point for sending values to the display
128 .bLength = USB_DT_ENDPOINT_SIZE,
129 .bDescriptorType = USB_DT_ENDPOINT,
130 .bEndpointAddress = 0x01,
131 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
132 .wMaxPacketSize = 64,
136 static const struct usb_interface_descriptor iface = {
137 .bLength = USB_DT_INTERFACE_SIZE,
138 .bDescriptorType = USB_DT_INTERFACE,
139 .bInterfaceNumber = 0,
140 .bAlternateSetting = 0,
142 .bInterfaceClass = 0xFF,
143 .bInterfaceSubClass = 0,
144 .bInterfaceProtocol = 0,
146 .endpoint = endpoints,
149 static const struct usb_dfu_descriptor dfu_function = {
150 .bLength = sizeof(struct usb_dfu_descriptor),
151 .bDescriptorType = DFU_FUNCTIONAL,
152 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
153 .wDetachTimeout = 255,
154 .wTransferSize = 1024,
155 .bcdDFUVersion = 0x0100,
158 static const struct usb_interface_descriptor dfu_iface = {
159 .bLength = USB_DT_INTERFACE_SIZE,
160 .bDescriptorType = USB_DT_INTERFACE,
161 .bInterfaceNumber = 1,
162 .bAlternateSetting = 0,
164 .bInterfaceClass = 0xFE,
165 .bInterfaceSubClass = 1,
166 .bInterfaceProtocol = 1,
169 .extra = &dfu_function,
170 .extralen = sizeof(dfu_function),
173 static const struct usb_interface ifaces[] = {{
175 .altsetting = &iface,
178 .altsetting = &dfu_iface,
181 static const struct usb_config_descriptor config = {
182 .bLength = USB_DT_CONFIGURATION_SIZE,
183 .bDescriptorType = USB_DT_CONFIGURATION,
186 .bConfigurationValue = 1,
188 .bmAttributes = 0x80,
189 .bMaxPower = 50, // multiplied by 2 mA
193 static byte usb_configured;
194 static uint8_t usbd_control_buffer[64];
196 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
198 // Reset to bootloader, which implements the rest of DFU
199 debug_printf("Switching to DFU\n");
204 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
205 struct usb_setup_data *req,
206 uint8_t **buf UNUSED,
207 uint16_t *len UNUSED,
208 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
210 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
211 return USBD_REQ_NOTSUPP;
213 *complete = dfu_detach_complete;
214 return USBD_REQ_HANDLED;
217 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
219 // We received a frame from the USB host
221 uint len = usbd_ep_read_packet(dev, 0x01, buf, 8);
222 debug_printf("USB: Host sent %u bytes\n", len);
225 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
227 usbd_register_control_callback(
229 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
230 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
232 usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
236 static void reset_cb(void)
238 debug_printf("USB: Reset\n");
242 static volatile bool usb_event_pending;
244 void usb_lp_can_rx0_isr(void)
247 * We handle USB in the main loop to avoid race conditions between
248 * USB interrupts and other code. However, we need an interrupt to
249 * up the main loop from sleep.
251 * We set up only the low-priority ISR, because high-priority ISR handles
252 * only double-buffered bulk transfers and isochronous transfers.
254 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
255 usb_event_pending = 1;
258 static void usb_init(void)
260 // Simulate USB disconnect
261 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
262 gpio_clear(GPIOA, GPIO11 | GPIO12);
265 usbd_dev = usbd_init(
266 &st_usbfs_v1_usb_driver,
270 ARRAY_SIZE(usb_strings),
272 sizeof(usbd_control_buffer)
274 usbd_register_reset_callback(usbd_dev, reset_cb);
275 usbd_register_set_config_callback(usbd_dev, set_config_cb);
276 usb_event_pending = 1;
288 desig_get_unique_id_as_dfu(usb_serial_number);
290 debug_printf("Hello, world!\n");
297 if (ms_ticks - last_blink >= 1000) {
299 last_blink = ms_ticks;
302 if (usb_event_pending) {
304 usb_event_pending = 0;
305 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
306 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
309 wait_for_interrupt();