blob: 13a3c60e38cd7f023a3d1b6ce53f5d386eefa1db [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
Yulia Garbovichf61ea352021-11-11 14:16:57 +020030#include <inference_process.hpp>
31#include <mailbox.hpp>
32
33#include <cstddef>
34#include <cstdio>
35#include <vector>
36
37namespace MessageHandler {
38
39class IncomingMessageHandler {
40public:
41 IncomingMessageHandler(EthosU::ethosu_core_queue &messageQueue,
42 Mailbox::Mailbox &mailbox,
43 QueueHandle_t inferenceQueue,
44 QueueHandle_t outputQueue);
45 void run();
46
47private:
48 bool handleMessage();
49 void queueErrorAndResetQueue(EthosU::ethosu_core_msg_err_type type, const char *message);
50 static void handleIrq(void *userArg);
51
52 MessageQueue::QueueImpl messageQueue;
53 Mailbox::Mailbox &mailbox;
54 QueueHandle_t inferenceQueue;
55 QueueHandle_t outputQueue;
56 SemaphoreHandle_t semaphore;
57};
58
59class InferenceHandler {
60public:
61 InferenceHandler(uint8_t *tensorArena, size_t arenaSize, QueueHandle_t inferenceQueue, QueueHandle_t outputQueue);
62
63 void run();
64
65private:
66 void runInference(EthosU::ethosu_core_inference_req &req, EthosU::ethosu_core_inference_rsp &rsp);
67
Kristofer Jonsson585ce692022-03-08 13:28:05 +010068 bool getInferenceJob(const EthosU::ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job);
69
Davide Grohmannadc908c2022-02-16 13:13:27 +010070#if defined(ETHOSU)
Kristofer Jonsson5410db12022-01-27 17:39:06 +010071 friend void ::ethosu_inference_begin(struct ethosu_driver *drv, void *userArg);
72 friend void ::ethosu_inference_end(struct ethosu_driver *drv, void *userArg);
Davide Grohmannadc908c2022-02-16 13:13:27 +010073#endif
Kristofer Jonsson5410db12022-01-27 17:39:06 +010074
Yulia Garbovichf61ea352021-11-11 14:16:57 +020075 QueueHandle_t inferenceQueue;
76 QueueHandle_t outputQueue;
77 InferenceProcess::InferenceProcess inference;
Kristofer Jonsson5410db12022-01-27 17:39:06 +010078 EthosU::ethosu_core_inference_req *currentReq;
79 EthosU::ethosu_core_inference_rsp *currentRsp;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020080};
81
82struct OutputMessage {
83 OutputMessage(EthosU::ethosu_core_msg_type _type = EthosU::ETHOSU_CORE_MSG_MAX) : type(_type) {}
84
85 EthosU::ethosu_core_msg_type type;
86 union {
87 EthosU::ethosu_core_inference_rsp inference;
Kristofer Jonssonac535f02022-03-10 11:08:39 +010088 EthosU::ethosu_core_network_info_rsp networkInfo;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020089 EthosU::ethosu_core_msg_err error;
90 uint64_t userArg;
91 } data;
92};
93
94class OutgoingMessageHandler {
95public:
96 OutgoingMessageHandler(EthosU::ethosu_core_queue &messageQueue,
97 Mailbox::Mailbox &mailbox,
98 QueueHandle_t outputQueue);
99 void run();
100
101private:
102 void sendPong();
103 void sendErrorRsp(EthosU::ethosu_core_msg_err &error);
104 void sendVersionRsp();
105 void sendCapabilitiesRsp(uint64_t userArg);
106 void sendInferenceRsp(EthosU::ethosu_core_inference_rsp &inference);
Kristofer Jonssonac535f02022-03-10 11:08:39 +0100107 void sendNetworkInfoRsp(EthosU::ethosu_core_network_info_rsp &networkInfo);
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200108 void readCapabilties(EthosU::ethosu_core_msg_capabilities_rsp &rsp);
109
110 MessageQueue::QueueImpl messageQueue;
111 Mailbox::Mailbox &mailbox;
112 QueueHandle_t outputQueue;
113 EthosU::ethosu_core_msg_capabilities_rsp capabilities;
114};
115
116} // namespace MessageHandler
117
118#endif