Skip to content

Commit 4817b86

Browse files
committed
HardwareTimer implementation
1 parent a835086 commit 4817b86

File tree

64 files changed

+2288
-1536
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

+2288
-1536
lines changed

cores/arduino/HardwareTimer.cpp

Lines changed: 1427 additions & 0 deletions
Large diffs are not rendered by default.

cores/arduino/HardwareTimer.h

Lines changed: 154 additions & 0 deletions
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+
void *__this; // Those 2 first field must remain in this order at the beginning of the structure
81+
TIM_HandleTypeDef handle; // Those 2 first field must remain in this order at the beginning of the structure
82+
// void (*callbacks[1 + TIMER_CHANNELS])(HardwareTimer*); //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
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_

cores/arduino/Tone.cpp

Lines changed: 103 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,129 @@
2020
*/
2121

2222
#include "Arduino.h"
23+
#include "HardwareTimer.h"
24+
25+
#if defined(HAL_TIM_MODULE_ENABLED) && defined(TIMER_TONE)
26+
27+
#define MAX_FREQ 65535
28+
29+
typedef struct {
30+
PinName pin;
31+
int32_t count;
32+
uint8_t state;
33+
} timerPinInfo_t;
34+
35+
static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration);
36+
static void tonePeriodElapsedCallback(HardwareTimer *HT);
37+
static timerPinInfo_t TimerTone_pinInfo;
38+
39+
static HardwareTimer *TimerTone = NULL;
2340

2441
PinName g_lastPin = NC;
2542

26-
#ifdef HAL_TIM_MODULE_ENABLED
27-
static stimer_t _timer;
43+
/**
44+
* @brief Tone Period elapsed callback in non-blocking mode
45+
* @param htim : timer handle
46+
* @retval None
47+
*/
48+
static void tonePeriodElapsedCallback(HardwareTimer *HT)
49+
{
50+
UNUSED(HT);
51+
GPIO_TypeDef *port = get_GPIO_Port(STM_PORT(TimerTone_pinInfo.pin));
2852

29-
// frequency (in hertz) and duration (in milliseconds).
53+
if (port != NULL) {
54+
if (TimerTone_pinInfo.count != 0) {
55+
if (TimerTone_pinInfo.count > 0) {
56+
TimerTone_pinInfo.count--;
57+
}
58+
TimerTone_pinInfo.state = (TimerTone_pinInfo.state == 0) ? 1 : 0;
59+
digital_io_write(port, STM_LL_GPIO_PIN(TimerTone_pinInfo.pin), TimerTone_pinInfo.state);
60+
} else {
61+
digital_io_write(port, STM_LL_GPIO_PIN(TimerTone_pinInfo.pin), 0);
62+
}
63+
}
64+
}
65+
66+
67+
/**
68+
* @brief This function will reset the tone timer
69+
* @param port : pointer to port
70+
* @param pin : pin number to toggle
71+
* @retval None
72+
*/
73+
static void timerTonePinDeinit()
74+
{
75+
TimerTone->timerHandleDeinit();
76+
pin_function(TimerTone_pinInfo.pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
77+
}
78+
79+
static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration)
80+
{
81+
uint32_t timFreq = 2 * frequency;
82+
83+
TimerTone_pinInfo.pin = p;
84+
85+
if (frequency > MAX_FREQ) {
86+
return;
87+
}
3088

89+
TimerTone_pinInfo.state = 0;
90+
91+
if (frequency == 0) {
92+
timerTonePinDeinit();
93+
return;
94+
}
95+
96+
//Calculate the toggle count
97+
if (duration > 0) {
98+
TimerTone_pinInfo.count = ((timFreq * duration) / 1000);
99+
} else {
100+
TimerTone_pinInfo.count = -1;
101+
}
102+
103+
pin_function(TimerTone_pinInfo.pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
104+
105+
TimerTone->setMode(1, TIMER_OUTPUT_COMPARE, NC);
106+
TimerTone->setOverflow(timFreq, HERTZ_FORMAT);
107+
TimerTone->attachInterrupt(tonePeriodElapsedCallback);
108+
TimerTone->resume();
109+
}
110+
111+
// frequency (in hertz) and duration (in milliseconds).
31112
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
32113
{
33114
PinName p = digitalPinToPinName(_pin);
115+
116+
if (TimerTone == NULL) {
117+
TimerTone = new HardwareTimer(TIMER_TONE);
118+
}
119+
34120
if (p != NC) {
35121
if ((g_lastPin == NC) || (g_lastPin == p)) {
36-
_timer.pin = p;
37-
TimerPinInit(&_timer, frequency, duration);
122+
123+
timerTonePinInit(p, frequency, duration);
38124
g_lastPin = p;
39125
}
40126
}
41127
}
42128

43-
44-
void noTone(uint8_t _pin)
129+
void noTone(uint8_t _pin, bool destruct)
45130
{
46131
PinName p = digitalPinToPinName(_pin);
47132
if (p != NC) {
48-
TimerPinDeinit(&_timer);
133+
timerTonePinDeinit();
49134
g_lastPin = NC;
50135
}
136+
if (destruct) {
137+
if (TimerTone != NULL) {
138+
delete (TimerTone);
139+
TimerTone = NULL;
140+
}
141+
}
51142
}
52143
#else
144+
145+
#warning "TIMER_TONE or HAL_TIM_MODULE_ENABLED not defined"
53146
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
54147
{
55148
UNUSED(_pin);
@@ -61,4 +154,5 @@ void noTone(uint8_t _pin)
61154
{
62155
UNUSED(_pin);
63156
}
64-
#endif /* HAL_TIM_MODULE_ENABLED */
157+
158+
#endif /* HAL_TIM_MODULE_ENABLED && TIMER_TONE */

cores/arduino/Tone.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ extern void tone(uint8_t _pin, unsigned int frequency, unsigned long duration =
3434
*
3535
* \param _pin
3636
*/
37-
extern void noTone(uint8_t _pin);
37+
extern void noTone(uint8_t _pin, bool destruct = false);
3838

3939
#endif
4040

cores/arduino/pins_arduino.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,13 @@ PinName analogInputToPinName(uint32_t pin);
302302
#define DACC_RESOLUTION 12
303303
#endif
304304
#ifndef PWM_RESOLUTION
305-
#define PWM_RESOLUTION 8
305+
#define PWM_RESOLUTION 12
306306
#endif
307307
#ifndef PWM_FREQUENCY
308308
#define PWM_FREQUENCY 1000
309309
#endif
310310
#ifndef PWM_MAX_DUTY_CYCLE
311-
#define PWM_MAX_DUTY_CYCLE 255
311+
#define PWM_MAX_DUTY_CYCLE 4095
312312
#endif
313313

314314
#endif /*_PINS_ARDUINO_H_*/

0 commit comments

Comments
 (0)