]> mj.ucw.cz Git - home-hw.git/blob - test-opencm3/test.c
Test: Experimental air temperature readings
[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         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
24 }
25
26 static volatile u32 ms_ticks;
27
28 void sys_tick_handler(void)
29 {
30         ms_ticks++;
31 }
32
33 static void tick_setup(void)
34 {
35         systick_set_frequency(1000, 72000000);
36         systick_counter_enable();
37         systick_interrupt_enable();
38 }
39
40 static void delay_ms(uint ms)
41 {
42         u32 start_ticks = ms_ticks;
43         while (ms_ticks - start_ticks < ms)
44                 ;
45 }
46
47 static void usart_setup(void)
48 {
49         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
50
51         usart_set_baudrate(USART1, 115200);
52         usart_set_databits(USART1, 8);
53         usart_set_stopbits(USART1, USART_STOPBITS_1);
54         usart_set_mode(USART1, USART_MODE_TX);
55         usart_set_parity(USART1, USART_PARITY_NONE);
56         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
57
58         usart_enable(USART1);
59 }
60
61 static void show_temperature(void)
62 {
63         debug_putc('#');
64         for (uint i=0; ds_sensors[i].address[0]; i++) {
65                 debug_putc(' ');
66                 int t = ds_sensors[i].current_temp;
67                 if (t == DS_TEMP_UNKNOWN)
68                         debug_puts("---.---");
69                 else
70                         debug_printf("%3d.%03d", t / 1000, t % 1000);
71         }
72         debug_puts("\r\n");
73 }
74
75 int main(void)
76 {
77         clock_setup();
78         gpio_setup();
79         tick_setup();
80         usart_setup();
81
82         ds_init();
83
84         byte cycles = 0;
85         for (;;) {
86                 gpio_toggle(GPIOC, GPIO13);
87                 delay_ms(100);
88                 ds_step();
89                 if (cycles++ >= 50) {
90                         cycles = 0;
91                         show_temperature();
92                 }
93         }
94
95         return 0;
96 }