blob: fa7920551c3a70dea9fe26e6828efd3bfa5cd3ee [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:
Davide Grohmann134c39e2022-04-25 12:21:12 +020042 IncomingMessageHandler(EthosU::ethosu_core_queue &inputMessageQueue,
43 EthosU::ethosu_core_queue &outputMessageQueue,
Yulia Garbovichf61ea352021-11-11 14:16:57 +020044 Mailbox::Mailbox &mailbox,
Davide Grohmann134c39e2022-04-25 12:21:12 +020045 QueueHandle_t inferenceInputQueue,
46 QueueHandle_t inferenceOutputQueue,
47 SemaphoreHandle_t messageNotify);
Yulia Garbovichf61ea352021-11-11 14:16:57 +020048 void run();
49
50private:
51 bool handleMessage();
Davide Grohmann134c39e2022-04-25 12:21:12 +020052 bool handleInferenceOutput();
Yulia Garbovichf61ea352021-11-11 14:16:57 +020053 static void handleIrq(void *userArg);
54
Davide Grohmann134c39e2022-04-25 12:21:12 +020055 void sendPong();
56 void sendErrorAndResetQueue(EthosU::ethosu_core_msg_err_type type, const char *message);
57 void sendVersionRsp();
58 void sendCapabilitiesRsp(uint64_t userArg);
59 void sendNetworkInfoRsp(uint64_t userArg, EthosU::ethosu_core_network_buffer &network);
60 void sendInferenceRsp(EthosU::ethosu_core_inference_rsp &inference);
61 void sendFailedInferenceRsp(uint64_t userArg, uint32_t status);
Davide Grohmann00de9ee2022-03-23 14:59:56 +010062 void sendCancelInferenceRsp(uint64_t userArg, uint32_t status);
Davide Grohmann134c39e2022-04-25 12:21:12 +020063 void readCapabilties(EthosU::ethosu_core_msg_capabilities_rsp &rsp);
64
65 MessageQueue::QueueImpl inputMessageQueue;
66 MessageQueue::QueueImpl outputMessageQueue;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020067 Mailbox::Mailbox &mailbox;
Davide Grohmann65520052022-04-07 15:01:34 +020068 InferenceProcess::InferenceParser parser;
Davide Grohmann134c39e2022-04-25 12:21:12 +020069 QueueHandle_t inferenceInputQueue;
70 QueueHandle_t inferenceOutputQueue;
71 SemaphoreHandle_t messageNotify;
72 EthosU::ethosu_core_msg_capabilities_rsp capabilities;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020073};
74
75class InferenceHandler {
76public:
Davide Grohmann134c39e2022-04-25 12:21:12 +020077 InferenceHandler(uint8_t *tensorArena,
78 size_t arenaSize,
79 QueueHandle_t inferenceInputQueue,
80 QueueHandle_t inferenceOutputQueue,
81 SemaphoreHandle_t messageNotify);
Yulia Garbovichf61ea352021-11-11 14:16:57 +020082
83 void run();
84
85private:
86 void runInference(EthosU::ethosu_core_inference_req &req, EthosU::ethosu_core_inference_rsp &rsp);
Kristofer Jonsson585ce692022-03-08 13:28:05 +010087 bool getInferenceJob(const EthosU::ethosu_core_inference_req &req, InferenceProcess::InferenceJob &job);
88
Davide Grohmannadc908c2022-02-16 13:13:27 +010089#if defined(ETHOSU)
Kristofer Jonsson5410db12022-01-27 17:39:06 +010090 friend void ::ethosu_inference_begin(struct ethosu_driver *drv, void *userArg);
91 friend void ::ethosu_inference_end(struct ethosu_driver *drv, void *userArg);
Davide Grohmannadc908c2022-02-16 13:13:27 +010092#endif
Davide Grohmann134c39e2022-04-25 12:21:12 +020093 QueueHandle_t inferenceInputQueue;
94 QueueHandle_t inferenceOutputQueue;
95 SemaphoreHandle_t messageNotify;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020096 InferenceProcess::InferenceProcess inference;
Kristofer Jonsson5410db12022-01-27 17:39:06 +010097 EthosU::ethosu_core_inference_req *currentReq;
98 EthosU::ethosu_core_inference_rsp *currentRsp;
Yulia Garbovichf61ea352021-11-11 14:16:57 +020099};
100
Yulia Garbovichf61ea352021-11-11 14:16:57 +0200101} // namespace MessageHandler
102
103#endif