Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
20 years ago

uart hal questions ?

1. how can I open the uart for non blocking read ?

2. how can I do "blocking output" with "non blocking read" ?

( without modifying altera_avalon_uart.c)

3. how can I disable RTS/CTS in software ?

1. without OS a blocking uart read is not very usefull

I open the uart in the following way:

int fdterm; // FILEDESCRIPTOR RETURNED BY OPEN

FILE *fpterm; // FILEPOINTER RETURNED BY FDOPEN

flags= O_RDWR | O_NONBLOCK | O_NOCTTY;

fdterm = open(devicename,flags);

if(fdterm==0) {

DPRINTF("ERROR open devive:%s\r\n",devicename);

return -1;

}

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

if(fpterm==0) {

DPRINTF("ERROR fdopen devive:%s\r\n",devicename);

}

..

// then I can read nonblocking

...

res=read(fdterm,uart1_tempbuff,sizeof(uart1_tempbuff)-1);

if(res>0) {

// we have receuived any characters

..

}

2. using fprintf(fpterm,"...") I get scrambled output

I think this is because in "altera_avalon_uart.c" in

function alt_avalon_uart_write the buffer is overwritten. I get

correct output, if I set disable "no_block=0"

..

int alt_avalon_uart_write (alt_fd* fd, const char* ptr, int len)

{

alt_irq_context context;

int no_block;

alt_u32 next;

alt_avalon_uart_dev* dev = (alt_avalon_uart_dev*) fd->dev;

int count = len;

/*

* Construct a flag to indicate whether the device is being accessed in

* blocking or non-blocking mode.

*/

no_block = (fd->fd_flags & O_NONBLOCK);

no_block=0; // set no_block to 0 to get printf working

/*

...

3. I have configured the uart in sopc builder with RTS/CTS

but don't want to use it in software.

I use the following function to clear the flag ALT_AVALON_UART_FC.

...

int uart_set_flowcontrol(int filedescriptor,BYTE flowctrl) {

alt_fd* fd;

alt_avalon_uart_dev* dev; // pointer to uart device

if(filedescriptor<0) return -1;

// see alt_ioctrl.c

fd = (filedescriptor < 0) ? NULL : &alt_fd_list[filedescriptor];

if (fd) {

dev=(alt_avalon_uart_dev*)fd->dev;

if(flowctrl) {

dev->flags|=ALT_AVALON_UART_FC;

}

else {

dev->flags&=~ALT_AVALON_UART_FC;

}

return 0;

}

return -1;

}

...

4. I change baudrate with the function

...

int uart_set_baudrate(int filedescriptor,int baudrate) {

struct termios term;

int res;

res=ioctl(filedescriptor,TIOCMGET,&term);

if(res<0) {

DPRINTF("ERROR ioctl filedescriptor:%d\r\n",filedescriptor);

return res;

}

term.c_ispeed=baudrate;

term.c_ospeed=baudrate;

res=ioctl(filedescriptor,TIOCMSET,&term);

if(res<0) {

DPRINTF("ERROR ioctl filedescriptor:%d\r\n",filedescriptor);

}

return res;

}

...

is this correct ???

6 Replies

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

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

    --- Quote Start ---

    1. how can I open the uart for non blocking read ?[/b]

    --- Quote End ---

    As you put it in your example, this opens the UART non-blocking

    int fdterm; // FILEDESCRIPTOR RETURNED BY OPEN

    flags= O_RDWR | O_NONBLOCK | O_NOCTTY;

    fdterm = open(devicename,flags);

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

    --- Quote Start ---

    2. how can I do "blocking output" with "non blocking read" ?[/b]

    --- Quote End ---

    Open it twice i.e.

    int fdterm; // NON BLOCKING FILEDESCRIPTOR RETURNED BY OPEN

    int fdterm_block; // BLOCKING FILEDESCRIPTOR RETURNED BY OPEN

    flags= O_RDWR | O_NONBLOCK | O_NOCTTY;

    fdterm = open(devicename,flags);

    flags= O_RDWR | O_NOCTTY;

    fdterm_block = open(devicename,flags);

    Use the appropriate handle depending upon whether you want blocking or non-blocking behaviour

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

    --- Quote Start ---

    3. how can I disable RTS/CTS in software ?[/b]

    --- Quote End ---

    You can&#39;t it&#39;s asssumed that if you selected this hardware in SOPC Builder you will want the software. If you don&#39;t want RTS CTS turn it off in SOPC builder and save yourself some hardware resources
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    thanks rugbybloke,

    the question of RTS/CTS is just that I don&#39;t know, if I will

    need it later. Also I have 3 nearly equal projects, so that

    I don&#39;t want to change the sopc system.

    So I think, it only needs to clear the flag ALT_AVALON_UART_FC.

    Is the function "uart_set_flowcontrol correct" ???
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    I can&#39;t seem to get the &#39;open&#39; command to work, only the &#39;fopen&#39; one which doesn&#39;t seem to support non-blocking access. What include files are you using?

    Thanks,

    Richard
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    The best way to find things like this is to use man on a unix machine, or type "linux man open" into google.

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    i think open is in <fcntl.h>, don&#39;t know if you need all, just a copy from my file

    # include <sys/ioctl.h>

    // open ???# include <fcntl.h># include<string.h>

    // rand(),srand()# include<stdlib.h>

    // write(),read(),usleep()# include<unistd.h>

    # include <priv/alt_file.h># include "altera_avalon_uart_regs.h"# include "altera_avalon_uart.h"

    # include<time.h>