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

Inverts the run direction of the capture counter in the STM32 Dcc Decoder. #620

Merged
merged 5 commits into from
Apr 16, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 12 additions & 0 deletions src/freertos_drivers/common/DccDecoder.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,10 @@ private:
inputData_->flush();
};

#ifdef DCC_DECODER_DEBUG
LogRing<uint16_t, 256> debugLog_;
#endif

typedef DCCPacket input_data_type;
DeviceBuffer<DCCPacket> *inputData_ {
DeviceBuffer<DCCPacket>::create(Module::Q_SIZE)};
Expand Down Expand Up @@ -291,6 +295,11 @@ __attribute__((optimize("-O3"))) void DccDecoder<Module>::interrupt_handler()
// Debug::DccDecodeInterrupts::toggle();
uint32_t raw_new_value = Module::get_capture_counter();
uint32_t old_value = lastTimerValue_;
#ifdef DCC_DECODER_DEBUG
debugLog_.add(0);
debugLog_.add(old_value);
debugLog_.add(raw_new_value);
#endif
if (raw_new_value > old_value) {
// Timer has overflowed.
if (nextSample_ < old_value) {
Expand All @@ -311,6 +320,9 @@ __attribute__((optimize("-O3"))) void DccDecoder<Module>::interrupt_handler()
nextSample_ -= Module::SAMPLE_PERIOD_CLOCKS;
}
uint32_t new_value = old_value - raw_new_value;
#ifdef DCC_DECODER_DEBUG
debugLog_.add(new_value);
#endif
bool cutout_just_finished = false;
decoder_.process_data(new_value);
if (decoder_.before_dcc_cutout())
Expand Down
22 changes: 10 additions & 12 deletions src/freertos_drivers/common/SimpleLog.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -64,24 +64,22 @@ private:
/// Actual class that keeps 8 log entries of one byte each.
typedef SimpleLog<uint64_t> LogBuffer;


/// Alternative for hundreds of entries.
template<class T, int N> class LogRing {
template <class T, int N> class LogRing
{
public:
void add(T data) {
data_[next_] = data;
last_ = data_ + next_;
if (next_) {
--next_;
} else {
next_ = N-1;
void add(T data)
{
data_[next_++] = data;
if (next_ >= N)
{
next_ = 0;
}
}

private:
T data_[N];
unsigned next_{N};
T* last_{data_};
unsigned next_ {0};
};

#endif // _FREERTOS_DRIVERS_COMMON_SIMPLELOG_HXX_
41 changes: 36 additions & 5 deletions src/freertos_drivers/st/Stm32DCCDecoder.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public:
// This code handles underflow of the timer correctly. We cannot wait
// longer than one full cycle though (65 msec -- typical RailCom waits
// are 20-500 usec).
uint32_t new_match_v = usecTimerStart_ + TIMER_MAX_VALUE + 1 - usec;
uint32_t new_match_v = usecTimerStart_ + usec;
new_match_v &= 0xffff;
__HAL_TIM_SET_COMPARE(
usec_timer_handle(), HW::USEC_CHANNEL, new_match_v);
Expand All @@ -245,7 +245,7 @@ public:
static void set_cap_timer_time()
{
if (shared_timers()) {
usecTimerStart_ = get_capture_counter();
usecTimerStart_ = get_raw_capture_counter();
} else {
usecTimerStart_ = __HAL_TIM_GET_COUNTER(usec_timer_handle());
}
Expand Down Expand Up @@ -280,13 +280,18 @@ private:
// 1 usec per tick
handle->Init.Prescaler = configCPU_CLOCK_HZ / 1000000;
handle->Init.ClockDivision = 0;
handle->Init.CounterMode = TIM_COUNTERMODE_DOWN;
handle->Init.CounterMode = TIM_COUNTERMODE_UP;
handle->Init.RepetitionCounter = 0;
handle->Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;

HASSERT(HAL_TIM_IC_DeInit(handle) == HAL_OK);
HASSERT(HAL_TIM_IC_Init(handle) == HAL_OK);
}

/// Helper function to read the raw capture register value.
/// @return the value of the CCx register matching the capture channel.
static inline uint32_t get_raw_capture_counter();

/// @return the hardware pointer to the capture timer.
static TIM_TypeDef *capture_timer()
{
Expand Down Expand Up @@ -457,8 +462,34 @@ bool Stm32DccTimerModule<HW>::int_get_and_clear_capture_event()

template <class HW> uint32_t Stm32DccTimerModule<HW>::get_capture_counter()
{
return HAL_TIM_ReadCapturedValue(
capture_timer_handle(), HW::CAPTURE_CHANNEL);
// Simulates down-counting.
return TIMER_MAX_VALUE - get_raw_capture_counter();
}

template <class HW> uint32_t Stm32DccTimerModule<HW>::get_raw_capture_counter()
{
// This if structure will be optimized away.
if (HW::CAPTURE_CHANNEL == TIM_CHANNEL_1)
{
return capture_timer()->CCR1;
}
else if (HW::CAPTURE_CHANNEL == TIM_CHANNEL_2)
{
return capture_timer()->CCR2;
}
else if (HW::CAPTURE_CHANNEL == TIM_CHANNEL_3)
{
return capture_timer()->CCR3;
}
else if (HW::CAPTURE_CHANNEL == TIM_CHANNEL_4)
{
return capture_timer()->CCR4;
}
else
{
DIE("Unknown capture channel");
return 0;
}
}

template <class HW>
Expand Down