Serial inturrupt seems to hault further code execution...

10 Oct 2012

Hi all, After the serial rx interrupt executes the specified function, I believe further code execution halts. I am not 100% certain if that is the issue. Why is that behavior happening? I would rather publish my code as a program because my project requires other libraries to function properly. As of right now I will have to settle with posting my main code in here until I figure out how to export my full project as a program. That way it maybe easier to help with this issue having all of the required libraries to run a test.

#include "mbed.h"
#include "MbedJSONValue.h"
#include "ConfigFile.h"

Serial pc(USBTX, USBRX);
Serial sensor(p28, p27);  // tx, rx

DigitalOut pc_activity(LED1);
DigitalOut uart_activity(LED2);

LocalFileSystem local("local");
ConfigFile cfg;

MbedJSONValue SensorData;
std::string s;

Ticker pingTick;
 
volatile bool pingTicked;
volatile bool pcResponse;

char pcResponseValue[BUFSIZ];
 
void pingTickFunction( void ) {
  pingTicked = true;
}

void pcRecvInterrupt( void ) {
    pcResponse = true;
}

bool linkedWithBaseStation = "FALSE";

char *sbNameKey = "SensorBoardName";
char sbNameValue[BUFSIZ];

char *sbFirstStartKey = "FirstStart";
char sbFirstStartValue[BUFSIZ];

char *sbUseBaseStationKey = "UseBaseStation";
char sbUseBaseStationValue[BUFSIZ];

void serializeJSON() {
    s = SensorData.serialize();
}

void pingBaseStation() {
    printf("{\"sbPing\": \"%s\"}\r\n", sbNameValue);
}

int main()
{
    pc.baud(38400);
    sensor.baud(38400);
    
    pingTicked = false;
    
    pc.attach(&pcRecvInterrupt);
    
    /*
     * Read a configuration file from a mbed.
     */
    if (!cfg.read("/local/settings.cfg")) {
        printf("Failure to read a configuration file.\n");
    }
    
     /*
      * Get a configuration value.
      * Then attach the sbNameValue to SensorData json
      */
     if (cfg.getValue(sbNameKey, &sbNameValue[0], sizeof(sbNameValue))) {
        //printf("'%s'='%s'\n", sbNameKey, sbNameValue);
        SensorData["name"] = sbNameValue;
     }

    /*
     * Get a configuration value.
     * Then attach the sbNameValue to SensorData json
     */
    if (cfg.getValue(sbFirstStartKey, &sbFirstStartValue[0], sizeof(sbFirstStartValue))) {
        if (strcmp(&sbFirstStartValue[0], "TRUE") == 0) {
            pingTick.attach(&pingTickFunction, 10);
            pingBaseStation();
                 
            while(!pc.readable()) {
                if (pingTicked) {
                  pingBaseStation();
                  pingTicked = false;
                }
            }
            
            if(pc.readable()) {
                pingTick.detach();
            }
            
            cfg.setValue("FirstStart", "FALSE");
            cfg.write("/local/settings.cfg");
        }
    }
    
    while(1) {
        wait(0.5);
        pc.printf("UHH");
    }
}
10 Oct 2012

You can give us the complete program by right clicking on it in the compiler, and publishing it.

Does it work without the serial interrupt? Because I don't expect that to be the problem, since it only changes one var which is not used anywhere else in the program. And how far does it get? I take it it doesnt arrive in the while(1) loop. But does pingBaseStation get executed? And you can for example add a statement which enables a LED to see how far the program is executed.

10 Oct 2012

Hi Erik, Only the MbedJSONValue library publishes as a new fork when I right click my code to publish my complete project as a program. Maybe that issue is due to me using the beta version of the mbed website? I am also using Mozilla Firefox to view mbeds compiler. Here is a link to my code repository: http://mbed.org/users/d0773d/code/ The while(1) loop does not get executed with the serial interrupt. When I comment out the serial interrupt the code executes as expected. I have two interrupts: a Ticker and a Serial interrupt on the USB. Maybe there is a interrupt conflict? At some point I would like to incorporate a function pointer inside to call another function from the results of my incoming serial data.

*EDIT*

I narrowed the publishing issue to my theory stated before. Publishing does not work corretly while using Firefox. After switching to IE, publishing functions as expected. Here is a link to my code: http://mbed.org/users/d0773d/code/YamanPonics/

Erik - wrote:

You can give us the complete program by right clicking on it in the compiler, and publishing it.

Does it work without the serial interrupt? Because I don't expect that to be the problem, since it only changes one var which is not used anywhere else in the program. And how far does it get? I take it it doesnt arrive in the while(1) loop. But does pingBaseStation get executed? And you can for example add a statement which enables a LED to see how far the program is executed.

10 Oct 2012

Hey Richard,

I did indeed manage to reproduce it (after deleting pretty much everything). I should have found earlier that there apparently still are bugs in official mbed serial lib. So option B: use MODSERIAL library. (http://mbed.org/users/AjK/code/MODSERIAL/). Just replace serial pc object by a MODSERIAL object, and change the interrupt function to: void pcRecvInterrupt( MODSERIAL_IRQ_INFO *info ).

Then it does seem to work fine.

10 Oct 2012

Hi Erik,

Can you try to read the characters received in your interrupt handler:

void pcRecvInterrupt( void ) {
    while(pc.readable()) {
        pc.getc();
    }
}

Cheers, Sam

10 Oct 2012

Yep that works. And also verified that the interrupt handler is indeed called continiously (I tried to verify that before, but if you get acute dyslectia regarding the difference between bitwise and logical operators you sometimes (read: often) mix them up).

So Richard can use that, in which case he needs to store the received data in another global variable, but it is a bit working around bugs tbh. Detaching the serial interrupt in the interrupt handler also works (btw it would be nice if serial class had just like most others a detach function, instead of attaching it to a null pointer).

11 Oct 2012

Erik, Thank you for mentioning MODSERIAL. MODSERIAL seems to work as expected. One more question, now that serial rx interrupt is working successfully should I temporarily disable the modserials interrupt so it does not trigger until a \r is found and the original rx buffer is empty?

-Rich

Erik - wrote:

Hey Richard,

I did indeed manage to reproduce it (after deleting pretty much everything). I should have found earlier that there apparently still are bugs in official mbed serial lib. So option B: use MODSERIAL library. (http://mbed.org/users/AjK/code/MODSERIAL/). Just replace serial pc object by a MODSERIAL object, and change the interrupt function to: void pcRecvInterrupt( MODSERIAL_IRQ_INFO *info ).

Then it does seem to work fine.