Rust 编译器对中断的支持

标签 rust interrupt bare-metal

我正在用 Rust 为 AMR 板编写裸机应用程序,其中涉及中断服务例程。目前,我将 #naked 函数与我自己的汇编程序 prolog/epilog 一起使用。但是,我想知道是否有我错过的更好(并且希望更便携)的方式,也许是 Rust nightly 中的类似 #interrupt 的属性或任何其他编译器支持。我认为与 GCC 的 __attribute__ ((interrupt ("IRQ"))) 一致,因为 Rust 的后端 LLVM 提供了这样一个属性。

最佳答案

中断只是另一种调用约定。对于 Rust 的 AVR 端口,我们添加了两种新的调用约定,一种用于 AVR 支持的每种中断。

调用约定的权威列表is the source code . Rust 1.16 列出了这些:

#[derive(PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Clone, Copy, Debug)]
pub enum Abi {
    // NB: This ordering MUST match the AbiDatas array below.
    // (This is ensured by the test indices_are_correct().)

    // Single platform ABIs
    Cdecl,
    Stdcall,
    Fastcall,
    Vectorcall,
    Aapcs,
    Win64,
    SysV64,
    PtxKernel,
    Msp430Interrupt,

    // Multiplatform / generic ABIs
    Rust,
    C,
    System,
    RustIntrinsic,
    RustCall,
    PlatformIntrinsic,
    Unadjusted
}

unstable book also mentions that the different calling conventions exist .

要使用它们,您需要用它声明您的函数:

#![feature(abi_msp430_interrupt)] 

extern "msp430-interrupt" fn handler() {}

将函数注册为中断向量表(或等效项)的异常处理程序仍然取决于您。

当然,您可能需要提交一个 PR,通知 Rust 前端要使用的特定 LLVM 调用约定,如果您的调用约定不在此列表中的话。

关于Rust 编译器对中断的支持,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43389196/

相关文章:

c++ - 在c++中使用21h中断的9个函数

java替代Thread.stop()中断特定调用

windows - 用MSVC工具生成裸机二进制文件的过程是什么?

rust - 如何使用可选的内部标签反序列化枚举?

rust - 你如何编译一个 Rust 库来定位 asm.js?

java - 关于ExecutorService的shutdownNow

用于平衡分配/释放的 C 模式/习惯用法

sockets - 如何路由QEMU内部的TCP端口/套接字?

rust - 为什么我会收到错误 "cannot borrow x as mutable more than once"?

multithreading - Rust 标准库中的线程局部变量是如何工作的?