Forum Discussion

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

Accesing PC files from UART

Hi everybody,

I am working in a project that requires sending datas stored in an internal register implemented into the FPGA to a file in a PC (W-XP). I think the best way is using the UART (11500 kbs).

however, when I try to access into a file in the computer from Nios, just for training, it doesn't work. I always receive the message "Cannot open file".

I use the UP3 Education kit.

Any idea? Thank you very much.

Roberto.

-------------------------------------------------------

The code I download to the Nios is written below:

# include <stdio.h># include <stddef.h># include <stdlib.h>

# define BUF_SIZE (10)

int main(void)

{

FILE* fp;

char buffer[BUF_SIZE];

fp=fopen("/prueba/test.txt", "r");

if (fp==NULL)

{

printf("Cannot open file. \n");

exit(1);

}

fread (buffer, BUF_SIZE, 1, fp);

fclose(fp);

return 0;

}

10 Replies

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

    --- Quote Start ---

    fp=fopen("/prueba/test.txt", "r");

    i don&#39;t understand one thing

    how do you excpect the cpu to recognize "/prueba/test.txt"?

    it doesn&#39;t share the same file system as your pc
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi Ilan-g7,

    I am starting to learn Altera Nios CPU, so I am probably doing silly things. Past days I realized that /prueba/test.txt is not recognized by the FPGA inside my PC, but it is considered a file path in the own FPGA. As there is no file in such FPGA it has no sense for it.

    Reading Altera&#39;s manuals I found a set of instructions to manage the UART:

    nr_uart_txcr

    nr_uart_txhex, etc, etc...

    This week I&#39;ll try to use them but I do not know if this way is the correct. When I am capable of sending datas from the FPGA to the PC I suppose I&#39;ll need a program in the PC to open a virtual console in order to store datas in a file.

    well, if you have some experience in this task or can help me clarifying concepts would be great!!!

    Thanks.

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

    hi Roberto,

    you should stick to the functions in stdio.h! the functions from altera you mentioned are meant for use with the legacy sdk.

    You will find more information about accessing the Nios 2 uart in the software developer&#39;s handbook and the embedded peripherals handbook.

    In order to store data to your pc you need a terminal program that reads the data from the pc&#39;s serial port and writes it into a file.

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

    Hi Roberto,

    Wolf is right,

    You should use the uart to transmit your file from nios to your pc

    On the pc side you will need to write a program that will read this transmitted chars and will write them to a file.

    Try to read n2cpu_nii5v3.pdf it is found in the document under your installation, you will find there a chapter about UART

    Try using # include <stdio.h>

    FILE* fp=fopen(YOUR_UART_NAME,"r+");

    fprintf(fp,"what ever you want to transmit on uart");

    fclose(fp);

    This is the way to transmit char over uart.

    YOUR_UART_NAME was defined as you added the uart component on SOPC

    You can find this name in a file called system.h

    The system.h describes your design and can be found under

    YOUR_PROJECT_syslib\Debug

    have fun

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

    Thanks everybody!!

    I&#39;ll try what you say!!

    --- Quote Start ---

    originally posted by ilan-g7@Jan 23 2006, 06:52 PM

    hi roberto,

    wolf is right,

    you should use the uart to transmit your file from nios to your pc

    on the pc side you will need to write a program that will read this transmitted chars and will write them to a file.

    try to read n2cpu_nii5v3.pdf it is found in the document under your installation, you will find there a chapter about uart

    try using # include <stdio.h>

    file* fp=fopen(your_uart_name,"r+");

    fprintf(fp,"what ever you want to transmit on uart");

    fclose(fp);

    this is the way to transmit char over uart.

    your_uart_name was defined as you added the uart component on sopc

    you can find this name in a file called system.h

    the system.h describes your design and can be found under

    your_project_syslib\debug

    have fun

    ilan

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

    --- quote end ---

    --- Quote End ---

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

    Hello,

    Everything has worked perfectly. I&#39;ve been able to store datas to a test.txt file from the FPGA via UART.

    Thank you everybody for your help!!!!

    cheers.

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

    Hi Roberto & everyone,

    I need to do the same thing you did, But I dont know which to use the JTAG UART or the UART "I hope anyone tell me the difference". Also, on the host PC side where to write the receiving program? and Where my program should look for the needed data?

    Any help appreciated,

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

    I would use UART and not jtag uart simply becuse of the fact that i don&#39;t know how to write a program that will sit at the pc side and will use the jtag protocol to communicate with the nios. this doesn&#39;t says that it can be done.

    so for simplicity use uart.

    you should write on your pc a program that will receive the data sent from the nios. for that you can use any C/C++ compiler that generate a code for your pc

    like Visual studio (you can&#39;t use the nios II ide).

    if you need a code that do this you can email me
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    For receiving datas to the pc I did not write any C-code. I just used the hyperterminal aplication that you can find with Windows XP. This opens a console that can be configured easily to stablish a communication with serial port. After, you can send datas from the FPGA to a text file with the same program

    Good luck!
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    fisrt thanks for the responses.

    It seems for me that Roberto and Ilan suggested two different working solutions for my problem "Using hyperterminal and writing code". I tried to follow, but I couldnont make connection to the USB port as I didnot find it listed. Can you please elaborate on this. I would like also to ask Ilan if he can send me the example code "my email is mkk6@njit.edu".

    Thanks.