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" |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 32 | #include "indexed_networks.hpp" |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 33 | #include "message_handler.hpp" |
| 34 | #include "message_queue.hpp" |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 35 | #include "networks.hpp" |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 36 | |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 37 | #include <mailbox.hpp> |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 38 | #if defined(MHU_V2) |
| 39 | #include <mhu_v2.hpp> |
| 40 | #elif defined(MHU_JUNO) |
| 41 | #include <mhu_juno.hpp> |
| 42 | #else |
| 43 | #include <mhu_dummy.hpp> |
| 44 | #endif |
| 45 | |
| 46 | /* Disable semihosting */ |
| 47 | __asm(".global __use_no_semihosting\n\t"); |
| 48 | |
| 49 | using namespace EthosU; |
| 50 | using namespace MessageHandler; |
| 51 | |
| 52 | /**************************************************************************** |
| 53 | * Defines |
| 54 | ****************************************************************************/ |
| 55 | |
| 56 | // Nr. of tasks to process inferences with, reserves driver & runs inference (Normally 1 per NPU, but not a must) |
Kristofer Jonsson | a739d21 | 2022-05-05 12:11:52 +0200 | [diff] [blame] | 57 | #if defined(ETHOSU) && defined(ETHOSU_NPU_COUNT) && ETHOSU_NPU_COUNT > 0 |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 58 | constexpr size_t NUM_PARALLEL_TASKS = ETHOSU_NPU_COUNT; |
| 59 | #else |
| 60 | constexpr size_t NUM_PARALLEL_TASKS = 1; |
| 61 | #endif |
| 62 | |
| 63 | // TensorArena static initialisation |
Kristofer Jonsson | a739d21 | 2022-05-05 12:11:52 +0200 | [diff] [blame] | 64 | constexpr size_t arenaSize = TENSOR_ARENA_SIZE; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 65 | |
| 66 | __attribute__((section(".bss.tensor_arena"), aligned(16))) uint8_t tensorArena[NUM_PARALLEL_TASKS][arenaSize]; |
| 67 | |
| 68 | // Message queue from remote host |
| 69 | __attribute__((section("ethosu_core_in_queue"))) MessageQueue::Queue<1000> inputMessageQueue; |
| 70 | |
| 71 | // Message queue to remote host |
| 72 | __attribute__((section("ethosu_core_out_queue"))) MessageQueue::Queue<1000> outputMessageQueue; |
| 73 | |
| 74 | namespace { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 75 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 76 | // Mailbox driver |
| 77 | #ifdef MHU_V2 |
| 78 | Mailbox::MHUv2 mailbox(MHU_TX_BASE_ADDRESS, MHU_RX_BASE_ADDRESS); // txBase, rxBase |
| 79 | #elif defined(MHU_JUNO) |
| 80 | Mailbox::MHUJuno mailbox(MHU_BASE_ADDRESS); |
| 81 | #else |
| 82 | Mailbox::MHUDummy mailbox; |
| 83 | #endif |
| 84 | |
| 85 | } // namespace |
| 86 | |
| 87 | /**************************************************************************** |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 88 | * Application |
| 89 | ****************************************************************************/ |
Per Åstrand | b055072 | 2022-05-18 13:56:26 +0200 | [diff] [blame] | 90 | namespace { |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 91 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 92 | struct TaskParams { |
| 93 | TaskParams() : |
| 94 | messageNotify(xSemaphoreCreateBinary()), |
| 95 | inferenceInputQueue(std::make_shared<Queue<ethosu_core_inference_req>>()), |
Davide Grohmann | f4379e9 | 2022-06-15 11:20:41 +0200 | [diff] [blame] | 96 | inferenceOutputQueue(xQueueCreate(5, sizeof(ethosu_core_inference_rsp))), |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 97 | networks(std::make_shared<WithIndexedNetworks>()) {} |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 98 | |
| 99 | SemaphoreHandle_t messageNotify; |
| 100 | // Used to pass inference requests to the inference runner task |
| 101 | std::shared_ptr<Queue<ethosu_core_inference_req>> inferenceInputQueue; |
| 102 | // Queue for message responses to the remote host |
| 103 | QueueHandle_t inferenceOutputQueue; |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 104 | // Networks provider |
| 105 | std::shared_ptr<Networks> networks; |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 106 | }; |
| 107 | |
| 108 | struct InferenceTaskParams { |
| 109 | TaskParams *taskParams; |
| 110 | uint8_t *arena; |
| 111 | }; |
| 112 | |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 113 | #ifdef MHU_IRQ |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 114 | void mailboxIrqHandler() { |
| 115 | mailbox.handleMessage(); |
| 116 | } |
Kristofer Jonsson | 29467e0 | 2021-11-26 16:10:43 +0100 | [diff] [blame] | 117 | #endif |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 118 | |
| 119 | void inferenceTask(void *pvParameters) { |
| 120 | printf("Starting inference task\n"); |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 121 | InferenceTaskParams *params = reinterpret_cast<InferenceTaskParams *>(pvParameters); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 122 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 123 | InferenceHandler process(params->arena, |
| 124 | arenaSize, |
| 125 | params->taskParams->inferenceInputQueue, |
| 126 | params->taskParams->inferenceOutputQueue, |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 127 | params->taskParams->messageNotify, |
| 128 | params->taskParams->networks); |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 129 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 130 | process.run(); |
| 131 | } |
| 132 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 133 | void messageTask(void *pvParameters) { |
| 134 | printf("Starting message task\n"); |
| 135 | TaskParams *params = reinterpret_cast<TaskParams *>(pvParameters); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 136 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 137 | IncomingMessageHandler process(*inputMessageQueue.toQueue(), |
| 138 | *outputMessageQueue.toQueue(), |
| 139 | mailbox, |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 140 | params->inferenceInputQueue, |
| 141 | params->inferenceOutputQueue, |
Davide Grohmann | 144b2d2 | 2022-05-31 15:24:02 +0200 | [diff] [blame] | 142 | params->messageNotify, |
| 143 | params->networks); |
Kristofer Jonsson | d89ee0d | 2022-04-01 15:41:06 +0200 | [diff] [blame] | 144 | |
| 145 | #ifdef MHU_IRQ |
| 146 | // Register mailbox interrupt handler |
| 147 | NVIC_SetVector((IRQn_Type)MHU_IRQ, (uint32_t)&mailboxIrqHandler); |
| 148 | NVIC_EnableIRQ((IRQn_Type)MHU_IRQ); |
| 149 | #endif |
| 150 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 151 | process.run(); |
| 152 | } |
| 153 | |
Per Åstrand | b055072 | 2022-05-18 13:56:26 +0200 | [diff] [blame] | 154 | /* |
| 155 | * Keep task parameters as global data as FreeRTOS resets the stack when the |
| 156 | * scheduler is started. |
| 157 | */ |
| 158 | TaskParams taskParams; |
| 159 | InferenceTaskParams infParams[NUM_PARALLEL_TASKS]; |
| 160 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 161 | } // namespace |
| 162 | |
| 163 | // FreeRTOS application. NOTE: Additional tasks may require increased heap size. |
| 164 | int main() { |
| 165 | BaseType_t ret; |
| 166 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 167 | if (!mailbox.verifyHardware()) { |
| 168 | printf("Failed to verify mailbox hardware\n"); |
| 169 | return 1; |
| 170 | } |
| 171 | |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 172 | // Task for handling incoming /outgoing messages from the remote host |
| 173 | ret = xTaskCreate(messageTask, "messageTask", 1024, &taskParams, 2, nullptr); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 174 | if (ret != pdPASS) { |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 175 | printf("Failed to create 'messageTask'\n"); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 176 | return ret; |
| 177 | } |
| 178 | |
| 179 | // One inference task for each NPU |
| 180 | for (size_t n = 0; n < NUM_PARALLEL_TASKS; n++) { |
Davide Grohmann | 160001c | 2022-03-24 15:38:27 +0100 | [diff] [blame] | 181 | infParams[n].taskParams = &taskParams; |
| 182 | infParams[n].arena = reinterpret_cast<uint8_t *>(&tensorArena[n]); |
| 183 | ret = xTaskCreate(inferenceTask, "inferenceTask", 8 * 1024, &infParams[n], 3, nullptr); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 184 | if (ret != pdPASS) { |
| 185 | printf("Failed to create 'inferenceTask%d'\n", n); |
| 186 | return ret; |
| 187 | } |
| 188 | } |
| 189 | |
| 190 | // Start Scheduler |
| 191 | vTaskStartScheduler(); |
| 192 | |
| 193 | return 1; |
| 194 | } |