blob: b7152f7ea68913307368e86a050647e4ebe3a0ed [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"
Davide Grohmannadc908c2022-02-16 13:13:27 +010027#if defined(ETHOSU)
Kristofer Jonsson5410db12022-01-27 17:39:06 +010028#include <ethosu_driver.h>
Davide Grohmannadc908c2022-02-16 13:13:27 +010029#endif
Davide Grohmann65520052022-04-07 15:01:34 +020030#include <inference_parser.hpp>
Yulia Garbovichf61ea352021-11-11 14:16:57 +020031#include <inference_process.hpp>
32#include <mailbox.hpp>
33
34#include <cstddef>
35#include <cstdio>
36#include <vector>
37
38namespace MessageHandler {
39
40class IncomingMessageHandler {
41public:
42 IncomingMessageHandler(EthosU::ethosu_core_queue &messageQueue,
43 Mailbox::Mailbox &mailbox,
44 QueueHandle_t inferenceQueue,
45 QueueHandle_t outputQueue);
46 void run();
47
48private:
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 Grohmann65520052022-04-07 15:01:34 +020055 InferenceProcess::InferenceParser parser;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020056 QueueHandle_t inferenceQueue;
57 QueueHandle_t outputQueue;
58 SemaphoreHandle_t semaphore;
59};
60
61class InferenceHandler {
62public:
63 InferenceHandler(uint8_t *tensorArena, size_t arenaSize, QueueHandle_t inferenceQueue, QueueHandle_t outputQueue);
64
65 void run();
66
67private:
68 void runInference(EthosU::ethosu_core_inference_req &req, EthosU::ethosu_core_inference_rsp &rsp);
69
Kristofer Jonsson585ce692022-03-08 13:28:05 +010070 bool getInferenceJob(const EthosU::ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job);
71
Davide Grohmannadc908c2022-02-16 13:13:27 +010072#if defined(ETHOSU)
Kristofer Jonsson5410db12022-01-27 17:39:06 +010073 friend void ::ethosu_inference_begin(struct ethosu_driver *drv, void *userArg);
74 friend void ::ethosu_inference_end(struct ethosu_driver *drv, void *userArg);
Davide Grohmannadc908c2022-02-16 13:13:27 +010075#endif
Kristofer Jonsson5410db12022-01-27 17:39:06 +010076
Yulia Garbovichf61ea352021-11-11 14:16:57 +020077 QueueHandle_t inferenceQueue;
78 QueueHandle_t outputQueue;
79 InferenceProcess::InferenceProcess inference;
Kristofer Jonsson5410db12022-01-27 17:39:06 +010080 EthosU::ethosu_core_inference_req *currentReq;
81 EthosU::ethosu_core_inference_rsp *currentRsp;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020082};
83
84struct 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 Jonssonac535f02022-03-10 11:08:39 +010090 EthosU::ethosu_core_network_info_rsp networkInfo;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020091 EthosU::ethosu_core_msg_err error;
92 uint64_t userArg;
93 } data;
94};
95
96class OutgoingMessageHandler {
97public:
98 OutgoingMessageHandler(EthosU::ethosu_core_queue &messageQueue,
99 Mailbox::Mailbox &mailbox,
100 QueueHandle_t outputQueue);
101 void run();
102
103private:
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 Jonssonac535f02022-03-10 11:08:39 +0100109 void sendNetworkInfoRsp(EthosU::ethosu_core_network_info_rsp &networkInfo);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200110 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