Skip to content

Commit

Permalink
Initial commit with zlog and init implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
trueb2 committed May 28, 2022
0 parents commit 8df6612
Show file tree
Hide file tree
Showing 17 changed files with 381 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/target
/Cargo.lock
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"editor.formatOnSave": true
}
26 changes: 26 additions & 0 deletions Cargo.toml
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"
3 changes: 3 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

config ZLOG_RS
bool "Enable logging through zlog-rs"
35 changes: 35 additions & 0 deletions Makefile.toml
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"]
32 changes: 32 additions & 0 deletions README.md
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.
52 changes: 52 additions & 0 deletions build.rs
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!");
}
4 changes: 4 additions & 0 deletions cbindgen.toml
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 */"
23 changes: 23 additions & 0 deletions include/bridge.h
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
21 changes: 21 additions & 0 deletions include/generated/libzlog_rs.h
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 */
8 changes: 8 additions & 0 deletions include/zlog.h
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
3 changes: 3 additions & 0 deletions module.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
build:
cmake: zephyr
kconfig: zephyr/Kconfig
2 changes: 2 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[toolchain]
channel = "stable"
5 changes: 5 additions & 0 deletions src/bindings.rs
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"));
73 changes: 73 additions & 0 deletions src/bridge.c
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);
}
13 changes: 13 additions & 0 deletions src/fatal.rs
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!();
}
Loading

0 comments on commit 8df6612

Please sign in to comment.