diff options
author | xengineering <me@xengineering.eu> | 2024-04-20 22:08:08 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2024-04-21 12:26:00 +0200 |
commit | 96f2358caadfd55d6fd3b888ef5d21f22fc2439c (patch) | |
tree | 4d2145c0df358fa917bdec729e905130230bf7f6 | |
parent | 9a713b06c3f776684dd026646c916b6daea7b25b (diff) | |
download | iot-core-96f2358caadfd55d6fd3b888ef5d21f22fc2439c.tar iot-core-96f2358caadfd55d6fd3b888ef5d21f22fc2439c.tar.zst iot-core-96f2358caadfd55d6fd3b888ef5d21f22fc2439c.zip |
firmware: Add 64 bit MAC to shell prompt
This demonstrates that the readout of the 64 bit MAC address from the
STM32WL55 microcontroller works and is useful to recognize used devices.
-rw-r--r-- | firmware/CMakeLists.txt | 4 | ||||
-rw-r--r-- | firmware/boards/nucleo_wl55jc.overlay | 7 | ||||
-rw-r--r-- | firmware/src/main.c | 32 | ||||
-rw-r--r-- | firmware/src/uid64.c | 41 | ||||
-rw-r--r-- | firmware/src/uid64.h | 8 |
5 files changed, 91 insertions, 1 deletions
diff --git a/firmware/CMakeLists.txt b/firmware/CMakeLists.txt index 70b99c7..ad51a9d 100644 --- a/firmware/CMakeLists.txt +++ b/firmware/CMakeLists.txt @@ -2,4 +2,6 @@ cmake_minimum_required(VERSION 3.20.0) find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) project(iot-core) -target_sources(app PRIVATE src/main.c) +target_sources(app PRIVATE + src/main.c + src/uid64.c) diff --git a/firmware/boards/nucleo_wl55jc.overlay b/firmware/boards/nucleo_wl55jc.overlay new file mode 100644 index 0000000..b04f810 --- /dev/null +++ b/firmware/boards/nucleo_wl55jc.overlay @@ -0,0 +1,7 @@ +/ { + soc { + uid64: uid64@1fff7580 { + reg = <0x1fff7580 0x8>; + }; + }; +}; diff --git a/firmware/src/main.c b/firmware/src/main.c index 7a1076c..e1c85c6 100644 --- a/firmware/src/main.c +++ b/firmware/src/main.c @@ -1,6 +1,38 @@ +#include <stdio.h> + #include <zephyr/kernel.h> +#include <zephyr/shell/shell.h> +#include <zephyr/shell/shell_backend.h> + +#include "uid64.h" + +static uint64_t uid = 0; +static char uid_str[UID64_STR_LEN]; + +#define MAX_PROMPT_SIZE 80 +char prompt[MAX_PROMPT_SIZE]; + +void init_uid64(void) { + uid = uid64_get(); + uid64_to_string(uid, uid_str); +} + +void init_shell_prompt(void) { + if (uid64_available()) { + snprintf(prompt, MAX_PROMPT_SIZE, "[iot-core %s] ", uid_str); + } else { + strncpy(prompt, "[iot-core without MAC] ", MAX_PROMPT_SIZE); + } + for (int i = 0; i < shell_backend_count_get(); i++) { + const struct shell *sh = shell_backend_get(i); + (void) shell_prompt_change(sh, (const char *)prompt); + } +} int main(void) { + init_uid64(); + init_shell_prompt(); + return 0; } diff --git a/firmware/src/uid64.c b/firmware/src/uid64.c new file mode 100644 index 0000000..984b7fe --- /dev/null +++ b/firmware/src/uid64.c @@ -0,0 +1,41 @@ +#include <stdio.h> + +#include <zephyr/devicetree.h> + +#include "uid64.h" + +#define UID64_NODE DT_NODELABEL(uid64) + +uint64_t uid64_get(void) { +#if DT_NODE_EXISTS(UID64_NODE) + return *(volatile uint64_t *) DT_REG_ADDR(UID64_NODE); +#else + return 0; +#endif +} + +void uid64_to_string(uint64_t uid, char *str) { + uint8_t *source = (uint8_t *)&uid + 7; + char *sink = str; + + for (unsigned int i = 0; i < 8; i++) { + /* delimiter */ + if (i > 0) { + *sink = ':'; + sink++; + } + + /* byte values */ + snprintf(sink, 3, "%02x", *source); + source--; + sink += 2; + } +} + +bool uid64_available(void) { +#if DT_NODE_EXISTS(UID64_NODE) + return true; +#else + return false; +#endif +} diff --git a/firmware/src/uid64.h b/firmware/src/uid64.h new file mode 100644 index 0000000..cd3fb13 --- /dev/null +++ b/firmware/src/uid64.h @@ -0,0 +1,8 @@ +#include <zephyr/devicetree.h> + +/* 2 chars per byte (hex), 1 char per ':' delimiter and one char for \0 */ +#define UID64_STR_LEN (8*2 + 7*1 + 1) + +uint64_t uid64_get(void); +void uid64_to_string(uint64_t, char *str); +bool uid64_available(void); |