Forum Discussion
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
Honored Contributor
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:
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: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); } } - Altera_Forum
Honored Contributor
--- 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. - Altera_Forum
Honored Contributor
Another example: created a .txt file with just random words:
And the result of the file read is:lemon orange apple bla bla bla hello.lemolemolemolemolemolemolemolemo hel he - Altera_Forum
Honored Contributor
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. - Altera_Forum
Honored Contributor
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! - Altera_Forum
Honored Contributor
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. - Altera_Forum
Honored Contributor
--- 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. - Altera_Forum
Honored Contributor
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! - Altera_Forum
Honored Contributor
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
- Altera_Forum
Honored Contributor
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