Merge branch 'master' of https://github.com/macpod/stlink
[fw/stlink] / example / blink / main.c
1 /* missing type */
2
3 typedef unsigned int uint32_t;
4
5
6 /* hardware configuration */
7
8 #if CONFIG_STM32VL_DISCOVERY
9
10 # define GPIOC 0x40011000 /* port C */
11 # define GPIOC_CRH (GPIOC + 0x04) /* port configuration register high */
12 # define GPIOC_ODR (GPIOC + 0x0c) /* port output data register */
13
14 # define LED_BLUE (1 << 8) /* port C, pin 8 */
15 # define LED_GREEN (1 << 9) /* port C, pin 9 */
16
17 static inline void setup_leds(void)
18 {
19   *(volatile uint32_t*)GPIOC_CRH = 0x44444411;
20 }
21
22 static inline void switch_leds_on(void)
23 {
24   *(volatile uint32_t*)GPIOC_ODR = LED_BLUE | LED_GREEN;
25 }
26
27 static inline void switch_leds_off(void)
28 {
29   *(volatile uint32_t*)GPIOC_ODR = 0;
30 }
31
32 #elif CONFIG_STM32L_DISCOVERY
33
34 # define GPIOB 0x40020400 /* port B */
35 # define GPIOB_MODER (GPIOB + 0x00) /* port mode register */
36 # define GPIOB_ODR (GPIOB + 0x14) /* port output data register */
37
38 # define LED_BLUE (1 << 6) /* port B, pin 6 */
39 # define LED_GREEN (1 << 7) /* port B, pin 7 */
40
41 static inline void setup_leds(void)
42 {
43   /* configure port 6 and 7 as output */
44   *(volatile uint32_t*)GPIOB_MODER |= (1 << (7 * 2)) | (1 << (6 * 2));
45 }
46
47 static inline void switch_leds_on(void)
48 {
49   *(volatile uint32_t*)GPIOB_ODR = LED_BLUE | LED_GREEN;
50 }
51
52 static inline void switch_leds_off(void)
53 {
54   *(volatile uint32_t*)GPIOB_ODR = 0;
55 }
56
57 #elif CONFIG_STM32F4_DISCOVERY
58
59 #define GPIOD 0x40020C00 /* port D */
60 # define GPIOD_MODER (GPIOD + 0x00) /* port mode register */
61 # define GPIOD_ODR (GPIOD + 0x14) /* port output data register */
62
63 # define LED_GREEN (1 << 12) /* port B, pin 12 */
64 # define LED_ORANGE (1 << 13) /* port B, pin 13 */
65 # define LED_RED (1 << 14) /* port B, pin 14 */
66 # define LED_BLUE (1 << 15) /* port B, pin 15 */
67
68 static inline void setup_leds(void)
69 {
70   *(volatile uint32_t*)GPIOD_MODER |= (1 << (12 * 2)) | (1 << (13 * 2)) |
71         (1 << (13 * 2)) | (1 << (14 * 2)) | (1 << (15 * 2));
72 }
73
74
75 static inline void switch_leds_on(void)
76 {
77   *(volatile uint32_t*)GPIOD_ODR = LED_GREEN | LED_ORANGE | LED_RED | LED_BLUE;
78 }
79
80 static inline void switch_leds_off(void)
81 {
82   *(volatile uint32_t*)GPIOD_ODR = 0;
83 }
84
85 #else
86 #error "Architecture must be defined!"
87 #endif /* otherwise, error */
88
89
90 #define delay()                                         \
91 do {                                                    \
92   register unsigned int i;                              \
93   for (i = 0; i < 1000000; ++i)                         \
94     __asm__ __volatile__ ("nop\n\t":::"memory");        \
95 } while (0)
96
97 static void __attribute__((naked)) __attribute__((used)) main(void)
98 {
99   setup_leds();
100
101   while (1)
102   {
103     switch_leds_on();
104     delay();
105     switch_leds_off();
106     delay();
107   }
108 }