WiringProject / Wiring

Wiring Framework
http://wiring.org.co/
Other
217 stars 168 forks source link

dead code in WHardwareSerial.cpp #43

Open nerdralph opened 8 years ago

nerdralph commented 8 years ago

timing error is never lower without U2X, so the error calculation in HardwareSerial::begin() is useless code. http://wormfood.net/avrbaudcalc.php

bhagman commented 8 years ago

Help me out here... If I remember correctly from when I first worked on this, the idea is that if the calculated bit rate is different, then U2X is set. However, if the calculated bit rate is the same, then U2X is not set. Please double check:

  calcUBRR = (F_CPU/2/baud + 4) / 8;
  calcUBRRU2X = (F_CPU/baud + 4) / 8;

  calcBaud = F_CPU/16/calcUBRR;
  calcBaudU2X = F_CPU/8/calcUBRRU2X;

  if (abs(calcBaudU2X - baud) < abs(calcBaud - baud))
  {
    *_ucsra = 1 << U2X;
    ubrrValue = calcUBRRU2X - 1;
  }
  else
  {
    *_ucsra = 0;
    ubrrValue = calcUBRR - 1;
  }

So to be more clear, it should be: abs(calcBaudU2X - baud) != abs(calcBaud - baud)

Do you agree?

nerdralph commented 8 years ago

I'm saying always use U2x. While in theory not using U2X is better since it is less error prone, in practice the probability of seeing a difference is so low it's pointless. Using U2x simplifies the code (easier maintenance), and slightly reduces code size as well.