blob: 5a195f0dbd9e0ea66c398adb822d7a69b1e6494f [file] [log] [blame]
Yulia Garbovichf61ea352021-11-11 14:16:57 +02001/*
2 * Copyright (c) 2020-2021 Arm Limited. All rights reserved.
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"
27#include <inference_process.hpp>
28#include <mailbox.hpp>
29
30#include <cstddef>
31#include <cstdio>
32#include <vector>
33
34namespace MessageHandler {
35
36class IncomingMessageHandler {
37public:
38 IncomingMessageHandler(EthosU::ethosu_core_queue &messageQueue,
39 Mailbox::Mailbox &mailbox,
40 QueueHandle_t inferenceQueue,
41 QueueHandle_t outputQueue);
42 void run();
43
44private:
45 bool handleMessage();
46 void queueErrorAndResetQueue(EthosU::ethosu_core_msg_err_type type, const char *message);
47 static void handleIrq(void *userArg);
48
49 MessageQueue::QueueImpl messageQueue;
50 Mailbox::Mailbox &mailbox;
51 QueueHandle_t inferenceQueue;
52 QueueHandle_t outputQueue;
53 SemaphoreHandle_t semaphore;
54};
55
56class InferenceHandler {
57public:
58 InferenceHandler(uint8_t *tensorArena, size_t arenaSize, QueueHandle_t inferenceQueue, QueueHandle_t outputQueue);
59
60 void run();
61
62private:
63 void runInference(EthosU::ethosu_core_inference_req &req, EthosU::ethosu_core_inference_rsp &rsp);
64
65 QueueHandle_t inferenceQueue;
66 QueueHandle_t outputQueue;
67 InferenceProcess::InferenceProcess inference;
68};
69
70struct OutputMessage {
71 OutputMessage(EthosU::ethosu_core_msg_type _type = EthosU::ETHOSU_CORE_MSG_MAX) : type(_type) {}
72
73 EthosU::ethosu_core_msg_type type;
74 union {
75 EthosU::ethosu_core_inference_rsp inference;
76 EthosU::ethosu_core_msg_err error;
77 uint64_t userArg;
78 } data;
79};
80
81class OutgoingMessageHandler {
82public:
83 OutgoingMessageHandler(EthosU::ethosu_core_queue &messageQueue,
84 Mailbox::Mailbox &mailbox,
85 QueueHandle_t outputQueue);
86 void run();
87
88private:
89 void sendPong();
90 void sendErrorRsp(EthosU::ethosu_core_msg_err &error);
91 void sendVersionRsp();
92 void sendCapabilitiesRsp(uint64_t userArg);
93 void sendInferenceRsp(EthosU::ethosu_core_inference_rsp &inference);
94 void readCapabilties(EthosU::ethosu_core_msg_capabilities_rsp &rsp);
95
96 MessageQueue::QueueImpl messageQueue;
97 Mailbox::Mailbox &mailbox;
98 QueueHandle_t outputQueue;
99 EthosU::ethosu_core_msg_capabilities_rsp capabilities;
100};
101
102} // namespace MessageHandler
103
104#endif