Skip to content

Commit 9cf8008

Browse files
committed
Merge branch 'feature/diegolhendrix/sdo-sample' of https://github.com/RIT-EVT/EVT-core into feature/diegolhendrix/sdo-sample
2 parents 3587bce + 59806c1 commit 9cf8008

39 files changed

Lines changed: 1432 additions & 492 deletions

CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ if(COMPDEFS MATCHES "(.*)STM32F3xx(.*)")
6060
src/core/io/platform/f3xx/UARTf3xx.cpp
6161
src/core/io/platform/f3xx/SPIf3xx.cpp
6262
src/core/dev/platform/f3xx/IWDGf3xx.cpp
63-
src/core/dev/platform/f3xx/RTCf3xx.cpp)
63+
src/core/dev/platform/f3xx/RTCf3xx.cpp
64+
)
6465
elseif(COMPDEFS MATCHES "(.*)STM32F4xx(.*)")
6566
target_sources(${PROJECT_NAME} PRIVATE
6667
src/core/utils/platform/f4xx/timef4xx.cpp

include/core/dev/MCUTimer.hpp

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,48 +9,53 @@
99
#endif
1010

1111
namespace core::dev {
12-
1312
/**
1413
* Enum for all the hardware timers available on the current MCU
1514
*/
1615
enum class MCUTimer {
1716
#if defined(STM32F302x8)
1817
Timer1,
1918
Timer2,
20-
Timer6,
2119
Timer15,
2220
Timer16,
2321
Timer17,
2422
#elif defined(STM32F334x8)
2523
Timer1,
2624
Timer2,
2725
Timer3,
28-
Timer6,
29-
Timer7,
3026
Timer15,
3127
Timer16,
3228
Timer17,
3329
#elif defined(STM32F446xx)
30+
Timer1,
3431
Timer2,
3532
Timer3,
3633
Timer4,
3734
Timer5,
35+
Timer8,
3836
Timer9,
3937
Timer10,
4038
Timer11,
4139
Timer12,
4240
Timer13,
4341
Timer14,
4442
#endif
43+
None
4544
};
4645

4746
/**
4847
* Gets the corresponding HAL TIM_TypeDef* for each MCUTimer
48+
* This function is inlined because of a couple of reasons.
49+
* 1. It is in a header file, and functions in header files should be inlined.
50+
* https://clang.llvm.org/extra/clang-tidy/checks/misc/definitions-in-headers.html
51+
* 2. When this function is not inlined, F3 targets will not build. This is because PWMF3xx requires its own
52+
* `timerInstance` function that requires an MCUTimer import. Since manager.hpp also includes MCUTimer.hpp, and has
53+
* generally broken imports (see [FWT-255] https://rit-evt.atlassian.net/browse/FWT-255).
4954
*
5055
* @param mcuTimer MCUTimer of which to get the HAL equivalent
5156
* @return HAL equivalent of mcuTimer
5257
*/
53-
TIM_TypeDef* getTIM(MCUTimer mcuTimer) {
58+
inline TIM_TypeDef* getTIM(const MCUTimer mcuTimer) {
5459
TIM_TypeDef* timPeriph;
5560
switch (mcuTimer) {
5661
#if defined(STM32F302x8)
@@ -60,9 +65,6 @@ TIM_TypeDef* getTIM(MCUTimer mcuTimer) {
6065
case MCUTimer::Timer2:
6166
timPeriph = TIM2;
6267
break;
63-
case MCUTimer::Timer6:
64-
timPeriph = TIM6;
65-
break;
6668
case MCUTimer::Timer15:
6769
timPeriph = TIM15;
6870
break;
@@ -72,6 +74,9 @@ TIM_TypeDef* getTIM(MCUTimer mcuTimer) {
7274
case MCUTimer::Timer17:
7375
timPeriph = TIM17;
7476
break;
77+
default:
78+
timPeriph = TIM1;
79+
break;
7580
#elif defined(STM32F334x8)
7681
case MCUTimer::Timer1:
7782
timPeriph = TIM1;
@@ -82,12 +87,6 @@ TIM_TypeDef* getTIM(MCUTimer mcuTimer) {
8287
case MCUTimer::Timer3:
8388
timPeriph = TIM3;
8489
break;
85-
case MCUTimer::Timer6:
86-
timPeriph = TIM6;
87-
break;
88-
case MCUTimer::Timer7:
89-
timPeriph = TIM7;
90-
break;
9190
case MCUTimer::Timer15:
9291
timPeriph = TIM15;
9392
break;
@@ -97,11 +96,13 @@ TIM_TypeDef* getTIM(MCUTimer mcuTimer) {
9796
case MCUTimer::Timer17:
9897
timPeriph = TIM17;
9998
break;
99+
default:
100+
timPeriph = TIM1;
101+
break;
100102
#elif defined(STM32F446xx)
101-
/*
102-
* Timers 1 and 8 are advanced timers
103-
* so they are not included in this switch statement
104-
*/
103+
case MCUTimer::Timer1:
104+
timPeriph = TIM1;
105+
break;
105106
case MCUTimer::Timer2:
106107
timPeriph = TIM2;
107108
break;
@@ -114,6 +115,9 @@ TIM_TypeDef* getTIM(MCUTimer mcuTimer) {
114115
case MCUTimer::Timer5:
115116
timPeriph = TIM5;
116117
break;
118+
case MCUTimer::Timer8:
119+
timPeriph = TIM8;
120+
break;
117121
case MCUTimer::Timer9:
118122
timPeriph = TIM9;
119123
break;
@@ -132,6 +136,9 @@ TIM_TypeDef* getTIM(MCUTimer mcuTimer) {
132136
case MCUTimer::Timer14:
133137
timPeriph = TIM14;
134138
break;
139+
default:
140+
timPeriph = TIM1;
141+
break;
135142
#endif
136143
}
137144

include/core/dev/RTCTimer.hpp

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class RTCTimer : public Timer {
1717
*
1818
* @param r
1919
*/
20-
RTCTimer(RTC& r);
20+
explicit RTCTimer(RTC& r);
2121

2222
/**
2323
* Create instance of RTCTimer.
@@ -31,49 +31,57 @@ class RTCTimer : public Timer {
3131
* not implemented.
3232
*
3333
* @param[in] irqHandler The IRQ Handler function pointer. Sets a new interrupt handler function
34+
* @param[in] context The context used by the irqHandler.
3435
*/
35-
void startTimer(void (*irqHandler)(void* htim)) override {}
36+
void startTimer(void (*irqHandler)(void* context, void* htim), void* context) override {}
3637

3738
void startTimer() override;
3839

3940
void stopTimer() override;
4041

4142
void reloadTimer() override;
4243

43-
void setPeriod(uint32_t clock) override;
44+
/**
45+
* Set the period for PWM.
46+
* Note: This does not match the dev::Timer set period, since the RTCTimer does not accept a different period.
47+
* This function will hide the definition from the superclass. So you can safely ignore the warning.
48+
* @param[in] period The period for the RTC timer.
49+
* @param[in] clockPrescaler UNUSED DO NOT SET.
50+
*/
51+
void setPeriod(uint32_t period, uint32_t clockPrescaler = AUTO_PRESCALER);
4452

4553
/**
4654
* Gets the time since the RTC clock began in seconds
4755
*
4856
* @return the time since the RTC clock began
4957
*/
50-
uint32_t getTime();
58+
[[nodiscard]] uint32_t getTime() const;
5159

5260
/**
5361
* Gets whether the timer has gone off
5462
*
5563
* @return whether the time has gone off
5664
*/
57-
bool hasGoneOff();
65+
[[nodiscard]] bool hasGoneOff() const;
5866

5967
private:
6068
/** Instance of on-board*/
6169
RTC& rtc;
6270

6371
/**
64-
* The amount of seconds that have elapsed while the timer is running.
72+
* The number of seconds that have elapsed while the timer is running.
6573
* Only updates when stopTimer() is called.
6674
*/
67-
uint32_t time;
75+
uint32_t time = 0;
6876

6977
/** The amount of time it takes the timer to go off in SECONDS */
70-
uint32_t clockPeriod;
78+
uint32_t clockPeriod = 0;
7179

7280
/** The epoc time the clock started */
73-
uint32_t startTime;
81+
uint32_t startTime = 0;
7482

7583
/** true if timer has been stopped */
76-
bool bTimerStopped;
84+
bool bTimerStopped = true;
7785
};
7886

7987
} // namespace core::dev

include/core/dev/Timer.hpp

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,29 @@
33

44
#include <cstdint>
55

6-
namespace core::dev {
7-
86
/**
9-
* This class will represent an internal general purpose timer device for the STM32.
10-
* It is capable of triggering interrupts with a given frequency
7+
* If AUTO_PRESCALER is sent as a prescaler the timer will calculate its own Prescaler in the setPeriod function.
8+
* The automatic prescaler is calculated by dividing the system clock speed by 1000, then subtracting 1.
9+
*
10+
* Yes, this is a negative in an uint32_t, but it ends up just causing an underflow, and making it the max of an
11+
* uint32_t.
1112
*/
13+
#define AUTO_PRESCALER uint32_t(-1)
14+
15+
namespace core::dev {
1216
class Timer {
1317
public:
18+
/**
19+
* Virtual destructor required by C++.
20+
*/
21+
virtual ~Timer() = default;
22+
1423
/**
1524
* Starts the given timer and registers the given interrupt pointer to trigger when the timer overflows
1625
* @param[in] irqHandler The IRQ Handler function pointer. Sets a new interrupt handler function
26+
* @param[in] context The context to be sent to the irqHandler when it is called.
1727
*/
18-
virtual void startTimer(void (*irqHandler)(void* htim)) = 0;
28+
virtual void startTimer(void (*irqHandler)(void* context, void* htim), void* context) = 0;
1929

2030
/**
2131
* Starts the given timer using the IRQ Handler already assigned to that timer.
@@ -36,11 +46,14 @@ class Timer {
3646
* Set the clock period for the timer. Will stop the timer, re-initialize the device with the updated period.
3747
* You must call startTimer again to continue timer operation.
3848
*
39-
* @param[in] clockPeriod the clock period in ms. An interrupt will be triggered at that frequency.
49+
* @param[in] clockPeriod the clock period in ticks (ms when using AUTO_PRESCALER). An interrupt will be triggered
50+
* at that frequency.
51+
* @param [in] clockPrescaler the prescaler used by the timer. Divides the system clock frequency to get it within
52+
* an acceptable range for use with the required period. If set to @ref AUTO_PRESCALER, the function implementation
53+
* will calculate its own prescaler.
4054
*/
41-
virtual void setPeriod(uint32_t clockPeriod) = 0;
55+
virtual void setPeriod(uint32_t clockPeriod, uint32_t clockPrescaler = AUTO_PRESCALER) = 0;
4256
};
43-
4457
} // namespace core::dev
4558

4659
#endif // EVT_TIMER_HPP

0 commit comments

Comments
 (0)