[BSP] Update sdcard driver in mini4020
This commit is contained in:
parent
8203b6004b
commit
f6a7136226
|
@ -14,15 +14,14 @@
|
|||
* 2010-10-13 Wangmeng Added sep4020 support
|
||||
*/
|
||||
|
||||
#include "sdcard.h"
|
||||
//#include <dfs_config.h>
|
||||
#include <rtthread.h>
|
||||
|
||||
volatile rt_int32_t RCA;
|
||||
#include "sdcard.h"
|
||||
|
||||
#ifdef RT_USING_DFS
|
||||
volatile rt_int32_t RCA;
|
||||
|
||||
/* RT-Thread Device Driver Interface */
|
||||
#include <rtthread.h>
|
||||
#include <dfs_fs.h>
|
||||
|
||||
/*GLOBAL SD DEVICE PONITER*/
|
||||
|
@ -57,14 +56,16 @@ static void sd_pwr(int sd_ctl)
|
|||
}
|
||||
|
||||
/*a nop operation to delay*/
|
||||
static void delay (U32 j)
|
||||
static void delay(U32 j)
|
||||
{
|
||||
U32 i;
|
||||
|
||||
for (i=0;i<j;i++)
|
||||
{};
|
||||
|
||||
for (i = 0; i < j; i++)
|
||||
{
|
||||
/* nothing */
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Send the command to set the data transfer mode
|
||||
* @param cmd:the command to sent
|
||||
|
@ -74,120 +75,117 @@ static void delay (U32 j)
|
|||
* @param num:number of blocks
|
||||
* @param mask:sdc interrupt mask
|
||||
*/
|
||||
static rt_err_t cmd_data(U16 cmd,U32 arg,U16 mode,U16 blk_len,U16 num,U16 mask)
|
||||
static rt_err_t cmd_data(U16 cmd, U32 arg, U16 mode, U16 blk_len, U16 num, U16 mask)
|
||||
{
|
||||
U32 i;
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
U32 to = 10000;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
*(RP)SDC_CLOCK_CONTROL = 0Xff00; //配置SD时钟,512分频,关闭SD 时钟
|
||||
*(RP)SDC_CLOCK_CONTROL = 0Xff04; //打开SD时钟,512分频,开启SD 时钟
|
||||
*(RP)SDC_INTERRUPT_STATUS_MASK = mask; //中断状态屏蔽寄存器赋值
|
||||
*(RP)SDC_CLOCK_CONTROL = 0Xff00;
|
||||
*(RP)SDC_CLOCK_CONTROL = 0Xff04;
|
||||
*(RP)SDC_INTERRUPT_STATUS_MASK = mask;
|
||||
|
||||
*(RP)SDC_TRANSFER_MODE = mode; //传输模式选择寄存器赋值
|
||||
*(RP)SDC_TRANSFER_MODE = mode;
|
||||
|
||||
*(RP)SDC_BLOCK_SIZE = blk_len; //数据块长度寄存器赋值
|
||||
*(RP)SDC_BLOCK_COUNT = num; //数据块数目寄存器赋值
|
||||
*(RP)SDC_ARGUMENT = arg; //命令参数寄存器赋值
|
||||
*(RP)SDC_COMMAND = cmd; //命令控制寄存器赋值
|
||||
*(RP)SDC_BLOCK_SIZE = blk_len;
|
||||
*(RP)SDC_BLOCK_COUNT = num;
|
||||
*(RP)SDC_ARGUMENT = arg;
|
||||
*(RP)SDC_COMMAND = cmd;
|
||||
|
||||
delay(10);
|
||||
|
||||
i = *(RP)SDC_INTERRUPT_STATUS & 0x1000;
|
||||
|
||||
while(i != 0x1000) //判断:是否命令发送完毕,并且收到响应
|
||||
while (i != 0x1000)
|
||||
{
|
||||
i = *(RP)SDC_INTERRUPT_STATUS & 0x1000;
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
to --;
|
||||
if(!to)
|
||||
if (!to)
|
||||
{
|
||||
EOUT("%s TIMEOUT\n",__FUNCTION__);
|
||||
EOUT("%s TIMEOUT\n", __FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
delay(160);
|
||||
|
||||
#ifdef USE_TIMEOUT
|
||||
//DBOUT("cmd_data TO is %d\n",to);
|
||||
#endif
|
||||
|
||||
return *(RP)SDC_RESPONSE0; //返回命令反馈信息
|
||||
return *(RP)SDC_RESPONSE0;
|
||||
}
|
||||
|
||||
static rt_err_t cmd_response(U16 Cmd,U32 Arg,U16 TransMode,U16 BlkLen,U16 Nob,U16 IntMask)
|
||||
static rt_err_t cmd_response(U16 Cmd, U32 Arg, U16 TransMode, U16 BlkLen, U16 Nob, U16 IntMask)
|
||||
{
|
||||
U32 i;
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
U32 to = 50000;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
*(RP)SDC_CLOCK_CONTROL=0Xff00; //配置SD时钟
|
||||
*(RP)SDC_CLOCK_CONTROL = 0Xff00;
|
||||
|
||||
*(RP)SDC_CLOCK_CONTROL=0Xff04; //打开SD时钟
|
||||
*(RP)SDC_CLOCK_CONTROL = 0Xff04;
|
||||
|
||||
|
||||
*(RP)SDC_INTERRUPT_STATUS_MASK=IntMask; //中断状态屏蔽寄存器赋值
|
||||
*(RP)SDC_TRANSFER_MODE=TransMode; //传输模式选择寄存器赋值
|
||||
*(RP)SDC_BLOCK_SIZE=BlkLen; //数据块长度寄存器赋值
|
||||
*(RP)SDC_BLOCK_COUNT=Nob; //数据块数目寄存器赋值
|
||||
*(RP)SDC_ARGUMENT=Arg; //命令参数寄存器赋值
|
||||
*(RP)SDC_COMMAND=Cmd; //命令控制寄存器赋值
|
||||
*(RP)SDC_INTERRUPT_STATUS_MASK = IntMask;
|
||||
*(RP)SDC_TRANSFER_MODE = TransMode;
|
||||
*(RP)SDC_BLOCK_SIZE = BlkLen;
|
||||
*(RP)SDC_BLOCK_COUNT = Nob;
|
||||
*(RP)SDC_ARGUMENT = Arg;
|
||||
*(RP)SDC_COMMAND = Cmd;
|
||||
|
||||
delay(10);
|
||||
|
||||
i = *(RP)SDC_INTERRUPT_STATUS & 0x1040;
|
||||
|
||||
while(i != 0x1040) //判断:命令发送完毕,并且收到响应,数据传输完毕。这三项是否已经都完成。
|
||||
while (i != 0x1040)
|
||||
{
|
||||
i = *(RP)SDC_INTERRUPT_STATUS & 0x1040;
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
to--;
|
||||
if(!to)
|
||||
if (!to)
|
||||
{
|
||||
EOUT("%s Timeout\n",__FUNCTION__);
|
||||
EOUT("%s Timeout\n", __FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
|
||||
//DBOUT("cmd_response TO is %d\n",to);
|
||||
delay(100);
|
||||
|
||||
return RT_EOK; //返回命令反馈信息
|
||||
return RT_EOK;
|
||||
}
|
||||
static rt_err_t cmd_wait(U16 Cmd,U32 Arg,U16 IntMask )
|
||||
|
||||
static rt_err_t cmd_wait(U16 Cmd, U32 Arg, U16 IntMask)
|
||||
{
|
||||
int i;
|
||||
#ifdef USE_TIMEOUT
|
||||
U32 to=200000;
|
||||
#endif
|
||||
#ifdef USE_TIMEOUT
|
||||
U32 to = 200000;
|
||||
#endif
|
||||
|
||||
*(RP)SDC_CLOCK_CONTROL=0Xff00; //配置SD时钟
|
||||
*(RP)SDC_CLOCK_CONTROL = 0Xff00;
|
||||
|
||||
*(RP)SDC_CLOCK_CONTROL=0Xff04; //打开SD时钟
|
||||
*(RP)SDC_CLOCK_CONTROL = 0Xff04;
|
||||
|
||||
*(RP)SDC_COMMAND=Cmd; //命令控制寄存器赋值
|
||||
*(RP)SDC_COMMAND = Cmd;
|
||||
|
||||
*(RP)SDC_INTERRUPT_STATUS_MASK=IntMask; //中断状态屏蔽寄存器赋值
|
||||
*(RP)SDC_INTERRUPT_STATUS_MASK = IntMask;
|
||||
|
||||
*(RP)SDC_ARGUMENT=Arg; //命令参数寄存器赋值
|
||||
*(RP)SDC_ARGUMENT = Arg;
|
||||
|
||||
i = *(RP)SDC_INTERRUPT_STATUS & 0x1000;
|
||||
|
||||
while(i != 0x1000) //判断:是否命令发送完毕,并且收到响应
|
||||
while (i != 0x1000)
|
||||
{
|
||||
i = *(RP)SDC_INTERRUPT_STATUS & 0x1000;
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
to--;
|
||||
if(!to)
|
||||
if (!to)
|
||||
{
|
||||
EOUT("%s Timeout\n",__FUNCTION__);
|
||||
EOUT("%s Timeout\n", __FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
|
@ -195,68 +193,6 @@ static rt_err_t cmd_wait(U16 Cmd,U32 Arg,U16 IntMask )
|
|||
|
||||
delay(10);
|
||||
|
||||
return RT_EOK; //返回命令反馈信息以及数值1
|
||||
}
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when a memory
|
||||
* block is allocated from heap memory.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
static rt_err_t sd_init(void)
|
||||
{
|
||||
rt_err_t err;
|
||||
#ifdef USE_TIMEOUT
|
||||
rt_uint32_t to=1000;
|
||||
#endif
|
||||
sd_pwr(1);
|
||||
|
||||
*(RP)SDC_SOFTWARE_RESET=0x0; //触发软复位,对其写0是进行reset
|
||||
delay(200);
|
||||
*(RP)SDC_SOFTWARE_RESET=0x1; //不触发软复位
|
||||
delay(200);
|
||||
|
||||
cmd_wait(0x08,0x0,0xfff); //CMD0,命令发送使能
|
||||
|
||||
do
|
||||
{
|
||||
err = cmd_wait(0x6ea,0x0,0xfff); //CMD55,以切换到ACMD命令
|
||||
|
||||
#ifdef USE_TIMEOUT
|
||||
if(err != RT_EOK)
|
||||
{
|
||||
EOUT("cmd_wait err in %s\n",__FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#endif
|
||||
|
||||
delay(3);
|
||||
err = cmd_wait(0x52a,0x80ff8000,0xfff); //ACMD41,向SD控制器发送命令,等待SD控制器确认收到命令
|
||||
if(err != RT_EOK)
|
||||
{
|
||||
EOUT("cmd_wait err in %s\n",__FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#ifdef USE_TIMEOUT
|
||||
to--;
|
||||
if(!to)
|
||||
{
|
||||
EOUT("%s timeout\n",__FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#endif
|
||||
|
||||
}while(*(RP)SDC_RESPONSE0<0X80008000);
|
||||
|
||||
#ifdef USE_TIMEOUT
|
||||
//DBOUT("%s TO is %d\n",__FUNCTION__,to);
|
||||
#endif
|
||||
|
||||
cmd_data(0x49,0X0,0X0,0x0,0x0,0Xfff);//CMD2,发送CID
|
||||
cmd_data(0x6a,0X0,0X0,0x0,0x0,0Xfff);//CMD3,询问卡片发出新的相关地址
|
||||
RCA = *(RP)SDC_RESPONSE0;
|
||||
cmd_data(0xea,RCA,0X0,0x0,0x0,0Xfff);//CMD7,设置选择性的相关参数
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
|
@ -266,16 +202,75 @@ static rt_err_t sd_init(void)
|
|||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
static rt_err_t sd_readblock(rt_uint32_t address, rt_uint8_t* buf)
|
||||
static rt_err_t sd_init(void)
|
||||
{
|
||||
U32 complete,i;
|
||||
rt_err_t err;
|
||||
#ifdef USE_TIMEOUT
|
||||
rt_uint32_t to = 1000;
|
||||
#endif
|
||||
sd_pwr(1);
|
||||
|
||||
*(RP)SDC_SOFTWARE_RESET = 0x0;
|
||||
delay(200);
|
||||
*(RP)SDC_SOFTWARE_RESET = 0x1;
|
||||
delay(200);
|
||||
|
||||
cmd_wait(0x08, 0x0, 0xfff);
|
||||
|
||||
do
|
||||
{
|
||||
err = cmd_wait(0x6ea, 0x0, 0xfff);
|
||||
|
||||
#ifdef USE_TIMEOUT
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
EOUT("cmd_wait err in %s\n", __FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#endif
|
||||
|
||||
delay(3);
|
||||
err = cmd_wait(0x52a, 0x80ff8000, 0xfff);
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
EOUT("cmd_wait err in %s\n", __FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#ifdef USE_TIMEOUT
|
||||
to--;
|
||||
if (!to)
|
||||
{
|
||||
EOUT("%s timeout\n", __FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#endif
|
||||
|
||||
}
|
||||
while (*(RP)SDC_RESPONSE0 < 0X80008000);
|
||||
|
||||
cmd_data(0x49, 0X0, 0X0, 0x0, 0x0, 0Xfff);
|
||||
cmd_data(0x6a, 0X0, 0X0, 0x0, 0x0, 0Xfff);
|
||||
RCA = *(RP)SDC_RESPONSE0;
|
||||
cmd_data(0xea, RCA, 0X0, 0x0, 0x0, 0Xfff);
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will set a hook function, which will be invoked when a memory
|
||||
* block is allocated from heap memory.
|
||||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
static rt_err_t sd_readblock(rt_uint32_t address, rt_uint8_t *buf)
|
||||
{
|
||||
U32 complete, i;
|
||||
rt_uint8_t temp;
|
||||
rt_err_t err;
|
||||
RT_UNUSED rt_uint32_t discard;
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
rt_uint32_t to = 10;
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
//rt_kprintf("in readblock:%x\n",address);
|
||||
//Clear all the errors & interrups
|
||||
|
@ -285,32 +280,32 @@ static rt_err_t sd_readblock(rt_uint32_t address, rt_uint8_t* buf)
|
|||
*(RP)DMAC_INTTCCLEAR &= ~0x1;
|
||||
|
||||
/*Clear read fifo*/
|
||||
*(RP)(SDC_INTERRUPT_STATUS_MASK) = ~(0x1<<9); //don't mask fifo empty
|
||||
while((*(RP)SDC_INTERRUPT_STATUS)&0x200 != 0x200)
|
||||
*(RP)(SDC_INTERRUPT_STATUS_MASK) = ~(0x1 << 9); //don't mask fifo empty
|
||||
while ((*(RP)SDC_INTERRUPT_STATUS) & 0x200 != 0x200)
|
||||
discard = *(RP)SDC_READ_BUFER_ACCESS;
|
||||
|
||||
/*DMAC2,word,size=0x80*/
|
||||
*(RP)DMAC_C2SRCADDR = SDC_READ_BUFER_ACCESS;
|
||||
*(RP)DMAC_C2DESTADDR = (rt_uint32_t)buf;
|
||||
*(RP)DMAC_C2CONTROL =0x20249b;
|
||||
*(RP)DMAC_C2CONTROL = 0x20249b;
|
||||
*(RP)DMAC_C2CONFIGURATION = 0x38d;
|
||||
|
||||
err = cmd_wait(0x6ea,RCA,0xfff);
|
||||
if(err != RT_EOK)
|
||||
err = cmd_wait(0x6ea, RCA, 0xfff);
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
rt_set_errno(err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = cmd_wait(0xca,0x2,0xfff);
|
||||
if(err != RT_EOK)
|
||||
err = cmd_wait(0xca, 0x2, 0xfff);
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
rt_set_errno(err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = cmd_response(0x22e,address,0X1,0x0200,0x1,0Xfff); //CMD17 4bit mode
|
||||
if(err != RT_EOK)
|
||||
err = cmd_response(0x22e, address, 0X1, 0x0200, 0x1, 0Xfff); //CMD17 4bit mode
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
rt_set_errno(err);
|
||||
return err;
|
||||
|
@ -319,28 +314,28 @@ static rt_err_t sd_readblock(rt_uint32_t address, rt_uint8_t* buf)
|
|||
complete = *(RP)SDC_INTERRUPT_STATUS;
|
||||
|
||||
/*CRC*/
|
||||
if((complete |0xfffffffd) !=0xfffffffd)
|
||||
if ((complete | 0xfffffffd) != 0xfffffffd)
|
||||
{
|
||||
rt_kprintf("CRC ERROR!!!\n");
|
||||
complete = *(RP)SDC_INTERRUPT_STATUS;
|
||||
}
|
||||
while(((*(RP)( DMAC_INTTCSTATUS)) & 0x4) != 0x4 )
|
||||
while (((*(RP)(DMAC_INTTCSTATUS)) & 0x4) != 0x4)
|
||||
{
|
||||
delay(10);
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
to--;
|
||||
if(!to)
|
||||
if (!to)
|
||||
{
|
||||
EOUT("%s TIMEOUT\n",__FUNCTION__);
|
||||
EOUT("%s TIMEOUT\n", __FUNCTION__);
|
||||
return RT_ETIMEOUT;
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
//DBOUT("%s timeout is %d\n",__FUNCTION__,to);
|
||||
#endif
|
||||
#endif
|
||||
/*for the buf is big-endian we must reverse it*/
|
||||
for(i = 0;i<0x80;i++)
|
||||
for (i = 0; i < 0x80; i++)
|
||||
{
|
||||
temp = buf[0];
|
||||
buf[0] = buf[3];
|
||||
|
@ -356,15 +351,15 @@ static rt_err_t sd_readblock(rt_uint32_t address, rt_uint8_t* buf)
|
|||
return RT_EOK;
|
||||
}
|
||||
|
||||
static rt_uint8_t sd_readmultiblock(rt_uint32_t address, rt_uint8_t* buf,rt_uint32_t size)
|
||||
static rt_uint8_t sd_readmultiblock(rt_uint32_t address, rt_uint8_t *buf, rt_uint32_t size)
|
||||
{
|
||||
rt_int32_t index;
|
||||
rt_uint8_t status=RT_EOK;
|
||||
rt_uint8_t status = RT_EOK;
|
||||
|
||||
for(index = 0;index < size;index++)
|
||||
for (index = 0; index < size; index++)
|
||||
{
|
||||
status = sd_readblock(address+index*SECTOR_SIZE,buf+index*SECTOR_SIZE);
|
||||
if(status!=RT_EOK)
|
||||
status = sd_readblock(address + index * SECTOR_SIZE, buf + index * SECTOR_SIZE);
|
||||
if (status != RT_EOK)
|
||||
break;
|
||||
}
|
||||
return status;
|
||||
|
@ -376,22 +371,22 @@ static rt_uint8_t sd_readmultiblock(rt_uint32_t address, rt_uint8_t* buf,rt_uint
|
|||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
static rt_uint8_t sd_writeblock(rt_uint32_t address, rt_uint8_t* buf)
|
||||
static rt_uint8_t sd_writeblock(rt_uint32_t address, rt_uint8_t *buf)
|
||||
{
|
||||
U32 complete;
|
||||
rt_uint8_t temp;
|
||||
rt_uint8_t *ptr = buf;
|
||||
rt_err_t err;
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
rt_uint32_t to = 10;
|
||||
#endif
|
||||
#endif
|
||||
|
||||
int i;
|
||||
|
||||
rt_kprintf("in writeblock:%x\n",address);
|
||||
rt_kprintf("in writeblock:%x\n", address);
|
||||
|
||||
/*for the buf is big-endian we must reverse it*/
|
||||
for(i = 0;i<0x80;i++)
|
||||
for (i = 0; i < 0x80; i++)
|
||||
{
|
||||
temp = ptr[0];
|
||||
ptr[0] = ptr[3];
|
||||
|
@ -409,29 +404,28 @@ static rt_uint8_t sd_writeblock(rt_uint32_t address, rt_uint8_t* buf)
|
|||
*(RP)DMAC_INTTCCLEAR |= 0x1;
|
||||
*(RP)DMAC_INTTCCLEAR &= ~0x1;
|
||||
|
||||
//***********************配置DMA2进行四位写*************************
|
||||
*(RP)DMAC_C2SRCADDR = (U32)buf; //DMAC道2源地址赋为0x30200000
|
||||
*(RP)DMAC_C2DESTADDR = SDC_WRITE_BUFER_ACCESS; //DMAC道2目的地址赋为发送FIFO的地址
|
||||
*(RP)DMAC_C2CONTROL = 0x20149b; //传输尺寸0x080,源地址增加目的地址不增加,传输宽度32bit,传输的数目4
|
||||
*(RP)DMAC_C2CONFIGURATION = 0x380b; //不屏蔽传输中断,屏蔽错误中断,通道使能,传输类型:存储器到外设
|
||||
*(RP)DMAC_C2SRCADDR = (U32)buf;
|
||||
*(RP)DMAC_C2DESTADDR = SDC_WRITE_BUFER_ACCESS;
|
||||
*(RP)DMAC_C2CONTROL = 0x20149b;
|
||||
*(RP)DMAC_C2CONFIGURATION = 0x380b;
|
||||
|
||||
|
||||
err = cmd_wait(0x6ea,RCA,0xfff); //CMD55,以切换到ACMD命令
|
||||
if(err != RT_EOK)
|
||||
err = cmd_wait(0x6ea, RCA, 0xfff);
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
rt_set_errno(err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = cmd_wait(0xca,0x2,0xfff); //ACMD6,定义数据线宽度,48 位短反馈,无数据传输
|
||||
if(err != RT_EOK)
|
||||
err = cmd_wait(0xca, 0x2, 0xfff);
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
rt_set_errno(err);
|
||||
return err;
|
||||
}
|
||||
|
||||
err = cmd_response(0x30e,address,0X3,0x0200,0x1,0Xfff); //CMD24 1bit mode
|
||||
if(err != RT_EOK)
|
||||
err = cmd_response(0x30e, address, 0X3, 0x0200, 0x1, 0Xfff); //CMD24 1bit mode
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
rt_set_errno(err);
|
||||
return err;
|
||||
|
@ -439,26 +433,26 @@ static rt_uint8_t sd_writeblock(rt_uint32_t address, rt_uint8_t* buf)
|
|||
|
||||
complete = *(RP)SDC_INTERRUPT_STATUS;
|
||||
|
||||
if((complete |0xfffffffe) !=0xfffffffe) //响应超时错误
|
||||
if ((complete | 0xfffffffe) != 0xfffffffe)
|
||||
{
|
||||
//printf("CRC ERROR");
|
||||
complete = *(RP)SDC_INTERRUPT_STATUS;
|
||||
}
|
||||
|
||||
while(((*(RP)( DMAC_INTTCSTATUS)) & 0x4) != 0x4 )
|
||||
while (((*(RP)(DMAC_INTTCSTATUS)) & 0x4) != 0x4)
|
||||
{
|
||||
delay(10);
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
to--;
|
||||
if(!to)
|
||||
if (!to)
|
||||
{
|
||||
EOUT("%s TIMEOUT\n",__FUNCTION__);
|
||||
EOUT("%s TIMEOUT\n", __FUNCTION__);
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
#ifdef USE_TIMEOUT
|
||||
#ifdef USE_TIMEOUT
|
||||
//DBOUT("%s timeout is %d\n",__FUNCTION__,to);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
return RT_EOK;
|
||||
}
|
||||
|
@ -505,7 +499,7 @@ static rt_err_t rt_sdcard_close(rt_device_t dev)
|
|||
*/
|
||||
static rt_err_t rt_sdcard_control(rt_device_t dev, int cmd, void *args)
|
||||
{
|
||||
rt_kprintf("cmd = %d\n",cmd);
|
||||
rt_kprintf("cmd = %d\n", cmd);
|
||||
RT_ASSERT(dev != RT_NULL);
|
||||
|
||||
if (cmd == RT_DEVICE_CTRL_BLK_GETGEOME)
|
||||
|
@ -532,35 +526,43 @@ static rt_err_t rt_sdcard_control(rt_device_t dev, int cmd, void *args)
|
|||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t size)
|
||||
static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void *buffer, rt_size_t size)
|
||||
{
|
||||
rt_uint32_t retry = 3;
|
||||
rt_uint8_t status;
|
||||
rt_uint32_t index;
|
||||
|
||||
///*take the semaphore
|
||||
struct dfs_partition *part = (struct dfs_partition *)dev->user_data;
|
||||
struct dfs_partition *part;
|
||||
|
||||
if (dev == RT_NULL)
|
||||
{
|
||||
rt_set_errno(-DFS_STATUS_EINVAL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
part = (struct dfs_partition *)dev->user_data;
|
||||
// take the semaphore
|
||||
rt_sem_take(part->lock, RT_WAITING_FOREVER);
|
||||
while(retry--)
|
||||
while (retry--)
|
||||
{
|
||||
if (((rt_uint32_t)buffer % 4 != 0) ||
|
||||
((rt_uint32_t)buffer > 0x20080000))
|
||||
{
|
||||
for(index = 0;index < size;index++)
|
||||
for (index = 0; index < size; index++)
|
||||
{
|
||||
status = sd_readblock((part->offset + pos) * SECTOR_SIZE,ptr_sddev->sec_buf);
|
||||
if(status != RT_EOK)
|
||||
status = sd_readblock((part->offset + pos) * SECTOR_SIZE, ptr_sddev->sec_buf);
|
||||
if (status != RT_EOK)
|
||||
break;
|
||||
|
||||
rt_memcpy((rt_uint8_t *)buffer + (index * SECTOR_SIZE),ptr_sddev->sec_buf,SECTOR_SIZE);
|
||||
rt_memcpy((rt_uint8_t *)buffer + (index * SECTOR_SIZE), ptr_sddev->sec_buf, SECTOR_SIZE);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for(index = 0;index<size;index++)
|
||||
for (index = 0; index < size; index++)
|
||||
{
|
||||
status = sd_readblock((pos) * SECTOR_SIZE,(rt_uint8_t*)buffer + index * SECTOR_SIZE);
|
||||
if(status != RT_EOK)
|
||||
status = sd_readblock((pos) * SECTOR_SIZE, (rt_uint8_t *)buffer + index * SECTOR_SIZE);
|
||||
if (status != RT_EOK)
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
@ -582,19 +584,20 @@ static rt_size_t rt_sdcard_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_
|
|||
*
|
||||
* @param hook the hook function
|
||||
*/
|
||||
static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buffer, rt_size_t size)
|
||||
static rt_size_t rt_sdcard_write(rt_device_t dev, rt_off_t pos, const void *buffer, rt_size_t size)
|
||||
{
|
||||
int i;
|
||||
rt_uint8_t status;
|
||||
struct dfs_partition *part;
|
||||
|
||||
struct dfs_partition *part = (struct dfs_partition *)dev->user_data;
|
||||
|
||||
if ( dev == RT_NULL )
|
||||
if (dev == RT_NULL)
|
||||
{
|
||||
rt_set_errno(-DFS_STATUS_EINVAL);
|
||||
return 0;
|
||||
}
|
||||
|
||||
part = (struct dfs_partition *)dev->user_data;
|
||||
|
||||
rt_sem_take(part->lock, RT_WAITING_FOREVER);
|
||||
|
||||
if (((rt_uint32_t)buffer % 4 != 0) ||
|
||||
|
@ -602,19 +605,19 @@ static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buf
|
|||
{
|
||||
rt_uint32_t index;
|
||||
|
||||
for(index=0;index<size;index++)
|
||||
for (index = 0; index < size; index++)
|
||||
{
|
||||
rt_memcpy(ptr_sddev->sec_buf, ((rt_uint8_t*)buffer + index * SECTOR_SIZE), SECTOR_SIZE);
|
||||
status = sd_writeblock((part->offset + index + pos)*SECTOR_SIZE,ptr_sddev->sec_buf);
|
||||
rt_memcpy(ptr_sddev->sec_buf, ((rt_uint8_t *)buffer + index * SECTOR_SIZE), SECTOR_SIZE);
|
||||
status = sd_writeblock((part->offset + index + pos) * SECTOR_SIZE, ptr_sddev->sec_buf);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
|
||||
for(i=0;i<size;i++)
|
||||
for (i = 0; i < size; i++)
|
||||
{
|
||||
status = sd_writeblock((part->offset + i + pos)*SECTOR_SIZE,
|
||||
(rt_uint8_t*)((rt_uint8_t*)buffer + i * SECTOR_SIZE));
|
||||
status = sd_writeblock((part->offset + i + pos) * SECTOR_SIZE,
|
||||
(rt_uint8_t *)((rt_uint8_t *)buffer + i * SECTOR_SIZE));
|
||||
if (status != RT_EOK) break;
|
||||
}
|
||||
}
|
||||
|
@ -631,11 +634,11 @@ static rt_size_t rt_sdcard_write (rt_device_t dev, rt_off_t pos, const void* buf
|
|||
|
||||
rt_err_t rt_hw_sdcard_exit()
|
||||
{
|
||||
if(ptr_sddev->device != RT_NULL)
|
||||
if (ptr_sddev->device != RT_NULL)
|
||||
rt_free(ptr_sddev->device);
|
||||
if(ptr_sddev->part != RT_NULL)
|
||||
if (ptr_sddev->part != RT_NULL)
|
||||
rt_free(ptr_sddev->part);
|
||||
if(ptr_sddev != RT_NULL)
|
||||
if (ptr_sddev != RT_NULL)
|
||||
rt_free(ptr_sddev);
|
||||
|
||||
return RT_EOK;
|
||||
|
@ -657,8 +660,8 @@ rt_err_t rt_hw_sdcard_init()
|
|||
|
||||
/*Initialize structure*/
|
||||
|
||||
ptr_sddev = (struct sd_device*)rt_malloc(sizeof(struct sd_device));
|
||||
if(ptr_sddev == RT_NULL)
|
||||
ptr_sddev = (struct sd_device *)rt_malloc(sizeof(struct sd_device));
|
||||
if (ptr_sddev == RT_NULL)
|
||||
{
|
||||
EOUT("Failed to allocate sdcard device structure\n");
|
||||
return RT_ENOMEM;
|
||||
|
@ -666,26 +669,26 @@ rt_err_t rt_hw_sdcard_init()
|
|||
|
||||
/*sdcard intialize*/
|
||||
err = sd_init();
|
||||
if(err != RT_EOK)
|
||||
if (err != RT_EOK)
|
||||
goto FAIL2;
|
||||
|
||||
/*set sector buffer*/
|
||||
ptr_sddev->sec_buf = gsec_buf;
|
||||
ptr_sddev->buf_size = SECTOR_SIZE;
|
||||
ptr_sddev->sdc = (struct sd_c*)SD_BASE;
|
||||
ptr_sddev->sdc = (struct sd_c *)SD_BASE;
|
||||
|
||||
//DBOUT("allocate partition sector buffer OK!");
|
||||
|
||||
err = sd_readblock(0,ptr_sddev->sec_buf);
|
||||
if(err != RT_EOK)
|
||||
err = sd_readblock(0, ptr_sddev->sec_buf);
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
EOUT("read first block error\n");
|
||||
goto FAIL2;
|
||||
}
|
||||
|
||||
/*sdcard driver initialize*/
|
||||
ptr_sddev->part = (struct dfs_partition*)rt_malloc(4 * sizeof(struct dfs_partition));
|
||||
if(ptr_sddev->part == RT_NULL)
|
||||
ptr_sddev->part = (struct dfs_partition *)rt_malloc(4 * sizeof(struct dfs_partition));
|
||||
if (ptr_sddev->part == RT_NULL)
|
||||
{
|
||||
EOUT("allocate partition failed\n");
|
||||
err = RT_ENOMEM;
|
||||
|
@ -693,8 +696,8 @@ rt_err_t rt_hw_sdcard_init()
|
|||
}
|
||||
|
||||
/*alloc device buffer*/
|
||||
ptr_sddev->device = (struct rt_device*)rt_malloc(4 * sizeof(struct rt_device));
|
||||
if(ptr_sddev->device == RT_NULL)
|
||||
ptr_sddev->device = (struct rt_device *)rt_malloc(4 * sizeof(struct rt_device));
|
||||
if (ptr_sddev->device == RT_NULL)
|
||||
{
|
||||
EOUT("allocate device failed\n");
|
||||
err = RT_ENOMEM;
|
||||
|
@ -703,15 +706,15 @@ rt_err_t rt_hw_sdcard_init()
|
|||
|
||||
ptr_sddev->part_num = 0;
|
||||
|
||||
err = sd_readblock(0,ptr_sddev->sec_buf);
|
||||
err = sd_readblock(0, ptr_sddev->sec_buf);
|
||||
|
||||
if(err != RT_EOK)
|
||||
if (err != RT_EOK)
|
||||
{
|
||||
EOUT("Read block 0 to initialize ERROR\n");
|
||||
goto FAIL1;
|
||||
}
|
||||
|
||||
for(i=0; i<4; i++)
|
||||
for (i = 0; i < 4; i++)
|
||||
{
|
||||
/* get the first partition */
|
||||
err = dfs_filesystem_get_partition(&(ptr_sddev->part[i]), ptr_sddev->sec_buf, i);
|
||||
|
@ -728,17 +731,17 @@ rt_err_t rt_hw_sdcard_init()
|
|||
ptr_sddev->device[i].read = rt_sdcard_read;
|
||||
ptr_sddev->device[i].write = rt_sdcard_write;
|
||||
ptr_sddev->device[i].control = rt_sdcard_control;
|
||||
ptr_sddev->device[i].user_data= &ptr_sddev->part[i];
|
||||
ptr_sddev->device[i].user_data = &ptr_sddev->part[i];
|
||||
|
||||
err = rt_device_register(&ptr_sddev->device[i], dname,
|
||||
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
|
||||
|
||||
if(err == RT_EOK)
|
||||
if (err == RT_EOK)
|
||||
ptr_sddev->part_num++;
|
||||
}
|
||||
else
|
||||
{
|
||||
if(i == 0)
|
||||
if (i == 0)
|
||||
{
|
||||
/* there is no partition table */
|
||||
ptr_sddev->part[0].offset = 0;
|
||||
|
@ -752,12 +755,12 @@ rt_err_t rt_hw_sdcard_init()
|
|||
ptr_sddev->device[0].read = rt_sdcard_read;
|
||||
ptr_sddev->device[0].write = rt_sdcard_write;
|
||||
ptr_sddev->device[0].control = rt_sdcard_control;
|
||||
ptr_sddev->device[0].user_data= &ptr_sddev->part[0];
|
||||
ptr_sddev->device[0].user_data = &ptr_sddev->part[0];
|
||||
|
||||
err = rt_device_register(&ptr_sddev->device[0], "sd0",
|
||||
RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_REMOVABLE | RT_DEVICE_FLAG_STANDALONE);
|
||||
|
||||
if(err == RT_EOK)
|
||||
if (err == RT_EOK)
|
||||
ptr_sddev->part_num++;
|
||||
|
||||
break;
|
||||
|
@ -765,26 +768,24 @@ rt_err_t rt_hw_sdcard_init()
|
|||
}
|
||||
}
|
||||
|
||||
if(ptr_sddev->part_num == 0)
|
||||
if (ptr_sddev->part_num == 0)
|
||||
goto FAIL0;
|
||||
|
||||
return err;
|
||||
|
||||
FAIL0:
|
||||
FAIL0:
|
||||
rt_free(ptr_sddev->device);
|
||||
ptr_sddev->device = RT_NULL;
|
||||
|
||||
FAIL1:
|
||||
FAIL1:
|
||||
rt_free(ptr_sddev->part);
|
||||
ptr_sddev->part = RT_NULL;
|
||||
|
||||
FAIL2:
|
||||
FAIL2:
|
||||
rt_free(ptr_sddev);
|
||||
ptr_sddev = RT_NULL;
|
||||
|
||||
return err;
|
||||
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue