用软件模拟SPI,一直调试不通,请各位高手指正程序错误,主要是还是感觉时序有点问题.但是一直基本上都是对的.还请那位仁兄指正程序错误, !!!!!!!!!!抱歉耽误5.1休息时间 调了2天了.还是没调出来. 感谢走过 ,路过,留言的人..
#define R0 0 #define R1 1 #define R1b 2 #define R2 3 #define R3 4 #include "44b.h" #include "Uart.h" #include "44blib.h" #include "DEF.h" uint32 looptime; uint8 rep[5];
#define nop() do{;}while(0);
#define CS_PIN 0 //CS GPF0 #define DI_PIN 7 //GPF5
#define SD_CS_PORT rPDATF #define SD_CS_DDR_PORT rPCONF #define SD_CS_PUP_PORT rPUPF
#define SD_DI_PORT rPDATF #define SD_DI_DDR_PORT rPCONF #define SD_DI_PUP_PORT rPUPF
#define DO_PIN 5 //EN GPF7 #define CLK_PIN 8 //RS GPF8
#define SD_DO_PORT rPDATF #define SD_DO_DDR_PORT rPCONF #define SD_DO_PUP_PORT rPUPF
#define SD_CLK_PORT rPDATF #define SD_CLK_DDR_PORT rPCONF #define SD_CLK_PUP_PORT rPUPF
#define CS_LOW() SD_CS_PORT &= ~(1<< CS_PIN) #define CS_HIGH() SD_CS_PORT |= (1<< CS_PIN) #define DI_LOW() SD_DI_PORT &= ~(1<< DI_PIN) #define DI_HIGH() SD_DI_PORT |= (1<< DI_PIN) #define CLK_LOW() SD_CLK_PORT &= ~(1<< CLK_PIN) #define CLK_HIGH() SD_CLK_PORT |= (1<< CLK_PIN) #define DO() SD_DO_PORT & 0xff #define _400KHZ_ 128 #define _10MHZ_ 1
//----------------------------------------------------------- //延时 void loop(uint32 i) //----------------------------------------------------------- { while (i--) { ;//nop(); } } //----------------------------------------------------------- //发送一字节数据 void SPI_send(uint8 cmd) //----------------------------------------------------------- { uint8 i; uint8 MASK[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
for (i = 0; i < 8; i++) { CLK_LOW();
if (cmd & MASK) DI_HIGH(); else DI_LOW();
loop(looptime);
CLK_HIGH(); loop(looptime); } }
//----------------------------------------------------------- //接收一字节数据 uint8 SPI_receive(void) //----------------------------------------------------------- { uint8 i; uint8 ret = 0; uint8 MASK[] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
for (i = 0; i < 8; i++) { CLK_LOW();
loop(looptime);
if (DO()) ret |= MASK;
CLK_HIGH(); loop(looptime); } return ret; }
//----------------------------------------------------------- //发送命令 uint8 SendCommand(uint8 cmd, uint32 arg, uint8 ACK) //----------------------------------------------------------- { uint8 status; uint8 i; uint8 len; CS_HIGH(); loop(74); CS_LOW();
SPI_send(cmd|0x40); SPI_send(arg>>24); SPI_send(arg>>16); SPI_send(arg>>8); SPI_send(arg); SPI_send(0x95); switch (ACK) { case R0: return 1; case R1: len=1; break; case R2: len=2; break; case R3: len=5; break; default: return 0; break; }
for (i = 0; i < 100; i++) { status = SPI_receive(); if (!(status & 0x80)) break; }
if (i >= 100) { SPI_send(0xff); return 0; }
rep[0] = status;
for (i = 1; i < len; i++) { rep = SPI_receive(); }
SPI_send(0xff);
return 1; }
//----------------------------------------------------------- void Reset(void) //----------------------------------------------------------- { CS_LOW();
SendCommand(0x00,0x00,R0);
CS_HIGH(); }
//----------------------------------------------------------- uint32 IsSD(void) //----------------------------------------------------------- { uint32 i;
CS_LOW();
for (i = 0; i < 1000; i++) { SendCommand(55,0x0000,R1);
SendCommand(41,0x0000,R1);
if (!rep[0]) break;
loop(200); }
CS_HIGH();
return i != 100; }
//----------------------------------------------------------- uint32 IsMMC(void) //----------------------------------------------------------- { uint32 i;
CS_LOW();
for (i = 0; i < 1000; i++) { SendCommand(1, 0x0000, R1);
if (!rep[0]) break;
loop(200); }
CS_HIGH();
return i != 100; }
//----------------------------------------------------------- int SD_erase(uint32 sector,uint8 *buff) //----------------------------------------------------------- { CS_LOW();
if (!SendCommand(32,sector<<9,R1)||!SendCommand(33,sector<<9,R1)||!SendCommand(38,0x0000,R1)) { CS_HIGH();
return 0; }
while (!DO()) SPI_send(0xff);
CS_HIGH(); return 1; }
uint32 SD_read(uint32 sector,uint8 *buff) { uint32 i;
CS_LOW();
if (!SendCommand(17,sector<<9,R1)) { CS_HIGH(); Uart_Printf("readerr1"); return 0; }
for (i = 0; i < 100; i++) { if (SPI_receive() == 0xfe) break; }
if (i >= 100) { CS_HIGH(); Uart_Printf("readerr2"); return 0; }
for (i = 0; i < 512; i++) { *buff++ = SPI_receive(); }
SPI_receive(); SPI_receive();
SPI_send(0xff);
CS_HIGH(); Uart_Printf("readOK");
return 1; }
uint32 SD_write(uint32 sector,uint8 *buff) { uint32 i;
CS_LOW();
SendCommand(24,sector<<9,R1);
SPI_send(0xfe);
for (i = 0; i < 512; i++) SPI_send(buff);
SPI_send(0xff); SPI_send(0xff); for (i = 0; i < 100; i++) { rep[0] = SPI_receive(); if (!rep[0]) break; } if (i >= 100) { Uart_Printf("writeerr1"); return 0; }
while (!DO()) SPI_send(0xff);
CS_HIGH(); Uart_Printf("writeOK"); //下面其实是复位 looptime =_400KHZ_; SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); IsSD(); looptime=_10MHZ_; //完 return 1; }
void SD_install(void) { looptime =_400KHZ_; SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff); SPI_send(0xff);
Reset();
if (!IsSD()&&!IsMMC()) { Uart_Printf("unknown card type!
"); return; } Uart_Printf("SD/MMC card found!
Now loading SD/MMC driver......
"); looptime=_10MHZ_; }
void SD_init(void) { rPCONF=0x082555;//PF8 :CLK,OUTPUT;PF7:DATAIN,INPUT;PF5:DATAOUT,OUTPUT;PF0,OUTPUT,CS SD_install(); }
|
|