0

I've got a strange situation with my C code for linux (ubuntu 19.04), I'm an FPGA developer, mainly I know only HDLs languages, now I've decided to focus on C coding, I have very poor background with C, and would like to write programs that can comunicate with my FPGA. I'm using for this a Basys3 board. I've got a basic UART module, (on FPGA) that on windows works perfectly with TERA TERM. FPGA does this: all what comes from UART goes to a buffer (8 bytes wide), when the first 4 bytes are the binary value of ASCII "data" it saves the remaining 4 bytes into the memory. When I press a button the 4 bytes from the memory are sent to UART Tx. I'm sure that it's sending data: without shutting off the board I load win10 and check on tera term. On ubuntu it just works sometimes. for example: turning on PC, loading bitstream to the FPGA, running C program for sending 8 bytes "data1234", running C uart receiver and pressing the button I get "1234123412341234". shutting off PC, rebooting, doing exactly the same things, can't read anything from UART (pc side).

the previous version of the FPGA bitstream was easier: what comes from UART goes to memory and when I press the button is sent to UART Tx. This works everytime, sending unknown characters, but at least it works, and makes me think that I need some kind of response from UART to enable receiving (pc side), but honestly I don't know..

this is the code that I'm using from an online tutorial and modded. I added some features in some version, but now I'm back to a nearly untouched version.

    #include <stdio.h>
    #include <fcntl.h>   /* File Control Definitions           */
    #include <termios.h> /* POSIX Terminal Control Definitions */
    #include <unistd.h>  /* UNIX Standard Definitions      */ 
    #include <errno.h>   /* ERROR Number Definitions           */

void main(void)
    {
        int fd;/*File Descriptor*/

    printf("\n +----------------------------------+");
    printf("\n |        Serial Port Read          |");
    printf("\n +----------------------------------+");

    /*------------------------------- Opening the Serial Port -------------------------------*/

    /* Change /dev/ttyUSB1 to the one corresponding to your system */

        fd = open("/dev/ttyUSB1",O_RDWR | O_NOCTTY );   /* ttyUSB0 is the FT232 based USB2SERIAL Converter   */
                            /* O_RDWR   - Read/Write access to serial port       */
                            /* O_NOCTTY - No terminal will control the process   */
                            /* Open in blocking mode,read will wait              */



        if(fd == -1)                        /* Error Checking */
               printf("\n  Error! in Opening ttyUSB1  ");
        else
               printf("\n  ttyUSB1 Opened Successfully ");


    /*---------- Setting the Attributes of the serial port using termios structure --------- */

    struct termios SerialPortSettings ; /* Create the structure                          */

    tcgetattr(fd, &SerialPortSettings); /* Get the current attributes of the Serial port */

    /* Setting the Baud rate */
    cfsetispeed(&SerialPortSettings,B9600); /* Set Read  Speed as 9600                       */
    cfsetospeed(&SerialPortSettings,B9600); /* Set Write Speed as 9600                       */

    /* 8N1 Mode */
    SerialPortSettings.c_cflag &= ~PARENB;   /* Disables the Parity Enable bit(PARENB),So No Parity   */
    SerialPortSettings.c_cflag &= ~CSTOPB;   /* CSTOPB = 2 Stop bits,here it is cleared so 1 Stop bit */
    SerialPortSettings.c_cflag &= ~CSIZE;    /* Clears the mask for setting the data size             */
    SerialPortSettings.c_cflag |=  CS8;      /* Set the data bits = 8                                 */

    SerialPortSettings.c_cflag &= ~CRTSCTS;       /* No Hardware flow Control                         */
    SerialPortSettings.c_cflag |= CREAD | CLOCAL; /* Enable receiver,Ignore Modem Control lines       */ 


    SerialPortSettings.c_iflag &= ~(IXON | IXOFF | IXANY);          /* Disable XON/XOFF flow control both i/p and o/p */
    SerialPortSettings.c_iflag &= ~(ICANON | ECHO | ECHOE | ISIG);  /* Non Cannonical mode                            */

    SerialPortSettings.c_oflag &= ~OPOST;/*No Output Processing*/

    /* Setting Time outs */
    SerialPortSettings.c_cc[VMIN] = 32; /* Read at least 10 characters */
    SerialPortSettings.c_cc[VTIME] = 5; /* Wait indefinetly   */


    if((tcsetattr(fd,TCSANOW,&SerialPortSettings)) != 0) /* Set the attributes to the termios structure*/
        printf("\n  ERROR ! in Setting attributes");
    else
                printf("\n  BaudRate = 9600 \n  StopBits = 1  \n  Parity   = none");

        /*------------------------------- Read data from serial port -----------------------------*/

    tcflush(fd, TCIFLUSH);   /* Discards old data in the rx buffer            */

    char read_buffer[32];   /* Buffer to store the data received              */
    int  bytes_read = 0;    /* Number of bytes read by the read() system call */
    int i = 0;

    bytes_read = read(fd,&read_buffer,32); /* Read the data                   */

    printf("\n\n  Bytes Rxed -%d", bytes_read); /* Print the number of bytes read */
    printf("\n\n  ");

    for(i=0;i<bytes_read;i++)    /*printing only the received characters*/
        printf("%c",read_buffer[i]);

    printf("\n +----------------------------------+\n\n\n");

    close(fd); /* Close the serial port */

    }

if you can help me finding what is wrong I will love you forever

Thanks

  • Does this help? https://stackoverflow.com/questions/6947413/how-to-open-read-and-write-from-serial-port-in-c I notice they open with O_SYNC to avoid buffering characters. – stark Nov 14 '19 at 01:40
  • O_SYNC is not working, any other suggestion? thanks for your time – Bioracing Nov 14 '19 at 16:44
  • does it need to be in C? Or is the task to just communicate with your FPGA via the PC? – Christian B. Nov 18 '19 at 09:01

0 Answers0