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.

UART0 Interrupt

Other Parts Discussed in Thread: TM4C123GH6PM

TM4C is connected to my PC via USB. My aim is to light the leds of PF3-0, by pressing some keys on the keyboard such as x,y (using interrupts). I'm using the TexaSDisplay for serial communication. The code is below, and it is not working correctly. UART0 interrupts are not created.

I wasn't able find much info about, how to arm and clear the UART0 interrupt (all i can find is periodic and edge interrupt tutorials). I guess this is the root of the problem. Ofcourse I may have done other mistakes.Could you please help me? Thank you.

#include <tm4c123gh6pm.h>

void EnableInterrupts(void);  // Enable interrupts
void WaitForInterrupt(void);  // low power mode

//------------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
    
  //setting up UART0 for interrupt
  NVIC_PRI1_R = 0x00004000; //set UART interrupt priority to 2 (set bits are between 13-15)
  NVIC_EN0_R = 0x00000020; //set bit 5
  EnableInterrupts(); //global enable
}

//enable the PF3-1 leds void PortF_Init(void) { volatile unsigned long delay; SYSCTL_RCGC2_R |= 0x00000020; // 1) F clock delay = SYSCTL_RCGC2_R; // delay GPIO_PORTF_CR_R |= 0x0E; // allow changes to PF3-1 GPIO_PORTF_AMSEL_R &= 0x00; // 3) disable analog function GPIO_PORTF_PCTL_R &= 0x00000000; // 4) GPIO clear bit PCTL GPIO_PORTF_DIR_R |= 0x0E; // 5.2) PF3-1 output GPIO_PORTF_AFSEL_R &= 0x00; // 6) no alternate function GPIO_PORTF_DEN_R |= 0x0E; // 7) enable digital pins PF3-PF1 } //------------UART_InChar------------ // Wait for new serial port input // Input: none // Output: ASCII code for key typed unsigned char UART_InChar(void){ while((UART0_FR_R&UART_FR_RXFE) != 0); return((unsigned char)(UART0_DR_R&0xFF)); } //------------UART_OutChar------------ // Output 8-bit to serial port // Input: letter is an 8-bit ASCII character to be transferred // Output: none void UART_OutChar(unsigned char data) { while((UART0_FR_R&UART_FR_TXFF) != 0); UART0_DR_R = data; } //UART interrupt handler, probably clear flag is missing void UART0_Handler(void) { unsigned char input=UART_InChar(); if(input=='x') { GPIO_PORTF_DATA_R |= 0x02; }else if(input=='y') { GPIO_PORTF_DATA_R |= 0x04; } }
//the main program int main(void) { UART_Init(); PortF_Init(); while(1) { WaitForInterrupt(); } }

  • Hello Emrah

    The UARTIM register bits are not set. Hence the UART is not generating an interrupt. Secondly how is the UART0 Handler mapped. I hope you have added it to the interrupt vector table in the startup file

    Regards
    Amit
  • Thank you Amit

    I have checked TM4C123GH6PM data sheet after your comments. Please note that I'm quite a beginner in electronics and microcontrollers, please correct me if I do a mistake and miss something:

    1) If one wants to get interrupts when data is recieved from the serial port, i think setting the RXIM bit (UART Receive Interrupt Mask) of the UARTIM register is enough.
    I have added the line "UART0_IM_R = 0x00000010;" where UART is initialized. (UART0_IM_R has mem. add. 0x4000C038, same with UARTIM)

    2) RXIC bit (Receive Interrupt Clear) of the UARTICR register has to be set as 1, in order to clear the flag caused by the data receive. Hence I have added "UART0_ICR_R = 0x00000010;" inside the UART0_Handler function.

    Still my code doesn't work. Can't get into UART0_Handler function (checking it with global variables).

    startup.s file is the one comes with the Prof. Volvano's edX course. I don't have any idea what is going on inside, since it is written in assembly. It begins with "startup_rvmdk.S - Startup code for use with Keil's uVision." If you want to have look, I'm adding the link of the file: http://textuploader.com/pxux
  • Hello Emrah,

    You have enabled the FIFO. When the FIFO is enabled the RXMIS bit is set when the FIFO level is crossed. You must instead use the RT (receive Timeout) for the CPU to know that there is data in the FIFO

    Regards
    Amit
  • And "long live" the use of DRM - and the "guaranteed" delays & uncertainties which such (over) usage creates.

    Adding complexity when it's not needed - blocks rather than assists, "Real World Problem Solving!" We in small tech biz "delight" at such practices.... (NOT!)
  • Could you please be more specific?
  • After your suggestions interrupt routine is now at least reacting to messages. But currently have problems with correctly reading the input data. I hope I will fix it too. I have seen the light at the end of the tunnel :)

    Thank you Amit

  • Hello Emrah,

    I believe that to be due to faulty Baud rate. The calculations indicate a 50MHz clock but in the code I do not see 50Mhz being generated

    Regards
    Amit
  • On a previous version of the program there was a phase locked loop function (it is no more). Clock speed on the program above is actualy default value 16 MHz, not 50 MHz as you stated.

    In addition, I have changed the Baudrate 19200 for safety (hence changing the IBRD - FBRD values too). I had problems with 115200 previously because of a bug in LibSerial in Ubuntu.

    Now the code works. Thanks alot!!
  • emrah erden said:
    Could you please be more specific?

    "DRM" abbreviates, "Direct Register Macro" which appears the predominant code style w/in this post.

    DRM forces an exacting read/review of the MCU manual - and (often) the interpretation of the handling of highly critical bits.    This predictably adds great time & effort to your programming exercise - and "unduly" complicates (and limits) the efforts of those trying to assist.

    Vendor here supplies a powerful & comprehensive code API library - which manages most all such critical set-ups & configurations precisely - and is far more familiar & usable - by ALL here.  (to include vendor's Amit)

    As small tech biz owner - we do not allow use of DRM unless a very strong case can be made.     I believe I know why some schools employ DRM - and myself/other small biz owners I know/meet - are NO fans!

    We're far more attracted to (thus hire/intern) those w/"real world" problem solving skills - which often involve, "Exploiting every advantage available" - so that the mission is accomplished, "Quickly, correctly and with the most proven (i.e. robust) solution!"     DRM fails - on all such counts!

  • Before your last post I was thinking DRM was "Digital Rights Management" (!!). I was asking to myself, gosh did I make a mistake to send startup file link to the public forum :))

    Anyway, I didn't have much choice since the course I have taken in edX use DRM aproach you mentioned. But I will try to check the API libraries. Thanks alot for the very illuminating and informative post. Wish I had learned these before.
  • Hello Emrach,

    As cb1 has stated and a more number of posts on the forum, using DRM is discouraged as code readability, and maintenance is extremely tough for large projects.

    Regards
    Amit
  • My friend - you have a "great" attitude - usually that (along w/hard work, luck) will take you far.     (my wish - for you)

    While you might have "no choice" during that (hopefully brief) course - nothing prevents you from employing (both) API and (dreaded) DRM method - and submitting (only) the DRM versions.     (by starting w/API - you'll quickly/easily discern which registers are employed - and the correct "sequence" of their use - as well as any "unexpected or complex set-ups.")      Little of that (i.e. none) is likely revealed by "near blind read" of the MCU's Register detail - which limits views to one single register at a time - and too often "hides" critical register inter-relationships.

    Indeed one should have general understanding of MCU registers - yet when a, "Tried/proven and extensive API library" stands at the ready - relying upon DRM solely - cannot be justified when/where "code efficiency" is required.    (i.e. might that be "everywhere?")   

    Getting to market "last" - or frustrating clients w/"unnecessary" delays (both caused by "DRM only" efforts) - may not provide the best road to a, "gainfully employed" tech future!

  • Dear cb1_mobile

    I had no idea about DRM, and API if you didn't write about them. This is an other great post to learn new terms and pratical aproaches.

    What I believe is, taking suggestions from an expert in the industry is always the best an engineer can get. Your experience and suggestions highly respected and valued by me.  I will work hard to learn the proven API library and build my prototype with it (to save time, customers and of course money). Bytheway which source do you suggest to start learning?

    Thanks for everything :)


    With my Best

    Emrah

  • emrah erden said:
    taking suggestions from an expert in the industry

    Smart, skilled staff here - reading the above - cry out (together), "Who is that?"   (while rolling their eyes...)

    I do try to (reasonably) represent industry's, "Needs and hopes - for new users such as you" - as too rarely are these properly & forcefully presented.

    This vendor has done a very good job in producing a high volume of useful user data.    That said - their organization & links to that data - may not be, "best/brightest."    (i.e. "signposts" have "gone missing" - or are illegible - or are widely scattered - immensely complicating search/find - deploy!)

    Vendor's skilled guide "Amit" is best able to guide you - my value is likely best confined to "squawking" & challenging methods & practices which run on inertia - and have not been well considered nor optimized...    (despite the delightful, and always most welcome, influx of "NIH" suggestions...)

    Thank you and bon chance, mon ami.