SDK for TESAIoT Dev Kit
API reference & tutorials (ModusToolbox)
Loading...
Searching...
No Matches
ai_engine.h
Go to the documentation of this file.
1/*******************************************************************************
2 * File Name : ai_engine.h
3 *
4 * Description : BENTO Edge AI engine — runs ONE DEEPCRAFT model at a time
5 * on CM55 (TFLite-Micro + Ethos-U55) and publishes a result
6 * snapshot for the UI and the MicroPython model link.
7 *
8 * WHY ONE AT A TIME (design ruling, three independent walls):
9 * 1. one NPU — Ethos-U55 inferences serialize anyway;
10 * 2. one sensor pipeline per model family (IMU / radar /
11 * mic) with different sample rates and windows;
12 * 3. CM55 CPU budget beside LVGL (GFX task, priority 6).
13 * Models are all COMPILED IN; activating one is a registry
14 * switch, not a reload — no flash or weight copy.
15 *
16 * Every inference is timed with the DWT cycle counter and
17 * published in the snapshot, so a model that outgrows its
18 * cadence is visible on-screen rather than silent.
19 *
20 * Target : PSoC Edge E84, CM55
21 * //! [doc-drift-fix] — docs/template_local_deltas.list
22 *******************************************************************************/
23
24#ifndef AI_ENGINE_H
25#define AI_ENGINE_H
26
27#include <stdint.h>
28#include <stdbool.h>
29#include <stddef.h> /* NULL — ai_result_top_positive() below is inline here */
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
35#define AI_MAX_CLASSES (8u)
36#define AI_MAX_LABEL_LEN (16u)
37
44
46typedef struct {
47 const char *name;
48 const char *description;
50 uint8_t class_count;
51 /* Registry contract: index 0 is the negative class -- the one that means
52 * "nothing is happening". It is "unlabelled" in the audio and radar models,
53 * "idle" in motion, "normal" in fall. Everything from 1 up is a positive
54 * detection.
55 *
56 * The watch rows depend on this: a row's confidence is the maximum over
57 * classes 1..class_count-1 (see row_score_pct in page_edge_ai.c), so a
58 * model whose classes were all positive would render as if the loudest one
59 * were always firing. A new model that cannot honour this needs that code
60 * changed, not a quiet exception here. */
62 uint32_t flash_bytes;
63 uint16_t period_ms;
64
65 /* DEEPCRAFT-generated entry points (symbol-prefixed per model so several
66 * models can be linked into one image — the generated sources all declare
67 * IMAI_* otherwise). */
68 int (*init)(void);
69 int (*enqueue)(const float *in);
70 int (*dequeue)(float *out);
71 void (*finalize)(void);
73
75typedef struct {
76 uint8_t model_index;
77 uint8_t class_count;
78 uint8_t top_class;
79 uint8_t running;
81 uint32_t inference_us;
83 uint32_t inferences;
84 uint32_t seq;
86
99#define AI_PARALLEL_MIC (255)
100
105#define AI_PARALLEL_ROOM (254)
106#define AI_PARALLEL_INTRUDER (253)
107
109#define AI_PARALLEL_ALL (252)
110
115#define AI_PARALLEL_FIRST AI_PARALLEL_ALL
116
117/* These numbers are also spelled in the wire header, because MicroPython has to
118 * resolve a legacy 13/14/15 to the same value before it can confirm a select.
119 * When the two drifted, every Sound Watch select reported "not confirmed" on a
120 * board that had in fact switched. Checked here rather than trusted, and only
121 * when the wire header is in the translation unit, so this header still
122 * compiles on its own. */
123#if defined(MODEL_LINK_SET_MIC)
124_Static_assert(AI_PARALLEL_ALL == MODEL_LINK_SET_ALL, "set index drift");
125_Static_assert(AI_PARALLEL_INTRUDER == MODEL_LINK_SET_INTRUDER, "set index drift");
126_Static_assert(AI_PARALLEL_ROOM == MODEL_LINK_SET_ROOM, "set index drift");
127_Static_assert(AI_PARALLEL_MIC == MODEL_LINK_SET_MIC, "set index drift");
128#endif
129
131uint32_t ai_engine_set_models(uint32_t set_index, uint8_t *idx, uint32_t max);
132
134const char *ai_engine_set_name(uint32_t set_index);
135
138int ai_engine_set_define(uint32_t set_index, const uint8_t *members, uint32_t n);
139
141uint32_t ai_engine_set_members_defined(uint32_t set_index, uint8_t *out, uint32_t max);
142
147void ai_engine_unload(uint32_t idx);
150
152
154const ai_model_desc_t *ai_engine_model(uint32_t index);
155
191
193uint32_t ai_engine_dyn_count(void);
195
198bool ai_engine_init(void);
199
202bool ai_engine_start(uint32_t index);
203
207
210uint32_t ai_engine_feeds(void);
211uint32_t ai_engine_dq_ok(void);
212uint32_t ai_engine_dq_calls(void);
213
217void ai_engine_set_sensor_rate(uint32_t interval_ms);
218
221uint32_t ai_engine_inits(void);
222
227uint32_t ai_engine_init_calls(void);
228
232
236uint64_t ai_engine_npu_cycles(void);
237
252
257
259void ai_engine_stop(void);
260
263
268
271
276bool ai_engine_snapshot_model(uint32_t index, ai_result_t *out);
277
282uint32_t ai_engine_set_members(uint8_t *idx, uint32_t max);
283
291static inline int ai_result_top_positive(const ai_result_t *r, uint8_t *which)
292{
293 if (which != NULL) { *which = 1u; }
294 if ((r == NULL) || (r->class_count < 2u)) { return 0; }
295 float best = r->scores[1];
296 uint8_t arg = 1u;
297 /* Bounded by class_count, not the array size: publish() writes only that
298 * many floats and the slots above were never set. */
299 for (uint8_t c = 2u; (c < r->class_count) && (c < AI_MAX_CLASSES); c++) {
300 if (r->scores[c] > best) { best = r->scores[c]; arg = c; }
301 }
302 if (which != NULL) { *which = arg; }
303 int pct = (int)(best * 100.0f + 0.5f);
304 if (pct < 0) { pct = 0; }
305 if (pct > 100) { pct = 100; }
306 return pct;
307}
308
312
315
316#ifdef __cplusplus
317}
318#endif
319
320#endif /* AI_ENGINE_H */
#define AI_MAX_CLASSES
Definition ai_engine.h:35
uint32_t ai_engine_dyn_capacity(void)
#define AI_PARALLEL_ROOM
Definition ai_engine.h:105
#define AI_PARALLEL_INTRUDER
Definition ai_engine.h:106
#define AI_PARALLEL_MIC
Definition ai_engine.h:99
int ai_engine_register(const ai_model_desc_t *desc)
#define AI_PARALLEL_ALL
Definition ai_engine.h:109
ai_sensor_t
Definition ai_engine.h:39
@ AI_SENSOR_IMU
Definition ai_engine.h:40
@ AI_SENSOR_RADAR
Definition ai_engine.h:41
@ AI_SENSOR_MIC
Definition ai_engine.h:42
uint32_t ai_engine_dyn_count(void)
uint32_t ai_engine_init_returns(void)
Model init() calls that RETURNED; always read as the pair.
int32_t ai_engine_last_init_rc(void)
Return code of the last model init(): 0x7FFFFFFF = never called; 0 = ok.
uint32_t ai_engine_feeds(void)
Samples accepted by the model; take the delta over at least 1 s.
uint32_t ai_engine_inits(void)
Successful model initialisations; a packed nibble in the heartbeat word.
uint32_t ai_engine_dq_ok(void)
Successful dequeues; frozen while dq_calls climbs = NPU stall.
uint32_t ai_engine_init_calls(void)
Model init() calls entered; read together with ai_engine_init_returns().
uint64_t ai_engine_npu_cycles(void)
NPU cycles accumulated so far; not proof any inference completed.
uint32_t ai_engine_dq_calls(void)
Dequeue attempts; pair with ai_engine_dq_ok() for stall detection.
uint32_t ai_engine_stale_drops(void)
Verdicts discarded past the Ethos-U wait bound; must stay flat while npu_cycles advances.
int ai_engine_active(void)
The LOADED model (s_current), or -1; lags requested by a cold-init window.
uint32_t ai_engine_stack_free_words(void)
All-time minimum of unused stack words; O(stack) scan — read at ~1 Hz at most.
int ai_engine_requested(void)
The REQUESTED model (s_active), or -1; the correct guard for a fallback.
uint32_t ai_engine_stack_words(void)
Stack the inference task got, in words; 0 = engine never started — the universal gate.
void ai_engine_stop(void)
Stop the active model (idempotent); pair with a sensor-rate walk-down.
bool ai_engine_start(uint32_t index)
Activate a model (or set) by index and begin inferring; check the return.
bool ai_engine_init(void)
Create the inference task (idempotent); it must precede ai_engine_start().
bool ai_engine_snapshot(ai_result_t *out)
Copy the latest published result; last-writer-wins — not valid inside a set.
uint32_t ai_engine_mic_settle_pct(void)
Settle progress 0..100; only meaningful inside a set (100 otherwise).
bool ai_engine_mic_settling(void)
True while a parallel set is still filling its windows; verdicts are withheld.
uint32_t ai_engine_model_count(void)
How many models are registered; the count leads the rows.
const ai_model_desc_t * ai_engine_model(uint32_t index)
Registry entry, NULL if out of range — and NULL for a set pseudo-index.
bool ai_engine_snapshot_model(uint32_t index, ai_result_t *out)
One model's last verdict — the reader to use inside a parallel set.
void ai_engine_set_sensor_rate(uint32_t interval_ms)
Set the accelerometer feed interval; the only legal sender on s_rate_msg.
void ai_engine_resume_sensor(void)
Standalone resume of the default cadence; never back-to-back with a rate set.
uint32_t ai_engine_set_members(uint8_t *idx, uint32_t max)
Registry indices of the ACTIVE set's members; test each member's sensor.
uint32_t ai_engine_set_members_defined(uint32_t set_index, uint8_t *out, uint32_t max)
The explicit membership if defined, else 0 — the only ack for set_define.
uint32_t ai_engine_set_models(uint32_t set_index, uint8_t *idx, uint32_t max)
Members of any set, in registry order; explicit membership wins outright.
int ai_engine_set_define(uint32_t set_index, const uint8_t *members, uint32_t n)
Give a set an explicit run-time membership; task context, all-or-nothing.
const char * ai_engine_set_name(uint32_t set_index)
Human name for a set; NULL for a non-set index.
void ai_engine_unload(uint32_t idx)
Asynchronous unload request — refused unless the engine is idle.
uint32_t ai_engine_unload_done(void)
Cumulative count of completed unloads; read as a pair with refused.
uint32_t ai_engine_unload_refused(void)
Cumulative count of refused unloads; read with ai_engine_unload_done().
Definition ai_engine.h:46
uint8_t class_count
Definition ai_engine.h:50
const char * name
Definition ai_engine.h:47
const char * class_labels[AI_MAX_CLASSES]
Definition ai_engine.h:61
uint16_t period_ms
Definition ai_engine.h:63
const char * description
Definition ai_engine.h:48
int(*) dequeue(float *out)
Definition ai_engine.h:70
ai_sensor_t sensor
Definition ai_engine.h:49
uint32_t flash_bytes
Definition ai_engine.h:62
void(*) finalize(void)
Definition ai_engine.h:71
int(*) init(void)
Definition ai_engine.h:68
int(*) enqueue(const float *in)
Definition ai_engine.h:69
Definition ai_engine.h:75
uint32_t inference_us
Definition ai_engine.h:81
uint32_t seq
Definition ai_engine.h:84
uint8_t running
Definition ai_engine.h:79
uint8_t class_count
Definition ai_engine.h:77
uint8_t model_index
Definition ai_engine.h:76
uint8_t top_class
Definition ai_engine.h:78
float scores[AI_MAX_CLASSES]
Definition ai_engine.h:80
uint32_t inference_us_max
Definition ai_engine.h:82
uint32_t inferences
Definition ai_engine.h:83