aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPer Åstrand <per.astrand@arm.com>2020-09-28 13:02:18 +0200
committerPer Åstrand <per.astrand@arm.com>2020-10-06 09:49:02 +0200
commitdc28b13e8bf4e5e7a788f289739e6301494c43a1 (patch)
treee851afe5c12d79e8b36a718ceaa09fcff19b07aa
parentbbd9c8fcfdda36fa69a6b65f95efdb242cbff30a (diff)
downloadethos-u-core-software-dc28b13e8bf4e5e7a788f289739e6301494c43a1.tar.gz
Refactor code to avoid typepunning tricks
Change-Id: Iecefa233561ddbef3e04f395f12fb56cd76ffd42
-rw-r--r--applications/message_process/include/message_process.hpp10
-rw-r--r--applications/message_process/src/message_process.cc42
2 files changed, 37 insertions, 15 deletions
diff --git a/applications/message_process/include/message_process.hpp b/applications/message_process/include/message_process.hpp
index 51f474d..e820b60 100644
--- a/applications/message_process/include/message_process.hpp
+++ b/applications/message_process/include/message_process.hpp
@@ -55,6 +55,7 @@ public:
bool read(uint8_t *dst, uint32_t length);
bool write(const Vec *vec, size_t length);
bool write(const uint32_t type, const void *src = nullptr, uint32_t length = 0);
+ bool skip(uint32_t length);
template <typename T>
bool read(T &dst) {
@@ -62,6 +63,15 @@ public:
}
template <typename T>
+ bool readOrSkip(T &dst, uint32_t expectedLength) {
+ if (expectedLength == sizeof(dst)) {
+ return read(reinterpret_cast<uint8_t *>(&dst), sizeof(dst));
+ } else {
+ return skip(expectedLength);
+ }
+ }
+
+ template <typename T>
bool write(const uint32_t type, const T &src) {
return write(type, reinterpret_cast<const void *>(&src), sizeof(src));
}
diff --git a/applications/message_process/src/message_process.cc b/applications/message_process/src/message_process.cc
index 1be6958..4333640 100644
--- a/applications/message_process/src/message_process.cc
+++ b/applications/message_process/src/message_process.cc
@@ -114,6 +114,22 @@ bool QueueImpl::write(const uint32_t type, const void *src, uint32_t length) {
return write(vec, 2);
}
+bool QueueImpl::skip(uint32_t length) {
+ uint32_t rpos = queue.header.read;
+
+ if (length > available()) {
+ return false;
+ }
+
+ queue.header.read = (rpos + length) % queue.header.size;
+
+#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
+ SCB_CleanDCache();
+#endif
+
+ return true;
+}
+
MessageProcess::MessageProcess(ethosu_core_queue &in,
ethosu_core_queue &out,
::InferenceProcess::InferenceProcess &_inferenceProcess) :
@@ -137,10 +153,6 @@ void MessageProcess::handleIrq() {
bool MessageProcess::handleMessage() {
ethosu_core_msg msg;
- union {
- ethosu_core_inference_req inferenceReq;
- uint8_t data[1000];
- } data;
#if defined(__DCACHE_PRESENT) && (__DCACHE_PRESENT == 1U)
SCB_InvalidateDCache();
@@ -153,21 +165,18 @@ bool MessageProcess::handleMessage() {
printf("Message. type=%" PRIu32 ", length=%" PRIu32 "\n", msg.type, msg.length);
- // Read payload
- if (!queueIn.read(data.data, msg.length)) {
- printf("Failed to read payload.\n");
- return false;
- }
-
switch (msg.type) {
case ETHOSU_CORE_MSG_PING:
printf("Ping\n");
sendPong();
break;
case ETHOSU_CORE_MSG_INFERENCE_REQ: {
- std::memcpy(&data.inferenceReq, data.data, sizeof(data.data));
+ ethosu_core_inference_req req;
- ethosu_core_inference_req &req = data.inferenceReq;
+ if (!queueIn.readOrSkip(req, msg.length)) {
+ printf("Failed to read payload.\n");
+ return false;
+ }
printf("InferenceReq. user_arg=0x%" PRIx64 ", network={0x%" PRIu32 ", %" PRIu32 "}",
req.user_arg,
@@ -212,11 +221,14 @@ bool MessageProcess::handleMessage() {
bool failed = inferenceProcess.runJob(job);
- sendInferenceRsp(data.inferenceReq.user_arg, job.output, failed);
+ sendInferenceRsp(req.user_arg, job.output, failed);
break;
}
- default:
- break;
+ default: {
+ printf("Unexpected message type: %" PRIu32 ", skipping %" PRIu32 " bytes\n", msg.type, msg.length);
+
+ queueIn.skip(msg.length);
+ } break;
}
return true;