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 void display_init(void)
119 debug_puts("I2C init\n");
120 i2c_peripheral_disable(I2C1);
121 i2c_set_speed(I2C1, i2c_speed_sm_100k, CPU_CLOCK_MHZ);
122 i2c_peripheral_enable(I2C1);
124 debug_puts("I2C transfer\n");
126 // byte disp[] = { 0x7f, 0x12, 0xb6, 0xd2 };
127 byte disp[] = { 0xff, 0xff, 0xff, 0xff };
128 byte cmds[] = { 0x00, 0x47, 0, 0, 0, 0 };
129 cmds[2] = (disp[0] & 0xf0) | (disp[2] >> 4);
130 cmds[3] = (disp[1] & 0xf0) | (disp[3] >> 4);
131 cmds[4] = (disp[2] & 0x0f) | (disp[0] << 4);
132 cmds[5] = (disp[3] & 0x0f) | (disp[1] << 4);
133 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
135 debug_puts("I2C done\n");
140 static usbd_device *usbd_dev;
143 STR_MANUFACTURER = 1,
148 static char usb_serial_number[13];
150 static const char *usb_strings[] = {
151 "United Computer Wizards",
156 static const struct usb_device_descriptor device = {
157 .bLength = USB_DT_DEVICE_SIZE,
158 .bDescriptorType = USB_DT_DEVICE,
160 .bDeviceClass = 0xFF,
161 .bDeviceSubClass = 0,
162 .bDeviceProtocol = 0,
163 .bMaxPacketSize0 = 64,
167 .iManufacturer = STR_MANUFACTURER,
168 .iProduct = STR_PRODUCT,
169 .iSerialNumber = STR_SERIAL,
170 .bNumConfigurations = 1,
173 static const struct usb_endpoint_descriptor endpoints[] = {{
174 // Bulk end-point for sending values to DMX
175 .bLength = USB_DT_ENDPOINT_SIZE,
176 .bDescriptorType = USB_DT_ENDPOINT,
177 .bEndpointAddress = 0x01,
178 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
179 .wMaxPacketSize = 64,
183 static const struct usb_interface_descriptor iface = {
184 .bLength = USB_DT_INTERFACE_SIZE,
185 .bDescriptorType = USB_DT_INTERFACE,
186 .bInterfaceNumber = 0,
187 .bAlternateSetting = 0,
189 .bInterfaceClass = 0xFF,
190 .bInterfaceSubClass = 0,
191 .bInterfaceProtocol = 0,
193 .endpoint = endpoints,
196 static const struct usb_dfu_descriptor dfu_function = {
197 .bLength = sizeof(struct usb_dfu_descriptor),
198 .bDescriptorType = DFU_FUNCTIONAL,
199 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
200 .wDetachTimeout = 255,
201 .wTransferSize = 1024,
202 .bcdDFUVersion = 0x0100,
205 static const struct usb_interface_descriptor dfu_iface = {
206 .bLength = USB_DT_INTERFACE_SIZE,
207 .bDescriptorType = USB_DT_INTERFACE,
208 .bInterfaceNumber = 1,
209 .bAlternateSetting = 0,
211 .bInterfaceClass = 0xFE,
212 .bInterfaceSubClass = 1,
213 .bInterfaceProtocol = 1,
216 .extra = &dfu_function,
217 .extralen = sizeof(dfu_function),
220 static const struct usb_interface ifaces[] = {{
222 .altsetting = &iface,
225 .altsetting = &dfu_iface,
228 static const struct usb_config_descriptor config = {
229 .bLength = USB_DT_CONFIGURATION_SIZE,
230 .bDescriptorType = USB_DT_CONFIGURATION,
233 .bConfigurationValue = 1,
235 .bmAttributes = 0x80,
236 .bMaxPower = 50, // multiplied by 2 mA
240 static byte usb_configured;
241 static uint8_t usbd_control_buffer[64];
243 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
245 // Reset to bootloader, which implements the rest of DFU
246 debug_printf("Switching to DFU\n");
251 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
252 struct usb_setup_data *req,
253 uint8_t **buf UNUSED,
254 uint16_t *len UNUSED,
255 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
257 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
258 return USBD_REQ_NOTSUPP;
260 *complete = dfu_detach_complete;
261 return USBD_REQ_HANDLED;
265 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
267 // We received a frame from the USB host
268 uint len = usbd_ep_read_packet(dev, 0x01, dmx_next_packet, DMX_MAX_PACKET_SIZE);
269 debug_printf("USB: Host sent %u bytes\n", len);
273 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
275 usbd_register_control_callback(
277 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
278 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
280 // usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
284 static void reset_cb(void)
286 debug_printf("USB: Reset\n");
290 static volatile bool usb_event_pending;
292 void usb_lp_can_rx0_isr(void)
295 * We handle USB in the main loop to avoid race conditions between
296 * USB interrupts and other code. However, we need an interrupt to
297 * up the main loop from sleep.
299 * We set up only the low-priority ISR, because high-priority ISR handles
300 * only double-buffered bulk transfers and isochronous transfers.
302 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
303 usb_event_pending = 1;
306 static void usb_init(void)
308 // Simulate USB disconnect
309 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
310 gpio_clear(GPIOA, GPIO11 | GPIO12);
313 usbd_dev = usbd_init(
314 &st_usbfs_v1_usb_driver,
318 ARRAY_SIZE(usb_strings),
320 sizeof(usbd_control_buffer)
322 usbd_register_reset_callback(usbd_dev, reset_cb);
323 usbd_register_set_config_callback(usbd_dev, set_config_cb);
324 usb_event_pending = 1;
335 desig_get_unique_id_as_dfu(usb_serial_number);
337 debug_printf("Hello, world!\n");
345 if (ms_ticks - last_blink >= 100) {
347 last_blink = ms_ticks;
350 if (usb_event_pending) {
352 usb_event_pending = 0;
353 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
354 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
357 wait_for_interrupt();