如何使用 MSP432 DriverLib'c 代码编写C++ ISR?

How to write C++ ISR with MSP432 DriverLib'c C code?

本文关键字:代码 C++ ISR MSP432 何使用 DriverLib      更新时间:2023-10-16

我想使用C++作为微控制器(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 接口(。所以我这样做了:

/* 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。我纳闷着,开始尝试挖掘自己。

在某些时候,我不小心在源代码C++部分中定义的 ISR 周围放置了extern "C"。它奏效了:

/* 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. */
}
}

我假设由于"I"(DriverLib(在源代码的C(而不是C++(部分中注册了ISR向量并externISR签名,因此我的C++ISR超出了ISR签名的范围。

1(我说得对吗?

但有一个问题。由于我将C++ ISR移动到C上下文中,因此我无法再在 ISR 中使用C++代码,例如类等。

2( 如何在不接触 DriverLib 的 ISR 初始化的情况下,将 ISR 中的C++保持在源代码的C++部分中(例如startup_msp432p401r_ccs.c(?

  • C++东西是C++03
  • C的东西是C89

如果驱动程序库是静态的(即.a(,您可以执行以下操作:

extern "C" void EUSCIA0_IRQHandler(void)
{
// whatever ...
}

这应该用你的功能替换标准功能[你可以用nm等检查]。而且,当驱动程序库注册 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 函数。