-
Notifications
You must be signed in to change notification settings - Fork 9
/
runner.cc
303 lines (257 loc) · 9.33 KB
/
runner.cc
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include "runner.h"
#include <memory>
#include <vector>
#include "FreeRTOS.h"
#include "FreeRTOSConfig.h"
#include "base.h"
#include "configuration.h"
#include "hardware/timer.h"
#include "hardware/watchdog.h"
#include "semphr.h"
#include "task.h"
#include "timers.h"
#include "usb.h"
#include "utils.h"
static std::vector<std::shared_ptr<GenericInputDevice>> input_devices;
static std::vector<std::shared_ptr<GenericOutputDevice>> output_devices;
static std::vector<std::shared_ptr<GenericOutputDevice>> slow_output_devices;
static TaskHandle_t input_task_handle = NULL;
static TimerHandle_t input_timer_handle = NULL;
static TaskHandle_t output_task_handle = NULL;
static TimerHandle_t output_timer_handle = NULL;
static TaskHandle_t slow_output_task_handle = NULL;
static TimerHandle_t slow_output_timer_handle = NULL;
static SemaphoreHandle_t semaphore;
static bool is_config_mode;
static bool update_config_flag;
namespace runner {
Status RunnerInit() {
input_devices = DeviceRegistry::GetInputDevices();
output_devices = DeviceRegistry::GetOutputDevices(
/*is_slow=*/false);
slow_output_devices = DeviceRegistry::GetOutputDevices(
/*is_slow=*/true);
volatile size_t output_size = output_devices.size();
volatile size_t slowoutput_size = slow_output_devices.size();
volatile size_t input_size = input_devices.size();
if (USBInit() != OK) {
return ERROR;
}
is_config_mode = false;
update_config_flag = false;
semaphore = xSemaphoreCreateBinary();
xSemaphoreGive(semaphore);
return OK;
}
extern "C" void InputDeviceTask(void* parameter);
extern "C" void InputDeviceTimerCallback(TimerHandle_t xTimer);
extern "C" void OutputDeviceTask(void* parameter);
extern "C" void OutputDeviceTimerCallback(TimerHandle_t xTimer);
extern "C" void SlowOutputDeviceTask(void* parameter);
extern "C" void SlowOutputDeviceTimerCallback(TimerHandle_t xTimer);
Status RunnerStart() {
// Start USB task first
if (StartUSBTask() != OK) {
return ERROR;
}
if (IBPDriverRegistry::InitializeAll() != OK) {
return ERROR;
}
// Start output device task
BaseType_t status = xTaskCreate(&OutputDeviceTask, "output_device_task",
CONFIG_TASK_STACK_SIZE, NULL,
CONFIG_TASK_PRIORITY, &output_task_handle);
if (status != pdPASS || output_task_handle == NULL) {
return ERROR;
}
output_timer_handle = xTimerCreate("output_device_timer", CONFIG_SCAN_TICKS,
pdTRUE, // Auto reload
NULL, &OutputDeviceTimerCallback);
if (output_timer_handle == NULL) {
return ERROR;
}
if (xTimerStart(output_timer_handle, 0) != pdPASS) {
return ERROR;
}
// Start slow output device task
status = xTaskCreate(&SlowOutputDeviceTask, "slow_output_device_task",
CONFIG_TASK_STACK_SIZE, NULL, CONFIG_TASK_PRIORITY - 1,
&slow_output_task_handle);
if (status != pdPASS || slow_output_task_handle == NULL) {
return ERROR;
}
slow_output_timer_handle =
xTimerCreate("slow_output_device_timer", CONFIG_SLOW_TICKS,
pdTRUE, // Auto reload
NULL, &SlowOutputDeviceTimerCallback);
if (slow_output_timer_handle == NULL) {
return ERROR;
}
if (xTimerStart(slow_output_timer_handle, 0) != pdPASS) {
return ERROR;
}
// Start input device task
// Pin input task to the same core as tick handler because there could be
// flash writes that will disable the other core. Don't want to block the tick
// handler as well as usb task (which is also pinned to the tick core) for too
// long.
status = xTaskCreateAffinitySet(
&InputDeviceTask, "input_device_task", CONFIG_TASK_STACK_SIZE, NULL,
CONFIG_TASK_PRIORITY, (1 << (configTICK_CORE)), &input_task_handle);
if (status != pdPASS || input_task_handle == NULL) {
return ERROR;
}
input_timer_handle = xTimerCreate("input_device_timer", CONFIG_SCAN_TICKS,
pdTRUE, // Auto reload
NULL, &InputDeviceTimerCallback);
if (input_timer_handle == NULL) {
return ERROR;
}
if (xTimerStart(input_timer_handle, 0) != pdPASS) {
return ERROR;
}
watchdog_enable(/*delay_ms=*/100, /*pause_on_debug=*/true);
return OK;
}
extern "C" void InputDeviceTask(void* parameter) {
(void)parameter;
bool local_is_config_mode = false;
while (true) {
// Initialization
DeviceRegistry::UpdateConfig();
for (auto output_device : output_devices) {
output_device->StartOfInputTick();
}
for (auto output_device : slow_output_devices) {
output_device->StartOfInputTick();
}
for (auto input_device : input_devices) {
input_device->InputLoopStart();
}
for (auto output_device : output_devices) {
output_device->FinalizeInputTickOutput();
}
for (auto output_device : slow_output_devices) {
output_device->FinalizeInputTickOutput();
}
while (true) {
const uint64_t sleep_time = time_us_64();
// Wait for the timer callback to wake it up. Running this outside the
// timer context to avoid overflowing the timer task.
xTaskNotifyWait(/*do not clear notification on enter*/ 0,
/*clear notification on exit*/ 0xffffffff,
/*pulNotificationValue=*/NULL, portMAX_DELAY);
const uint64_t start_time = time_us_64();
if (start_time - sleep_time < 1000) {
LOG_WARNING(
"Input task didn't sleep enough. Remaining time budget is less "
"than 1ms.");
}
bool should_change_config_mode;
bool should_update_config;
{
LockSemaphore lock(semaphore);
should_change_config_mode = local_is_config_mode != is_config_mode;
should_update_config = update_config_flag;
update_config_flag = false;
}
if (should_change_config_mode) {
local_is_config_mode = !local_is_config_mode;
for (auto device : output_devices) {
device->SetConfigMode(local_is_config_mode);
}
for (auto device : input_devices) {
device->SetConfigMode(local_is_config_mode);
}
for (auto device : slow_output_devices) {
device->SetConfigMode(local_is_config_mode);
}
}
if (should_update_config) {
// Rerun the initialization
break;
}
for (auto output_device : output_devices) {
output_device->StartOfInputTick();
}
for (auto output_device : slow_output_devices) {
output_device->StartOfInputTick();
}
for (auto input_device : input_devices) {
input_device->InputTick();
}
for (auto output_device : output_devices) {
output_device->FinalizeInputTickOutput();
}
for (auto output_device : slow_output_devices) {
output_device->FinalizeInputTickOutput();
}
const uint64_t end_time = time_us_64();
LOG_DEBUG("Input task per iteration takes %d us", end_time - start_time);
watchdog_update();
}
}
}
extern "C" void InputDeviceTimerCallback(TimerHandle_t xTimer) {
xTaskNotifyGive(input_task_handle);
}
extern "C" void OutputDeviceTask(void* parameter) {
(void)parameter;
while (true) {
const uint64_t sleep_time = time_us_64();
// Wait for the timer callback to wake it up. Running this outside the timer
// context to avoid overflowing the timer task.
xTaskNotifyWait(/*do not clear notification on enter*/ 0,
/*clear notification on exit*/ 0xffffffff,
/*pulNotificationValue=*/NULL, portMAX_DELAY);
const uint64_t start_time = time_us_64();
if (start_time - sleep_time < 1000) {
LOG_WARNING(
"Output task didn't sleep enough. Remaining time budget is less than "
"1ms.");
}
for (auto output_device : output_devices) {
output_device->OutputTick();
}
const uint64_t end_time = time_us_64();
LOG_DEBUG("Output task per iteration takes %d us", end_time - start_time);
}
}
extern "C" void OutputDeviceTimerCallback(TimerHandle_t xTimer) {
xTaskNotifyGive(output_task_handle);
}
extern "C" void SlowOutputDeviceTask(void* parameter) {
(void)parameter;
while (true) {
const uint64_t sleep_time = time_us_64();
// Wait for the timer callback to wake it up. Running this outside the timer
// context to avoid overflowing the timer task.
xTaskNotifyWait(/*do not clear notification on enter*/ 0,
/*clear notification on exit*/ 0xffffffff,
/*pulNotificationValue=*/NULL, portMAX_DELAY);
const uint64_t start_time = time_us_64();
if (start_time - sleep_time < 1000) {
LOG_WARNING(
"Slow output task didn't sleep enough. Remaining time budget is less "
"than 1ms.");
}
for (auto output_device : slow_output_devices) {
output_device->OutputTick();
}
const uint64_t end_time = time_us_64();
LOG_DEBUG("Slow output task per iteration takes %d us",
end_time - start_time);
}
}
extern "C" void SlowOutputDeviceTimerCallback(TimerHandle_t xTimer) {
xTaskNotifyGive(slow_output_task_handle);
}
void SetConfigMode(bool is_config) {
LockSemaphore lock(semaphore);
is_config_mode = is_config;
}
void NotifyConfigChange() {
LockSemaphore lock(semaphore);
update_config_flag = true;
}
} // namespace runner