SatCat5
task.h
Go to the documentation of this file.
1 // Copyright 2024-2025 The Aerospace Corporation.
3 // This file is a part of SatCat5, licensed under CERN-OHL-W v2 or later.
12 
13 #pragma once
14 
15 #include <satcat5/build_date.h>
16 #include <satcat5/polling.h>
17 #include <satcat5/log.h>
18 extern "C"{
19 #include <FreeRTOS.h>
20 #include <task.h>
21 };
22 #include <hal_freertos/interrupts.h>
23 #include <hal_freertos/tick_timer.h>
24 
25 // This HAL requires static allocation and tick hooks to be enabled.
26 #if (configSUPPORT_STATIC_ALLOCATION == 0)
27 #error FreeRTOS static allocation must be enabled for SatCat5.
28 #endif // (configSUPPORT_STATIC_ALLOCATION == 0)
29 #if (configUSE_TICK_HOOK == 0)
30 #error The FreeRTOS tick hook must be enabled for SatCat5.
31 #endif // (configUSE_TICK_HOOK == 0)
32 
33 #ifndef portYIELD_FROM_ISR
34 #define portYIELD_FROM_ISR(x) portEND_SWITCHING_ISR(x)
35 #endif
36 
37 namespace satcat5 {
38  namespace freertos {
39 
42  template <configSTACK_DEPTH_TYPE TASK_SIZE, unsigned TASK_PRIORITY>
43  class StaticTask {
44  public:
46  inline void suspend(void) {
47  vTaskSuspend(m_task_handle);
48  }
50  inline void resume(void) {
51  vTaskResume(m_task_handle);
52  }
54  inline void resume_from_isr(void) {
55  xTaskResumeFromISR(m_task_handle);
56  }
58  inline TaskHandle_t get_task_handle(void) {
59  return m_task_handle;
60  }
62  inline void notify(void) {
63  xTaskNotifyGive(m_task_handle);
64  }
66  inline void notify_from_isr(void) {
67  BaseType_t yield_flag = pdFALSE;
68  vTaskNotifyGiveFromISR(m_task_handle, &yield_flag);
69  portYIELD_FROM_ISR(yield_flag);
70  }
71 
72  protected:
73  // Class Constructor
74  StaticTask(
75  const char* task_name,
76  TaskFunction_t task_function,
77  void * const task_params = NULL)
78  {
79  // Create Static Task
80  m_task_handle = xTaskCreateStatic(
81  task_function,
82  task_name,
83  TASK_SIZE,
84  task_params,
85  TASK_PRIORITY,
86  m_stack,
87  &m_static_task);
88  }
89 
90  // Member Variables
91  TaskHandle_t m_task_handle;
92  StackType_t m_stack[TASK_SIZE];
93  StaticTask_t m_static_task;
94  };
95 
98  extern TaskHandle_t satcat_task_handle;
99 
105  template <configSTACK_DEPTH_TYPE TASK_SIZE, unsigned TASK_PRIORITY>
106  class SatCatTask : public StaticTask<TASK_SIZE, TASK_PRIORITY> {
107  public:
108  // Constructor
109  SatCatTask(
110  ControllerFreeRTOS* irq_controller,
111  TickTimer* tick_timer)
113  "SatCat OS Task",
114  task,
115  this)
116  , m_irq_controller(irq_controller)
117  , m_tick_timer(tick_timer)
118  {
120  }
121 
125  static void task(void* pvParams)
126  {
127  // Link Reference Tick to Timekeeper, init Interrupt Controller
128  SatCatTask* params = (SatCatTask*) pvParams;
129  satcat5::poll::timekeeper.set_clock(params->m_tick_timer);
130  params->m_irq_controller->irq_start(params->m_tick_timer);
131 
132  // Start-up Message
133  satcat5::log::Log(satcat5::log::INFO,
134  "FreeRTOS with SatCat5!\r\n\t"
135  "Built ").write(satcat5::get_sw_build_string());
136 
137  // Main loop services all demands then returns to the scheduler.
138  while (1) {
139  satcat5::poll::service_all();
140  vTaskSuspend(params->m_task_handle);
141  }
142  }
143 
144  // Member Variables, left public for access from `task()`.
147  };
148  } // namespace freertos
149 } // namespace satcat5
150 
Build-date reporting.
const char * get_sw_build_string()
Return a pointer to the ISO8601 date and time constructed at build time.
Definition: build_date.cc:79
FreeRTOS implementation of the "InterruptController" class.
void irq_start(satcat5::util::TimeRef *timer=0)
Initialize FreeRTOS controller and start SatCat5 interrupts.
Instantiate a FreeRTOS statically-allocated Task that services the SatCat5 core loop.
Definition: task.h:106
static void task(void *pvParams)
Core task entry function, performs setup then calls poll::service_all() on a loop.
Definition: task.h:125
TickTimer * m_tick_timer
Reference Timer.
Definition: task.h:146
ControllerFreeRTOS * m_irq_controller
Interrupt Controller.
Definition: task.h:145
Instantiate a FreeRTOS statically-allocated Task.
Definition: task.h:43
void resume_from_isr(void)
Resumes suspended task (should be called from ISR)
Definition: task.h:54
void resume(void)
Resumes suspended task.
Definition: task.h:50
void notify(void)
Notify Task.
Definition: task.h:62
TaskHandle_t m_task_handle
Task Handle.
Definition: task.h:91
StackType_t m_stack[TASK_SIZE]
Task Stack.
Definition: task.h:92
StaticTask_t m_static_task
Task internals.
Definition: task.h:93
void suspend(void)
Suspends running task.
Definition: task.h:46
void notify_from_isr(void)
Notify Task (should be called from ISR)
Definition: task.h:66
TaskHandle_t get_task_handle(void)
Returns task handle of created task.
Definition: task.h:58
SatCat5 time reference using the FreeRTOS tick counter.
Definition: tick_timer.h:19
The Log class creates and formats one log message.
Definition: log.h:195
Log & write(const char *str)
Formatting methods for various data types.
Definition: log.cc:198
void set_clock(satcat5::util::TimeRef *timer)
Immediately set the system time reference.
Definition: polling.cc:250
Diagnostic logging to UART and/or Ethernet ports.
Core event-processing loop for SatCat5 software.
Provides a FreeRTOS task function and hooks for using SatCat5 as a task inside a larger FreeRTOS proj...
TaskHandle_t satcat_task_handle
Task handle, must be outside StaticTask for access by vApplicationTickHook()
Definition: task.cc:9