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.

EK-TM4C123GXL: My code runs very slow when I implement multiple uarts

Part Number: EK-TM4C123GXL

Hello, so basically I was using UART0 on pins A0,A1 for monitoring my data with serial monitor, and I then initialized UART1 on pins PC4, PC5 to send data to A6 module and it worked fine. 
for some reasons I need another UART so I initialized UART2 on pins PD6, PD7. The thing is my uC seems receiving information but not sending so I unlocked the port, it started sending data but it takes really long time like 2 mins to send the data, I don't understand the reason behind this. 

These are the important chunks of my code 

int main(void){
int i,j;unsigned char b,N; char character; char end_c[2]={0x1a,'\0'};
unsigned char AT_response[100]; unsigned char test[5]="hello";

UART_Init(); // initialize UART
UART_Init1(); // initialize UART1
UART_Init2(); // initialize UART2

while(1)
{

here is my code

}

and the init functions are the following : 

void UART_Init(void){
// as part of Lab 11, modify this program to use UART0 instead of UART1
// switching from PC5,PC4 to PA1,PA0
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 = 43; // IBRD = int(80,000,000 / (16 * 115200)) = int(43.402778)
UART0_FBRD_R = 26; // FBRD = round(0.402778 * 64) = 26
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,PA0
GPIO_PORTA_DEN_R |= 0x03; // enable digital I/O on PA1,PA0
// configure PA1,PA0 as UART0
GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00)+0x00000011;
GPIO_PORTA_AMSEL_R &= ~0x03; // disable analog functionality on PA1,PA0
}


void UART_Init1(void){
SYSCTL_RCGC1_R |= SYSCTL_RCGC1_UART1; // activate UART1
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOC; // activate port C
UART1_CTL_R &= ~UART_CTL_UARTEN; // disable UART
UART1_IBRD_R = 520; // 80,000,000/(16*115,200)) = 43.40278
UART1_FBRD_R = 53; //6-bbit fraction, round(0.40278 * 64) = 26
UART1_LCRH_R = (UART_LCRH_WLEN_8|UART_LCRH_FEN);
UART1_CTL_R |= UART_CTL_UARTEN; // enable UART
GPIO_PORTC_AFSEL_R |= 0x30; // enable alt funct on PC5-4
GPIO_PORTC_DEN_R |= 0x30; // enable digital I/O on PC5-4
// configure PC5-4 as UART1
GPIO_PORTC_PCTL_R = (GPIO_PORTC_PCTL_R&0xFF00FFFF)+0x00220000;
GPIO_PORTC_AMSEL_R &= ~0x30; // disable analog functionality on PC5-4
}

void UART_Init2(void){
volatile unsigned long delay;
// as part of Lab 11, modify this program to use UART0 instead of UART1
// switching from PC5,PC4 to PA1,PA0

SYSCTL_RCGC1_R |= SYSCTL_RCGC1_UART2; // activate UART2
SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOD; // activate port D
delay = SYSCTL_RCGC2_R; // allow time for clock to start
GPIO_PORTD_LOCK_R = 0x4C4F434B; // 2) unlock GPIO Port D
GPIO_PORTD_CR_R = 0x80; // allow changes to PD7
UART2_CTL_R &= ~UART_CTL_UARTEN; // disable UART
UART2_IBRD_R = 520; // IBRD = int(80,000,000 / (16 * 115200)) = int(43.402778)
UART2_FBRD_R = 53; // 8 bit word length (no parity bits, one stop bit, FIFOs)
UART2_LCRH_R = (UART_LCRH_WLEN_8|UART_LCRH_FEN);
UART2_CTL_R |= UART_CTL_UARTEN; // enable UART
GPIO_PORTD_AFSEL_R |= 0xC0; // enable alt funct on PD6,PD7
GPIO_PORTD_DEN_R |= 0xC0; // enable digital I/O on PD6,PD7
// configure PD7,PD6 as UART2
GPIO_PORTD_PCTL_R = (GPIO_PORTD_PCTL_R&0x00FFFFFF)+0x11000000;
GPIO_PORTD_AMSEL_R &= ~0xC0; // disable analog functionality on PA1,PA0
}

  • user5175619 said:
    . The thing is my uC seems receiving information but not sending so I unlocked the port, it started sending data but it takes really long time like 2 mins to send the data,

    Why is this a long time (there's a lot of information missing)?

    user5175619 said:
    These are the important chunks of my code 

    Or not.

    user5175619 said:
    void UART_Init(void){
    // as part of Lab 11, modify this program to use UART0 instead of UART1
    // switching from PC5,PC4 to PA1,PA0
    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 = 43; // IBRD = int(80,000,000 / (16 * 115200)) = int(43.402778)
    UART0_FBRD_R = 26; // FBRD = round(0.402778 * 64) = 26
    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,PA0
    GPIO_PORTA_DEN_R |= 0x03; // enable digital I/O on PA1,PA0
    // configure PA1,PA0 as UART0
    GPIO_PORTA_PCTL_R = (GPIO_PORTA_PCTL_R&0xFFFFFF00)+0x00000011;
    GPIO_PORTA_AMSEL_R &= ~0x03; // disable analog functionality on PA1,PA0
    }

    This is all DRM. Use TIVAWare. You are far more likely to get help and to get something that works.

    Also use past code (the </> icon in the advanced formatting). Just direct pasting the code loses all your formatting and makes the code much more difficult to read.

    user5175619 said:
    volatile unsigned long delay;

    This is unnecessary. As long as

    user5175619 said:
    SYSCTL_RCGC2_R
    is declared as volatile then

    (void)SYSCTL_RCGC2_R;

    performs the same function as

    user5175619 said:
    delay = SYSCTL_RCGC2_R; // allow time for clock to start
    w/o requiring extra stack space. If SYSCTL_RCGC2_R is not declared as volatile then you have a bug.

    Robert

  • Answering your first question : Taking too long time because when I replace uart2 with uart1 it works in less than a second or in ms maybe, the problem is code takes really long when it goes to check the flag if fifo is full or no, then it should send data if it's not filled completely. the thing is same algorithm worked fine for both UART0 AND UART1 but it is not working for UART2

    void UART_OutChar2(unsigned char data){
    while((UART2_FR_R&UART_FR_TXFF) != 0);
    UART2_DR_R = data;
    }




    1- Sorry what do you mean by DRM? Tivaware you mean the driver library? if yes sure I can use it but I have deadline and I am using the start up file of Jonathan Valvano and shape the world cuz I was following the course in my internship.
    2-I got the volatile thing but still this is not the issue, I still have more than 30K free stack but yea will consider it
    3- What do you mean by (</>) please explain to me.
    I am still not aware why uart 2 is not working and I am out of solutions for now

  • user5175619 said:
    Answering your first question : Taking too long time because when I replace uart2 with uart1 it works in less than a second or in ms maybe,

    What is taking too long and how have you measured it?

    user5175619 said:
    Sorry what do you mean by DRM?

    Direct Register Manipulation

    user5175619 said:
    if yes sure I can use it but I have deadline and I am using the start up file of Jonathan Valvano and shape the world cuz I was following the course in my internship.

    Jonathan has led you astray. If you use DRM you first have to re-invent TIVAWare.

    user5175619 said:
    2-I got the volatile thing but still this is not the issue

    I didn't expect it to be, it's just not particularly clean code.

    user5175619 said:
    3- What do you mean by (</>) please explain to me.

    Icon in the upper right corner of the advanced editing box. It's a icon showing a window with </> in it. Click on that then click in the window that pops up to past your code. Most of the time that will provide line numbers and syntax highlighting. Even when it doesn't it preserves the white space in your code, making it possible to at least read it.

    user5175619 said:
    I am still not aware why uart 2 is not working and I am out of solutions for now

    I would say there's 80% odds that if you switch to TIVAWare it will work immediately and if it doesn't then you will at least have some code that others will be willing to provide some critique and suggestions for.

    Robert

  • It would appear that, "Shape the world" may better be described as, "Shape a "well-worn" trail (here)!"      (here - where hapless course attendees - in great abundance - are (almost) certain to land - due to their (enforced) rejection of the "Tried/Tested/Proven/EFFICIENT API!"

    Why is it that the professor is not tasked to "assist?"      (it is his "direction" that lands (so many) here - is it not?)       His arrival/assistance/justification of the archaic -  would be considerably more "world-shaping" - would it not?

  • Thanks for help, I already saw many versions of TIVAWare, mainly I will be using UARTS, CAN, Timers, I/O digital pins, maybe interrupts and ADC, so which version I need in this link http://www.ti.com/tool/SW-TM4C ? I guess the 2nd one?
    and if u can save my time by giving me me instructions how to install it on windows or how to use the functions until I figure out myself, I know I should search but my deadline is very near and I am trying

    Answering your question how did I measure time : I send data to A6 module using UART1 and I monitor data using UART0, so when I send data I receive the echo and response on my serial monitor, I need more 3 uarts to obtain data from 3 devices through modbus basically so I can upload data through A6 module.
    so basically I will need to configure uart0, 1, 2, 3, 4 and 5

    and I've a very important question : for an embedded software engineer, is it good to use libraries like Tivaware? I think I should be able to design my code efficiently from scratch because I use multiple brands of microcontrollers. and btw when I designed my uart, I made some changes to make it suitable for my project, regular blocking and non blocking uart will not be a good thing for me unless maybe I've interrupt uart enabled.

  • it's too late and I am a beginner on my 1st internship and I am working on that project since 2 months so I didn't know, I think even in his start up file uart2 should work but I am unable to make it work.
    I will leave him a question for sure I was planning to do that but I thought here they have the same method of coding Tiva C.
    pls help if u can.
  • Feel your pain - really - both Robert & I DO...

    Can you "escape" from that particular UART - employ another - and report if the same situation (excessive DELAY) continues?     It is doubted that either Robert or I have (ever) encountered, "such an issue!"    

    And - due to the enforced use of "DRM" - both his (and my) response is delayed - made unnecessarily more complex - thus will be harder & will take longer - and for what?       (we'd LIKE to assist - yet the archaic method "in play" (DRM) effectively BLOCKS our (and vendor's) assistance...      

    And really - what "insights into the hardware" (alleged justification for such DRM usage) have been gleaned?      (and - if (some) FEW were - such could have been grasped in far more detail - and far faster - by simple READ of the highly detailed MCU Manual - Peripheral Chapters!

  • Other uarts don't work at all :/ I need to make sure my initialization function has nothing wrong so I can have another guessing
  • user5175619 said:
    Thanks for help, I already saw many versions of TIVAWare, mainly I will be using UARTS, CAN, Timers, I/O digital pins, maybe interrupts and ADC, so which version I need in this link http://www.ti.com/tool/SW-TM4C ? I guess the 2nd one?

    Looks like it to me. I think the first just combines the rest. Should have someone else confirm though.

    user5175619 said:
    Answering your question how did I measure time : I send data to A6 module using UART1 and I monitor data using UART0, so when I send data I receive the echo and response on my serial monitor,

    OK, if I understand correctly you have too may things involved for good measurement of time of your routine. Better just to toggle a pin when you send and compare the pin toggle to when the bit stream shows up on the 'scope (or logic analyzer)

    user5175619 said:
    and I've a very important question : for an embedded software engineer, is it good to use libraries like Tivaware? I think I should be able to design my code efficiently from scratch because I use multiple brands of microcontrollers.

    A decade ago I would answer very differently and I was skeptical of TIVAWare as well. Indeed I used it with some trepidation. The available libraries were either too high a level of abstraction and inflexible so they could only solve a particular kind of problem in a particular way or they were such small skins over the hardware (such as a function for each bit) that they were not of a great deal of use. In addition they were buggy and not well supported.

    TIVAWare has hit the right level of abstraction. It hides levels of detail that are not relevant (like making sure access order is properly managed where it matters) without forcing a particular model of access upon you (IE it doesn't force you to use DMA if it is not useful for your problem). It also allows you do add DRM code when and where needed if the library does not support a particular kind of use, the writers have not assumed that they know all possible ways the hardware can be used.

    I understand other modern ARM micro libraries have similar characteristics. As such migrating between libraries should be of no greater difficulty than migrating between the direct register implementations of the various vendors and indeed probably easier since extraneous details are likely to remain abstracted away for you.

    That's not to say TIVAWare is without issues. I've taken TI to task over several items (no type safety, too much reliance on 32 bit ints etc...) but it is still an efficiency and quality improvement over DRM.

    user5175619 said:
    regular blocking and non blocking uart will not be a good thing for me unless maybe I've interrupt uart enabled.

    The library is completely agnostic here. It really doesn't care what model you are using any more than the DRM approach does. That's one of its strengths.

    Robert

  • Robert's suggestion of, "GPIO Pin Toggle" as a superior (& easier) method to note UART transmission (and likely reception, as well) is a far "safer and more practical/direct means" - to confirm that your "description of delay" proves a "real" - and not "phantom" issue.
  • okay pls guide me what source I should follow to install the library into KEIL compiler asap and how to learn how to use the library in the fastest way? thanks a lot in advance
  • sorry but I am now curious to know the DRM method, I am a fresher and I have big project which requires good memory optimization I don't wanna use libraries for now. Plus I can use libraries maybe in the future but now I wanna build fundamentals.
    Any illustration of initialization of UART2? pls if u didn't try can you or anyone try to initialize it using DRM and make sure if it works?
  • You (continue) to "cling" to (likely) "mistaken hopes/dreams" that (somehow) - "DRM will lead you to the promised land! (which now has extended to, "Good memory optimization.")

    You, "Don't wanna use" API - and it remains HIGHLY Doubtful that Robert, Vendor or I "wanna use DRM!"      (we have (some) regard for, "Speed, Ease, Efficiency" - none of those offered up by "DRM")

    Why is not the person "Pushing for DRM" pressed into service?       Is it not he/she who CAUSED your (DRM launched) misfortune?

  • user5175619 said:
    sorry but I am now curious to know the DRM method,

    Not a bad thing, just not something to indulge in when faced with a deadline when there are faster alternatives.

    user5175619 said:
    I am a fresher and I have big project

    A very good reason to avoid DRM

    user5175619 said:
    I have big project which requires good memory optimization

    That cannot be a reason to avoid TIVAWare in favour of DRM

    Everything that you would use in TIVAWare, you would otherwise have to write yourself. It's highly unlikely that you could write code that takes less room while maintaining quality (and a good chance you could not even if you sacrificed quality). The permanent RAM footprint is close to zero. The flash footprint can be quite small if you choose to use the ROM based functions (via the MAP prefix), even if you don't I would expect the footprint to be close to what you write and quite possibly less. Having said that you have a short term project on a micro with a lot of memory, any memory issues you have will not be significantly effected by TIVAWare, your own design choices are what will make a difference.

    user5175619 said:
    Plus I can use libraries maybe in the future but now I wanna build fundamentals.

    Not finish your project?

    user5175619 said:
    Any illustration of initialization of UART2?

    There are examples with TIVAWare.

    user5175619 said:
    pls if u didn't try can you or anyone try to initialize it using DRM and make sure if it works?

    Nope. A few people have found it necessary to add DRM in order to enhance the TIVAWare API (I can think of an area or two where it might be needed as well) but I don't think anyone here uses it regularly, particularly for initialization.

    Robert

  • Dare it be noted that poster has, "Drinken deeply" from (someone's) Kool-Aid - and attaches (great) status to "DRM" (even w/out knowing its meaning, definition - or (likely) consequences!)

    Still - the "purveyor" of poster's Kool-Aid should justify such "DRM direction!"      Notable that he/she "is seldom seen here" (where "so many" wash ashore, de-masted) - while crack volunteer staff devotes time/effort to, "employ fact/reason" as the best disinfectant to, a proven, distasteful - unjustifiable "concoction!"

  • I actually posted a question on his forum but no one viewed or answered, I don't know why is he not responding.
    it's not about him, it's about embedded industry, I know people who work in Valeo, Mentor Graphics and other companies they never use libraries they use DRM and it works perfectly. the thing is I just want to know the UART2 DRM initialization part which is like 5 lines of coding or at least why is it not working.

    Today I went through the TIVAWare datasheet and I already understand what's going on slightly, but trust me for someone in my position I never want to rely on this because I really can't (I wish I can).

    I am not refusing, I am just being practical and aware of what I need for now.
  • As one who has made (most) of his living "in/around" embedded - and also "knows people w/in the industry" - I've noted that (anything) which, "Speeds, Eases, Enhances" Design and/or Project progress - is (much) preferred.

    In the specific case - here/now - unless the API fails to, "Cover a highly specific device requirement and/or performance level" the API has been identified as (by far) the "Code vehicle of choice!"      And this by: "Vendor, Experienced Users, and Beginners/Students!"        

    In stark contrast, "DRM/ASM" prove, "FAR from working perfectly" - instead are "error prone" - delaying - and "indecipherable w/in days" (often just hours) after writing!       And - despite your "wish/hope otherwise" - your experience (project in near total shambles) has completely confirmed the known, "Pitfalls of DRM!"

    It has been noted that you make statements, (i.e. "because I really can't" - yet never go on to "explain or justify" said statement.    This makes it difficult for others to grasp - especially accept, "How you came to such conclusion."

    As to practical - you've "crashed/burned - washed ashore here" (as have so many of your DRM peers) - and yet (still) reject the KNOWN, SAFE, SPEEDY/PROTECTED API path/harbor.     Can that API "rejection" be characterized - by any "way/means" - as "practical?"

    Those responding to you here, "Want you/similar others to succeed!"      And that proves best through "considered LOGIC & the "wealth of past user success" - realized via the API" - over "wish/hope" and other (unexplained/questionable) conclusion...

  • user5175619 said:
    it's about embedded industry,

    There is no such thing as the embedded industry. Embedded work covers a wide range of processors (from 4 bit masked rom to multiprocessor behemoths).

    user5175619 said:
    I know people who work in Valeo, Mentor Graphics and other companies they never use libraries they use DRM and it works perfectly.

    I know there are people who work exclusively in assembly and others that work in forth. Both are rather narrow niches.

    As far as Mentor using DRM I'd ask which processors. It's still the only real way of dealing with some processors since there is no equivalent to TIVAWare and one must do what needs to be done. However, where something like TIVAWare exists taking the effort to re-invent appears tedious, error-prone and foolish.

    user5175619 said:
    the thing is I just want to know the UART2 DRM initialization part which is like 5 lines of coding or at least why is it not working.

    You could always copy TIVAWare but it seems rather pointless.

    user5175619 said:
    Today I went through the TIVAWare datasheet and I already understand what's going on slightly, but trust me for someone in my position I never want to rely on this because I really can't (I wish I can).

    Reminds me of those who did not want to use C because 'they didn't want to rely on the compiler'. And you've presented no justification as to why you cannot rely on it. Especially as a student on a short deadline. Why do you want to take extra time to reproduce what's readily available and tested from the manufacturer and well supported?

    user5175619 said:
    , I am just being practical and aware of what I need for now.

    You said you had to produce a large project in a short period of time. It rather strikes me that the practical approach is to grab the existing interface code that works with thanks and get on with the meat of your work.

    user5175619 said:
    I just want to know the UART2 DRM initialization part which is like 5 lines of coding

    Without checks and comments I would estimate you are off by at least a factor of 2, probably more. And that will be error-prone code. Do you have the TDD framework set up to test it?

    Robert

  • cb1_mobile said:
    As one who has made (most) of his living "in/around" embedded - and also "knows people w/in the industry" - I've noted that (anything) which, "Speeds, Eases, Enhances" Design and/or Project progress - is (much) preferred.

    Especially when it's free. People will balk at high priced tools but free, efficient, supported, debugged libraries? I don't know of any who would balk at that, once assuring themselves that it was indeed as advertised.

    Robert

    Note that even if the particular company had an abstraction layer that all their products used, writing the abstraction layer to TIVAWare rather than the register set would still be a gain.

  • Robert Adsett said:
    user5175619
    I just want to know the UART2 DRM initialization part which is like 5 lines of coding

    An additional note on productivity (and why debugged libraries are worth using). The oft quoted rate for code is 8 to 16 SLOC (Source Lines Of Code) per day1. Some people will do less, some more. Student project can be more 'productive' since they are not producing a product but just rough prototypes. Since you are producing a large project on a short time frame you might want to consider this in your estimation of the effort you want to put into DRM init (I would be surprised if you are approaching the low end of that estimate currently).

    Now lest you think that estimate is too pessimistic SEI (Software Engineering Institute of Carnegie Mellon University) has studied a large number of projects and published the report2. Their results are interesting.

    •  real-time software  1.5 months per KESLOC
    •  engineering software  1.3 months per KESLOC
    •  mission support software  1.3 months per KESLOC
    •  automated information system  1.1 months per KESLOC

    KESLOC (Thousand Equivalent Source Lines Of Code), months is person-months. I suspect your project would fall under what they labeled as engineering software. Now this does vary with project size and with the organization. They have some graphs to show that.

    Lower person months per KESLOC is better.

    Note that these are log/log plots and also note that the disparity between the best and worst performers.

    So even given that the source of this data (pretty much by necessity) is government contract how well do you think you can compare? SEI is quite open about their sources and methods and do comment about why they chose the data set they did and the effect it may have on the conclusions.

    Robert

    1 - https://betterembsw.blogspot.ca/2010/05/only-10-lines-of-code-per-day-really.html

    2 - https://resources.sei.cmu.edu/asset_files/WhitePaper/2015_019_001_453264.pdf