Skip to content

Commit

Permalink
Add example peripherals
Browse files Browse the repository at this point in the history
  • Loading branch information
commonkestrel committed Nov 19, 2023
1 parent ee83f62 commit f825eac
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 1 deletion.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
/target
target
/.vscode
*.dll
*.so
*.dylib
23 changes: 23 additions & 0 deletions examples/c-peripheral/peripheral.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#include "peripheral.h"
#include<stdio.h>
#include <stdint.h>

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";
}
6 changes: 6 additions & 0 deletions examples/c-peripheral/peripheral.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#include <stdint.h>

__declspec(dllexport) void init(void);
__declspec(dllexport) uint8_t read(void);
__declspec(dllexport) void write(uint8_t data);
__declspec(dllexport) const char* name(void);
65 changes: 65 additions & 0 deletions examples/rust-peripheral/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions examples/rust-peripheral/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
29 changes: 29 additions & 0 deletions examples/rust-peripheral/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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()
}

0 comments on commit f825eac

Please sign in to comment.