Repository navigation
Expand file tree
/
Copy pathapp.c
More file actions
162 lines (156 loc) · 6.8 KB
/
Copy pathapp.c
File metadata and controls
162 lines (156 loc) · 6.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: MIT
// Copyright (c) 2026 The RadioFoundry Authors
// The application: the start-up task brings up the board and starts the core tasks, then stays on as housekeeping
// (power switch watch).
//
// Tasks (FreeRTOS, configMAX_PRIORITIES 7, heap 28 KB on the STM32F405 radios, shared by stacks and queues; stack sizes in
// words):
// timer service 6 256 FreeRTOS
// start / house 5 512 this file: start-up, then a 100 ms power switch poll, the settings saves and the
// UI's codeplug writes (storage_job_poll; sleeps nearly always)
// audio 5 1280 board (DMR): PCM blocks, the vocoder runs here (created at the first DMR call)
// dmr 5 512 board (DMR): modem events to the DMR call stack's queue or the hotspot's
// radio 4 768 core/radio: requests, 25 ms status poll, squelch, PTT, TX time-out; 10 ms DMR call
// stack poll (512 without DMR); the voice sink / source and the hotspot radio ops
// input 3 256 core/input: 10 ms key scan, gestures, PTT / monitor straight to the radio core
// host 3 512 core/hostlink: the host port (USB CDC or a UART): MMDVM hotspot, CPS
// usb 3 256 drivers/usb: TinyUSB device stack (tud_task)
// ui 2 - core/ui: chosen there (keep it at or below 2 and within about 1024 words)
// idle 0 128 FreeRTOS
// The radio task is above input so a PTT edge is acted on as soon as it is queued; input is above the UI so a busy
// redraw never delays a key scan.
#include "board.h"
#include "hal_time.h"
#include "codeplug/codeplug.h"
#include "storage/storage.h"
#include "settings/settings.h"
#include "radio/radio.h"
#include "input/input.h"
#include "ui/ui.h"
#include "ui/ui_svc.h"
#include "config.h"
#if CONFIG_HOSTLINK
#include "hostlink/hostlink.h"
#endif
#if CONFIG_TFTP
#include "net/tftp_codeplug.h"
#endif
#if CONFIG_CPS_OPENFW
#include "cps/cps_openfw.h"
#endif
#if CONFIG_CRASH_LOG
#include "crash/crash.h"
#endif
#if CONFIG_CPS_FAST
#include "cps/cps_fast.h"
#endif
#include "FreeRTOS.h"
#include "task.h"
#define START_TASK_PRIORITY 5
// words: the radio core's set-up runs here at boot (radio_sm_init, 584 bytes of frame on an HR_C7000 radio, then the APRS
// identity load or a DMR VFO's contact search down to the flash driver); 512 left that radio 88 bytes (make stack-check)
#define START_TASK_STACK 640
#define HOUSEKEEPING_MS 100
#define POWER_OFF_TX_MS 200 // after the PTT release, before the latch drops
#define POWER_OFF_DEREG_MS 3000 // at most this long for the ARS deregistration (gotcha 7.4)
#define POWER_OFF_STAY_MS 2000 // auto power off: still running this long after the latch dropped = the knob holds it
// knob: the user switched off (or the battery is flat), so the supply is going away whatever happens; otherwise this
// is the UI's auto power off, and the knob may still hold the supply: then the radio carries on and says so
static void power_off(bool knob)
{
radio_req_ptt(false); // stop any transmission first
radio_req_power_off(); // then what has to go out before the supply drops (ARS deregistration)
hal_delay_ms(POWER_OFF_TX_MS);
for (unsigned t = 0; t < POWER_OFF_DEREG_MS && radio_power_off_busy(); t += 50) {
hal_delay_ms(50);
}
settings_flush(); // unsaved changes (the debounce hadn't run out yet)
board.display->set_backlight(0);
board.power->latch(false);
for (uint32_t t = 0; knob || t < POWER_OFF_STAY_MS; t += 100) { // the supply goes away; nothing more to do
hal_delay_ms(100);
}
board.power->latch(true); // still here: the knob is on
radio_req_power_off_cancel();
input_inject_wake(); // the backlight comes back for the UI's banner
ui_event_t ev = { .kind = UI_EV_POWER_OFF_FAILED };
ui_notify(&ev); // "Couldn't switch off"; the UI waits a whole period again
}
// The stored "Use anyway" (settings_t.cp_flags) mirrors the codeplug's: set when the owner chose it, cleared when a
// codeplug was written since (codeplug_written) or a factory reset (docs/spec/codeplug-native.md 2)
static void accept_sync(void)
{
codeplug_accept_t a;
if (!codeplug_accept_take(&a)) {
return;
}
settings_t *st = settings_begin();
st->cp_flags = a == CODEPLUG_ACCEPT_YES ? (uint8_t)(st->cp_flags | SETTINGS_CP_ACCEPTED)
: (uint8_t)(st->cp_flags & ~SETTINGS_CP_ACCEPTED);
settings_end(hal_millis());
}
static void startup_task(void *arg)
{
(void)arg;
board_init();
board.display->init();
board.display->set_backlight(100);
board.input->init();
storage_init(); // finishes a codeplug sector rewrite a power cut interrupted
bool written = false; // a commit below wrote a new codeplug
#if CONFIG_CPS_FAST
written = cps_fast_resume(); // and a fast codeplug protocol commit (docs/spec/cps-fast.md 7)
#endif
#if CONFIG_TFTP
written = tftp_codeplug_resume() || written; // and a TFTP codeplug write's commit (before the codeplug is read)
#endif
settings_init();
settings_t *cp = settings_begin();
if (written) {
cp->cp_flags &= (uint8_t)~SETTINGS_CP_ACCEPTED; // "Use anyway" was for the codeplug replaced
}
codeplug_accept((cp->cp_flags & SETTINGS_CP_ACCEPTED) != 0);
board_rx_ext_set((cp->cp_flags & SETTINGS_CP_RX_EXT) != 0); // before the radio core checks the stored VFO against the bands
settings_end(hal_millis());
codeplug_init();
board.radio->init();
radio_core_start();
input_start();
ui_start();
#if CONFIG_HOSTLINK
// The two fields read in place: a settings copy here would stay on this task's stack under the radio core's
// set-up above, its deepest path (stack-check.md)
const settings_t *st = settings_begin();
bool hotspot_allowed = st->hotspot_allowed != 0;
uint8_t net_flags = st->net_flags;
settings_end(hal_millis()); // nothing changed: nothing to save
hostlink_set_hotspot_allowed(hotspot_allowed);
#if CONFIG_TFTP
tftp_codeplug_allow_write((net_flags & SETTINGS_NET_TFTP_NO_WRITE) == 0);
#else
(void)net_flags;
#endif
#if CONFIG_CPS_OPENFW
cps_openfw_start(); // registers the CPS handler with the host link
#endif
hostlink_start();
#endif // after the settings: the host link personality depends on them
for (;;) {
hal_delay_ms(HOUSEKEEPING_MS);
settings_poll(hal_millis()); // saves here, so no other task waits for the flash
storage_job_poll(); // and the codeplug writes the UI asks for (save to channel, factory reset)
accept_sync();
if (!board.power->switch_on() || radio_power_off_wanted()) {
power_off(true); // the knob, or the radio core after a long critical battery (battery_off_s)
} else if (ui_app_power_off_due()) {
power_off(false); // Settings > Power saving > Auto power off
}
}
}
void app_start(void)
{
#if CONFIG_CRASH_LOG
crash_init(); // the record of a crash before this start, before anything can crash again
#endif
xTaskCreate(startup_task, "start", START_TASK_STACK, NULL, START_TASK_PRIORITY, NULL);
}