forked from wolfSSL/wolfBoot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathled.c
232 lines (200 loc) · 7.57 KB
/
led.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/* led.c
*
* Test bare-metal blinking led application
*
* Copyright (C) 2021 wolfSSL Inc.
*
* This file is part of wolfBoot.
*
* wolfBoot is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* wolfBoot 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
*/
#include <stdint.h>
#include "wolfboot/wolfboot.h"
#ifdef PLATFORM_stm32f4
#define AHB1_CLOCK_ER (*(volatile uint32_t *)(0x40023830))
#define GPIOD_AHB1_CLOCK_ER (1 << 3)
#define GPIOD_BASE 0x40020c00
#define GPIOD_MODE (*(volatile uint32_t *)(GPIOD_BASE + 0x00))
#define GPIOD_OTYPE (*(volatile uint32_t *)(GPIOD_BASE + 0x04))
#define GPIOD_OSPD (*(volatile uint32_t *)(GPIOD_BASE + 0x08))
#define GPIOD_PUPD (*(volatile uint32_t *)(GPIOD_BASE + 0x0c))
#define GPIOD_ODR (*(volatile uint32_t *)(GPIOD_BASE + 0x14))
#define GPIOD_BSRR (*(volatile uint32_t *)(GPIOD_BASE + 0x18))
#define GPIOD_AFL (*(volatile uint32_t *)(GPIOD_BASE + 0x20))
#define GPIOD_AFH (*(volatile uint32_t *)(GPIOD_BASE + 0x24))
#define LED_PIN (15)
#define LED_BOOT_PIN (14)
#define GPIO_OSPEED_100MHZ (0x03)
void led_pwm_setup(void)
{
uint32_t reg;
AHB1_CLOCK_ER |= GPIOD_AHB1_CLOCK_ER;
reg = GPIOD_MODE & ~ (0x03 << (LED_PIN * 2));
GPIOD_MODE = reg | (2 << (LED_PIN * 2));
reg = GPIOD_OSPD & ~(0x03 << (LED_PIN * 2));
GPIOD_OSPD = reg | (0x03 << (LED_PIN * 2));
reg = GPIOD_PUPD & ~(0x03 << (LED_PIN * 2));
GPIOD_PUPD = reg | (0x02 << (LED_PIN * 2));
/* Alternate function: use high pin */
reg = GPIOD_AFH & ~(0xf << ((LED_PIN - 8) * 4));
GPIOD_AFH = reg | (0x2 << ((LED_PIN - 8) * 4));
}
void boot_led_on(void)
{
uint32_t reg;
uint32_t pin = LED_BOOT_PIN;// - 2 * (wolfBoot_current_firmware_version() & 0x01);
AHB1_CLOCK_ER |= GPIOD_AHB1_CLOCK_ER;
reg = GPIOD_MODE & ~(0x03 << (pin * 2));
GPIOD_MODE = reg | (1 << (pin * 2));
reg = GPIOD_PUPD & ~(0x03 << (pin * 2));
GPIOD_PUPD = reg | (1 << (pin * 2));
GPIOD_BSRR |= (1 << pin);
}
#endif /* PLATFORM_stm32f4 */
#ifdef PLATFORM_stm32l0
#define LED_BOOT_PIN (5)
#define RCC_IOPENR (*(volatile uint32_t *)(0x4002102C))
#define IOPAEN (1 << 0)
#define GPIOA_BASE 0x50000000
#define GPIOA_MODE (*(volatile uint32_t *)(GPIOA_BASE + 0x00))
#define GPIOA_OTYPE (*(volatile uint32_t *)(GPIOA_BASE + 0x04))
#define GPIOA_OSPD (*(volatile uint32_t *)(GPIOA_BASE + 0x08))
#define GPIOA_PUPD (*(volatile uint32_t *)(GPIOA_BASE + 0x0c))
#define GPIOA_ODR (*(volatile uint32_t *)(GPIOA_BASE + 0x14))
#define GPIOA_BSRR (*(volatile uint32_t *)(GPIOA_BASE + 0x18))
#define GPIOA_AFL (*(volatile uint32_t *)(GPIOA_BASE + 0x20))
#define GPIOA_AFH (*(volatile uint32_t *)(GPIOA_BASE + 0x24))
void boot_led_on(void)
{
uint32_t reg;
uint32_t pin = LED_BOOT_PIN;
RCC_IOPENR |= IOPAEN;
reg = GPIOA_MODE & ~(0x03 << (pin * 2));
GPIOA_MODE = reg | (1 << (pin * 2));
reg = GPIOA_PUPD & ~(0x03 << (pin * 2));
GPIOA_PUPD = reg | (1 << (pin * 2));
GPIOA_BSRR |= (1 << pin);
}
void boot_led_off(void)
{
uint32_t pin = LED_BOOT_PIN;
GPIOA_BSRR |= (1 << (pin + 16));
}
#endif /* PLATFORM_stm32l0 */
#if defined(PLATFORM_stm32g0) || defined(PLATFORM_stm32c0)
/* GPIOA5 */
#define RCC_IOPENR (*(volatile uint32_t *)(0x40021034))
#define RCC_IOPENR_GPIOAEN (1 << 0)
#define GPIOA_BASE 0x50000000
#define GPIOA_MODE (*(volatile uint32_t *)(GPIOA_BASE + 0x00))
#define GPIOA_OTYPE (*(volatile uint32_t *)(GPIOA_BASE + 0x04))
#define GPIOA_OSPD (*(volatile uint32_t *)(GPIOA_BASE + 0x08))
#define GPIOA_PUPD (*(volatile uint32_t *)(GPIOA_BASE + 0x0c))
#define GPIOA_ODR (*(volatile uint32_t *)(GPIOA_BASE + 0x14))
#define GPIOA_BSRR (*(volatile uint32_t *)(GPIOA_BASE + 0x18))
#define GPIOA_AFL (*(volatile uint32_t *)(GPIOA_BASE + 0x20))
#define GPIOA_AFH (*(volatile uint32_t *)(GPIOA_BASE + 0x24))
#define LED_PIN (5)
#define LED_BOOT_PIN (5)
#define GPIO_OSPEED_100MHZ (0x03)
void boot_led_on(void)
{
uint32_t reg;
uint32_t pin = LED_BOOT_PIN;
RCC_IOPENR |= RCC_IOPENR_GPIOAEN;
reg = GPIOA_MODE & ~(0x03 << (pin * 2));
GPIOA_MODE = reg | (1 << (pin * 2)); /* general purpose output mode */
reg = GPIOA_PUPD & ~(0x03 << (pin * 2));
GPIOA_PUPD = reg | (1 << (pin * 2)); /* pull-up */
GPIOA_BSRR |= (1 << pin); /* set pin */
}
#endif /* PLATFORM_stm32g0 || PLATFORM_stm32c0 */
#ifdef PLATFORM_stm32wb
#define LED_BOOT_PIN (0)
#define RCC_AHB2_CLOCK_ER (*(volatile uint32_t *)(0x5800004C))
#define GPIOB_AHB2_CLOCK_ER (1 << 1)
#define GPIOB_BASE 0x48000400
#define GPIOB_MODE (*(volatile uint32_t *)(GPIOB_BASE + 0x00))
#define GPIOB_OTYPE (*(volatile uint32_t *)(GPIOB_BASE + 0x04))
#define GPIOB_OSPD (*(volatile uint32_t *)(GPIOB_BASE + 0x08))
#define GPIOB_PUPD (*(volatile uint32_t *)(GPIOB_BASE + 0x0c))
#define GPIOB_ODR (*(volatile uint32_t *)(GPIOB_BASE + 0x14))
#define GPIOB_BSRR (*(volatile uint32_t *)(GPIOB_BASE + 0x18))
#define GPIOB_AFL (*(volatile uint32_t *)(GPIOB_BASE + 0x20))
#define GPIOB_AFH (*(volatile uint32_t *)(GPIOB_BASE + 0x24))
void boot_led_on(void)
{
uint32_t reg;
uint32_t pin = LED_BOOT_PIN;
RCC_AHB2_CLOCK_ER |= GPIOB_AHB2_CLOCK_ER;
reg = GPIOB_MODE & ~(0x03 << (pin * 2));
GPIOB_MODE = reg | (1 << (pin * 2));
reg = GPIOB_PUPD & ~(0x03 << (pin * 2));
GPIOB_PUPD = reg | (1 << (pin * 2));
GPIOB_BSRR |= (1 << pin);
}
void boot_led_off(void)
{
uint32_t reg;
uint32_t pin = LED_BOOT_PIN;
GPIOB_BSRR |= (1 << (pin + 16));
}
#endif /* PLATFORM_stm32wb */
#ifdef PLATFORM_stm32l4
#define AHB2_CLOCK_ER (*(volatile uint32_t *)(0x4002104C)) /* RCC_AHB2ENR */
#define GPIOB_AHB2_CLOCK_ER (1 << 1)
#define GPIOB_BASE 0x48000400
#define GPIOB_MODE (*(volatile uint32_t *)(GPIOB_BASE + 0x00))
#define GPIOB_OTYPE (*(volatile uint32_t *)(GPIOB_BASE + 0x04))
#define GPIOB_OSPD (*(volatile uint32_t *)(GPIOB_BASE + 0x08))
#define GPIOB_PUPD (*(volatile uint32_t *)(GPIOB_BASE + 0x0c))
#define GPIOB_ODR (*(volatile uint32_t *)(GPIOB_BASE + 0x14))
#define GPIOB_BSRR (*(volatile uint32_t *)(GPIOB_BASE + 0x18))
#define GPIOB_AFL (*(volatile uint32_t *)(GPIOB_BASE + 0x20))
#define GPIOB_AFH (*(volatile uint32_t *)(GPIOB_BASE + 0x24))
#define LED_PIN (14) /* User LD3: a red user LED is connected to PB14 */
#define LED_BOOT_PIN (7) /* User LD2: a blue user LED is connected to PB7 */
#define GPIO_OSPEED_100MHZ (0x03)
void boot_led_on(void)
{
uint32_t reg;
uint32_t pin = LED_BOOT_PIN;
AHB2_CLOCK_ER |= GPIOB_AHB2_CLOCK_ER;
reg = GPIOB_MODE & ~(0x03 << (pin * 2));
GPIOB_MODE = reg | (1 << (pin * 2));
reg = GPIOB_PUPD & ~(0x03 << (pin * 2));
GPIOB_PUPD = reg | (1 << (pin * 2));
GPIOB_BSRR |= (1 << pin);
}
void led_on(void)
{
uint32_t reg;
uint32_t pin = LED_PIN;
AHB2_CLOCK_ER |= GPIOB_AHB2_CLOCK_ER;
reg = GPIOB_MODE & ~(0x03 << (pin * 2));
GPIOB_MODE = reg | (1 << (pin * 2));
reg = GPIOB_PUPD & ~(0x03 << (pin * 2));
GPIOB_PUPD = reg | (1 << (pin * 2));
GPIOB_BSRR |= (1 << pin);
}
void led_off(void)
{
GPIOB_BSRR |= (1 << (LED_PIN + 16));
}
void boot_led_off(void)
{
GPIOB_BSRR |= (1 << (LED_BOOT_PIN + 16));
}
#endif /* PLATFORM_stm32l4 */