-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add FreeRTOS variant of NPU demo (#10004)
* Add FreeRTOS variant of NPU demo This adds an extra flag to the existing NPU demo that runs it using the FreeRTOS kernel task scheduling and queues. * Add FreeRTOS notes to tutorial * Update FreeRTOS demo to run in demo script Also, minor text fixes. * Minor text fixes * Fix docs formatting
- Loading branch information
Showing
12 changed files
with
307 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
include/inputs.h | ||
include/outputs.h | ||
include/labels.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/* Please refer to http://www.freertos.org/a00110.html for refernce. */ | ||
#ifndef FREERTOS_CONFIG_H | ||
#define FREERTOS_CONFIG_H | ||
/****************************************************************************** | ||
* Defines | ||
**********SYSTEM_CORE_CLOCK********************************************************************/ | ||
/* Hardware features */ | ||
#define configENABLE_MPU 0 | ||
#define configENABLE_FPU 0 | ||
#define configENABLE_TRUSTZONE 0 | ||
/* Scheduling */ | ||
#define configCPU_CLOCK_HZ 25000000 | ||
#define configUSE_PORT_OPTIMISED_TASK_SELECTION 0 | ||
#define configUSE_PREEMPTION 1 | ||
#define configUSE_TIME_SLICING 0 | ||
#define configMAX_PRIORITIES 5 | ||
#define configIDLE_SHOULD_YIELD 1 | ||
#define configUSE_16_BIT_TICKS 0 | ||
#define configRUN_FREERTOS_SECURE_ONLY 1 | ||
/* Stack and heap */ | ||
#define configMINIMAL_STACK_SIZE (uint16_t)128 | ||
#define configMINIMAL_SECURE_STACK_SIZE 1024 | ||
#define configTOTAL_HEAP_SIZE (size_t)(50 * 1024) | ||
#define configMAX_TASK_NAME_LEN 12 | ||
/* OS features */ | ||
#define configUSE_MUTEXES 1 | ||
#define configUSE_TICKLESS_IDLE 1 | ||
#define configUSE_APPLICATION_TASK_TAG 0 | ||
#define configUSE_NEWLIB_REENTRANT 0 | ||
#define configUSE_CO_ROUTINES 0 | ||
#define configUSE_COUNTING_SEMAPHORES 1 | ||
#define configUSE_RECURSIVE_MUTEXES 1 | ||
#define configUSE_QUEUE_SETS 0 | ||
#define configUSE_TASK_NOTIFICATIONS 1 | ||
#define configUSE_TRACE_FACILITY 1 | ||
/* Hooks */ | ||
#define configUSE_IDLE_HOOK 0 | ||
#define configUSE_TICK_HOOK 0 | ||
#define configUSE_MALLOC_FAILED_HOOK 0 | ||
/* Debug features */ | ||
#define configCHECK_FOR_STACK_OVERFLOW 0 | ||
#define configASSERT(x) \ | ||
if ((x) == 0) { \ | ||
taskDISABLE_INTERRUPTS(); \ | ||
for (;;) \ | ||
; \ | ||
} | ||
#define configQUEUE_REGISTRY_SIZE 0 | ||
/* Timers and queues */ | ||
#define configUSE_TIMERS 1 | ||
#define configTIMER_TASK_PRIORITY (configMAX_PRIORITIES - 1) | ||
#define configTIMER_TASK_STACK_DEPTH configMINIMAL_STACK_SIZE | ||
#define configTIMER_QUEUE_LENGTH 5 | ||
/* Task settings */ | ||
#define INCLUDE_vTaskPrioritySet 1 | ||
#define INCLUDE_uxTaskPriorityGet 1 | ||
#define INCLUDE_vTaskDelete 1 | ||
#define INCLUDE_vTaskCleanUpResources 0 | ||
#define INCLUDE_vTaskSuspend 1 | ||
#define INCLUDE_vTaskDelayUntil 1 | ||
#define INCLUDE_vTaskDelay 1 | ||
#define INCLUDE_uxTaskGetStackHighWaterMark 0 | ||
#define INCLUDE_xTaskGetIdleTaskHandle 0 | ||
#define INCLUDE_eTaskGetState 1 | ||
#define INCLUDE_xTaskResumeFromISR 0 | ||
#define INCLUDE_xTaskGetCurrentTaskHandle 1 | ||
#define INCLUDE_xTaskGetSchedulerState 0 | ||
#define INCLUDE_xSemaphoreGetMutexHolder 0 | ||
#define INCLUDE_xTimerPendFunctionCall 1 | ||
#define configUSE_STATS_FORMATTING_FUNCTIONS 1 | ||
#define configCOMMAND_INT_MAX_OUTPUT_SIZE 2048 | ||
#ifdef __NVIC_PRIO_BITS | ||
#define configPRIO_BITS __NVIC_PRIO_BITS | ||
#else | ||
#define configPRIO_BITS 3 | ||
#endif | ||
/* Interrupt settings */ | ||
#define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 0x07 | ||
#define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5 | ||
#define configKERNEL_INTERRUPT_PRIORITY \ | ||
(configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS)) | ||
#define configMAX_SYSCALL_INTERRUPT_PRIORITY \ | ||
(configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS)) | ||
#ifndef __IASMARM__ | ||
#define configGENERATE_RUN_TIME_STATS 0 | ||
#define portCONFIGURE_TIMER_FOR_RUN_TIME_STATS() | ||
#define portGET_RUN_TIME_COUNTER_VALUE() 0 | ||
#define configTICK_RATE_HZ (TickType_t)1000 | ||
#endif /* __IASMARM__ */ | ||
#define xPortPendSVHandler PendSV_Handler | ||
#define vPortSVCHandler SVC_Handler | ||
#define xPortSysTickHandler SysTick_Handler | ||
#endif /* FREERTOS_CONFIG_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
#include <FreeRTOS.h> | ||
#include <queue.h> | ||
#include <stdio.h> | ||
#include <task.h> | ||
#include <tvm_runtime.h> | ||
|
||
#include "ethosu_mod.h" | ||
#include "uart.h" | ||
|
||
// Header files generated by convert_image.py and convert_labels.py | ||
#include "inputs.h" | ||
#include "labels.h" | ||
#include "outputs.h" | ||
|
||
static void prvInferenceTask(void* pvParameters); | ||
static void prvDataCollectionTask(void* pvParameters); | ||
|
||
#define mainQUEUE_INFERENCE_TASK_PRIORITY (tskIDLE_PRIORITY + 3) | ||
#define mainQUEUE_INFERENCE_TASK_STACK_SIZE 4096 | ||
#define mainQUEUE_DATA_TASK_PRIORITY (tskIDLE_PRIORITY + 2) | ||
#define mainQUEUE_DATA_TASK_STACK_SIZE configMINIMAL_STACK_SIZE | ||
#define mainQUEUE_LENGTH (1) | ||
#define mainQUEUE_SEND_FREQUENCY_MS (100 / portTICK_PERIOD_MS) | ||
|
||
/* The queue used to pass data to run through our model */ | ||
static QueueHandle_t xQueue = NULL; | ||
|
||
int main(void) { | ||
// Platform UART | ||
uart_init(); | ||
// NPU | ||
EthosuInit(); | ||
// TVM Memory Manager | ||
StackMemoryManager_Init(&app_workspace, g_aot_memory, WORKSPACE_SIZE); | ||
|
||
// Queue for inferences | ||
xQueue = xQueueCreate(mainQUEUE_LENGTH, sizeof(uint8_t*)); | ||
|
||
if (xQueue != NULL) { | ||
// Inference task | ||
xTaskCreate(prvInferenceTask, "Inference", mainQUEUE_INFERENCE_TASK_STACK_SIZE, NULL, | ||
mainQUEUE_INFERENCE_TASK_PRIORITY, NULL); | ||
|
||
// Data collector task | ||
xTaskCreate(prvDataCollectionTask, "Data", mainQUEUE_DATA_TASK_STACK_SIZE, NULL, | ||
mainQUEUE_DATA_TASK_PRIORITY, NULL); | ||
|
||
// Start the task scheduling | ||
vTaskStartScheduler(); | ||
} | ||
|
||
// The task scheduler should take over before this is reached | ||
printf("Unreachable code reached!\n"); | ||
} | ||
|
||
/* | ||
* This task emulates collection of data and sending it to another inference task | ||
* for processing | ||
*/ | ||
static void prvDataCollectionTask(void* pvParameters) { | ||
// Unused | ||
(void)pvParameters; | ||
|
||
// Working | ||
vTaskDelay(mainQUEUE_SEND_FREQUENCY_MS); | ||
|
||
// Construct pointer to copy to queue | ||
uint8_t** pucInputData = &input; | ||
xQueueSend(xQueue, &pucInputData, 0U); | ||
} | ||
|
||
/* | ||
* This task emulates the inference of data sent by the collector task | ||
*/ | ||
static void prvInferenceTask(void* pvParameters) { | ||
uint8_t* pucReceivedData; | ||
|
||
// Unused | ||
(void)pvParameters; | ||
|
||
// Wait for data collection | ||
xQueueReceive(xQueue, &pucReceivedData, portMAX_DELAY); | ||
|
||
// Print output of inference and exit task | ||
printf("Running inference\n"); | ||
struct tvmgen_default_inputs xInputs = { | ||
.tfl_quantize = pucReceivedData, | ||
}; | ||
struct tvmgen_default_outputs xOutputs = { | ||
.output = output, | ||
}; | ||
struct ethosu_driver* xDriver = ethosu_reserve_driver(); | ||
struct tvmgen_default_devices xDevices = { | ||
.ethos_u = xDriver, | ||
}; | ||
tvmgen_default_run(&xInputs, &xOutputs, &xDevices); | ||
ethosu_release_driver(xDriver); | ||
|
||
// Calculate index of max value | ||
int8_t ucMaxValue = -128; | ||
int32_t lMaxIndex = -1; | ||
for (unsigned int i = 0; i < output_len; ++i) { | ||
if (output[i] > ucMaxValue) { | ||
ucMaxValue = output[i]; | ||
lMaxIndex = i; | ||
} | ||
} | ||
printf("The image has been classified as '%s'\n", labels[lMaxIndex]); | ||
|
||
// The FVP will shut down when it receives "EXITTHESIM" on the UART | ||
printf("EXITTHESIM\n"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.