SatCat5
cfgbus_timer.h
Go to the documentation of this file.
1 // Copyright 2021-2024 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
7 
8 #pragma once
9 
10 #include <satcat5/cfgbus_interrupt.h>
11 #include <satcat5/timeref.h>
12 
13 namespace satcat5 {
14  namespace cfg {
22  class Timer
23  : public satcat5::util::TimeRef
24  , protected satcat5::cfg::Interrupt
25  {
26  public:
29  Timer(satcat5::cfg::ConfigBus* cfg, unsigned devaddr)
30  : Timer(cfg, devaddr, hw_ticks_per_sec(cfg, devaddr)) {}
31 
35  inline Timer(satcat5::cfg::ConfigBus* cfg, unsigned devaddr, u32 refclk_hz)
36  : TimeRef(refclk_hz)
37  , Interrupt(cfg, devaddr, REGADDR_TIMER_IRQ)
38  , m_ctrl(cfg->get_register(devaddr))
39  , m_callback(0)
40  {
41  m_ctrl[REGADDR_WDOG] = WDOG_PAUSE;
42  }
43 
47  u32 raw() override;
48 
54 
58  void timer_interval(unsigned usec);
59 
63 
68  void wdog_disable();
69 
75  void wdog_update(u32 usec);
76 
77  protected:
78  // Writing the special PAUSE value stops the watchdog.
79  static constexpr u32 WDOG_PAUSE = (u32)(-1);
80 
81  // Define the hardware register map:
82  static constexpr unsigned REGADDR_WDOG = 0;
83  static constexpr unsigned REGADDR_CPU_HZ = 1;
84  static constexpr unsigned REGADDR_PERF_CTR = 2;
85  static constexpr unsigned REGADDR_LAST_EVT = 3;
86  static constexpr unsigned REGADDR_TIMER_LEN = 4;
87  static constexpr unsigned REGADDR_TIMER_IRQ = 5;
88 
89  // Access REGADDR_CPU_HZ before m_ctrl is initialized.
90  static inline u32 hw_ticks_per_sec(satcat5::cfg::ConfigBus* cfg, unsigned devaddr) {
91  satcat5::cfg::Register reg = cfg->get_register(devaddr);
92  return reg[REGADDR_CPU_HZ];
93  }
94 
95  // Timer interrupt handler.
96  void irq_event() override;
97 
98  // Link to the hardware register map.
100 
101  // Callback object is polled after each timer interrupt.
102  satcat5::poll::OnDemand* m_callback;
103  };
104  }
105 }
Generic ConfigBus API.
Definition: cfgbus_core.h:124
satcat5::cfg::Register get_register(unsigned dev, unsigned reg=0)
Create register-map object for the given device address.
Definition: cfgbus_core.cc:101
Event-handler for individual ConfigBus interrupts.
ConfigBus Timer driver.
Definition: cfgbus_timer.h:25
satcat5::util::TimeVal last_event()
Read timestamp of the last external event signal.
Definition: cfgbus_timer.cc:16
void timer_interval(unsigned usec)
Change the timer-interrupt interval.
Definition: cfgbus_timer.cc:20
void wdog_disable()
Disable the hardware watchdog function.
Definition: cfgbus_timer.cc:28
void timer_callback(satcat5::poll::OnDemand *callback)
Set the callback for timer-interrupt notifications.
Definition: cfgbus_timer.cc:24
Timer(satcat5::cfg::ConfigBus *cfg, unsigned devaddr)
Auto-configuration constructor.
Definition: cfgbus_timer.h:29
void irq_event() override
Interrupt service routine.
Definition: cfgbus_timer.cc:36
u32 raw() override
Read the current time.
Definition: cfgbus_timer.cc:12
void wdog_update(u32 usec)
Resume or reset the watchdog countdown.
Definition: cfgbus_timer.cc:32
Timer(satcat5::cfg::ConfigBus *cfg, unsigned devaddr, u32 refclk_hz)
Manual-configuration constructor.
Definition: cfgbus_timer.h:35
Pointer-like wrapper for one or more ConfigBus registers.
Definition: cfgbus_core.h:76
An "OnDemand" object is polled only on request.
Definition: polling.h:131
The TimeRef API provides access to a monotonic time-counter.
Definition: timeref.h:142
constexpr TimeRef(u64 ticks_per_sec)
Constructor accepts a scale-factor for conversion to real-time.
Definition: timeref.h:146
Timestamp for measuring elapsed time.
Definition: timeref.h:48
TimeRef and TimeVal define the API for monotonic timers.