📄 sdi.c
字号:
#include <stdio.h>
#include <string.h>
#include "def.h"
#include "option.h"
#include "2410addr.h"
#include "2410lib.h"
#include "sdi.h"
#define INICLK 400000//300000
#define NORCLK 5000000
#define POL 0
#define INT 1
#define DMA 2
int CMD13(void); // Send card status
int CMD9(void);
// Global variables
int *Tx_buffer; //128[word]*16[blk]=8192[byte]
int *Rx_buffer; //128[word]*16[blk]=8192[byte]
volatile unsigned int rd_cnt;
volatile unsigned int wt_cnt;
volatile unsigned int block;
volatile unsigned int TR_end=0;
int Wide=0; // 0:1bit, 1:4bit
int MMC=0; // 0:SD , 1:MMC
int Maker_ID;
char Product_Name[7];
int Serial_Num;
volatile int RCA;
/*****************************************
SD卡测试函数
函数名: Test_SDI
描述: SD卡测试函数
返回值:void
*****************************************/
void Test_SDI(void)
{
RCA=0;
MMC=0;
//GPEUP GPE[15:0] 1: The pull-up function is disabled.
rGPEUP = 0xf83f; // The pull up
//GPECON GPE5 [11:10] = 10 : SDCLK
//GPECON GPE6 [13:12] = 10 : SDCMD
//GPECON GPE7 [15:14] = 10 : SDDAT0
//GPECON GPE8 [17:16] = 10 : SDDAT1
//GPECON GPE9 [19:18] = 10 : SDDAT2
//GPECON GPE10 [21:20] = 10 : SDDAT3
rGPECON = 0xaaaaaaaa;
Uart_Printf("\n[SDI test]\n");
if(!SD_card_init())
return;
TR_Buf_new();
/*
INOM:
Uart_Printf("How many blocks to test?(1~16)?");
block=(U32)Uart_GetIntNum();
if((block==0)|block>16)
goto INOM;
//block=1;//tark
Uart_Printf("\n");
*/
block=5;
//CMD13();
Wt_Block();
Rd_Block();
View_Rx_buf();
if(MMC)
TR_Buf_new();
if(MMC)
{
Wt_Stream();
Rd_Stream();
View_Rx_buf();
}
Card_sel_desel(0); // Card deselect
if(!CMD9()) //cmd9()==0,fail
Uart_Printf("Get CSD fail!!!\n");
//SDIDCON BlkNum [11: 0] = 0 : Block Number (0~4095).
//SDIDCON Data Transfer Mode (DatMode) [13:12] = 0 : ready,
//SDIDCON Stop by force (STOP) [14 ] = 0 : normal
//SDIDCON DMA Enable(EnDMA) [15 ] = 0 : disable(polling),
//SDIDCON Wide bus enable (WideBus) [16 ] = 0 : standard bus mode(only SDIDAT[0] used),
//SDIDCON Block mode (BlkMode) [17 ] = 0 : stream data transfer,
//SDIDCON Busy AfterCommand(BACMD) [18 ] = 0 : directly after DatMode set,
//SDIDCON Receive After Command (RACMD) [19 ] = 0 : directly after DatMode set,
//SDIDCON Transmit After Response(TARSP) [20 ] = 0 : directly after DatMode set,
//SDIDCON SDIO InterruptPeriodType(PrdType)[21 ] = 0 : exactly 2 cycle,
rSDIDCON=0;
//SDICSTA RspIndex [7:0] R Response index 6bit with start 2bit (8bit)
//SDICSTA CMD line progress On (CmdOn) [8 ] R Command transfer in progress.
//SDICSTA Response Receive End (RspFin)[9 ] = 1 : response end
//SDICSTA Command Time Out (CmdTout) [10 ] = 1 : timeout
//SDICSTA Command Sent (CmdSent) [11 ] = 1 : command end
//SDICSTA Response CRC Fail(RspCrc [12 ] = 1 : crc fail
rSDICSTA=0xffff;
}
void TR_Buf_new(void)
{
//-- Tx & Rx Buffer initialize
int i, j;
int start = 0x03020100;
Tx_buffer=(int *)0x31000000;
j=0;
for(i=0;i<2048;i++) //128[word]*16[blk]=8192[byte]
*(Tx_buffer+i)=i+j; //i+j's value sent into the address
// *(Tx_buffer+i)=0x5555aaaa;
Flush_Rx_buf();
/*
for(i=0;i<20;i++){
for(j=0;j<128;j++){
Tx_buffer[j+i*128]=start;
if(j % 64 == 63) start = 0x0302010;
else start = start + 0x04040404;
}
start = 0x03020100;
}
*/
}
void Flush_Rx_buf(void)
{
//-- Flushing Rx buffer
int i;
Rx_buffer=(int *)0x31800000;
for(i=0;i<2048;i++) //128[word]*16[blk]=8192[byte]
*(Rx_buffer+i)=0;
// Uart_Printf("\n--End Rx buffer flush\n");
}
/*****************************************
写入和读出缓冲区数据检查函数
函数名: View_Rx_buf
描述: 检测写入和输出数据
返回值:
*****************************************/
void View_Rx_buf()
{
//-- Display Rx buffer
int i,error=0;
/*
for(i=0;i<2048;i++)
Uart_Printf("RB[%02x]=%x,",Rx_buffer[i]);
*/
Tx_buffer=(int *)0x31000000;
Rx_buffer=(int *)0x31800000;
Uart_Printf("Check Rx data\n");
for(i=0;i<128*block;i++)
{
if(Rx_buffer[i] != Tx_buffer[i])
{
Uart_Printf("\nTx/Rx error\n");
Uart_Printf("%d:Tx-0x%08x, Rx-0x%08x\n",i,Tx_buffer[i], Rx_buffer[i]);
error=1;
break;
}
Uart_Printf(".");
}
if(!error)
Uart_Printf(" O.K.\n");
}
void View_Tx_buf(void)
{
//-- Display Tx buffer
int i;
for(i=0;i<2048;i++)
Uart_Printf("TB[%02x]=%x,",Tx_buffer[i]);
}
/*****************************************
SD卡初始化函数
函数名: SD_card_init
描述: SD卡的初始化
返回值:void
*****************************************/
int SD_card_init(void)
{
//-- SD controller & card initialize
int i;
char key;
/* Important notice for MMC test condition */
/* Cmd & Data lines must be enabled pull up resister */
//SDIPRE Prescaler Value [7:0] = PCLK/(2*INICLK)-1 :Baud rate = PCLK / 2 / (Prescaler value + 1)
rSDIPRE=PCLK/(2*INICLK)-1; // 400KHz
//SDICON Clock Out Enable (ENCLK) [0] = 1 : clock enable
//SDICON FIFO Reset (FRST) [1] = 1 : FIFO reset
//SDICON Byte Order Type(ByteOrder)[4] = 1 : Type B
rSDICON=(1<<4)|(1<<1)|1;
//SDIBSIZE BlkSize [11:0] = 0x200 : 512byte(128word)
rSDIBSIZE=0x200;
//SDIDTIMER DataTimer [15:0] = 0xffff : Data / busy timeout period (0~65535 cycle)
rSDIDTIMER=0xffff;
for(i=0;i<0x1000;i++); // Wait 74SDCLK for MMC card
//Uart_Printf("rSDIRSP0=0x%x\n",rSDIRSP0);
CMD0();
Uart_Printf("\nIn idle\n");
//-- Check MMC card OCR
if(Chk_MMC_OCR())
{
Uart_Printf("\nIn MMC ready\n");
MMC=1;
goto RECMD2;
}
//Uart_Printf("MMC check end!!\n");
//-- Check SD card OCR
if(Chk_SD_OCR())
Uart_Printf("\nIn SD ready\n");
else
{
Uart_Printf("\nInitialize fail\nNo Card assertion\n");
return 0;
}
RECMD2:
//-- Check attaced cards, it makes card identification state
//SDICARG CmdArg [31:0] = 0 : Command Argument
rSDICARG=0x0; // CMD2(stuff bit)
//SDICCON CmdIndex [7:0] = 0X42 : Command index with start 2bit (8bit)
//SDICCON Command Start(CMST) [8 ] = 1 : command start
//SDICCON WaitRsp [9 ] = 1 : wait response
//SDICCON LongRsp [10 ] = 1 : long response
rSDICCON=(0x1<<10)|(0x1<<9)|(0x1<<8)|0x42; //lng_resp, wait_resp, start, CMD2
//-- Check end of CMD2
if(!Chk_CMDend(2, 1))
goto RECMD2;
//rSDICSTA=0xa00; // Clear cmd_end(with rsp)
Uart_Printf("\nEnd id\n");
RECMD3:
//--Send RCA
//SDICARG CMD3(MMC:Set RCA, SD:Ask RCA-->SBZ)
rSDICARG=MMC<<16;
//SDICCON CmdIndex [7:0] = 0X43 : Command index with start 2bit (8bit)
//SDICCON Command Start(CMST) [8 ] = 1 : command start
//SDICCON WaitRsp [9 ] = 1 : wait response
//SDICCON LongRsp [10 ] = 0 : short response
rSDICCON=(0x1<<9)|(0x1<<8)|0x43; // sht_resp, wait_resp, start, CMD3
//-- Check end of CMD3
if(!Chk_CMDend(3, 1))
goto RECMD3;
//rSDICSTA=0xa00; // Clear cmd_end(with rsp)
//--Publish RCA
if(MMC)
RCA=1;
else
RCA=( rSDIRSP0 & 0xffff0000 )>>16;
Uart_Printf("RCA=0x%x\n",RCA);
//--State(stand-by) check
if( rSDIRSP0 & 0x1e00!=0x600 ) // CURRENT_STATE check
goto RECMD3;
Uart_Printf("\nIn stand-by\n");
//SDIPRE Prescaler Value [7:0] = PCLK/(2*NORCLK)-1 : Normal clock=25MHz
rSDIPRE=PCLK/(2*NORCLK)-1;
Card_sel_desel(1); // Select
if(!MMC)
Set_4bit_bus();
else
Set_1bit_bus();
return 1;
}
void Card_sel_desel(char sel_desel)
{
//-- Card select or deselect
if(sel_desel)
{
RECMDS7:
//SDICARG CmdArg [31:0] = RCA<<16 : CMD7(RCA,stuff bit)
rSDICARG=RCA<<16;
//SDICCON CmdIndex [7:0] = 0X47 : Command index with start 2bit (8bit)
//SDICCON Command Start(CMST) [8 ] = 1 : command start
//SDICCON WaitRsp [9 ] = 1 : wait response
//SDICCON LongRsp [10 ] = 0 : short response
rSDICCON= (0x1<<9)|(0x1<<8)|0x47; // sht_resp, wait_resp, start, CMD7
//-- Check end of CMD7
if(!Chk_CMDend(7, 1))
goto RECMDS7;
//rSDICSTA=0xa00; // Clear cmd_end(with rsp)
//--State(transfer) check
if( rSDIRSP0 & 0x1e00!=0x800 )
goto RECMDS7;
}
else
{
RECMDD7:
//SDICARG CmdArg [31:0] = 0<<16 : CMD7(RCA,stuff bit)
rSDICARG=0<<16; //CMD7(RCA,stuff bit)
//SDICCON CmdIndex [7:0] = 0X47 : Command index with start 2bit (8bit)
//SDICCON Command Start(CMST) [8 ] = 1 : command start
//SDICCON WaitRsp [9 ] = 0 : no respons
//SDICCON LongRsp [10 ] = 0 : short response
rSDICCON=(0x1<<8)|0x47; //no_resp, start, CMD7
//-- Check end of CMD7
if(!Chk_CMDend(7, 0))
goto RECMDD7;
//rSDICSTA=0x800; // Clear cmd_end(no rsp)
}
}
void __irq Rd_Int(void)
{
U32 i,status;
status=rSDIFSTA;
if( (status&0x200) == 0x200 ) // Check Last interrupt?
{
for(i=(status & 0x7f)/4;i>0;i--)
{
*Rx_buffer++=rSDIDAT;
rd_cnt++;
}
}
else if( (status&0x80) == 0x80 ) // Check Half interrupt?
{
for(i=0;i<8;i++)
{
*Rx_buffer++=rSDIDAT;
rd_cnt++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -