Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
Stm32DccTimerModule< HW > Class Template Reference

Helper module for decoding a DCC signal on an STM32 class microcontroller. More...

#include <Stm32DCCDecoder.hxx>

Public Types

using NRZ_Pin = typename HW::NRZ_Pin
 Exports the input pin to the driver on the module interface.
 

Static Public Member Functions

static uint32_t get_ticks_per_usec ()
 
static void module_init ()
 Called once during construction time.
 
static void module_enable ()
 Called inline with Device::enable().
 
static void module_disable ()
 Called inline with Device::disable().
 
static void trigger_os_interrupt ()
 Calls a software interrupt.
 
static void before_cutout_hook ()
 hook
 
static void dcc_before_cutout_hook ()
 hook
 
static void dcc_packet_finished_hook ()
 hook
 
static void after_feedback_hook ()
 hook
 
static int time_delta_railcom_pre_usec ()
 How many usec later should the railcom cutout start happen.
 
static int time_delta_railcom_mid_usec ()
 How many usec later should the railcom cutout middle happen.
 
static int time_delta_railcom_end_usec ()
 How many usec later should the railcom cutout middle happen.
 
static bool int_get_and_clear_capture_event ()
 Called from the capture interrupt handler.
 
static uint32_t get_capture_counter ()
 Called from the interrupt handler if int_get_and_clear_capture_event said yes.
 
static bool int_get_and_clear_delay_event ()
 Called from the timeout interrupt handler.
 
static void set_cap_timer_delay_usec (int usec)
 Delays a given number of usec using the usec timer feature.
 
static void set_cap_timer_capture ()
 Sets the timer to capture mode.
 
static void set_cap_timer_time ()
 Sets the timer to oneshot (timer) mode.
 
static void stop_cap_timer_time ()
 Called once inline in an interrupt.
 

Static Public Attributes

static constexpr uint32_t TIMER_MAX_VALUE = 0xffff
 This is the counter from which the timer starts counting down.
 
static constexpr uint32_t SAMPLE_PERIOD_CLOCKS = HW::SAMPLE_PERIOD_TICKS
 After how many timer counts we should take one sample for occupancy feedback.
 
static constexpr unsigned Q_SIZE = HW::Q_SIZE
 Length of the device queue.
 

Private Member Functions

 Stm32DccTimerModule ()
 Private constructor. This class cannot be instantiated.
 
 DISALLOW_COPY_AND_ASSIGN (Stm32DccTimerModule)
 

Static Private Member Functions

static void init_timer (TIM_HandleTypeDef *handle, TIM_TypeDef *instance)
 Initializes a timer resource (shared for all channels).
 
static uint32_t get_raw_capture_counter ()
 Helper function to read the raw capture register value.
 
static TIM_TypeDef * capture_timer ()
 
static TIM_HandleTypeDef * capture_timer_handle ()
 
static TIM_TypeDef * usec_timer ()
 
static TIM_HandleTypeDef * usec_timer_handle ()
 
static bool shared_timers ()
 

Static Private Attributes

static TIM_HandleTypeDef captureTimerHandle_
 
static TIM_HandleTypeDef usecTimerHandle_
 
static uint32_t usecTimerStart_
 Holds the base value from where the usec timer should be counting down when the delay_usec call is invoked.
 

Detailed Description

template<class HW>
class Stm32DccTimerModule< HW >

Helper module for decoding a DCC signal on an STM32 class microcontroller.

Usage: Define a structure HW declaring certain constants around your timer usage.

Example hardware definitions:

struct DccDecoderHW
{
using NRZ_Pin = ::DCC_IN_Pin;
static constexpr auto CAPTURE_AF_MODE = GPIO_AF1_TIM3;
static constexpr uint32_t SAMPLE_PERIOD_TICKS = 3000;
static constexpr unsigned Q_SIZE = 6;
static const auto CAPTURE_TIMER = TIM3_BASE;
static constexpr uint32_t CAPTURE_CHANNEL = TIM_CHANNEL_1;
static constexpr auto CAPTURE_IF = TIM_FLAG_CC1;
static constexpr unsigned CAPTURE_FILTER = 0b1000;
static constexpr auto CAPTURE_IRQn = TIM3_IRQn;
static void cap_event_hook() {}
static inline void dcc_before_cutout_hook() {}
static inline void dcc_packet_finished_hook() {}
static inline void after_feedback_hook() {}
static const auto USEC_TIMER = TIM3_BASE;
static constexpr uint32_t USEC_CHANNEL = TIM_CHANNEL_2;
static constexpr auto USEC_IF = TIM_FLAG_CC2;
static_assert(TIM_FLAG_CC2 == TIM_IT_CC2,
"Flag and interrupt registers must be in parallel. The HAL driver is "
"broken.");
static constexpr auto TIMER_IRQn = TIM3_IRQn;
static constexpr auto OS_IRQn = TSC_IRQn;
};
typename HW::NRZ_Pin NRZ_Pin
Exports the input pin to the driver on the module interface.

Definition at line 115 of file Stm32DCCDecoder.hxx.

Member Typedef Documentation

◆ NRZ_Pin

template<class HW >
using Stm32DccTimerModule< HW >::NRZ_Pin = typename HW::NRZ_Pin

Exports the input pin to the driver on the module interface.

Definition at line 119 of file Stm32DCCDecoder.hxx.

Member Function Documentation

◆ after_feedback_hook()

template<class HW >
static void Stm32DccTimerModule< HW >::after_feedback_hook ( )
inlinestatic

hook

Definition at line 173 of file Stm32DCCDecoder.hxx.

◆ before_cutout_hook()

template<class HW >
static void Stm32DccTimerModule< HW >::before_cutout_hook ( )
inlinestatic

hook

Definition at line 155 of file Stm32DCCDecoder.hxx.

◆ capture_timer()

template<class HW >
static TIM_TypeDef * Stm32DccTimerModule< HW >::capture_timer ( )
inlinestaticprivate
Returns
the hardware pointer to the capture timer.

Definition at line 296 of file Stm32DCCDecoder.hxx.

◆ capture_timer_handle()

template<class HW >
static TIM_HandleTypeDef * Stm32DccTimerModule< HW >::capture_timer_handle ( )
inlinestaticprivate
Returns
the HAL handle to the capture timer.

Definition at line 301 of file Stm32DCCDecoder.hxx.

◆ dcc_before_cutout_hook()

template<class HW >
static void Stm32DccTimerModule< HW >::dcc_before_cutout_hook ( )
inlinestatic

hook

Definition at line 161 of file Stm32DCCDecoder.hxx.

◆ dcc_packet_finished_hook()

template<class HW >
static void Stm32DccTimerModule< HW >::dcc_packet_finished_hook ( )
inlinestatic

hook

Definition at line 167 of file Stm32DCCDecoder.hxx.

◆ get_capture_counter()

template<class HW >
uint32_t Stm32DccTimerModule< HW >::get_capture_counter ( )
inlinestatic

Called from the interrupt handler if int_get_and_clear_capture_event said yes.

Returns
the value of the downcounting capture at the time when the edge was captured.

Definition at line 463 of file Stm32DCCDecoder.hxx.

◆ get_raw_capture_counter()

template<class HW >
uint32_t Stm32DccTimerModule< HW >::get_raw_capture_counter ( )
inlinestaticprivate

Helper function to read the raw capture register value.

Returns
the value of the CCx register matching the capture channel.

Definition at line 469 of file Stm32DCCDecoder.hxx.

◆ get_ticks_per_usec()

template<class HW >
static uint32_t Stm32DccTimerModule< HW >::get_ticks_per_usec ( )
inlinestatic
Returns
Timer clocks per usec.

Definition at line 133 of file Stm32DCCDecoder.hxx.

◆ init_timer()

template<class HW >
static void Stm32DccTimerModule< HW >::init_timer ( TIM_HandleTypeDef *  handle,
TIM_TypeDef *  instance 
)
inlinestaticprivate

Initializes a timer resource (shared for all channels).

Parameters
handlepointer to the HAL timer handle.
instancepointer to the timer registers, such as TIM3.

Definition at line 275 of file Stm32DCCDecoder.hxx.

◆ int_get_and_clear_capture_event()

template<class HW >
bool Stm32DccTimerModule< HW >::int_get_and_clear_capture_event ( )
inlinestatic

Called from the capture interrupt handler.

Checks interrupt status, clears interrupt.

Returns
true if the interrupt was generated by a capture event.

Definition at line 451 of file Stm32DCCDecoder.hxx.

◆ int_get_and_clear_delay_event()

template<class HW >
bool Stm32DccTimerModule< HW >::int_get_and_clear_delay_event ( )
inlinestatic

Called from the timeout interrupt handler.

Checks interrupt status, clears timer expired interrupt.

Returns
true if the interrupt was generated by the timer_delay_usec expiring event.

Definition at line 496 of file Stm32DCCDecoder.hxx.

◆ module_disable()

template<class HW >
void Stm32DccTimerModule< HW >::module_disable ( )
static

Called inline with Device::disable().

Definition at line 428 of file Stm32DCCDecoder.hxx.

◆ module_enable()

template<class HW >
void Stm32DccTimerModule< HW >::module_enable ( )
static

Called inline with Device::enable().

Definition at line 348 of file Stm32DCCDecoder.hxx.

◆ module_init()

template<class HW >
void Stm32DccTimerModule< HW >::module_init ( )
static

Called once during construction time.

Definition at line 340 of file Stm32DCCDecoder.hxx.

◆ set_cap_timer_capture()

template<class HW >
static void Stm32DccTimerModule< HW >::set_cap_timer_capture ( )
inlinestatic

Sets the timer to capture mode.

Needed for the digitization of DCC signal bits.

Todo:
consider clearing the overflow flag as well.

Definition at line 235 of file Stm32DCCDecoder.hxx.

◆ set_cap_timer_delay_usec()

template<class HW >
static void Stm32DccTimerModule< HW >::set_cap_timer_delay_usec ( int  usec)
inlinestatic

Delays a given number of usec using the usec timer feature.

Needed for the timing of the railcom cutout. An rcom_interrupt shall be raised after this many usec.

Parameters
usechow much to delay, with the measurement started from the set_cap_timer_time() call (or the previous edge captured before that by the capture timer).

Definition at line 219 of file Stm32DCCDecoder.hxx.

◆ set_cap_timer_time()

template<class HW >
static void Stm32DccTimerModule< HW >::set_cap_timer_time ( )
inlinestatic

Sets the timer to oneshot (timer) mode.

Called once, then set_cap_timer_delay_usec() will be called multiple times, expecting each to deliver an rcom_interrupt().

@TODO __HAL_TIM_DISABLE_IT(capture_timer_handle(), HW::CAPTURE_IF);

Definition at line 245 of file Stm32DCCDecoder.hxx.

◆ shared_timers()

template<class HW >
static bool Stm32DccTimerModule< HW >::shared_timers ( )
inlinestaticprivate
Returns
true if the same timer resource (on different channels) is used for both the capture and the usec timer.

Definition at line 324 of file Stm32DCCDecoder.hxx.

◆ stop_cap_timer_time()

template<class HW >
static void Stm32DccTimerModule< HW >::stop_cap_timer_time ( )
inlinestatic

Called once inline in an interrupt.

Signals that the delay timer is not needed anymore.

Definition at line 258 of file Stm32DCCDecoder.hxx.

◆ time_delta_railcom_end_usec()

template<class HW >
static int Stm32DccTimerModule< HW >::time_delta_railcom_end_usec ( )
inlinestatic

How many usec later should the railcom cutout middle happen.

Definition at line 191 of file Stm32DCCDecoder.hxx.

◆ time_delta_railcom_mid_usec()

template<class HW >
static int Stm32DccTimerModule< HW >::time_delta_railcom_mid_usec ( )
inlinestatic

How many usec later should the railcom cutout middle happen.

Definition at line 185 of file Stm32DCCDecoder.hxx.

◆ time_delta_railcom_pre_usec()

template<class HW >
static int Stm32DccTimerModule< HW >::time_delta_railcom_pre_usec ( )
inlinestatic

How many usec later should the railcom cutout start happen.

Definition at line 179 of file Stm32DCCDecoder.hxx.

◆ trigger_os_interrupt()

template<class HW >
static void Stm32DccTimerModule< HW >::trigger_os_interrupt ( )
inlinestatic

Calls a software interrupt.

Definition at line 149 of file Stm32DCCDecoder.hxx.

◆ usec_timer()

template<class HW >
static TIM_TypeDef * Stm32DccTimerModule< HW >::usec_timer ( )
inlinestaticprivate
Returns
the hardware pointer to the usec timer.

Definition at line 307 of file Stm32DCCDecoder.hxx.

◆ usec_timer_handle()

template<class HW >
static TIM_HandleTypeDef * Stm32DccTimerModule< HW >::usec_timer_handle ( )
inlinestaticprivate
Returns
the HAL handle to the usec timer (which may alias to the capture timer handle).

Definition at line 313 of file Stm32DCCDecoder.hxx.

Member Data Documentation

◆ captureTimerHandle_

template<class HW >
TIM_HandleTypeDef Stm32DccTimerModule< HW >::captureTimerHandle_
staticprivate

Definition at line 265 of file Stm32DCCDecoder.hxx.

◆ Q_SIZE

template<class HW >
constexpr unsigned Stm32DccTimerModule< HW >::Q_SIZE = HW::Q_SIZE
staticconstexpr

Length of the device queue.

Definition at line 130 of file Stm32DCCDecoder.hxx.

◆ SAMPLE_PERIOD_CLOCKS

template<class HW >
constexpr uint32_t Stm32DccTimerModule< HW >::SAMPLE_PERIOD_CLOCKS = HW::SAMPLE_PERIOD_TICKS
staticconstexpr

After how many timer counts we should take one sample for occupancy feedback.

Definition at line 128 of file Stm32DCCDecoder.hxx.

◆ TIMER_MAX_VALUE

template<class HW >
constexpr uint32_t Stm32DccTimerModule< HW >::TIMER_MAX_VALUE = 0xffff
staticconstexpr

This is the counter from which the timer starts counting down.

When the timer overflows, it starts from this value.

Definition at line 125 of file Stm32DCCDecoder.hxx.

◆ usecTimerHandle_

template<class HW >
TIM_HandleTypeDef Stm32DccTimerModule< HW >::usecTimerHandle_
staticprivate

Definition at line 266 of file Stm32DCCDecoder.hxx.

◆ usecTimerStart_

template<class HW >
uint32_t Stm32DccTimerModule< HW >::usecTimerStart_
staticprivate

Holds the base value from where the usec timer should be counting down when the delay_usec call is invoked.

This is set from the last capture edge when the mode is switched to the usec timer.

Definition at line 270 of file Stm32DCCDecoder.hxx.


The documentation for this class was generated from the following file: