From f825eacf16706f39a7b076add2bc178d07eea5a2 Mon Sep 17 00:00:00 2001 From: Kestrel Date: Sun, 19 Nov 2023 00:42:36 -0800 Subject: [PATCH] Add example peripherals --- .gitignore | 5 ++- examples/c-peripheral/peripheral.c | 23 ++++++++++ examples/c-peripheral/peripheral.h | 6 +++ examples/rust-peripheral/Cargo.lock | 65 +++++++++++++++++++++++++++++ examples/rust-peripheral/Cargo.toml | 13 ++++++ examples/rust-peripheral/src/lib.rs | 29 +++++++++++++ 6 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 examples/c-peripheral/peripheral.c create mode 100644 examples/c-peripheral/peripheral.h create mode 100644 examples/rust-peripheral/Cargo.lock create mode 100644 examples/rust-peripheral/Cargo.toml create mode 100644 examples/rust-peripheral/src/lib.rs diff --git a/.gitignore b/.gitignore index 81cf465..176538e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,5 @@ -/target +target /.vscode +*.dll +*.so +*.dylib diff --git a/examples/c-peripheral/peripheral.c b/examples/c-peripheral/peripheral.c new file mode 100644 index 0000000..e54238d --- /dev/null +++ b/examples/c-peripheral/peripheral.c @@ -0,0 +1,23 @@ +#include "peripheral.h" +#include +#include + +uint8_t STATE = 0x00; + +__declspec(dllexport) void init(void) { + printf("C init\n"); +} + +__declspec(dllexport) uint8_t read(void) { + printf("C read\n"); + return STATE; +} + +__declspec(dllexport) void write(uint8_t data) { + printf("C write\n"); + STATE = data; +} + +__declspec(dllexport) const char* name(void) { + return "C Example"; +} diff --git a/examples/c-peripheral/peripheral.h b/examples/c-peripheral/peripheral.h new file mode 100644 index 0000000..7469953 --- /dev/null +++ b/examples/c-peripheral/peripheral.h @@ -0,0 +1,6 @@ +#include + +__declspec(dllexport) void init(void); +__declspec(dllexport) uint8_t read(void); +__declspec(dllexport) void write(uint8_t data); +__declspec(dllexport) const char* name(void); diff --git a/examples/rust-peripheral/Cargo.lock b/examples/rust-peripheral/Cargo.lock new file mode 100644 index 0000000..a069d81 --- /dev/null +++ b/examples/rust-peripheral/Cargo.lock @@ -0,0 +1,65 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "byte-strings" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "002ee5531feb8450e59862fefa550eeac39b726d60b186071672751045ebc29a" +dependencies = [ + "byte-strings-proc_macros", +] + +[[package]] +name = "byte-strings-proc_macros" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "62f7e0e71f98d6c71bfe42b0a7a47d0f870ad808401fad2d44fa156ed5b0ae03" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "example-peripheral" +version = "0.1.0" +dependencies = [ + "byte-strings", +] + +[[package]] +name = "proc-macro2" +version = "1.0.69" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "134c189feb4956b20f6f547d2cf727d4c0fe06722b20a0eec87ed445a97f92da" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.33" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5267fca4496028628a95160fc423a33e8b2e6af8a5302579e322e4b520293cae" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.39" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23e78b90f2fcf45d3e842032ce32e3f2d1545ba6636271dcbf24fa306d87be7a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" diff --git a/examples/rust-peripheral/Cargo.toml b/examples/rust-peripheral/Cargo.toml new file mode 100644 index 0000000..74fdd53 --- /dev/null +++ b/examples/rust-peripheral/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "example-peripheral" +version = "0.1.0" +edition = "2021" + +[lib] +name = "rust_peripheral" +crate-type = ["rlib", "cdylib"] + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +byte-strings = "0.3.1" diff --git a/examples/rust-peripheral/src/lib.rs b/examples/rust-peripheral/src/lib.rs new file mode 100644 index 0000000..071db64 --- /dev/null +++ b/examples/rust-peripheral/src/lib.rs @@ -0,0 +1,29 @@ +use byte_strings::c_str; + +static mut STATE: u8 = 0x00; + +// Will be called on library load +#[no_mangle] +pub extern "C" fn init() { + println!("Rust init"); +} + +// Will be called whenever the attached port is read from. +#[no_mangle] +pub unsafe extern "C" fn read() -> u8 { + println!("Rust read"); + STATE +} + +// Will be called whenever the attached port is written to. +#[no_mangle] +pub unsafe extern "C" fn write(data: u8) { + println!("Rust example write"); + STATE = data; +} + +// Will be called whenever the attached port is written to. +#[no_mangle] +pub extern "C" fn name() -> *const i8 { + c_str!("Rust Example").as_ptr() +}