Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12608 Discussions

Speed! when reading from the serial port

Altera_Forum
Honored Contributor II
1,132 Views

Hello folks,  

 

 

I am reading messages from the UART at 230400 8-N-1. The messages are 41-byte long. The whole message takes about 2ms to be sent into the UART. However, when I do fscanf, fgets, or getchar, etc... it is really slow. The whole message is being read in almost 40 ms. This means every character is read at almost 1 ms. That is too slow! It doesn't matter that I increase the Baud rate for the UART if it will be read at that same rate by the OS. 

 

Does anyone know if this can be changed? I believe this is within the structure of the OS and don't know if it can be changed somewhere. The latency for the UART is too big.  

 

Any ideas will be GREATLY appreciated.  

 

Regards,  

 

Francisco
0 Kudos
9 Replies
Altera_Forum
Honored Contributor II
498 Views

Supposedly, speed is not the problem but latency. Speed can't be increased, as (I believe) the UART driver is done in a quite optimum way. 

Do you use the FifoedUart IPCore ? The AlteaUART needs a too low latency interrupt service, which Linux can't provide. The hardware Fifo will relax the problem. 

-Michael
0 Kudos
Altera_Forum
Honored Contributor II
498 Views

Hello Michael, 

 

<div class='quotetop'>QUOTE (mschnell @ Jul 11 2009, 02:18 AM) <{post_snapback}> (index.php?act=findpost&pid=23075)</div> 

--- Quote Start ---  

Supposedly, speed is not the problem but latency. Speed can&#39;t be increased, as (I believe) the UART driver is done in a quite optimum way.[/b] 

--- Quote End ---  

 

 

Indeed, its performance is remarkable. 

 

<div class='quotetop'>QUOTE </div> 

--- Quote Start ---  

Do you use the FifoedUart IPCore ? The AlteaUART needs a too low latency interrupt service, which Linux can&#39;t provide. The hardware Fifo will relax the problem.[/b] 

--- Quote End ---  

 

 

 

I am already using the FIFOed UART. I am making the measurements in the TX and RX pins. The message arrives and is registered correctly. My problem is the correct message takes too long to be read.  

 

Yesterday, when modifying some things about Processor type and features I noticed parameters that might be useful. However, didn&#39;t move anything to leave my OS alive but there was , for example, a parameter corresponding to the timer frequency. It says 100 Hz. But isn&#39;t this "timer" actually timer_0 (I am using a DE2)? Shouldn&#39;t it be 1 KHz since the timer is configured for every 1 ms? 

 

I don&#39;t think the problem is within the driver of the UART. The FIFOed one has proven very useful. I believe the peed that must be improved must be in the OS or in management of some sort.  

 

 

Regards, 

 

Francisco
0 Kudos
Altera_Forum
Honored Contributor II
498 Views

You should use an /f processor and a decent amount of both data and instruction cache.  

 

-Michael
0 Kudos
Altera_Forum
Honored Contributor II
498 Views

<div class='quotetop'>QUOTE (mschnell @ Jul 11 2009, 09:10 AM) <{post_snapback}> (index.php?act=findpost&pid=23078)</div> 

--- Quote Start ---  

You should use an /f processor and a decent amount of both data and instruction cache.  

 

-Michael[/b] 

--- Quote End ---  

 

 

 

Hello Michael,  

 

Yes, I am using an /f processor. I&#39;ll read the manual on how to increase data and instruction cache because I&#39;m not sure what size my hardware config has. Will let you know about the results.  

 

Thank you very much 

 

Francisco
0 Kudos
Altera_Forum
Honored Contributor II
498 Views

Hello again,  

 

I am trying to increase the data and instruction cache to test if the speed increases when reading data from the UART. At first, I tried making it four times bigger, Quartus said it couldn&#39;t fit into the device, so I eliminated the VGA and Audio modules in SOPC (I am not using them). After that I increased Instruction Cache to 8 KBytes, and Data Cache to 4 KBytes, which is the double of what I had by default.  

 

Quartus compiles the project succesfully, and everything is loaded into the FPGA. However, when I open nios2-terminal it never boots correctly, It just freezes after "Uncompressing........done" And then nothing happens. I don&#39;t know if I should change something in the kernel or app config. Because I&#39;ve tried so far with different combinations of Data and Instruction Cache sizes, but none of them boots correctly, except for the default values (4 K Instruction and 2K data). 

 

Any ideas on this please? :S 

 

Regards, 

 

Francisco
0 Kudos
Altera_Forum
Honored Contributor II
498 Views

I doubt that the cache size itself could prevent the system from, booting, so something else will be the culprit.  

 

-Michael
0 Kudos
Altera_Forum
Honored Contributor II
498 Views

Please post the lines in your code where you open/configure/read the uart, it might be helpful. 

 

Cheers, 

 

Ricardo.
0 Kudos
Altera_Forum
Honored Contributor II
498 Views

<div class='quotetop'>QUOTE (jasinski @ Jul 14 2009, 07:36 AM) <{post_snapback}> (index.php?act=findpost&pid=23103)</div> 

--- Quote Start ---  

Please post the lines in your code where you open/configure/read the uart, it might be helpful. 

 

Cheers, 

 

Ricardo.[/b] 

--- Quote End ---  

 

 

 

Hello Ricardo,  

 

Below are the lines I currently use to read form the port. The line is 42 characters long. I&#39;ve already tried with fgets, and changing the number of characters read simultaneously. The thing is, the OS always takes about 1 ms per character reading, regardless of the method used. That is why I was suggested to increase the cache memory to see if it read the port a little faster. I need it to read at most at 0.25 ms per character. 

 

Any ideas are greatly appreciated. 

 

 

FRancisco 

 

void buffer2 (void) { //Declaración de las variables que se usarán -  Variables used int contador=0; char retraso = 0;      //caracter que viene desde el serial char c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21; printf("buffer2\n"); archivo_log = fopen ("/logs/buffer2.txt","w");                  while(contador <= 1320)         {                  fscanf(serial_port,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",&c1,&c2,&c3,&c4,&c5,&c6,&c7,&c8,&c9,&c10,&c11,&c12,&c13,&c14,&c15,&c16,&c17,&c18,&c19,&c20,&c21);         fprintf(archivo_log,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",c1,c2,c3,c4,c5,c6,c7,c8,c9,c10,c11,c12,c13,c14,c15,c16,c17,c18,c19,c20,c21);         contador = contador + 1;         }                  fclose( archivo_log );                captura2 = fopen("/captura2Lista","w");         while(retraso <= 25)         {             retraso = retraso + 1;         }         fclose(captura2); //printf("termina rutina_buffer2\n"); }
0 Kudos
Altera_Forum
Honored Contributor II
498 Views

I can&#39;t see where in your code you are opening the serial port. In order to make it easier for people wanting to help, you could post the smallest/simplest code that reproduces the problem you are seeing. That would be a simple main() function including the places where you open the uart, configure it and read the characters. Please do that if you can. 

 

From what you&#39;ve said in your previous posts, it doesn&#39;t seem that the OS is guilty about this delay. You say that the whole message is seen on the wire for 2 ms (I&#39;m assuming you&#39;ve seen that with a scope); after that period, the bytes were probably already received by the OS and placed in its rx buffer. So the only thing that could be slow is getting them from this buffer, which is done by the C functions you use for it (fscanf, etc.).  

 

My first try would be to read the incoming bytes with fread(). Of course you should declare a buffer to hold the data in advance: 

 

<div class='codetop'>CODE</div><div class='codemain' style='height:200px;white-space:pre;overflow:auto'> char buf[1024]; 

int bytesCount = read(fd, buf, 1024);</div> 

 

Good luck, 

 

Ricardo.
0 Kudos
Reply