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.

Help me with UART1 configuration in Tiva TM4C123



Below is the code for UART0 what I have to change for UART1.
Explain please for nub! please please please!
#include "TM4C123.h" // Device header
#include <stdint.h>
 
char UART0Rx(void);
void delayMs(int n);
 
int main(void)
{
 char c;
 
 SYSCTL->RCGCUART |= 1; /* provide clock to UART0 */
 SYSCTL->RCGCGPIO |= 1; /* enable clock to PORTA */
 SYSCTL->RCGCGPIO |= 0x20; /* enable clock to PORTF */
 
 /* UART0 initialization */
 UART0->CTL = 0; /* disable UART0 */
 UART0->IBRD = 104; /* 16MHz/16=1MHz, 1MHz/104=9600 baud rate */
 UART0->FBRD = 11; /* fraction part, see Example 4-4 */
 UART0->CC = 0; /* use system clock */
 UART0->LCRH = 0x60; /* 8-bit, no parity, 1-stop bit, no FIFO */
 UART0->CTL = 0x301; /* enable UART0, TXE, RXE */
 
 /* UART0 TX0 and RX0 use PA0 and PA1. Set them up. */
 GPIOA->DEN = 0x03; /* Make PA0 and PA1 as digital */
 GPIOA->AFSEL = 0x03; /* Use PA0,PA1 alternate function */
 GPIOA->PCTL = 0x11; /* configure PA0 and PA1 for UART */
 
 GPIOF->DIR = 0x0E; /* configure Port F to control the LEDs */
 GPIOF->DEN = 0x0E;
 GPIOF->DATA = 0;
 
 for(;;)
 {
 c = UART0Rx(); /* get a character from UART */
 GPIOF->DATA = c << 1; /* shift left and write it to LEDs */ } } /* UART0 Receive */ /* This function waits until a character is received then returns it. */ char UART0Rx(void) { char c; while((UART0->FR & 0x10) != 0); /* wait until the buffer is not empty */
 c = UART0->DR; /* read the received data */
 return c; /* and return it */
}
 
/* Append delay functions and SystemInit() here */
void delayMs(int n){
int i,j;
 
 for(i=0;i<n;i++){
 for(j=0;j<3180;j++)
 {}
 }
}
  • Well, thanks for providing an almost perfect example of how NOT to do it...

     SYSCTL->RCGCGPIO |= 0x20; /* enable clock to PORTF */

     UART0->IBRD = 104; /* 16MHz/16=1MHz, 1MHz/104=9600 baud rate */

     UART0->FBRD = 11; /* fraction part, see Example 4-4 */

     UART0->LCRH = 0x60; /* 8-bit, no parity, 1-stop bit, no FIFO */

     UART0->CTL = 0x301; /* enable UART0, TXE, RXE */

    Are you sure these Magic Numbers really do what the should, and if so, can you remember the bits in a few weeks or even month ... ? Do you expect others to know the MCU datasheet by heart, or wade through it while reading your code ? (Look like you are migrating from MSP430, where this style is still common and accepted ...)

    There are good reasons why this Direct Register Access method is discouraged (at least where code quality and portability count).

    Rather, download the TivaWare for the TM4123C (if you haven't already), and use the examples in <TivaWare-Folder>/examples/peripherals/uart as guidance.

    For initialisations, you don't lose performance when using the driverlib functions instead of direct-register-access, but gain a lot of clarity and portability.

  • Hello f.m.

    And not to forget, MSP devices are now moving their software to DriverLib style as well....

    Regards
    Amit
  • More or less, at least according to my impression.
    Being present at the MSP forum as well, I noticed some examples and recommendations toward the DriverLib style for the MSP432, but solely DRM-style for the (16 bit) MSP430 core.
    "Old" devices tend to have a large pool of examples, which use to provide a certain "inertia" - even toward improvements ...
  • Hello f.m.

    Using the DRM style was easy compared to creating and maintaining the driver files. Also it is a more grey matter operation thinking on how to keep the API and its parameters consistent (at least 5 years in advance).

    Regards
    Amit
  • Also it is a more grey matter operation thinking on how to keep the API and its parameters consistent (at least 5 years in advance).

    That's for sure. One needs to find the "lowest common denominator" for the class of target devices or platforms to design a proper library. And a high degree of target device family "orthogonality" helps, too. But still, it is additional design work spent in the beginning.

    But having been forced to port old code (and often crappy "ad hoc" hacks), I can assure this is a place where libraries definitely pay off.

  • Hello f.m.

    Couldn't have agreed with you more. I see a lot of tardy DRM examples from almost all manufacturers (this one included).

    Regards
    Amit
  • IMHO very much depends on the target market, and consequently marketing/sales strategy. For mass products with short product cycles, project managements usually opt for the cheapest silicon that does the job. And they rather spend another month and/or another sw engineer to squeeze out the last fractions of a percent of that cheaper MCU, before resorting to a bigger/more expensive one. And this begs for DRM style code.

    With simpler words (i.e. without sugar coating): Throw-away code for Throw-away products.

    If this sounds like I'm not much in appreciation of this method - probably correct.

  • Hello f.m,

    I am not aware of the throw away products, but for most of the "long" run products this has been the case. Very few provide dual support by adding driverlib style coding even at a latter stage.

    Regards
    Amit
  • Hi everybody,

    Could anybody post right porsion of code for initialisation UART1?

    Or correct my code.

  • Hello Alexander,

    Sorry for the slight of course discussion, but we would strongly suggest to move to driverlib approach as it uses well defined API's and debug is way simpler for you and me.

    Regards
    Amit
  • Is it so difficult post a few rows of code???

  • Hello Alexander

    It is not difficult, but we have seen a lot of times in the forum (search warranted) it always starts with a few lines and then gets complicated. Hence the first help here is to migrate to driverlib API version

    Regards
    Amit
  • Agree.

    If you download TivaWare, you can use ".../TivaWare_C_Series-2.1.2.111/examples/peripherals/uart/uart_loopback.c" as a template example. (TivaWare version numbers might vary).

    Repost this example code here would be really superfluous.

  • Hello f.m.

    Thanks. That code example completely skipped my mind.

    Regards
    Amit
  • I have found the answer. Someself.

    !? Thank you. Your advices were very helpfull !?

    And all of you was very frendly!

     

  • !? Thank you. Your advices were very helpfull !?

    And all of you was very frendly!

    The embedded sarcasm does not go undetected.

    You can of course always do things your very own way. But you cannot force others to see and do things your way - especially when it's about reinventing the wheel. BTW, I know project leaders who interpret such a dismissal of established libraries and the use of DRM coding style as anti-social and an attempt to make oneself indispensable ...