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
|
// vim: shiftwidth=4 tabstop=4 noexpandtab
/*
------------------------------------------------------------------------------
ledcontrol firmware
Copyright (C) 2021 xengineering
This program 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 3 of the License, or
(at your option) any later version.
This program 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, see <https://www.gnu.org/licenses/>.
Contact: me@xengineering.eu
------------------------------------------------------------------------------
This firmware currently has these functions:
- blink the onboard LED C13
- let an external LED at PB0 fade
Future features should be:
- adjust the brightness of the external LED by an incremental encoder
- enable / disable the light by pressing a push button (included in encoder)
- offer this functionality via WiFi with an external WiFi-capable board
------------------------------------------------------------------------------
*/
#include <libopencm3/stm32/rcc.h>
#include <libopencm3/stm32/gpio.h>
#include <libopencm3/stm32/timer.h>
#include <libopencm3/cm3/nvic.h>
static void clock_init(void);
static void gpio_init(void);
static void nvic_init(void);
static void timer_init(void);
void tim2_isr(void);
/* main function
This should give you a brief overview of the control flow. Read the
corresponding function definitions for details about configuration and
implementation.
*/
int main(void)
{
clock_init();
gpio_init();
nvic_init();
timer_init();
while(1); // wait forever
}
/* Clock initialization
This function configures the system clock of the microcontroller (sysclk). The
dynamic configuration allows to execute the program faster (high clock speed)
or to save energy (low clock speed).
Important to notice: On STM32 microcontrollers it is mandatory to enable the
clocks for peripherals and timer / counter units explicitly!
*/
static void clock_init(void)
{
// set system clock to 72 MHz via external 8 MHz crystal and a PLL
rcc_clock_setup_pll(&rcc_hse_configs[RCC_CLOCK_HSE8_72MHZ]);
// enable clocks for GPIO ports B and C
rcc_periph_clock_enable(RCC_GPIOB);
rcc_periph_clock_enable(RCC_GPIOC);
// enable clock for timer 2 (blinking)
rcc_periph_clock_enable(RCC_TIM2);
// enable clock for timer 3 (PWM)
rcc_periph_clock_enable(RCC_TIM3);
}
/* Initialization of general purpose inputs and outputs (GPIOs) */
static void gpio_init(void)
{
// init PC13 onboard LED for blinking
gpio_set_mode(
GPIOC,
GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_PUSHPULL,
GPIO13
);
// set initial value to 'on'
gpio_clear(GPIOC, GPIO13);
// setup PB0 (connected to TIM3_CH3) for PWM
gpio_set_mode(
GPIOB,
GPIO_MODE_OUTPUT_50_MHZ,
GPIO_CNF_OUTPUT_ALTFN_PUSHPULL,
GPIO_TIM3_CH3
);
}
/* Nested Vectored Interrupt Controller (NVIC) setup
This function configures NVIC. This way the interrupt triggered by
timer / counter units is enabled explicitly.
*/
static void nvic_init(void)
{
// enable timer 2 interrupt and set priority
nvic_enable_irq(NVIC_TIM2_IRQ);
nvic_set_priority(NVIC_TIM2_IRQ, 1);
}
/* Timer / counter setup
This function configures two timer / counter units (TIM2 and TIM3). TIM2
controls the blinking and fading animation and TIM3 is for the
pulse-width-modulation (PWM) of the external LED.
*/
static void timer_init(void)
{
// setup timer 2 for blinking
timer_set_counter(TIM2, 1);
timer_set_prescaler(TIM2, 1440);
timer_set_period(TIM2, 50);
// enable timer / counter 2 and the corresponding interrupt for blinking
timer_enable_irq(TIM2, TIM_DIER_UIE);
timer_enable_counter(TIM2);
/*
enable timer / counter 3
// TODO: clean the rest of this function ... it is a mess
*/
/* Clock division and mode */
TIM3_CR1 = TIM_CR1_CKD_CK_INT | TIM_CR1_CMS_EDGE;
/* Period */
TIM3_ARR = 65535;
/* Prescaler */
TIM3_PSC = 0;
TIM3_EGR = TIM_EGR_UG;
/* ---- */
/* Output compare 3 mode and preload */
TIM3_CCMR2 |= TIM_CCMR2_OC3M_PWM1 | TIM_CCMR2_OC3PE;
/* Polarity and state */
TIM3_CCER |= TIM_CCER_CC3P | TIM_CCER_CC3E;
//TIM3_CCER |= TIM_CCER_CC3E;
/* Capture compare value */
TIM3_CCR3 = 30000;
/* ---- */
/* ARR reload enable */
TIM3_CR1 |= TIM_CR1_ARPE;
/* Counter enable */
TIM3_CR1 |= TIM_CR1_CEN;
}
/* Timer / counter 2 interrupt service routine
This function is called by the interrupt of timer / counter 2. The function is
not called explicitly in this file but hard-coded in the libopencm3 library.
The purpose of TIM2 is to blink the onboard LED and to decrement the PWM value
resulting in a increment of brightness. Thus this function defines the logic of
the 'animation' shown by the LEDs.
*/
void tim2_isr(void)
{
// PWM handling
static uint32_t pwm_value = 0; // static variable to remember value on next
// function call
if (pwm_value == 0) {
pwm_value = 65535;
}
else {
pwm_value -= 20;
}
TIM3_CCR3 = pwm_value;
// toggle blink LED
static uint32_t counter;
counter += 1;
if (counter > 500) {
gpio_toggle(GPIOC, GPIO13);
counter = 0;
}
// clear interrrupt flag
TIM_SR(TIM2) &= ~TIM_SR_UIF;
}
|