Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 1 | /* |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 2 | * Copyright (c) 2019-2022 Arm Limited. |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 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 { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 73 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 74 | SemaphoreHandle_t messageNotify; |
| 75 | QueueHandle_t inferenceInputQueue; |
| 76 | QueueHandle_t inferenceOutputQueue; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 77 | |
| 78 | // Mailbox driver |
| 79 | #ifdef MHU_V2 |
| 80 | Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase |
| 81 | #elif defined(MHU_JUNO) |
| 82 | Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS); |
| 83 | #else |
| 84 | Mailbox::MHUDummy mailbox; |
| 85 | #endif |
| 86 | |
| 87 | } // namespace |
| 88 | |
| 89 | /**************************************************************************** |
| 90 | * Mutex & Semaphore |
| 91 | ****************************************************************************/ |
| 92 | |
| 93 | extern "C" { |
| 94 | |
| 95 | void *ethosu_mutex_create(void) { |
| 96 | return xSemaphoreCreateMutex(); |
| 97 | } |
| 98 | |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 99 | int ethosu_mutex_lock(void *mutex) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 100 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex); |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 101 | if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { |
| 102 | printf("Error: Failed to lock mutex.\n"); |
| 103 | return -1; |
| 104 | } |
| 105 | return 0; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 106 | } |
| 107 | |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 108 | int ethosu_mutex_unlock(void *mutex) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 109 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(mutex); |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 110 | if (xSemaphoreGive(handle) != pdTRUE) { |
| 111 | printf("Error: Failed to unlock mutex.\n"); |
| 112 | return -1; |
| 113 | } |
| 114 | return 0; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | void *ethosu_semaphore_create(void) { |
| 118 | return xSemaphoreCreateBinary(); |
| 119 | } |
| 120 | |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 121 | int ethosu_semaphore_take(void *sem) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 122 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem); |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 123 | if (xSemaphoreTake(handle, portMAX_DELAY) != pdTRUE) { |
| 124 | printf("Error: Failed to take semaphore.\n"); |
| 125 | return -1; |
| 126 | } |
| 127 | return 0; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 128 | } |
| 129 | |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 130 | int ethosu_semaphore_give(void *sem) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 131 | SemaphoreHandle_t handle = reinterpret_cast<SemaphoreHandle_t>(sem); |
Ledion Daja | 60c5737 | 2022-04-05 15:04:11 +0200 | [diff] [blame] | 132 | if (xPortIsInsideInterrupt()) { |
| 133 | if (xSemaphoreGiveFromISR(handle, NULL) != pdTRUE) { |
| 134 | printf("Error: Failed to give semaphore from ISR.\n"); |
| 135 | return -1; |
| 136 | } |
| 137 | } else { |
| 138 | /* A FreeRTOS binary semaphore is fundamentally a queue that can only hold one item. If the queue is full, |
| 139 | * xSemaphoreGive will return a pdFALSE value. Ignoring the return value in here, as a semaphore give failure |
| 140 | * does not affect the application correctness. */ |
| 141 | if (xSemaphoreGive(handle) != pdTRUE) { |
| 142 | // do nothing |
| 143 | } |
| 144 | } |
| 145 | return 0; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
| 149 | /**************************************************************************** |
| 150 | * Application |
| 151 | ****************************************************************************/ |
| 152 | |
| 153 | namespace { |
| 154 | |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 155 | #ifdef MHU_IRQ |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 156 | void mailboxIrqHandler() { |
| 157 | mailbox.handleMessage(); |
| 158 | } |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 159 | #endif |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 160 | |
| 161 | void inferenceTask(void *pvParameters) { |
| 162 | printf("Starting inference task\n"); |
| 163 | |
| 164 | uint8_t *arena = reinterpret_cast<uint8_t *>(pvParameters); |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 165 | InferenceHandler process(arena, arenaSize, inferenceInputQueue, inferenceOutputQueue, messageNotify); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 166 | process.run(); |
| 167 | } |
| 168 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 169 | void messageTask(void *) { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 170 | printf("Starting input message task\n"); |
| 171 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 172 | IncomingMessageHandler process(*inputMessageQueue.toQueue(), |
| 173 | *outputMessageQueue.toQueue(), |
| 174 | mailbox, |
| 175 | inferenceInputQueue, |
| 176 | inferenceOutputQueue, |
| 177 | messageNotify); |
Kristofer Jonsson | d89ee0d | 2022-04-01 15:41:06 +0200 | [diff] [blame] | 178 | |
| 179 | #ifdef MHU_IRQ |
| 180 | // Register mailbox interrupt handler |
| 181 | NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler); |
| 182 | NVIC_EnableIRQ((IRQn_Type)MHU_IRQ); |
| 183 | #endif |
| 184 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 185 | process.run(); |
| 186 | } |
| 187 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 188 | } // namespace |
| 189 | |
| 190 | // FreeRTOS application. NOTE: Additional tasks may require increased heap size. |
| 191 | int main() { |
| 192 | BaseType_t ret; |
| 193 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 194 | if (!mailbox.verifyHardware()) { |
| 195 | printf("Failed to verify mailbox hardware\n"); |
| 196 | return 1; |
| 197 | } |
| 198 | |
| 199 | // Create message queues for inter process communication |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 200 | messageNotify = xSemaphoreCreateBinary(); |
| 201 | inferenceInputQueue = xQueueCreate(10, sizeof(ethosu_core_inference_req)); |
| 202 | inferenceOutputQueue = xQueueCreate(10, sizeof(ethosu_core_inference_rsp)); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 203 | |
| 204 | // Task for handling incoming messages from the remote host |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 205 | ret = xTaskCreate(messageTask, "messageTask", 1024, nullptr, 2, nullptr); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 206 | if (ret != pdPASS) { |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 207 | printf("Failed to create 'messageTask'\n"); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 208 | return ret; |
| 209 | } |
| 210 | |
| 211 | // One inference task for each NPU |
| 212 | for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) { |
| 213 | ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &tensorArena[n], 3, nullptr); |
| 214 | if (ret != pdPASS) { |
| 215 | printf("Failed to create 'inferenceTask%d'\n", n); |
| 216 | return ret; |
| 217 | } |
| 218 | } |
| 219 | |
| 220 | // Start Scheduler |
| 221 | vTaskStartScheduler(); |
| 222 | |
| 223 | return 1; |
| 224 | } |