2

mbed プラットフォームを使用して割り込みがトリガーされたときに、UDP パケットを送信しようとしています。

udp_sendただし、割り込み関数から呼び出そうとするとkey_pressedsys_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
    }
}
4

1 に答える 1

0

@Tony dに感謝します。とにかく、割り込みルーチン内に大きな関数呼び出しを含めることはお勧めできません。

今後の参考のために、コードが添付されています。

#include "mbed.h"
#include "EthernetInterface.h"
#include "rtos.h"
#include <string>
#include "PinDetect.h"

/*--CONSTANTS---------------------------------------------------------------------------*/
const int BROADCAST_PORT = 58083;
char pin_status[1] = {0};
int global_flag;

InterruptIn push_button(SW3);

/*--FUNCTION DEFINITIONS----------------------------------------------------------------*/
void udp_send(void);
void keyPressed(void);
void keyReleased(void);

EthernetInterface eth;
Endpoint broadcast;
UDPSocket sock;

/*--------------------------------------------------------------------------------------*/
void keyPressed(void)
{
    printf("Key Pressed\r\n"); //debug
    udp_send();
}

/*--------------------------------------------------------------------------------------*/
void keyReleased( void )
{
    printf("Key Released\r\n"); //debug
}

/*--------------------------------------------------------------------------------------*/
void udp_send(void)
{
    global_flag = 1;        //sends UDP broadcast packet
        //pin_status changed elsewhere
}

/*--------------------------------------------------------------------------------------*/
int main() {
    eth.init();
    eth.connect();
    sock.init();
    sock.set_broadcasting();

    broadcast.set_address("255.255.255.255", BROADCAST_PORT);   //broadcast UDP to all
    printf("IP Address is %s\r\n", eth.getIPAddress());

    udp_send();     //test call to confirm UDP_send function is working

    while( 1 )
    {
        push_button.rise(keyPressed);   //debounce omitted, calls interupt
        push_button.fall(keyReleased);
        if (global_flag)
        {
            global_flag = 0;
            sock.sendTo(broadcast, pin_status, sizeof(pin_status));
        }
    }
}
于 2016-01-22T09:39:49.000 に答える