I am working on an embedded project.
My code is written in Rust, but it needs to link with some C code to create the final executable.
The project is read some serialized data, and operates the hardware registers accordingly.
The project supports two RTOS, and multiple boards, so crates that depends on hardware or RTOS features need to have cargo features to differ RTOS or board.
Let's say all crates in the project prefixed with the name "foo":
Current stucture:
foo_version: Store static data of git version info
foo_support: General code for not OS or hardware related
foo_header: Bindgen of C header of OS, contain feature to different OS
foo_math: API for math
foo_parser: Parser for the serialized data
foo_pac: Peripheral Access Crate, for hardware register definition, contain features for different board.
foo_hal: Hardware abstract Layer, depend on foo_pac
foo_log: Extra api for log
crate
foo_rtos: Code that OS dependent, but not depend on hardware
foo_collections: Extra container-like structs, currently depend on foo_rtos
, but I am not sure if this good.
foo_log_impl: Register logger, depend on foo_rtos and foo_log
foo_panic: Implement #[panic_handler]
, depend on foo_rtos,
foo_galloc: Implement #[global_allocator]
, depend on foo_rtos.
foo_sched: Do the main loop, depend on most other crates
foo_init: Depend on most crates above, to call init functions for global variable
foo_clib: Depend on all crates above, to create a staticlib
Question:
- How to organize crates, so that I can run as many unit tests in hosted x86_64 environment as possible?
- Should my
foo_collections
crate depends on any OS related behavior? - Synchronize primitives, such as semphoare,mutex, are OS dependent.
Some of my container-like struct needs them. Which crate should I put those container.
Should I create a trait forSemaphore
, where the trait does not depend on OS feature. - What features should be inside a
hal
crate? Should api for cpu, such as invalidate icache, in that crate, or separate crate? - How to transform
#[test]
code to stuffs that is runable on real board? - I am not sure the scope of
foo_support
crate. It currently contains code for const_assert, spinlock,Display
trait impl for time, macro for convenient, etc. Many stuffs that is not OS or hardware related. Is it better to split the crate? - Any other general suggestions?