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, rcc_apb1_frequency / 1000000);
122 i2c_peripheral_enable(I2C1);
124 debug_puts("I2C transfer\n");
126 // byte disp[] = { 0xb6, 0xb6, 0xb6, 0xb6 };
127 // byte disp[] = { 0x7f, 0x12, 0xbc, 0xb6 };
128 // byte disp[] = { 0xff, 0xff, 0xff, 0xff };
129 byte disp[] = { 0x80, 0x80, 0x80, 0xff };
130 byte cmds[] = { 0x00, 0x47, 0, 0, 0, 0 };
131 cmds[2] = (disp[0] & 0xf0) | (disp[2] >> 4);
132 cmds[3] = (disp[1] & 0xf0) | (disp[3] >> 4);
133 cmds[4] = (disp[2] & 0x0f) | (disp[0] << 4);
134 cmds[5] = (disp[3] & 0x0f) | (disp[1] << 4);
135 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
137 debug_puts("I2C done\n");
140 static void display_test(void)
144 byte cmds[] = { 0x00, mode ? 0x77 : 0x77 };
145 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
152 static usbd_device *usbd_dev;
155 STR_MANUFACTURER = 1,
160 static char usb_serial_number[13];
162 static const char *usb_strings[] = {
163 "United Computer Wizards",
168 static const struct usb_device_descriptor device = {
169 .bLength = USB_DT_DEVICE_SIZE,
170 .bDescriptorType = USB_DT_DEVICE,
172 .bDeviceClass = 0xFF,
173 .bDeviceSubClass = 0,
174 .bDeviceProtocol = 0,
175 .bMaxPacketSize0 = 64,
179 .iManufacturer = STR_MANUFACTURER,
180 .iProduct = STR_PRODUCT,
181 .iSerialNumber = STR_SERIAL,
182 .bNumConfigurations = 1,
185 static const struct usb_endpoint_descriptor endpoints[] = {{
186 // Bulk end-point for sending values to DMX
187 .bLength = USB_DT_ENDPOINT_SIZE,
188 .bDescriptorType = USB_DT_ENDPOINT,
189 .bEndpointAddress = 0x01,
190 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
191 .wMaxPacketSize = 64,
195 static const struct usb_interface_descriptor iface = {
196 .bLength = USB_DT_INTERFACE_SIZE,
197 .bDescriptorType = USB_DT_INTERFACE,
198 .bInterfaceNumber = 0,
199 .bAlternateSetting = 0,
201 .bInterfaceClass = 0xFF,
202 .bInterfaceSubClass = 0,
203 .bInterfaceProtocol = 0,
205 .endpoint = endpoints,
208 static const struct usb_dfu_descriptor dfu_function = {
209 .bLength = sizeof(struct usb_dfu_descriptor),
210 .bDescriptorType = DFU_FUNCTIONAL,
211 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
212 .wDetachTimeout = 255,
213 .wTransferSize = 1024,
214 .bcdDFUVersion = 0x0100,
217 static const struct usb_interface_descriptor dfu_iface = {
218 .bLength = USB_DT_INTERFACE_SIZE,
219 .bDescriptorType = USB_DT_INTERFACE,
220 .bInterfaceNumber = 1,
221 .bAlternateSetting = 0,
223 .bInterfaceClass = 0xFE,
224 .bInterfaceSubClass = 1,
225 .bInterfaceProtocol = 1,
228 .extra = &dfu_function,
229 .extralen = sizeof(dfu_function),
232 static const struct usb_interface ifaces[] = {{
234 .altsetting = &iface,
237 .altsetting = &dfu_iface,
240 static const struct usb_config_descriptor config = {
241 .bLength = USB_DT_CONFIGURATION_SIZE,
242 .bDescriptorType = USB_DT_CONFIGURATION,
245 .bConfigurationValue = 1,
247 .bmAttributes = 0x80,
248 .bMaxPower = 50, // multiplied by 2 mA
252 static byte usb_configured;
253 static uint8_t usbd_control_buffer[64];
255 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
257 // Reset to bootloader, which implements the rest of DFU
258 debug_printf("Switching to DFU\n");
263 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
264 struct usb_setup_data *req,
265 uint8_t **buf UNUSED,
266 uint16_t *len UNUSED,
267 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
269 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
270 return USBD_REQ_NOTSUPP;
272 *complete = dfu_detach_complete;
273 return USBD_REQ_HANDLED;
277 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
279 // We received a frame from the USB host
280 uint len = usbd_ep_read_packet(dev, 0x01, dmx_next_packet, DMX_MAX_PACKET_SIZE);
281 debug_printf("USB: Host sent %u bytes\n", len);
285 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
287 usbd_register_control_callback(
289 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
290 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
292 // usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
296 static void reset_cb(void)
298 debug_printf("USB: Reset\n");
302 static volatile bool usb_event_pending;
304 void usb_lp_can_rx0_isr(void)
307 * We handle USB in the main loop to avoid race conditions between
308 * USB interrupts and other code. However, we need an interrupt to
309 * up the main loop from sleep.
311 * We set up only the low-priority ISR, because high-priority ISR handles
312 * only double-buffered bulk transfers and isochronous transfers.
314 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
315 usb_event_pending = 1;
318 static void usb_init(void)
320 // Simulate USB disconnect
321 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
322 gpio_clear(GPIOA, GPIO11 | GPIO12);
325 usbd_dev = usbd_init(
326 &st_usbfs_v1_usb_driver,
330 ARRAY_SIZE(usb_strings),
332 sizeof(usbd_control_buffer)
334 usbd_register_reset_callback(usbd_dev, reset_cb);
335 usbd_register_set_config_callback(usbd_dev, set_config_cb);
336 usb_event_pending = 1;
347 desig_get_unique_id_as_dfu(usb_serial_number);
349 debug_printf("Hello, world!\n");
357 if (ms_ticks - last_blink >= 100) {
359 last_blink = ms_ticks;
363 if (usb_event_pending) {
365 usb_event_pending = 0;
366 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
367 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
370 wait_for_interrupt();