Skip to content

Commit

Permalink
feat(HardwareTimer): support regular and complementary channels
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic Pillon <[email protected]>
  • Loading branch information
fpistm committed Mar 6, 2024
1 parent c12103b commit 05b5d68
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 27 deletions.
6 changes: 3 additions & 3 deletions cores/arduino/HardwareTimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,9 @@ class HardwareTimer {
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
// Store for each channel if regular, complementary or both are configured
// High part for complementary (0x10), low part for regular (0x01)
uint8_t configuredChannels[TIMER_CHANNELS];
private:
TimerModes_t _ChannelMode[TIMER_CHANNELS];
timerObj_t _timerObj;
Expand Down
48 changes: 24 additions & 24 deletions libraries/SrcWrapper/src/HardwareTimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
/* Private Defines */
#define PIN_NOT_USED 0xFF
#define MAX_RELOAD ((1 << 16) - 1) // Currently even 32b timers are used as 16b to have generic behavior
#define REGULAR_CHAN_MASK 0x0F
#define COMPLEMENTARY_CHAN_MASK 0xF0

/* Private Variables */
timerObj_t *HardwareTimer_Handle[TIMER_NUM] = {NULL};
Expand Down Expand Up @@ -111,9 +113,7 @@ void HardwareTimer::setup(TIM_TypeDef *instance)

// Initialize channel mode and complementary
for (int i = 0; i < TIMER_CHANNELS; i++) {
#if defined(TIM_CCER_CC1NE)
isComplementaryChannel[i] = false;
#endif
configuredChannels[i] = 0x00;
_ChannelMode[i] = TIMER_DISABLED;
}

Expand Down Expand Up @@ -190,11 +190,11 @@ void HardwareTimer::pauseChannel(uint32_t channel)
/* Starting from G4, new Channel state implementation prevents to restart a channel,
if the channel has not been explicitly be stopped with HAL interface */
#if defined(TIM_CHANNEL_N_STATE_SET)
if (isComplementaryChannel[channel - 1]) {
if (configuredChannels[channel - 1] & COMPLEMENTARY_CHAN_MASK) {
TIM_CHANNEL_N_STATE_SET(&(_timerObj.handle), getChannel(channel), HAL_TIM_CHANNEL_STATE_READY);
} else
}
#endif
{
if (configuredChannels[channel - 1] & REGULAR_CHAN_MASK) {
TIM_CHANNEL_STATE_SET(&(_timerObj.handle), getChannel(channel), HAL_TIM_CHANNEL_STATE_READY);
}
#endif
Expand Down Expand Up @@ -260,15 +260,16 @@ int HardwareTimer::getChannel(uint32_t channel)
}

/**
* @brief Convert arduino channel into LL channel
* @brief Convert configuraed arduino channel into LL channels (regular and/or complementary)
* @param Arduino channel [1..4]
* @retval LL channel. return -1 if arduino channel is invalid
*/
int HardwareTimer::getLLChannel(uint32_t channel)
{
uint32_t return_value;
int return_value = 0;

#if defined(TIM_CCER_CC1NE)
if (isComplementaryChannel[channel - 1]) {
if (configuredChannels[channel - 1] & COMPLEMENTARY_CHAN_MASK) {
// Complementary channel
switch (channel) {
case 1:
Expand All @@ -288,22 +289,22 @@ int HardwareTimer::getLLChannel(uint32_t channel)
default:
return_value = -1;
}
} else
}
#endif
{
if ((return_value != -1) && (configuredChannels[channel - 1] & REGULAR_CHAN_MASK)) {
// Regular channel not complementary
switch (channel) {
case 1:
return_value = LL_TIM_CHANNEL_CH1;
return_value |= LL_TIM_CHANNEL_CH1;
break;
case 2:
return_value = LL_TIM_CHANNEL_CH2;
return_value |= LL_TIM_CHANNEL_CH2;
break;
case 3:
return_value = LL_TIM_CHANNEL_CH3;
return_value |= LL_TIM_CHANNEL_CH3;
break;
case 4:
return_value = LL_TIM_CHANNEL_CH4;
return_value |= LL_TIM_CHANNEL_CH4;
break;
default:
return_value = -1;
Expand Down Expand Up @@ -401,11 +402,11 @@ void HardwareTimer::resumeChannel(uint32_t channel)
case TIMER_OUTPUT_COMPARE_PWM1:
case TIMER_OUTPUT_COMPARE_PWM2: {
#if defined(TIM_CCER_CC1NE)
if (isComplementaryChannel[channel - 1]) {
if (configuredChannels[channel - 1] & COMPLEMENTARY_CHAN_MASK) {
HAL_TIMEx_PWMN_Start(&(_timerObj.handle), timChannel);
} else
}
#endif
{
if (configuredChannels[channel - 1] & REGULAR_CHAN_MASK) {
HAL_TIM_PWM_Start(&(_timerObj.handle), timChannel);
}
}
Expand All @@ -416,11 +417,11 @@ void HardwareTimer::resumeChannel(uint32_t channel)
case TIMER_OUTPUT_COMPARE_FORCED_ACTIVE:
case TIMER_OUTPUT_COMPARE_FORCED_INACTIVE: {
#if defined(TIM_CCER_CC1NE)
if (isComplementaryChannel[channel - 1]) {
if (configuredChannels[channel - 1] & COMPLEMENTARY_CHAN_MASK) {
HAL_TIMEx_OCN_Start(&(_timerObj.handle), timChannel);
} else
}
#endif
{
if (configuredChannels[channel - 1] & REGULAR_CHAN_MASK) {
HAL_TIM_OC_Start(&(_timerObj.handle), timChannel);
}
}
Expand Down Expand Up @@ -731,6 +732,7 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin, Ch

// Identify and configure 2nd associated channel
timAssociatedInputChannel = getAssociatedChannel(channel);
configuredChannels[timAssociatedInputChannel - 1] |= (STM_PIN_INVERTED(pinmap_function(pin, PinMap_TIM))) ? 0x10 : 0x01;
_ChannelMode[timAssociatedInputChannel - 1] = mode;
channelIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_FALLING;
channelIC.ICSelection = TIM_ICSELECTION_INDIRECTTI;
Expand Down Expand Up @@ -759,9 +761,7 @@ void HardwareTimer::setMode(uint32_t channel, TimerModes_t mode, PinName pin, Ch
Error_Handler();
}

#if defined(TIM_CCER_CC1NE)
isComplementaryChannel[channel - 1] = STM_PIN_INVERTED(pinmap_function(pin, PinMap_TIM));
#endif
configuredChannels[channel - 1] |= (STM_PIN_INVERTED(pinmap_function(pin, PinMap_TIM))) ? 0x10 : 0x01;
}
}

Expand Down

0 comments on commit 05b5d68

Please sign in to comment.