SatCat5
systick_timer.cc
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
5 
6 // SatCat
7 #include <satcat5/polling.h>
8 
9 // SAMV71 Drivers
10 extern "C" {
11  #include <samv71.h>
12 };
13 
14 // SatCat HAL
15 #include <hal_samv71/systick_timer.h>
16 
18 
20 // Registers & Constants
22 
23 // SysTick Register Map
24 static const unsigned REGADDR_CTRL = 0;
25 static const unsigned REGADDR_LOAD = 1;
26 static const unsigned REGADDR_CURRENT_VALUE = 2;
27 
28 // Constants
29 static const unsigned SYSTICK_CLK_BIT = (1UL << 2UL);
30 static const unsigned SYSTICK_INT_BIT = (1UL << 1UL);
31 static const unsigned SYSTICK_ENABLE_BIT = (1UL << 0UL);
32 
34 // Class
36 
37 SysTickTimer::SysTickTimer(
38  const u32 cpu_freq_hz,
39  const u32 tick_rate_hz)
40  : TimeRef(tick_rate_hz)
41  , HandlerSAMV71("SysTick IRQ", SysTick_IRQn)
42  , m_ctrl((u32*)SysTick_BASE)
43  , m_callback(0)
44 {
45  // Stop & Clear SysTick
46  m_ctrl[REGADDR_CTRL] = 0;
47  m_ctrl[REGADDR_CURRENT_VALUE] = 0;
48 
49  // Configure & Enable SysTick
50  m_ctrl[REGADDR_LOAD] = (cpu_freq_hz / tick_rate_hz) - 1;
51  m_ctrl[REGADDR_CTRL] = SYSTICK_CLK_BIT | SYSTICK_INT_BIT | SYSTICK_ENABLE_BIT;
52 }
53 
55 {
56  return m_tick_num;
57 }
58 
60 {
61  m_callback = callback;
62 }
63 
65 {
66  m_tick_num++;
67  if (m_callback) m_callback->request_poll();
68 }
69 
An "OnDemand" object is polled only on request.
Definition: polling.h:131
void request_poll()
Call this method to request polling at a later time.
Definition: polling.cc:208
Interrupt handler interface for the Microchip SAM V71.
Microchip SAM V71 implementation of the "TimeRef" API.
Definition: systick_timer.h:23
u32 raw() override
Get raw tick-count,.
void irq_event() override
Method called whenever an interrupt is triggered.
void timer_callback(satcat5::poll::OnDemand *callback)
Set callback for timer events.
Core event-processing loop for SatCat5 software.