Page 1 of 1

Use of UART0

Posted: Wed Jul 25, 2012 8:15 pm
by tufty
It seems that everyone is scared of UART0, and are therefore jumping though hoops to use UART1, which is significantly less performant.

Here's how you set up UART0...

1 - set up GPIO14 & 15 to use alternate mapping 0 (this may already be done at boot, but it doesn't hurt to redo it)
2 - Stop the uart by writing 0x0 to the CR
3 - Clear any outstanding interrupts by writing 0x7ff to the ICR
4 - Calculate the baud rate divider and fraction as follows:

divider = UART_CLK / (16 * baud)
temp = (((UART_CLK % (16 * baud)) * 8) / baud)
fraction = (temp >> 1) + (temp & 1)

UART_CLK is fixed at 3000000 for the Pi hardware

5 - write divider and fraction to IBRD and FBRD repsectively
6 - configure fifo enable, bits, parity and stop bits in LCRH

7 - reenable the UART by setting the RXE, TXE and UARTEN bits in CR
8 - enable the UART interrupts as necessary

Example code to do this is available on the ARM site (AN256 covers using the TX interrupt, DDI 013 covers the rest).

Re: Use of UART0

Posted: Thu Jul 26, 2012 10:39 am
by Cycl0ne
Hmm wehre is UART0 and where is UART1 :-) Im just using the Dwelch config :lol:

Re: Use of UART0

Posted: Thu Jul 26, 2012 4:59 pm
by DexOS
Cycl0ne wrote:Hmm wehre is UART0 and where is UART1 :-) Im just using the Dwelch config :lol:
See the BMC2835 pdf, page 10 for min Uart and page 175 for main Uart

Re: Use of UART0

Posted: Thu Jul 26, 2012 6:32 pm
by dwelch67
I have been meaning to do examples with the other uart, maybe I will just do that...

Re: Use of UART0

Posted: Thu Jul 26, 2012 8:20 pm
by DexOS
dwelch67 wrote:I have been meaning to do examples with the other uart, maybe I will just do that...
[Off topic]
@dwelch67, When you enable the ARM_TIMER with bit 9 to 1, i know your not able to reset it, and it just rolls over, but can you stop it by setting bit 9 to 0, then later set it on again by setting bit 9 to 1 ?. [/Off topic]

Re: Use of UART0

Posted: Thu Jul 26, 2012 8:23 pm
by dwelch67
http://github.com/dwelch67/raspberrypi/

added uartx01 example, uses UART0 instead of UART1.

Thank you Simon for the uart clock frequency. Is that printed somewhere or did you figure it out experimentally, etc?

Re: Use of UART0

Posted: Thu Jul 26, 2012 9:25 pm
by tufty
Ripped it from the linux source. Clock stuff is woefully missing from the broadcom doc, far as I can tell.

Simon

Re: Use of UART0

Posted: Thu Jul 26, 2012 10:21 pm
by romell
The 3MHz UART clock is at least mentioned in the firware wiki on github: "...e.g. 3MHz for UART, 50/100MHz for EMMC..." (https://github.com/raspberrypi/firmware ... -interface).

Another way (possibly the 'correct' way) to get the clock rate is with the mailbox message "Get clock rate" for the clock ID 0x000000002.

Re: Use of UART0

Posted: Fri Jul 27, 2012 7:11 am
by tufty
Ah, yes, that makes sense, and is almost certainly the right way to go.

Simon