blob: 4bd721eb22f0b583dd64c36ef1139f7dfd5bfe84 [file] [log] [blame]
Yulia Garbovichf61ea352021-11-11 14:16:57 +02001/*
Kristofer Jonssonac535f02022-03-10 11:08:39 +01002 * Copyright (c) 2019-2022 Arm Limited.
Yulia Garbovichf61ea352021-11-11 14:16:57 +02003 *
4 * SPDX-License-Identifier: Apache-2.0
5 *
6 * Licensed under the Apache License, Version 2.0 (the License); you may
7 * not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
9 *
10 * www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an AS IS BASIS, WITHOUT
14 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18
19/****************************************************************************
20 * Includes
21 ****************************************************************************/
22
23#include "FreeRTOS.h"
24#include "queue.h"
25#include "semphr.h"
26#include "task.h"
27
28#include <inttypes.h>
29#include <stdio.h>
30
31#include "ethosu_core_interface.h"
Davide Grohmann144b2d22022-05-31 15:24:02 +020032#include "indexed_networks.hpp"
Yulia Garbovichf61ea352021-11-11 14:16:57 +020033#include "message_handler.hpp"
34#include "message_queue.hpp"
Davide Grohmann144b2d22022-05-31 15:24:02 +020035#include "networks.hpp"
Yulia Garbovichf61ea352021-11-11 14:16:57 +020036
Davide Grohmann144b2d22022-05-31 15:24:02 +020037#include <mailbox.hpp>
Yulia Garbovichf61ea352021-11-11 14:16:57 +020038#if defined(MHU_V2)
39#include <mhu_v2.hpp>
40#elif defined(MHU_JUNO)
41#include <mhu_juno.hpp>
42#else
43#include <mhu_dummy.hpp>
44#endif
45
46/* Disable semihosting */
47__asm(".global __use_no_semihosting\n\t");
48
49using namespace EthosU;
50using namespace MessageHandler;
51
52/****************************************************************************
53 * Defines
54 ****************************************************************************/
55
56// Nr. of tasks to process inferences with, reserves driver & runs inference (Normally 1 per NPU, but not a must)
Kristofer Jonssona739d212022-05-05 12:11:52 +020057#if defined(ETHOSU) && defined(ETHOSU_NPU_COUNT) && ETHOSU_NPU_COUNT > 0
Yulia Garbovichf61ea352021-11-11 14:16:57 +020058constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT;
59#else
60constexpr size_t NUM_PARALLEL_TASKS = 1;
61#endif
62
63// TensorArena static initialisation
Kristofer Jonssona739d212022-05-05 12:11:52 +020064constexpr size_t arenaSize = TENSOR_ARENA_SIZE;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020065
66__attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t tensorArena[NUM_PARALLEL_TASKS][arenaSize];
67
68// Message queue from remote host
69__attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue;
70
71// Message queue to remote host
72__attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue;
73
74namespace {
Yulia Garbovichf61ea352021-11-11 14:16:57 +020075
Yulia Garbovichf61ea352021-11-11 14:16:57 +020076// Mailbox driver
77#ifdef MHU_V2
78Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase
79#elif defined(MHU_JUNO)
80Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS);
81#else
82Mailbox::MHUDummy mailbox;
83#endif
84
85} // namespace
86
87/****************************************************************************
Yulia Garbovichf61ea352021-11-11 14:16:57 +020088 * Application
89 ****************************************************************************/
Per Åstrandb0550722022-05-18 13:56:26 +020090namespace {
Yulia Garbovichf61ea352021-11-11 14:16:57 +020091
Davide Grohmann160001c2022-03-24 15:38:27 +010092struct TaskParams {
93 TaskParams() :
94 messageNotify(xSemaphoreCreateBinary()),
95 inferenceInputQueue(std::make_shared<Queue<ethosu_core_inference_req>>()),
Davide Grohmannf4379e92022-06-15 11:20:41 +020096 inferenceOutputQueue(xQueueCreate(5, sizeof(ethosu_core_inference_rsp))),
Davide Grohmann144b2d22022-05-31 15:24:02 +020097 networks(std::make_shared<WithIndexedNetworks>()) {}
Davide Grohmann160001c2022-03-24 15:38:27 +010098
99 SemaphoreHandle_t messageNotify;
100 // Used to pass inference requests to the inference runner task
101 std::shared_ptr<Queue<ethosu_core_inference_req>> inferenceInputQueue;
102 // Queue for message responses to the remote host
103 QueueHandle_t inferenceOutputQueue;
Davide Grohmann144b2d22022-05-31 15:24:02 +0200104 // Networks provider
105 std::shared_ptr<Networks> networks;
Davide Grohmann160001c2022-03-24 15:38:27 +0100106};
107
108struct InferenceTaskParams {
109 TaskParams *taskParams;
110 uint8_t *arena;
111};
112
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100113#ifdef MHU_IRQ
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200114void mailboxIrqHandler() {
115 mailbox.handleMessage();
116}
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100117#endif
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200118
119void inferenceTask(void *pvParameters) {
120 printf("Starting inference task\n");
Davide Grohmann160001c2022-03-24 15:38:27 +0100121 InferenceTaskParams *params = reinterpret_cast<InferenceTaskParams *>(pvParameters);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200122
Davide Grohmann160001c2022-03-24 15:38:27 +0100123 InferenceHandler process(params->arena,
124 arenaSize,
125 params->taskParams->inferenceInputQueue,
126 params->taskParams->inferenceOutputQueue,
Davide Grohmann144b2d22022-05-31 15:24:02 +0200127 params->taskParams->messageNotify,
128 params->taskParams->networks);
Davide Grohmann160001c2022-03-24 15:38:27 +0100129
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200130 process.run();
131}
132
Davide Grohmann160001c2022-03-24 15:38:27 +0100133void messageTask(void *pvParameters) {
134 printf("Starting message task\n");
135 TaskParams *params = reinterpret_cast<TaskParams *>(pvParameters);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200136
Davide Grohmann134c39e2022-04-25 12:21:12 +0200137 IncomingMessageHandler process(*inputMessageQueue.toQueue(),
138 *outputMessageQueue.toQueue(),
139 mailbox,
Davide Grohmann160001c2022-03-24 15:38:27 +0100140 params->inferenceInputQueue,
141 params->inferenceOutputQueue,
Davide Grohmann144b2d22022-05-31 15:24:02 +0200142 params->messageNotify,
143 params->networks);
Kristofer Jonssond89ee0d2022-04-01 15:41:06 +0200144
145#ifdef MHU_IRQ
146 // Register mailbox interrupt handler
147 NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler);
148 NVIC_EnableIRQ((IRQn_Type)MHU_IRQ);
149#endif
150
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200151 process.run();
152}
153
Per Åstrandb0550722022-05-18 13:56:26 +0200154/*
155 * Keep task parameters as global data as FreeRTOS resets the stack when the
156 * scheduler is started.
157 */
158TaskParams taskParams;
159InferenceTaskParams infParams[NUM_PARALLEL_TASKS];
160
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200161} // namespace
162
163// FreeRTOS application. NOTE: Additional tasks may require increased heap size.
164int main() {
165 BaseType_t ret;
166
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200167 if (!mailbox.verifyHardware()) {
168 printf("Failed to verify mailbox hardware\n");
169 return 1;
170 }
171
Davide Grohmann160001c2022-03-24 15:38:27 +0100172 // Task for handling incoming /outgoing messages from the remote host
173 ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200174 if (ret != pdPASS) {
Davide Grohmann134c39e2022-04-25 12:21:12 +0200175 printf("Failed to create 'messageTask'\n");
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200176 return ret;
177 }
178
179 // One inference task for each NPU
180 for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) {
Davide Grohmann160001c2022-03-24 15:38:27 +0100181 infParams[n].taskParams = &taskParams;
182 infParams[n].arena = reinterpret_cast<uint8_t *>(&tensorArena[n]);
183 ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &infParams[n], 3, nullptr);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200184 if (ret != pdPASS) {
185 printf("Failed to create 'inferenceTask%d'\n", n);
186 return ret;
187 }
188 }
189
190 // Start Scheduler
191 vTaskStartScheduler();
192
193 return 1;
194}