600 Baud comm. support, unsupported baudrate notification

- Added a 600 Baud line in DELAY_TABLE, for each CPU frequency.
(successfully tested for 16 MHz)

- begin(long speed) now returns a bool indicating if speed has been
found in DELAY_TABLE (if not, the method returns false immediately)
This commit is contained in:
sebastienjean 2012-12-01 00:20:55 +01:00
parent 0e0715abd3
commit 369aeeef51
2 changed files with 13 additions and 5 deletions

View File

@ -40,8 +40,8 @@ http://arduiniana.org.
//
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "Arduino.h"
#include "SoftwareSerial.h"
#include <Arduino.h>
#include <SoftwareSerial.h>
//
// Lookup table
//
@ -70,6 +70,7 @@ static const DELAY_TABLE PROGMEM table[] =
{ 4800, 233, 474, 474, 471, },
{ 2400, 471, 950, 950, 947, },
{ 1200, 947, 1902, 1902, 1899, },
{ 600, 1902, 3804, 3804, 3800, },
{ 300, 3804, 7617, 7617, 7614, },
};
@ -91,6 +92,7 @@ static const DELAY_TABLE table[] PROGMEM =
{ 4800, 110, 233, 233, 230, },
{ 2400, 229, 472, 472, 469, },
{ 1200, 467, 948, 948, 945, },
{ 600, 948, 1895, 1895, 1890, },
{ 300, 1895, 3805, 3805, 3802, },
};
@ -115,6 +117,7 @@ static const DELAY_TABLE PROGMEM table[] =
{ 4800, 296, 595, 595, 592, },
{ 2400, 592, 1189, 1189, 1186, },
{ 1200, 1187, 2379, 2379, 2376, },
{ 600, 2379, 4759, 4759, 4755, },
{ 300, 4759, 9523, 9523, 9520, },
};
@ -373,13 +376,15 @@ void SoftwareSerial::setRX(uint8_t rx)
// Public methods
//
void SoftwareSerial::begin(long speed)
bool SoftwareSerial::begin(long speed)
{
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
long baud = 0;
for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
{
long baud = pgm_read_dword(&table[i].baud);
baud = pgm_read_dword(&table[i].baud);
if (baud == speed)
{
_rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering);
@ -389,6 +394,7 @@ void SoftwareSerial::begin(long speed)
break;
}
}
if (baud != speed) return false;
// Set up RX interrupts, but only if we have a valid RX baud rate
if (_rx_delay_stopbit)
@ -407,6 +413,8 @@ void SoftwareSerial::begin(long speed)
#endif
listen();
return true;
}
void SoftwareSerial::end()

View File

@ -82,7 +82,7 @@ public:
// public methods
SoftwareSerial(uint8_t receivePin, uint8_t transmitPin, bool inverse_logic = false);
~SoftwareSerial();
void begin(long speed);
bool begin(long speed);
bool listen();
void end();
bool isListening() { return this == active_object; }