]> mj.ucw.cz Git - home-hw.git/blob - test-bsb/test.c
f923ffc38f810a60f47c37f8b07b059608d89566
[home-hw.git] / test-bsb / test.c
1 #include "util.h"
2
3 #include <libopencm3/cm3/cortex.h>
4 #include <libopencm3/cm3/nvic.h>
5 #include <libopencm3/cm3/scb.h>
6 #include <libopencm3/cm3/systick.h>
7 #include <libopencm3/stm32/rcc.h>
8 #include <libopencm3/stm32/gpio.h>
9 #include <libopencm3/stm32/usart.h>
10
11 static void clock_setup(void)
12 {
13         rcc_clock_setup_in_hse_8mhz_out_72mhz();
14
15         rcc_periph_clock_enable(RCC_GPIOB);
16         rcc_periph_clock_enable(RCC_GPIOC);
17         rcc_periph_clock_enable(RCC_USART1);
18         rcc_periph_clock_enable(RCC_USART3);
19
20         rcc_periph_reset_pulse(RST_GPIOB);
21         rcc_periph_reset_pulse(RST_GPIOC);
22         rcc_periph_reset_pulse(RST_USART1);
23         rcc_periph_reset_pulse(RST_USART3);
24 }
25
26 static void gpio_setup(void)
27 {
28         // PC13 = BluePill LED
29         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
30         gpio_clear(GPIOC, GPIO13);
31 }
32
33 static volatile u32 ms_ticks;
34
35 void sys_tick_handler(void)
36 {
37         ms_ticks++;
38 }
39
40 static void tick_setup(void)
41 {
42         systick_set_frequency(1000, 72000000);
43         systick_counter_enable();
44         systick_interrupt_enable();
45 }
46
47 static void delay_ms(uint ms)
48 {
49         u32 start_ticks = ms_ticks;
50         while (ms_ticks - start_ticks < ms)
51                 ;
52 }
53
54 static void usart_setup(void)
55 {
56         // Debugging USART
57         gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
58         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
59
60         usart_set_baudrate(USART1, 115200);
61         usart_set_databits(USART1, 8);
62         usart_set_stopbits(USART1, USART_STOPBITS_1);
63         usart_set_mode(USART1, USART_MODE_TX);
64         usart_set_parity(USART1, USART_PARITY_NONE);
65         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
66
67         usart_enable(USART1);
68
69         // BSB USART
70         gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART3_RX);
71         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART3_TX);
72
73         usart_set_baudrate(USART3, 4800);
74         usart_set_databits(USART3, 8);
75         usart_set_stopbits(USART3, USART_STOPBITS_1);
76         usart_set_mode(USART3, USART_MODE_RX);
77         usart_set_parity(USART3, USART_PARITY_ODD);
78         usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE);
79
80         usart_enable(USART3);
81 }
82
83 int main(void)
84 {
85         clock_setup();
86         gpio_setup();
87         usart_setup();
88         tick_setup();
89         // This is needed when programming via serial boot loader, harmless otherwise.
90         SCB_VTOR = 0x08000000;
91         cm_enable_interrupts();
92
93         u32 start_ticks = ms_ticks;
94         for (;;) {
95                 if (USART_SR(USART3) & USART_SR_RXNE) {
96                         uint x = usart_recv(USART3);
97                         debug_printf("%02x ", x);
98                 }
99                 if (ms_ticks - start_ticks >= 100) {
100                         start_ticks = ms_ticks;
101                         debug_led_toggle();
102                         debug_putc(' ');
103                 }
104         }
105
106         return 0;
107 }