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: |
| 42 | IncomingMessageHandler(EthosU::ethosu_core_queue &messageQueue, |
| 43 | Mailbox::Mailbox &mailbox, |
| 44 | QueueHandle_t inferenceQueue, |
| 45 | QueueHandle_t outputQueue); |
| 46 | void run(); |
| 47 | |
| 48 | private: |
| 49 | bool handleMessage(); |
| 50 | void queueErrorAndResetQueue(EthosU::ethosu_core_msg_err_type type, const char *message); |
| 51 | static void handleIrq(void *userArg); |
| 52 | |
| 53 | MessageQueue::QueueImpl messageQueue; |
| 54 | Mailbox::Mailbox &mailbox; |
Davide Grohmann | 6552005 | 2022-04-07 15:01:34 +0200 | [diff] [blame^] | 55 | InferenceProcess::InferenceParser parser; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 56 | QueueHandle_t inferenceQueue; |
| 57 | QueueHandle_t outputQueue; |
| 58 | SemaphoreHandle_t semaphore; |
| 59 | }; |
| 60 | |
| 61 | class InferenceHandler { |
| 62 | public: |
| 63 | InferenceHandler(uint8_t *tensorArena, size_t arenaSize, QueueHandle_t inferenceQueue, QueueHandle_t outputQueue); |
| 64 | |
| 65 | void run(); |
| 66 | |
| 67 | private: |
| 68 | void runInference(EthosU::ethosu_core_inference_req &req, EthosU::ethosu_core_inference_rsp &rsp); |
| 69 | |
Kristofer Jonsson | 585ce69 | 2022-03-08 13:28:05 +0100 | [diff] [blame] | 70 | bool getInferenceJob(const EthosU::ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job); |
| 71 | |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 72 | #if defined(ETHOSU) |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 73 | friend void ::ethosu_inference_begin(struct ethosu_driver *drv, void *userArg); |
| 74 | friend void ::ethosu_inference_end(struct ethosu_driver *drv, void *userArg); |
Davide Grohmann | adc908c | 2022-02-16 13:13:27 +0100 | [diff] [blame] | 75 | #endif |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 76 | |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 77 | QueueHandle_t inferenceQueue; |
| 78 | QueueHandle_t outputQueue; |
| 79 | InferenceProcess::InferenceProcess inference; |
Kristofer Jonsson | 5410db1 | 2022-01-27 17:39:06 +0100 | [diff] [blame] | 80 | EthosU::ethosu_core_inference_req *currentReq; |
| 81 | EthosU::ethosu_core_inference_rsp *currentRsp; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 82 | }; |
| 83 | |
| 84 | struct OutputMessage { |
| 85 | OutputMessage(EthosU::ethosu_core_msg_type _type = EthosU::ETHOSU_CORE_MSG_MAX) : type(_type) {} |
| 86 | |
| 87 | EthosU::ethosu_core_msg_type type; |
| 88 | union { |
| 89 | EthosU::ethosu_core_inference_rsp inference; |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 90 | EthosU::ethosu_core_network_info_rsp networkInfo; |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 91 | EthosU::ethosu_core_msg_err error; |
| 92 | uint64_t userArg; |
| 93 | } data; |
| 94 | }; |
| 95 | |
| 96 | class OutgoingMessageHandler { |
| 97 | public: |
| 98 | OutgoingMessageHandler(EthosU::ethosu_core_queue &messageQueue, |
| 99 | Mailbox::Mailbox &mailbox, |
| 100 | QueueHandle_t outputQueue); |
| 101 | void run(); |
| 102 | |
| 103 | private: |
| 104 | void sendPong(); |
| 105 | void sendErrorRsp(EthosU::ethosu_core_msg_err &error); |
| 106 | void sendVersionRsp(); |
| 107 | void sendCapabilitiesRsp(uint64_t userArg); |
| 108 | void sendInferenceRsp(EthosU::ethosu_core_inference_rsp &inference); |
Kristofer Jonsson | ac535f0 | 2022-03-10 11:08:39 +0100 | [diff] [blame] | 109 | void sendNetworkInfoRsp(EthosU::ethosu_core_network_info_rsp &networkInfo); |
Yulia Garbovich | f61ea35 | 2021-11-11 14:16:57 +0200 | [diff] [blame] | 110 | void readCapabilties(EthosU::ethosu_core_msg_capabilities_rsp &rsp); |
| 111 | |
| 112 | MessageQueue::QueueImpl messageQueue; |
| 113 | Mailbox::Mailbox &mailbox; |
| 114 | QueueHandle_t outputQueue; |
| 115 | EthosU::ethosu_core_msg_capabilities_rsp capabilities; |
| 116 | }; |
| 117 | |
| 118 | } // namespace MessageHandler |
| 119 | |
| 120 | #endif |