1. NanoEdge AI HSP backend
1.1. Introduction
The hardware signal processor (HSP) is a dedicated signal processor engine. It can offload and speed-up processing initially handled by the CPU.
When the NanoEdge AI library is compiled with the HSP backend, the signal processing operations (FFT, normalization, feature extraction, etc.) are executed on the HSP engine instead of the CPU. This page describes the additional setup required to use an HSP-enabled NanoEdge AI library.
1.2. Additional library file
The library .zip includes an extra header file when compiling with HSP backend:
| File | Description |
|---|---|
libneai.a |
Static library |
NanoEdgeAI.h |
Main API header |
knowledge.h |
Pre-trained model knowledge (AD only, when using pretrained mode) |
NanoEdgeAI_hsp.h |
HSP initialization header |
1.3. Setup
1.3.1. Include order
NanoEdgeAI_hsp.h must be included before any other HSP middleware header (such as hsp_engine.h). It internally includes the required middleware prerequisites and ensures that HSP accelerator functions are emitted as linkable symbols for libneai.a.
#include "NanoEdgeAI.h"
#include "NanoEdgeAI_hsp.h" /* must come before any other HSP include */
1.3.2. HSP initialization
After MX_HSP_Engine_Init() (auto-generated by CubeMX), retrieve the HSP context and pass it to the NEAI library:
hsp_engine_context_t *hsp_ctx = MX_HSP_Engine_GetContext();
neai_hsp_set_handle(hsp_ctx);
This must be done before any other NEAI function call, specifically before _init.
1.4. API: neai_hsp_set_handle
void neai_hsp_set_handle(hsp_engine_context_t *handle);
handle(IN): Pointer to an initialized HSP context obtained fromMX_HSP_Engine_GetContext().
1.5. Hello World examples
1.5.1. Anomaly detection (AD)
/* Demo: NanoEdge AI anomaly detection with HSP backend
* This program must be completed and customized by the user.
*/
#include "NanoEdgeAI.h"
#include "NanoEdgeAI_hsp.h" /* must come before any other HSP include */
/* Buffers */
float input_user_buffer[NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER];
uint8_t similarity;
int main(void)
{
/* System initialization (HAL, clocks, peripherals, ...) */
MX_HSP_Engine_Init(); /* auto-generated by CubeMX */
/* Pass HSP handle to NEAI library */
hsp_engine_context_t *hsp_ctx = MX_HSP_Engine_GetContext();
neai_hsp_set_handle(hsp_ctx);
/* Initialize NanoEdge AI anomaly detection */
enum neai_state state = neai_anomalydetection_init(false);
if (state != NEAI_OK) {
/* Handle error */
}
/* Learning phase */
for (int i = 0; i < LEARNING_ITERATIONS; i++) {
fill_buffer(input_user_buffer);
state = neai_anomalydetection_learn(input_user_buffer);
}
/* Detection phase */
while (1) {
fill_buffer(input_user_buffer);
state = neai_anomalydetection_detect(input_user_buffer, &similarity);
if (state == NEAI_OK) {
/* similarity is in [0, 100] */
/* 100 = similar to learned patterns, 0 = anomalous */
}
}
}
1.5.2. Outlier detection (1CC)
/* Demo: NanoEdge AI outlier detection with HSP backend
* This program must be completed and customized by the user.
*/
#include "NanoEdgeAI.h"
#include "NanoEdgeAI_hsp.h" /* must come before any other HSP include */
/* Buffers */
float input_user_buffer[NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER];
uint8_t is_outlier;
int main(void)
{
/* System initialization (HAL, clocks, peripherals, ...) */
MX_HSP_Engine_Init(); /* auto-generated by CubeMX */
/* Pass HSP handle to NEAI library */
hsp_engine_context_t *hsp_ctx = MX_HSP_Engine_GetContext();
neai_hsp_set_handle(hsp_ctx);
/* Initialize NanoEdge AI outlier detection */
enum neai_state state = neai_outlier_init();
if (state != NEAI_OK) {
/* Handle error */
}
/* Detection phase */
while (1) {
fill_buffer(input_user_buffer);
state = neai_outlier(input_user_buffer, &is_outlier);
if (state == NEAI_OK) {
/* is_outlier: 1 = outlier, 0 = normal */
}
}
}
1.5.3. N-class classification (nCC)
/* Demo: NanoEdge AI n-class classification with HSP backend
* This program must be completed and customized by the user.
*/
#include "NanoEdgeAI.h"
#include "NanoEdgeAI_hsp.h" /* must come before any other HSP include */
/* Buffers */
float input_user_buffer[NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER];
float output_class_buffer[NEAI_NUMBER_OF_CLASSES];
int id_class;
int main(void)
{
/* System initialization (HAL, clocks, peripherals, ...) */
MX_HSP_Engine_Init(); /* auto-generated by CubeMX */
/* Pass HSP handle to NEAI library */
hsp_engine_context_t *hsp_ctx = MX_HSP_Engine_GetContext();
neai_hsp_set_handle(hsp_ctx);
/* Initialize NanoEdge AI n-class classification */
enum neai_state state = neai_classification_init();
if (state != NEAI_OK) {
/* Handle error */
}
/* Classification phase */
while (1) {
fill_buffer(input_user_buffer);
state = neai_classification(input_user_buffer, output_class_buffer, &id_class);
if (state == NEAI_OK) {
/* id_class contains the predicted class (highest probability) */
/* output_class_buffer contains probabilities for each class */
}
}
}
1.5.4. Extrapolation (E)
/* Demo: NanoEdge AI extrapolation with HSP backend
* This program must be completed and customized by the user.
*/
#include "NanoEdgeAI.h"
#include "NanoEdgeAI_hsp.h" /* must come before any other HSP include */
/* Buffers */
float input_user_buffer[NEAI_INPUT_SIGNAL_LENGTH * NEAI_INPUT_AXIS_NUMBER];
float extrapolated_value;
int main(void)
{
/* System initialization (HAL, clocks, peripherals, ...) */
MX_HSP_Engine_Init(); /* auto-generated by CubeMX */
/* Pass HSP handle to NEAI library */
hsp_engine_context_t *hsp_ctx = MX_HSP_Engine_GetContext();
neai_hsp_set_handle(hsp_ctx);
/* Initialize NanoEdge AI extrapolation */
enum neai_state state = neai_extrapolation_init();
if (state != NEAI_OK) {
/* Handle error */
}
/* Prediction phase */
while (1) {
fill_buffer(input_user_buffer);
state = neai_extrapolation(input_user_buffer, &extrapolated_value);
if (state == NEAI_OK) {
/* extrapolated_value contains the predicted target value */
}
}
}