This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

Determining UART Clock Frequency

Other Parts Discussed in Thread: EK-TM4C123GXL

I am trying to write some UART drivers using the TM4C123GH5PMI based Stellaris Launchpad, and have stumbled upon a bit of confusion with regards to the UARTSysClk frequency referenced in the Baud Rate Generator section of the data sheet.  I have seen some of the examples from Valavano's book and class, though the UART.c files that I am using as a guide to understand and build all have different values as assumed UARTSysClk values...

One such example shows:

//------------UART_Init------------
// Wait for new serial port input
// Initialize the UART for 115,200 baud rate (assuming 16 MHz UART clock),
// 8 bit word length, no parity bits, one stop bit, FIFOs enabled
// Input: none
// Output: none
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 = 8;                    // IBRD = int(16,000,000 / (16 * 115,200)) = int(8.68)
  UART0_FBRD_R = 43;                     // FBRD = round(0.68 * 64 ) = 43
                                        // 8 bit word length (no parity bits, one stop bit, FIFOs)
  UART0_LCRH_R = (UART_LCRH_WLEN_8|UART_LCRH_FEN);
  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
                                        // configure PA1-0 as UART
  GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00)+0x00000011;
  GPIO_PORTA_AMSEL_R &= ~0x03;          // disable analog functionality on PA
}

Yet another shows:

//------------UART_Init------------
// Initialize the UART for 115,200 baud rate (assuming 50 MHz UART clock),
// 8 bit word length, no parity bits, one stop bit, FIFOs enabled
// Input: none
// Output: none
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
                                        // 8 bit word length (no parity bits, one stop bit, FIFOs)
  UART0_LCRH_R = (UART_LCRH_WLEN_8|UART_LCRH_FEN);
  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
                                        // configure PA1-0 as UART
  GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00)+0x00000011;
  GPIO_PORTA_AMSEL_R &= ~0x03;          // disable analog functionality on PA
}

and looking through the datasheet, I would have guess that it would be a value of 80 MHz upon reset...

  • Hello Kyle,

    That is the problem with (a) Code Snapshots and (b) Direct Register Macro. Both of them together make code debug "extremely" difficult. To answer the question it depends on the what the UART Clock is based on the UARTCC Register setting and if the PLL is used or not. If the UARTCC shows PIOSC clock then it always is 16MHz and the Baud rate needs to be computed off it.

    If the UARTCC register shows the System Clock as the clock source then the RCC-RCC2 registers meed to be checked to determine the System Clock and then compute the Baud Rate

    Regards
    Amit
  • Thank you for your reply! Direct register macro is the only approach I am familiar with, is there a better solution for building drivers/libraries for custom purposes?

    The entire clocking system is something I am still working on getting a better grasp on understanding, so that I can more flexibly use peripherals....
  • Hello Kyle,

    I would suggest using TivaWare which has driver libs and example code. If the clocking mechanism is new then I would suggest that use TivaWare to get rid of the nuances and jump to the application development. For the example you have already stated

    D:\ti\TivaWare_C_Series-2.1.0.12573\examples\boards\ek-tm4c123gxl\uart_echo

    example is there in TivaWare Full Installation.

    Regards
    Amit