Skip to content

HardwareTimer: Add API to check whether timer/channels are running #1550

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 1 commit into from
Nov 19, 2021
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
36 changes: 36 additions & 0 deletions cores/arduino/HardwareTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,42 @@ void HardwareTimer::captureCompareCallback(TIM_HandleTypeDef *htim)
}
}

/**
* @brief Check whether HardwareTimer is running (paused or resumed).
* @retval return true if the HardwareTimer is running
*/
bool HardwareTimer::isRunning()
{
return LL_TIM_IsEnabledCounter(_timerObj.handle.Instance);
}

/**
* @brief Check whether channel is running (paused or resumed).
* @param channel: Arduino channel [1..4]
* @retval return true if HardwareTimer is running and the channel is enabled
*/
bool HardwareTimer::isRunningChannel(uint32_t channel)
{
int timAssociatedInputChannel;
int LLChannel = getLLChannel(channel);
int interrupt = getIT(channel);
bool ret;

if (LLChannel == -1) {
Error_Handler();
}

if (interrupt == -1) {
Error_Handler();
}

// channel is runnning if: timer is running, and either output channel is
// enabled or interrupt is set
ret = LL_TIM_CC_IsEnabledChannel(_timerObj.handle.Instance, LLChannel)
|| (__HAL_TIM_GET_IT_SOURCE(&(_timerObj.handle), interrupt) == SET);
return (isRunning() && ret);
}

/**
* @brief HardwareTimer destructor
* @retval None
Expand Down
5 changes: 4 additions & 1 deletion cores/arduino/HardwareTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,12 @@ class HardwareTimer {

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 captureCompareCallback(TIM_HandleTypeDef *htim); // Generic Capture and Compare callback which will call user callback
static void updateCallback(TIM_HandleTypeDef *htim); // Generic Update (rollover) callback which will call user callback

bool isRunning(); // return true if HardwareTimer is running
bool isRunningChannel(uint32_t channel); // return true if channel is running

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