Skip to content
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

Add support for setting the channel input filter #2136

Merged
merged 3 commits into from
Sep 27, 2023
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
23 changes: 21 additions & 2 deletions cores/arduino/HardwareTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,25 @@ typedef enum {
PERCENT_COMPARE_FORMAT, // used for Dutycycle
} TimerCompareFormat_t;

typedef enum {
FILTER_NONE = 0, // No filter
FILTER_CKINT_N2, // Sampling rate is same as clock interrupt, n=2 events
FILTER_CKINT_N4, // Sampling rate is same as clock interrupt, n=4 events
FILTER_CKINT_N8, // Sampling rate is same as clock interrupt, n=8 events
FILTER_DTS2_N6, // Sampling rate is DTS/2, n=6 events
FILTER_DTS2_N8, // Sampling rate is DTS/2, n=8 events
FILTER_DTS4_N6, // Sampling rate is DTS/4, n=6 events
FILTER_DTS4_N8, // Sampling rate is DTS/4, n=8 events
FILTER_DTS8_N6, // Sampling rate is DTS/8, n=6 events
FILTER_DTS8_N8, // Sampling rate is DTS/8, n=8 events
FILTER_DTS16_N5, // Sampling rate is DTS/16, n=5 events
FILTER_DTS16_N6, // Sampling rate is DTS/16, n=6 events
FILTER_DTS16_N8, // Sampling rate is DTS/16, n=8 events
FILTER_DTS32_N5, // Sampling rate is DTS/32, n=5 events
FILTER_DTS32_N6, // Sampling rate is DTS/32, n=6 events
FILTER_DTS32_N8, // Sampling rate is DTS/32, n=8 events
} ChannelInputFilter_t;

#ifdef __cplusplus

#include <functional>
Expand Down Expand Up @@ -121,8 +140,8 @@ class HardwareTimer {
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);
void setMode(uint32_t channel, TimerModes_t mode, PinName pin = NC, ChannelInputFilter_t filter = FILTER_NONE); // Configure timer channel with specified mode on specified pin if available
void setMode(uint32_t channel, TimerModes_t mode, uint32_t pin, ChannelInputFilter_t filter = FILTER_NONE);

TimerModes_t getMode(uint32_t channel); // Retrieve configured mode

Expand Down
8 changes: 4 additions & 4 deletions libraries/SrcWrapper/src/HardwareTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -619,9 +619,9 @@ void HardwareTimer::setCount(uint32_t counter, TimerFormat_t format)
* @param pin: Arduino pin number, ex: D1, 1 or PA1
* @retval None
*/
void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, uint32_t pin)
void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, uint32_t pin, ChannelInputFilter_t filter)
{
setMode(channel, mode, digitalPinToPinName(pin));
setMode(channel, mode, digitalPinToPinName(pin), filter);
}

/**
Expand All @@ -631,7 +631,7 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, uint32_t pin)
* @param pin: pin name, ex: PB_0
* @retval None
*/
void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin)
void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin, ChannelInputFilter_t filter)
{
int timChannel = getChannel(channel);
int timAssociatedInputChannel;
Expand Down Expand Up @@ -659,7 +659,7 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin)
channelIC.ICPolarity = TIM_ICPOLARITY_RISING;
channelIC.ICSelection = TIM_ICSELECTION_DIRECTTI;
channelIC.ICPrescaler = TIM_ICPSC_DIV1;
channelIC.ICFilter = 0;
channelIC.ICFilter = filter;

switch (mode) {
case TIMER_DISABLED:
Expand Down