mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-04-05 18:06:07 +00:00
feat(core): introduce kernel event loop
[no changelog]
This commit is contained in:
parent
fdc2ca9693
commit
f55cd1cc8f
@ -28,6 +28,7 @@
|
||||
#include <sys/applet.h>
|
||||
#include <sys/bootutils.h>
|
||||
#include <sys/mpu.h>
|
||||
#include <sys/sysevent.h>
|
||||
#include <sys/system.h>
|
||||
#include <sys/systick.h>
|
||||
#include <util/bl_check.h>
|
||||
@ -169,6 +170,19 @@ void drivers_init() {
|
||||
#endif
|
||||
}
|
||||
|
||||
// Kernel task main loop
|
||||
//
|
||||
// Returns when the coreapp task is terminated
|
||||
static void kernel_loop(applet_t *coreapp) {
|
||||
do {
|
||||
sysevents_t awaited = {0};
|
||||
sysevents_t signalled = {0};
|
||||
|
||||
sysevents_poll(&awaited, &signalled, 100);
|
||||
|
||||
} while (applet_is_alive(coreapp));
|
||||
}
|
||||
|
||||
// defined in linker script
|
||||
extern uint32_t _codelen;
|
||||
|
||||
@ -218,6 +232,10 @@ static void show_rsod(const systask_postmortem_t *pminfo) {
|
||||
if (applet_reset(&coreapp, 1, pminfo, sizeof(systask_postmortem_t))) {
|
||||
// Run the applet & wait for it to finish
|
||||
applet_run(&coreapp);
|
||||
// Loop until the coreapp is terminated
|
||||
kernel_loop(&coreapp);
|
||||
// Release the coreapp resources
|
||||
applet_stop(&coreapp);
|
||||
|
||||
if (coreapp.task.pminfo.reason == TASK_TERM_REASON_EXIT) {
|
||||
// RSOD was shown successfully
|
||||
@ -277,8 +295,12 @@ int main(void) {
|
||||
error_shutdown("Cannot start coreapp");
|
||||
}
|
||||
|
||||
// Run the applet & wait for it to finish
|
||||
// Run the applet
|
||||
applet_run(&coreapp);
|
||||
// Loop until the coreapp is terminated
|
||||
kernel_loop(&coreapp);
|
||||
// Release the coreapp resources
|
||||
applet_stop(&coreapp);
|
||||
|
||||
// Coreapp crashed, show RSOD
|
||||
show_rsod(&coreapp.task.pminfo);
|
||||
|
@ -90,6 +90,12 @@ bool applet_reset(applet_t* applet, uint32_t cmd, const void* arg,
|
||||
// Runs the applet and waits until it finishes.
|
||||
void applet_run(applet_t* applet);
|
||||
|
||||
// Release all resources help by the applet
|
||||
void applet_stop(applet_t* applet);
|
||||
|
||||
// Returns `true` if the applet task is alive.
|
||||
bool applet_is_alive(applet_t* applet);
|
||||
|
||||
// Returns the currently active applet.
|
||||
//
|
||||
// Returns `NULL` if no applet is currently active.
|
||||
|
@ -114,7 +114,7 @@ typedef struct {
|
||||
// Exception return value
|
||||
uint32_t exc_return;
|
||||
// Set to nonzero, if the task is killed
|
||||
uint32_t killed;
|
||||
volatile uint32_t killed;
|
||||
|
||||
// Task id
|
||||
systask_id_t id;
|
||||
@ -147,6 +147,9 @@ void systask_yield_to(systask_t* task);
|
||||
bool systask_init(systask_t* task, uint32_t stack_ptr, uint32_t stack_size,
|
||||
void* context);
|
||||
|
||||
// Returns true if the task is alive (not terminated, killed or crashed)
|
||||
bool systask_is_alive(const systask_t* task);
|
||||
|
||||
// Pushes data onto the stack of the task
|
||||
//
|
||||
// The task must be not be running when the function is called
|
||||
|
@ -104,12 +104,18 @@ void applet_run(applet_t* applet) {
|
||||
#endif
|
||||
|
||||
systask_yield_to(&applet->task);
|
||||
}
|
||||
|
||||
void applet_stop(applet_t* applet) {
|
||||
#ifdef USE_TRUSTZONE
|
||||
applet_set_unpriv(applet, false);
|
||||
#endif
|
||||
}
|
||||
|
||||
bool applet_is_alive(applet_t* applet) {
|
||||
return systask_is_alive(&applet->task);
|
||||
}
|
||||
|
||||
applet_t* applet_active(void) {
|
||||
systask_t* task = systask_active();
|
||||
|
||||
|
@ -255,6 +255,8 @@ static void systask_kill(systask_t* task) {
|
||||
}
|
||||
}
|
||||
|
||||
bool systask_is_alive(const systask_t* task) { return !task->killed; }
|
||||
|
||||
void systask_exit(systask_t* task, int exit_code) {
|
||||
systask_scheduler_t* scheduler = &g_systask_scheduler;
|
||||
|
||||
|
@ -21,6 +21,7 @@
|
||||
#include <trezor_types.h>
|
||||
|
||||
#include <io/display.h>
|
||||
#include <sys/sysevent.h>
|
||||
#include <sys/systick.h>
|
||||
|
||||
#ifdef USE_BLE
|
||||
@ -84,6 +85,15 @@ STATIC mp_obj_t mod_trezorio_poll(mp_obj_t ifaces, mp_obj_t list_ref,
|
||||
const mp_uint_t deadline = mp_hal_ticks_ms() + timeout;
|
||||
mp_obj_iter_buf_t iterbuf = {0};
|
||||
|
||||
{
|
||||
// This is just a dummy call to `sysevent_poll` to ensure cooperative
|
||||
// multitasking works. Event polling is still performed in the loop below
|
||||
// but will be replaced in the future.
|
||||
sysevents_t awaited = {0};
|
||||
sysevents_t signalled = {0};
|
||||
sysevent_poll(&awaited, &signalled, 0);
|
||||
}
|
||||
|
||||
for (;;) {
|
||||
mp_obj_t iter = mp_getiter(ifaces, &iterbuf);
|
||||
mp_obj_t item = 0;
|
||||
|
Loading…
Reference in New Issue
Block a user