为什么这个函数在声明之前没有主体被调用,“2”到底在做什么?

Why is this function called with no body before being declared, and what exactly is the '2' doing?

void on_low_interrupt(void) __interrupt 2;    

void on_low_interrupt(void) __interrupt 2
{
#ifdef CONFIG_ENABLE_I2C
  i2c_handle_interrupt();
#endif

#ifdef CONFIG_ENABLE_SERIAL
  serial_handle_interrupt();
#endif
}

我正在调整一些代码以适应 PIC18F47Q10 上处于从属模式的 运行 I2C。
该代码是为另一个类似的 PIC 编写的,但它确实需要一些调整。
这是原始代码:https://github.com/texane/pic18f_i2c

我不明白定义之前的调用,我也不明白'2'是什么意思。
这是来自原始文件中发布的 int.c 文件。感谢任何帮助或解释。
现在我正在评论所有这些,希望没有它它也能正常工作。

看到的错误是:

"unexpected token: __interrupt

Unable to resolve identifier on_low_interrupt."

"error: expected function body after function declarator"

在“C”中需要区分函数声明和函数定义。

函数声明基本上告诉编译器某处存在一个具有特定名称、参数类型和结果类型的函数,因此编译器可能会从 type-safety 的角度检查此函数的调用是否正确,并且可以生成适当的函数调用代码。声明是可选的,可以调用未声明的函数,从而使编译器猜测有关被调用函数的详细信息,但是现在这被认为是不好的做法,很可能会导致警告。

函数声明通常位于头文件 (.h) 中。在您的代码函数声明中是:

void on_low_interrupt(void) __interrupt 2;

函数定义告诉编译器函数实际做什么,即包含函数的代码。在你的代码函数定义中是:

void on_low_interrupt(void) __interrupt 2
{
#ifdef CONFIG_ENABLE_I2C
  i2c_handle_interrupt();
#endif

#ifdef CONFIG_ENABLE_SERIAL
  serial_handle_interrupt();
#endif
}

同一个函数可以声明任意多次,但只能定义一次。

我相信,__interrupt 2 意味着该函数是 low-level 中断号 2 的处理程序。Low-level 中断是一个事件,通常由硬件触发,程序可能需要反应。 __interrupt 关键字是 non-standard 并且可能特定于您正在使用的编译器。也许以下 link 是相关的:http://downloads.ti.com/docs/esd/SPRUI04/the---interrupt-keyword-stdz0559860.html

Why is this function called with no body before being declared

void on_low_interrupt(void) __interrupt 2; 不是 function call, this is the on_low_interrupt function declaration. Below is found the on_low_interrupt function definition 而是 body。

and what exactly is the '2' doing?

前导两个下划线的标识符是为实现保留的,通常由编译器实现者使用。因此,因为 2 在带有两个下划线 __interrupt 的标识符之后,所以它很可能在做特定于编译器的事情,它的含义也是特定于编译器的。

一个好的猜测是代码是为 sdcc compiler. You may read on page 44 in section 3.1. General Information in sdcc manual 编写的:

The optional number following the __interrupt keyword is the interrupt number this routine will service.

所以 2 表示例程要服务的硬件中断号。究竟什么是“2 号中断”的解释取决于设备。在您链接的存储库中,有一个 18f4550.lkr 文件 - 很可能是 pic18f4550 的链接描述文件。 datasheet for the device 可能会指导您进一步了解数字。