Skip to content

Commit cfe6dd8

Browse files
ABOSTMfpistm
authored andcommitted
HardwareTimer implementation (stm32duino#576)
* HardwareTimer implementation Fixes stm32duino#146 * Fix: Unchecked reference to TIM13 in TIM8_IRQHandler Fixes stm32duino#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_

cores/arduino/Tone.cpp

+96-14
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,118 @@
2020
*/
2121

2222
#include "Arduino.h"
23+
#include "HardwareTimer.h"
2324

24-
PinName g_lastPin = NC;
25+
#if defined(HAL_TIM_MODULE_ENABLED) && defined(TIMER_TONE)
2526

26-
#ifdef HAL_TIM_MODULE_ENABLED
27-
static stimer_t _timer;
27+
#define MAX_FREQ 65535
2828

29-
// frequency (in hertz) and duration (in milliseconds).
29+
typedef struct {
30+
PinName pin;
31+
int32_t count;
32+
} timerPinInfo_t;
33+
34+
static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration);
35+
static void tonePeriodElapsedCallback(HardwareTimer *HT);
36+
static timerPinInfo_t TimerTone_pinInfo = {NC, 0};
37+
static HardwareTimer *TimerTone = NULL;
38+
39+
/**
40+
* @brief Tone Period elapsed callback in non-blocking mode
41+
* @param htim : timer handle
42+
* @retval None
43+
*/
44+
static void tonePeriodElapsedCallback(HardwareTimer *HT)
45+
{
46+
UNUSED(HT);
47+
GPIO_TypeDef *port = get_GPIO_Port(STM_PORT(TimerTone_pinInfo.pin));
48+
49+
if (port != NULL) {
50+
if (TimerTone_pinInfo.count != 0) {
51+
if (TimerTone_pinInfo.count > 0) {
52+
TimerTone_pinInfo.count--;
53+
}
54+
digital_io_toggle(port, STM_LL_GPIO_PIN(TimerTone_pinInfo.pin));
55+
} else {
56+
digital_io_write(port, STM_LL_GPIO_PIN(TimerTone_pinInfo.pin), 0);
57+
}
58+
}
59+
}
60+
61+
/**
62+
* @brief This function will reset the tone timer
63+
* @param port : pointer to port
64+
* @param pin : pin number to toggle
65+
* @retval None
66+
*/
67+
static void timerTonePinDeinit()
68+
{
69+
if (TimerTone != NULL) {
70+
TimerTone->timerHandleDeinit();
71+
}
72+
if (TimerTone_pinInfo.pin != NC) {
73+
pin_function(TimerTone_pinInfo.pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
74+
TimerTone_pinInfo.pin = NC;
75+
}
76+
}
77+
78+
static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration)
79+
{
80+
uint32_t timFreq = 2 * frequency;
81+
82+
if (frequency <= MAX_FREQ) {
83+
if (frequency == 0) {
84+
timerTonePinDeinit();
85+
} else {
86+
TimerTone_pinInfo.pin = p;
3087

88+
//Calculate the toggle count
89+
if (duration > 0) {
90+
TimerTone_pinInfo.count = ((timFreq * duration) / 1000);
91+
} else {
92+
TimerTone_pinInfo.count = -1;
93+
}
94+
95+
pin_function(TimerTone_pinInfo.pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));
96+
97+
TimerTone->setMode(1, TIMER_OUTPUT_COMPARE, NC);
98+
TimerTone->setOverflow(timFreq, HERTZ_FORMAT);
99+
TimerTone->attachInterrupt(tonePeriodElapsedCallback);
100+
TimerTone->resume();
101+
}
102+
}
103+
}
104+
105+
// frequency (in hertz) and duration (in milliseconds).
31106
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
32107
{
33108
PinName p = digitalPinToPinName(_pin);
109+
110+
if (TimerTone == NULL) {
111+
TimerTone = new HardwareTimer(TIMER_TONE);
112+
}
113+
34114
if (p != NC) {
35-
if ((g_lastPin == NC) || (g_lastPin == p)) {
36-
_timer.pin = p;
37-
TimerPinInit(&_timer, frequency, duration);
38-
g_lastPin = p;
115+
if ((TimerTone_pinInfo.pin == NC) || (TimerTone_pinInfo.pin == p)) {
116+
timerTonePinInit(p, frequency, duration);
39117
}
40118
}
41119
}
42120

43-
44-
void noTone(uint8_t _pin)
121+
void noTone(uint8_t _pin, bool destruct)
45122
{
46123
PinName p = digitalPinToPinName(_pin);
47-
if (p != NC) {
48-
TimerPinDeinit(&_timer);
49-
g_lastPin = NC;
124+
if ((p != NC) && (TimerTone_pinInfo.pin == p)) {
125+
timerTonePinDeinit();
126+
127+
if ((destruct) && (TimerTone != NULL)) {
128+
delete (TimerTone);
129+
TimerTone = NULL;
130+
}
50131
}
51132
}
52133
#else
134+
#warning "TIMER_TONE or HAL_TIM_MODULE_ENABLED not defined"
53135
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
54136
{
55137
UNUSED(_pin);
@@ -61,4 +143,4 @@ void noTone(uint8_t _pin)
61143
{
62144
UNUSED(_pin);
63145
}
64-
#endif /* HAL_TIM_MODULE_ENABLED */
146+
#endif /* HAL_TIM_MODULE_ENABLED && TIMER_TONE */

cores/arduino/Tone.h

+1-1
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

+2-2
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)