blob: 0b4860aaeb04ba424f326c2a0e1f4c20641f804e [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)
Kristofer Jonssona739d212022-05-05 12:11:52 +020055#if defined(ETHOSU) && defined(ETHOSU_NPU_COUNT) && ETHOSU_NPU_COUNT > 0
Yulia Garbovichf61ea352021-11-11 14:16:57 +020056constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT;
57#else
58constexpr size_t NUM_PARALLEL_TASKS = 1;
59#endif
60
61// TensorArena static initialisation
Kristofer Jonssona739d212022-05-05 12:11:52 +020062constexpr size_t arenaSize = TENSOR_ARENA_SIZE;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020063
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
Davide Grohmannbbbd9162022-05-09 15:15:20 +0200101void operator delete(void *ptr, std::size_t) {
102 vPortFree(ptr);
103}
104
Davide Grohmann160001c2022-03-24 15:38:27 +0100105void operator delete[](void *ptr) {
106 vPortFree(ptr);
107}
108
Davide Grohmannbbbd9162022-05-09 15:15:20 +0200109void operator delete[](void *ptr, std::size_t) {
110 vPortFree(ptr);
111}
112
Davide Grohmann160001c2022-03-24 15:38:27 +0100113/****************************************************************************
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200114 * Mutex & Semaphore
115 ****************************************************************************/
116
117extern "C" {
118
119void *ethosu_mutex_create(void) {
120 return xSemaphoreCreateMutex();
121}
122
Ledion Daja60c57372022-04-05 15:04:11 +0200123int ethosu_mutex_lock(void *mutex) {
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200124 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
Ledion Daja60c57372022-04-05 15:04:11 +0200125 if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) {
126 printf("Error: Failed to lock mutex.\n");
127 return -1;
128 }
129 return 0;
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200130}
131
Ledion Daja60c57372022-04-05 15:04:11 +0200132int ethosu_mutex_unlock(void *mutex) {
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200133 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
Ledion Daja60c57372022-04-05 15:04:11 +0200134 if (xSemaphoreGive(handle) != pdTRUE) {
135 printf("Error: Failed to unlock mutex.\n");
136 return -1;
137 }
138 return 0;
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200139}
140
141void *ethosu_semaphore_create(void) {
142 return xSemaphoreCreateBinary();
143}
144
Ledion Daja60c57372022-04-05 15:04:11 +0200145int ethosu_semaphore_take(void *sem) {
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200146 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
Ledion Daja60c57372022-04-05 15:04:11 +0200147 if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) {
148 printf("Error: Failed to take semaphore.\n");
149 return -1;
150 }
151 return 0;
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200152}
153
Ledion Daja60c57372022-04-05 15:04:11 +0200154int ethosu_semaphore_give(void *sem) {
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200155 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
Ledion Daja60c57372022-04-05 15:04:11 +0200156 if (xPortIsInsideInterrupt()) {
157 if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) {
158 printf("Error: Failed to give semaphore from ISR.\n");
159 return -1;
160 }
161 } else {
162 /* A FreeRTOS binary semaphore is fundamentally a queue that can only hold one item. If the queue is full,
163 * xSemaphoreGive will return a pdFALSE value. Ignoring the return value in here, as a semaphore give failure
164 * does not affect the application correctness. */
165 if (xSemaphoreGive(handle) != pdTRUE) {
166 // do nothing
167 }
168 }
169 return 0;
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200170}
171}
172
173/****************************************************************************
174 * Application
175 ****************************************************************************/
Per Åstrandb0550722022-05-18 13:56:26 +0200176namespace {
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200177
Davide Grohmann160001c2022-03-24 15:38:27 +0100178struct TaskParams {
179 TaskParams() :
180 messageNotify(xSemaphoreCreateBinary()),
181 inferenceInputQueue(std::make_shared<Queue<ethosu_core_inference_req>>()),
182 inferenceOutputQueue(xQueueCreate(10, sizeof(ethosu_core_inference_rsp))) {}
183
184 SemaphoreHandle_t messageNotify;
185 // Used to pass inference requests to the inference runner task
186 std::shared_ptr<Queue<ethosu_core_inference_req>> inferenceInputQueue;
187 // Queue for message responses to the remote host
188 QueueHandle_t inferenceOutputQueue;
189};
190
191struct InferenceTaskParams {
192 TaskParams *taskParams;
193 uint8_t *arena;
194};
195
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100196#ifdef MHU_IRQ
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200197void mailboxIrqHandler() {
198 mailbox.handleMessage();
199}
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100200#endif
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200201
202void inferenceTask(void *pvParameters) {
203 printf("Starting inference task\n");
Davide Grohmann160001c2022-03-24 15:38:27 +0100204 InferenceTaskParams *params = reinterpret_cast<InferenceTaskParams *>(pvParameters);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200205
Davide Grohmann160001c2022-03-24 15:38:27 +0100206 InferenceHandler process(params->arena,
207 arenaSize,
208 params->taskParams->inferenceInputQueue,
209 params->taskParams->inferenceOutputQueue,
210 params->taskParams->messageNotify);
211
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200212 process.run();
213}
214
Davide Grohmann160001c2022-03-24 15:38:27 +0100215void messageTask(void *pvParameters) {
216 printf("Starting message task\n");
217 TaskParams *params = reinterpret_cast<TaskParams *>(pvParameters);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200218
Davide Grohmann134c39e2022-04-25 12:21:12 +0200219 IncomingMessageHandler process(*inputMessageQueue.toQueue(),
220 *outputMessageQueue.toQueue(),
221 mailbox,
Davide Grohmann160001c2022-03-24 15:38:27 +0100222 params->inferenceInputQueue,
223 params->inferenceOutputQueue,
224 params->messageNotify);
Kristofer Jonssond89ee0d2022-04-01 15:41:06 +0200225
226#ifdef MHU_IRQ
227 // Register mailbox interrupt handler
228 NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler);
229 NVIC_EnableIRQ((IRQn_Type)MHU_IRQ);
230#endif
231
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200232 process.run();
233}
234
Per Åstrandb0550722022-05-18 13:56:26 +0200235/*
236 * Keep task parameters as global data as FreeRTOS resets the stack when the
237 * scheduler is started.
238 */
239TaskParams taskParams;
240InferenceTaskParams infParams[NUM_PARALLEL_TASKS];
241
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200242} // namespace
243
244// FreeRTOS application. NOTE: Additional tasks may require increased heap size.
245int main() {
246 BaseType_t ret;
247
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200248 if (!mailbox.verifyHardware()) {
249 printf("Failed to verify mailbox hardware\n");
250 return 1;
251 }
252
Davide Grohmann160001c2022-03-24 15:38:27 +0100253 // Task for handling incoming /outgoing messages from the remote host
254 ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200255 if (ret != pdPASS) {
Davide Grohmann134c39e2022-04-25 12:21:12 +0200256 printf("Failed to create 'messageTask'\n");
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200257 return ret;
258 }
259
260 // One inference task for each NPU
261 for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) {
Davide Grohmann160001c2022-03-24 15:38:27 +0100262 infParams[n].taskParams = &taskParams;
263 infParams[n].arena = reinterpret_cast<uint8_t *>(&tensorArena[n]);
264 ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &infParams[n], 3, nullptr);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200265 if (ret != pdPASS) {
266 printf("Failed to create 'inferenceTask%d'\n", n);
267 return ret;
268 }
269 }
270
271 // Start Scheduler
272 vTaskStartScheduler();
273
274 return 1;
275}