当使用 mbed 平台触发中断时,我正在尝试发送 UDP 数据包。
但是,当我尝试udp_send
从中断函数调用时key_pressed
,我得到sys_arch_protect 错误。
这可能是因为 UDPsocket 的某些部分没有被传递给中断函数吗?
为了简洁起见,我省略了大部分代码
在此先感谢,格雷格
/*--INCLUDES----------------------------------------------------------------------------*/
#include "mbed.h"
#include "EthernetInterface.h"
/*--CONSTANTS---------------------------------------------------------------------------*/
const int BROADCAST_PORT = 58083;
char pin_status[1] = {0};
InterruptIn push_button(SW3);
/*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
void udp_send(void);
void keyPressed(void);
void keyReleased(void);
/*--------------------------------------------------------------------------------------*/
void keyPressed(void)
{
printf("Key Pressed\r\n"); //debug
udp_send(); //calling the function to send UDP packet, this casuses errors
}
/*--------------------------------------------------------------------------------------*/
void keyReleased( void )
{
printf("Key Released\r\n"); //debug
}
/*--------------------------------------------------------------------------------------*/
void udp_send(void) //sends UDP broadcast packet
{
UDPSocket sock;
sock.init();
sock.set_broadcasting();
Endpoint broadcast;
broadcast.set_address("255.255.255.255", BROADCAST_PORT); //broadcast UDP to all
sock.sendTo(broadcast, pin_status, sizeof(pin_status)); //pin_status changed elsewhere
}
/*--------------------------------------------------------------------------------------*/
int main() {
EthernetInterface eth;
eth.init();
eth.connect();
printf("IP Address is %s\r\n", eth.getIPAddress());
udp_send(); //test call to confirm UDP_send function is working (with Wireshark)
while( 1 )
{
push_button.rise(keyPressed); //debounce omitted, calls interupt
push_button.fall(keyReleased);
//other stuff
}
}