Forum Discussion

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

UART on DE2

Hi, I'm using DE2 (old kit) and my application has a UART to exchange data with PC. I'm using the kit's RS232 connection (not the JTAG). I created the system (including the UART at 9600bps) in Qsys and my Nios code initially is the same as Example 7-2 of Embedded Peripherals IP User Guide. When I run the code in Nios II I observe (through terminal on Windows) that I can send data from DE2 (fwrite or fprintf) to PC but I can't read data from PC. Does anybody has any tip to solve my problem?

5 Replies

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

    Hi, could you post your piece of code (more comfortable for us to help you) ?

    I personally use 2 file descriptors : 1 for sending, 1 for receiving.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    This is my code:

    # include <stdio.h>

    # include <string.h>

    int main ()

    {

    char* msg = "Detected the character 't'.\n";

    FILE* fp;

    char prompt = 0;

    fp = fopen ("/dev/rs232", "r+"); //Open file for reading and writing

    if (fp)

    {

    while (prompt != 'v')

    { // Loop until we receive a 'v'.

    fprintf(fp, "Entered while.\n");

    prompt = getc(fp); // Get a character from the UART.

    if (prompt == 't')

    { // Print a message if character is 't'.

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

    }

    }

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

    fclose (fp);

    }

    return 0;

    }

    I have made some changes after I wrote the post and now I'm able to send and receive characters. Although, it isn't suitable for my application to use polling to receive characters, so I need to implement interrupt at least for reception, but I have no idea how to do this. Any tip?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi, look at how to write an interrupt in NIOS II (Altera Nios II software design handbook).

    If you employ interrupt, you will forget the file pointer and functions like fwrite(), getc()

    and need to employ lower layer functions like iord()

    For my project, I have tried interrupts to recieve data but it missed a few.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    So, if I decide to use interrupts I have to forget the HAL facilities? Do I really have to write low level code? I thought there was a way to use interrupts through HAL.

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

    It is quite simple to use interrupts : look at Nios II examples. You just need few functions like

    
    // Read status and compare to RRDY_MSK
    if (IORD_ALTERA_AVALON_UART_STATUS(UART1_BASE) & ALTERA_AVALON_UART_STATUS_RRDY_MSK)
        {
          donnee = IORD_ALTERA_AVALON_UART_RXDATA(UART1_BASE);
    }
    //...
    

    By doing this way, your main program will be interrupted at every character received (of course).

    Be careful about blocking situations.

    Good Luck