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>
10 static void clock_setup(void)
12 rcc_clock_setup_in_hse_8mhz_out_72mhz();
14 rcc_periph_clock_enable(RCC_GPIOA);
15 rcc_periph_clock_enable(RCC_GPIOC);
16 rcc_periph_clock_enable(RCC_USART1);
19 rcc_periph_reset_pulse(RST_GPIOA);
20 rcc_periph_reset_pulse(RST_GPIOC);
21 rcc_periph_reset_pulse(RST_USART1);
25 static void gpio_setup(void)
27 // PC13 = BluePill LED
28 gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
29 gpio_clear(GPIOC, GPIO13);
32 static void usart_setup(void)
34 gpio_set_mode(GPIOA, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_ALTFN_PUSHPULL, GPIO_USART1_TX);
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);
46 static volatile u32 ms_ticks;
48 void sys_tick_handler(void)
53 static void tick_setup(void)
55 systick_set_frequency(1000, 72000000);
56 systick_counter_enable();
57 systick_interrupt_enable();
60 static void delay_ms(uint ms)
62 u32 start_ticks = ms_ticks;
63 while (ms_ticks - start_ticks < ms)
74 for (uint i=0; i<20; i++) {
76 gpio_toggle(GPIOC, GPIO13);
81 for (uint i=0; i<2048; i++) {
82 u32 a = 0x1ffff000 + i;
84 debug_printf("%08x:", a);
85 gpio_toggle(GPIOC, GPIO13);
87 debug_printf(" %02x", *(byte *)a);