XMegaForArduino / arduino

required (and optional) source files for the Arduino development environment, specifically the hardware/arduino sub-directory, to support xmega processors
19 stars 17 forks source link

Support for non-standard baud rates #27

Closed bombasticbob closed 7 years ago

bombasticbob commented 9 years ago

currently the HardwareSerial.cpp file supports the following baud rates only:

2400, 4800, 9600, 14400, 19200, 28800, 31250, 38400, 57600, 76800, 115200, 230400, 460800, 921600

With the exception of 31250 baud [which was calculated], the others use standard values for "BSEL" and "BSCALE" that are (literally) straight out of the documentation (see 'D' manual, section 19-9, pg 220, or AU manual section 23.9 page 289). These are combined to assign "BAUDCTRLA" and "BAUDCTRLB" in order to instruct the dividers on how to create the right clock for the specified baud rate.

This calculation is NOT simple and is not easily done with integer math. I have written a program to calculate it, and my numbers don't always match the ones in the documentation (particularly with respect to the 'baud error').

For now it is best to use "ideal values" as specified in the documentation for the standard baud rates listed above. It makes for a much smaller footprint. For other 'in between' baud rates, it may be possible to calculate BSEL and BSCALE values without writing excessively large (or slow) code to do so. If that can reasonably fit into the xmega board support, perhaps it should.

bombasticbob commented 7 years ago

Important notes:

The value 2 |BSCALE| must be at most half the minimum number of clock cycles of a frame. For instance, for 10-bit frames, the minimum number of clock cycles is 160.

This means that the highest applicable scale factor is -6 2^abs(-6) = 64 < (160 / 2) = 80.

For higher BSEL settings, the scale factor can be increased.

(AU manual page 290)

bombasticbob commented 7 years ago

new 'baud_rate' function appears to work well to speeds down to 600 baud. At 300 baud I had no serial output.