SatNOGS-COMMS
4.1.0
A COMMS subsystem for CubeSats
Toggle main menu visibility
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>
28
#include <
satnogs-comms-lib/board.hpp
>
29
#include <
satnogs-comms-lib/radio.hpp
>
30
#include <zephyr/kernel.h>
31
#include <zephyr/task_wdt/task_wdt.h>
32
33
namespace
scl
=
satnogs::comms::lib
;
34
35
namespace
satnogs::comms
36
{
37
38
static
uint32_t
39
min_to_ms(uint32_t mins)
40
{
41
return
(mins * 60000);
42
}
43
44
#if CONFIG_IO_WDG_PERIOD_MINS > 0
45
K_THREAD_STACK_DEFINE
(io_wdg_thread_stack, CONFIG_IO_WDG_STACK_SIZE);
46
#endif
47
53
void
54
io_wdg::start
()
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
73
uint32_t
74
io_wdg::period_mins
()
75
{
76
scoped_lock
lock(&m_mtx);
77
return
m_period_mins;
78
}
79
80
void
81
io_wdg::monitor_thread(
void
*arg1,
void
*arg2,
void
*arg3)
82
{
83
io_wdg
&wdg =
io_wdg::get_instance
();
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
*/
99
wdg.
set_period
(s.get<
settings::param::IO_WDG_PERIOD_MINS
>());
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
134
void
135
io_wdg::set_period
(uint32_t
period_mins
)
136
{
137
scoped_lock
lock(&m_mtx);
138
auto
&s =
settings::get_instance
();
139
period_mins
=
140
etl::clamp(
period_mins
,
io_wdg::min_period_mins
,
io_wdg::max_period_mins
);
141
s.set<
settings::param::IO_WDG_PERIOD_MINS
>(
period_mins
);
142
m_period_mins =
period_mins
;
143
}
144
145
io_wdg::io_wdg
()
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
159
void
160
io_wdg::reset
()
161
{
162
scoped_lock
lock(&m_mtx);
163
m_last_reset_ts = k_uptime_get();
164
}
165
166
}
// namespace satnogs::comms
board.hpp
callbacks.hpp
satnogs::comms::io_wdg
IO and GS watchdog.
Definition
io_wdg.hpp:42
satnogs::comms::io_wdg::period_mins
uint32_t period_mins()
Gets the currently configured period in minutes.
Definition
io_wdg.cpp:74
satnogs::comms::io_wdg::start
void start()
Starts the io_wdg. This method should be called after the board initialization process (satnogs::comm...
Definition
io_wdg.cpp:54
satnogs::comms::io_wdg::io_wdg
io_wdg(io_wdg const &)=delete
satnogs::comms::io_wdg::set_period
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
satnogs::comms::io_wdg::task_wdg_interval
static constexpr uint32_t task_wdg_interval
Definition
io_wdg.hpp:54
satnogs::comms::io_wdg::get_instance
static io_wdg & get_instance()
Singleton access to the io_wdg subsystem.
Definition
io_wdg.hpp:62
satnogs::comms::io_wdg::reset
void reset()
Resets the watchdog timer.
Definition
io_wdg.cpp:160
satnogs::comms::io_wdg::min_period_mins
static constexpr uint32_t min_period_mins
Definition
io_wdg.hpp:47
satnogs::comms::io_wdg::max_period_mins
static constexpr uint32_t max_period_mins
Definition
io_wdg.hpp:52
satnogs::comms::logger::get_instance
static logger & get_instance()
Singleton access to the logger subsystem.
Definition
logger.hpp:102
satnogs::comms::scoped_lock
Implements a scoped lock utilizing the Zephyr mutex.
Definition
scoped_lock.hpp:34
satnogs::comms::settings::get_instance
static settings & get_instance()
Get a singleton access to the settings subsystem.
Definition
settings.hpp:210
satnogs::comms::settings::param::IO_WDG_PERIOD_MINS
@ IO_WDG_PERIOD_MINS
Definition
settings.hpp:81
error_handler.hpp
io.hpp
io_wdg.hpp
satnogs::comms::lib
Definition
ad8318.hpp:30
satnogs::comms
Definition
bsp.cpp:28
satnogs::comms::task_wdt_callback
void task_wdt_callback(int channel_id, void *user_data)
Definition
callbacks.cpp:37
satnogs::comms::K_THREAD_STACK_DEFINE
K_THREAD_STACK_DEFINE(radio_rx_thread_stack, CONFIG_RADIO_RX_THREAD_STACK_SIZE)
radio.hpp
settings.hpp
src
io_wdg.cpp
Generated by
1.17.0