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 static void ep82_send(u32 key_code);
26 /*** Hardware init ***/
28 static void clock_init(void)
30 rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
32 rcc_periph_clock_enable(RCC_GPIOA);
33 rcc_periph_clock_enable(RCC_GPIOB);
34 rcc_periph_clock_enable(RCC_GPIOC);
35 rcc_periph_clock_enable(RCC_I2C1);
36 rcc_periph_clock_enable(RCC_USART1);
37 rcc_periph_clock_enable(RCC_USB);
38 rcc_periph_clock_enable(RCC_TIM1);
40 rcc_periph_reset_pulse(RST_GPIOA);
41 rcc_periph_reset_pulse(RST_GPIOB);
42 rcc_periph_reset_pulse(RST_GPIOC);
43 rcc_periph_reset_pulse(RST_I2C1);
44 rcc_periph_reset_pulse(RST_USART1);
45 rcc_periph_reset_pulse(RST_USB);
46 rcc_periph_reset_pulse(RST_TIM1);
49 static void gpio_init(void)
51 // PA9 = TXD1 for debugging console
52 // PA10 = RXD1 for debugging console
53 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
54 gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO10);
56 // PC13 = BluePill LED
57 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
58 gpio_clear(GPIOC, GPIO13);
60 // PB7 = SDA for display controller
61 // PB6 = SCL for display controller
62 gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO6 | GPIO7);
64 // PA8 = SFH5110 output (5V tolerant) connected to TIM1_CH1
65 gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO8);
68 static void usart_init(void)
70 usart_set_baudrate(USART1, 115200);
71 usart_set_databits(USART1, 8);
72 usart_set_stopbits(USART1, USART_STOPBITS_1);
73 usart_set_mode(USART1, USART_MODE_TX);
74 usart_set_parity(USART1, USART_PARITY_NONE);
75 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
80 /*** System ticks ***/
82 static volatile u32 ms_ticks;
84 void sys_tick_handler(void)
89 static void tick_init(void)
91 systick_set_frequency(1000, CPU_CLOCK_MHZ * 1000000);
92 systick_counter_enable();
93 systick_interrupt_enable();
96 static void delay_ms(uint ms)
98 u32 start_ticks = ms_ticks;
99 while (ms_ticks - start_ticks < ms)
125 static byte ctrl = 0x56;
127 static const byte disp_font[] = {
146 static void display_update(void)
148 // debug_puts("Display update\n");
153 cmds[2] = ((disp[1] & 0x88) >> 3) | ((disp[1] & 0x44) >> 1) | ((disp[1] & 0x22) << 1) | ((disp[1] & 0x11) << 3);
155 i2c_transfer7(I2C1, 0x76/2, (byte *) cmds, sizeof(cmds), NULL, 0);
157 cmds[2] = ((disp[3] & 0x88) >> 3) | ((disp[3] & 0x44) >> 1) | ((disp[3] & 0x22) << 1) | ((disp[3] & 0x11) << 3);
159 i2c_transfer7(I2C1, 0x70/2, (byte *) cmds, sizeof(cmds), NULL, 0);
161 // debug_puts("Update done\n");
164 static void display_init(void)
166 debug_puts("I2C init\n");
167 i2c_peripheral_disable(I2C1);
168 i2c_set_speed(I2C1, i2c_speed_sm_100k, rcc_apb1_frequency / 1000000);
169 i2c_peripheral_enable(I2C1);
178 /*** Infrared Remote Control ***/
180 static void ir_init(void)
182 debug_puts("IR init\n");
184 // TIM1 will measure pulses and spaces between them with 1μs resolution
185 timer_set_prescaler(TIM1, 71); // 72 MHz / 72 = 1 MHz
186 timer_set_mode(TIM1, TIM_CR1_CKD_CK_INT, TIM_CR1_CMS_EDGE, TIM_CR1_DIR_UP);
187 timer_set_period(TIM1, 65535);
188 timer_update_on_overflow(TIM1);
190 // IC1 will trigger on TI1 (TIM1_CH1) falling edge
191 timer_ic_set_input(TIM1, TIM_IC1, TIM_IC_IN_TI1);
192 // timer_ic_set_filter(TIM1, TIM_IC1, TIM_IC_OFF);
193 timer_set_oc_polarity_low(TIM1, TIM_OC1); // OC functions affect IC, too
195 // IC2 will trigger on TI1 (TIM1_CH1) rising edge
196 timer_ic_set_input(TIM1, TIM_IC2, TIM_IC_IN_TI1);
197 timer_set_oc_polarity_high(TIM1, TIM_OC2);
199 // OC3 will trigger on a break longer than 50 ms
200 timer_set_oc_mode(TIM1, TIM_OC3, TIM_OCM_ACTIVE);
201 timer_set_oc_value(TIM1, TIM_OC3, 30000);
203 // Program slave controller to reset the timer on IC1
204 timer_slave_set_trigger(TIM1, TIM_SMCR_TS_TI1FP1);
205 timer_slave_set_mode(TIM1, TIM_SMCR_SMS_RM);
207 // Request interrupts
208 timer_enable_irq(TIM1, TIM_DIER_CC1IE | TIM_DIER_CC2IE | TIM_DIER_CC3IE);
209 nvic_enable_irq(NVIC_TIM1_CC_IRQ);
211 // Enable ICs and OCs
212 timer_enable_oc_output(TIM1, TIM_OC1);
213 timer_enable_oc_output(TIM1, TIM_OC2);
214 timer_enable_oc_output(TIM1, TIM_OC3);
216 timer_enable_counter(TIM1);
219 // Circular queue of pulse durations
220 #define IR_MAX_PULSES 32
221 static u32 ir_pulses[IR_MAX_PULSES]; // Top 16 bits = mark, bottom 16 bits = space
222 static u16 ir_last_pulse;
223 static uint ir_pulses_rx, ir_pulses_tx;
225 #define IR_INF 0xffff
227 #define IR_MARK(x) (uint)((x) >> 16)
228 #define IR_SPACE(x) (uint)((x) & 0xffff)
230 static inline bool between(uint x, uint min, uint max)
232 return x >= min && x <= max;
235 static void ir_record_pulse(uint mark, uint space)
237 uint i = ir_pulses_tx;
238 ir_pulses_tx = (i + 1) % IR_MAX_PULSES;
239 if (ir_pulses_tx != ir_pulses_rx) {
240 ir_pulses[i] = (mark << 16) | space;
243 ir_pulses[i] = (IR_INF << 16) | IR_INF;
247 void tim1_cc_isr(void)
249 if (TIM_SR(TIM1) & TIM_SR_CC1IF) {
250 TIM_SR(TIM1) &= ~TIM_SR_CC1IF;
251 u16 now = TIM_CCR1(TIM1);
253 ir_record_pulse(ir_last_pulse, now - ir_last_pulse);
257 if (TIM_SR(TIM1) & TIM_SR_CC2IF) {
258 TIM_SR(TIM1) &= ~TIM_SR_CC2IF;
259 ir_last_pulse = TIM_CCR2(TIM1);
261 if (TIM_SR(TIM1) & TIM_SR_CC3IF) {
262 TIM_SR(TIM1) &= ~TIM_SR_CC3IF;
264 ir_record_pulse(ir_last_pulse, IR_INF);
270 static u32 ir_get_pulse(void)
274 cm_disable_interrupts();
275 if (ir_pulses_rx != ir_pulses_tx) {
276 out = ir_pulses[ir_pulses_rx];
277 ir_pulses_rx = (ir_pulses_rx + 1) % IR_MAX_PULSES;
279 cm_enable_interrupts();
283 // Decoder for Onkyo RC-748S
285 static u32 ir_blink_start;
287 static void ir_decode(void)
289 u32 pulse = ir_get_pulse();
293 uint mark = IR_MARK(pulse);
294 uint space = IR_SPACE(pulse);
297 debug_printf("IR: %d %d\n", mark, space);
305 // debug_printf("IR(%d): %d %d\n", ir_bits, mark, space);
307 if (space == IR_INF) {
309 } else if (ir_bits == IR_ERR) {
311 } else if (ir_bits == 0) {
313 if (between(mark, 8900, 9200)) {
314 if (between(space, 4200, 4600)) {
317 } else if (between(space, 2000, 2300)) {
318 debug_printf("IR: => REP\n");
323 if (between(mark, 500, 700)) {
325 if (between(space, 400, 700)) {
327 } else if (between(space, 1500, 1800)) {
329 ir_code |= 1U << (33 - ir_bits);
334 debug_printf("IR: => %08x\n", (uint)ir_code);
336 ir_blink_start = ms_ticks;
349 static usbd_device *usbd_dev;
352 STR_MANUFACTURER = 1,
357 static char usb_serial_number[13];
359 static const char *usb_strings[] = {
360 "United Computer Wizards",
365 static const struct usb_device_descriptor device = {
366 .bLength = USB_DT_DEVICE_SIZE,
367 .bDescriptorType = USB_DT_DEVICE,
369 .bDeviceClass = 0xFF,
370 .bDeviceSubClass = 0,
371 .bDeviceProtocol = 0,
372 .bMaxPacketSize0 = 64,
376 .iManufacturer = STR_MANUFACTURER,
377 .iProduct = STR_PRODUCT,
378 .iSerialNumber = STR_SERIAL,
379 .bNumConfigurations = 1,
382 static const struct usb_endpoint_descriptor endpoints[] = {{
383 // Bulk end-point for sending values to the display
384 .bLength = USB_DT_ENDPOINT_SIZE,
385 .bDescriptorType = USB_DT_ENDPOINT,
386 .bEndpointAddress = 0x01,
387 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
388 .wMaxPacketSize = 64,
391 // Bulk end-point for receiving remote control keys
392 .bLength = USB_DT_ENDPOINT_SIZE,
393 .bDescriptorType = USB_DT_ENDPOINT,
394 .bEndpointAddress = 0x82,
395 .bmAttributes = USB_ENDPOINT_ATTR_BULK,
400 static const struct usb_interface_descriptor iface = {
401 .bLength = USB_DT_INTERFACE_SIZE,
402 .bDescriptorType = USB_DT_INTERFACE,
403 .bInterfaceNumber = 0,
404 .bAlternateSetting = 0,
406 .bInterfaceClass = 0xFF,
407 .bInterfaceSubClass = 0,
408 .bInterfaceProtocol = 0,
410 .endpoint = endpoints,
413 static const struct usb_dfu_descriptor dfu_function = {
414 .bLength = sizeof(struct usb_dfu_descriptor),
415 .bDescriptorType = DFU_FUNCTIONAL,
416 .bmAttributes = USB_DFU_CAN_DOWNLOAD | USB_DFU_WILL_DETACH,
417 .wDetachTimeout = 255,
418 .wTransferSize = 1024,
419 .bcdDFUVersion = 0x0100,
422 static const struct usb_interface_descriptor dfu_iface = {
423 .bLength = USB_DT_INTERFACE_SIZE,
424 .bDescriptorType = USB_DT_INTERFACE,
425 .bInterfaceNumber = 1,
426 .bAlternateSetting = 0,
428 .bInterfaceClass = 0xFE,
429 .bInterfaceSubClass = 1,
430 .bInterfaceProtocol = 1,
433 .extra = &dfu_function,
434 .extralen = sizeof(dfu_function),
437 static const struct usb_interface ifaces[] = {{
439 .altsetting = &iface,
442 .altsetting = &dfu_iface,
445 static const struct usb_config_descriptor config = {
446 .bLength = USB_DT_CONFIGURATION_SIZE,
447 .bDescriptorType = USB_DT_CONFIGURATION,
450 .bConfigurationValue = 1,
452 .bmAttributes = 0x80,
453 .bMaxPower = 50, // multiplied by 2 mA
457 static bool usb_configured;
458 static uint8_t usbd_control_buffer[64];
460 static bool usb_tx_in_flight;
461 static byte ep82_tx_buffer[4];
463 static byte disp_alive;
465 static void dfu_detach_complete(usbd_device *dev UNUSED, struct usb_setup_data *req UNUSED)
467 // Reset to bootloader, which implements the rest of DFU
468 debug_printf("Switching to DFU\n");
473 static enum usbd_request_return_codes dfu_control_cb(usbd_device *dev UNUSED,
474 struct usb_setup_data *req,
475 uint8_t **buf UNUSED,
476 uint16_t *len UNUSED,
477 void (**complete)(usbd_device *dev, struct usb_setup_data *req))
479 if (req->bmRequestType != 0x21 || req->bRequest != DFU_DETACH)
480 return USBD_REQ_NOTSUPP;
482 *complete = dfu_detach_complete;
483 return USBD_REQ_HANDLED;
486 static void ep01_cb(usbd_device *dev, uint8_t ep UNUSED)
488 // We received a frame from the USB host
490 uint len = usbd_ep_read_packet(dev, 0x01, buf, 8);
491 debug_printf("USB: Host sent %u bytes\n", len);
493 for (uint i=0; i<4; i++) {
496 disp[i] |= disp_font[buf[i]];
506 static void ep82_send(u32 key_code)
508 if (usb_tx_in_flight) {
509 debug_printf("USB: Send overrun!\n");
513 debug_printf("USB: Sending key to host\n");
514 put_u32_be(ep82_tx_buffer, key_code);
515 usbd_ep_write_packet(usbd_dev, 0x82, ep82_tx_buffer, 4);
516 usb_tx_in_flight = true;
519 static void ep82_cb(usbd_device *dev UNUSED, uint8_t ep UNUSED)
521 // We completed sending a frame to the USB host
522 usb_tx_in_flight = false;
523 debug_printf("USB: Key sending complete\n");
526 static void set_config_cb(usbd_device *dev, uint16_t wValue UNUSED)
528 usbd_register_control_callback(
530 USB_REQ_TYPE_CLASS | USB_REQ_TYPE_INTERFACE,
531 USB_REQ_TYPE_TYPE | USB_REQ_TYPE_RECIPIENT,
533 usbd_ep_setup(dev, 0x01, USB_ENDPOINT_ATTR_BULK, 64, ep01_cb);
534 usbd_ep_setup(dev, 0x82, USB_ENDPOINT_ATTR_BULK, 4, ep82_cb);
535 usb_configured = true;
538 static void reset_cb(void)
540 debug_printf("USB: Reset\n");
541 usb_configured = false;
544 static volatile bool usb_event_pending;
546 void usb_lp_can_rx0_isr(void)
549 * We handle USB in the main loop to avoid race conditions between
550 * USB interrupts and other code. However, we need an interrupt to
551 * up the main loop from sleep.
553 * We set up only the low-priority ISR, because high-priority ISR handles
554 * only double-buffered bulk transfers and isochronous transfers.
556 nvic_disable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
557 usb_event_pending = 1;
560 static void usb_init(void)
562 // Simulate USB disconnect
563 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO11 | GPIO12);
564 gpio_clear(GPIOA, GPIO11 | GPIO12);
567 usbd_dev = usbd_init(
568 &st_usbfs_v1_usb_driver,
572 ARRAY_SIZE(usb_strings),
574 sizeof(usbd_control_buffer)
576 usbd_register_reset_callback(usbd_dev, reset_cb);
577 usbd_register_set_config_callback(usbd_dev, set_config_cb);
578 usb_event_pending = 1;
590 desig_get_unique_id_as_dfu(usb_serial_number);
592 debug_printf("Hello, world!\n");
601 if (ms_ticks - last_blink >= 500) {
603 last_blink = ms_ticks;
606 disp[0] = (disp[0] & 0x01) | 0x10;
607 disp[1] = (disp[1] & 0x01) | 0x10;
608 disp[2] = (disp[2] & 0x01) | 0x10;
609 disp[3] = (disp[3] & 0x01) | 0x10;
615 if ((disp[3] & 0x01) && ms_ticks - ir_blink_start >= 100) {
622 if (usb_event_pending) {
624 usb_event_pending = 0;
625 nvic_clear_pending_irq(NVIC_USB_LP_CAN_RX0_IRQ);
626 nvic_enable_irq(NVIC_USB_LP_CAN_RX0_IRQ);
629 wait_for_interrupt();