Forum Discussion

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

UART application on NEEK

Hi,

I use ocm_mmu_spi from Hippo and I'm able to use the UART for console, or if I use JTAG console, I can print on the UART using "/dev/ttyS0". So UART is working properly.

But I would like to use the serial link (RS232) with my NEEK board for another application like control/command and I need to develop a small code in C.

I'm a little bit confused (I've read a lot of posts), should I use the generic termios.h and in that case where is the library for the compilation or should I use the one from my linux distribution?

Or should I use altuart.c and in that case, is there a small example code available somewhere?

Thanks in advance

6 Replies

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

    An application is a user space program. So you cannot use altuart, which is kernel driver.

    You should open file /dev/ttyS0, and use file read write or select on it. You may use stty to change the mode.

    The include header and lib is with your toolchain, not your pc host.

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

    OK I understand I'm really newbie.

    But open read and so on are function defined in library?

    Which one should I use for my application in user space ? Because I have to built my Makefile and point to specific libraries...

    Can I use for my applications all the 'standard' library from my linux distribution (Fedora for instance) or nios2 specific ones (in that case where are they) ?

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

    I had already tried Hello and it worked. But I have more difficlulties when static or dynamic libraries are needed.

    I use ocm_spi_mmu and I wonder if the uclibc is really used?

    I can use under uClinux the ttyS0 device, for instance

    > cat hello.txt > /dev/ttyS0

    and it prints to RS232 port (connected to my PC).

    Anyway, when I wrote a small C code for printing a sentence :

    # include <stdio.h># include <string.h>

    char msg[]="Bonjour le monde";

    int main(void)

    {

    FILE *fp;

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

    if(fp)

    {

    printf("Ouverture du port reussit\n");

    fwrite(msg, strlen(msg), 1, fp);

    fprintf(fp, "Closing the UART file.\n");

    fclose(fp);

    }

    else

    {

    printf("Erreur à l ouverture de /dev/ttyS0\n");

    return (1);

    }

    return(0);

    }

    The code is correct (I can not tested on my PC) ?

    I have no error and receive nothing at RS232 port ???

    Is there a conflict with the ttyS0 already open at kernel level?

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

    You can test the same code on your Linux PC, connecting ttyS0 to another RS232 port or another PC.

    The mode of the file open is incorrect. You have to write. Please look at some examples in the user dir.

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

    Now it works, with the same code. Don't know why it didn't before ...

    I tried both buffered -fopen("/dev/ttyS0")- and unbuffered -open("/dev/ttyS0")- file manipulation and it works.

    Just in the buffered case (fopen), using fflush (fp) allows to empty the buffer without closing the file.

    So now it works, thanks.