我想使用 C++
作为我的微 Controller (MSP432) 项目的主要编程语言。
我编写了一些不涉及中断服务例程(ISR)的简单脚本。他们都工作得很好。代码如下所示:
/* MSP and DriverLib includes */
/* no <<extern "C">> needed due to the header files implement it. */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>
int main()
{
/* Some C++ code here that worked fine. */
}
现在我想升级我的代码以获得简单的 ISR,例如 UART 通信(串行 PC 接口(interface))。所以我这样做了:
/* MSP and DriverLib includes */
/* no <<extern "C">> needed due to the header files implement it. */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>
int main()
{
/* Some C++ code here that worked fine. */
}
void EUSCIA0_IRQHandler(void)
{
/* Some C++ code here that worked fine. */
}
此代码的问题是 ISR 未被触发。相反,会调用 DriverLib 中的默认 ISR。我想知道并开始尝试挖掘自己。
在某些时候,我不小心将extern "C"
放在源代码的C++部分中定义的ISR周围。它起作用了:
/* MSP and DriverLib includes */
/* no <<extern "C">> needed due to the header files implement it. */
#include <ti/devices/msp432p4xx/driverlib/driverlib.h>
int main()
{
/* Some C++ code here that worked fine. */
}
extern "C"
{
void EUSCIA0_IRQHandler(void)
{
/* Only C code here works fine. */
}
}
我假设由于“我”(DriverLib)在源代码的 C(而不是 C++)部分中注册了 ISR vector 和 extern ISR 签名,所以我的 C++ ISR 超出了范围用于 ISR 签名..
1)我说得对吗?
但是有一个问题。由于我将 C++ ISR 移至 C 上下文中,因此我无法使用 C++ 代码,例如ISR 中不再有类等。
2) 如何将 C++ 保留在源代码的 C++ 部分的 ISR 中,而不触及 DriverLib 的 ISR 初始化(例如 startup_msp432p401r_ccs.c
)?
- C++ 的东西是
C++03
- C 的东西是
C89
最佳答案
如果驱动程序库是静态的(即.a
),您可以执行以下操作:
extern "C" void EUSCIA0_IRQHandler(void)
{
// whatever ...
}
这应该用您的函数替换标准函数[您可以使用nm
等进行检查]。并且,当驱动程序库注册 ISR 函数时,它应该捕获您的 ISR 函数,而不是其内部函数
我相信您现在可以从此函数调用 c++
代码。
如果没有,您可能需要:
void cplus_plus_handler(void)
{
// whatever ...
}
extern "C" void EUSCIA0_IRQHandler(void)
{
cplus_plus_handler();
}
这可能按原样工作。但是,cplus_plus_handler
可能需要位于单独的 .cpp
文件中 [C 处理程序位于 .c
中]。
如果库是动态的(即 .so
、.dll
),您可能需要调用注册函数来附加 ISR 函数。
关于c++ - 如何使用 MSP432 DriverLib'c C 代码编写 C++ ISR?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45219247/