Просмотр исходного кода

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>
Vadim Kaushan 5 лет назад
Родитель
Сommit
b13925f7ab

+ 1 - 1
Dockerfile

@@ -17,7 +17,7 @@ RUN apt-get update && \
         python \
         python-pip \
         libstdc++-arm-none-eabi-newlib \
-        curl && \
+        libclang-10-dev && \
     apt-get clean && rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*
 
 RUN curl https://sh.rustup.rs -sSf | sh -s -- -y --profile=minimal --target thumbv7em-none-eabi thumbv7em-none-eabihf

+ 3 - 0
applications/tests/test_index.c

@@ -64,5 +64,8 @@ void flipper_test_app(void* p) {
         fuprintf(log, "[TEST] Rust add FAILED\n");
     }
 
+    rust_uart_write();
+
+
     furiac_exit(NULL);
 }

+ 0 - 1
core-rs/.gitignore

@@ -1,3 +1,2 @@
-/bindings
 /target
 /.idea

+ 211 - 0
core-rs/Cargo.lock

@@ -1,5 +1,14 @@
 # This file is automatically @generated by Cargo.
 # It is not intended for manual editing.
+[[package]]
+name = "aho-corasick"
+version = "0.7.13"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "043164d8ba5c4c3035fec9bbee8647c0261d788f3474306f93bb65901cae0e86"
+dependencies = [
+ "memchr",
+]
+
 [[package]]
 name = "ansi_term"
 version = "0.11.0"
@@ -20,6 +29,30 @@ dependencies = [
  "winapi",
 ]
 
+[[package]]
+name = "bindgen"
+version = "0.55.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "75b13ce559e6433d360c26305643803cb52cfbabbc2b9c47ce04a58493dfb443"
+dependencies = [
+ "bitflags",
+ "cexpr",
+ "cfg-if",
+ "clang-sys",
+ "clap",
+ "env_logger",
+ "lazy_static",
+ "lazycell",
+ "log",
+ "peeking_take_while",
+ "proc-macro2",
+ "quote",
+ "regex",
+ "rustc-hash",
+ "shlex",
+ "which",
+]
+
 [[package]]
 name = "bitflags"
 version = "1.2.1"
@@ -44,12 +77,32 @@ dependencies = [
  "toml",
 ]
 
+[[package]]
+name = "cexpr"
+version = "0.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f4aedb84272dbe89af497cf81375129abda4fc0a9e7c5d317498c15cc30c0d27"
+dependencies = [
+ "nom",
+]
+
 [[package]]
 name = "cfg-if"
 version = "0.1.10"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "4785bdd1c96b2a846b2bd7cc02e86b6b3dbf14e7e53446c4f54c92a361040822"
 
+[[package]]
+name = "clang-sys"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9da1484c6a890e374ca5086062d4847e0a2c1e5eba9afa5d48c09e8eb39b2519"
+dependencies = [
+ "glob",
+ "libc",
+ "libloading",
+]
+
 [[package]]
 name = "clap"
 version = "2.33.3"
@@ -65,11 +118,32 @@ dependencies = [
  "vec_map",
 ]
 
+[[package]]
+name = "env_logger"
+version = "0.7.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "44533bbbb3bb3c1fa17d9f2e4e38bbbaf8396ba82193c4cb1b6445d711445d36"
+dependencies = [
+ "atty",
+ "humantime",
+ "log",
+ "regex",
+ "termcolor",
+]
+
 [[package]]
 name = "flipper-core"
 version = "0.1.0"
 dependencies = [
  "cbindgen",
+ "flipper-f1-sys",
+]
+
+[[package]]
+name = "flipper-f1-sys"
+version = "0.1.0"
+dependencies = [
+ "bindgen",
 ]
 
 [[package]]
@@ -83,6 +157,12 @@ dependencies = [
  "wasi",
 ]
 
+[[package]]
+name = "glob"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9b919933a397b79c37e33b77bb2aa3dc8eb6e165ad809e58ff75bc7db2e34574"
+
 [[package]]
 name = "heck"
 version = "0.3.1"
@@ -101,18 +181,49 @@ dependencies = [
  "libc",
 ]
 
+[[package]]
+name = "humantime"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "df004cfca50ef23c36850aaaa59ad52cc70d0e90243c3c7737a4dd32dc7a3c4f"
+dependencies = [
+ "quick-error",
+]
+
 [[package]]
 name = "itoa"
 version = "0.4.6"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "dc6f3ad7b9d11a0c00842ff8de1b60ee58661048eb8049ed33c73594f359d7e6"
 
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
+[[package]]
+name = "lazycell"
+version = "1.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "830d08ce1d1d941e6b30645f1a0eb5643013d835ce3779a5fc208261dbe10f55"
+
 [[package]]
 name = "libc"
 version = "0.2.76"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "755456fae044e6fa1ebbbd1b3e902ae19e73097ed4ed87bb79934a867c007bc3"
 
+[[package]]
+name = "libloading"
+version = "0.6.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2443d8f0478b16759158b2f66d525991a05491138bc05814ef52a250148ef4f9"
+dependencies = [
+ "cfg-if",
+ "winapi",
+]
+
 [[package]]
 name = "log"
 version = "0.4.11"
@@ -122,6 +233,28 @@ dependencies = [
  "cfg-if",
 ]
 
+[[package]]
+name = "memchr"
+version = "2.3.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3728d817d99e5ac407411fa471ff9800a778d88a24685968b36824eaf4bee400"
+
+[[package]]
+name = "nom"
+version = "5.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ffb4262d26ed83a1c0a33a38fe2bb15797329c85770da05e6b828ddb782627af"
+dependencies = [
+ "memchr",
+ "version_check",
+]
+
+[[package]]
+name = "peeking_take_while"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "19b17cddbe7ec3f8bc800887bab5e717348c95ea2ca0b1bf0837fb964dc67099"
+
 [[package]]
 name = "ppv-lite86"
 version = "0.2.9"
@@ -137,6 +270,12 @@ dependencies = [
  "unicode-xid",
 ]
 
+[[package]]
+name = "quick-error"
+version = "1.2.3"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0"
+
 [[package]]
 name = "quote"
 version = "1.0.7"
@@ -193,6 +332,24 @@ version = "0.1.57"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "41cc0f7e4d5d4544e8861606a285bb08d3e70712ccc7d2b84d7c0ccfaf4b05ce"
 
+[[package]]
+name = "regex"
+version = "1.3.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c3780fcf44b193bc4d09f36d2a3c87b251da4a046c87795a0d35f4f927ad8e6"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+ "thread_local",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.18"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "26412eb97c6b088a6997e05f69403a802a92d520de2f8e63c2b65f9e0f47c4e8"
+
 [[package]]
 name = "remove_dir_all"
 version = "0.5.3"
@@ -202,6 +359,12 @@ dependencies = [
  "winapi",
 ]
 
+[[package]]
+name = "rustc-hash"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "08d43f7aa6b08d49f382cde6a7982047c3426db949b1424bc4b7ec9ae12c6ce2"
+
 [[package]]
 name = "ryu"
 version = "1.0.5"
@@ -239,6 +402,12 @@ dependencies = [
  "serde",
 ]
 
+[[package]]
+name = "shlex"
+version = "0.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7fdf1b9db47230893d76faad238fd6097fd6d6a9245cd7a4d90dbd639536bbd2"
+
 [[package]]
 name = "strsim"
 version = "0.8.0"
@@ -270,6 +439,15 @@ dependencies = [
  "winapi",
 ]
 
+[[package]]
+name = "termcolor"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "bb6bfa289a4d7c5766392812c0a1f4c1ba45afa1ad47803c11e1f407d846d75f"
+dependencies = [
+ "winapi-util",
+]
+
 [[package]]
 name = "textwrap"
 version = "0.11.0"
@@ -279,6 +457,15 @@ dependencies = [
  "unicode-width",
 ]
 
+[[package]]
+name = "thread_local"
+version = "1.0.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d40c6d1b69745a6ec6fb1ca717914848da4b44ae29d9b3080cbee91d72a69b14"
+dependencies = [
+ "lazy_static",
+]
+
 [[package]]
 name = "toml"
 version = "0.5.6"
@@ -312,12 +499,27 @@ version = "0.8.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
 
+[[package]]
+name = "version_check"
+version = "0.9.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed"
+
 [[package]]
 name = "wasi"
 version = "0.9.0+wasi-snapshot-preview1"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "cccddf32554fecc6acb585f82a32a72e28b48f8c4c1883ddfeeeaa96f7d8e519"
 
+[[package]]
+name = "which"
+version = "3.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d011071ae14a2f6671d0b74080ae0cd8ebf3a6f8c9589a2cd45f23126fe29724"
+dependencies = [
+ "libc",
+]
+
 [[package]]
 name = "winapi"
 version = "0.3.9"
@@ -334,6 +536,15 @@ version = "0.4.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
 
+[[package]]
+name = "winapi-util"
+version = "0.1.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
+dependencies = [
+ "winapi",
+]
+
 [[package]]
 name = "winapi-x86_64-pc-windows-gnu"
 version = "0.4.0"

+ 5 - 11
core-rs/Cargo.toml

@@ -1,14 +1,8 @@
-[package]
-name = "flipper-core"
-version = "0.1.0"
-authors = ["Vadim Kaushan <admin@disasm.info>"]
-edition = "2018"
-
-[lib]
-crate-type = ["staticlib"]
-
-[build-dependencies]
-cbindgen = "0.14"
+[workspace]
+members = [
+    "flipper-core",
+    "flipper-f1-sys",
+]
 
 [profile.release]
 codegen-units = 1 # better optimizations

+ 3 - 0
core-rs/flipper-core/.gitignore

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

+ 23 - 0
core-rs/flipper-core/Cargo.toml

@@ -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"

+ 0 - 0
core-rs/build.rs → core-rs/flipper-core/build.rs


+ 0 - 0
core-rs/cbindgen.toml → core-rs/flipper-core/cbindgen.toml


+ 39 - 0
core-rs/flipper-core/src/lib.rs

@@ -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 }
+    }
+}

+ 2 - 0
core-rs/flipper-f1-sys/.gitignore

@@ -0,0 +1,2 @@
+/target
+/Cargo.lock

+ 12 - 0
core-rs/flipper-f1-sys/Cargo.toml

@@ -0,0 +1,12 @@
+[package]
+name = "flipper-f1-sys"
+version = "0.1.0"
+authors = ["Vadim Kaushan <admin@disasm.info>"]
+edition = "2018"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+
+[build-dependencies]
+bindgen = "0.55"

+ 293 - 0
core-rs/flipper-f1-sys/build.rs

@@ -0,0 +1,293 @@
+use std::env;
+use std::fs;
+use std::path::{Path, PathBuf};
+
+fn main() {
+    println!("cargo:rerun-if-env-changed=FORCE_BINDGEN");
+
+    let generator = BindingsGenerator::new();
+    generator.generate_cmsis_os_bindings();
+    generator.generate_stm32_hal_bindings();
+    generator.generate_stm32_hal_statics();
+}
+
+struct BindingsGenerator {
+    clib_dir: PathBuf,
+    workspace_dir: PathBuf,
+    out_dir: PathBuf,
+    gcc_include_dir: PathBuf,
+    force_bindgen: bool,
+}
+
+impl BindingsGenerator {
+    pub fn new() -> Self {
+        let out_dir = env::var("OUT_DIR").unwrap();
+
+        let crate_dir = env::var("CARGO_MANIFEST_DIR").unwrap();
+        let workspace_dir = Path::new(&crate_dir).parent().unwrap();
+        let clib_dir = workspace_dir.parent().unwrap().join("target_f1");
+        assert!(clib_dir.is_dir());
+
+        let force_bindgen: bool = std::env::var_os("FORCE_BINDGEN").is_some();
+
+        let gcc_include_dir = detect_gcc_inclide_dir();
+
+        Self {
+            clib_dir: clib_dir.to_path_buf(),
+            workspace_dir: workspace_dir.to_path_buf(),
+            out_dir: Path::new(&out_dir).to_path_buf(),
+            gcc_include_dir,
+            force_bindgen,
+        }
+    }
+
+    fn builder(&self) -> bindgen::Builder {
+        let stm32_sdk_includes = [
+            "Inc",
+            "Drivers/STM32L4xx_HAL_Driver/Inc",
+            "Drivers/STM32L4xx_HAL_Driver/Inc/Legacy",
+            "Middlewares/Third_Party/FreeRTOS/Source/include",
+            "Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS",
+            "Middlewares/Third_Party/FreeRTOS/Source/portable/GCC/ARM_CM4F",
+            "Drivers/CMSIS/Device/ST/STM32L4xx/Include",
+            "Drivers/CMSIS/Include",
+            "Middlewares/ST/STM32_USB_Device_Library/Core/Inc",
+            "Middlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc",
+        ];
+    
+        let stm32_sdk_includes = stm32_sdk_includes
+            .iter()
+            .map(|stm32_include| format!("{}/{}", self.clib_dir.to_string_lossy(), stm32_include));
+
+        let flipper_core_bindings = self.workspace_dir.join("flipper-core").join("bindings");
+
+        let includes = [
+            // This are bindings generated by cbindgen nearby
+            &flipper_core_bindings.to_string_lossy(),
+
+            &self.gcc_include_dir.to_string_lossy(),
+        ];
+
+        #[rustfmt::skip]
+        return bindgen::Builder::default()
+            .use_core()
+    
+            .ctypes_prefix("self")
+            .blacklist_type("__uint8_t")
+            .blacklist_type("__uint32_t")
+            .blacklist_type("c_int")
+            .blacklist_type("__int32_t")
+    
+            // TODO there's no .no_debug method, to disable only for specific type
+            .derive_debug(false)
+    
+            .clang_arg("-DUSE_HAL_DRIVER")
+            .clang_arg("-DSTM32L476xx")
+            .clang_arg("-DBUTON_INVERT=false")
+            .clang_arg("-DDEBUG_UART=huart1")
+    
+            .clang_args(
+                (includes.iter().map(|x| From::from(x as &str)).chain(stm32_sdk_includes))
+                    .map(|include| format!("-I{}", include))
+            )
+            .clang_arg("--target=thumbv7em-none-eabihf")
+            .clang_arg("--verbose")
+            //.clang_arg("-nostdinc")
+        ;
+    }
+
+    pub fn generate_cmsis_os_bindings(&self) {
+        let result_path = self.out_dir.join("cmsis_os_bindings.rs");
+        let should_build = !result_path.is_file() || self.force_bindgen;
+        if !should_build {
+            return;
+        }
+
+        println!("cargo:warning=writing cmsis os bindings");
+
+        #[rustfmt::skip]
+        let builder = self.builder()
+            .whitelist_recursively(false)
+
+            .header(format!("{}/Middlewares/Third_Party/FreeRTOS/Source/CMSIS_RTOS/cmsis_os.h", self.clib_dir.to_string_lossy()))
+
+            .whitelist_type("osStatus")
+            .rustified_enum("osStatus")
+
+            .whitelist_type("osEvent")
+
+            .whitelist_type("os_pthread")
+            .whitelist_type("osThreadId")
+            .opaque_type("osThreadId")
+            .whitelist_type("os_thread_def")
+            .whitelist_type("osThreadDef_t")
+            .whitelist_type("osStaticThreadDef_t")
+            .opaque_type("osStaticThreadDef_t")
+            .whitelist_type("osPriority")
+            .rustified_enum("osPriority")
+            .whitelist_function("osThreadCreate")
+            .whitelist_function("osThreadGetId")
+            .whitelist_function("osThreadTerminate")
+            .whitelist_function("osThreadYield")
+
+            .whitelist_type("osMutexId")
+            .opaque_type("osMutexId")
+            .whitelist_type("os_mutex_def")
+            .whitelist_type("osMutexDef_t")
+            .whitelist_type("osStaticMutexDef_t")
+            .opaque_type("osStaticMutexDef_t")
+            .whitelist_function("osMutexCreate")
+            .whitelist_function("osMutexWait")
+            .whitelist_function("osMutexRelease")
+
+            .whitelist_type("osSemaphoreId")
+            .opaque_type("osSemaphoreId")
+            .whitelist_type("os_semaphore_def")
+            .whitelist_type("osSemaphoreDef_t")
+            .whitelist_type("osStaticSemaphoreDef_t")
+            .opaque_type("osStaticSemaphoreDef_t")
+            .whitelist_function("osSemaphoreCreate")
+            .whitelist_function("osSemaphoreWait")
+            .whitelist_function("osSemaphoreRelease")
+
+            .whitelist_type("osMessageQId")
+            .opaque_type("osMessageQId")
+
+            .whitelist_type("osMailQId")
+            .opaque_type("osMailQId")
+            .whitelist_type("os_mailQ_def")
+            .whitelist_type("osMailQDef_t")
+            .whitelist_type("os_mailQ_cb")
+            .whitelist_function("osMailCreate")
+            .whitelist_function("osMailAlloc")
+            .whitelist_function("osMailFree")
+            .whitelist_function("osMailPut")
+            .whitelist_function("osMailGet")
+
+            .whitelist_var("osWaitForever")
+            .whitelist_function("osDelay")
+
+            // TODO for some reason, bindgen wont generate osKernelSysTickFrequency
+            .whitelist_var("osKernelSysTickFrequency")
+            .whitelist_function("osKernelSysTick")
+        ;
+
+        let bindings = builder.generate().expect("Unable to generate bindings");
+
+        bindings
+            .write_to_file(result_path)
+            .expect("Couldn't write bindings!");
+    }
+
+    pub fn generate_stm32_hal_bindings(&self) {
+        let result_path = self.out_dir.join("stm32_hal_bindings.rs");
+        let should_build = !result_path.is_file() || self.force_bindgen;
+        if !should_build {
+            return;
+        }
+
+        println!("cargo:warning=writing STM32 HAL bindings");
+
+        #[rustfmt::skip]
+        let builder = self.builder()
+            .whitelist_recursively(false)
+
+            .header(format!("{}/Drivers/STM32L4xx_HAL_Driver/Inc/stm32l4xx_hal.h", self.clib_dir.to_string_lossy()))
+
+            .whitelist_type("HAL_StatusTypeDef")
+            .rustified_enum("HAL_StatusTypeDef")
+
+            .whitelist_type("GPIO_TypeDef")
+            .opaque_type("GPIO_TypeDef")
+            .whitelist_type("GPIO_PinState")
+            .rustified_enum("GPIO_PinState")
+            .whitelist_function("HAL_GPIO_WritePin")
+            .whitelist_function("HAL_GPIO_TogglePin")
+            .whitelist_function("HAL_GPIO_ReadPin")
+
+            .whitelist_type("UART_HandleTypeDef")
+            .opaque_type("UART_HandleTypeDef")
+            .whitelist_function("HAL_UART_Transmit_IT")
+
+            .whitelist_type("SPI_HandleTypeDef")
+            .opaque_type("SPI_HandleTypeDef")
+            .whitelist_function("HAL_SPI_Transmit_IT")
+            .whitelist_function("HAL_SPI_Receive_IT")
+            .whitelist_function("HAL_SPI_TransmitReceive_IT")
+
+            .whitelist_type("ADC_HandleTypeDef")
+            .opaque_type("ADC_HandleTypeDef")
+
+            .whitelist_type("COMP_HandleTypeDef")
+            .opaque_type("COMP_HandleTypeDef")
+
+            .whitelist_type("DAC_HandleTypeDef")
+            .opaque_type("DAC_HandleTypeDef")
+
+            .whitelist_type("TIM_HandleTypeDef")
+            .opaque_type("TIM_HandleTypeDef")
+        ;
+
+        let bindings = builder.generate().expect("Unable to generate bindings");
+
+        bindings
+            .write_to_file(result_path)
+            .expect("Couldn't write bindings!");
+    }
+
+    pub fn generate_stm32_hal_statics(&self) {
+        let result_path = self.out_dir.join("stm32_hal_statics.rs");
+        let should_build = !result_path.is_file() || self.force_bindgen;
+        if !should_build {
+            return;
+        }
+
+        println!("cargo:warning=writing STM32 HAL bindings");
+
+        #[rustfmt::skip]
+        let builder = self.builder()
+            .whitelist_recursively(false)
+
+            .header(format!("{}/Src/main.c", self.clib_dir.to_string_lossy()))
+
+            .whitelist_var(".*_Pin")
+            .whitelist_var(".*_Port")
+
+            .whitelist_var("HAL_.*_Pin")
+            .whitelist_var("HAL_.*_Port")
+            .whitelist_var("hadc[0-9]+")
+            .whitelist_var("hcomp[0-9]+")
+            .whitelist_var("hdac[0-9]+")
+            .whitelist_var("hspi[0-9]+")
+            .whitelist_var("htim[0-9]+")
+            .whitelist_var("huart[0-9]+")
+            ;
+
+        let bindings = builder.generate().expect("Unable to generate bindings");
+
+        bindings
+            .write_to_file(result_path)
+            .expect("Couldn't write bindings!");
+    }    
+}
+
+fn detect_gcc_inclide_dir() -> PathBuf {
+    let base_path = Path::new("/usr/lib/gcc/arm-none-eabi");
+    if !base_path.is_dir() {
+        panic!("Can't find arm-none-eabi-gcc lib directory");
+    }
+
+    let entries = fs::read_dir(base_path).expect("Can't read arm-none-eabi-gcc lib directory");
+
+    for entry in entries {
+        let entry = entry.expect("Can't read dir entry");
+        let path = entry.path();
+        if path.is_dir() {
+            let include_dir = path.join("include");
+            if include_dir.is_dir() {
+                return include_dir.to_path_buf();
+            }
+        }
+    }
+    panic!("Can't find arm-none-eabi-gcc include directory");
+}

+ 20 - 0
core-rs/flipper-f1-sys/src/lib.rs

@@ -0,0 +1,20 @@
+#![no_std]
+
+#![allow(dead_code)]
+#![allow(non_camel_case_types)]
+#![allow(non_upper_case_globals)]
+
+pub mod cmsis_os {
+    #[allow(non_camel_case_types)]
+    pub use core::ffi::c_void;
+
+    #[allow(non_camel_case_types)]
+    pub type c_char = i8;
+
+    include!(concat!(env!("OUT_DIR"), "/cmsis_os_bindings.rs"));
+}
+
+pub mod hal {
+    include!(concat!(env!("OUT_DIR"), "/stm32_hal_bindings.rs"));
+    include!(concat!(env!("OUT_DIR"), "/stm32_hal_statics.rs"));
+}

+ 0 - 16
core-rs/src/lib.rs

@@ -1,16 +0,0 @@
-#![no_std]
-
-#[no_mangle]
-pub extern "C" fn add(a: u32, b: u32) -> u32 {
-    a + b
-}
-
-
-mod aux {
-    use core::panic::PanicInfo;
-
-    #[panic_handler]
-    fn panic(_info: &PanicInfo) -> ! {
-        loop { continue }
-    }
-}

+ 0 - 3
target_f1/Inc/usbd_conf.h

@@ -28,9 +28,6 @@
 #endif
 
 /* Includes ------------------------------------------------------------------*/
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
 #include "main.h"
 #include "stm32l4xx.h"
 #include "stm32l4xx_hal.h"

+ 11 - 9
target_f1/Makefile

@@ -141,10 +141,6 @@ endif
 
 # Add C_SOURCES +=, C_DEFS += or CPP_SOURCES += here
 
-C_SOURCES += ../applications/tests/furiac_test.c
-C_SOURCES += ../applications/tests/furi_record_test.c
-C_SOURCES += ../applications/tests/test_index.c
-
 #######################################
 # binaries
 #######################################
@@ -184,7 +180,7 @@ else
     RUST_LIB_PATH = $(RUST_LIB_SRC)/target/$(RUST_LIB_TARGET)/release
 endif
 
-RUST_LIB_CMD = cd $(RUST_LIB_SRC) && cargo build $(RUST_LIB_FLAGS)
+RUST_LIB_CMD = cd $(RUST_LIB_SRC) && cargo build -p flipper-core $(RUST_LIB_FLAGS)
 
 #######################################
 # CFLAGS
@@ -224,7 +220,7 @@ C_INCLUDES =  \
 -IDrivers/CMSIS/Include \
 -IMiddlewares/ST/STM32_USB_Device_Library/Core/Inc \
 -IMiddlewares/ST/STM32_USB_Device_Library/Class/CDC/Inc \
--I../core-rs/bindings
+-I../core-rs/flipper-core/bindings
 
 
 # compile gcc flags
@@ -256,14 +252,22 @@ LDFLAGS = $(MCU) -specs=nano.specs -specs=nosys.specs -T$(LDSCRIPT) $(LIBDIR) $(
 # default action: build all
 all: $(BUILD_DIR)/$(TARGET).elf $(BUILD_DIR)/$(TARGET).hex $(BUILD_DIR)/$(TARGET).bin
 
+rust_lib:
+	$(RUST_LIB_CMD)
+
 example_blink:
 	EXAMPLE_BLINK=1 make
+	rm $(BUILD_DIR)/app.o
 
 example_uart_write:
 	EXAMPLE_UART_WRITE=1 make
+	rm $(BUILD_DIR)/app.o
 
 test:
 	TEST=1 make
+	rm $(BUILD_DIR)/app.o
+
+.PHONY: all rust_lib example_blink example_uart_write test
 
 #######################################
 # build the application
@@ -292,8 +296,7 @@ $(BUILD_DIR)/%.o: %.s Makefile | $(BUILD_DIR)
 $(BUILD_DIR)/%.pb.c $(BUILD_DIR)/%.pb.h: ../flipper_proto/%.proto
 	$(PROTOC) $(PROTOC_OPTS) --nanopb_out=$(BUILD_DIR) $<
 
-$(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a:
-	$(RUST_LIB_CMD)
+$(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a: rust_lib
 
 $(BUILD_DIR)/$(TARGET).elf: $(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a $(OBJECTS) Makefile
 	$(CPP) $(OBJECTS) $(LDFLAGS) -o $@
@@ -319,6 +322,5 @@ clean:
 # dependencies
 #######################################
 -include $(wildcard $(BUILD_DIR)/*.d)
--include $(wildcard $(RUST_LIB_PATH)/*.d)
 
 # *** EOF ***

+ 7 - 5
target_lo/Makefile

@@ -96,7 +96,7 @@ else
     RUST_LIB_PATH = $(RUST_LIB_SRC)/target/$(RUST_LIB_TARGET)/release
 endif
 
-RUST_LIB_CMD = cd $(RUST_LIB_SRC) && cargo build $(RUST_LIB_FLAGS)
+RUST_LIB_CMD = cd $(RUST_LIB_SRC) && cargo build -p flipper-core $(RUST_LIB_FLAGS)
 
 #######################################
 # CFLAGS
@@ -108,7 +108,7 @@ C_INCLUDES =  \
 -IInc \
 -I../applications \
 -I../core \
--I../core-rs/bindings
+-I../core-rs/flipper-core/bindings
 
 # compile gcc flags
 CFLAGS = $(C_DEFS) $(C_INCLUDES) $(OPT) -Wall -fdata-sections -ffunction-sections -pthread
@@ -135,6 +135,9 @@ LDFLAGS = $(LIBDIR) $(LIBS) -Wl,-Map=$(BUILD_DIR)/$(TARGET).map,--cref -Wl,--gc-
 # default action: build all
 all: $(BUILD_DIR)/$(TARGET)
 
+rust_lib:
+	$(RUST_LIB_CMD)
+
 example_blink:
 	EXAMPLE_BLINK=1 make
 	rm $(BUILD_DIR)/app.o
@@ -151,6 +154,7 @@ test:
 	rm $(BUILD_DIR)/app.o
 	$(BUILD_DIR)/$(TARGET)
 
+.PHONY: all rust_lib example_blink example_uart_write test
 
 #######################################
 # build the application
@@ -168,8 +172,7 @@ $(BUILD_DIR)/%.o: %.c Makefile | $(BUILD_DIR)
 $(BUILD_DIR)/%.o: %.cpp Makefile | $(BUILD_DIR)
 	$(CPP) -c $(CFLAGS) $(CPPFLAGS) -Wa,-a,-ad,-alms=$(BUILD_DIR)/$(notdir $(<:.c=.lst)) $< -o $@
 
-$(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a:
-	$(RUST_LIB_CMD)
+$(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a: rust_lib
 
 $(BUILD_DIR)/$(TARGET): $(RUST_LIB_PATH)/lib$(RUST_LIB_NAME).a $(OBJECTS) Makefile
 	$(CPP) $(OBJECTS) $(LDFLAGS) -o $@
@@ -189,6 +192,5 @@ clean:
 # dependencies
 #######################################
 -include $(wildcard $(BUILD_DIR)/*.d)
--include $(wildcard $(RUST_LIB_PATH)/*.d)
 
 # *** EOF ***