blob: b52784096b1fafe9b65a28569a1ff2406c54ab93 [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"
32#include "message_handler.hpp"
33#include "message_queue.hpp"
34#include <mailbox.hpp>
35
36#if defined(MHU_V2)
37#include <mhu_v2.hpp>
38#elif defined(MHU_JUNO)
39#include <mhu_juno.hpp>
40#else
41#include <mhu_dummy.hpp>
42#endif
43
44/* Disable semihosting */
45__asm(".global __use_no_semihosting\n\t");
46
47using namespace EthosU;
48using namespace MessageHandler;
49
50/****************************************************************************
51 * Defines
52 ****************************************************************************/
53
54// Nr. of tasks to process inferences with, reserves driver & runs inference (Normally 1 per NPU, but not a must)
55#if defined(ETHOSU_NPU_COUNT)
56constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT;
57#else
58constexpr size_t NUM_PARALLEL_TASKS = 1;
59#endif
60
61// TensorArena static initialisation
62constexpr size_t arenaSize = TENSOR_ARENA_SIZE / NUM_PARALLEL_TASKS;
63
64__attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t tensorArena[NUM_PARALLEL_TASKS][arenaSize];
65
66// Message queue from remote host
67__attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue;
68
69// Message queue to remote host
70__attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue;
71
72namespace {
Yulia Garbovichf61ea352021-11-11 14:16:57 +020073
Yulia Garbovichf61ea352021-11-11 14:16:57 +020074// Mailbox driver
75#ifdef MHU_V2
76Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase
77#elif defined(MHU_JUNO)
78Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS);
79#else
80Mailbox::MHUDummy mailbox;
81#endif
82
83} // namespace
84
85/****************************************************************************
Davide Grohmann160001c2022-03-24 15:38:27 +010086 * Override new operators to call in FreeRTOS allocator
87 ****************************************************************************/
88
89void *operator new(size_t size) {
90 return pvPortMalloc(size);
91}
92
93void *operator new[](size_t size) {
94 return pvPortMalloc(size);
95}
96
97void operator delete(void *ptr) {
98 vPortFree(ptr);
99}
100
101void operator delete[](void *ptr) {
102 vPortFree(ptr);
103}
104
105/****************************************************************************
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200106 * Mutex & Semaphore
107 ****************************************************************************/
108
109extern "C" {
110
111void *ethosu_mutex_create(void) {
112 return xSemaphoreCreateMutex();
113}
114
Ledion Daja60c57372022-04-05 15:04:11 +0200115int ethosu_mutex_lock(void *mutex) {
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200116 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
Ledion Daja60c57372022-04-05 15:04:11 +0200117 if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) {
118 printf("Error: Failed to lock mutex.\n");
119 return -1;
120 }
121 return 0;
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200122}
123
Ledion Daja60c57372022-04-05 15:04:11 +0200124int ethosu_mutex_unlock(void *mutex) {
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200125 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
Ledion Daja60c57372022-04-05 15:04:11 +0200126 if (xSemaphoreGive(handle) != pdTRUE) {
127 printf("Error: Failed to unlock mutex.\n");
128 return -1;
129 }
130 return 0;
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200131}
132
133void *ethosu_semaphore_create(void) {
134 return xSemaphoreCreateBinary();
135}
136
Ledion Daja60c57372022-04-05 15:04:11 +0200137int ethosu_semaphore_take(void *sem) {
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200138 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
Ledion Daja60c57372022-04-05 15:04:11 +0200139 if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) {
140 printf("Error: Failed to take semaphore.\n");
141 return -1;
142 }
143 return 0;
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200144}
145
Ledion Daja60c57372022-04-05 15:04:11 +0200146int ethosu_semaphore_give(void *sem) {
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200147 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
Ledion Daja60c57372022-04-05 15:04:11 +0200148 if (xPortIsInsideInterrupt()) {
149 if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) {
150 printf("Error: Failed to give semaphore from ISR.\n");
151 return -1;
152 }
153 } else {
154 /* A FreeRTOS binary semaphore is fundamentally a queue that can only hold one item. If the queue is full,
155 * xSemaphoreGive will return a pdFALSE value. Ignoring the return value in here, as a semaphore give failure
156 * does not affect the application correctness. */
157 if (xSemaphoreGive(handle) != pdTRUE) {
158 // do nothing
159 }
160 }
161 return 0;
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200162}
163}
164
165/****************************************************************************
166 * Application
167 ****************************************************************************/
168
Davide Grohmann160001c2022-03-24 15:38:27 +0100169struct TaskParams {
170 TaskParams() :
171 messageNotify(xSemaphoreCreateBinary()),
172 inferenceInputQueue(std::make_shared<Queue<ethosu_core_inference_req>>()),
173 inferenceOutputQueue(xQueueCreate(10, sizeof(ethosu_core_inference_rsp))) {}
174
175 SemaphoreHandle_t messageNotify;
176 // Used to pass inference requests to the inference runner task
177 std::shared_ptr<Queue<ethosu_core_inference_req>> inferenceInputQueue;
178 // Queue for message responses to the remote host
179 QueueHandle_t inferenceOutputQueue;
180};
181
182struct InferenceTaskParams {
183 TaskParams *taskParams;
184 uint8_t *arena;
185};
186
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200187namespace {
188
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100189#ifdef MHU_IRQ
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200190void mailboxIrqHandler() {
191 mailbox.handleMessage();
192}
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100193#endif
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200194
195void inferenceTask(void *pvParameters) {
196 printf("Starting inference task\n");
Davide Grohmann160001c2022-03-24 15:38:27 +0100197 InferenceTaskParams *params = reinterpret_cast<InferenceTaskParams *>(pvParameters);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200198
Davide Grohmann160001c2022-03-24 15:38:27 +0100199 InferenceHandler process(params->arena,
200 arenaSize,
201 params->taskParams->inferenceInputQueue,
202 params->taskParams->inferenceOutputQueue,
203 params->taskParams->messageNotify);
204
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200205 process.run();
206}
207
Davide Grohmann160001c2022-03-24 15:38:27 +0100208void messageTask(void *pvParameters) {
209 printf("Starting message task\n");
210 TaskParams *params = reinterpret_cast<TaskParams *>(pvParameters);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200211
Davide Grohmann134c39e2022-04-25 12:21:12 +0200212 IncomingMessageHandler process(*inputMessageQueue.toQueue(),
213 *outputMessageQueue.toQueue(),
214 mailbox,
Davide Grohmann160001c2022-03-24 15:38:27 +0100215 params->inferenceInputQueue,
216 params->inferenceOutputQueue,
217 params->messageNotify);
Kristofer Jonssond89ee0d2022-04-01 15:41:06 +0200218
219#ifdef MHU_IRQ
220 // Register mailbox interrupt handler
221 NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler);
222 NVIC_EnableIRQ((IRQn_Type)MHU_IRQ);
223#endif
224
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200225 process.run();
226}
227
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200228} // namespace
229
230// FreeRTOS application. NOTE: Additional tasks may require increased heap size.
231int main() {
232 BaseType_t ret;
233
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200234 if (!mailbox.verifyHardware()) {
235 printf("Failed to verify mailbox hardware\n");
236 return 1;
237 }
238
Davide Grohmann160001c2022-03-24 15:38:27 +0100239 TaskParams taskParams;
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200240
Davide Grohmann160001c2022-03-24 15:38:27 +0100241 // Task for handling incoming /outgoing messages from the remote host
242 ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200243 if (ret != pdPASS) {
Davide Grohmann134c39e2022-04-25 12:21:12 +0200244 printf("Failed to create 'messageTask'\n");
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200245 return ret;
246 }
247
Davide Grohmann160001c2022-03-24 15:38:27 +0100248 InferenceTaskParams infParams[NUM_PARALLEL_TASKS];
249
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200250 // One inference task for each NPU
251 for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) {
Davide Grohmann160001c2022-03-24 15:38:27 +0100252 infParams[n].taskParams = &taskParams;
253 infParams[n].arena = reinterpret_cast<uint8_t *>(&tensorArena[n]);
254 ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &infParams[n], 3, nullptr);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200255 if (ret != pdPASS) {
256 printf("Failed to create 'inferenceTask%d'\n", n);
257 return ret;
258 }
259 }
260
261 // Start Scheduler
262 vTaskStartScheduler();
263
264 return 1;
265}