]> mj.ucw.cz Git - home-hw.git/blob - test-dumploader/test.c
Rolety: Annotation
[home-hw.git] / test-dumploader / 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_GPIOA);
15         rcc_periph_clock_enable(RCC_GPIOC);
16         rcc_periph_clock_enable(RCC_USART1);
17
18 #if 0
19         rcc_periph_reset_pulse(RST_GPIOA);
20         rcc_periph_reset_pulse(RST_GPIOC);
21         rcc_periph_reset_pulse(RST_USART1);
22 #endif
23 }
24
25 static void gpio_setup(void)
26 {
27         // PC13 = BluePill LED
28         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
29         gpio_clear(GPIOC, GPIO13);
30 }
31
32 static void usart_setup(void)
33 {
34         gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
35
36         usart_set_baudrate(USART1, 115200);
37         usart_set_databits(USART1, 8);
38         usart_set_stopbits(USART1, USART_STOPBITS_1);
39         usart_set_mode(USART1, USART_MODE_TX_RX);
40         usart_set_parity(USART1, USART_PARITY_NONE);
41         usart_set_flow_control(USART1, USART_FLOWCONTROL_NONE);
42
43         usart_enable(USART1);
44 }
45
46 static volatile u32 ms_ticks;
47
48 void sys_tick_handler(void)
49 {
50         ms_ticks++;
51 }
52
53 static void tick_setup(void)
54 {
55         systick_set_frequency(1000, 72000000);
56         systick_counter_enable();
57         systick_interrupt_enable();
58 }
59
60 static void delay_ms(uint ms)
61 {
62         u32 start_ticks = ms_ticks;
63         while (ms_ticks - start_ticks < ms)
64                 ;
65 }
66
67 int main(void)
68 {
69         clock_setup();
70         gpio_setup();
71         tick_setup();
72         usart_setup();
73
74         for (uint i=0; i<20; i++) {
75                 delay_ms(100);
76                 gpio_toggle(GPIOC, GPIO13);
77         }
78
79         for (;;) {
80                 delay_ms(1000);
81                 for (uint i=0; i<2048; i++) {
82                         u32 a = 0x1ffff000 + i;
83                         if (!(i%32)) {
84                                 debug_printf("%08x:", a);
85                                 gpio_toggle(GPIOC, GPIO13);
86                         }
87                         debug_printf(" %02x", *(byte *)a);
88                         if (i%32 == 31)
89                                 debug_putc('\n');
90                 }
91         }
92
93         return 0;
94 }