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

break loop with 'q'

Altera_Forum
Honored Contributor II
1,009 Views

Hello, 

 

this may be a very beginner question, but I cannot get ahead. 

I have a loop wich should be performed until the user presses 'q'. I tried with getchar() but it seems that getchar() waits for a return and my loop is not performed anymore. So I'm looking for a command which is not waiting for a return, or maybe a command who tells me if the uart buffer is empty or not. 

 

Thank you for each suggestion, Ralf 

 

do {  printf( "do something" ); } while( getchar() != 'q' );
0 Kudos
3 Replies
Altera_Forum
Honored Contributor II
295 Views

This happens because the default STDIN is blocking; it waits for an input before returning a value. To make the system do what you want you must make it a non-blocking call. If you are using the 5.0 release of Nios II you can use the fcntl call and change STDIN to non blocking (O_NONBLOCK flag). If you are using an earlier release, you can change the flags directly or create a new file handle for the serial line and create to be non-blocking. 

 

Hope this helps. 

 

Michael
0 Kudos
Altera_Forum
Honored Contributor II
295 Views

Thank you very much, works fine now. 

 

My code: 

int32 KeyPressed( void ) {  int32 key = -1;  int32 flags;    /* save flags */  flags = fcntl( fileno(stdin), F_GETFL );    /* set non blocking */  if( ! fcntl( fileno(stdin), F_SETFL, flags | O_NONBLOCK ) );  {    key = getchar();  }    /* restore flags */  fcntl( fileno(stdin), F_SETFL, flags );    return( key ); }
0 Kudos
Altera_Forum
Honored Contributor II
295 Views

Hi, 

 

This code works with Quartus II 9.0 using the JTAG uart but failed when I use an UART core. 

 

Have you an idea to allow non-blocking with the UART corre from ALTERA ??
0 Kudos
Reply