Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Valued Contributor III
842 Views

University IP SD Card Interface with the DE0 board

Hello all, 

 

I'm a Uni student in Bristol, UK. I'm trying to use the DE0 board for a project using a robot arm and a keypad to control it. We have to record so I'd like to get the SD card working. 

Using NIOS2 and FreeRTOS. The Altera SD card interface provided with the DE0 is quite low level, so I have tried to integrate the University IP so I can use the convenient FAT16 routines. 

 

I included the vhd files in SOPC builder, it's generated and provided system.h with the interface name:# define SD_CARD_INTERFACE_NAME "/dev/SD_CARD_Interface" 

 

I'm trying to use the SD card example given at the end of the PDF file, but the following line returns a NULL pointer: 

 

device_reference = alt_up_sd_card_open_dev(SD_CARD_INTERFACE_NAME); 

 

so the following if statement evaluates FALSE and just drops out. 

There was a previous post concerning the function that gets called in the line above: 

alt_dev* alt_find_dev(const char* name, alt_llist* llist) 

which looks like it isn't finding the name of the device in the linked list and returning NULL. 

 

The SD card I am using was detected while using the original SD interface, so I know it's not hardware. I followed the Pin assignments on the PDF which I assume are right. 

 

Pin Name DE0 

b_SD_cmd PIN_Y22 

b_SD_dat PIN_AA22 

b_SD_dat3 PIN_W21 

o_SD_clock PIN_Y21 

 

Any help would be greatly appreciated. It's quite curious.
0 Kudos
2 Replies
Highlighted
Valued Contributor III
12 Views

Hi, could I bump this up please? 

I'd really like an answer soon. 

Cheers
0 Kudos
Highlighted
Valued Contributor III
12 Views

OK, found the solution to this! 

 

ftp://ftp.altera.com/up/pub/university_program_ip_cores/90/altera_up_avalon_sd_card_interface.zip 

 

The altera_up_sd_card_interface folder should go in the sopc builder default library component directory (/altera/11.0/ip/sopc_builder_ip/) 

 

The way our university has set it up, these files require root permissions to change on the local machines, so I will have a chat to the admins to see if I can get them to put it in this directory for me. 

The work around I found was to put an ip folder in the quartus project folder and then extract the zip file there: 

(~/Altera_Systems_Labs/De0_Counter/ip/altera_up_sd_card_interface/) 

De0_Counter being the project folder. 

I ran ip-make-ipx from De0_Counter to generate components.ipx which sopc_builder uses to find components. Now there's no need to specify the IP search path in sopc builder. 

Launching quartus and sopc builder the component is now available in the Project Library on the left hand side. Added the components and gave the instance the name: "Altera_UP_SD_Card" case sensitive. 

This step is important, since all of the macros in the header files are looking for that name. 

Generate in sopc builder, compile in quartus, clean project in nios2-ide. 

At this point, the alt_sys_init.c is generated. In my nios2 project (FreeRTOS) it is located: 

~/Altera_Systems_Labs/New_FreeRTOS/FreeRTOS/Demo/NiosII_CycloneIII_DBC3C40_GCC/RTOSDemo_syslib/Debug/system_description/alt_sys_init.c 

I haven't managed to get the macros from the header file included automatically, hopefully that will be sorted by putting it in the default library path. But the lines that I added to this file were from the bottom of altera_up_sd_card_avalon_interface.h: 

 

@72:# include "altera_up_sd_card_avalon_interface.h" 

@85: ALTERA_UP_SD_CARD_AVALON_INTERFACE_INSTANCE( ALTERA_UP_SD_CARD, altera_up_sd_card ); 

@114: ALTERA_UP_SD_CARD_AVALON_INTERFACE_INIT( ALTERA_UP_SD_CARD, altera_up_sd_card ); 

 

Using FreeRTOS so added a Task that calls the demo code available in the PDFs 

ftp://ftp.altera.com/up/pub/altera_material/11.0/university_program_ip_cores/memory/sd_card_interfac... 

 

Have a look in the header files for the functions available. So some functions I wrote: 

 

int sd_card_read_names(void) 

int number_of_files = 0; 

char buffer_name[25]; 

short int handler; 

 

handler = alt_up_sd_card_find_first("/.", buffer_name); 

printf("%d, %s \n", handler, buffer_name); 

while ((handler = alt_up_sd_card_find_next(buffer_name)) != -1) 

number_of_files++; 

printf("%d, %s \n", handler, buffer_name); 

printf("Number of files: %d\n",number_of_files); 

alt_up_sd_card_fclose(handler); 

return number_of_files;  

 

/*-----------------------------------*/ 

 

int sd_card_read_file(char *file_name) 

int number_of_chars = 0; 

short handler; 

short read; 

char cFileLine[100] = {0}; 

 

if ((handler = alt_up_sd_card_fopen(file_name, false)) != -1) 

printf("looking for the file %s\n",file_name); 

while ((read = alt_up_sd_card_read(handler)) != -1){ 

printf("%c", read); 

cFileLine[number_of_chars] = (char)read; 

number_of_chars++; 

printf("'EOF'\nnumber_of_chars = %d\n",number_of_chars); 

alt_up_sd_card_fclose(handler); 

handler = 0; 

if (handler == -1) 

printf("File not found\n"); 

}  

return number_of_chars;  

 

Hope that helps all those who are struggling with the SD card too. 

 

I found that the files it writes don't have the proper attributes and appear to have been created in 1601 (from windows). Windows created .txt files are 36 bytes min, whereas files created with the altera ip seem to be as long as the characters written to it. Linux can read the files fine with cat. Haven't got friendly with the get and set attributes yet. I'll leave that up to you to figure out ;) 

 

Peace
0 Kudos