Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
9 years, 6 months ago.
Problem with storing serial interrupt data in buffer...
Hello guys, I want to interface sim900 modem with mbed microcontroller.I can ablr to send the At command without any problem ,but i cant able to read the response correctly using serial interrupt. I had made this receive interrupt as a callback function . After sending appropriate command i cant able to store the data in buffer, i dont know why? .I am receiving only the last arrived data (only single char eg: 'K'). Help me in solving this problem...
- include "mbed.h" char buffer[255]; void rx_interrupt() { while(sim900.readeable()) { buffer[ptr++] = sim900.getc(); } }
void main() { sim900.attach(&rx_interrupt,serial::rxiqr) sim900.printf("AT\r\n"); interupt function happens here.... }
2 Answers
9 years, 6 months ago.
may be you have to remove the 'while' into the interrupt... because another interrput can be raised during the while...
Try it with :
#include "mbed.h" char buffer[255]; void rx_interrupt() { if (sim900.readeable()) buffer[ptr++] = sim900.getc(); }
you have to use '<<code>>' your code '<</code>>
' tags to make your code more 'readable' when you post.
Second thing, it's better to use volatile variables into the interrupt if they are global. So try : volatile char buffer[255]; and the same for ptr if ptr is not local.
9 years, 6 months ago.
From the Handbook (see Ticker)
"No printf, malloc, or new in ISR
In ISR you should avoid any call to bulky library functions. In particular, certain library functions (like printf, malloc and new) are non re-entrant and their behaviour could be corrupted when called from an ISR."
I presume getc() has similar behaviour, however this example uses it in an ISR
https://developer.mbed.org/cookbook/Serial-Interrupts
This also has some useful information
https://developer.mbed.org/users/AjK/notebook/regarding-interrupts-use-and-blocking/
getc() is fine in an interrupt. It's waiting for a flag in a register and then reading a byte. In fact serial port interrupts would be fairly pointless without being able to use getc(). If it gets called from an ISR while being in the middle of a call in the main code then the main loop may or may not also see that byte of data but that's about all that would happen.
Just avoid something like scanf().
posted by 26 May 2015