]> mj.ucw.cz Git - home-hw.git/blob - test-opencm3/test.c
a38394cb2b1d7f255d16fed07665dcd2cee8459d
[home-hw.git] / test-opencm3 / test.c
1 #include <stdint.h>
2
3 #include <libopencm3/cm3/nvic.h>
4 #include <libopencm3/cm3/systick.h>
5 #include <libopencm3/stm32/rcc.h>
6 #include <libopencm3/stm32/gpio.h>
7
8 typedef uint8_t byte;
9 typedef uint16_t u16;
10 typedef uint32_t u32;
11 typedef unsigned int uint;
12
13 static void clock_setup(void)
14 {
15         rcc_clock_setup_in_hse_8mhz_out_72mhz();
16
17         rcc_periph_clock_enable(RCC_GPIOC);
18 }
19
20 static void gpio_setup(void)
21 {
22         gpio_set_mode(GPIOC, GPIO_MODE_OUTPUT_50_MHZ, GPIO_CNF_OUTPUT_PUSHPULL, GPIO13);
23 }
24
25 static volatile u32 ms_ticks;
26
27 void sys_tick_handler(void)
28 {
29         ms_ticks++;
30 }
31
32 static void tick_setup(void)
33 {
34         systick_set_frequency(1000, 72000000);
35         systick_counter_enable();
36         systick_interrupt_enable();
37 }
38
39 static void delay_ms(uint ms)
40 {
41         u32 start_ticks = ms_ticks;
42         while (ms_ticks - start_ticks < ms)
43                 ;
44 }
45
46 int main(void)
47 {
48         clock_setup();
49         gpio_setup();
50         tick_setup();
51
52         /* Blink the LED (PC13) on the board. */
53         for (;;) {
54                 gpio_toggle(GPIOC, GPIO13);     /* LED on/off */
55                 delay_ms(1000);
56         }
57
58         return 0;
59 }