Skip to content

Commit 39d0453

Browse files
committed
templates.stm: F4 C++ template multi-LED
1 parent e04f80a commit 39d0453

File tree

11 files changed

+578
-15
lines changed

11 files changed

+578
-15
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//
2+
// This file is part of the GNU ARM Eclipse distribution.
3+
// Copyright (c) 2014 Liviu Ionescu.
4+
//
5+
6+
#ifndef BLINKLED_H_
7+
#define BLINKLED_H_
8+
9+
// ----------------------------------------------------------------------------
10+
11+
class BlinkLed
12+
{
13+
public:
14+
BlinkLed (unsigned int port, unsigned int bit, bool active_low);
15+
16+
void
17+
powerUp ();
18+
19+
void
20+
turnOn ();
21+
22+
void
23+
turnOff ();
24+
25+
void
26+
toggle ();
27+
28+
bool
29+
isOn ();
30+
31+
private:
32+
unsigned int fPortNumber;
33+
unsigned int fBitNumber;
34+
unsigned int fBitMask;
35+
bool fIsActiveLow;
36+
};
37+
38+
// ----------------------------------------------------------------------------
39+
40+
#endif // BLINKLED_H_
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
//
2+
// This file is part of the GNU ARM Eclipse distribution.
3+
// Copyright (c) 2014 Liviu Ionescu.
4+
//
5+
6+
#include "BlinkLed.h"
7+
8+
#include "$(CMSIS_name).h"
9+
#include "$(CMSIS_name)_hal.h"
10+
11+
#define BLINK_GPIOx(_N) ((GPIO_TypeDef *)(GPIOA_BASE + (GPIOB_BASE-GPIOA_BASE)*(_N)))
12+
#define BLINK_PIN_MASK(_N) (1 << (_N))
13+
#define BLINK_RCC_MASKx(_N) (RCC_AHB1ENR_GPIOAEN << (_N))
14+
15+
// ----------------------------------------------------------------------------
16+
17+
BlinkLed::BlinkLed (unsigned int port, unsigned int bit, bool activeLow)
18+
{
19+
fPortNumber = port;
20+
fBitNumber = bit;
21+
fIsActiveLow = activeLow;
22+
fBitMask = BLINK_PIN_MASK(fBitNumber);
23+
}
24+
25+
void
26+
BlinkLed::powerUp ()
27+
{
28+
// Enable GPIO Peripheral clock
29+
RCC->AHB1ENR |= BLINK_RCC_MASKx(fPortNumber);
30+
31+
GPIO_InitTypeDef GPIO_InitStructure;
32+
33+
// Configure pin in output push/pull mode
34+
GPIO_InitStructure.Pin = fBitMask;
35+
GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_PP;
36+
GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
37+
GPIO_InitStructure.Pull = GPIO_PULLUP;
38+
HAL_GPIO_Init (BLINK_GPIOx(fPortNumber), &GPIO_InitStructure);
39+
40+
// Start with led turned off
41+
turnOff ();
42+
}
43+
44+
void
45+
BlinkLed::turnOn ()
46+
{
47+
if (fIsActiveLow)
48+
{
49+
BLINK_GPIOx(fPortNumber)->BSRR = BLINK_PIN_MASK(fBitNumber + 16);
50+
}
51+
else
52+
{
53+
BLINK_GPIOx(fPortNumber)->BSRR = fBitMask;
54+
}
55+
}
56+
57+
void
58+
BlinkLed::turnOff ()
59+
{
60+
if (fIsActiveLow)
61+
{
62+
BLINK_GPIOx(fPortNumber)->BSRR = fBitMask;
63+
}
64+
else
65+
{
66+
BLINK_GPIOx(fPortNumber)->BSRR = BLINK_PIN_MASK(fBitNumber + 16);
67+
}
68+
}
69+
70+
void
71+
BlinkLed::toggle ()
72+
{
73+
if (BLINK_GPIOx(fPortNumber)->IDR & fBitMask)
74+
{
75+
BLINK_GPIOx(fPortNumber)->ODR &= ~fBitMask;
76+
}
77+
else
78+
{
79+
BLINK_GPIOx(fPortNumber)->ODR |= fBitMask;
80+
}
81+
}
82+
83+
bool
84+
BlinkLed::isOn ()
85+
{
86+
if (fIsActiveLow)
87+
{
88+
return (BLINK_GPIOx(fPortNumber)->IDR & fBitMask) == 0;
89+
}
90+
else
91+
{
92+
return (BLINK_GPIOx(fPortNumber)->IDR & fBitMask) != 0;
93+
}
94+
}
95+
96+
// ----------------------------------------------------------------------------

ilg.gnuarmeclipse.templates.stm/templates/stm32f4xx_exe_c_project/src/main/main-145-leds-blinky.c

Whitespace-only changes.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
2+
// ----- LED definitions ------------------------------------------------------
3+
4+
#if defined(STM32F401xE)
5+
6+
#warning "Assume a NUCLEO-F401RE board, PA5, active high."
7+
8+
// PA5
9+
#define BLINK_PORT_NUMBER (0)
10+
#define BLINK_PIN_NUMBER (5)
11+
#define BLINK_ACTIVE_LOW (false)
12+
13+
BlinkLed blinkLeds[1] =
14+
{
15+
{ BLINK_PORT_NUMBER, BLINK_PIN_NUMBER, BLINK_ACTIVE_LOW },
16+
};
17+
18+
#elif defined(STM32F407xx)
19+
20+
#warning "Assume a STM32F4-Discovery board, PC12-PC15, active high."
21+
22+
#define BLINK_PORT_NUMBER (3)
23+
#define BLINK_PIN_NUMBER_GREEN (12)
24+
#define BLINK_PIN_NUMBER_ORANGE (13)
25+
#define BLINK_PIN_NUMBER_RED (14)
26+
#define BLINK_PIN_NUMBER_BLUE (15)
27+
#define BLINK_ACTIVE_LOW (false)
28+
29+
BlinkLed blinkLeds[4] =
30+
{
31+
{ BLINK_PORT_NUMBER, BLINK_PIN_NUMBER_GREEN, BLINK_ACTIVE_LOW },
32+
{ BLINK_PORT_NUMBER, BLINK_PIN_NUMBER_ORANGE, BLINK_ACTIVE_LOW },
33+
{ BLINK_PORT_NUMBER, BLINK_PIN_NUMBER_RED, BLINK_ACTIVE_LOW },
34+
{ BLINK_PORT_NUMBER, BLINK_PIN_NUMBER_BLUE, BLINK_ACTIVE_LOW },
35+
};
36+
37+
#elif defined(STM32F411xE)
38+
39+
#warning "Assume a NUCLEO-F411RE board, PA5, active high."
40+
41+
#define BLINK_PORT_NUMBER (0)
42+
#define BLINK_PIN_NUMBER (5)
43+
#define BLINK_ACTIVE_LOW (false)
44+
45+
BlinkLed blinkLeds[1] =
46+
{
47+
{ BLINK_PORT_NUMBER, BLINK_PIN_NUMBER, BLINK_ACTIVE_LOW },
48+
};
49+
50+
#elif defined(STM32F429xx)
51+
52+
#warning "Assume a STM32F429I-Discovery board, PG13-PG14, active high."
53+
54+
#define BLINK_PORT_NUMBER (6)
55+
#define BLINK_PIN_NUMBER_GREEN (13)
56+
#define BLINK_PIN_NUMBER_RED (14)
57+
#define BLINK_ACTIVE_LOW (false)
58+
59+
BlinkLed blinkLeds[2] =
60+
{
61+
{ BLINK_PORT_NUMBER, BLINK_PIN_NUMBER_GREEN, BLINK_ACTIVE_LOW },
62+
{ BLINK_PORT_NUMBER, BLINK_PIN_NUMBER_RED, BLINK_ACTIVE_LOW },
63+
};
64+
65+
#else
66+
67+
#warning "Unknown board, assume PA5, active high."
68+
69+
#define BLINK_PORT_NUMBER (0)
70+
#define BLINK_PIN_NUMBER (5)
71+
#define BLINK_ACTIVE_LOW (false)
72+
73+
BlinkLed blinkLeds[1] =
74+
{
75+
{ BLINK_PORT_NUMBER, BLINK_PIN_NUMBER, BLINK_ACTIVE_LOW },
76+
};
77+
78+
#endif
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
timer_start();
3+
4+
blink_led_init();
5+
6+
uint32_t seconds = 0;
7+
8+
// Infinite loop
9+
while (1)
10+
{
11+
blink_led_on();
12+
timer_sleep(seconds == 0 ? TIMER_FREQUENCY_HZ : BLINK_ON_TICKS);
13+
14+
blink_led_off();
15+
timer_sleep(BLINK_OFF_TICKS);
16+
17+
++seconds;
18+
// Count seconds on the trace device.
19+
trace_printf("Second %u\n", seconds);
20+
}
21+
// Infinite loop, never return.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
2+
Timer timer;
3+
timer.start ();
4+
5+
// Perform all necessary initialisations for the LEDs.
6+
for (size_t i = 0; i < (sizeof(blinkLeds) / sizeof(blinkLeds[0])); ++i)
7+
{
8+
blinkLeds[i].powerUp ();
9+
}
10+
11+
uint32_t seconds = 0;
12+
13+
for (size_t i = 0; i < (sizeof(blinkLeds) / sizeof(blinkLeds[0])); ++i)
14+
{
15+
blinkLeds[i].turnOn ();
16+
}
17+
18+
// First second is long.
19+
timer.sleep (Timer::FREQUENCY_HZ);
20+
21+
for (size_t i = 0; i < (sizeof(blinkLeds) / sizeof(blinkLeds[0])); ++i)
22+
{
23+
blinkLeds[i].turnOff ();
24+
}
25+
26+
timer.sleep (BLINK_OFF_TICKS);
27+
28+
++seconds;
29+
trace_printf ("Second %u\n", seconds);
30+
31+
if ((sizeof(blinkLeds) / sizeof(blinkLeds[0])) > 1)
32+
{
33+
// Blink individual LEDs.
34+
for (size_t i = 0; i < (sizeof(blinkLeds) / sizeof(blinkLeds[0])); ++i)
35+
{
36+
blinkLeds[i].turnOn ();
37+
timer.sleep (BLINK_ON_TICKS);
38+
39+
blinkLeds[i].turnOff ();
40+
timer.sleep (BLINK_OFF_TICKS);
41+
42+
++seconds;
43+
trace_printf ("Second %u\n", seconds);
44+
}
45+
46+
// Blink binary.
47+
while (1)
48+
{
49+
for (size_t l = 0; l < (sizeof(blinkLeds) / sizeof(blinkLeds[0]));
50+
++l)
51+
{
52+
blinkLeds[l].toggle ();
53+
if (blinkLeds[l].isOn ())
54+
{
55+
break;
56+
}
57+
}
58+
timer.sleep (Timer::FREQUENCY_HZ);
59+
60+
++seconds;
61+
trace_printf ("Second %u\n", seconds);
62+
}
63+
// Infinite loop, never return.
64+
}
65+
else
66+
{
67+
while (1)
68+
{
69+
blinkLeds[0].turnOn ();
70+
timer.sleep (BLINK_ON_TICKS);
71+
72+
blinkLeds[0].turnOff ();
73+
timer.sleep (BLINK_OFF_TICKS);
74+
75+
++seconds;
76+
trace_printf ("Second %u\n", seconds);
77+
}
78+
// Infinite loop, never return.
79+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
timer_start();
3+
4+
blink_led_init();
5+
6+
uint32_t seconds = 0;
7+
8+
// Infinite loop
9+
while (1)
10+
{
11+
blink_led_on();
12+
timer_sleep(seconds == 0 ? TIMER_FREQUENCY_HZ : BLINK_ON_TICKS);
13+
14+
blink_led_off();
15+
timer_sleep(BLINK_OFF_TICKS);
16+
17+
++seconds;
18+
// Count seconds on the trace device.
19+
trace_printf("Second %u\n", seconds);
20+
}
21+
// Infinite loop, never return.

0 commit comments

Comments
 (0)