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);
55 // PB7 = SDA for display controller
56 // PB6 = SCL for display controller
57 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO6 | GPIO7);
60 static void usart_init(void)
62 usart_set_baudrate(USART1, 115200);
63 usart_set_databits(USART1, 8);
64 usart_set_stopbits(USART1, USART_STOPBITS_1);
65 usart_set_mode(USART1, USART_MODE_TX);
66 usart_set_parity(USART1, USART_PARITY_NONE);
67 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
72 /*** System ticks ***/
74 static volatile u32 ms_ticks;
76 void sys_tick_handler(void)
81 static void tick_init(void)
83 systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
84 systick_counter_enable();
85 systick_interrupt_enable();
88 static void delay_ms(uint ms)
90 u32 start_ticks = ms_ticks;
91 while (ms_ticks - start_ticks < ms)
117 static byte ctrl = 0x77;
119 static void display_update(void)
124 cmds[2] = (disp[0] & 0xf0) | (disp[2] >> 4);
125 cmds[3] = (disp[1] & 0xf0) | (disp[3] >> 4);
126 cmds[4] = (disp[2] & 0x0f) | (disp[0] << 4);
127 cmds[5] = (disp[3] & 0x0f) | (disp[1] << 4);
129 debug_puts("I2C transfer\n");
130 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
131 debug_puts("I2C done\n");
134 static void display_init(void)
136 debug_puts("I2C init\n");
137 i2c_peripheral_disable(I2C1);
138 i2c_set_speed(I2C1, i2c_speed_sm_100k, rcc_apb1_frequency / 1000000);
139 i2c_peripheral_enable(I2C1);
148 static void display_test(void)
156 byte cmds[] = { 0x00, mode ? 0x77 : 0x77 };
157 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
161 byte disp[] = { 0xff, 0xff, mode ? 0xff : 0x00, mode ? 0xff : 0xff };
162 byte cmds[] = { 0x00, 0x77, 0, 0, 0, 0 };
163 cmds[2] = (disp[0] & 0xf0) | (disp[2] >> 4);
164 cmds[3] = (disp[1] & 0xf0) | (disp[3] >> 4);
165 cmds[4] = (disp[2] & 0x0f) | (disp[0] << 4);
166 cmds[5] = (disp[3] & 0x0f) | (disp[1] << 4);
167 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
175 static usbd_device *usbd_dev;
178 STR_MANUFACTURER = 1,
183 static char usb_serial_number[13];
185 static const char *usb_strings[] = {
186 "United Computer Wizards",
191 static const struct usb_device_descriptor device = {
192 .bLength = USB_DT_DEVICE_SIZE,
193 .bDescriptorType = USB_DT_DEVICE,
195 .bDeviceClass = 0xFF,
196 .bDeviceSubClass = 0,
197 .bDeviceProtocol = 0,
198 .bMaxPacketSize0 = 64,
202 .iManufacturer = STR_MANUFACTURER,
203 .iProduct = STR_PRODUCT,
204 .iSerialNumber = STR_SERIAL,
205 .bNumConfigurations = 1,
208 static const struct usb_endpoint_descriptor endpoints[] = {{
209 // Bulk end-point for sending values to DMX
210 .bLength = USB_DT_ENDPOINT_SIZE,
211 .bDescriptorType = USB_DT_ENDPOINT,
212 .bEndpointAddress = 0x01,
213 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
214 .wMaxPacketSize = 64,
218 static const struct usb_interface_descriptor iface = {
219 .bLength = USB_DT_INTERFACE_SIZE,
220 .bDescriptorType = USB_DT_INTERFACE,
221 .bInterfaceNumber = 0,
222 .bAlternateSetting = 0,
224 .bInterfaceClass = 0xFF,
225 .bInterfaceSubClass = 0,
226 .bInterfaceProtocol = 0,
228 .endpoint = endpoints,
231 static const struct usb_dfu_descriptor dfu_function = {
232 .bLength = sizeof(struct usb_dfu_descriptor),
233 .bDescriptorType = DFU_FUNCTIONAL,
234 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
235 .wDetachTimeout = 255,
236 .wTransferSize = 1024,
237 .bcdDFUVersion = 0x0100,
240 static const struct usb_interface_descriptor dfu_iface = {
241 .bLength = USB_DT_INTERFACE_SIZE,
242 .bDescriptorType = USB_DT_INTERFACE,
243 .bInterfaceNumber = 1,
244 .bAlternateSetting = 0,
246 .bInterfaceClass = 0xFE,
247 .bInterfaceSubClass = 1,
248 .bInterfaceProtocol = 1,
251 .extra = &dfu_function,
252 .extralen = sizeof(dfu_function),
255 static const struct usb_interface ifaces[] = {{
257 .altsetting = &iface,
260 .altsetting = &dfu_iface,
263 static const struct usb_config_descriptor config = {
264 .bLength = USB_DT_CONFIGURATION_SIZE,
265 .bDescriptorType = USB_DT_CONFIGURATION,
268 .bConfigurationValue = 1,
270 .bmAttributes = 0x80,
271 .bMaxPower = 50, // multiplied by 2 mA
275 static byte usb_configured;
276 static uint8_t usbd_control_buffer[64];
278 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
280 // Reset to bootloader, which implements the rest of DFU
281 debug_printf("Switching to DFU\n");
286 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
287 struct usb_setup_data *req,
288 uint8_t **buf UNUSED,
289 uint16_t *len UNUSED,
290 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
292 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
293 return USBD_REQ_NOTSUPP;
295 *complete = dfu_detach_complete;
296 return USBD_REQ_HANDLED;
300 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
302 // We received a frame from the USB host
303 uint len = usbd_ep_read_packet(dev, 0x01, dmx_next_packet, DMX_MAX_PACKET_SIZE);
304 debug_printf("USB: Host sent %u bytes\n", len);
308 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
310 usbd_register_control_callback(
312 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
313 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
315 // usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
319 static void reset_cb(void)
321 debug_printf("USB: Reset\n");
325 static volatile bool usb_event_pending;
327 void usb_lp_can_rx0_isr(void)
330 * We handle USB in the main loop to avoid race conditions between
331 * USB interrupts and other code. However, we need an interrupt to
332 * up the main loop from sleep.
334 * We set up only the low-priority ISR, because high-priority ISR handles
335 * only double-buffered bulk transfers and isochronous transfers.
337 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
338 usb_event_pending = 1;
341 static void usb_init(void)
343 // Simulate USB disconnect
344 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
345 gpio_clear(GPIOA, GPIO11 | GPIO12);
348 usbd_dev = usbd_init(
349 &st_usbfs_v1_usb_driver,
353 ARRAY_SIZE(usb_strings),
355 sizeof(usbd_control_buffer)
357 usbd_register_reset_callback(usbd_dev, reset_cb);
358 usbd_register_set_config_callback(usbd_dev, set_config_cb);
359 usb_event_pending = 1;
370 desig_get_unique_id_as_dfu(usb_serial_number);
372 debug_printf("Hello, world!\n");
380 if (ms_ticks - last_blink >= 500) {
382 last_blink = ms_ticks;
386 if (usb_event_pending) {
388 usb_event_pending = 0;
389 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
390 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
393 wait_for_interrupt();