summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2025-03-21 22:28:09 +0100
committerxengineering <me@xengineering.eu>2025-03-21 22:28:09 +0100
commitfb9bf2457dae53cd10a916c9beac6d292e28654a (patch)
tree41073c75c82c59ae86d69d6a172852132629ad84
parent61e108a0ae856b3e1c849482d0008f698d41db27 (diff)
downloadiot-contact-fb9bf2457dae53cd10a916c9beac6d292e28654a.tar
iot-contact-fb9bf2457dae53cd10a916c9beac6d292e28654a.tar.zst
iot-contact-fb9bf2457dae53cd10a916c9beac6d292e28654a.zip
fw: heart: Add zbus-based heartbeat code
The heartbeat of the firmware might be used for multiple purposes. It can trigger a blinking LED on the PCB, can be displayed in a client program or might serve additional purposes. Since at least display in client programs should be implemented and multiple clients should be support in long term it improves the code structure to use a zbus channel here to publish heartbeat messages in a publish-subscribe pattern. That way the publishing of the heartbeat message and the receiving by an unknown number of observers is completely decoupled. A central trait of the publish-subscribe pattern and an advantage for a modular code structure.
-rw-r--r--fw/CMakeLists.txt1
-rw-r--r--fw/prj.conf7
-rw-r--r--fw/src/heart.c52
-rw-r--r--fw/src/heart.h20
4 files changed, 79 insertions, 1 deletions
diff --git a/fw/CMakeLists.txt b/fw/CMakeLists.txt
index cffb6a1..b7a3783 100644
--- a/fw/CMakeLists.txt
+++ b/fw/CMakeLists.txt
@@ -23,6 +23,7 @@ target_sources(app
"${CMAKE_CURRENT_SOURCE_DIR}/src/syslog.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/mac.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/http.c"
+ "${CMAKE_CURRENT_SOURCE_DIR}/src/heart.c"
)
zephyr_linker_sources(SECTIONS sections-rom.ld)
diff --git a/fw/prj.conf b/fw/prj.conf
index bd761fc..7984caa 100644
--- a/fw/prj.conf
+++ b/fw/prj.conf
@@ -20,10 +20,15 @@ CONFIG_LOG_MODE_DEFERRED=y
CONFIG_POSIX_C_LANG_SUPPORT_R=y
-CONFIG_HTTP_SERVER=y
CONFIG_HTTP_PARSER=y
CONFIG_HTTP_PARSER_URL=y
+CONFIG_HTTP_SERVER=y
+
CONFIG_FILE_SYSTEM=y
CONFIG_EVENTFD=y
+
+CONFIG_ZBUS=y
+CONFIG_ZBUS_MSG_SUBSCRIBER=y
+CONFIG_HEAP_MEM_POOL_SIZE=2048
diff --git a/fw/src/heart.c b/fw/src/heart.c
new file mode 100644
index 0000000..6bcced6
--- /dev/null
+++ b/fw/src/heart.c
@@ -0,0 +1,52 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public License,
+ * v. 2.0. If a copy of the MPL was not distributed with this file, You can
+ * obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+
+#include <stdbool.h>
+
+#include <zephyr/kernel.h>
+#include <zephyr/logging/log.h>
+
+#include "heart.h"
+
+
+LOG_MODULE_REGISTER(heart);
+
+
+#define HEART_PERIOD K_MSEC(1000)
+#define HEART_STACK_SIZE 500
+#define HEART_PRIO K_PRIO_PREEMPT(8)
+
+
+ZBUS_CHAN_DEFINE(
+ heartbeat_channel,
+ struct heartbeat,
+ NULL,
+ NULL,
+ ZBUS_OBSERVERS_EMPTY,
+ ZBUS_MSG_INIT(.ttl_ms = 0)
+);
+
+
+static void heart_thread_function(void *ptr1, void *ptr2, void *ptr3) {
+ LOG_INF("Starting to beat");
+
+ struct heartbeat heartbeat = {.ttl_ms = 1100};
+
+ while (true) {
+ LOG_DBG("Heart beat");
+ int ret = zbus_chan_pub(&heartbeat_channel, &heartbeat, K_FOREVER);
+ if (ret < 0) {
+ LOG_ERR("Could not publish heartbeat");
+ }
+ k_sleep(HEART_PERIOD);
+ }
+}
+
+
+K_THREAD_DEFINE(heart_thread, HEART_STACK_SIZE,
+ heart_thread_function, NULL, NULL, NULL,
+ HEART_PRIO, 0, 0);
diff --git a/fw/src/heart.h b/fw/src/heart.h
new file mode 100644
index 0000000..cd32c15
--- /dev/null
+++ b/fw/src/heart.h
@@ -0,0 +1,20 @@
+/*
+ * This Source Code Form is subject to the terms of the Mozilla Public License,
+ * v. 2.0. If a copy of the MPL was not distributed with this file, You can
+ * obtain one at https://mozilla.org/MPL/2.0/.
+ */
+
+#ifndef SRC_HEART_H
+#define SRC_HEART_H
+
+
+#include <zephyr/zbus/zbus.h>
+
+
+struct heartbeat {
+ uint32_t ttl_ms;
+};
+
+ZBUS_CHAN_DECLARE(heartbeat_channel);
+
+#endif // !SRC_HEART_H