Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++

Problem with the baud rate

Altera_Forum
Honored Contributor II
1,916 Views

I'm studying NiosII these days, and I'm confused with the baud rate of the UART. 

In the Altera's user guide, the relation among baud rate, frequency and divisor is given by 

divisor=int(frequency/(baud rate)+0.5)...................(1) 

baud rate=frequency/(divisor+1)...........................(2) 

but according to (2), 

divisor=frequency/(baud rate)-1, why is +0.5 in (1)? 

Besides, int the simulation settings, there is an "accelerated" item, in the user guide, it's described as "accelerated (use divisor = 2)—TXD emits one bit per 2 clock cycles in simulation." But according to (2), TXD emits one bit per 3 clock cycles, what's the problem?
0 Kudos
6 Replies
Altera_Forum
Honored Contributor II
902 Views

Since frequency/baud_rate is generally a non integer number but divisor must be integer, the +0.5 trick gives you the best approximation of the required baud rate: i.e if decimal part <0.5 approximates to lower integer, while if >=0.5 approximates to upper one.

0 Kudos
Altera_Forum
Honored Contributor II
902 Views

I can understand your meaning. 

But if that, the baud rate should equal to freq/dev instead of freq/(dev+1).
0 Kudos
Altera_Forum
Honored Contributor II
902 Views

You are right. Probably there's an error in one of the formulas. 

Anyway you'd usually get only a small error, unless you want a high baudrate (>115200) with a low clock frequency (<50kHz)
0 Kudos
Altera_Forum
Honored Contributor II
902 Views

The 'divisor + 1' could well be correct, on successive clock ticks the counter register could easily be: 2, 1, 0, divisor, divisor-1 (etc).

0 Kudos
Altera_Forum
Honored Contributor II
902 Views

 

--- Quote Start ---  

The 'divisor + 1' could well be correct, on successive clock ticks the counter register could easily be: 2, 1, 0, divisor, divisor-1 (etc). 

--- Quote End ---  

 

I agree this is what is usually done by all other processor I worked with: 'divisor' is always 1 unit less the real clock division, and this allows not wasting the zero value (or worse, making it an absurd value).  

Anyway, as giraffe remarked in the original post, the information in the manual is somehow inconsistent: 

The formula asserts you must use divisor=1 in order to get a baudrate=frequency/2. But the example regarding accelerated mode says you must use divisor=2. 

If anyone is using the uart module and would like to the test the actual baudrate coming out with divisor=2, please clarify which is the correct formula.
0 Kudos
EClose
Beginner
841 Views

I used an oscilloscope to settle this. Equation (2) is correct, but equation (1) should be:

divisor=int((frequency/baud rate)-0.5)

As others noted, typically this won't make much of a difference unless your divisor is small (ie. less than 40). Note that NIOS SOPC builder incorrectly uses equation (1) with the + 0.5 so that your baud rate will be slower than ideal. I'm not sure if this has been corrected in later versions.

0 Kudos
Reply