Forum Discussion

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

Writing .txt file in SD Card - DE2

Hello, I am Student from a University beginner in FPGAs, and I was doing a project with Nios II that uses a Altera DE2 Board to do some operations and save the results. The main ideia was to transmit to computer the results, but the USB take too much time to the FPGA.

Now I am seaching a way to save those results at the SD card. But I couldn't find a way to store data in SD card. My idea was to create a .txt file that could receive at the end of each operation a value in the next line.

If someone knows how save values in the SD card, I really appreciate your help, if you help send me.

37 Replies

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

    Hello! I successfully create a file in my SD card! I only having problems to write in the file! I am doing some tests I post my results when I get something.

    Here is my C program:

    # include <stdio.h>

    # include <altera_up_sd_card_avalon_interface.h>

    # include <altera_avalon_pio_regs.h>

    int main(void){

    alt_up_sd_card_dev *device_reference = NULL;

    int connected = 0;

    short int arquivo = 0;

    short int att = 0;

    char data = 0;

    printf("Insira o SD Card \n");

    device_reference = alt_up_sd_card_open_dev("/dev/Altera_UP_SD_Card");

    if (device_reference != NULL) {

    while(1){

    if((connected ==0) && (alt_up_sd_card_is_Present())){

    printf("SD Card Connected. \n");

    IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, 0xFFFF);

    if(alt_up_sd_card_is_FAT16()){

    printf("FAT16 file system detected!!! \n");

    /* Start to create a file */

    arquivo = alt_up_sd_card_fopen ("file.txt", true);

    printf ("fopen returns: %d\n", arquivo);

    alt_up_sd_card_write(arquivo, data);

    att = alt_up_sd_card_get_attributes(arquivo);

    printf ("get_attrubutes return: %d\n", att);

    /*alt_up_sd_card_fclose(arquivo);*/

    /* End */

    } else{

    printf("Unknown file system. \n");

    }

    connected = 1;

    } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)){

    printf("SD disconected. \n");

    IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, 0);

    connected = 0;

    }

    }

    }

    return 0;

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

    Hello! I write successfully in the SD Card!

    But the alt_up_sd_card_write function available, writes a char based on the ASCII table (inputting a hexadecimal).

    Now I am thing a way to convert a double value to enter in this write function. But this is a C problem! But I will post here when I find the solution.

    This is a simple program that demonstrates the write function

    ******************************************

    # include <stdio.h>

    # include <altera_up_sd_card_avalon_interface.h>

    # include <altera_avalon_pio_regs.h>

    int main(void){

    alt_up_sd_card_dev *device_reference = NULL;

    int connected = 0;

    short int handler;

    short int att;

    char data = 0x00;

    int loop;

    printf("Insert the SD Card \n");

    device_reference = alt_up_sd_card_open_dev("/dev/Altera_UP_SD_Card");

    if (device_reference != NULL) {

    while(1){

    if((connected ==0) && (alt_up_sd_card_is_Present())){

    printf("SD Card Connected. \n");

    IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, 0xFFFF);

    if(alt_up_sd_card_is_FAT16()){

    printf("FAT16 file system detected!!! \n");

    /* Start to create a file */

    handler = alt_up_sd_card_fopen ("file.txt", true);

    for(loop = 0; loop < 1000; loop++){

    data++;

    alt_up_sd_card_write(handler, 0x33);

    alt_up_sd_card_write(handler, 0x20);/*0x20 = space , 0x0D = enter*/

    }

    att = alt_up_sd_card_get_attributes(handler);

    printf ("get_attrubutes return: %d\n", att);

    alt_up_sd_card_fclose(handler);

    /* End */

    } else{

    printf("Unknown file system. \n");

    }

    connected = 1;

    } else if ((connected == 1) && (alt_up_sd_card_is_Present() == false)){

    printf("SD disconected. \n");

    IOWR_ALTERA_AVALON_PIO_DATA(LEDR_BASE, 0);

    connected = 0;

    }

    }

    }

    return 0;

    }

    ******************************************

    This program only writes "3 3 3 3 3 3 3 3 3..." in the SD Card. I tried to write "0x0D" that means ENTER button to write a matrix, but this failed, but that is ok for me writing the number separated by spaces (or comas).

    Thank everyone!

    PS: Yes I am from Brazil! Oi! Tudo bem :)
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Didn't try to write on SD yet ... but, the "Enter" of the keyboard is the combination of two ascii codes: 13 (carriage return) that just gets the cursor back to the start of the actual line, and 10 (line feed), that goes to the next line on the file. You could try using the line feed to try to write the matrix you want.

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

    Gp150, I did this to write a float value to the SD:

    
    char *name = malloc(sizeof(char) * 10); // a string with 9 positions; 1 is for the '\0'
    float valor = 3.14; //for example
    sprintf(name, "%.2f", valor); //prints the float value to the string
    int i = 0;
    while(name != '\0')
    {
        alt_up_sd_card_write(handler, name);
        i++;
    }
    

    Hope this helps you.

    Also, if you want to add a new line:

    alt_up_sd_card_write(handler, 13);

    alt_up_sd_card_write(handler, 10);
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    hi, Tiakumosan,I'm tring to writing a txt file to the sd card,but if the file is larger than 4k, I get problem. I can't open the file in windows,even use the winhex, it tells that can't read the *.txt file . wish your help.thanks

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

    this is my code:

    char * myfile = "004.txt";

    short int handler;

    handler = alt_up_sd_card_fopen(myfile, false);

    if (handler == -1)////file not exist

    {

    //creat file

    handler = alt_up_sd_card_fopen(myfile, true);

    }

    if (handler < 0) {

    printf("file open failed!!");

    }

    int i=0;

    for(;i<65530;i++)

    {

    alt_up_sd_card_write(handler,(char) 0x38);

    }

    alt_up_sd_card_fclose(handler);