Path: news.mathworks.com!not-for-mail
From: "Benjamin " <benjaminst@gmail.com>
Newsgroups: comp.soft-sys.matlab
Subject: Re: Time elapsed using serial fwrite and fread functions
Date: Wed, 12 Nov 2008 08:58:03 +0000 (UTC)
Organization: Universidad Polit&#233;cnica de catalu&#241;a
Lines: 88
Message-ID: <gfe5qr$r0b$1@fred.mathworks.com>
References: <gfcvsa$dpv$1@fred.mathworks.com> <gfdaf2$el3$1@fred.mathworks.com>
Reply-To: "Benjamin " <benjaminst@gmail.com>
NNTP-Posting-Host: webapp-02-blr.mathworks.com
Content-Type: text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding: 8bit
X-Trace: fred.mathworks.com 1226480283 27659 172.30.248.37 (12 Nov 2008 08:58:03 GMT)
X-Complaints-To: news@mathworks.com
NNTP-Posting-Date: Wed, 12 Nov 2008 08:58:03 +0000 (UTC)
X-Newsreader: MATLAB Central Newsreader 1190546
Xref: news.mathworks.com comp.soft-sys.matlab:500357


"Trent Jarvi" <tjarvi@mathworks.com> wrote in message <gfdaf2$el3$1@fred.mathworks.com>...
> 
> "Benjamin " <benjaminst@gmail.com> wrote in message 
> news:gfcvsa$dpv$1@fred.mathworks.com...
> > Hi all!
> >
> > Currently I am working in my master thesis. It consists on implementing an 
> > arbitrary waveform generator using Matlab and a FPGA. So, I need to 
> > transfer signal samples (100Kbytes) from Matlab to a Altera Nios 2 
> > processor(85MHz) using a serial protocol (UART 115Kbps). I have done the 
> > following code which I have tested and it works ok:
> >
> > ....
> > tic
> > for i=1:length(data_to_send) %length(data_to_send)=100e3
> >    tic
> >    fwrite(s,data_to_send(i),'uchar');
> >    time_write(i)=toc;
> >    tic
> >    if(fread(s,1,'uchar')~=0)
> >        Error('Unexpected error while reading UART');
> >    end
> >    time_read(i)=toc;
> > end
> > time_elapsed=toc;
> > fprintf (1,'Elapsed CPU time (min) = %f\n',time_elapsed/60);
> > ...
> >
> > The main problem is that it is too slow (the elapsed cpu time is more than 
> > 30minutes!). I can't understand it because the expected time a grosso modo 
> > would be 100Kbytes/115.2Kbits/s=6,94seconds.
> >
> > I'm sure that the problem is not from Nios 2 processor because the main 
> > function is just a infinite loop that reads from the uart and send 0 
> > character.
> >
> > Does anybody know if there is a manner to increase the perfomance of the 
> > serial transmision? Thanks in advance,
> >
> 
> Hi Ben,
> 
> There is considerable overhead in the parameter checking for each function 
> call made.  You may want to reduce the number of calls if possible to 
> improve the throughput.  As an example, I performed the following on a 
> loopback connection to see how close MATLAB Serial was to the 6.94 seconds 
> (write) you mention:
> 
> chunk=5120;
> data_to_send=mod(1:102401,255);
> s=serial('/dev/ttyS0');
> s.outputbuffersize=chunk;
> s.inputbuffersize=chunk;
> s.baudrate=115200;
> fopen(s);
> tic;
> a=[];
> for i=0:length(data_to_send)/chunk-1
>     fwrite(s,data_to_send((1:chunk)+chunk*i),'uchar');
>     a = [a fread(s,5120,'uchar')'];
> end
> toc;
> fclose(s);
> delete(s);
> clear('s');
> 
> The result on R2008b was reasonable for a quick test:
> 
> Elapsed time is 9.361430 seconds.
> 
> Thats actually less time than one would expect for both read and write if 
> they did not overlap underneath. 
> 


Hi Trent!

Thank you very much for your feedback!!

I will try to configure output and input buffer size as big as possible and minimize fwrite/fread function calls to avoid overhead. 

Also I will configure the timeout because I suspect that maybe it could expires while Nios is reading from the output buffer and Matlab is blocked at fread waiting until input buffer is full.

I will inform you as soon I can try it!

Regards
Ben