From EdWiki

Enabling and configuring the UART

Enabling the UART is a two step process. The GPIO on which the UART resides must be enabled, and the UART pins must be configured as alternate function, digital pins. Pins must also be muxed to the appropriate alternate function. This is done with the GPIO API.

The second step involves enabling and the UART itself. The UART should be disabled while it is being configured.

  1. The UART clock must be enabled with periph_clock_enable().
  2. The UART must be disabled with uart_disable().
  3. The UART clock source should be chosen before setting the baudrate.
  4. Baudrate, data bits, stop bit length, and parity can be configured.
  5. If needed, enable CTS or RTS lines via the uart_set_flow_control().
  6. The UART can now be enabled with uart_enable().

For example, to enable UART1 at 115200 8n1 with hardware flow control:

// Enable the UART clock
// We need a brief delay before we can access UART config registers
__asm__("nop"); __asm__("nop"); __asm__("nop");
// Disable the UART while we mess with its settings
// Configure the UART clock source as precision internal oscillator
// Set communication parameters
uart_set_baudrate(UART1, 115200);
uart_set_databits(UART1, 8);
uart_set_parity(UART1, UART_PARITY_NONE);
uart_set_stopbits(UART1, 1);
// Enable RTC and CTS lines
uart_set_flow_control(UART1, UART_FLOWCTL_HARD_RTS_CTS);
// Now that we're done messing with the settings, enable the UART