blob: 9f7c7cc469f290e161c03181b76058628b32ede9 [file] [log] [blame]
Yulia Garbovichf61ea352021-11-11 14:16:57 +02001/*
2 * Copyright (c) 2019-2021 Arm Limited. All rights reserved.
3 *
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
131void mailboxIrqHandler() {
132 mailbox.handleMessage();
133}
134
135void inferenceTask(void *pvParameters) {
136 printf("Starting inference task\n");
137
138 uint8_t *arena = reinterpret_cast<uint8_t *>(pvParameters);
139 InferenceHandler process(arena, arenaSize, inferenceQueue, outputQueue);
140 process.run();
141}
142
143void inputMessageTask(void *pvParameters) {
144 (void)pvParameters;
145
146 printf("Starting input message task\n");
147
148 IncomingMessageHandler process(*inputMessageQueue.toQueue(), mailbox, inferenceQueue, outputQueue);
149 process.run();
150}
151
152void outputMessageTask(void *pvParameters) {
153 (void)pvParameters;
154
155 printf("Starting output message task\n");
156
157 MessageHandler::OutgoingMessageHandler process(*outputMessageQueue.toQueue(), mailbox, outputQueue);
158 process.run();
159}
160
161} // namespace
162
163// FreeRTOS application. NOTE: Additional tasks may require increased heap size.
164int main() {
165 BaseType_t ret;
166
167#ifdef MHU_IRQ
168 // Register mailbox interrupt handler
169 NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler);
170 NVIC_EnableIRQ((IRQn_Type)MHU_IRQ);
171#endif
172
173 if (!mailbox.verifyHardware()) {
174 printf("Failed to verify mailbox hardware\n");
175 return 1;
176 }
177
178 // Create message queues for inter process communication
179 inferenceQueue = xQueueCreate(10, sizeof(ethosu_core_inference_req));
180 outputQueue = xQueueCreate(10, sizeof(OutputMessage));
181
182 // Task for handling incoming messages from the remote host
183 ret = xTaskCreate(inputMessageTask, "inputMessageTask", 512, nullptr, 2, nullptr);
184 if (ret != pdPASS) {
185 printf("Failed to create 'inputMessageTask'\n");
186 return ret;
187 }
188
189 // Task for handling outgoing messages resposes to the remote host
190 ret = xTaskCreate(outputMessageTask, "outputMessageTask", 512, nullptr, 2, nullptr);
191 if (ret != pdPASS) {
192 printf("Failed to create 'outputMessageTask'\n");
193 return ret;
194 }
195
196 // One inference task for each NPU
197 for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) {
198 ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &tensorArena[n], 3, nullptr);
199 if (ret != pdPASS) {
200 printf("Failed to create 'inferenceTask%d'\n", n);
201 return ret;
202 }
203 }
204
205 // Start Scheduler
206 vTaskStartScheduler();
207
208 return 1;
209}