Arduino softwareserial clashing with SPI? -
i have arduino project using nrf24l01+ radio module on spi (using library: http://tmrh20.github.io/rf24/) , rfid reader on softwareserial. sleeping arduino , having them wake via interrupt when message has been received or rfid tag ready read. rfid on pins 4 , 5, while nrf covers pins 9 - 13 number 2 interrupt.
both of these modules work fine sleep , interrupt code separately, when combined in single sketch, arduino wake due rfid tag, read it, try send on radio , hang, waiting library call write() return.
i have delved bit 2 libraries, can't make heads or tails of softwareserial library. seems maybe using same isr behind scenes nrf module, don't see why should big problem, , don't understand why should cause radio hang.
i know may long shot, have idea might going on? maybe knows these libraries? thoughts on work around? thanks.
i experiencing same symptoms, , problem turned out buffer overrun in code. overrun due softwareserial
dropping bytes because rf24
library interring interrupt handling.
the code in question reads gps receiver using softwareserial
, parses nmea sentences , extracts latitude , longitude information sending on radio using rf24
. goes this:
if (gpsserial.available()) { int c = gpsserial.read(); if (c == '\r') { buf[bdx] = '\0'; // process buf here, contains null terminated nmea sentence // , send via rf24 bdx = 0; else if (c != '\n' && bdx < buflen) buf[bdx++] = c; }
where buflen
sized larger single nmea sentence.
the experienced reader pick on problematic line:
buf[bdx] = '\0';
which writes buf without range check. in normal operation, uninterrupted stream of characters gps module, code works fine, because encounter \r
before running out of space in buf
. sending information on rf24
causes enough delays characters dropped or corrupted softwareserial
, , assumption no longer holds.
so happens this:
- a
\r
missed, , previous nmea sentence not discarded - the next nmea sentence read
buf
bdx
advances until stopped range check inelse
\r
of next nmea sentence counteredbuf[bdx] = '\0';
writes past end ofbuf
at point arduino stops responding, , looks write
blocking.
the fix line added before if
:
if (bdx >= buflen) bdx = 0;
with no other modification line, code has been running on 5 hours without issue, whereas not last more 30 seconds before write
"blocks".
Comments
Post a Comment