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++

Read from UART

Altera_Forum
Honored Contributor II
3,235 Views

Good morning!  

I´m a NIOSII starter and trying to get first results. 

My question: Why can´t i read anything from the UART? I can write to it, but i don´t receive anything or i receive and don´t recognize it.  

I´m working with WIN2000, a DSP 1S25-Board, Quartus II 4.2, NIOSII 1.1. 

Because of tutorials i suppose that "normal" C should work with NIOSII (this is a fragment of the SW-Tutorial for writing to the UART and it works: 

// fp = fopen ("/dev/uart1", "w"); 

// if (fp) 

// { 

// fprintf(fp, "%s",msg); 

// } 

// fclose (fp); 

 

Now i want to read from it: 

 

// TRY TO READ FROM UART1 

// input = NULL; 

// input = fopen ("/dev/uart1", "r+"); 

// if (input) 

// { 

// printf("%c",fgetc(input)); 

// } 

// fclose(input); 

// input = NULL; 

 

But there is no reaction http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/huh.gif !?  

I am thankful for any ideas and please respect my beginner-status... 

Nice day! Max
0 Kudos
11 Replies
Altera_Forum
Honored Contributor II
1,773 Views

I have the sam problem.I think it is a bug niosII.only can transfer data to pc,but I can&#39;t  

receive data from pc. 

 

I want The altera can give me a definite explaination!!!
0 Kudos
Altera_Forum
Honored Contributor II
1,772 Views

Hi guys, 

 

Make sure that the baud rate of the PC serial port and the port on the fpga is the same. the same goes for the stop bits, parity and data bits 

 

We experienced data corruption at the standard speed (115200) over here, when using the serial cable that was supplied with our stratix II kit. So we lowered the speed to a save 9600 

 

I successfully use here: 

Baud: 9600 

Databits: 8 

Stopbit: 1: 

Parity: None 

Flowcontrol: none 

 

The speed of the port on the board can be set in the SOPC builder by doublicking on the UART item. You can choose to make it reconfigurable at runtime, but I haven&#39;t used that yet. 

 

 

Good luck! 

 

Jos
0 Kudos
Altera_Forum
Honored Contributor II
1,772 Views

And another thing, there seems to be something strange going on with the character read functions. In the "board diagnostics" sample program that comes with the NIOS II IDE, somewhere in the code, you will find: 

 

/* This extra getchar() to work around an Eclipse feature */ 

getchar(); 

ch = getchar(); 

 

I have no idea why this is an eclipse feature and not a nios Bug, but anyway... http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/blink.gif  

 

Maybe this is part of your problem?
0 Kudos
Altera_Forum
Honored Contributor II
1,772 Views

see "How to set stdin to nonblocking"  

 

I thought you might be interested in reading this web page:  

 

http://www.niosforum.com/forum/index.php?a...=st&f=17&t=1239 (http://www.niosforum.com/forum/index.php?act=st&f=17&t=1239)
0 Kudos
Altera_Forum
Honored Contributor II
1,772 Views

Thanks for your help! 

 

I modified my UART in SOPC-Builder, now it has the same properties (i took the values of jmastron) as my Hyperterminal (is there a better alternative?). Also i modified my source code , i took Fischers code example in the other thread about nonblocking stdin as base: 

 

int fdterm; // FILEDESCRIPTOR RETURNED BY OPEN 

FILE * fpterm; // FILE pointer returned by fdopen 

int uart1_rxcount = 0; 

char uart1_rxbuffer[255]; 

 

int main() 

{  

fdterm = open("/dev/uart1", O_RDWR | O_NONBLOCK | O_NOCTTY);  

fpterm = fdopen(fdterm,"rw+"); 

uart1_rxcount=read(fdterm, &uart1_rxbuffer, sizeof(uart1_rxbuffer)); 

 

if(uart1_rxcount>0)  

fprintf(fpterm,"received:%s\r\n",uart1_rxbuffer); 

 

Now: my debugger says uart1_rxcount is always NULL http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/dry.gif , still nothing happens... Do i have to implement interrupts from the uart? And whats about this EPP port Jeroen was talking about? 

 

Anyway, it´s funny to see that something happens with my board http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/biggrin.gif  

Hope to hear you... 

Max
0 Kudos
Altera_Forum
Honored Contributor II
1,772 Views

do you use a while(1) around read ??? 

you don&#39;t need an interrupt, this is always done in the HAL driver altera_avalon_uart.c 

 

I don`t know if this is necessary, but set stdin,stdout,stderr in syslib properties to null or something other than 

uart1. 

 

 

int main() 

fdterm = open("/dev/uart1", O_RDWR | O_NONBLOCK | O_NOCTTY); 

fpterm = fdopen(fdterm,"rw+"); 

 

fprintf(fpterm,"UART1 STARTED\r\n"); // check initial output 

 

 

while(1) { 

uart1_rxcount=read(fdterm, &uart1_rxbuffer, sizeof(uart1_rxbuffer)); 

 

if(uart1_rxcount>0) { { 

fprintf(fpterm,"received:%s\r\n",uart1_rxbuffer); 

 

}
0 Kudos
Altera_Forum
Honored Contributor II
1,773 Views

http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/rolleyes.gif  

It works! I receive and send data to/from the UART! Thanks very much for your help! 

http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/rolleyes.gif
0 Kudos
Altera_Forum
Honored Contributor II
1,773 Views

This is great stuff guys, I&#39;m trying to do something similar. I have one question: What headers do I need to include? The Software developers guide says to include <unistd.h> but I get compile errors saying that the compiler doesn&#39;t know what open or O_NONBLOCK is. Thanks

0 Kudos
Altera_Forum
Honored Contributor II
1,773 Views

How to do that in Nios I? (send and receive from uart) 

http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/blink.gif
0 Kudos
Altera_Forum
Honored Contributor II
1,773 Views

i use these: 

# include <stdio.h># include <stddef.h># include <string.h># include <fcntl.h># include <unistd.h># include "system.h"# include "sys/alt_alarm.h"# include "alt_types.h"# include "altera_avalon_pio_regs.h"# include "sys/alt_irq.h"# include "altera_avalon_uart.h" 

 

don´t think you need all of them, but my programs runs... 

 

no idea about NIOSI http://forum.niosforum.com/work2/style_emoticons/<#EMO_DIR#>/sad.gif
0 Kudos
Altera_Forum
Honored Contributor II
1,773 Views

I got it down to just these three: 

# include <fcntl.h># include <stdio.h># include <unistd.h> 

 

 

for the uart at least.
0 Kudos
Reply