chipKIT® Development Platform

Inspired by Arduino™

HardwareSerial.cpp fix

Created Sun, 03 Jul 2011 02:38:05 +0000 by svofski


svofski

Sun, 03 Jul 2011 02:38:05 +0000

I couldn't get bitrates higher than 115200 working and I had to peek into the implementation. Here's what I've found: Line 132:

*_UxBRG      =   (__PIC32_pbClk / 16 / (baudRate - 1));  // calculate actual BAUD generate value.

This is obviously a typo because the formula in the datasheet says that this line should read:

*_UxBRG     =   (__PIC32_pbClk / 16 / baudRate - 1);    // calculate actual BAUD generate value.

With this little change, I can communicate with Max32 at up to 1.25 Mbps, which is about 110 kB/s. Not too bad compared to 115200, although it's a pity that the FTDI chip seems to be limiting us here. PIC32MX can put out much more.


Mark

Sun, 03 Jul 2011 23:57:35 +0000

svofski

Thanks for finding this, I will get it into the code

Can you point to a page where you found that, I am very particular about things like that being clear and obvious, the way it stated is not obvious how the compiler is going to evaluate it.

Mark


svofski

Mon, 04 Jul 2011 11:17:17 +0000

Sure, it's in the section 21.3, "UART BAUD RATE GENERATOR" in this document:

ww1.microchip.com / downloads / en / DeviceDoc / 61107F.pdf

I'm not sure what is not obvious here, but the parenthesis can be added for extra clarity like this:

(__PIC32_pbClk / 16 / baudRate) - 1

Martin Hodge

Tue, 05 Jul 2011 19:39:19 +0000

I'm a little confused on this. Wouldn't the equation in that datasheet dictate the following?

(__PIC32_pbClk / 16 * baudRate) - 1

svofski

Wed, 06 Jul 2011 10:52:50 +0000

The expressions are evaluated left to right according to order of precedence of operators. Division and multiplication precede over addition and subtraction. To override the precedence, we use parentheses. To illustrate this, your version, expanded to simple subexpressions like calculator keypresses, would work like this:

temp = __PIC32_pbClk / 16
temp = temp * baudRate
temp = temp - 1

while the formula says:

temp = 16 * baudRate
temp = __PIC32_pbClk / temp
temp = temp - 1

and the code in the repo now says:

temp = __PIC32_pbClk / 16
temp = temp / (baudRate - 1)

Martin Hodge

Wed, 06 Jul 2011 16:18:37 +0000

I was less concerned about the precedence than I was the division where multiplication seems to be indicated...


svofski

Wed, 06 Jul 2011 21:09:56 +0000

A/B/C ≡ A/(B*C) | A,B,C ∈ ℝ


Martin Hodge

Wed, 06 Jul 2011 21:27:19 +0000

That's what I was missing, thanks!