You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// Used 2 channels for a single pin. One channel in TIM_INPUTCHANNELPOLARITY_RISING another channel in TIM_INPUTCHANNELPOLARITY_FALLING.
55
+
// Channels must be used by pair: CH1 with CH2, or CH3 with CH4
56
+
// This mode is very useful for Frequency and Dutycycle measurement
57
+
TIMER_INPUT_FREQ_DUTY_MEASUREMENT,
58
+
59
+
TIMER_NOT_USED = 0xFFFF// This must be the last item of this enum
60
+
} TimerModes_t;
61
+
62
+
typedefenum {
63
+
TICK_FORMAT, // default
64
+
MICROSEC_FORMAT,
65
+
HERTZ_FORMAT,
66
+
} TimerFormat_t;
67
+
68
+
typedefenum {
69
+
TICK_COMPARE_FORMAT, // default
70
+
MICROSEC_COMPARE_FORMAT,
71
+
HERTZ_COMPARE_FORMAT,
72
+
PERCENT_COMPARE_FORMAT, // used for Dutycycle
73
+
RESOLUTION_8B_COMPARE_FORMAT, // used for Dutycycle: [0.. 255]
74
+
RESOLUTION_12B_COMPARE_FORMAT // used for Dutycycle: [0.. 4095]
75
+
} TimerCompareFormat_t;
76
+
77
+
// This structure is used to be able to get HardwareTimer instance (C++ class)
78
+
// from handler (C structure) specially for interrupt management
79
+
typedefstruct {
80
+
// Those 2 first fields must remain in this order at the beginning of the structure
81
+
void *__this;
82
+
TIM_HandleTypeDef handle;
83
+
} HardwareTimerObj_t;
84
+
85
+
#ifdef __cplusplus
86
+
87
+
/* Class --------------------------------------------------------*/
88
+
classHardwareTimer {
89
+
public:
90
+
HardwareTimer(TIM_TypeDef *instance);
91
+
~HardwareTimer(); // destructor
92
+
93
+
voidpause(void); // Pause counter and all output channels
94
+
voidresume(void); // Resume counter and all output channels
95
+
96
+
voidsetPrescaleFactor(uint32_t format = TICK_FORMAT); // set prescaler register (which is factor value - 1)
97
+
uint32_tgetPrescaleFactor();
98
+
99
+
voidsetOverflow(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set AutoReload register depending on format provided
100
+
uint32_tgetOverflow(TimerFormat_t format = TICK_FORMAT); // return overflow depending on format provided
101
+
102
+
voidsetPWM(uint32_t channel, PinName pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *) = NULL, void (*CompareCallback)(HardwareTimer *) = NULL); // Set all in one command freq in HZ, Duty in percentage. Including both interrup.
uint32_tgetCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided
113
+
114
+
voidsetCaptureCompare(uint32_t channel, uint32_t compare, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // set Compare register value of specified channel depending on format provided
115
+
116
+
//Add interrupt to period update
117
+
voidattachInterrupt(void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon update event (timer rollover)
118
+
voiddetachInterrupt(); // remove interrupt callback which was attached to update event
119
+
//Add interrupt to capture/compare channel
120
+
voidattachInterrupt(uint32_t channel, void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon compare match event of specified channel
121
+
voiddetachInterrupt(uint32_t channel); // remove interrupt callback which was attached to compare match event of specified channel
voidrefresh(void); // Generate update event to force all registers (Autoreload, prescaler, compare) to be taken into account
126
+
127
+
uint32_tgetTimerClkFreq(); // return timer clock frequency in Hz.
128
+
129
+
staticvoidcaptureCompareCallback(TIM_HandleTypeDef *htim); // Generic Caputre and Compare callback which will call user callback
130
+
staticvoidupdateCallback(TIM_HandleTypeDef *htim); // Generic Update (rollover) callback which will call user callback
131
+
132
+
private:
133
+
TIM_OC_InitTypeDef _channelOC[TIMER_CHANNELS];
134
+
TIM_IC_InitTypeDef _channelIC[TIMER_CHANNELS];
135
+
HardwareTimerObj_t _HardwareTimerObj;
136
+
void (*callbacks[1 + TIMER_CHANNELS])(HardwareTimer *); //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
0 commit comments