如何使用 MSP432 DriverLib'c 代码编写C++ ISR?
How to write C++ ISR with MSP432 DriverLib'c C code?
我想使用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向量并extern
ISR签名,因此我的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 函数。
相关文章:
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 代码在main()中运行,但在函数中出现错误
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 编译包含字符串的代码时遇到问题
- 我在c++代码中生成了一个运行时#3异常
- 如何在linux终端中同时编译和运行c++代码
- 为cl.exe(Visual Studio代码)指定命令行C++版本
- 在Linux for Windows上编译C++代码时出错
- 我的字符计数代码计算错误.为什么
- 孤立代码块在结构中引发异常
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 此代码是否违反一个定义规则
- 为什么我的代码在输出中增加了93天
- 我的简单if-else语句是如何无法访问的代码
- 使用动态分配的数组会导致代码分析发出虚假的C6386缓冲区溢出警告
- 为什么在这个代码结束循环中没有得到结束
- 在c代码之间共享数据的最佳方式
- 这个指针和内存代码打印是什么?我不知道是打印垃圾还是如何打印我需要的值