Skip to content

HardwareTimer: Allow setting preload enable bits #900

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
Jan 30, 2020
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
30 changes: 29 additions & 1 deletion cores/arduino/HardwareTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ HardwareTimer::HardwareTimer(TIM_TypeDef *instance)
#if defined(TIM_RCR_REP)
_timerObj.handle.Init.RepetitionCounter = 0;
#endif
_timerObj.handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
_timerObj.handle.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
HAL_TIM_Base_Init(&(_timerObj.handle));
}

Expand Down Expand Up @@ -442,6 +442,11 @@ uint32_t HardwareTimer::getOverflow(TimerFormat_t format)

/**
* @brief Set overflow (rollover)
*
* Note that by default, the new value will not be applied
* immediately, but become effective at the next update event
* (usually the next timer overflow). See setPreloadEnable()
* for controlling this behaviour.
* @param overflow: depend on format parameter
* @param format of overflow parameter. If ommited default format is Tick
* TICK_FORMAT: overflow is the number of tick for overflow
Expand Down Expand Up @@ -677,6 +682,29 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin)
}
}

/**
* @brief Enable or disable preloading for overflow value
* When disabled, changes to the overflow value take effect
* immediately. When enabled (the default), the value takes
* effect only at the next update event (typically the next
* overflow).
*
* Note that the capture/compare register has its own preload
* enable bit, which is independent and enabled in PWM modes
* and disabled otherwise. If you need more control of that
* bit, you can use the HAL functions directly.
* @param value: true to enable preloading, false to disable
* @retval None
*/
void HardwareTimer::setPreloadEnable(bool value)
{
if (value) {
LL_TIM_EnableARRPreload(_timerObj.handle.Instance);
} else {
LL_TIM_DisableARRPreload(_timerObj.handle.Instance);
}
}

/**
* @brief Set channel Capture/Compare register
* @param channel: Arduino channel [1..4]
Expand Down
16 changes: 7 additions & 9 deletions cores/arduino/HardwareTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,15 +112,15 @@ class HardwareTimer {
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 setPreloadEnable(bool value); // Configure overflow preload enable setting

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

void setInterruptPriority(uint32_t preemptPriority, uint32_t subPriority); // set interrupt priority
Expand All @@ -139,26 +139,24 @@ class HardwareTimer {
// Refresh() is usefull while timer is running after some registers update
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

// The following function(s) are available for more advanced timer options
TIM_HandleTypeDef *getHandle(); // return the handle address for HAL related configuration

private:
TimerModes_t _ChannelMode[TIMER_CHANNELS];
timerObj_t _timerObj;
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);
int getLLChannel(uint32_t channel);
int getIT(uint32_t channel);
int getAssociatedChannel(uint32_t channel);
#if defined(TIM_CCER_CC1NE)
bool isComplementaryChannel[TIMER_CHANNELS];
#endif
private:
TimerModes_t _ChannelMode[TIMER_CHANNELS];
timerObj_t _timerObj;
void (*callbacks[1 + TIMER_CHANNELS])(HardwareTimer *); //Callbacks: 0 for update, 1-4 for channels. (channel5/channel6, if any, doesn't have interrupt)
};

extern timerObj_t *HardwareTimer_Handle[TIMER_NUM];
Expand All @@ -168,4 +166,4 @@ extern timer_index_t get_timer_index(TIM_TypeDef *htim);
#endif /* __cplusplus */

#endif // HAL_TIM_MODULE_ENABLED && !HAL_TIM_MODULE_ONLY
#endif // HARDWARETIMER_H_
#endif // HARDWARETIMER_H_