我正在为恩智浦的LPC82X系列控制器开发嵌入式Rust代码 - 确切的工具链对于这个问题无关紧要.
这些控制器包含ROM中的外设驱动程序 我想使用这些驱动程序,这意味着我需要使用不安全的Rust和FFI而不链接实际代码.
ROM API将函数指针暴露在特定地址位置的C结构中.如果有人想要这个API的细节,LPC82X手册的第29章描述了有问题的API.
我的Rust操场虚拟草图看起来像这样,通过一个尚未写入的I2C抽象lib从应用程序代码中隐藏.这编译.
#![feature(naked_functions)]
const I2C_ROM_API_ADDRESS: usize = 0x1fff_200c;
static mut ROM_I2C_API: Option<&RomI2cApi> = None;
#[repr(C)]
struct RomI2cApi {
// Dummy functions, real ones take arguments, and have different return
// These won't be called directly, only through the struct's implemented methods
// value
master_transmit_poll: extern "C" fn() -> bool,
master_receive_poll: extern "C" fn() -> bool,
}
impl RomI2cApi {
fn api_table() -> &'static RomI2cApi {
unsafe {
match ROM_I2C_API {
None => …Run Code Online (Sandbox Code Playgroud)