.from: http://read.pudn.com/downloads102/sourcecode/embed/416830/FAT%E6%88%90%E5%8A%9F/sd.c__.htm
- /************************************************************/
- /* SD Code for AT91SAM7S64 */
- /* By pasyong */
- /* 2006-5 */
- /* Base IAR 4.30A */
- /************************************************************/
- /*-----------------------------------------------------------------------
- Ó²¼þÁ¬½Ó SD_CS-----------PA25
- -----------------------------------------------------------------------*/
- #include "ioat91sam7s64.h"
- #include "uart.h"
- #include "vs1011.h"
- #define uchar unsigned char
- #define uint unsigned int
- #define SD_CS ((unsigned int) 1 <<25)
- extern uchar BUFFER[512];
- uchar reading;
- //Ò»¸öÉÈÇø»º³åÇø
- void delay_Nus(unsigned int n)
- {
- unsigned char b;
- for (b = 1; b<n; b++)
- ;
- }
- /*-----------------------------------------------------------------------
- PORT_INITº¯Êý
- -----------------------------------------------------------------------*/
- void SD_port_init(void)
- {
- SPI_init();
- *AT91C_PIOA_PER|=SD_CS;
- //×÷ΪI/OʹÓÃ
- *AT91C_PIOA_OER|=SD_CS;
- //Êä³ö
- *AT91C_PIOA_SODR|=SD_CS;
- //Êä³ö¸ßµçƽ
- }
- //****************************************************************************
- //Send a Command to SD-Card
- //Return: the second byte of response register of SD-Card
- //****************************************************************************
- uchar SD_Write_Command(uchar cmd,unsigned long arg)
- {
- uchar tmp;
- uchar retry=0;
- //MMC_PORT|=MMC_CS_PIN; //SD¿¨¹Ø±Õ
- //send 8 Clock Impulse
- Write_Byte_SPI(0xFF);
- //set MMC_Chip_Select to low (MMC/SD-Card active)
- *AT91C_PIOA_CODR|=SD_CS; //SD¿¨Ê¹ÄÜ
- Write_Byte_SPI(cmd|0x40); //ËÍÍ·ÃüÁî
- Write_Byte_SPI(arg>>24);
- Write_Byte_SPI(arg>>16); //send 6 Byte Command to MMC/SD-Card
- Write_Byte_SPI(arg>>8);
- Write_Byte_SPI(arg&0xff);
- Write_Byte_SPI(0x95); //½ö½ö¶ÔRESETÓÐЧµÄCRCЧÑéÂë
- //get 8 bit response
- //Read_Byte_MMC(); //read the first byte,ignore it.
- do
- { //Only last 8 bit is used here.Read it out.
- tmp = Read_Byte_SPI();
- retry++;
- }
- while((tmp==0xff)&&(retry<100)); //µ±Ã»ÓÐÊÕµ½ÓÐЧµÄÃüÁîµÄʱºò
- if(reading==0)
- *AT91C_PIOA_SODR|=SD_CS; //MMC_CS_PIN=1;
- else *AT91C_PIOA_CODR|=SD_CS; //MMC_CS_PIN=0;
- return(tmp);
- }
- //****************************************************************************
- //SD¿¨³õʼ»¯(SPI-MODE)
- //****************************************************************************
- uchar SD_Init(void)
- {
- uchar retry,temp;
- uchar i;
- *AT91C_PIOA_CODR|=SD_CS; //SD¿¨Ê¹ÄÜ
- delay_Nus(250); //Wait MMC/SD ready...
- for (i=0;i<16;i++)
- {
- Write_Byte_SPI(0xff); //send 74 clock at least!!!
- }
- //Send Command CMD0 to MMC/SD Card
- retry=0;
- do
- { //retry 200 times to send CMD0 command
- temp=SD_Write_Command(0,0);
- retry++;
- if(retry==100)
- {
- ;//CMD0 Error!
- }
- }
- while(temp!=1);
- //Send Command CMD1 to MMC/SD-Card
- retry=0;
- do
- { //retry 100 times to send CMD1 command
- temp=SD_Write_Command(1,0);
- retry++;
- if(retry==100)
- {
- ;
- }
- }
- while(temp!=0);
- retry=0;
- SD_Write_Command(16,512); //ÉèÖÃÒ»´Î¶ÁдBLOCKµÄ³¤¶ÈΪ512¸ö×Ö½Ú
- *AT91C_PIOA_SODR|=SD_CS; //MMC_CS_PIN=1; //set MMC_Chip_Select to high
- return(0); //All commands have been taken.
- }
- //****************************************************************************
- //´ÓSD¿¨¶ÁÒ»¸öÉÈÇø Return 0 if no Error.
- //****************************************************************************
- uchar SD_Read_Block(unsigned long address)
- {
- uchar temp=0;uint i=0;
- reading=1;
- temp=SD_Write_Command(17,address); //¶Á³öRESPONSE
- while (Read_Byte_SPI()!= 0xfe)
- {;} //Ö±µ½¶ÁÈ¡µ½ÁËÊý¾ÝµÄ¿ªÊ¼Í·0XFE£¬²ÅÄüÌÐø
- for(i=0; i<512; i++)
- {
- BUFFER[i]=Read_Byte_SPI();
- }
- Read_Byte_SPI();//CRC - Byte
- Read_Byte_SPI();//CRC - Byte
- reading=0;
- *AT91C_PIOA_SODR|=SD_CS; //¹Ø±ÕSD¿¨
- return(temp);
- }
No comments:
Post a Comment