]> mj.ucw.cz Git - home-hw.git/blob - test-opencm3/test.c
e49bce74eeccaeec3a6875413ed42609e8f24c01
[home-hw.git] / test-opencm3 / test.c
1 #include "util.h"
2 #include "ds18b20.h"
3
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_GPIOC);
16         rcc_periph_clock_enable(RCC_USART1);
17         rcc_periph_clock_enable(RCC_TIM3);
18         rcc_periph_clock_enable(RCC_DMA1);
19 }
20
21 static void gpio_setup(void)
22 {
23         // PC13 = BluePill LED
24         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
25         gpio_clear(GPIOC, GPIO13);
26
27         // PC14 = bypass LED*
28         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO14);
29         gpio_set(GPIOC, GPIO14);
30
31         // PC15 = bypass opto-coupler
32         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO15);
33         gpio_clear(GPIOC, GPIO15);
34 }
35
36 static volatile u32 ms_ticks;
37
38 void sys_tick_handler(void)
39 {
40         ms_ticks++;
41 }
42
43 static void tick_setup(void)
44 {
45         systick_set_frequency(1000, 72000000);
46         systick_counter_enable();
47         systick_interrupt_enable();
48 }
49
50 static void delay_ms(uint ms)
51 {
52         u32 start_ticks = ms_ticks;
53         while (ms_ticks - start_ticks < ms)
54                 ;
55 }
56
57 static void usart_setup(void)
58 {
59         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
60
61         usart_set_baudrate(USART1, 115200);
62         usart_set_databits(USART1, 8);
63         usart_set_stopbits(USART1, USART_STOPBITS_1);
64         usart_set_mode(USART1, USART_MODE_TX_RX);
65         usart_set_parity(USART1, USART_PARITY_NONE);
66         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
67
68         usart_enable(USART1);
69 }
70
71 static bool bypass_active;
72
73 static void show_temperature(void)
74 {
75         debug_putc('#');
76         for (uint i=0; ds_sensors[i].address[0]; i++) {
77                 debug_putc(' ');
78                 int t = ds_sensors[i].current_temp;
79                 if (t == DS_TEMP_UNKNOWN)
80                         debug_puts("---.---");
81                 else
82                         debug_printf("%3d.%03d", t / 1000, t % 1000);
83         }
84         debug_putc(' ');
85         debug_putc('0' + bypass_active);
86         debug_puts("\r\n");
87 }
88
89 int main(void)
90 {
91         clock_setup();
92         gpio_setup();
93         tick_setup();
94         usart_setup();
95
96         ds_init();
97
98         byte cycles = 0;
99         for (;;) {
100                 gpio_toggle(GPIOC, GPIO13);
101                 delay_ms(100);
102                 ds_step();
103                 if (usart_get_flag(USART1, USART_SR_RXNE)) {
104                         uint ch = usart_recv(USART1);
105                         if (ch == 'B') {
106                                 bypass_active = 1;
107                                 gpio_set(GPIOC, GPIO15);        // opto-coupler
108                                 gpio_clear(GPIOC, GPIO14);      // LED
109                         } else if (ch == 'b') {
110                                 bypass_active = 0;
111                                 gpio_clear(GPIOC, GPIO15);      // opto-coupler
112                                 gpio_set(GPIOC, GPIO14);        // LED
113                         }
114                 }
115                 if (cycles++ >= 50) {
116                         cycles = 0;
117                         show_temperature();
118                 }
119         }
120
121         return 0;
122 }