Merge branch 'tmaster' into future
[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 LED_PORT_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 #define LED_ORANGE 0
17 #define LED_RED 0
18
19 static inline void setup_leds(void)
20 {
21   *(volatile uint32_t*)GPIOC_CRH = 0x44444411;
22 }
23
24 #elif CONFIG_STM32L_DISCOVERY
25
26 #define GPIOB 0x40020400 /* port B */
27 #define GPIOB_MODER (GPIOB + 0x00) /* port mode register */
28 #define LED_PORT_ODR (GPIOB + 0x14) /* port output data register */
29
30 #define LED_BLUE (1 << 6) /* port B, pin 6 */
31 #define LED_GREEN (1 << 7) /* port B, pin 7 */
32 #define LED_ORANGE 0
33 #define LED_RED 0
34
35 static inline void setup_leds(void)
36 {
37   /* configure port 6 and 7 as output */
38   *(volatile uint32_t*)GPIOB_MODER |= (1 << (7 * 2)) | (1 << (6 * 2));
39 }
40
41 #elif CONFIG_STM32F4_DISCOVERY
42
43 #define GPIOD 0x40020C00 /* port D */
44 #define GPIOD_MODER (GPIOD + 0x00) /* port mode register */
45 #define LED_PORT_ODR (GPIOD + 0x14) /* port output data register */
46
47 #define LED_GREEN (1 << 12) /* port D, pin 12 */
48 #define LED_ORANGE (1 << 13) /* port D, pin 13 */
49 #define LED_RED (1 << 14) /* port D, pin 14 */
50 #define LED_BLUE (1 << 15) /* port D, pin 15 */
51
52 void _tmain(void) {
53         main();
54 }
55 static inline void setup_leds(void)
56 {
57   *(volatile uint32_t*)GPIOD_MODER |= (1 << (12 * 2)) | (1 << (13 * 2)) |
58         (1 << (13 * 2)) | (1 << (14 * 2)) | (1 << (15 * 2));
59 }
60
61 #else
62 #error "Architecture must be defined!"
63 #endif /* otherwise, error */
64
65 static inline void switch_leds_on(void)
66 {
67   *(volatile uint32_t*)LED_PORT_ODR = LED_BLUE | LED_GREEN | LED_ORANGE | LED_RED;
68 }
69
70 static inline void switch_leds_off(void)
71 {
72   *(volatile uint32_t*)LED_PORT_ODR = 0;
73 }
74
75 #define delay()                                         \
76 do {                                                    \
77   register unsigned int i;                              \
78   for (i = 0; i < 1000000; ++i)                         \
79     __asm__ __volatile__ ("nop\n\t":::"memory");        \
80 } while (0)
81
82 /* static void __attribute__((naked)) __attribute__((used)) main(void) */
83 void main(void)
84 {
85   setup_leds();
86
87   while (1)
88   {
89     switch_leds_on();
90     delay();
91     switch_leds_off();
92     delay();
93   }
94 }