Skip to content

Commit a9aa6f0

Browse files
Default config template for getTim
I decided that a default config should be used in the template methods. This makes this PR a drop in change for previous projects, and will reduce the overhead needed to merge it into EVT-Core. Signed-off-by: Taylor Lineman <git@actuallytaylor.com>
1 parent f3d32c7 commit a9aa6f0

10 files changed

Lines changed: 147 additions & 68 deletions

File tree

include/core/dev/platform/f3xx/Timerf3xx.hpp

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ namespace core::dev {
1414
*/
1515
class TimerF3xx : public Timer {
1616
public:
17+
/**
18+
* The default configuration for an F3 timer.
19+
*/
20+
static constexpr TimerConfiguration_t defaultConfig = {
21+
TIM_COUNTERMODE_UP,
22+
TIM_CLOCKDIVISION_DIV1,
23+
TIM_AUTORELOAD_PRELOAD_ENABLE,
24+
TIM_CLOCKSOURCE_INTERNAL,
25+
TIM_TRGO_RESET,
26+
TIM_MASTERSLAVEMODE_DISABLE
27+
};
28+
1729
/**
1830
* Will initialize the timer device on the STM with the given period and the given IRQ Handler
1931
* that triggers with the given period. Starts the timer
@@ -24,14 +36,11 @@ class TimerF3xx : public Timer {
2436
* @param[in] timerPeripheral The timer to use
2537
* @param[in] clockPeriod the clock period in ticks (ms when using AUTO_PRESCALER). An interrupt will be triggered
2638
* at this frequency.
27-
* @param[in] configuration the configuration tells the timer how to configure itself. Defaults to @ref
28-
* TimerF3xx::defaultConfig.
39+
* @param[in] configuration the configuration tells the timer how to configure itself. Defaults to @ref TimerF3xx::defaultConfig.
2940
* @param[in] clockPrescaler the prescaler that the clock will use. If clockPrescaler is set to @ref AUTO_PRESCALER,
3041
* this function will calculate its own prescaler value.
3142
*/
32-
explicit TimerF3xx(TIM_TypeDef* timerPeripheral, uint32_t clockPeriod,
33-
const TimerConfiguration_t& configuration = defaultConfig,
34-
uint32_t clockPrescaler = AUTO_PRESCALER);
43+
explicit TimerF3xx(TIM_TypeDef* timerPeripheral, uint32_t clockPeriod, const TimerConfiguration_t& configuration = defaultConfig, uint32_t clockPrescaler = AUTO_PRESCALER);
3544

3645
/**
3746
* Starts the given timer and registers the given interrupt pointer to trigger when the timer overflows
@@ -92,14 +101,6 @@ class TimerF3xx : public Timer {
92101
* this function will calculate its own prescaler value.
93102
*/
94103
void initTimer(TIM_TypeDef* timerPeripheral, uint32_t clockPeriod, uint32_t clockPrescaler);
95-
96-
private:
97-
static constexpr TimerConfiguration_t defaultConfig = {TIM_COUNTERMODE_UP,
98-
TIM_CLOCKDIVISION_DIV1,
99-
TIM_AUTORELOAD_PRELOAD_ENABLE,
100-
TIM_CLOCKSOURCE_INTERNAL,
101-
TIM_TRGO_RESET,
102-
TIM_MASTERSLAVEMODE_DISABLE};
103104
};
104105

105106
} // namespace core::dev

include/core/dev/platform/f4xx/Timerf4xx.hpp

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,18 @@ namespace core::dev {
1414
*/
1515
class TimerF4xx final : public Timer {
1616
public:
17+
/**
18+
* A default configuration for an F4 timer.
19+
*/
20+
static constexpr TimerConfiguration_t defaultConfig = {
21+
TIM_COUNTERMODE_UP,
22+
TIM_CLOCKDIVISION_DIV1,
23+
TIM_AUTORELOAD_PRELOAD_ENABLE,
24+
TIM_CLOCKSOURCE_INTERNAL,
25+
TIM_TRGO_RESET,
26+
TIM_MASTERSLAVEMODE_DISABLE
27+
};
28+
1729
~TimerF4xx() override;
1830

1931
/**
@@ -26,14 +38,12 @@ class TimerF4xx final : public Timer {
2638
* @param[in] timerPeripheral the timer peripheral to use.
2739
* @param[in] clockPeriod the clock period in ticks (ms when using AUTO_PRESCALER). An interrupt will be triggered
2840
* at this frequency.
29-
* @param[in] configuration the configuration tells the timer how to configure itself. Defaults to @ref
30-
* TimerF4xx::defaultConfig.
41+
* @param[in] configuration the configuration tells the timer how to configure itself. Defaults to @ref TimerF4xx::defaultConfig.
3142
* @param[in] clockPrescaler the prescaler that the clock will use. If clockPrescaler is set to @ref AUTO_PRESCALER,
3243
* this function will calculate its own prescaler value.
3344
*/
34-
explicit TimerF4xx(TIM_TypeDef* timerPeripheral, uint32_t clockPeriod,
35-
const TimerConfiguration_t& configuration = defaultConfig,
36-
uint32_t clockPrescaler = AUTO_PRESCALER);
45+
explicit TimerF4xx(TIM_TypeDef* timerPeripheral, uint32_t clockPeriod, const TimerConfiguration_t& configuration = defaultConfig,
46+
uint32_t clockPrescaler = AUTO_PRESCALER);
3747

3848
/**
3949
* Starts the given timer and registers the given interrupt pointer to trigger when the timer overflows
@@ -67,7 +77,6 @@ class TimerF4xx final : public Timer {
6777
* own prescaler.
6878
*/
6979
void setPeriod(uint32_t clockPeriod, uint32_t clockPrescaler = AUTO_PRESCALER) override;
70-
7180
protected:
7281
/**
7382
* Pointer to the halTimer struct stored in the global array in Timerf4xx.cpp
@@ -94,14 +103,6 @@ class TimerF4xx final : public Timer {
94103
* this function will calculate its own prescaler value.
95104
*/
96105
void initTimer(TIM_TypeDef* timerPeripheral, uint32_t clockPeriod, uint32_t clockPrescaler);
97-
98-
private:
99-
static constexpr TimerConfiguration_t defaultConfig = {TIM_COUNTERMODE_UP,
100-
TIM_CLOCKDIVISION_DIV1,
101-
TIM_AUTORELOAD_PRELOAD_ENABLE,
102-
TIM_CLOCKSOURCE_INTERNAL,
103-
TIM_TRGO_RESET,
104-
TIM_MASTERSLAVEMODE_DISABLE};
105106
};
106107
} // namespace core::dev
107108

include/core/manager.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,15 @@ RTC& getRTC() {
124124
#endif
125125

126126
#ifdef MCU_SUPPORTED
127+
/**
128+
* A verbose method for getting an EVT-Core timer. This initializer supports a timer configuration object, which allows
129+
* for configuration of the timer.
130+
*
131+
* @tparam mcuTimer The microcontroller timer that the timer will run on.
132+
* @param clockPeriod The clock period to run the timer at
133+
* @param configuration Configuration variables for the timer.
134+
* @return
135+
*/
127136
template<MCUTimer mcuTimer>
128137
Timer& getTimer(uint32_t clockPeriod, TimerConfiguration_t configuration) {
129138
#ifdef STM32F3xx
@@ -135,6 +144,25 @@ Timer& getTimer(uint32_t clockPeriod, TimerConfiguration_t configuration) {
135144
return timer;
136145
#endif
137146
}
147+
148+
/**
149+
* A simple method for getting an EVT-Core timer. This timer will be initialized with default configuration options.
150+
*
151+
* @tparam mcuTimer The microcontroller timer that the timer will run on.
152+
* @param clockPeriod The clock period to run the timer at
153+
* @return
154+
*/
155+
template<MCUTimer mcuTimer>
156+
Timer& getTimer(uint32_t clockPeriod) {
157+
#ifdef STM32F3xx
158+
static TimerF3xx timer(getTIM(mcuTimer), clockPeriod, TimerF3xx::defaultConfig);
159+
return timer;
160+
#endif
161+
#ifdef STM32F4xx
162+
static TimerF4xx timer(getTIM(mcuTimer), clockPeriod, TimerF4xx::defaultConfig);
163+
return timer;
164+
#endif
165+
}
138166
#endif
139167

140168
} // namespace core::dev

samples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ add_subdirectory(rtos)
2323
add_subdirectory(thermistor)
2424
add_subdirectory(timer)
2525
add_subdirectory(spi)
26+
add_subdirectory(timer_configuration)

samples/canopen/canopen_rpdo/main.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -59,16 +59,8 @@ int main() {
5959
// Initialize system
6060
core::platform::init();
6161

62-
// Initialize a configuration object for the timer.
63-
dev::TimerConfiguration_t configuration = {TIM_COUNTERMODE_UP,
64-
TIM_CLOCKDIVISION_DIV1,
65-
TIM_AUTORELOAD_PRELOAD_ENABLE,
66-
TIM_CLOCKSOURCE_INTERNAL,
67-
TIM_TRGO_RESET,
68-
TIM_MASTERSLAVEMODE_DISABLE};
69-
7062
// Initialize the timer
71-
dev::Timer& timer = dev::getTimer<dev::MCUTimer::Timer2>(100, configuration);
63+
dev::Timer& timer = dev::getTimer<dev::MCUTimer::Timer2>(100);
7264

7365
// create the RPDO node
7466
RPDOCanNode testCanNode;

samples/canopen/canopen_sample/main.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,8 @@ int main() {
5050
// Initialize system
5151
core::platform::init();
5252

53-
// Initialize a configuration object for the timer.
54-
dev::TimerConfiguration_t configuration = {TIM_COUNTERMODE_UP,
55-
TIM_CLOCKDIVISION_DIV1,
56-
TIM_AUTORELOAD_PRELOAD_ENABLE,
57-
TIM_CLOCKSOURCE_INTERNAL,
58-
TIM_TRGO_RESET,
59-
TIM_MASTERSLAVEMODE_DISABLE};
60-
6153
// Initialize the timer
62-
dev::Timer& timer = dev::getTimer<dev::MCUTimer::Timer2>(100, configuration);
54+
dev::Timer& timer = dev::getTimer<dev::MCUTimer::Timer2>(100);
6355

6456
// UART for testing
6557
io::UART& uart = io::getUART<io::Pin::UART_TX, io::Pin::UART_RX>(9600);

samples/canopen/canopen_tpdo/main.cpp

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,16 +71,8 @@ int main() {
7171
// create the TPDO node
7272
TPDOCanNode testCanNode;
7373

74-
// Initialize a configuration object for the timer.
75-
dev::TimerConfiguration_t configuration = {TIM_COUNTERMODE_UP,
76-
TIM_CLOCKDIVISION_DIV1,
77-
TIM_AUTORELOAD_PRELOAD_ENABLE,
78-
TIM_CLOCKSOURCE_INTERNAL,
79-
TIM_TRGO_RESET,
80-
TIM_MASTERSLAVEMODE_DISABLE};
81-
8274
// Initialize the timer
83-
dev::Timer& timer = dev::getTimer<dev::MCUTimer::Timer2>(100, configuration);
75+
dev::Timer& timer = dev::getTimer<dev::MCUTimer::Timer2>(100);
8476

8577
///////////////////////////////////////////////////////////////////////////
8678
// Setup CAN configuration, this handles making drivers, applying settings.

samples/timer/main.cpp

Lines changed: 5 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -43,25 +43,16 @@ int main() {
4343
interruptGPIOStopStart = &io::getGPIO<io::Pin::PC_2>(io::GPIO::Direction::OUTPUT);
4444
reloadGPIO = &io::getGPIO<io::Pin::PC_0>(io::GPIO::Direction::OUTPUT);
4545

46-
// Initialize a configuration object for the timer.
47-
dev::TimerConfiguration_t configuration = {TIM_COUNTERMODE_UP,
48-
TIM_CLOCKDIVISION_DIV1,
49-
TIM_AUTORELOAD_PRELOAD_ENABLE,
50-
TIM_CLOCKSOURCE_INTERNAL,
51-
TIM_TRGO_RESET,
52-
TIM_MASTERSLAVEMODE_DISABLE};
53-
5446
// Set up the Timer
55-
dev::Timer& sampleTimer1 = dev::getTimer<dev::MCUTimer::Timer2>(1000, configuration);
47+
dev::Timer& sampleTimer1 = dev::getTimer<dev::MCUTimer::Timer2>(1000);
5648

5749
#ifdef STM32F4xx
5850
// F4xx does not support Timers 15 & 16, change them to Timer11 & Timer12
59-
dev::Timer& sampleTimer2 = dev::getTimer<dev::MCUTimer::Timer11>(200, configuration);
60-
dev::Timer& sampleTimer3 = dev::getTimer<dev::MCUTimer::Timer12>(200, configuration);
51+
dev::Timer& sampleTimer2 = dev::getTimer<dev::MCUTimer::Timer11>(200);
52+
dev::Timer& sampleTimer3 = dev::getTimer<dev::MCUTimer::Timer12>(200);
6153
#else
62-
dev::Timer& sampleTimer2 = dev::getTimer<dev::MCUTimer::Timer15>(1000, configuration);
63-
dev::Timer& sampleTimer3 = dev::getTimer<dev::MCUTimer::Timer16>(1000, configuration);
64-
54+
dev::Timer& sampleTimer2 = dev::getTimer<dev::MCUTimer::Timer15>(1000);
55+
dev::Timer& sampleTimer3 = dev::getTimer<dev::MCUTimer::Timer16>(1000);
6556
#endif
6657

6758
sampleTimer1.startTimer(timer2IRQHandler);
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
include(${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/evt-core_build.cmake)
2+
3+
make_exe(timer_configuration main.cpp)
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/**
2+
* This sample will demo the the configuration object of a timer.
3+
*/
4+
#include <core/io/UART.hpp>
5+
#include <core/io/pin.hpp>
6+
#include <core/manager.hpp>
7+
#include <core/utils/time.hpp>
8+
9+
namespace io = core::io;
10+
namespace dev = core::dev;
11+
12+
io::GPIO* ledGPIO;
13+
io::GPIO* interruptGPIO2Hz;
14+
io::GPIO* interruptGPIOStopStart;
15+
io::GPIO* reloadGPIO;
16+
17+
void timer2IRQHandler(void* htim) {
18+
io::GPIO::State state = ledGPIO->readPin();
19+
io::GPIO::State toggleState = state == io::GPIO::State::HIGH ? io::GPIO::State::LOW : io::GPIO::State::HIGH;
20+
ledGPIO->writePin(toggleState);
21+
interruptGPIO2Hz->writePin(toggleState);
22+
}
23+
24+
void timer15IRQHandler(void* htim) {
25+
io::GPIO::State state = interruptGPIOStopStart->readPin();
26+
io::GPIO::State toggleState = state == io::GPIO::State::HIGH ? io::GPIO::State::LOW : io::GPIO::State::HIGH;
27+
interruptGPIOStopStart->writePin(toggleState);
28+
}
29+
30+
void timer16IRQHandler(void* htim) {
31+
io::GPIO::State state = reloadGPIO->readPin();
32+
io::GPIO::State toggleState = state == io::GPIO::State::HIGH ? io::GPIO::State::LOW : io::GPIO::State::HIGH;
33+
reloadGPIO->writePin(toggleState);
34+
}
35+
36+
int main() {
37+
// Initialize system
38+
core::platform::init();
39+
40+
// Setup GPIO
41+
ledGPIO = &io::getGPIO<io::Pin::LED>();
42+
interruptGPIO2Hz = &io::getGPIO<io::Pin::PC_3>(io::GPIO::Direction::OUTPUT);
43+
interruptGPIOStopStart = &io::getGPIO<io::Pin::PC_2>(io::GPIO::Direction::OUTPUT);
44+
reloadGPIO = &io::getGPIO<io::Pin::PC_0>(io::GPIO::Direction::OUTPUT);
45+
46+
dev::TimerConfiguration_t configuration = {
47+
TIM_COUNTERMODE_UP, // Tell the counter to count upwards towards the clock period
48+
TIM_CLOCKDIVISION_DIV1, // Used the default clock division
49+
TIM_AUTORELOAD_PRELOAD_ENABLE, // Enables the auto reload preload which makes TIMx_ARR buffered.
50+
TIM_CLOCKSOURCE_INTERNAL, // Set the clock source to be the microcontrollers internal clock.
51+
TIM_TRGO_RESET, // Tells the timer to reset whenever it enters a Master / Slave configuration. Will not be triggered or used in this configuration.
52+
TIM_MASTERSLAVEMODE_DISABLE // Disable Masster Slave Mode for the timer
53+
};
54+
55+
// Set up the Timer
56+
dev::Timer& sampleTimer1 = dev::getTimer<dev::MCUTimer::Timer2>(1000, configuration);
57+
58+
#ifdef STM32F4xx
59+
// F4xx does not support Timers 15 & 16, change them to Timer11 & Timer12
60+
dev::Timer& sampleTimer2 = dev::getTimer<dev::MCUTimer::Timer11>(200, configuration);
61+
dev::Timer& sampleTimer3 = dev::getTimer<dev::MCUTimer::Timer12>(200, configuration);
62+
#else
63+
dev::Timer& sampleTimer2 = dev::getTimer<dev::MCUTimer::Timer15>(1000, configuration);
64+
dev::Timer& sampleTimer3 = dev::getTimer<dev::MCUTimer::Timer16>(1000, configuration);
65+
#endif
66+
67+
sampleTimer1.startTimer(timer2IRQHandler);
68+
sampleTimer2.startTimer(timer15IRQHandler);
69+
sampleTimer3.startTimer(timer16IRQHandler);
70+
71+
while (1) {
72+
core::time::wait(500);
73+
sampleTimer2.stopTimer();
74+
sampleTimer3.reloadTimer();
75+
core::time::wait(500);
76+
sampleTimer2.startTimer();
77+
}
78+
}

0 commit comments

Comments
 (0)