4 * (c) 2020-2023 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/stm32/timer.h>
19 #include <libopencm3/usb/dfu.h>
20 #include <libopencm3/usb/usbd.h>
24 /*** Hardware init ***/
26 static void clock_init(void)
28 rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
30 rcc_periph_clock_enable(RCC_GPIOA);
31 rcc_periph_clock_enable(RCC_GPIOB);
32 rcc_periph_clock_enable(RCC_GPIOC);
33 rcc_periph_clock_enable(RCC_I2C1);
34 rcc_periph_clock_enable(RCC_USART1);
35 rcc_periph_clock_enable(RCC_USB);
36 rcc_periph_clock_enable(RCC_TIM1);
38 rcc_periph_reset_pulse(RST_GPIOA);
39 rcc_periph_reset_pulse(RST_GPIOB);
40 rcc_periph_reset_pulse(RST_GPIOC);
41 rcc_periph_reset_pulse(RST_I2C1);
42 rcc_periph_reset_pulse(RST_USART1);
43 rcc_periph_reset_pulse(RST_USB);
44 rcc_periph_reset_pulse(RST_TIM1);
47 static void gpio_init(void)
49 // PA9 = TXD1 for debugging console
50 // PA10 = RXD1 for debugging console
51 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
52 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
54 // PC13 = BluePill LED
55 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
56 gpio_clear(GPIOC, GPIO13);
58 // PB7 = SDA for display controller
59 // PB6 = SCL for display controller
60 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO6 | GPIO7);
62 // PA8 = SFH5110 output (5V tolerant) connected to TIM1_CH1
63 gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO8);
66 static void usart_init(void)
68 usart_set_baudrate(USART1, 115200);
69 usart_set_databits(USART1, 8);
70 usart_set_stopbits(USART1, USART_STOPBITS_1);
71 usart_set_mode(USART1, USART_MODE_TX);
72 usart_set_parity(USART1, USART_PARITY_NONE);
73 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
78 /*** System ticks ***/
80 static volatile u32 ms_ticks;
82 void sys_tick_handler(void)
87 static void tick_init(void)
89 systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
90 systick_counter_enable();
91 systick_interrupt_enable();
94 static void delay_ms(uint ms)
96 u32 start_ticks = ms_ticks;
97 while (ms_ticks - start_ticks < ms)
123 static byte ctrl = 0x56;
125 static void display_update(void)
127 // debug_puts("Display update\n");
131 cmds[2] = ((disp[1] & 0x88) >> 3) | ((disp[1] & 0x44) >> 1) | ((disp[1] & 0x22) << 1) | ((disp[1] & 0x11) << 3);
133 i2c_transfer7(I2C1, 0x76/2, (byte *) cmds, sizeof(cmds), NULL, 0);
135 cmds[2] = ((disp[3] & 0x88) >> 3) | ((disp[3] & 0x44) >> 1) | ((disp[3] & 0x22) << 1) | ((disp[3] & 0x11) << 3);
137 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
139 // debug_puts("Update done\n");
142 static void display_init(void)
144 debug_puts("I2C init\n");
145 i2c_peripheral_disable(I2C1);
146 i2c_set_speed(I2C1, i2c_speed_sm_100k, rcc_apb1_frequency / 1000000);
147 i2c_peripheral_enable(I2C1);
156 static void display_test(void)
164 byte cmds[] = { 0x00, mode ? 0x77 : 0x77 };
165 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
169 byte disp[] = { 0xff, 0xff, mode ? 0xff : 0x00, mode ? 0xff : 0xff };
170 byte cmds[] = { 0x00, 0x77, 0, 0, 0, 0 };
171 cmds[2] = (disp[0] & 0xf0) | (disp[2] >> 4);
172 cmds[3] = (disp[1] & 0xf0) | (disp[3] >> 4);
173 cmds[4] = (disp[2] & 0x0f) | (disp[0] << 4);
174 cmds[5] = (disp[3] & 0x0f) | (disp[1] << 4);
175 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
181 static const byte lcd_font[] = {
200 /*** Infrared Remote Control ***/
202 static void ir_init(void)
204 debug_puts("IR init\n");
206 timer_set_prescaler(TIM1, 71); // 72 MHz / 72 = 1 MHz
207 timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
208 timer_set_period(TIM1, 65535);
209 timer_update_on_overflow(TIM1);
211 // IC1 will trigger on TI1 (TIM1_CH1) falling edge
212 timer_ic_set_input(TIM1, TIM_IC1, TIM_IC_IN_TI1);
213 // timer_ic_set_filter(TIM1, TIM_IC1, TIM_IC_OFF);
214 timer_set_oc_polarity_low(TIM1, TIM_OC1); // OC functions affect IC, too
216 // IC2 will trigger on TI1 (TIM1_CH1) rising edge
217 timer_ic_set_input(TIM1, TIM_IC2, TIM_IC_IN_TI1);
218 timer_set_oc_polarity_high(TIM1, TIM_OC2);
220 // CH3 will trigger on a break longer than 50 ms
221 timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_ACTIVE);
222 timer_set_oc_value(TIM1, TIM_OC3, 30000);
224 // Program slave controller to reset the timer on IC1
225 timer_slave_set_trigger(TIM1, TIM_SMCR_TS_TI1FP1);
226 timer_slave_set_mode(TIM1, TIM_SMCR_SMS_RM);
228 // Request interrupts
229 timer_enable_irq(TIM1, TIM_DIER_CC1IE | TIM_DIER_CC2IE | TIM_DIER_CC3IE);
230 nvic_enable_irq(NVIC_TIM1_CC_IRQ);
232 // Enable ICs and OCs
233 timer_enable_oc_output(TIM1, TIM_OC1);
234 timer_enable_oc_output(TIM1, TIM_OC2);
235 timer_enable_oc_output(TIM1, TIM_OC3);
237 timer_enable_counter(TIM1);
240 #define IR_MAX_PULSES 64
241 static u16 ir_pulses[IR_MAX_PULSES];
242 static uint ir_num_pulses;
243 static u16 ir_last_pulse;
245 void tim1_cc_isr(void)
247 if (TIM_SR(TIM1) & TIM_SR_CC1IF) {
248 TIM_SR(TIM1) &= ~TIM_SR_CC1IF;
249 u16 now = TIM_CCR1(TIM1);
251 ir_pulses[ir_num_pulses++] = ir_last_pulse;
252 ir_pulses[ir_num_pulses++] = now - ir_last_pulse;
256 if (TIM_SR(TIM1) & TIM_SR_CC2IF) {
257 TIM_SR(TIM1) &= ~TIM_SR_CC2IF;
258 ir_last_pulse = TIM_CCR2(TIM1);
260 if (TIM_SR(TIM1) & TIM_SR_CC3IF) {
261 TIM_SR(TIM1) &= ~TIM_SR_CC3IF;
263 ir_pulses[ir_num_pulses++] = ir_last_pulse;
264 ir_pulses[ir_num_pulses++] = 0xffff;
272 static usbd_device *usbd_dev;
275 STR_MANUFACTURER = 1,
280 static char usb_serial_number[13];
282 static const char *usb_strings[] = {
283 "United Computer Wizards",
288 static const struct usb_device_descriptor device = {
289 .bLength = USB_DT_DEVICE_SIZE,
290 .bDescriptorType = USB_DT_DEVICE,
292 .bDeviceClass = 0xFF,
293 .bDeviceSubClass = 0,
294 .bDeviceProtocol = 0,
295 .bMaxPacketSize0 = 64,
299 .iManufacturer = STR_MANUFACTURER,
300 .iProduct = STR_PRODUCT,
301 .iSerialNumber = STR_SERIAL,
302 .bNumConfigurations = 1,
305 static const struct usb_endpoint_descriptor endpoints[] = {{
306 // Bulk end-point for sending values to the display
307 .bLength = USB_DT_ENDPOINT_SIZE,
308 .bDescriptorType = USB_DT_ENDPOINT,
309 .bEndpointAddress = 0x01,
310 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
311 .wMaxPacketSize = 64,
315 static const struct usb_interface_descriptor iface = {
316 .bLength = USB_DT_INTERFACE_SIZE,
317 .bDescriptorType = USB_DT_INTERFACE,
318 .bInterfaceNumber = 0,
319 .bAlternateSetting = 0,
321 .bInterfaceClass = 0xFF,
322 .bInterfaceSubClass = 0,
323 .bInterfaceProtocol = 0,
325 .endpoint = endpoints,
328 static const struct usb_dfu_descriptor dfu_function = {
329 .bLength = sizeof(struct usb_dfu_descriptor),
330 .bDescriptorType = DFU_FUNCTIONAL,
331 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
332 .wDetachTimeout = 255,
333 .wTransferSize = 1024,
334 .bcdDFUVersion = 0x0100,
337 static const struct usb_interface_descriptor dfu_iface = {
338 .bLength = USB_DT_INTERFACE_SIZE,
339 .bDescriptorType = USB_DT_INTERFACE,
340 .bInterfaceNumber = 1,
341 .bAlternateSetting = 0,
343 .bInterfaceClass = 0xFE,
344 .bInterfaceSubClass = 1,
345 .bInterfaceProtocol = 1,
348 .extra = &dfu_function,
349 .extralen = sizeof(dfu_function),
352 static const struct usb_interface ifaces[] = {{
354 .altsetting = &iface,
357 .altsetting = &dfu_iface,
360 static const struct usb_config_descriptor config = {
361 .bLength = USB_DT_CONFIGURATION_SIZE,
362 .bDescriptorType = USB_DT_CONFIGURATION,
365 .bConfigurationValue = 1,
367 .bmAttributes = 0x80,
368 .bMaxPower = 50, // multiplied by 2 mA
372 static byte usb_configured;
373 static uint8_t usbd_control_buffer[64];
375 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
377 // Reset to bootloader, which implements the rest of DFU
378 debug_printf("Switching to DFU\n");
383 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
384 struct usb_setup_data *req,
385 uint8_t **buf UNUSED,
386 uint16_t *len UNUSED,
387 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
389 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
390 return USBD_REQ_NOTSUPP;
392 *complete = dfu_detach_complete;
393 return USBD_REQ_HANDLED;
396 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
398 // We received a frame from the USB host
400 uint len = usbd_ep_read_packet(dev, 0x01, buf, 8);
401 debug_printf("USB: Host sent %u bytes\n", len);
403 for (uint i=0; i<4; i++) {
405 disp[i] = lcd_font[buf[i]];
415 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
417 usbd_register_control_callback(
419 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
420 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
422 usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
426 static void reset_cb(void)
428 debug_printf("USB: Reset\n");
432 static volatile bool usb_event_pending;
434 void usb_lp_can_rx0_isr(void)
437 * We handle USB in the main loop to avoid race conditions between
438 * USB interrupts and other code. However, we need an interrupt to
439 * up the main loop from sleep.
441 * We set up only the low-priority ISR, because high-priority ISR handles
442 * only double-buffered bulk transfers and isochronous transfers.
444 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
445 usb_event_pending = 1;
448 static void usb_init(void)
450 // Simulate USB disconnect
451 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
452 gpio_clear(GPIOA, GPIO11 | GPIO12);
455 usbd_dev = usbd_init(
456 &st_usbfs_v1_usb_driver,
460 ARRAY_SIZE(usb_strings),
462 sizeof(usbd_control_buffer)
464 usbd_register_reset_callback(usbd_dev, reset_cb);
465 usbd_register_set_config_callback(usbd_dev, set_config_cb);
466 usb_event_pending = 1;
478 desig_get_unique_id_as_dfu(usb_serial_number);
480 debug_printf("Hello, world!\n");
489 if (ms_ticks - last_blink >= 500) {
491 last_blink = ms_ticks;
495 if (usb_event_pending) {
497 usb_event_pending = 0;
498 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
499 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
502 static u16 pulses[IR_MAX_PULSES];
505 cm_disable_interrupts();
507 memcpy(pulses, ir_pulses, 2*np);
509 cm_enable_interrupts();
513 for (uint i=0; i < np; i++)
514 debug_printf(" %u", pulses[i]);
518 wait_for_interrupt();