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

Nios II, nonblocking i/o, anyone

Altera_Forum
Honored Contributor II
3,243 Views

Does anyone know how to implement a nonblocking input function on the jtag uart on NiosII ? 

 

On Nios I used nr_rxchar(), which returns even if there is no character waiting. 

 

The suggested replacement on NiosII is getchar(), which waits until a character is written. 

 

Havard
0 Kudos
7 Replies
Altera_Forum
Honored Contributor II
1,908 Views

Should be able to read/write directly, but without the functionality that altera threw in, how would you know when your data has gone across?  

 

Find the HAL register for the input and use it. But you're going to need some way of detecting whether you're input is valid or not. 

I guess you can check the recieve reg (I haven't looked at it so I have no idea if there is one or not), and pad you're data if it's not valid to something like -1 just like the RS232 did. 

 

Goodluck
0 Kudos
Altera_Forum
Honored Contributor II
1,908 Views

Just found a way: 

 

// Open the device as nonblocking 

console_io_fi=open("/dev/uart_0",O_RDWR|O_NONBLOCK|O_NOCTTY|O_SYNC); 

// i returns the number of characters read 

i=read(console_io_fi,&ch,1);  

 

I spent some time before I realized that the terminal window integrated in the IDE does not send anything until you hit CR. -So I had to use RS232 instead.
0 Kudos
Altera_Forum
Honored Contributor II
1,908 Views

Hi NiosII users, 

 

I also need a nonblocking uart read, but I am having some trouble with the defined switches. 

 

// if I open a stream in this way: 

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

 

//then  

 

fread(buff,sizeof(char),1,fp); 

 

// it works OK but blocks until a character is available. 

 

// but if I use the defined switch (from fcntl.h) 

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

 

// it dosnt read or write! 

 

I wish to use: 

fp = fopen("/dev/uart1","O_RDWR|O_NONBLOCK"); 

 

By the way, the second argument to fopen should be of type 'const char*' (eg. "O_RDWR|O_NONBLOCK"), no 

t an integer produced by: O_RDWR|O_NONBLOCK 

 

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

The above line will compile with a (implicit cast) warning, but should not give correct operation. 

 

Any ideas?
0 Kudos
Altera_Forum
Honored Contributor II
1,908 Views

use the "open" function and not "fopen"

0 Kudos
Altera_Forum
Honored Contributor II
1,908 Views

So how are you compiling when you use open() ? 

What are your# includes ?  

I don't see it defined anywhere in the NioII source tree. Likewise for fnctl.h. 

 

thanks
0 Kudos
Altera_Forum
Honored Contributor II
1,908 Views

I use these: 

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

 

I believe open to be in fcntl.h
0 Kudos
Altera_Forum
Honored Contributor II
1,908 Views

Works great, thanks.

0 Kudos
Reply