Skip to content

Proposal to improve SoftwareSerial #1198

Closed
@RobTillaart

Description

@RobTillaart

see also - http://arduino.cc/forum/index.php?topic=138497.0 -

Investigated if the magic numbers in SoftwareSerial to determine the bit timing could not be replaced by formulas.

the colums named - rxintra rxstop - are in every table identical so merging that would save footprint.

It appeared that the timing tables can be replaced by formulas that gives smaller footprint and enables non-standard baud rates over SoftwareSerial. This latter allows to compensate the baud rate a bit if the baud rate of the other devices differs due to clock differences.

-- 16Mhz formulas:
rxstop = 16000000L/(7 * baudrate) - 2;
rxintra = rxstop; // this can be removed too as these are always identical.
tx = rxstop - 4;
rxcenter = rxstop/2 - 5;

-- 8Mhz formulas (not tested)
int rxstop = 8000000L/(7 * baudrate) - 4;
int rxintra = rxstop;
int tx = rxstop - 3;
int rxcenter = max(rxstop/2 - 7, 1);

-- 20Mhz formulas (not tested)
int rxstop = 20000000L/(7 * baudrate) - 1;
int rxintra = rxstop;
int tx = rxstop - 3;
int rxcenter = rxstop/2 - 4;

Only tested the 16Mhz version and the behaviour tested so far is identical to the table driven version.
Furthermore the sketch size - and thus the library footprint - is 182/192 bytes less. (See forum thread).

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions