Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Altera_Forum
Honored Contributor I
814 Views

write file .txt to sd card on DE2

Hi everybody!!!! 

 

I'm trying to write a txt.file using SD Card Ip from Altera. when i built my project no error, but when i RUN AS, my project isn't writen successfuly. 

Help me!!!!!!!!!!!!!!! 

give me some advised 

 

my code: 

# include <io.h># include <system.h># include <stdio.h># include "altera_up_sd_card_avalon_interface.h" 

char name[] =" vidu.txt"; // when you open file vidu.txt, you see " xinchaocacban" 

short int file_handle; 

bool create, hum ; 

//=================initial sd card connect================== 

alt_up_sd_card_dev *device_reference = NULL; 

int connected = 0; 

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

if (device_reference != NULL)  

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

printf("Card connected.\n"); 

if (alt_up_sd_card_is_FAT16()) 

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

else 

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

connected = 1; 

}  

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

printf("Card disconnected.\n"); 

connected = 0; 

}  

}  

//===============ghi file text================ 

 

file_handle = alt_up_sd_card_fopen(name, create); 

 

if (file_handle == -1) 

printf("file could not be open.\n"); 

else if ((file_handle == -2)) 

printf("file be already.\n"); 

else  

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

if (create == true) 

printf("file be create .\n"); 

 

for(j=0; j< 256; j++) 

{  

hum= alt_up_sd_card_write(file_handle,ag[j]); 

if (hum == true) 

printf ("successful. \n"); 

 

alt_up_sd_card_fclose(file_handle); 

}
0 Kudos
1 Reply
Altera_Forum
Honored Contributor I
19 Views

you can try this code : 

int main() 

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

if (device_reference != NULL)  

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

printf("Card connected.\n"); 

if (alt_up_sd_card_is_FAT16()) 

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

else 

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

connected = 1; 

}  

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

printf("Card disconnected.\n"); 

connected = 0; 

}  

}  

//===============ghi file text================ 

file_handle = alt_up_sd_card_fopen(name, true); 

if (file_handle == -1) 

printf("file could not be open.\n"); 

else if ((file_handle == -2)) 

printf("file is already open.\n"); 

else  

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

for(j=0; j< 24; j++) 

{  

hum= alt_up_sd_card_write(file_handle,ag[j]); 

if (hum == true) 

printf ("successful. \n"); 

alt_up_sd_card_fclose(file_handle); 

return 0; 

}