Serial interrupt

15 Jan 2010 . Edited: 18 Jan 2010

Hi mbedders.

I'm sorry to post a new topic, as I couldn't find it anywhere else.

I have attached a serial interrupt to my code, and don't know if the interrupt fires when there's a character ready,
or if it just fires when something happens on the line (falling/rising edge).

Could anybody clarify this for me?

/ Lerche

15 Jan 2010

Hi,

 

Christian Lerche wrote:
have attached a serial interrupt to my code, and don't know if the interrupt fires when there's a character ready, or if it just fires when something happens on the line (falling/rising edge).

It is when a character arrives. I'll make a note it is not clearly documented.

Thanks!

Simon

 

15 Jan 2010

So now I can be sure of getting the characters into my buffer :-)

Great, thanks Simon!

/ Lerche

15 Jan 2010 . Edited: 18 Jan 2010

The serial interrupt somehow stop my program from working.
I don't know why:

/*
Author: Christian Lerche
Date: 02-01-2010
MCU: LPC1768
Notes: SMS controlled relay
*/
#include "mbed.h"

DigitalOut led(LED1);
DigitalOut led2(LED2);
Serial pc(USBTX, USBRX); // tx, rx of USB
Serial device(p9, p10);  // tx, rx of RS232 pins
unsigned char buf[1000];
unsigned int i;

void rec() {                                             // Here's my serial interrupt routine.
    buf[i]=device.getc();                                // Put the character received into the buffer, at place "i"
    i++;                                                 // Increment "i" to make ready for next interrupt
}                                                        // interrupt is done.

int main() {                                             // Main structure.
    led=!led;
    wait(0.2);
    device.printf("AT+CPMS=\"ME\",\"ME\",\"ME\"\r");     // Preferred message storage= "ME""ME""ME" - Internal memory for all
    led=!led;
    wait(0.2);
    led2=!led2;
    wait(0.2);
        led2=!led2;
        device.attach(&rec);                                 // Attach the "Rec" interrupt to the serial, so we receive every character.
        device.printf("AT+CMGR=3\r");                        // Read the incoming message
        //device.attach(&rec);                                 // Attach the "Rec" interrupt to the serial, so we receive every character.
        led=!led;
        wait(0.2);
        led=!led;
        wait(0.2);
        for ( i=0; i<1000; i++) {                             // Put the character received into the computer.
            pc.putc(buf[i]);                                 // ^^
        }
        led=!led;
        wait(0.2);
        for ( i=0; i<1000; i++) {                             // Delete whats in the buffer (making it ready for a loop, for reset to be unnessesary
            buf[i]=0;                                        // ^^
        }
        led=!led;
        wait(0.2);
        wait(3);                                             // wait command, to make ready for loop
}
18 Jan 2010

The phone answers back, the same time it has received a character. Fx. Send an 'A', and it loops it back to the mbed module. Somehow, I think the mbed i not capable of sending and receiving in such short time.
Can anybody cast some light on the subject?

Somehow, I think that the hardware buffer in the LPC1768, can't handle all that traffic at once. One byte send, same time receiving one. Then my interrupts running, getting back to the program, and i think that in the meantime, it wan't to send the next char. That's why it stops my program. Too little hardware-buffer. I guess?!

And then I saw a post. 16 Bytes of buffer. That's more than enough. But why does the serial.interrupt stop my program from running?!

24 Jan 2010

Christian,

 

I've had some similar issues where the whole program freezes if I happen to simultenously send and receive characters while using the usb based serial port.  I happen to also be using p9/p10 serial port as well in my code...  The best thread I've found about it is here:

http://mbed.org/forum/mbed/topic/181/?page=1#comment-790

I assume the library revision 19 has the fix about which Simon mentions over in that other thread...  It may just be something about being in the interrupt function and not exiting quickly enough before another interrupt occurs, or perhaps the interrupt occurs while in the middle of the getc call so the port stays blocked or something...  I may try using Richard Sewell's example for a buffered serial class and see if I have any luck with that...

Good luck!

-John

24 Jan 2010 . Edited: 24 Jan 2010

Christian,

 

I've done some more testing and put together a little example program that seems to be the minimum necessary to recreate the system hang when using serial interrupts.  I quick work around may be for you to immedeately do a putc while inside of your interrupt service routine like so:

void rec() {                                             // Here's my serial interrupt routine.
    buf[i]=device.getc();                                // Put the character received into the buffer, at place "i"
    device.putc('\0');     //<--- this odd line may fix the system hang, but might mess up your device :)
    i++;                                                 // Increment "i" to make ready for next interrupt
}                                                        // interrupt is done.

 

 

I'm not sure why, but when I run this test program it seems to work fine if I have a putc in the interrupt routine, but it hangs if I send some bytes from my terminal while it is simultenously sending me stuff... It is kinda interesting to see how the "interrupt_led" actually goes off and even though the system seems hung, it comes on the next time i push a key...  I wonder if the putc properly clears the interrupt flag or something different that the getc isn't doing (wild speculation here...)

 

Okay, hope this sheds some light on the subject, or maybe we're both just crazy and aren't doing something right... *scratch head*

Thanks for any tips!

-John

24 Jan 2010

Hi, John!

Great, thanks for the try with the code, I will try it when I'm home from work tomorrow!

I think my phone will answer "Error" to the /0 send to it :P

 

/ Lerche

25 Jan 2010

Christian,

 

I did some more experimenting, including a slightly modified version of Richard Sewell' SerialBufferdDemo and still ran into problems.  I was able to narrow it down to a problem between the interaction of Serial::printf and the attach serial interrupt.  If you were to replace all of your device.printf("whatever") commands with a bunch of putc's I bet that would also prevent the hang.  One last note, since you mentioned there is a 16 byte serial fifo buffer, you may want your interrupt to keep reading bytes until the buffer is empty (as I'm guessing that you could receive up to 16 bytes before the interrupt actually triggers, and if you read only one byte, the others may remain in the buffer until you get some more to trigger the next interrupt...)  so just put in something like in richard's demo... Okay, I hope you get it working!  -John

void rec() {                                             // Here's my serial interrupt routine.
    while(device.readable() && buffer_is_not_full)
    {
      buf[i]=device.getc();                                // Put the character received into the buffer, at place "i"
      i++;                                                 // Increment "i" to make ready for next interrupt
    }
    handle_buffer_overflow()
}
25 Jan 2010

Hi, John!

I've now tried the '/0' solution you came up with, and it looks like it works!!

By now it reads messages, and doesn't stop my program whenever it feels like it. I've even inserted and while loop with a little delay, so I can follow it on the computer. This is simply perfect!!

I thank you for your great replies to this thread!

/ Lerche

24 Jun 2011

Hello, I have a problem that but I am not sure is the same: http://mbed.org/forum/helloworld/topic/2439/ Regards, Juan