Results 1 to 9 of 9

Thread: NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

  1. #1
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    1

    Default NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

    Hi, I'm new about Quartus II & NIOS II.
    I made hardware part using sopc builder (nios II cpu, sdram, on-chip mem,
    jtag, sysid, and so on)
    and composed c source code for software part.

    I want to get right output data from 'printf' function
    and test1.txt file from 'fopen, fprintf, fclose' function
    ,but i didn't get any right result.
    How can I solve this problems?

    ================================================== =====
    #include <stdio.h>
    #include <unistd.h>
    #include "system.h"

    #define DEBOUNCE 10000

    int main()
    {
    int input_data = 0x0001;
    int output_data;
    FILE *fp;

    IOWR(SDRAM_0_BASE, 0, input_data);
    output_data = IORD(SDRAM_0_BASE, input_data);
    printf("output data = %d\n", output_data);

    fp = fopen("C:\test1.txt", "w");
    fprintf(fp, "%d", output_data);
    fclose(fp);
    return 0;
    }
    ================================================== =====

    Thanks in advance.

  2. #2
    Join Date
    May 2010
    Posts
    191
    Rep Power
    1

    Default Re: NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

    > fp = fopen("C:\test1.txt", "w");
    if you know where is "c:\" on your board?

    when you just create CPU(NiosII)
    you don't have file-system
    you need to create file-system as well.( Linux, uC-OSII or by yourself).

  3. #3
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    1,222
    Rep Power
    1

    Default Re: NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

    Hi Teddy,
    Apart from the fopen issue, I'm concerned about the two R/W instructions:
    Code:
    IOWR(SDRAM_0_BASE, 0, input_data);
     output_data = IORD(SDRAM_0_BASE, input_data);
    Problem 1:
    You are writing to address 0 and reading from address 1 (content of input_data) which has not been initialized.
    Maybe, you meant this:
    Code:
    output_data = IORD(SDRAM_0_BASE, 0);
    Problem 2:
    Are you sure this part of sdram is not already used for anything else?
    Otherwise your IOWR would corrupt code or other data.

    Cris

  4. #4
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    82
    Rep Power
    1

    Default Re: NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

    Hi teddy,

    p = fopen("C:\test1.txt", "w");
    fprintf(fp, "%d", output_data);
    fclose(fp);
    Akira is right. You need a filesystem for that.


    But as Cris mentioned, I am also concerned about the IOWR/IORD-part.
    If I got you right, you want to write input_data to some SDRAM-address and read it back for printing it.

    First: I would let the compiler allocate memory for you, unless you know what is at the specified address. You can use malloc or something like that or you just declare a variable.

    For example (assumed that data region is located at SDRAM):
    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include "system.h"
    
    int input_data = 1;
    int output_data = 0;
    int output_data_read = 0;
    
    int main()
    {
        IOWR(&output_data,0,input_data); // write input_data to output_data with IOWR
        output_data_read = IORD(&output_data,0); // read it back from output_data to output_data_read
        printf("output data = %d\n", output_data_read); // printing on STDOUT
       return 0;
    }
    Second: I would usually use IOWR/IORD only in cases where it can be neccessary. Like bypassing the data cache. Otherwise, better use standard C-coding for accessing memory.

    Regards,
    Philipp

  5. #5
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    1

    Default Re: NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

    Quote Originally Posted by Cris72 View Post
    Hi Teddy,
    Apart from the fopen issue, I'm concerned about the two R/W instructions:
    Code:
    IOWR(SDRAM_0_BASE, 0, input_data);
     output_data = IORD(SDRAM_0_BASE, input_data);
    Problem 1:
    You are writing to address 0 and reading from address 1 (content of input_data) which has not been initialized.
    Maybe, you meant this:
    Code:
    output_data = IORD(SDRAM_0_BASE, 0);
    Problem 2:
    Are you sure this part of sdram is not already used for anything else?
    Otherwise your IOWR would corrupt code or other data.

    Cris
    Thanks for your answer.
    I used 16bit input_data in c code and
    16bit SDRAM using SOPC Builder,
    but I got only 8bit output_data.
    Where is the problem?

    <result>
    output data1 = 11111111
    output data2 = 10101010

    =================================================
    #include <stdio.h>
    #include <unistd.h>
    #include "system.h"

    int main()
    {
    long input_data1 = 0x0000000011111111;
    long input_data2 = 0x1010101010101010;
    long output_data1;
    long output_data2;

    IOWR(SDRAM_0_BASE, 0, input_data1);
    output_data1 = IORD(SDRAM_0_BASE, 0);
    printf("output data1 = %x\n", output_data1);

    IOWR(SDRAM_0_BASE + 1, 0, input_data2);
    output_data2 = IORD(SDRAM_0_BASE + 1, 0);
    printf("output data2 = %x\n", output_data2);

    return 0;
    }
    =================================================

  6. #6
    Join Date
    Aug 2010
    Posts
    14
    Rep Power
    1

    Default Re: NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

    Quote Originally Posted by akira View Post
    > fp = fopen("C:\test1.txt", "w");
    if you know where is "c:\" on your board?

    when you just create CPU(NiosII)
    you don't have file-system
    you need to create file-system as well.( Linux, uC-OSII or by yourself).
    Hi again!
    Do you know how can I get from SDRAM data to my hard driver?
    My goal is to have binary data of txt file form...

    Thanks for your continuous concern.

  7. #7
    Join Date
    Jun 2010
    Location
    Spain
    Posts
    1,222
    Rep Power
    1

    Default Re: NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

    Teddy,
    0x prefix stands for hex number. One hex digit = 4bit !!!
    You must use 0b00101... to specify a binary number.
    Moreover, your 'long' data variables are actually 64bit long !
    You are getting the correct 32bit prints with %x format.
    If you want complete 64bit print you should use %lx or %llx
    If you want ram native 16bit data access, you should define variables as 'short'.

    Cris

  8. #8
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    82
    Rep Power
    1

    Default Re: NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

    int main()
    {
    long input_data1 = 0x0000000011111111;
    long input_data2 = 0x1010101010101010;
    long output_data1;
    long output_data2;

    IOWR(SDRAM_0_BASE, 0, input_data1);
    output_data1 = IORD(SDRAM_0_BASE, 0);
    printf("output data1 = %x\n", output_data1);

    IOWR(SDRAM_0_BASE + 1, 0, input_data2);
    output_data2 = IORD(SDRAM_0_BASE + 1, 0);
    printf("output data2 = %x\n", output_data2);

    return 0;
    }
    I think it is not possible to perform 64 bit IOWR.

    If you want to write 64 bit data you have to split it up into 2*IOWR.
    Use IOWR twice.

    Or am I wrong here? NIOS is 32bit

  9. #9
    Join Date
    May 2010
    Posts
    191
    Rep Power
    1

    Default Re: NIOS II C language about SDRAM IOWR & IORD function and fopen & fclose

    oh you are teddy80
    hi again.

    if you have UART(RS-232C)
    you can program on NiosII to send data from SDRAM.
    and your computer can get RS232C data some how like hyper terminal.
    RS232C is slow...
    there is several way. LAN / SD-Card / USB ...
    but many things are difficult after all.
    you need to learn more.

    UART is easy to use.
    I recommend biginer to use UART to read data from embedded system.

    see you and good luck

Similar Threads

  1. fopen and fclose
    By farhaenis in forum Nios Forum
    Replies: 1
    Last Post: September 29th, 2010, 09:44 PM
  2. IOWR/IORD problem, what does IORD return?
    By badgrant in forum General Discussion Forum
    Replies: 2
    Last Post: April 5th, 2007, 02:51 PM
  3. Using IORD - IOWR
    By quan1328 in forum General Software Forum
    Replies: 5
    Last Post: July 14th, 2006, 07:36 PM
  4. IOWR, IORD macros
    By gpsky in forum General Software Forum
    Replies: 1
    Last Post: April 17th, 2006, 06:00 AM
  5. how to set char by uart use fopen and fclose ?
    By yangroot in forum General Software Forum
    Replies: 3
    Last Post: December 10th, 2005, 09:58 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •