]> mj.ucw.cz Git - home-hw.git/blob - ir-probe/test.c
ir-probe: Random bug fixes
[home-hw.git] / ir-probe / test.c
1 #include "util.h"
2
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>
9
10 static void clock_setup(void)
11 {
12         rcc_clock_setup_in_hse_8mhz_out_72mhz();
13
14         rcc_periph_clock_enable(RCC_GPIOA);
15         rcc_periph_clock_enable(RCC_GPIOB);
16         rcc_periph_clock_enable(RCC_GPIOC);
17         rcc_periph_clock_enable(RCC_USART1);
18
19         rcc_periph_reset_pulse(RST_GPIOA);
20         rcc_periph_reset_pulse(RST_GPIOB);
21         rcc_periph_reset_pulse(RST_GPIOC);
22         rcc_periph_reset_pulse(RST_USART1);
23 }
24
25 static void gpio_setup(void)
26 {
27         // PC13 = BluePill LED
28         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
29         gpio_clear(GPIOC, GPIO13);
30
31         // PB9 = SFH5110 output (5V tolerant)
32         gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO9);
33 }
34
35 static void usart_setup(void)
36 {
37         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO9);
38
39         usart_set_baudrate(USART1, 115200);
40         usart_set_databits(USART1, 8);
41         usart_set_stopbits(USART1, USART_STOPBITS_1);
42         usart_set_mode(USART1, USART_MODE_TX_RX);
43         usart_set_parity(USART1, USART_PARITY_NONE);
44         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
45
46         usart_enable(USART1);
47 }
48
49 static void tick_setup(void)
50 {
51         systick_set_reload(0xffffff);
52         systick_counter_enable();
53         systick_interrupt_disable();
54         systick_clear();
55 }
56
57 static u16 get_bit(void)
58 {
59         return !gpio_get(GPIOB, GPIO9);
60 }
61
62 #define MAX_SAMPLES 1024
63 u32 samples[MAX_SAMPLES];
64
65 int main(void)
66 {
67         clock_setup();
68         gpio_setup();
69         usart_setup();
70         tick_setup();
71
72         debug_puts("\n\n### Infrared Remote Control receiver ###\n\n");
73
74         for (;;) {
75                 gpio_set(GPIOC, GPIO13);
76
77                 if (get_bit()) {
78                         debug_puts("Waiting for silence\n");
79                         while (get_bit())
80                                 ;
81                 }
82                 debug_puts("Ready...");
83
84                 u16 last = 0;
85                 uint nsamp = 0;
86                 u32 start;
87
88                 do {
89                         start = systick_get_value();
90                         last = get_bit();
91                 } while (!last);
92
93                 gpio_clear(GPIOC, GPIO13);
94
95                 for (;;) {
96                         u32 now;
97                         u16 curr;
98                         uint len;
99                         do {
100                                 now = systick_get_value();
101                                 len = (start - now) & 0xffffff;
102                                 if (len > 5000000) {
103                                         samples[nsamp++] = len;
104                                         goto timeout;
105                                 }
106                                 curr = get_bit();
107                         } while (curr == last);
108                         samples[nsamp++] = len;
109                         if (nsamp >= MAX_SAMPLES)
110                                 break;
111                         start = now;
112                         last = curr;
113                 }
114
115         timeout:
116                 for (uint i=0; i<nsamp; i++) {
117                         debug_putc(i ? ' ' : '\n');
118                         debug_printf("%u", (samples[i] + CPU_CLOCK_MHZ - 1) / CPU_CLOCK_MHZ);   // in μs
119                 }
120                 debug_putc('\n');
121         }
122
123         return 0;
124 }