Skip to content

Commit cfe6dd8

Browse files
ABOSTMfpistm
authored andcommittedAug 19, 2019
HardwareTimer implementation (#576)
* HardwareTimer implementation Fixes #146 * Fix: Unchecked reference to TIM13 in TIM8_IRQHandler Fixes #585
1 parent a835086 commit cfe6dd8

File tree

64 files changed

+2283
-1541
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+2283
-1541
lines changed
 

‎cores/arduino/HardwareTimer.cpp

+1,429
Large diffs are not rendered by default.

‎cores/arduino/HardwareTimer.h

+154
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
33+
#ifdef HAL_TIM_MODULE_ENABLED
34+
35+
#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
36+
37+
typedef enum {
38+
TIMER_DISABLED,
39+
// Output Compare
40+
TIMER_OUTPUT_COMPARE, // == TIM_OCMODE_TIMING no output, useful for only-interrupt
41+
TIMER_OUTPUT_COMPARE_ACTIVE, // == TIM_OCMODE_ACTIVE pin is set high when counter == channel compare
42+
TIMER_OUTPUT_COMPARE_INACTIVE, // == TIM_OCMODE_INACTIVE pin is set low when counter == channel compare
43+
TIMER_OUTPUT_COMPARE_TOGGLE, // == TIM_OCMODE_TOGGLE pin toggles when counter == channel compare
44+
TIMER_OUTPUT_COMPARE_PWM1, // == TIM_OCMODE_PWM1 pin high when counter < channel compare, low otherwise
45+
TIMER_OUTPUT_COMPARE_PWM2, // == TIM_OCMODE_PWM2 pin low when counter < channel compare, high otherwise
46+
TIMER_OUTPUT_COMPARE_FORCED_ACTIVE, // == TIM_OCMODE_FORCED_ACTIVE pin always high
47+
TIMER_OUTPUT_COMPARE_FORCED_INACTIVE, // == TIM_OCMODE_FORCED_INACTIVE pin always low
48+
49+
//Input capture
50+
TIMER_INPUT_CAPTURE_RISING, // == TIM_INPUTCHANNELPOLARITY_RISING
51+
TIMER_INPUT_CAPTURE_FALLING, // == TIM_INPUTCHANNELPOLARITY_FALLING
52+
TIMER_INPUT_CAPTURE_BOTHEDGE, // == TIM_INPUTCHANNELPOLARITY_BOTHEDGE
53+
54+
// 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+
typedef enum {
63+
TICK_FORMAT, // default
64+
MICROSEC_FORMAT,
65+
HERTZ_FORMAT,
66+
} TimerFormat_t;
67+
68+
typedef enum {
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+
typedef struct {
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+
class HardwareTimer {
89+
public:
90+
HardwareTimer(TIM_TypeDef *instance);
91+
~HardwareTimer(); // destructor
92+
93+
void pause(void); // Pause counter and all output channels
94+
void resume(void); // Resume counter and all output channels
95+
96+
void setPrescaleFactor(uint32_t format = TICK_FORMAT); // set prescaler register (which is factor value - 1)
97+
uint32_t getPrescaleFactor();
98+
99+
void setOverflow(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set AutoReload register depending on format provided
100+
uint32_t getOverflow(TimerFormat_t format = TICK_FORMAT); // return overflow depending on format provided
101+
102+
void setPWM(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.
103+
void setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *) = NULL, void (*CompareCallback)(HardwareTimer *) = NULL);
104+
105+
106+
void setCount(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set timer counter to value 'val' depending on format provided
107+
uint32_t getCount(TimerFormat_t format = TICK_FORMAT); // return current counter value of timer depending on format provided
108+
109+
void setMode(uint32_t channel, TimerModes_t mode, PinName pin = NC); // Configure timer channel with specified mode on specified pin if available
110+
void setMode(uint32_t channel, TimerModes_t mode, uint32_t pin);
111+
112+
uint32_t getCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided
113+
114+
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
115+
116+
//Add interrupt to period update
117+
void attachInterrupt(void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon update event (timer rollover)
118+
void detachInterrupt(); // remove interrupt callback which was attached to update event
119+
//Add interrupt to capture/compare channel
120+
void attachInterrupt(uint32_t channel, void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon compare match event of specified channel
121+
void detachInterrupt(uint32_t channel); // remove interrupt callback which was attached to compare match event of specified channel
122+
123+
void timerHandleDeinit(); // Timer deinitialization
124+
125+
void refresh(void); // Generate update event to force all registers (Autoreload, prescaler, compare) to be taken into account
126+
127+
uint32_t getTimerClkFreq(); // return timer clock frequency in Hz.
128+
129+
static void captureCompareCallback(TIM_HandleTypeDef *htim); // Generic Caputre and Compare callback which will call user callback
130+
static void updateCallback(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)
137+
138+
int getChannel(uint32_t channel);
139+
void resumeChannel(uint32_t channel);
140+
#if defined(TIM_CCER_CC1NE)
141+
bool isComplementaryChannel[TIMER_CHANNELS];
142+
#endif
143+
};
144+
145+
HardwareTimerObj_t *get_timer_obj(TIM_HandleTypeDef *htim);
146+
147+
extern HardwareTimerObj_t *HardwareTimer_Handle[TIMER_NUM];
148+
149+
extern timer_index_t get_timer_index(TIM_TypeDef *htim);
150+
151+
#endif /* __cplusplus */
152+
153+
#endif // HAL_TIM_MODULE_ENABLED
154+
#endif // HARDWARETIMER_H_

0 commit comments

Comments
 (0)
Please sign in to comment.