Skip to content
Glenn Lopez edited this page Jan 29, 2017 · 20 revisions

UART Driver Initialisation

  1. Activate clock gate controls for your desired UART and GPIO:
    • UART is under RCGC1
    • GPIO under RCGC2
  2. Configure the following UART Registers:
    • Disable the UART module using UARTEN under UARTCTL
    • Set the Baud Rate:
      • UARTIBRD = (Clock speed / (16 * Desired Baud rate))
        • `ie: (50,000,000 / (16 * 115,200)) = (27.1267) = 27`
      • UARTFBRD = (UARTIBRD's -nth remainder * 64 + 0.5)
        • `ie: (0.1267 * 64 + 0.5) = 8 `
    • Set the number of bits to be transmited using WLEN**(6:5) under **UARTLCRH
    • Enable FIFO using FEN**(4) under **UARTLCRH
    • Enable the UART module under using UARTEN under UARTCTL
  3. Configure the following GPIO Registers:
    • Enable Alternative Function for UART TX and RX under GPIOAFSEL
    • Enable Digital Pins for UART's TX and RX under GPIODEN
    • Configure which bits in GPIOPCTL should be the alternative function
      • `ie: GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00)+0x00000011; `
    • Disable analog functionality on TX and RX bits under GPIOAMSEL

Example Code

void UART_Init(void){
  SYSCTL_RCGC1_R |= SYSCTL_RCGC1_UART0; // activate UART0
  SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOA; // activate port A
	
  UART0_CTL_R &= ~UART_CTL_UARTEN;      // disable UART
  UART0_IBRD_R = 27;                    // IBRD = int(50,000,000 / (16 * 115,200)) = int(27.1267)
  UART0_FBRD_R = 8;                     // FBRD = int(0.1267 * 64 + 0.5) = 8                     
  UART0_LCRH_R = (UART_LCRH_WLEN_8|UART_LCRH_FEN);	// 8 bit word length 
  UART0_CTL_R |= UART_CTL_UARTEN;       // enable UART
	
	
  GPIO_PORTA_AFSEL_R |= 0x03;           // enable alt funct on PA1-0
  GPIO_PORTA_DEN_R |= 0x03;             // enable digital I/O on PA1-0                                  
  GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00)+0x00000011;  // configure PA1-0 as UART
  GPIO_PORTA_AMSEL_R &= ~0x03;          // disable analog functionality on PA
}

TM4C123G (datasheet)

TM4C123G is a 32bit MCU based on the ARM® Cortex®-M4F architecture. Make sure to read C++ Support on TI Compilers if you plan on using C++

Clone this wiki locally