Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12603 Discussions

Writing .txt file in SD Card - DE2

Altera_Forum
Honored Contributor II
6,679 Views

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.
0 Kudos
37 Replies
Altera_Forum
Honored Contributor II
539 Views

There's something strange when I try to read from my sd_card. 

 

I just wanna to see the contents in hexadecimal. Here's the code: 

 

short handler = alt_up_sd_card_fopen(temp, false); printf("\n\n"); if(handler >= 0) { short read; while ((read = alt_up_sd_card_read(handler)) != -1) { printf("%02x ", read); } }  

 

And here are the first results printed on screen: 

http://i44.tinypic.com/1551mo8.png 

 

And the file contents seen on a hexadecimal editor: 

http://i41.tinypic.com/ezvib.png 

 

Note that the first 4 bytes of the file is repeated 8 times on the code, and the "f9 00 f7 00" part of the file is never read using the code. Strange :cry:
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

 

--- Quote Start ---  

PeteCSI: I'm having some trouble when creating a NIOS with the Altera_UP_SD_Card, I tested the .c example using a Nios from the Altera CD, but I get no response from SD. 

--- Quote End ---  

 

 

GP150, are the hardware you created right? 

 

I followed the instructions on PDF from University Altera and it is detecting the SD card.
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

Another example: created a .txt file with just random words: 

 

lemon orange apple bla bla bla hello. 

 

And the result of the file read is: 

 

lemolemolemolemolemolemolemolemo hel he
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

I just solved my problem and it seems that my altera_up_sd_card_avalon_interface.h and altera_up_sd_card_avalon_interface.c files were outdated. I replaced my files with the ones at ftp://ftp.altera.com/up/pub/altera_material/11.0/university_program_ip_cores/ and now it seems to read my files correctly. 

 

Another problem I had was when trying to read characters with value above 127, because the alt_up_sd_card_read returns a short int. Then, I changed its return value to int on altera_up_sd_card_avalon_interface.h and altera_up_sd_card_avalon_interface.c, and at the former, I changed: 

 

* Line 1733 from (char) to (int); 

* Line 1680 from char to int; 

 

That resolved my problems, and now I can read values above 127.
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

Hello Tiakumosan! 

Thank you for your help! 

 

I was using a hardware from altera, but i couldn't get any response from sd card! So I create my own hardware (following the instructions from the PDF from University Program), and Its detecting the SD Card!  

Now I can use all the functions describe in the PDF! But... 

 

But I am having problems with the FAT16, because the Core isn't detecting correctly the FAT16 file system. The function "lt_up_sd_card_is_FAT16" always return false! 

 

How did you format your SD Card? 

I formatted a 128Mb using "cmd" in windows (format E: /FS:FAT /A:2048 /V:SDCARD), doing it I get 2048bytes in each allocation unity and 65536 allocation units. 

But I will need to store a large data, maybe I will need to use a bigger SD. 

 

 

PS: Im using the 11.0 Altera UP!
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

65536 allocation units requires FAT32, the max for FAT16 is a few less than 2^16 (0, 1 and 0xffff are definitely reserved). 

 

The maximum allocation unit size is 64k (but has been 32k - signed byte issues!), so the max size of a FAT16 fs is somewhat larger. OTOH the altera code mught not support large allocation units (or might require too many allocation-unit sized buffers for the memory footprint). 

 

You might be able to use a raw partition to store the data. But reading them on windows might be 'tricky'. Keeping a small FAT partition might help the OS keep the USB stick active. 

 

The FAT fs format is also quite well documented, updating the FAT16 code to support FAT32 instead probably isn't that difficult. 

Even writing it from scratch isn't that hard.
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

 

--- Quote Start ---  

Hello Tiakumosan! 

Thank you for your help! 

 

I was using a hardware from altera, but i couldn't get any response from sd card! So I create my own hardware (following the instructions from the PDF from University Program), and Its detecting the SD Card!  

Now I can use all the functions describe in the PDF! But... 

 

But I am having problems with the FAT16, because the Core isn't detecting correctly the FAT16 file system. The function "lt_up_sd_card_is_FAT16" always return false! 

 

How did you format your SD Card? 

I formatted a 128Mb using "cmd" in windows (format E: /FS:FAT /A:2048 /V:SDCARD), doing it I get 2048bytes in each allocation unity and 65536 allocation units. 

But I will need to store a large data, maybe I will need to use a bigger SD. 

 

 

PS: Im using the 11.0 Altera UP! 

--- Quote End ---  

 

 

I have a 256mb card and used MiniTool Partition Wizard Home Edition to format it. Although the card was opened correctly, alt_up_sd_card_find_next and alt_up_sd_card_read did not work right until I changed them, like I said earlier.  

 

alt_up_sd_card_is_FAT16 worked right here, only when I used fat32/exfat/ntfs cards it returned false, as expected.
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

Thank you Tiakumosan! 

 

I created a 128Mb FAT16 (primary) partition with 2kB in my 1GB SD Card with 2KB Cluster Size, and the core recognizes the file system format. I couldn't get bigger partitions! 

 

Now I'm trying to use the "alt_up_sd_card_write" to write a file 

 

I post my results when I get something!
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

search "University Program IP" on Altera website.....plus search "University Program SD Card" on google...you will find pdf explaining the ip core as well the functions that are part of the HAL driver

0 Kudos
Altera_Forum
Honored Contributor II
539 Views

search "University Program IP" on Altera website.....plus search "University Program SD Card" on google...you will find pdf explaining the ip core as well the functions that are part of the HAL driver

0 Kudos
Altera_Forum
Honored Contributor II
539 Views

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; 

}
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

Which problems are you having? 

 

And are you brazilian too? Olá =)
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

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 :)
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

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.

0 Kudos
Altera_Forum
Honored Contributor II
539 Views

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);
0 Kudos
Altera_Forum
Honored Contributor II
539 Views

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

0 Kudos
Altera_Forum
Honored Contributor II
539 Views

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);
0 Kudos
Reply