blob: 36768ee21681eb8fa2f4cf954edecc5405ca3ced [file] [log] [blame]
Yulia Garbovichf61ea352021-11-11 14:16:57 +02001/*
Kristofer Jonsson5410db12022-01-27 17:39:06 +01002 * Copyright (c) 2020-2022 Arm Limited. All rights reserved.
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 Jonsson5410db12022-01-27 17:39:06 +010066 friend void ::ethosu_inference_begin(struct ethosu_driver *drv, void *userArg);
67 friend void ::ethosu_inference_end(struct ethosu_driver *drv, void *userArg);
68
Yulia Garbovichf61ea352021-11-11 14:16:57 +020069 QueueHandle_t inferenceQueue;
70 QueueHandle_t outputQueue;
71 InferenceProcess::InferenceProcess inference;
Kristofer Jonsson5410db12022-01-27 17:39:06 +010072 EthosU::ethosu_core_inference_req *currentReq;
73 EthosU::ethosu_core_inference_rsp *currentRsp;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020074};
75
76struct OutputMessage {
77 OutputMessage(EthosU::ethosu_core_msg_type _type = EthosU::ETHOSU_CORE_MSG_MAX) : type(_type) {}
78
79 EthosU::ethosu_core_msg_type type;
80 union {
81 EthosU::ethosu_core_inference_rsp inference;
82 EthosU::ethosu_core_msg_err error;
83 uint64_t userArg;
84 } data;
85};
86
87class OutgoingMessageHandler {
88public:
89 OutgoingMessageHandler(EthosU::ethosu_core_queue &messageQueue,
90 Mailbox::Mailbox &mailbox,
91 QueueHandle_t outputQueue);
92 void run();
93
94private:
95 void sendPong();
96 void sendErrorRsp(EthosU::ethosu_core_msg_err &error);
97 void sendVersionRsp();
98 void sendCapabilitiesRsp(uint64_t userArg);
99 void sendInferenceRsp(EthosU::ethosu_core_inference_rsp &inference);
100 void readCapabilties(EthosU::ethosu_core_msg_capabilities_rsp &rsp);
101
102 MessageQueue::QueueImpl messageQueue;
103 Mailbox::Mailbox &mailbox;
104 QueueHandle_t outputQueue;
105 EthosU::ethosu_core_msg_capabilities_rsp capabilities;
106};
107
108} // namespace MessageHandler
109
110#endif