SD卡引导WinCE启动引发的种种问题
前几天在调试SD卡引导WinCE内核的Bootloader,功能调试成功后系统可以从SD卡启动,但是发现有以下问题:
SD卡只能工作在5MHz以下,确切的说SDIPRE的预分频值只能是13,如果换成其它频率,那么在Bootloader运行完毕跳转到内核启动地址(比如0x30200000)的时候会卡死,不知道是什么原因。这样的话从上电开机到内核初始化大约需要一分钟的时间,其中大部分时间应该是消耗在了内核拷贝的过程上。接着,内核初始化完毕以后又需要等待大约2分钟才能在LCD上看到桌面,这是为什么?我回忆了一下,如果是Nand引导的话,会事先通过EBOOT将TOC写到Nand的Block1中,启动的时候Bootloader会根据TOC的指示加载内核镜像,这样从内核初始化完毕到看到系统桌面大约2秒,不知道上面的现象是不是这个原因,如果是的话如何解决?如何在SD卡中实现?
请教各位指点~
我的SD卡容量128MB,支持的最高工作频率是50MHz(从CSD读出来的),我的程序是参照坛里一位前辈的修改的:
http://topic.csdn.net/u/20090811/16/B0924B18-F28D-41FA-8656-3450C331D40A.html
我把主要代码再贴一遍:
void xmain(void)
{
INT8U ret;
U32 j;
MMU_EnableICache();
ChangeClockDivider(3, 1);
ChangeMPllValue(127, 2, 1);//405MHZ
Isr_Init();
Uart_Init(0, 115200);
Uart_Select(0);
Delay100us(12000);
Uart_Printf("The main is running\n");
NF_Init();
ret = SD_Initialize();
if (ret == NO_ERR)
{
//初始化SD卡成功
Uart_Printf("SD_Initalize Success\n");
ret = FAT_Init();
if (ret == NO_ERR)
{
//初始化FAT成功
Uart_Printf("\nFAT Initalize Success\n");
FAT_DisDir("\\");
RunByBootFile("\\BOOT.TXT");
}
else
{
//初始化FAT失败
Uart_Printf("\nFAT Initalize error:%d\n", ret);
}
}
else
{
//初始化SD卡失败
Uart_Printf("SD_initalize error:%d\n", ret);
}
Uart_Printf("\nPress Key 'U' Enter USBMON\n");
for (j = 0;j < 10;j++)
{
Uart_Printf("*");
Delay100us(12000);
if (rUTRSTAT0 & 0x1) //UART 有数据输入
{
ret = RdURXH0();
if ((ret == 'U') || (ret == 'u'))
{
Uart_Printf("\nUSBMON\n");
Run_USBMON(); //启动USBMON
}
}
}
Uart_Printf("\n");
ret = RunImageFromNand(1, 0);
Uart_Printf("\nBoot ERROR:%d", ret);
while (1);
}
/*********************************************************************************************************
** Function name: SD_Initialize
** Descriptions: initialize SD/MMC card
** 初始化SD/MMC卡
** Input: sd_struct *sds: SD卡信息结构体 sd_struct *sds: the information structure of SD Card
** Output: 0: 正确 >0: 错误码 0: right >0: error code
** Created by: MingYuan Zheng 郑明远
** Created Date: 2006-01-09
**-------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------
********************************************************************************************************/
INT8U SD_Initialize(void) //(sd_struct *sds)
{
INT8U response[16], ret;
SD_HardWareInit(); /* 1. 初始化SD/MMC接口硬件 Initialize the hardware of SD/MMC interface */
sds.timeout_erase = 0x7fffff;
sds.timeout_read = 0x7fffff;
sds.timeout_write = 0x7fffff;
Debug_Printf("\nSD_Initialize start");
ret = SD_ResetSD();
if (ret != NO_ERR) /* 2. 发出CMD0命令复位SD卡 send CMD0 command to reset SD/MMC card */
return ret;
sds.card_type = Card_Indentify(); /* 3. 判别卡的型号:SD或MMC卡 identify card type: SD or MMC card */
Debug_Printf("card_type:%d\n",sds.card_type);
ret = SD_ActiveCard(); //sds); /* 4. 激活SD/MMC卡 active SD/MMC card */
if (ret != NO_ERR)
return ret;
ret = SD_ReadAllCID(16, response); /* 5. 请所有卡发送CID寄存器 ask all card send their CID number */
if (ret != NO_ERR)
return ret;
if (sds.RCA == 0)
{
card_id++;
sds.RCA = card_id; /* 给卡分配一个地址 assign a address */
}
ret = SD_GetRCA(sds.card_type, &sds.RCA); /* 6. 得到卡的RCA get the RCA of the card */
if (ret != NO_ERR)
return ret;
ret = SD_GetCardInfo(); //sds); /* 7. 读CSD寄存器,获取SD卡信息 read CSD register, get the information of SD card */
if (ret != NO_ERR)
return ret;
SD_ClkToMax(); /* 8. 设置读/写SD/MMC时钟到最大值 set clock of reading or writing SD/MMC to maximum */
return (SD_SetBlockLen(sds.RCA, SD_BLOCKSIZE)); /* 9. 设置块的长度: 512Bytes Set the block length: 512Bytes */
}
/*********************************************************************************************************
** Function name: SD_ReadBlock
** Descriptions: read a block from SD/MMC card
** 从SD/MMC卡中读出一个块
** Input: sd_struct *sds : SD/MMC卡信息结构体 sd_struct *sds : the information structure of SD/MMC Card
INT32U blockaddr: 块地址 INT32U blockaddr: the address of the block
INT8U *recbuf : 接收缓冲区,长度512Bytes INT8U *recbuf : the buffer of receive,length is 512Bytes
** Output: 1: 成功 > 0: 错误码 1: sucessfully > 0: error code
** Created by: MingYuan Zheng 郑明远
** Created Date: 2006-01-09
**-------------------------------------------------------------------
** Modified by:
** Modified Date:
**------------------------------------------------------------------
********************************************************************************************************/
INT8U SD_ReadBlock(INT32U blockaddr, INT8U *recbuf)
{
INT8U ret, status[4];
// INT32U stat = 0;
// Debug_Printf("\nSD_ReadBlock start");
ret = SD_ReadCard_Status(sds.RCA, 4, status); /* 读取卡的状态 read the status of the card */
if (ret != NO_ERR)
return ret;
ret = SD_SelectCard(sds.RCA); /* CMD7,进入传输状态 CMD7, enter tranfer status */
if (ret != NO_ERR)
return ret;
if (sds.card_type == CARDTYPE_SD)
{ /* 卡为SD卡 card is SD card */
ret = SD_SetBusWidth(sds.RCA, 1); /* 设置卡为数据总线方式 set card to wide data bus */
if (ret != NO_ERR)
return ret;
}
#if SD_INTERRUPT_EN
rSDIIMSK = SDIIMSK_TOUT | SDIIMSK_DFIN | SDIIMSK_RX_LAST | SDIIMSK_RX_FULL;
reading = 1;
buffer = recbuf; /* 数据缓冲区指针 buffer pointer */
bufcnt = 0;
datalen = SD_BLOCKSIZE; /* 接收数据长度 receive data length */
error = SD_ERR_TIMEOUT_READ; /* be initialized to error code */
#endif
// if (sds.card_type == CARDTYPE_SD)
// stat = SDIDCON_RACMD_1 | SDIDCON_BLK | SDIDCON_RX | SDIDCON_WIDE | 1 << 0;
// else
// stat = SDIDCON_RACMD_1 | SDIDCON_BLK | SDIDCON_RX | 1 << 0;
rSDIBSIZE = SD_BLOCKSIZE; /* 块数据的长度 block data length */
rSDICON |= SDICON_FRESET; /* 复位 FIFO reset FIFO */
// rSDIDCON = stat; /* 写数据控制寄存器 write data control register */
rSDIFSTA = rSDIFSTA | (1 << 16); // FIFO reset
rSDIDCON = (2 << 22) | (1 << 19) | (1 << 17) | (1 << 16) | (1 << 14) | (2 << 12) | (1 << 0); //YH 040220
//Word Rx, Rx after cmd, blk, 4bit bus, Rx start, blk num, data start, data transmit mode
rSDICARG = 0x0; // CMD17/18(addr)
ret = SD_ReadSingleBlock(blockaddr); /* 读单块命令 read single blocks command */
if (ret != NO_ERR)
return ret;
#if SD_INTERRUPT_EN
interruptible_sleep_on(&wq); /* 进入可被中断的睡眠状态, 数据在中断中自动接收 */
#ifdef CHECK_CARD_EN
if (!card_insert)
{
return SD_ERR_NO_CARD; /* 卡未插入,返回错误 */
}
#endif /* CHECK_CARD_EN */
if (error)
{ /* there is error, return error code */
return error;
}
ret = NO_ERR;
#else /* 读出数据 read data from sd card */
ret = SD_ReadBlockData(SD_BLOCKSIZE, recbuf, sds.timeout_read);
#endif
if (ret != NO_ERR)
return ret;
ret = SD_DeSelectCard(); /* CMD7, 退出传输状态 */
return NO_ERR;
}
[解决办法]