Nios® II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
Announcements
Intel Support hours are Monday-Fridays, 8am-5pm PST, except Holidays. Thanks to our community members who provide support during our down time or before we get to your questions. We appreciate you!

Need Forum Guidance? Click here
Search our FPGA Knowledge Articles here.

Need to undestand SD_Card.h

Altera_Forum
Honored Contributor II
822 Views

Hi, so I'm just a newbie and I plan to use the SD card of my Altera DE2 board for a project. I managed to find the example that comes with the CD named as DE2_SD_Card_Audio. So I saw that there's a header file named "SD_Card.h". I understand this is the one responsible for reading files that's inside the SD Card. Can someone help me understand the codes in this header file?  

#ifndef __SD_Card_H__ # define __SD_Card_H__ //------------------------------------------------------------------------- // SD Card Set I/O Direction # define SD_CMD_IN IOWR(SD_CMD_BASE, 1, 0) # define SD_CMD_OUT IOWR(SD_CMD_BASE, 1, 1) # define SD_DAT_IN IOWR(SD_DAT_BASE, 1, 0) # define SD_DAT_OUT IOWR(SD_DAT_BASE, 1, 1) // SD Card Output High/Low # define SD_CMD_LOW IOWR(SD_CMD_BASE, 0, 0) # define SD_CMD_HIGH IOWR(SD_CMD_BASE, 0, 1) # define SD_DAT_LOW IOWR(SD_DAT_BASE, 0, 0) # define SD_DAT_HIGH IOWR(SD_DAT_BASE, 0, 1) # define SD_CLK_LOW IOWR(SD_CLK_BASE, 0, 0) # define SD_CLK_HIGH IOWR(SD_CLK_BASE, 0, 1) // SD Card Input Read # define SD_TEST_CMD IORD(SD_CMD_BASE, 0) # define SD_TEST_DAT IORD(SD_DAT_BASE, 0) //------------------------------------------------------------------------- # define BYTE unsigned char # define UINT16 unsigned int # define UINT32 unsigned long //------------------------------------------------------------------------- void Ncr(void); void Ncc(void); BYTE response_R(BYTE); BYTE send_cmd(BYTE *); BYTE SD_read_lba(BYTE *,UINT32,UINT32); BYTE SD_card_init(void); //------------------------------------------------------------------------- BYTE read_status; BYTE response_buffer; BYTE RCA; BYTE cmd_buffer; const BYTE cmd0 = {0x40,0x00,0x00,0x00,0x00}; const BYTE cmd55 = {0x77,0x00,0x00,0x00,0x00}; const BYTE cmd2 = {0x42,0x00,0x00,0x00,0x00}; const BYTE cmd3 = {0x43,0x00,0x00,0x00,0x00}; const BYTE cmd7 = {0x47,0x00,0x00,0x00,0x00}; const BYTE cmd9 = {0x49,0x00,0x00,0x00,0x00}; const BYTE cmd16 = {0x50,0x00,0x00,0x02,0x00}; const BYTE cmd17 = {0x51,0x00,0x00,0x00,0x00}; const BYTE acmd6 = {0x46,0x00,0x00,0x00,0x02}; const BYTE acmd41 = {0x69,0x0f,0xf0,0x00,0x00}; const BYTE acmd51 = {0x73,0x00,0x00,0x00,0x00}; //------------------------------------------------------------------------- void Ncr(void) { SD_CMD_IN; SD_CLK_LOW; SD_CLK_HIGH; SD_CLK_LOW; SD_CLK_HIGH; } //------------------------------------------------------------------------- void Ncc(void) { int i; for(i=0;i<8;i++) { SD_CLK_LOW; SD_CLK_HIGH; } } //------------------------------------------------------------------------- BYTE SD_card_init(void) { BYTE x,y; SD_CMD_OUT; SD_DAT_IN; SD_CLK_HIGH; SD_CMD_HIGH; SD_DAT_LOW; read_status=0; for(x=0;x<40;x++) Ncr(); for(x=0;x<5;x++) cmd_buffer=cmd0; y = send_cmd(cmd_buffer); do { for(x=0;x<40;x++); Ncc(); for(x=0;x<5;x++) cmd_buffer=cmd55; y = send_cmd(cmd_buffer); Ncr(); if(response_R(1)>1) //response too long or crc error return 1; Ncc(); for(x=0;x<5;x++) cmd_buffer=acmd41; y = send_cmd(cmd_buffer); Ncr(); } while(response_R(3)==1); Ncc(); for(x=0;x<5;x++) cmd_buffer=cmd2; y = send_cmd(cmd_buffer); Ncr(); if(response_R(2)>1) return 1; Ncc(); for(x=0;x<5;x++) cmd_buffer=cmd3; y = send_cmd(cmd_buffer); Ncr(); if(response_R(6)>1) return 1; RCA=response_buffer; RCA=response_buffer; Ncc(); for(x=0;x<5;x++) cmd_buffer=cmd9; cmd_buffer = RCA; cmd_buffer = RCA; y = send_cmd(cmd_buffer); Ncr(); if(response_R(2)>1) return 1; Ncc(); for(x=0;x<5;x++) cmd_buffer=cmd7; cmd_buffer = RCA; cmd_buffer = RCA; y = send_cmd(cmd_buffer); Ncr(); if(response_R(1)>1) return 1; Ncc(); for(x=0;x<5;x++) cmd_buffer=cmd16; y = send_cmd(cmd_buffer); Ncr(); if(response_R(1)>1) return 1; read_status =1; //sd card ready return 0; } //------------------------------------------------------------------------- BYTE SD_read_lba(BYTE *buff,UINT32 lba,UINT32 seccnt) { BYTE c=0; UINT32 i,j; lba+=101; for(j=0;j<seccnt;j++) { { Ncc(); cmd_buffer = cmd17; cmd_buffer = (lba>>15)&0xff; cmd_buffer = (lba>>7)&0xff; cmd_buffer = (lba<<1)&0xff; cmd_buffer = 0; lba++; send_cmd(cmd_buffer); Ncr(); } while(1) { SD_CLK_LOW; SD_CLK_HIGH; if(!(SD_TEST_DAT)) break; } for(i=0;i<512;i++) { BYTE j; for(j=0;j<8;j++) { SD_CLK_LOW; SD_CLK_HIGH; c <<= 1; if(SD_TEST_DAT) c |= 0x01; } *buff=c; buff++; } for(i=0; i<16; i++) { SD_CLK_LOW; SD_CLK_HIGH; } } read_status = 1; //SD data next in return 0; } //------------------------------------------------------------------------- BYTE response_R(BYTE s) { BYTE a=0,b=0,c=0,r=0,crc=0; BYTE i,j=6,k; while(1) { SD_CLK_LOW; SD_CLK_HIGH; if(!(SD_TEST_CMD)) break; if(crc++ >100) return 2; } crc =0; if(s == 2) j = 17; for(k=0; k<j; k++) { c = 0; if(k > 0) //for crc culcar b = response_buffer; for(i=0; i<8; i++) { SD_CLK_LOW; if(a > 0) c <<= 1; else i++; a++; SD_CLK_HIGH; if(SD_TEST_CMD) c |= 0x01; if(k > 0) { crc <<= 1; if((crc ^ b) & 0x80) crc ^= 0x09; b <<= 1; crc &= 0x7f; } } if(s==3) { if( k==1 &&(!(c&0x80))) r=1; } response_buffer = c; } if(s==1 || s==6) { if(c != ((crc<<1)+1)) r=2; } return r; } //------------------------------------------------------------------------- BYTE send_cmd(BYTE *in) { int i,j; BYTE b,crc=0; SD_CMD_OUT; for(i=0; i < 5; i++) { b = in; for(j=0; j<8; j++) { SD_CLK_LOW; if(b&0x80) SD_CMD_HIGH; else SD_CMD_LOW; crc <<= 1; SD_CLK_HIGH; if((crc ^ b) & 0x80) crc ^= 0x09; b<<=1; } crc &= 0x7f; } crc =((crc<<1)|0x01); b = crc; for(j=0; j<8; j++) { SD_CLK_LOW; if(crc&0x80) SD_CMD_HIGH; else SD_CMD_LOW; SD_CLK_HIGH; crc<<=1; } return b; } //------------------------------------------------------------------------- # endif
0 Kudos
0 Replies
Reply