blob: 8a363258315fd46f8de1ca07604ae7e9c29c0378 [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 {
73// Queue used to pass inference requests to the inference runner task
74QueueHandle_t inferenceQueue;
75
76// Queue for message responses to the remote host
77QueueHandle_t outputQueue;
78
79// Mailbox driver
80#ifdef MHU_V2
81Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase
82#elif defined(MHU_JUNO)
83Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS);
84#else
85Mailbox::MHUDummy mailbox;
86#endif
87
88} // namespace
89
90/****************************************************************************
91 * Mutex & Semaphore
92 ****************************************************************************/
93
94extern "C" {
95
96void *ethosu_mutex_create(void) {
97 return xSemaphoreCreateMutex();
98}
99
100void ethosu_mutex_lock(void *mutex) {
101 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
102 xSemaphoreTake(handle, portMAX_DELAY);
103}
104
105void ethosu_mutex_unlock(void *mutex) {
106 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex);
107 xSemaphoreGive(handle);
108}
109
110void *ethosu_semaphore_create(void) {
111 return xSemaphoreCreateBinary();
112}
113
114void ethosu_semaphore_take(void *sem) {
115 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
116 xSemaphoreTake(handle, portMAX_DELAY);
117}
118
119void ethosu_semaphore_give(void *sem) {
120 SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem);
121 xSemaphoreGiveFromISR(handle, NULL);
122}
123}
124
125/****************************************************************************
126 * Application
127 ****************************************************************************/
128
129namespace {
130
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100131#ifdef MHU_IRQ
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200132void mailboxIrqHandler() {
133 mailbox.handleMessage();
134}
Kristofer Jonsson29467e02021-11-26 16:10:43 +0100135#endif
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200136
137void inferenceTask(void *pvParameters) {
138 printf("Starting inference task\n");
139
140 uint8_t *arena = reinterpret_cast<uint8_t *>(pvParameters);
141 InferenceHandler process(arena, arenaSize, inferenceQueue, outputQueue);
142 process.run();
143}
144
145void inputMessageTask(void *pvParameters) {
146 (void)pvParameters;
147
148 printf("Starting input message task\n");
149
150 IncomingMessageHandler process(*inputMessageQueue.toQueue(), mailbox, inferenceQueue, outputQueue);
151 process.run();
152}
153
154void outputMessageTask(void *pvParameters) {
155 (void)pvParameters;
156
157 printf("Starting output message task\n");
158
159 MessageHandler::OutgoingMessageHandler process(*outputMessageQueue.toQueue(), mailbox, outputQueue);
160 process.run();
161}
162
163} // namespace
164
165// FreeRTOS application. NOTE: Additional tasks may require increased heap size.
166int main() {
167 BaseType_t ret;
168
169#ifdef MHU_IRQ
170 // Register mailbox interrupt handler
171 NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler);
172 NVIC_EnableIRQ((IRQn_Type)MHU_IRQ);
173#endif
174
175 if (!mailbox.verifyHardware()) {
176 printf("Failed to verify mailbox hardware\n");
177 return 1;
178 }
179
180 // Create message queues for inter process communication
181 inferenceQueue = xQueueCreate(10, sizeof(ethosu_core_inference_req));
182 outputQueue = xQueueCreate(10, sizeof(OutputMessage));
183
184 // Task for handling incoming messages from the remote host
Kristofer Jonssonac535f02022-03-10 11:08:39 +0100185 ret = xTaskCreate(inputMessageTask, "inputMessageTask", 1024, nullptr, 2, nullptr);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200186 if (ret != pdPASS) {
187 printf("Failed to create 'inputMessageTask'\n");
188 return ret;
189 }
190
191 // Task for handling outgoing messages resposes to the remote host
192 ret = xTaskCreate(outputMessageTask, "outputMessageTask", 512, nullptr, 2, nullptr);
193 if (ret != pdPASS) {
194 printf("Failed to create 'outputMessageTask'\n");
195 return ret;
196 }
197
198 // One inference task for each NPU
199 for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) {
200 ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &tensorArena[n], 3, nullptr);
201 if (ret != pdPASS) {
202 printf("Failed to create 'inferenceTask%d'\n", n);
203 return ret;
204 }
205 }
206
207 // Start Scheduler
208 vTaskStartScheduler();
209
210 return 1;
211}