tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Use of UART0

Wed Jul 25, 2012 8:15 pm

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).

User avatar
Cycl0ne
Posts: 102
Joined: Mon Jun 25, 2012 8:03 am

Re: Use of UART0

Thu Jul 26, 2012 10:39 am

Hmm wehre is UART0 and where is UART1 :-) Im just using the Dwelch config :lol:

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: Use of UART0

Thu Jul 26, 2012 4:59 pm

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
Batteries not included, Some assembly required.

dwelch67
Posts: 959
Joined: Sat May 26, 2012 5:32 pm

Re: Use of UART0

Thu Jul 26, 2012 6:32 pm

I have been meaning to do examples with the other uart, maybe I will just do that...

User avatar
DexOS
Posts: 876
Joined: Wed May 16, 2012 6:32 pm
Contact: Website

Re: Use of UART0

Thu Jul 26, 2012 8:20 pm

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]
Batteries not included, Some assembly required.

dwelch67
Posts: 959
Joined: Sat May 26, 2012 5:32 pm

Re: Use of UART0

Thu Jul 26, 2012 8:23 pm

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?

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: Use of UART0

Thu Jul 26, 2012 9:25 pm

Ripped it from the linux source. Clock stuff is woefully missing from the broadcom doc, far as I can tell.

Simon

romell
Posts: 25
Joined: Mon Jul 23, 2012 6:57 pm

Re: Use of UART0

Thu Jul 26, 2012 10:21 pm

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.

tufty
Posts: 1456
Joined: Sun Sep 11, 2011 2:32 pm

Re: Use of UART0

Fri Jul 27, 2012 7:11 am

Ah, yes, that makes sense, and is almost certainly the right way to go.

Simon

Return to “Bare metal, Assembly language”