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