SatNOGS-COMMS  4.1.0
A COMMS subsystem for CubeSats
Loading...
Searching...
No Matches
io_wdg.cpp
Go to the documentation of this file.
1/*
2 * SatNOGS-COMMS MCU software
3 *
4 * Copyright (C) 2024, Libre Space Foundation <http://libre.space>
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 *
19 * SPDX-License-Identifier: GNU General Public License v3.0 or later
20 */
21
22#include "io_wdg.hpp"
23#include "callbacks.hpp"
24#include "error_handler.hpp"
25#include "io.hpp"
26#include "settings.hpp"
27#include <etl/algorithm.h>
30#include <zephyr/kernel.h>
31#include <zephyr/task_wdt/task_wdt.h>
32
33namespace scl = satnogs::comms::lib;
34
35namespace satnogs::comms
36{
37
38static uint32_t
39min_to_ms(uint32_t mins)
40{
41 return (mins * 60000);
42}
43
44#if CONFIG_IO_WDG_PERIOD_MINS > 0
45K_THREAD_STACK_DEFINE(io_wdg_thread_stack, CONFIG_IO_WDG_STACK_SIZE);
46#endif
47
53void
55{
56#if CONFIG_IO_WDG_PERIOD_MINS > 0
57 m_tid = k_thread_create(&m_thread_data, io_wdg_thread_stack,
58 K_THREAD_STACK_SIZEOF(io_wdg_thread_stack),
59 monitor_thread, NULL, NULL, NULL, CONFIG_IO_WDG_PRIO,
60 0, K_NO_WAIT);
61 if (!m_tid) {
62 k_oops();
63 }
64 k_thread_name_set(m_tid, "io_wdg");
65#endif
66}
67
73uint32_t
75{
76 scoped_lock lock(&m_mtx);
77 return m_period_mins;
78}
79
80void
81io_wdg::monitor_thread(void *arg1, void *arg2, void *arg3)
82{
84 auto &s = settings::get_instance();
85
86 int64_t ellapsed_time = 0;
87 uint32_t rx_frame_uhf = 0;
88 uint32_t rx_frame_sband = 0;
89
90 int task_wdt_id = task_wdt_add(task_wdg_interval, task_wdt_callback,
91 (void *)k_current_get());
92 task_wdt_feed(task_wdt_id);
93
94 /*
95 * Make sure that the period is properly clamped both in the settings and
96 * within the class. This will avoid any misconfiguration in case an invalid
97 * value is somehow retrieved by the settings subsystem
98 */
100
101 int64_t curr_time = k_uptime_get();
102 while (1) {
103 task_wdt_feed(task_wdt_id);
104 wdg.reset();
105
106 while (1) {
107 int64_t curr_time = k_uptime_get();
108 uint32_t period_ms = min_to_ms(wdg.period_mins());
109 if (wdg.m_last_reset_ts + period_ms > curr_time) {
110 task_wdt_feed(task_wdt_id);
111 } else {
112 auto &log = logger::get_instance();
113 log.log("IO watchdog resetting the system");
114 /* Instruct a reboot. */
115 sys_reboot(SYS_REBOOT_COLD);
116 /* If this fails for any reason, rely on the IWDG */
117 k_sleep(K_FOREVER);
118 }
119 k_sleep(K_SECONDS(10));
120 }
121 }
122}
123
134void
144
146 : m_period_mins(max_period_mins), // avoid calling the stack hungry settings
147 // subsystem at construction
148 m_last_reset_ts(0)
149{
150 k_mutex_init(&m_mtx);
151}
152
159void
161{
162 scoped_lock lock(&m_mtx);
163 m_last_reset_ts = k_uptime_get();
164}
165
166} // namespace satnogs::comms
IO and GS watchdog.
Definition io_wdg.hpp:42
uint32_t period_mins()
Gets the currently configured period in minutes.
Definition io_wdg.cpp:74
void start()
Starts the io_wdg. This method should be called after the board initialization process (satnogs::comm...
Definition io_wdg.cpp:54
io_wdg(io_wdg const &)=delete
void set_period(uint32_t period_mins)
Updates the watchdog period in both the currently running instance and at the persistent storage.
Definition io_wdg.cpp:135
static constexpr uint32_t task_wdg_interval
Definition io_wdg.hpp:54
static io_wdg & get_instance()
Singleton access to the io_wdg subsystem.
Definition io_wdg.hpp:62
void reset()
Resets the watchdog timer.
Definition io_wdg.cpp:160
static constexpr uint32_t min_period_mins
Definition io_wdg.hpp:47
static constexpr uint32_t max_period_mins
Definition io_wdg.hpp:52
static logger & get_instance()
Singleton access to the logger subsystem.
Definition logger.hpp:102
Implements a scoped lock utilizing the Zephyr mutex.
static settings & get_instance()
Get a singleton access to the settings subsystem.
Definition settings.hpp:210
void task_wdt_callback(int channel_id, void *user_data)
Definition callbacks.cpp:37
K_THREAD_STACK_DEFINE(radio_rx_thread_stack, CONFIG_RADIO_RX_THREAD_STACK_SIZE)