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.

Simple UART Loopback code

Other Parts Discussed in Thread: TM4C123GH6PM

Hello TI community,

I am just beginning with a Tiva C series Launchpad and I am using CCS v6.1 to develop some code. Eventually I'd like to use the UART to drive a RF module (non-TI), and so I have written some simple code to write values to UART1, and then I try and read them out to the PuTTY console on UART0. Try as I might to self-educate, it seems that I've missed something with how to program this device. I've worked trhough a good number of the LaunchPad workshop labs and searched through this forum, and my suspicion is that I have a locked pin on UART1. Here is my code, and the device in particular on my launchpad is the TM4C123GH6PM. Any help you can give or tips for beginners would be appreciated, and sorry for the assuredly high-caliber noob-ness of this code.

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"
#include "driverlib/pin_map.h"
#include "driverlib/uart.h"


void UART_Init(void);

void UART_Init(void){
    SysCtlPeripheralReset(SYSCTL_PERIPH_UART1);
    SysCtlPeripheralReset(SYSCTL_PERIPH_GPIOB);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART1);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    GPIOPinConfigure(GPIO_PB0_U1RX);
    GPIOPinConfigure(GPIO_PB1_U1TX);
    GPIOPinTypeUART(GPIO_PORTB_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    UARTConfigSetExpClk(UART1_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));

    SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    GPIOPinConfigure(GPIO_PA0_U0RX);
    GPIOPinConfigure(GPIO_PA1_U0TX);
    GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);

    UARTConfigSetExpClk(UART0_BASE, SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));

	SysCtlClockSet(SYSCTL_SYSDIV_4|SYSCTL_USE_PLL|SYSCTL_XTAL_16MHZ|SYSCTL_OSC_MAIN);

}

int main(void)
{

 UART_Init();


	while(1)
	{
		UARTCharPut(UART1_BASE, 'a');
		if (UARTCharsAvail(UART1_BASE)) {UARTCharPut( UART0_BASE, UARTCharGet( GPIO_PORTB_BASE));}
		// Delay for a bit
		SysCtlDelay(20000000);

		UARTCharPut(UART1_BASE, 'b');
		if (UARTCharsAvail(UART1_BASE)) {UARTCharPut( UART0_BASE, UARTCharGet( GPIO_PORTB_BASE));}
		// Delay for a bit
		SysCtlDelay(20000000);

		UARTCharPut(UART0_BASE, 'c');
		if (UARTCharsAvail(UART1_BASE)) {UARTCharPut( UART0_BASE, UARTCharGet( GPIO_PORTB_BASE));}
		// Delay for a bit
		SysCtlDelay(20000000);


	}
}

  • Hello user4388205,

    Not clear as to how have you wired UART1 on the LaunchPad. Please explain.

    Regards
    Amit
  • Dawns that we may have "missed" this, "Use of other than UART_0" as useful w/in, "Whats REALLY unclear?"

    "Unguided" (sorry, but so true) posters ALWAYS miss this needed info - endlessly expanding "back-forth."

  • Hello cb1,

    Does not this look interesting in the code? A second go through the post made me open my eyes wide...

    UARTCharGet( GPIO_PORTB_BASE)

    Regards
    Amit
  • Hi Amit,

    Mais certainement - yet I (rarely) "dig that deep" especially when it's a "foreign UART" and its interconnect unspecified...
  • Hi,

    Thanks for the quick response, and sorry about the unclear post. I have nothing special going on as far as wiring my Launch Pad, I just want to see characters clocking into UART1. I though the best way to check that was by displaying the contents of UART1 on the PuTTY terminal. Eventually, I would like my wiring of UART1 to connect to the UART interface of an RF chip to control, but I cannot do that until I know I can get a digital signal on the UART pins (PB0 and PB1). I am not seeing anything on the scope, however, I am getting 'characters' on PuTTY (incompletely filled squares pop up periodically on the terminal, about the size a character would take up). If there are any more questions, please let me know, but keep in mind I am very new to all this and I apologize if I miss what you're asking for...
  • Also is it kosher to reset a peripheral before enabling it?

    Certainly it's not correct to set the baud rates before setting the micro's frequency.

    Robert
  • Hello user4388205,

    So the logical question.

    You have sent a character from UART1 to the UART1TX Pin and then checking if UART1 has received a data or not. The RX pin is not connected to anything then what is UART1 going to receive?

    Secondly a run through of the post (shown in my subsequent writting indicates a logical flaw in the code).

    Regards
    Amit
  • And another thing, when I called this post loopback, I was referring to my intended implementation of putting chars into one UART, and seeing them in another. I was hoping by putting them in UART1 and checking trhough UART0 would be a good way to do this.
  • Amit,

    I saw your previous post, and will make corrections immediately. I am not sure what you are asking in the second post. I simply tried to emulate the PuTTY echo code in the LaunchPad workshop, but instead of inserting characters into UART0 through PuTTY I though I could enter them into UART1 with this code. Have I missed something on how to use the UART properly?
  • Hello user4388205

    Logically and Physically yes.

    The UART1 is not connected to anything. So the High and Low levels are just terminating in "mid-air". That is why we (cb1 and myself) have asked for is connection of UART1 to the PC?

    Regards
    Amit
  • Yes! This is exactly what I wanted help with. Okay, I thought I would be able to set the pins with the software and leave them unconnected and pass signals. But, this is perfect, how do I send a signal on those pins and see it? should I jumper the TX pin of UART1 to the RX pin of UART0? Sorry if this seems so trivial, I had assumed I could pass those signals within the microcontroller. Thanks again for your help.
  • Hello user4388205,

    Pins are the uC connection to the external world. They cannot be passed within w/o a loopback. I would ask you to study the LaunchPad Schematics first to see if UART0 RX can be connected electrically to UART1 TX.

    Only then will "trivial" translate to "non assumed logical".

    Regards
    Amit
  • Thank you for the clear advice! Never even thought of looking at the electrical schematic, I got so intimidated by the manual for the chip I wasn't even sure where to begin.
  • To aid user's understanding - UART_0 "alone" connects to a 2nd MCU on your board - and them employs USB as transfer medium to the PC.

    ALL OTHER UARTs do not have (eased) access to that 2nd MCU - thus connect (as Amit states) "To the Air!"
    You are not alone in failing to recognize that fact.

    You must add a proper USB to UART converter to properly connect your MCU's (Non UART_0) Uart ports to the PC.

    Review of your board's schematic should enhance (or lead) to your recognition & understanding. (and maybe - acceptance)
  • See, on some level I suspected that, and so I though I could use the UART0 as a back door by reading UART1 into it. Now I see that I was relying far too heavily on the software to connect. Thanks for the further explanation.
  • Your logic is mostly valid - the lack of "review of your board's schematic" was your major shortcoming.    (always unwise to "assume" when "hard facts" stand nearby)

    While you'll need the stated "USB to UART" converter (some come embedded w/in a cable) to "connect & transact w/PC's USB" - later - after that method succeeds - you may switch to MCU UART <-> External Device's UART - but do so only after confiming the external device to employ "normal" CMOS voltage levels.

    Again - the (not too well noted) presence of the 2nd MCU enables the quick/eased connection to the PC - via UART_0.    No other UART enjoys that luxury - and that's an "easy trap" in which to fall - and imho - vendor here must far better "caution & (pardon) guide!"    But you - now - not so much...