]> mj.ucw.cz Git - home-hw.git/blob - ir-probe/test.c
DS18B20: Better timer magic
[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_GPIOB);
15         rcc_periph_clock_enable(RCC_GPIOC);
16         rcc_periph_clock_enable(RCC_USART1);
17
18         rcc_periph_reset_pulse(RST_GPIOB);
19         rcc_periph_reset_pulse(RST_GPIOC);
20         rcc_periph_reset_pulse(RST_USART1);
21 }
22
23 static void gpio_setup(void)
24 {
25         // PC13 = BluePill LED
26         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
27         gpio_clear(GPIOC, GPIO13);
28
29         // PB9 = SFH5110 output (5V tolerant)
30         gpio_set_mode(GPIOC, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO9);
31 }
32
33 static void usart_setup(void)
34 {
35         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
36
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);
43
44         usart_enable(USART1);
45 }
46
47 static void tick_setup(void)
48 {
49         systick_set_reload(0xffffff);
50         systick_counter_enable();
51         systick_interrupt_disable();
52         systick_clear();
53 }
54
55 static u16 get_bit(void)
56 {
57         return !gpio_get(GPIOB, GPIO9);
58 }
59
60 #define MAX_SAMPLES 1024
61 u32 samples[MAX_SAMPLES];
62
63 int main(void)
64 {
65         clock_setup();
66         gpio_setup();
67         usart_setup();
68         tick_setup();
69
70         debug_puts("\n\n### Infrared Remote Control receiver ###\n\n");
71
72         for (;;) {
73                 gpio_set(GPIOC, GPIO13);
74
75                 if (get_bit()) {
76                         debug_puts("Waiting for silence\n");
77                         while (get_bit())
78                                 ;
79                 }
80                 debug_puts("Ready...");
81
82                 u16 last = 0;
83                 uint nsamp = 0;
84                 u32 start;
85
86                 do {
87                         start = systick_get_value();
88                         last = get_bit();
89                 } while (!last);
90
91                 gpio_clear(GPIOC, GPIO13);
92
93                 for (;;) {
94                         u32 now;
95                         u16 curr;
96                         uint len;
97                         do {
98                                 now = systick_get_value();
99                                 len = (start - now) & 0xffffff;
100                                 if (len > 5000000) {
101                                         samples[nsamp++] = len;
102                                         goto timeout;
103                                 }
104                                 curr = get_bit();
105                         } while (curr == last);
106                         samples[nsamp++] = len;
107                         if (nsamp >= MAX_SAMPLES)
108                                 break;
109                         start = now;
110                         last = curr;
111                 }
112
113         timeout:
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
117                 }
118                 debug_putc('\n');
119         }
120
121         return 0;
122 }