|
35 | 35 | /* Private Variables */
|
36 | 36 | timerObj_t *HardwareTimer_Handle[TIMER_NUM] = {NULL};
|
37 | 37 |
|
| 38 | +/** |
| 39 | + * @brief HardwareTimer constructor: make uninitialized timer |
| 40 | + * Before calling any methods, call setup to select and setup |
| 41 | + * the timer to be used. |
| 42 | + * @retval None |
| 43 | + */ |
| 44 | +HardwareTimer::HardwareTimer() { |
| 45 | + _timerObj.handle.Instance = nullptr; |
| 46 | +} |
| 47 | + |
38 | 48 | /**
|
39 | 49 | * @brief HardwareTimer constructor: set default configuration values
|
| 50 | + * The timer will be usable directly, there is no need to call |
| 51 | + * setup(). Using this constructor is not recommended for |
| 52 | + * global variables that are automatically initalized at |
| 53 | + * startup, since this will happen to early to report any |
| 54 | + * errors. Better use the argumentless constructor and call the |
| 55 | + * setup() method during initialization later. |
40 | 56 | * @param Timer instance ex: TIM1, ...
|
41 | 57 | * @retval None
|
42 | 58 | */
|
43 |
| -HardwareTimer::HardwareTimer(TIM_TypeDef *instance) |
| 59 | +HardwareTimer::HardwareTimer(TIM_TypeDef *instance) { |
| 60 | + _timerObj.handle.Instance = nullptr; |
| 61 | + setup(instance); |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * @brief HardwareTimer setup: configuration values. Must be called |
| 66 | + * exactly once before any other methods, except when an instance is |
| 67 | + * passed to the constructor. |
| 68 | + * @param Timer instance ex: TIM1, ... |
| 69 | + * @retval None |
| 70 | + */ |
| 71 | +void HardwareTimer::setup(TIM_TypeDef *instance) |
44 | 72 | {
|
45 | 73 | uint32_t index = get_timer_index(instance);
|
46 | 74 | if (index == UNKNOWN_TIMER) {
|
47 | 75 | Error_Handler();
|
48 | 76 | }
|
49 | 77 |
|
| 78 | + // Already initialized? |
| 79 | + if (_timerObj.handle.Instance) |
| 80 | + Error_Handler(); |
| 81 | + |
50 | 82 | HardwareTimer_Handle[index] = &_timerObj;
|
51 | 83 |
|
52 | 84 | _timerObj.handle.Instance = instance;
|
|
0 commit comments