-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Initial commit with zlog and init implementation
- Loading branch information
0 parents
commit 8df6612
Showing
17 changed files
with
381 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
/target | ||
/Cargo.lock |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"editor.formatOnSave": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
[package] | ||
name = "zlog-rs" | ||
version = "0.1.0" | ||
edition = "2021" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[profile.dev] | ||
panic = "abort" | ||
|
||
[profile.release] | ||
panic = "abort" | ||
incremental = false | ||
lto = "fat" | ||
codegen-units = 1 | ||
opt-level = 3 | ||
|
||
[dependencies] | ||
cty = "0.2.2" | ||
heapless = "0.7.13" | ||
log = "0.4.17" | ||
|
||
|
||
[build-dependencies] | ||
bindgen = "0.59.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
|
||
config ZLOG_RS | ||
bool "Enable logging through zlog-rs" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
# Test on local, build for embedded on NRF52 | ||
|
||
[tasks.cbindgen] | ||
command = "cbindgen" | ||
args = ["--config", "cbindgen.toml", "--crate", "zlog-rs", "--output", "include/generated/libzlog_rs.h"] | ||
|
||
[tasks.build-nrf52] | ||
command = "cargo" | ||
toolchain = "stable" | ||
args = ["build", "--lib", "--target", "thumbv7em-none-eabihf", "--release"] | ||
dependencies = ["cbindgen"] | ||
|
||
[tasks.build-nrf53] | ||
command = "cargo" | ||
toolchain = "stable" | ||
args = ["build", "--lib", "--target", "thumbv8m.main-none-eabihf", "--release"] | ||
dependencies = ["cbindgen"] | ||
|
||
[tasks.build] | ||
command = "cargo" | ||
toolchain = "stable" | ||
args = ["build"] | ||
dependencies = ["build-nrf52", "build-nrf53"] | ||
|
||
[tasks.test-release] | ||
command = "cargo" | ||
toolchain = "stable" | ||
args = ["test", "--lib", "--release"] | ||
dependencies = ["build"] | ||
|
||
[tasks.test] | ||
command = "cargo" | ||
toolchain = "stable" | ||
args = ["test", "--lib"] | ||
dependencies = ["test-release"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# ZLOG | ||
|
||
This repo provides a crate with a Rust Logger implementation for use on an embedded device with Zephyr RTOS. `bindgen` and `cbindgen` are used to bind Rust calls to Zephyr RTOS's log2 implementation | ||
|
||
## Build | ||
|
||
Include this crate in another crate that needs a logger implementation and make the usual log macro calls like `log::trace!("2.0 * 2.0 = {}", 2. * 2.);`. With a high initialization priority, the global logger is initialized to call the matching LOG_DBG, LOG_INF, LOG_WRN, or LOG_ERR macro in the bridging C. | ||
|
||
### Build Tools | ||
|
||
In order to build there are several tools to install | ||
|
||
* NRF Connect SDK | ||
* v1.9.1 | ||
* Rustup and Cargo | ||
* stable | ||
* v1.24.3 and v1.61.0 | ||
* Cross compiling targets | ||
* M4(F): `thumbv7em-none-eabihf` | ||
* M33(F): `thumbv8m.main-none-eabihf` | ||
* Tools and Subcommands | ||
* `cargo install cargo-make,cbindgen` | ||
* `brew install llvm` | ||
* `llvm-config` needs to be in your `PATH` | ||
* `echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc` | ||
* `sudo xcode-select --install` | ||
|
||
If you have all of that, then `cargo make test` should work. | ||
|
||
## Example | ||
|
||
In the example, we have a `blinky` application compiled for NRF52 or NRF53. The CMakeLists.txt of the sample project adds zlog-rs as a static library, exposes the headers to sample app, and runs the build with cargo whenever you build. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
extern crate bindgen; | ||
|
||
use std::env; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
|
||
fn main() { | ||
// Tell cargo to invalidate the built crate whenever the wrapper changes | ||
println!("cargo:rerun-if-changed=include/bridge.h"); | ||
|
||
let llvm_config_path = Command::new("llvm-config") | ||
.args(["--prefix"]) | ||
.output() | ||
.expect("`llvm-config` must be in PATH") | ||
.stdout; | ||
let llvm_config_path = | ||
String::from_utf8(llvm_config_path).expect("`llvm-config --prefix` output must be UTF-8"); | ||
|
||
std::env::set_var( | ||
"LLVM_CONFIG_PATH", | ||
format!("{}/bin/llvm-config", llvm_config_path.trim()), | ||
); | ||
|
||
// The bindgen::Builder is the main entry point | ||
// to bindgen, and lets you build up options for | ||
// the resulting bindings. | ||
let bindings = bindgen::Builder::default() | ||
// The input header we would like to generate | ||
// bindings for. | ||
.header("include/bridge.h") | ||
.allowlist_function("zlog_rs_error_handler") | ||
.allowlist_function("log_.*") | ||
.allowlist_var("CONFIG_.*") | ||
// Tell cargo to invalidate the built crate whenever any of the | ||
// included header files changed. | ||
.parse_callbacks(Box::new(bindgen::CargoCallbacks)) | ||
// Tell cargo that bindings will be no_std | ||
.use_core() | ||
.ctypes_prefix("cty") | ||
// Don't create layout tests that would only run on desktop | ||
.layout_tests(false) | ||
// Finish the builder and generate the bindings. | ||
.generate() | ||
// Unwrap the Result and panic on failure. | ||
.expect("Unable to generate bindings"); | ||
|
||
// Write the bindings to the $OUT_DIR/bindings.rs file. | ||
let out_path = PathBuf::from(env::var("OUT_DIR").unwrap()); | ||
bindings | ||
.write_to_file(out_path.join("bindings.rs")) | ||
.expect("Couldn't write bindings!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
language = "C" | ||
include_guard = "LIBZLOG_RS_INCLUDE_GENERATED_LIB_ZLOG_H" | ||
include_version = true | ||
autogen_warning = "/* DON'T TOUCH. THIS FILE IS AUTOGENERATED */" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#ifndef ZLOG_RS_INCLUDE_BRIDGE_H | ||
#define ZLOG_RS_INCLUDE_BRIDGE_H | ||
|
||
#include <stdbool.h> | ||
#include <stdint.h> | ||
|
||
/// | ||
/// ZEPHYR RTOS | ||
/// | ||
|
||
// FATAL ERROR HANDLER | ||
|
||
__attribute__((__noreturn__)) void zlog_rs_error_handler(); | ||
|
||
// LOG | ||
|
||
void log_inf(const char *restrict msg); | ||
|
||
void log_wrn(const char *restrict msg); | ||
|
||
void log_err(const char *restrict msg); | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#ifndef LIBZLOG_RS_INCLUDE_GENERATED_LIB_ZLOG_H | ||
#define LIBZLOG_RS_INCLUDE_GENERATED_LIB_ZLOG_H | ||
|
||
/* Generated with cbindgen:0.23.0 */ | ||
|
||
/* DON'T TOUCH. THIS FILE IS AUTOGENERATED */ | ||
|
||
#include <stdarg.h> | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdlib.h> | ||
|
||
void zlog_init_error(void); | ||
|
||
void zlog_init_warn(void); | ||
|
||
void zlog_init_info(void); | ||
|
||
void zlog_init_trace(void); | ||
|
||
#endif /* LIBZLOG_RS_INCLUDE_GENERATED_LIB_ZLOG_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
#ifndef ZLOG_RS_INCLUDE_ZLOG_H | ||
#define ZLOG_RS_INCLUDE_ZLOG_H | ||
|
||
#include "zephyr.h" | ||
|
||
#include "generated/libzlog_rs.h" | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
build: | ||
cmake: zephyr | ||
kconfig: zephyr/Kconfig |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[toolchain] | ||
channel = "stable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
#![allow(non_upper_case_globals)] | ||
#![allow(non_camel_case_types)] | ||
#![allow(non_snake_case)] | ||
|
||
include!(concat!(env!("OUT_DIR"), "/bindings.rs")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#include "bridge.h" | ||
#include "zlog.h" | ||
|
||
#include "autoconf.h" | ||
#include "zephyr.h" | ||
#include "kernel.h" | ||
#include "fatal.h" | ||
#include "init.h" | ||
#include "logging/log.h" | ||
|
||
/// | ||
/// ZEPHYR RTOS | ||
/// | ||
|
||
void zlog_init(const struct device* _) | ||
{ | ||
switch (CONFIG_LOG_DEFAULT_LEVEL) | ||
{ | ||
case LOG_LEVEL_ERR: | ||
{ | ||
zlog_init_error(); | ||
break; | ||
} | ||
case LOG_LEVEL_WRN: | ||
{ | ||
zlog_init_warn(); | ||
break; | ||
} | ||
case LOG_LEVEL_INF: | ||
{ | ||
zlog_init_info(); | ||
break; | ||
} | ||
case LOG_LEVEL_DBG: | ||
{ | ||
zlog_init_trace(); | ||
break; | ||
} | ||
default: | ||
{ | ||
zlog_init_trace(); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
SYS_INIT(zlog_init, POST_KERNEL, 10); | ||
|
||
// FATAL ERROR HANDLER | ||
__attribute__((__noreturn__)) void zlog_rs_error_handler() | ||
{ | ||
k_sys_fatal_error_handler(0, NULL); | ||
NVIC_SystemReset(); | ||
} | ||
|
||
// LOG | ||
|
||
LOG_MODULE_REGISTER(rs, LOG_LEVEL_DBG); | ||
|
||
void log_inf(const char *restrict msg) | ||
{ | ||
LOG_INF("%s", msg); | ||
} | ||
|
||
void log_wrn(const char *restrict msg) | ||
{ | ||
LOG_WRN("%s", msg); | ||
} | ||
|
||
void log_err(const char *restrict msg) | ||
{ | ||
LOG_ERR("%s", msg); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#![cfg(not(test))] | ||
|
||
use super::bindings::zlog_rs_error_handler; | ||
use core::panic::PanicInfo; | ||
|
||
#[panic_handler] | ||
fn panic(_panic: &PanicInfo<'_>) -> ! { | ||
// Log is probably too long to print | ||
unsafe { | ||
zlog_rs_error_handler(); | ||
} | ||
unreachable!(); | ||
} |
Oops, something went wrong.