3 #include <libopencm3/cm3/cortex.h>
4 #include <libopencm3/cm3/nvic.h>
5 #include <libopencm3/cm3/systick.h>
6 #include <libopencm3/stm32/rcc.h>
7 #include <libopencm3/stm32/gpio.h>
8 #include <libopencm3/stm32/usart.h>
10 static void clock_setup(void)
12 rcc_clock_setup_in_hse_8mhz_out_72mhz();
14 rcc_periph_clock_enable(RCC_GPIOB);
15 rcc_periph_clock_enable(RCC_GPIOC);
16 rcc_periph_clock_enable(RCC_USART1);
18 rcc_periph_reset_pulse(RST_GPIOB);
19 rcc_periph_reset_pulse(RST_GPIOC);
20 rcc_periph_reset_pulse(RST_USART1);
23 static void gpio_setup(void)
25 // PC13 = BluePill LED
26 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
27 gpio_clear(GPIOC, GPIO13);
29 // PB9 = SFH5110 output (5V tolerant)
30 gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO9);
33 static void usart_setup(void)
35 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
37 usart_set_baudrate(USART1, 115200);
38 usart_set_databits(USART1, 8);
39 usart_set_stopbits(USART1, USART_STOPBITS_1);
40 usart_set_mode(USART1, USART_MODE_TX_RX);
41 usart_set_parity(USART1, USART_PARITY_NONE);
42 usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
47 static void tick_setup(void)
49 systick_set_reload(0xffffff);
50 systick_counter_enable();
51 systick_interrupt_disable();
55 static u16 get_bit(void)
57 return !gpio_get(GPIOB, GPIO9);
60 #define MAX_SAMPLES 1024
61 u32 samples[MAX_SAMPLES];
70 debug_puts("\n\n### Infrared Remote Control receiver ###\n\n");
73 gpio_set(GPIOC, GPIO13);
76 debug_puts("Waiting for silence\n");
80 debug_puts("Ready...");
87 start = systick_get_value();
91 gpio_clear(GPIOC, GPIO13);
98 now = systick_get_value();
99 len = (start - now) & 0xffffff;
101 samples[nsamp++] = len;
105 } while (curr == last);
106 samples[nsamp++] = len;
107 if (nsamp >= MAX_SAMPLES)
114 for (uint i=0; i<nsamp; i++) {
115 debug_putc(i ? ' ' : '\n');
116 debug_printf("%u", (samples[i] + CPU_CLOCK_MHZ - 1) / CPU_CLOCK_MHZ); // in μs