TM4C123GH6PM UART problem [SOLVED] - Page 1
Forum section: electronics / general

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 17, 2016, 08:26:00 pm
content: I am making the step up from 8 bit to 32 bit and it is hell.I decided to sit down for an hour or two each day for the rest of the summer and try to learn how to do something different. Today I decided to do a bit of UART. I went through the data sheet steps on how to set up the UART but I am having problems. I am using the TM4C123GXL launch pad with ti's ccs. The problem with my code is It doesn't seem to transmit. It only receives, I know that as the LEDs go off but I am not receiving any characters back I have spent a few hours trying to realize why and I am still clueless. I would be grateful for any help that is offered.Code:#include <stdio.h>#include <stdint.h>#include "inc/tm4c123gh6pm.h"void printChar(char);char readChar(void);int main(void){//UART 2 = RX PD6 TX PD7//Run Mode Clock Gating to UART 2SYSCTL_RCGC1_R |= (1<<2);//Clock gating to GPIOSYSCTL_RCGC2_R |= (1<<3);//Enable and provide clock in run mode to UART module 2SYSCTL_RCGCUART_R |= (1<<2);//GPIO Enable clock to Port DSYSCTL_RCGCGPIO_R |= (1<<3);//Disable UART2UART2_CTL_R &= ~(1<<0);//Set Baud-Rate Divisor//(Integer Part)UART2_IBRD_R = 0x8;//Decimal partUART2_FBRD_R = 0x2C;//Set UART 2 parametersUART2_LCRH_R |= ((1<<6)|(1<<5)|(1<<4)); //Word Length 8 bits 1 stop//Enable UART2UART2_CTL_R |= (1<<0)|(1<<8)|(1<<9);//GPIO Enable Alternate Function (UART)GPIO_PORTD_AFSEL_R |= (1<<6)|(1<<7);//GPIO Enable DigitalGPIO_PORTD_DEN_R |= (1<<6)|(1<<7);//GPIO Port Mux Enable UART2 Peripherial for PD6 PD7GPIO_PORTD_PCTL_R |= (1<<24)|(1<<28);//Disable AnalogGPIO_PORTD_AMSEL_R &= ~((1<<6)|(1<<7));// Configure LED pinsSYSCTL_RCGCGPIO_R |= (1<<5); // enable clock on PortFGPIO_PORTF_DIR_R = (1<<1)|(1<<2)|(1<<3); // make LED pins (PF1, PF2, and PF3) outputsGPIO_PORTF_DEN_R = (1<<1)|(1<<2)|(1<<3); // enable digital function on LED pinsGPIO_PORTF_DATA_R = ((1<<1)|(1<<2)|(1<<3));readChar();printChar('G');return 0;}char readChar(void){char c;while((UART2_FR_R & (1<<4)) != 0);c = UART2_DR_R;if(c=='A'){GPIO_PORTF_DATA_R &= ~((1<<1)|(1<<2)|(1<<3));}return c;}void printChar(char c){while((UART2_FR_R & (1<<5)) != 0);UART2_DR_R = c;}

author: dannyf
author_rank: Super Contributor
timestamp: August 17, 2016, 09:30:54 pm
content: Use one of thebuart examples and go through the source code to the flow of code and how they acted on the registers.

author: rsjsouza
author_rank: Super Contributor
timestamp: August 17, 2016, 09:42:46 pm
content: I haven't tried to set up UART comms on TM4C123 using direct register accesses, thus I can't help you in this particular regard. However, I just wanted to let you know the examples in their software package (Tivaware) have ROM coded libraries that end up saving you flash memory. I haven't had any problems with their UART initialization routines, although afew other bugshad crept in.

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 17, 2016, 10:41:22 pm
content: Quote from: dannyf on August 17, 2016, 09:30:54 pmI have and I think that they are identical starting to really piss me off.

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 17, 2016, 10:43:11 pm
content: Quote from: rsjsouza on August 17, 2016, 09:42:46 pmThanks for showing that ill remember it for the future if I have any problems with the tivaware functions. I've done everything with just the registers here. I haven't started using them yet.

author: dannyf
author_rank: Super Contributor
timestamp: August 17, 2016, 10:49:06 pm
content: 'they are identical starting to really piss me off.'You have lots of empirical evidence suggesting otherwise.

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 17, 2016, 10:57:09 pm
content: Quote from: dannyf on August 17, 2016, 10:49:06 pmPlease show I have tried like 50 sites and changed them each time as each site was slightly different in the order none worked. It is just the transmit that doesn't work it does receive.

author: dannyf
author_rank: Super Contributor
timestamp: August 17, 2016, 11:01:06 pm
content: Easy.You take a working sample code from ti. You identify every relevant function calls. You expand its source code. You find out how those calls impact which registers. You then replicate that in your code.Done.

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 18, 2016, 12:07:04 am
content: I got UART0 working but UART2 wont work with the same code. I think there are some registers set by default for UART0 but not UART2 so that might be preventing my code from working. I will look in to it another time.

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 18, 2016, 11:49:24 pm
content: PROBLEM SOLVEDThe problem was I picked a UART at random and chose 2. The tx pin for UART2 (PD7) is protected and needs unlocked before use. That is why it could receive but not transmit and that is why my code was identical to all the UART tutorials on the internet but would not work.

author: K5HJ
author_rank: Regular Contributor
timestamp: August 19, 2016, 03:38:16 am
content: You're not the first one to do this. I've been writing code for these processors for at least 4 years and I still get caught in this trap sometimes.The data sheet should have a caveat in big red letters on page one." Any pin that can be assigned to NMI must be unlocked before use."The registers do look a bit daunting but it is a very capable device and well worth the time spent learning.Randy

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 19, 2016, 12:05:05 pm
content: Quote from: K5HJ on August 19, 2016, 03:38:16 amI have to say I don't like the data sheet for the processor. I prefer the atmel 8 bit ones I am used to but this is a lot more complicated so I expect the datasheet to be a bit like that.I wasted about 6 hrs on that NMI pin I was copying tutorials from everywhere but none would work for UART2.I have decided to put a bit of time in to learning this so hopefully it is worthwhile. How do the other Ti arm cortex parts compare? Is coding them much the same?

author: dannyf
author_rank: Super Contributor
timestamp: August 19, 2016, 02:05:58 pm
content: Many chips have similar features to assign multiple functions to a limited number of pins.The pin remapping feature on the PIC24F is the best in that regard.

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 19, 2016, 02:09:31 pm
content: Quote from: dannyf on August 19, 2016, 02:05:58 pmI am still learning, I accidentally just reassigned a JTAG pin to be a UART and was wondering why my board wouldn't reprogram.I have fixed it using a tool from ti's website. I'm not going to make that mistake again.

author: dannyf
author_rank: Super Contributor
timestamp: August 19, 2016, 02:18:48 pm
content: you aren't along there.Here is a tip that I learned on Luminary chips and I have been using it pretty much anything I do: insert a delay loop as the first thing to do in the user space. It gives the debugger time before it is kicked out by your code.It has save me lots of time,

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 19, 2016, 02:19:56 pm
content: Quote from: dannyf on August 19, 2016, 02:18:48 pmThat is an ingenious idea. Thanks I will do it in the future.

author: dannyf
author_rank: Super Contributor
timestamp: August 19, 2016, 02:32:20 pm
content: Don't thank me: thank Luminary and their "wonderful" chips,They actually mentioned that in the datasheets.

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 19, 2016, 02:45:54 pm
content: Quote from: dannyf on August 19, 2016, 02:32:20 pmI never knew that TI had bought those chips. I suppose that explains the whole lm4f120 and TM4C1233H6PM situation.

author: K5HJ
author_rank: Regular Contributor
timestamp: August 19, 2016, 05:30:02 pm
content: Quote from: Aodhan145 on August 19, 2016, 12:05:05 pmThe TM4C123 and TM4C 129 parts are very similar. I have programmed both.I have grown fond of Code Composer Studio after getting used to the Eclipse interface. And the free license is nice.My (ex)company also used the Sitara family of parts and the CCS tools are the same.Using TivaWare and AM335x StarterWare has been a big help. Although some of the code is buggy, it still has been useful.Welcome the ARM.Randy

author: dannyf
author_rank: Super Contributor
timestamp: August 19, 2016, 07:28:26 pm
content: The Luminary chips are quite complicated and having Stellaris / Tivaware help getting up the learning curve.The MSP432 is an interesting chip for its blend of performance and low-power consumption. Too bad TI has yet to fix the compiler to allow a more seamless transition between 430 and 432 code.

author: Aodhan145
author_rank: Frequent Contributor
timestamp: August 19, 2016, 09:10:20 pm
content: Quote from: dannyf on August 19, 2016, 07:28:26 pmThis is my first experience with ti micros. I need to pick up a couple of MSP430s. I have only ever used a lot of Atmel ones and one PIC16. I got into electronics when I picked up an arduino a few years ago when I thought it was amazing that i could turn on an LED over the internet. So thats why i'm a bit of an atmel fanboy.

author: dannyf
author_rank: Super Contributor
timestamp: August 19, 2016, 09:47:00 pm
content: then you may want to check out msp432: it is essentially a 32-bit msp430, with (mostly) compatible peripherals.Check out the porting guide before you jump in, however.

author: dannyf
author_rank: Super Contributor
timestamp: August 19, 2016, 10:28:39 pm
content: Updated my compiler to 15.12 LTS and the interrupts worked beautifully - as I had used the same vector names so my code compiled and ran as expected.I'm a happy camper.BTW: TI should have updated the porting guide.