saoudimassi / arduino

Automatically exported from code.google.com/p/arduino
0 stars 0 forks source link

Baud-rate calculation in firmware #49

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
The firmware upload baud-rate should use the functionality in setbaud.h
http://www.nongnu.org/avr-libc/user-manual/group__util__setbaud.html

The firmware baud-rate calculation was changed for:
http://code.google.com/p/arduino/source/detail?r=581
but this is just a one-time fix. If you use the macros in util/setbaud.h,
you will not run into this same issue again for other upload speeds.

Also,
the firmware is using the old
  (F_CPU/(BAUD_RATE*16L)-1)
which doesn't approximate the baud rate as closely as
  (F_CPU/(BAUD_RATE*8L)-1)/2
or
  (((F_CPU)+8UL*(BAUD_RATE))/(16UL*(BAUD_RATE))-1)

which means that uploads will be corrupted more than they need to be.

Original issue reported on code.google.com by gabebear@gmail.com on 3 Jul 2009 at 5:08

GoogleCodeExporter commented 9 years ago
I'm sure everyone is aware of it right now, but for history...

Leaving the U2X bit set in the firmware for 8Mhz devices caused
http://code.google.com/p/arduino/issues/detail?id=47

Original comment by gabebear@gmail.com on 3 Jul 2009 at 5:10

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Currently, if "-DDOUBLE_SPEED" wasn't used, the baud error rate would be 7.8% 
for
57600 baud on an 8MHz Arduino!!!!!

firmware equation:
    F_CPU/(BAUD_RATE*16L)-1
better equation:
    (F_CPU/(BAUD_RATE*8L)-1)/2

--------

7.84% error = 8MHz,57600 baud,firmware's equation,w/o U2X
3.68% error = 8MHz,57600 baud,better equation, w/o U2X
2.08% error = 8MHz,57600 baud,firmware's equation,w/ U2X
2.08% error = 8MHz,57600 baud,better equation, w/ U2X

Anything above 2% is considered "bad"

If you use setbaud.h without changing the default error-rate, it will generate a
warning if there is more than 2% baud error rate.

The Math for an 8MHz Arduino at 57600 baud:
--------
with the firmware's equation, not using U2X:
    UBBR = 7 = 7.68 = (8000000/16/57600-1)
    true_baud = 62500 = 8000000/(16*(7+1)) = 8000000/(16*(UBBR +1))
    error = 7.84% = abs(1-(57600/62500)) = abs(1-( target_baud / true_baud ))
--------
with the better equation, not using U2X:
    UBBR = 8 = 8.18 = (8000000/8/57600-1)/2
    true_baud = 55555.56 = 8000000/(16*(8+1)) = 8000000/(16*(UBBR +1))
    error = 3.68% = abs(1-(57600/55555.56)) = abs(1-( target_baud / true_baud ))
--------
with the firmware's equation, using U2X:
    UBBR = 16 = 16.36 = (8000000/8/57600-1)
    true_baud = 58823.53 = 8000000/(8*(16+1)) = 8000000/(8*(UBBR +1))
    error = 2.08% = abs(1-(57600/58823.53)) = abs(1-( target_baud / true_baud ))
--------
with the better equation, using U2X:
    UBBR = 16 = 16.86 = (8000000/4/57600-1)/2
    true_baud = 58823.53 = 8000000/(8*(16+1)) = 8000000/(8*(UBBR +1))
    error = 2.08% = abs(1-(57600/58823.53)) = abs(1-( target_baud / true_baud ))

Bleck!!! too bad we can't go back in time and choose 76800 baud as the upload 
rate for
328s...

Original comment by gabebear@gmail.com on 3 Jul 2009 at 7:18

GoogleCodeExporter commented 9 years ago

Original comment by dmel...@gmail.com on 12 Jul 2009 at 3:04