-
-
Notifications
You must be signed in to change notification settings - Fork 303
/
main.c
107 lines (102 loc) · 2.84 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/*
* This file is part of the libopencm3 project.
*
* Copyright (C) 2009 Uwe Hermann <uwe@hermann-uwe.de>
* Copyright (C) 2011 Stephen Caudle <scaudle@doceme.com>
* Copyright (C) 2012 Karl Palsson <karlp@tweak.net.au>
*
* This library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#ifdef STM32F0
#define RCCLEDPORT (RCC_GPIOE)
#define LEDPORT (GPIOE)
#define LEDPIN (GPIO8)
#elif STM32F1
#define RCCLEDPORT (RCC_GPIOC)
#define LEDPORT (GPIOC)
#define LEDPIN (GPIO13)
#define GPIO_MODE_OUTPUT GPIO_MODE_OUTPUT_2_MHZ
#define GPIO_PUPD_NONE GPIO_CNF_OUTPUT_PUSHPULL
#elif STM32F2
#define RCCLEDPORT (RCC_GPIOE)
#define LEDPORT (GPIOE)
#define LEDPIN (GPIO8)
#elif STM32F3
#define RCCLEDPORT (RCC_GPIOE)
#define LEDPORT (GPIOE)
#define LEDPIN (GPIO8)
#elif STM32F4
#define RCCLEDPORT (RCC_GPIOD)
#define LEDPORT (GPIOD)
#define LEDPIN (GPIO12)
#elif STM32F7
#define RCCLEDPORT (RCC_GPIOD)
#define LEDPORT (GPIOD)
#define LEDPIN (GPIO12)
#elif STM32G0
#define RCCLEDPORT (RCC_GPIOD)
#define LEDPORT (GPIOD)
#define LEDPIN (GPIO12)
#elif STM32G4
#define RCCLEDPORT (RCC_GPIOD)
#define LEDPORT (GPIOD)
#define LEDPIN (GPIO12)
#elif STM32H7
#define RCCLEDPORT (RCC_GPIOD)
#define LEDPORT (GPIOD)
#define LEDPIN (GPIO12)
#elif STM32L0
#define RCCLEDPORT (RCC_GPIOB)
#define LEDPORT (GPIOB)
#define LEDPIN (GPIO6)
#elif STM32L1
#define RCCLEDPORT (RCC_GPIOB)
#define LEDPORT (GPIOB)
#define LEDPIN (GPIO6)
#elif STM32L4
#define RCCLEDPORT (RCC_GPIOB)
#define LEDPORT (GPIOB)
#define LEDPIN (GPIO6)
#else
#error "This example doesn't support this target!"
#endif
static void gpio_setup(void)
{
/* Enable GPIO clock. */
/* Using API functions: */
rcc_periph_clock_enable(RCCLEDPORT);
/* Set pin to 'output push-pull'. */
/* Using API functions: */
#ifdef STM32F1
gpio_set_mode(LEDPORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LEDPIN);
#else
gpio_mode_setup(LEDPORT, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, LEDPIN);
#endif
}
int main(void)
{
int i;
gpio_setup();
/* Blink the LED on the board. */
while (1) {
/* Using API function gpio_toggle(): */
gpio_toggle(LEDPORT, LEDPIN); /* LED on/off */
for (i = 0; i < 1000000; i++) { /* Wait a bit. */
__asm__("nop");
}
}
return 0;
}