Forum Discussion

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

DE2 -> NiosII UART (RS-232) problems

I am using the DE2 evaluation board for a project I am working on. I have just recently been learning this hardware and have upto now have been able to successfully use the LCD, sdram, swtiches and leds.

As I was hoping the UART for serial comm would be simple, I'm having trouble.

I've correctly setup the NIOS 2 processor with the UART (rs-232) component on the avalon switch, and inserted RXD and TXD to my HDL file. Using the IDE, i wrote a sample program to send and receive data from my PC which is using Hyperterminal.

The following is the code I use for my test:

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

int main{

FILE* fp;

char prompt = 0;

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

if(fp){

printf("UART open. \n");

}

else{

printf("UART closed. \n);

}

fprintf(fp, "UART\n");

while(prompt != &#39;v&#39;){

prompt = getc(fp);

printf("%c \n",prompt);

}

fclose(fp);

return 0;

}

This program stays in the while loop foever, as it cannot read input from my terminal.

Any ideas????

2 Replies

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

    There are a syntax error in the posted code, so the following code is used:

    #include <stdio.h># include <string.h>
    int main()
    {
        FILE* fp;
        char prompt = 0;
        fp = fopen("/dev/uart", "r+");
        if(fp){
      printf("UART open. \n");
        }
        else{
      printf("UART closed. \n");
            return 0;
        }
        fprintf(fp, "UART\n");
        while(prompt != &#39;v&#39;){
      prompt = getc(fp);
      printf(" \n", prompt);
        }
        fclose(fp);
        return 0;
    }

    (btw, usually "\r\n" is needed in HyperTerminal for newline)

    I compiled the hardware in Quartus II 6.1 and software in Nios IDE and it works fine, outputing to the UART and receiving characters from there too.

    Assuming the "UART open." message can be seen so the program should be running correctly, there could be a few causes for the error:

    1. Make sure the name "dev/uart" matches the UART name in SOPC Builder, i.e., "uart" is the name in SOPC Builder.

    2. Connect to the correct UART port. Usually a computer has COM1 and COM2, so make sure the right one is chosen in HyperTerminal. (If you are using the PC for testing)

    3. Make sure the UART setting in SOPC Builder matches the settings in HyperTerminal, such as Parity, Data Bits, Stop Bit, Flow Control, etc.

    Please let me know if the above doesn&#39;t help and provide more information, such as the program output, sopc ptf file, etc.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    --- Quote Start ---

    originally posted by arrive@Mar 12 2007, 12:41 PM

    there are a syntax error in the posted code, so the following code is used:

    #include <stdio.h># include <string.h>
    int main()
    {
        file* fp;
        char prompt = 0;
        fp = fopen("/dev/uart", "r+");
        if(fp){
      printf("uart open. \n");
        }
        else{
      printf("uart closed. \n");
            return 0;
        }
        fprintf(fp, "uart\n");
        while(prompt != &#39;v&#39;){
      prompt = getc(fp);
      printf(" \n", prompt);
        }
        fclose(fp);
        return 0;
    }

    (btw, usually "\r\n" is needed in hyperterminal for newline)

    i compiled the hardware in quartus ii 6.1 and software in nios ide and it works fine, outputing to the uart and receiving characters from there too.

    assuming the "uart open." message can be seen so the program should be running correctly, there could be a few causes for the error:

    1. make sure the name "dev/uart" matches the uart name in sopc builder, i.e., "uart" is the name in sopc builder.

    2. connect to the correct uart port. usually a computer has com1 and com2, so make sure the right one is chosen in hyperterminal. (if you are using the pc for testing)

    3. make sure the uart setting in sopc builder matches the settings in hyperterminal, such as parity, data bits, stop bit, flow control, etc.

    please let me know if the above doesn&#39;t help and provide more information, such as the program output, sopc ptf file, etc.

    <div align='right'><{post_snapback}> (index.php?act=findpost&pid=22235)</div>

    --- Quote End ---