Add library with Rust bindings to target_f1 code (#68)

* Move flipper-core into a workspace

* Fix target build rules

* Add flipper-f1-sys library

* Add flipper-f1-sys dependency to flipper-core

* Remove apparently useless includes

* Build and export HAL statics

* Disable Rust dependency detection for target_f1 build

* Install libclang-10-dev in docker

* Build Rust libs every time

* remove duplicate sources from make

* clean build different example

* wip add example fn

* Implement rust_uart_write()

* fix rebuild instructions for target_f1

Co-authored-by: aanper <mail@s3f.ru>
This commit is contained in:
Vadim Kaushan
2020-08-31 22:33:38 +03:00
committed by GitHub
parent 1b82b3d3b6
commit b13925f7ab
18 changed files with 630 additions and 46 deletions

3
core-rs/flipper-core/.gitignore vendored Normal file
View File

@@ -0,0 +1,3 @@
/bindings
/target
/Cargo.lock

View File

@@ -0,0 +1,23 @@
[package]
name = "flipper-core"
version = "0.1.0"
authors = ["Vadim Kaushan <admin@disasm.info>"]
edition = "2018"
[lib]
crate-type = ["staticlib"]
[target.'cfg(target_arch = "arm")'.dependencies]
flipper-f1-sys = { path = "../flipper-f1-sys" }
[build-dependencies]
cbindgen = "0.14"
[profile.release]
codegen-units = 1 # better optimizations
debug = true # symbols are nice and they don't increase the size on Flash
lto = true # better optimizations
panic = "abort"
[profile.dev]
panic = "abort"

View File

@@ -0,0 +1,13 @@
use std::env;
use std::path::Path;
fn main() {
let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
let pkg_name = env::var("CARGO_PKG_NAME").unwrap();
cbindgen::generate(&crate_dir)
.expect("Unable to generate cbindgen bindings")
.write_to_file(
Path::new(&crate_dir).join("bindings").join(format!("{}.h", pkg_name))
);
}

View File

@@ -0,0 +1,4 @@
language = "C"
[export]
item_types = ["functions"]

View File

@@ -0,0 +1,39 @@
#![no_std]
#[cfg(target_arch = "arm")]
use flipper_f1_sys::hal::{HAL_UART_Transmit_IT, huart1};
#[no_mangle]
pub extern "C" fn add(a: u32, b: u32) -> u32 {
a + b
}
#[no_mangle]
pub extern "C" fn rust_uart_write() {
let string = "Rust test string\n";
let bytes = string.as_bytes();
#[cfg(target_arch = "arm")]
unsafe {
HAL_UART_Transmit_IT(&mut huart1, bytes.as_ptr() as *mut _, bytes.len() as u16);
}
#[cfg(not(target_arch = "arm"))]
unsafe {
extern "C" {
fn write(handle: i32, ptr: *const u8, size: usize) -> isize;
}
write(1, bytes.as_ptr(), bytes.len());
}
}
mod aux {
use core::panic::PanicInfo;
#[panic_handler]
fn panic(_info: &PanicInfo) -> ! {
loop { continue }
}
}