]> mj.ucw.cz Git - home-hw.git/blob - test.c
345670e4536502bb86143dae5997de445bae193b
[home-hw.git] / 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_GPIOA);
16         rcc_periph_clock_enable(RCC_GPIOB);
17         rcc_periph_clock_enable(RCC_GPIOC);
18         rcc_periph_clock_enable(RCC_USART1);
19         rcc_periph_clock_enable(RCC_USART3);
20
21         rcc_periph_reset_pulse(RST_GPIOA);
22         rcc_periph_reset_pulse(RST_GPIOB);
23         rcc_periph_reset_pulse(RST_GPIOC);
24         rcc_periph_reset_pulse(RST_USART1);
25         rcc_periph_reset_pulse(RST_USART3);
26 }
27
28 static void gpio_setup(void)
29 {
30         // PC13 = BluePill LED
31         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
32         gpio_clear(GPIOC, GPIO13);
33
34         // PB0 = yellow LED*, PB1 = green LED*
35         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_OPENDRAIN, GPIO0 | GPIO1);
36         gpio_set(GPIOB, GPIO0 | GPIO1);
37 }
38
39 static volatile u32 ms_ticks;
40
41 void sys_tick_handler(void)
42 {
43         ms_ticks++;
44 }
45
46 static void tick_setup(void)
47 {
48         systick_set_frequency(1000, 72000000);
49         systick_counter_enable();
50         systick_interrupt_enable();
51 }
52
53 #if 0
54 static void delay_ms(uint ms)
55 {
56         u32 start_ticks = ms_ticks;
57         while (ms_ticks - start_ticks < ms)
58                 ;
59 }
60 #endif
61
62 static void usart_setup(void)
63 {
64         // Debugging USART
65         gpio_set_mode(GPIOA, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART1_RX);
66         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
67
68         usart_set_baudrate(USART1, 115200);
69         usart_set_databits(USART1, 8);
70         usart_set_stopbits(USART1, USART_STOPBITS_1);
71         // usart_set_mode(USART1, USART_MODE_TX);
72         usart_set_mode(USART1, USART_MODE_TX_RX);
73         usart_set_parity(USART1, USART_PARITY_NONE);
74         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
75
76         usart_enable(USART1);
77
78         // BSB USART
79         gpio_set_mode(GPIOB, GPIO_MODE_INPUT, GPIO_CNF_INPUT_FLOAT, GPIO_USART3_RX);
80         gpio_set_mode(GPIOB, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_OPENDRAIN, GPIO_USART3_TX);
81
82         usart_set_baudrate(USART3, 4800);
83         usart_set_databits(USART3, 9);
84         usart_set_stopbits(USART3, USART_STOPBITS_1);
85         usart_set_mode(USART3, USART_MODE_TX_RX);
86         usart_set_parity(USART3, USART_PARITY_ODD);
87         usart_set_flow_control(USART3, USART_FLOWCONTROL_NONE);
88
89         usart_enable(USART3);
90 }
91
92 static const byte send_pkt[] = {
93         0xdc, 0xc2, 0x00, 0x0b, 0x06, 0x3d, 0x2e, 0x11, 0x25, 0x40, 0x78,
94 };
95
96 int main(void)
97 {
98         clock_setup();
99         gpio_setup();
100         usart_setup();
101         tick_setup();
102         // This is needed when programming via serial boot loader, harmless otherwise.
103         SCB_VTOR = 0x08000000;
104         cm_enable_interrupts();
105
106         debug_printf("Hello, kitty!\n");
107
108         u32 last_ticks = 0;
109         u32 led_ticks = ms_ticks;
110         uint tx_pos = 0;
111         for (;;) {
112                 if (USART_SR(USART1) & USART_SR_RXNE) {
113                         uint c = usart_recv(USART1) & 0xff;
114                         if (c == '#')
115                                 tx_pos = 1;
116                 }
117                 if (USART_SR(USART3) & USART_SR_RXNE) {
118                         uint x = (usart_recv(USART3) & 0xff) ^ 0xff;
119                         debug_printf("%02x ", x);
120                         last_ticks = ms_ticks;
121                 }
122                 if (tx_pos && (USART_SR(USART3) & USART_SR_TXE)) {
123                         if (tx_pos > sizeof(send_pkt)) {
124                                 tx_pos = 0;
125                                 debug_putc('.');
126                         } else {
127                                 uint x = send_pkt[tx_pos++ - 1];
128                                 usart_send(USART3, x ^ 0xff);
129                                 debug_putc('>');
130                         }
131                 }
132                 if (last_ticks && ms_ticks - last_ticks >= 100) {
133                         last_ticks = 0;
134                         debug_putc('\n');
135                 }
136                 if (ms_ticks - led_ticks >= 100) {
137                         led_ticks = ms_ticks;
138                         debug_led_toggle();
139                 }
140         }
141
142         return 0;
143 }