Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 1 | /* |
| 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 | |
| 47 | using namespace EthosU; |
| 48 | using 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) |
| 56 | constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT; |
| 57 | #else |
| 58 | constexpr size_t NUM_PARALLEL_TASKS = 1; |
| 59 | #endif |
| 60 | |
| 61 | // TensorArena static initialisation |
| 62 | constexpr 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 | |
| 72 | namespace { |
| 73 | // Queue used to pass inference requests to the inference runner task |
| 74 | QueueHandle_t inferenceQueue; |
| 75 | |
| 76 | // Queue for message responses to the remote host |
| 77 | QueueHandle_t outputQueue; |
| 78 | |
| 79 | // Mailbox driver |
| 80 | #ifdef MHU_V2 |
| 81 | Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase |
| 82 | #elif defined(MHU_JUNO) |
| 83 | Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS); |
| 84 | #else |
| 85 | Mailbox::MHUDummy mailbox; |
| 86 | #endif |
| 87 | |
| 88 | } // namespace |
| 89 | |
| 90 | /**************************************************************************** |
| 91 | * Mutex & Semaphore |
| 92 | ****************************************************************************/ |
| 93 | |
| 94 | extern "C" { |
| 95 | |
| 96 | void *ethosu_mutex_create(void) { |
| 97 | return xSemaphoreCreateMutex(); |
| 98 | } |
| 99 | |
| 100 | void ethosu_mutex_lock(void *mutex) { |
| 101 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex); |
| 102 | xSemaphoreTake(handle, portMAX_DELAY); |
| 103 | } |
| 104 | |
| 105 | void ethosu_mutex_unlock(void *mutex) { |
| 106 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex); |
| 107 | xSemaphoreGive(handle); |
| 108 | } |
| 109 | |
| 110 | void *ethosu_semaphore_create(void) { |
| 111 | return xSemaphoreCreateBinary(); |
| 112 | } |
| 113 | |
| 114 | void ethosu_semaphore_take(void *sem) { |
| 115 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem); |
| 116 | xSemaphoreTake(handle, portMAX_DELAY); |
| 117 | } |
| 118 | |
| 119 | void 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 | |
| 129 | namespace { |
| 130 | |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 131 | #ifdef MHU_IRQ |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 132 | void mailboxIrqHandler() { |
| 133 | mailbox.handleMessage(); |
| 134 | } |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 135 | #endif |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 136 | |
| 137 | void 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 | |
| 145 | void 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 | |
| 154 | void 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. |
| 166 | int 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 |
| 185 | ret = xTaskCreate(inputMessageTask, "inputMessageTask", 512, nullptr, 2, nullptr); |
| 186 | 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 | } |