Skip to content

HardwareTimer implementation #576

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1,429 changes: 1,429 additions & 0 deletions cores/arduino/HardwareTimer.cpp

Large diffs are not rendered by default.

154 changes: 154 additions & 0 deletions cores/arduino/HardwareTimer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
Copyright (c) 2017 Daniel Fekete

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Copyright (c) 2019 STMicroelectronics
Modified to support Arduino_Core_STM32
*/

/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef HARDWARETIMER_H_
#define HARDWARETIMER_H_

/* Includes ------------------------------------------------------------------*/
#include "timer.h"

#ifdef HAL_TIM_MODULE_ENABLED

#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

typedef enum {
TIMER_DISABLED,
// Output Compare
TIMER_OUTPUT_COMPARE, // == TIM_OCMODE_TIMING no output, useful for only-interrupt
TIMER_OUTPUT_COMPARE_ACTIVE, // == TIM_OCMODE_ACTIVE pin is set high when counter == channel compare
TIMER_OUTPUT_COMPARE_INACTIVE, // == TIM_OCMODE_INACTIVE pin is set low when counter == channel compare
TIMER_OUTPUT_COMPARE_TOGGLE, // == TIM_OCMODE_TOGGLE pin toggles when counter == channel compare
TIMER_OUTPUT_COMPARE_PWM1, // == TIM_OCMODE_PWM1 pin high when counter < channel compare, low otherwise
TIMER_OUTPUT_COMPARE_PWM2, // == TIM_OCMODE_PWM2 pin low when counter < channel compare, high otherwise
TIMER_OUTPUT_COMPARE_FORCED_ACTIVE, // == TIM_OCMODE_FORCED_ACTIVE pin always high
TIMER_OUTPUT_COMPARE_FORCED_INACTIVE, // == TIM_OCMODE_FORCED_INACTIVE pin always low

//Input capture
TIMER_INPUT_CAPTURE_RISING, // == TIM_INPUTCHANNELPOLARITY_RISING
TIMER_INPUT_CAPTURE_FALLING, // == TIM_INPUTCHANNELPOLARITY_FALLING
TIMER_INPUT_CAPTURE_BOTHEDGE, // == TIM_INPUTCHANNELPOLARITY_BOTHEDGE

// Used 2 channels for a single pin. One channel in TIM_INPUTCHANNELPOLARITY_RISING another channel in TIM_INPUTCHANNELPOLARITY_FALLING.
// Channels must be used by pair: CH1 with CH2, or CH3 with CH4
// This mode is very useful for Frequency and Dutycycle measurement
TIMER_INPUT_FREQ_DUTY_MEASUREMENT,

TIMER_NOT_USED = 0xFFFF // This must be the last item of this enum
} TimerModes_t;

typedef enum {
TICK_FORMAT, // default
MICROSEC_FORMAT,
HERTZ_FORMAT,
} TimerFormat_t;

typedef enum {
TICK_COMPARE_FORMAT, // default
MICROSEC_COMPARE_FORMAT,
HERTZ_COMPARE_FORMAT,
PERCENT_COMPARE_FORMAT, // used for Dutycycle
RESOLUTION_8B_COMPARE_FORMAT, // used for Dutycycle: [0.. 255]
RESOLUTION_12B_COMPARE_FORMAT // used for Dutycycle: [0.. 4095]
} TimerCompareFormat_t;

// This structure is used to be able to get HardwareTimer instance (C++ class)
// from handler (C structure) specially for interrupt management
typedef struct {
// Those 2 first fields must remain in this order at the beginning of the structure
void *__this;
TIM_HandleTypeDef handle;
} HardwareTimerObj_t;

#ifdef __cplusplus

/* Class --------------------------------------------------------*/
class HardwareTimer {
public:
HardwareTimer(TIM_TypeDef *instance);
~HardwareTimer(); // destructor

void pause(void); // Pause counter and all output channels
void resume(void); // Resume counter and all output channels

void setPrescaleFactor(uint32_t format = TICK_FORMAT); // set prescaler register (which is factor value - 1)
uint32_t getPrescaleFactor();

void setOverflow(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set AutoReload register depending on format provided
uint32_t getOverflow(TimerFormat_t format = TICK_FORMAT); // return overflow depending on format provided

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.
void setPWM(uint32_t channel, uint32_t pin, uint32_t frequency, uint32_t dutycycle, void (*PeriodCallback)(HardwareTimer *) = NULL, void (*CompareCallback)(HardwareTimer *) = NULL);


void setCount(uint32_t val, TimerFormat_t format = TICK_FORMAT); // set timer counter to value 'val' depending on format provided
uint32_t getCount(TimerFormat_t format = TICK_FORMAT); // return current counter value of timer depending on format provided

void setMode(uint32_t channel, TimerModes_t mode, PinName pin = NC); // Configure timer channel with specified mode on specified pin if available
void setMode(uint32_t channel, TimerModes_t mode, uint32_t pin);

uint32_t getCaptureCompare(uint32_t channel, TimerCompareFormat_t format = TICK_COMPARE_FORMAT); // return Capture/Compare register value of specified channel depending on format provided

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

//Add interrupt to period update
void attachInterrupt(void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon update event (timer rollover)
void detachInterrupt(); // remove interrupt callback which was attached to update event
//Add interrupt to capture/compare channel
void attachInterrupt(uint32_t channel, void (*handler)(HardwareTimer *)); // Attach interrupt callback which will be called upon compare match event of specified channel
void detachInterrupt(uint32_t channel); // remove interrupt callback which was attached to compare match event of specified channel

void timerHandleDeinit(); // Timer deinitialization

void refresh(void); // Generate update event to force all registers (Autoreload, prescaler, compare) to be taken into account

uint32_t getTimerClkFreq(); // return timer clock frequency in Hz.

static void captureCompareCallback(TIM_HandleTypeDef *htim); // Generic Caputre and Compare callback which will call user callback
static void updateCallback(TIM_HandleTypeDef *htim); // Generic Update (rollover) callback which will call user callback

private:
TIM_OC_InitTypeDef _channelOC[TIMER_CHANNELS];
TIM_IC_InitTypeDef _channelIC[TIMER_CHANNELS];
HardwareTimerObj_t _HardwareTimerObj;
void (*callbacks[1 + TIMER_CHANNELS])(HardwareTimer *); //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)

int getChannel(uint32_t channel);
void resumeChannel(uint32_t channel);
#if defined(TIM_CCER_CC1NE)
bool isComplementaryChannel[TIMER_CHANNELS];
#endif
};

HardwareTimerObj_t *get_timer_obj(TIM_HandleTypeDef *htim);

extern HardwareTimerObj_t *HardwareTimer_Handle[TIMER_NUM];

extern timer_index_t get_timer_index(TIM_TypeDef *htim);

#endif /* __cplusplus */

#endif // HAL_TIM_MODULE_ENABLED
#endif // HARDWARETIMER_H_
110 changes: 96 additions & 14 deletions cores/arduino/Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,118 @@
*/

#include "Arduino.h"
#include "HardwareTimer.h"

PinName g_lastPin = NC;
#if defined(HAL_TIM_MODULE_ENABLED) && defined(TIMER_TONE)

#ifdef HAL_TIM_MODULE_ENABLED
static stimer_t _timer;
#define MAX_FREQ 65535

// frequency (in hertz) and duration (in milliseconds).
typedef struct {
PinName pin;
int32_t count;
} timerPinInfo_t;

static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration);
static void tonePeriodElapsedCallback(HardwareTimer *HT);
static timerPinInfo_t TimerTone_pinInfo = {NC, 0};
static HardwareTimer *TimerTone = NULL;

/**
* @brief Tone Period elapsed callback in non-blocking mode
* @param htim : timer handle
* @retval None
*/
static void tonePeriodElapsedCallback(HardwareTimer *HT)
{
UNUSED(HT);
GPIO_TypeDef *port = get_GPIO_Port(STM_PORT(TimerTone_pinInfo.pin));

if (port != NULL) {
if (TimerTone_pinInfo.count != 0) {
if (TimerTone_pinInfo.count > 0) {
TimerTone_pinInfo.count--;
}
digital_io_toggle(port, STM_LL_GPIO_PIN(TimerTone_pinInfo.pin));
} else {
digital_io_write(port, STM_LL_GPIO_PIN(TimerTone_pinInfo.pin), 0);
}
}
}

/**
* @brief This function will reset the tone timer
* @param port : pointer to port
* @param pin : pin number to toggle
* @retval None
*/
static void timerTonePinDeinit()
{
if (TimerTone != NULL) {
TimerTone->timerHandleDeinit();
}
if (TimerTone_pinInfo.pin != NC) {
pin_function(TimerTone_pinInfo.pin, STM_PIN_DATA(STM_MODE_INPUT, GPIO_NOPULL, 0));
TimerTone_pinInfo.pin = NC;
}
}

static void timerTonePinInit(PinName p, uint32_t frequency, uint32_t duration)
{
uint32_t timFreq = 2 * frequency;

if (frequency <= MAX_FREQ) {
if (frequency == 0) {
timerTonePinDeinit();
} else {
TimerTone_pinInfo.pin = p;

//Calculate the toggle count
if (duration > 0) {
TimerTone_pinInfo.count = ((timFreq * duration) / 1000);
} else {
TimerTone_pinInfo.count = -1;
}

pin_function(TimerTone_pinInfo.pin, STM_PIN_DATA(STM_MODE_OUTPUT_PP, GPIO_NOPULL, 0));

TimerTone->setMode(1, TIMER_OUTPUT_COMPARE, NC);
TimerTone->setOverflow(timFreq, HERTZ_FORMAT);
TimerTone->attachInterrupt(tonePeriodElapsedCallback);
TimerTone->resume();
}
}
}

// frequency (in hertz) and duration (in milliseconds).
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
{
PinName p = digitalPinToPinName(_pin);

if (TimerTone == NULL) {
TimerTone = new HardwareTimer(TIMER_TONE);
}

if (p != NC) {
if ((g_lastPin == NC) || (g_lastPin == p)) {
_timer.pin = p;
TimerPinInit(&_timer, frequency, duration);
g_lastPin = p;
if ((TimerTone_pinInfo.pin == NC) || (TimerTone_pinInfo.pin == p)) {
timerTonePinInit(p, frequency, duration);
}
}
}


void noTone(uint8_t _pin)
void noTone(uint8_t _pin, bool destruct)
{
PinName p = digitalPinToPinName(_pin);
if (p != NC) {
TimerPinDeinit(&_timer);
g_lastPin = NC;
if ((p != NC) && (TimerTone_pinInfo.pin == p)) {
timerTonePinDeinit();

if ((destruct) && (TimerTone != NULL)) {
delete (TimerTone);
TimerTone = NULL;
}
}
}
#else
#warning "TIMER_TONE or HAL_TIM_MODULE_ENABLED not defined"
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration)
{
UNUSED(_pin);
Expand All @@ -61,4 +143,4 @@ void noTone(uint8_t _pin)
{
UNUSED(_pin);
}
#endif /* HAL_TIM_MODULE_ENABLED */
#endif /* HAL_TIM_MODULE_ENABLED && TIMER_TONE */
2 changes: 1 addition & 1 deletion cores/arduino/Tone.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extern void tone(uint8_t _pin, unsigned int frequency, unsigned long duration =
*
* \param _pin
*/
extern void noTone(uint8_t _pin);
extern void noTone(uint8_t _pin, bool destruct = false);

#endif

Expand Down
4 changes: 2 additions & 2 deletions cores/arduino/pins_arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -302,13 +302,13 @@ PinName analogInputToPinName(uint32_t pin);
#define DACC_RESOLUTION 12
#endif
#ifndef PWM_RESOLUTION
#define PWM_RESOLUTION 8
#define PWM_RESOLUTION 12
#endif
#ifndef PWM_FREQUENCY
#define PWM_FREQUENCY 1000
#endif
#ifndef PWM_MAX_DUTY_CYCLE
#define PWM_MAX_DUTY_CYCLE 255
#define PWM_MAX_DUTY_CYCLE 4095
#endif

#endif /*_PINS_ARDUINO_H_*/
Loading