JM9 XRCU Board 0.1.2
Libraries API Reference
Loading...
Searching...
No Matches
HardwareTimer.h
1/*
2 Copyright (c) 2017 Daniel Fekete
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21
22 Copyright (c) 2019 STMicroelectronics
23 Modified to support Arduino_Core_STM32
24*/
25
26/* Define to prevent recursive inclusion -------------------------------------*/
27#ifndef HARDWARETIMER_H_
28#define HARDWARETIMER_H_
29
30/* Includes ------------------------------------------------------------------*/
31#include "timer.h"
32#include "stm32yyxx_ll_tim.h"
33
34#if defined(HAL_TIM_MODULE_ENABLED) && !defined(HAL_TIM_MODULE_ONLY)
35
36#define TIMER_CHANNELS 4 // channel5 and channel 6 are not considered here has they don't have gpio output and they don't have interrupt
37
38typedef enum {
39 TIMER_DISABLED, // == TIM_OCMODE_TIMING no output, useful for only-interrupt
40 // Output Compare
41 TIMER_OUTPUT_COMPARE, // == Obsolete, use TIMER_DISABLED instead. Kept for compatibility reason
42 TIMER_OUTPUT_COMPARE_ACTIVE, // == TIM_OCMODE_ACTIVE pin is set high when counter == channel compare
43 TIMER_OUTPUT_COMPARE_INACTIVE, // == TIM_OCMODE_INACTIVE pin is set low when counter == channel compare
44 TIMER_OUTPUT_COMPARE_TOGGLE, // == TIM_OCMODE_TOGGLE pin toggles when counter == channel compare
45 TIMER_OUTPUT_COMPARE_PWM1, // == TIM_OCMODE_PWM1 pin high when counter < channel compare, low otherwise
46 TIMER_OUTPUT_COMPARE_PWM2, // == TIM_OCMODE_PWM2 pin low when counter < channel compare, high otherwise
47 TIMER_OUTPUT_COMPARE_FORCED_ACTIVE, // == TIM_OCMODE_FORCED_ACTIVE pin always high
48 TIMER_OUTPUT_COMPARE_FORCED_INACTIVE, // == TIM_OCMODE_FORCED_INACTIVE pin always low
49
50 //Input capture
51 TIMER_INPUT_CAPTURE_RISING, // == TIM_INPUTCHANNELPOLARITY_RISING
52 TIMER_INPUT_CAPTURE_FALLING, // == TIM_INPUTCHANNELPOLARITY_FALLING
53 TIMER_INPUT_CAPTURE_BOTHEDGE, // == TIM_INPUTCHANNELPOLARITY_BOTHEDGE
54
55 // Used 2 channels for a single pin. One channel in TIM_INPUTCHANNELPOLARITY_RISING another channel in TIM_INPUTCHANNELPOLARITY_FALLING.
56 // Channels must be used by pair: CH1 with CH2, or CH3 with CH4
57 // This mode is very useful for Frequency and Dutycycle measurement
58 TIMER_INPUT_FREQ_DUTY_MEASUREMENT,
59
60 TIMER_NOT_USED = 0xFFFF // This must be the last item of this enum
61} TimerModes_t;
62
63typedef enum {
64 TICK_FORMAT, // default
65 MICROSEC_FORMAT,
66 HERTZ_FORMAT,
67} TimerFormat_t;
68
69typedef enum {
70 RESOLUTION_1B_COMPARE_FORMAT = 1, // used for Dutycycle: [0 .. 1]
71 RESOLUTION_2B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 3]
72 RESOLUTION_3B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 7]
73 RESOLUTION_4B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 15]
74 RESOLUTION_5B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 31]
75 RESOLUTION_6B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 63]
76 RESOLUTION_7B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 127]
77 RESOLUTION_8B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 255]
78 RESOLUTION_9B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 511]
79 RESOLUTION_10B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 1023]
80 RESOLUTION_11B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 2047]
81 RESOLUTION_12B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 4095]
82 RESOLUTION_13B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 8191]
83 RESOLUTION_14B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 16383]
84 RESOLUTION_15B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 32767]
85 RESOLUTION_16B_COMPARE_FORMAT, // used for Dutycycle: [0 .. 65535]
86
87 TICK_COMPARE_FORMAT = 0x80, // default
88 MICROSEC_COMPARE_FORMAT,
89 HERTZ_COMPARE_FORMAT,
90 PERCENT_COMPARE_FORMAT, // used for Dutycycle
91} TimerCompareFormat_t;
92
93typedef enum {
94 FILTER_NONE = 0, // No filter
95 FILTER_CKINT_N2, // Sampling rate is same as clock interrupt, n=2 events
96 FILTER_CKINT_N4, // Sampling rate is same as clock interrupt, n=4 events
97 FILTER_CKINT_N8, // Sampling rate is same as clock interrupt, n=8 events
98 FILTER_DTS2_N6, // Sampling rate is DTS/2, n=6 events
99 FILTER_DTS2_N8, // Sampling rate is DTS/2, n=8 events
100 FILTER_DTS4_N6, // Sampling rate is DTS/4, n=6 events
101 FILTER_DTS4_N8, // Sampling rate is DTS/4, n=8 events
102 FILTER_DTS8_N6, // Sampling rate is DTS/8, n=6 events
103 FILTER_DTS8_N8, // Sampling rate is DTS/8, n=8 events
104 FILTER_DTS16_N5, // Sampling rate is DTS/16, n=5 events
105 FILTER_DTS16_N6, // Sampling rate is DTS/16, n=6 events
106 FILTER_DTS16_N8, // Sampling rate is DTS/16, n=8 events
107 FILTER_DTS32_N5, // Sampling rate is DTS/32, n=5 events
108 FILTER_DTS32_N6, // Sampling rate is DTS/32, n=6 events
109 FILTER_DTS32_N8, // Sampling rate is DTS/32, n=8 events
110} ChannelInputFilter_t;
111
112#ifdef __cplusplus
113
114#include <functional>
115using callback_function_t = std::function<void(void)>;
116
117/* Class --------------------------------------------------------*/
118class HardwareTimer {
119 public:
120 HardwareTimer();
121 HardwareTimer(TIM_TypeDef *instance);
122 ~HardwareTimer(); // destructor
123
124 void setup(TIM_TypeDef *instance); // Setup, only needed if no instance was passed to the constructor
125
126 void pause(void); // Pause counter and all output channels
127 void pauseChannel(uint32_t channel); // Timer is still running but channel (output and interrupt) is disabled
128 void resume(void); // Resume counter and all output channels
129 void resumeChannel(uint32_t channel); // Resume only one channel
130
131 void setPrescaleFactor(uint32_t prescaler); // set prescaler register (which is factor value - 1)
132 uint32_t getPrescaleFactor();
133
134 void setOverflow(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set AutoReload register depending on format provided
135 uint32_t getOverflow(TimerFormat_t format = TICK_FORMAT); // return overflow depending on format provided
136
137 void setPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback = nullptr, callback_function_t CompareCallback = nullptr); // Set all in one command freq in HZ, Duty in percentage. Including both interrupt.
138 void setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, callback_function_t PeriodCallback = nullptr, callback_function_t CompareCallback = nullptr);
139
140 void setCount(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set timer counter to value 'val' depending on format provided
141 uint32_t getCount(TimerFormat_t format = TICK_FORMAT); // return current counter value of timer depending on format provided
142
143 void setMode(uint32_t channel, TimerModes_t mode, PinName pin = NC, ChannelInputFilter_t filter = FILTER_NONE); // Configure timer channel with specified mode on specified pin if available
144 void setMode(uint32_t channel, TimerModes_t mode, uint32_t pin, ChannelInputFilter_t filter = FILTER_NONE);
145
146 TimerModes_t getMode(uint32_t channel); // Retrieve configured mode
147
148 void setPreloadEnable(bool value); // Configure overflow preload enable setting
149
150 uint32_t getCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided
151 void setCaptureCompare(uint32_t channel, uint32_t compare, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // set Compare register value of specified channel depending on format provided
152
153 void setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority); // set interrupt priority
154
155 //Add interrupt to period update
156 void attachInterrupt(callback_function_t callback); // Attach interrupt callback which will be called upon update event (timer rollover)
157 void detachInterrupt(); // remove interrupt callback which was attached to update event
158 bool hasInterrupt(); //returns true if a timer rollover interrupt has already been set
159 //Add interrupt to capture/compare channel
160 void attachInterrupt(uint32_t channel, callback_function_t callback); // Attach interrupt callback which will be called upon compare match event of specified channel
161 void detachInterrupt(uint32_t channel); // remove interrupt callback which was attached to compare match event of specified channel
162 bool hasInterrupt(uint32_t channel); //returns true if an interrupt has already been set on the channel compare match
163 void timerHandleDeinit(); // Timer deinitialization
164
165 // Refresh() is useful while timer is running after some registers update
166 void refresh(void); // Generate update event to force all registers (Autoreload, prescaler, compare) to be taken into account
167
168 uint32_t getTimerClkFreq(); // return timer clock frequency in Hz.
169
170 static void captureCompareCallback(TIM_HandleTypeDef *htim); // Generic Capture and Compare callback which will call user callback
171 static void updateCallback(TIM_HandleTypeDef *htim); // Generic Update (rollover) callback which will call user callback
172
173 void updateRegistersIfNotRunning(TIM_TypeDef *TIMx); // Take into account registers update immediately if timer is not running,
174
175 bool isRunning(); // return true if HardwareTimer is running
176 bool isRunningChannel(uint32_t channel); // return true if channel is running
177
178 // The following function(s) are available for more advanced timer options
179 TIM_HandleTypeDef *getHandle(); // return the handle address for HAL related configuration
180 int getChannel(uint32_t channel);
181 int getLLChannel(uint32_t channel);
182 int getIT(uint32_t channel);
183 int getAssociatedChannel(uint32_t channel);
184#if defined(TIM_CCER_CC1NE)
185 bool isComplementaryChannel[TIMER_CHANNELS];
186#endif
187 private:
188 TimerModes_t _ChannelMode[TIMER_CHANNELS];
189 timerObj_t _timerObj;
190 callback_function_t callbacks[1 + TIMER_CHANNELS]; //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
191};
192
193extern timerObj_t *HardwareTimer_Handle[TIMER_NUM];
194
195extern timer_index_t get_timer_index(TIM_TypeDef *htim);
196
197#endif /* __cplusplus */
198
199#endif // HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY
200#endif // HARDWARETIMER_H_