26.4 Initialization and Configuration
To enable and initialize the UART, perform the following steps:
- Enable the UART module using the RCGCUART register (see Section 4.2.91).
- Enable the clock to the appropriate GPIO module through the RCGCGPIO register (see Section 4.2.87). To find out which GPIO port to enable, see the device-specific data sheet.
- Set the GPIO AFSEL bits for the appropriate pins (see Section 17.5.10). To determine which GPIOs to configure, see the device-specific data sheet.
- Configure the GPIO current level or slew rate as specified for the mode selected (see Section 17.5.11 and Section 17.5.17).
- Configure the PMCn fields in the GPIOPCTL register to assign the UART signals to the appropriate pins (see Section 17.5.22 and the device-specific data sheet).
To use the UART, the peripheral clock must be enabled by setting the appropriate bit in the RCGCUART register (Section 4.2.91). In addition, the clock to the appropriate GPIO module must be enabled through the RCGCGPIO register (Section 4.2.87) in the system control module. To find out which GPIO port to enable, see the device-specific data sheet.
This section discusses the steps that are required to use a UART module. For this example, the UART clock is assumed to be 20 MHz, and the desired UART configuration is:
- 115200 baud rate
- Data length of 8 bits
- One-stop bit
- No parity
- FIFOs disabled
- No interrupts
The first thing to consider when programming the UART is the baud-rate divisor (BRD), because the UARTIBRD and UARTFBRD registers must be written before the UARTLCRH register. Using the equation described in Section 26.3.2, the BRD can be calculated:
Equation 71. BRD = 20000000 / (16 × 115200) = 10.8507
This means that the DIVINT field of the UARTIBRD register (see Section 26.5.5) should be set to 10 decimal or 0xA. The value to be loaded into the UARTFBRD register (see Section 26.5.6) is calculated by the equation:
Equation 72. UARTFBRD[DIVFRAC] = integer(0.8507 × 64 + 0.5) = 54
With the BRD values in hand, the UART configuration is written to the module in the following order:
- Disable the UART by clearing the UARTEN bit in the UARTCTL register.
- Write the integer portion of the BRD to the UARTIBRD register.
- Write the fractional portion of the BRD to the UARTFBRD register.
- Write the desired serial parameters to the UARTLCRH register (in this case, a value of 0x0000.0060).
- Configure the UART clock source by writing to the UARTCC register.
- Optionally, configure the µDMA channel (see Section 8) and enable the DMA options in the UARTDMACTL register.
- Enable the UART by setting the UARTEN bit in the UARTCTL register.