Skip to content

Commit

Permalink
Add support for setting the channel input filter (#2136)
Browse files Browse the repository at this point in the history
* Add support for setting the channel input filter

* Format with astyle

* Start enum from 0 so we can directly convert enum to filter value
  • Loading branch information
dberlin authored Sep 27, 2023
1 parent 1ee0a03 commit 61a1680
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
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

0 comments on commit 61a1680

Please sign in to comment.