blob: 90b1cd2593266a17ce415b4b0fe99aa57dd71d6b [file] [log] [blame]
Yulia Garbovichf61ea352021-11-11 14:16:57 +02001/*
Kristofer Jonssonac535f02022-03-10 11:08:39 +01002 * Copyright (c) 2020-2022 Arm Limited.
Yulia Garbovichf61ea352021-11-11 14:16:57 +02003 *
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"
Kristofer Jonsson5410db12022-01-27 17:39:06 +010027#include <ethosu_driver.h>
Yulia Garbovichf61ea352021-11-11 14:16:57 +020028#include <inference_process.hpp>
29#include <mailbox.hpp>
30
31#include <cstddef>
32#include <cstdio>
33#include <vector>
34
35namespace MessageHandler {
36
37class IncomingMessageHandler {
38public:
39 IncomingMessageHandler(EthosU::ethosu_core_queue &messageQueue,
40 Mailbox::Mailbox &mailbox,
41 QueueHandle_t inferenceQueue,
42 QueueHandle_t outputQueue);
43 void run();
44
45private:
46 bool handleMessage();
47 void queueErrorAndResetQueue(EthosU::ethosu_core_msg_err_type type, const char *message);
48 static void handleIrq(void *userArg);
49
50 MessageQueue::QueueImpl messageQueue;
51 Mailbox::Mailbox &mailbox;
52 QueueHandle_t inferenceQueue;
53 QueueHandle_t outputQueue;
54 SemaphoreHandle_t semaphore;
55};
56
57class InferenceHandler {
58public:
59 InferenceHandler(uint8_t *tensorArena, size_t arenaSize, QueueHandle_t inferenceQueue, QueueHandle_t outputQueue);
60
61 void run();
62
63private:
64 void runInference(EthosU::ethosu_core_inference_req &req, EthosU::ethosu_core_inference_rsp &rsp);
65
Kristofer Jonsson585ce692022-03-08 13:28:05 +010066 bool getInferenceJob(const EthosU::ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job);
67
Kristofer Jonsson5410db12022-01-27 17:39:06 +010068 friend void ::ethosu_inference_begin(struct ethosu_driver *drv, void *userArg);
69 friend void ::ethosu_inference_end(struct ethosu_driver *drv, void *userArg);
70
Yulia Garbovichf61ea352021-11-11 14:16:57 +020071 QueueHandle_t inferenceQueue;
72 QueueHandle_t outputQueue;
73 InferenceProcess::InferenceProcess inference;
Kristofer Jonsson5410db12022-01-27 17:39:06 +010074 EthosU::ethosu_core_inference_req *currentReq;
75 EthosU::ethosu_core_inference_rsp *currentRsp;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020076};
77
78struct OutputMessage {
79 OutputMessage(EthosU::ethosu_core_msg_type _type = EthosU::ETHOSU_CORE_MSG_MAX) : type(_type) {}
80
81 EthosU::ethosu_core_msg_type type;
82 union {
83 EthosU::ethosu_core_inference_rsp inference;
Kristofer Jonssonac535f02022-03-10 11:08:39 +010084 EthosU::ethosu_core_network_info_rsp networkInfo;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020085 EthosU::ethosu_core_msg_err error;
86 uint64_t userArg;
87 } data;
88};
89
90class OutgoingMessageHandler {
91public:
92 OutgoingMessageHandler(EthosU::ethosu_core_queue &messageQueue,
93 Mailbox::Mailbox &mailbox,
94 QueueHandle_t outputQueue);
95 void run();
96
97private:
98 void sendPong();
99 void sendErrorRsp(EthosU::ethosu_core_msg_err &error);
100 void sendVersionRsp();
101 void sendCapabilitiesRsp(uint64_t userArg);
102 void sendInferenceRsp(EthosU::ethosu_core_inference_rsp &inference);
Kristofer Jonssonac535f02022-03-10 11:08:39 +0100103 void sendNetworkInfoRsp(EthosU::ethosu_core_network_info_rsp &networkInfo);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200104 void readCapabilties(EthosU::ethosu_core_msg_capabilities_rsp &rsp);
105
106 MessageQueue::QueueImpl messageQueue;
107 Mailbox::Mailbox &mailbox;
108 QueueHandle_t outputQueue;
109 EthosU::ethosu_core_msg_capabilities_rsp capabilities;
110};
111
112} // namespace MessageHandler
113
114#endif