|
bootloader 从sd卡引导wince的问题
[复制链接]
为了方便更新wince系统,想做sd上引导wince,bootloader 还是在nandflash上。网上下了bootloader源码,,sd卡初始化成功了,fat初始化也成功,能够读出sd的容量,但是查找boot.txt文件失败,无法加载。我跟踪发现在读sd卡0扇区时,读出了的Mbr就不对,sd_read_sector 读出来的数是乱码。所以fat_findItem()就找不到文件。我的源码sd卡好像是sd bus的,不是网上常见的那种spi的方式。现在不是很懂,希望大家提点意见,看看问题出在哪里?是否sd初始化错误?初始化和读扇区源码贴在下面
/*********************************************************************************************************
** 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;
}
|
|