Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dead code in WHardwareSerial.cpp #43

Open
nerdralph opened this issue Oct 17, 2015 · 2 comments
Open

dead code in WHardwareSerial.cpp #43

nerdralph opened this issue Oct 17, 2015 · 2 comments

Comments

@nerdralph
Copy link

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

@bhagman
Copy link
Member

bhagman commented Oct 18, 2015

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
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants