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) 2020-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 | #ifndef MESSAGE_HANDLER_H |
| 20 | #define MESSAGE_HANDLER_H |
| 21 | |
| 22 | #include "FreeRTOS.h" |
| 23 | #include "queue.h" |
| 24 | #include "semphr.h" |
| 25 | |
| 26 | #include "message_queue.hpp" |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 27 | #if defined(ETHOSU) |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 28 | #include <ethosu_driver.h> |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 29 | #endif |
Davide Grohmann | 6552005 | 2022-04-07 15:01:34 +0200 | [diff] [blame] | 30 | #include <inference_parser.hpp> |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 31 | #include <inference_process.hpp> |
| 32 | #include <mailbox.hpp> |
| 33 | |
| 34 | #include <cstddef> |
| 35 | #include <cstdio> |
| 36 | #include <vector> |
| 37 | |
| 38 | namespace MessageHandler { |
| 39 | |
| 40 | class IncomingMessageHandler { |
| 41 | public: |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 42 | IncomingMessageHandler(EthosU::ethosu_core_queue &inputMessageQueue, |
| 43 | EthosU::ethosu_core_queue &outputMessageQueue, |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 44 | Mailbox::Mailbox &mailbox, |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 45 | QueueHandle_t inferenceInputQueue, |
| 46 | QueueHandle_t inferenceOutputQueue, |
| 47 | SemaphoreHandle_t messageNotify); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 48 | void run(); |
| 49 | |
| 50 | private: |
| 51 | bool handleMessage(); |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 52 | bool handleInferenceOutput(); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 53 | static void handleIrq(void *userArg); |
| 54 | |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 55 | void sendPong(); |
| 56 | void sendErrorAndResetQueue(EthosU::ethosu_core_msg_err_type type, const char *message); |
| 57 | void sendVersionRsp(); |
| 58 | void sendCapabilitiesRsp(uint64_t userArg); |
| 59 | void sendNetworkInfoRsp(uint64_t userArg, EthosU::ethosu_core_network_buffer &network); |
| 60 | void sendInferenceRsp(EthosU::ethosu_core_inference_rsp &inference); |
| 61 | void sendFailedInferenceRsp(uint64_t userArg, uint32_t status); |
Davide Grohmann | 00de9ee | 2022-03-23 14:59:56 +0100 | [diff] [blame^] | 62 | void sendCancelInferenceRsp(uint64_t userArg, uint32_t status); |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 63 | void readCapabilties(EthosU::ethosu_core_msg_capabilities_rsp &rsp); |
| 64 | |
| 65 | MessageQueue::QueueImpl inputMessageQueue; |
| 66 | MessageQueue::QueueImpl outputMessageQueue; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 67 | Mailbox::Mailbox &mailbox; |
Davide Grohmann | 6552005 | 2022-04-07 15:01:34 +0200 | [diff] [blame] | 68 | InferenceProcess::InferenceParser parser; |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 69 | QueueHandle_t inferenceInputQueue; |
| 70 | QueueHandle_t inferenceOutputQueue; |
| 71 | SemaphoreHandle_t messageNotify; |
| 72 | EthosU::ethosu_core_msg_capabilities_rsp capabilities; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 73 | }; |
| 74 | |
| 75 | class InferenceHandler { |
| 76 | public: |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 77 | InferenceHandler(uint8_t *tensorArena, |
| 78 | size_t arenaSize, |
| 79 | QueueHandle_t inferenceInputQueue, |
| 80 | QueueHandle_t inferenceOutputQueue, |
| 81 | SemaphoreHandle_t messageNotify); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 82 | |
| 83 | void run(); |
| 84 | |
| 85 | private: |
| 86 | void runInference(EthosU::ethosu_core_inference_req &req, EthosU::ethosu_core_inference_rsp &rsp); |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 87 | bool getInferenceJob(const EthosU::ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job); |
| 88 | |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 89 | #if defined(ETHOSU) |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 90 | friend void ::ethosu_inference_begin(struct ethosu_driver *drv, void *userArg); |
| 91 | friend void ::ethosu_inference_end(struct ethosu_driver *drv, void *userArg); |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 92 | #endif |
Davide Grohmann | 134c39e | 2022-04-25 12:21:12 +0200 | [diff] [blame] | 93 | QueueHandle_t inferenceInputQueue; |
| 94 | QueueHandle_t inferenceOutputQueue; |
| 95 | SemaphoreHandle_t messageNotify; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 96 | InferenceProcess::InferenceProcess inference; |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 97 | EthosU::ethosu_core_inference_req *currentReq; |
| 98 | EthosU::ethosu_core_inference_rsp *currentRsp; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 99 | }; |
| 100 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 101 | } // namespace MessageHandler |
| 102 | |
| 103 | #endif |