AN1302 STM32 SDIO的使用
本应用文档(AN1302,对应ALIENTEK 战舰STM32 扩展实验2)将教大家如何使用STM32
的SDIO接口来驱动SD卡,并结合FATFS,实现对 SD卡的读写操作。
本文档分为如下几部分:
1, SDIO简介
2, 硬件连接
3, 软件实现
4, 验证
1、SDIO接口简介
STM32F103大容量系列(FLASH≥256K)产品都含有 SDIO控制器, ALIENTEK 战舰STM32
开发板所使用STM32F103ZET6 就属于大容量产品,带有 SDIO接口。本节,我们将简单介绍
STM32的 SDIO接口,包括:主要功能及框图、时钟、命令与响应和相关寄存器简介等,最
后,我们将介绍SD卡的初始化流程。
基于STM32实现SD卡读写实用例程
u16 pic_get_tnum(u8 *path)
{
u8 res;
u16 rval=0;
DIR tdir;
FILINFO tfileinfo;
u8 *fn;
res=f_opendir(&tdir,(const TCHAR*)path);
tfileinfo.lfsize=_MAX_LFN*2+1;
tfileinfo.lfname=mymalloc(SRAMIN,tfileinfo.lfsize);
if(res==FR_OK&&tfileinfo.lfname!=NULL)
{
while(1)
{
res=f_readdir(&tdir,&tfileinfo);
if(res!=FR_OK||tfileinfo.fname[0]==0)break;
fn=(u8*)(*tfileinfo.lfname?tfileinfo.lfname:tfileinfo.fname);
res=f_typetell(fn);
if((res&0XF0)==0X50)
{
rval++;
}
}
}
return rval;
}
void testwrite(u8 *fname,u8* strx,u32 repeat)
{
FIL* f_test;
u8 *databuf;
u8 strxlen=strlen((const char*)strx);
u32 bufrpt=2048/strxlen;
u32 rpcnt=0;
u8 *ptr;
u8 res,i;
f_test=(FIL*)mymalloc(SRAMIN,sizeof(FIL));
databuf=mymalloc(SRAMIN,2048);
if(f_test==NULL||databuf==NULL)
{
myfree(SRAMIN,f_test);
myfree(SRAMIN,databuf);
return ;
}
res=f_open(f_test,(const TCHAR*)fname,FA_CREATE_NEW|FA_WRITE);
if(res==FR_OK)
{
ptr=databuf;
for(rpcnt=0;rpcnt<repeat;)
{
for(i=0;i<bufrpt;)
{
strncpy((char*)ptr,(char*)strx,strxlen);
ptr+=strxlen;
rpcnt++;i++;
if(rpcnt>=repeat)break;
}
printf("\r\nreptx:%02d strxlen:%d",rpcnt,strxlen);
res=f_write(f_test,databuf,(u32)i*strxlen,&bw);
if(res||bw!=(bufrpt*strxlen))break;
ptr=databuf;
}
f_close(f_test);
printf("\r\nfile size:%d\r\n",rpcnt*strxlen);
}
myfree(SRAMIN,f_test);
myfree(SRAMIN,databuf);
}
void show_sdcard_info(void)
{
switch(SDCardInfo.CardType)
{
case SDIO_STD_CAPACITY_SD_CARD_V1_1:printf("Card Type:SDSC V1.1\r\n");break;
case SDIO_STD_CAPACITY_SD_CARD_V2_0:printf("Card Type:SDSC V2.0\r\n");break;
case SDIO_HIGH_CAPACITY_SD_CARD:printf("Card Type:SDHC V2.0\r\n");break;
case SDIO_MULTIMEDIA_CARD:printf("Card Type:MMC Card\r\n");break;
}
printf("Card ManufacturerID:%d\r\n",SDCardInfo.SD_cid.ManufacturerID);
printf("Card RCA:%d\r\n",SDCardInfo.RCA);
printf("Card Capacity:%d MB\r\n",(u32)(SDCardInfo.CardCapacity>>20));
printf("Card BlockSize:%d\r\n\r\n",SDCardInfo.CardBlockSize);
}
int main(void)
{
u8 res;
DIR picdir;
FILINFO picfileinfo;
u8 *fn;
u8 *pname;
u16 totpicnum;
u16 curindex;
u8 key;
u8 pause=0;
u8 t;
u16 temp;
u16 *picindextbl;
Stm32_Clock_Init(9);
delay_init(72);
uart_init(72,115200);
LCD_Init();
LED_Init();
KEY_Init();
usmart_dev.init(72);
mem_init(SRAMIN);
exfuns_init();
f_mount(0,fs[0]);
f_mount(1,fs[1]);
POINT_COLOR=RED;
while(font_init())
{
LCD_ShowString(60,50,200,16,16,"Font Error!");
delay_ms(200);
LCD_Fill(60,50,240,66,WHITE);
delay_ms(200);
}
while(SD_Init())
{
Show_Str(60,150,240,16,"SD?¨3?ê??ˉ′í?ó!",16,0);
delay_ms(200);
LCD_Fill(60,150,240,166,WHITE);
delay_ms(200);
}
show_sdcard_info();
while(f_opendir(&picdir,"0:/PICTURE"))
{
Show_Str(60,150,240,16,"PICTURE???t?D′í?ó!",16,0);
delay_ms(200);
LCD_Fill(60,150,240,166,WHITE);
delay_ms(200);
}
totpicnum=pic_get_tnum("0:/PICTURE");
while(totpicnum==NULL)
{
Show_Str(60,150,240,16,"??óDí??????t!",16,0);
delay_ms(200);
LCD_Fill(60,150,240,166,WHITE);
delay_ms(200);
}
picfileinfo.lfsize=_MAX_LFN*2+1;
picfileinfo.lfname=mymalloc(SRAMIN,picfileinfo.lfsize);
pname=mymalloc(SRAMIN,picfileinfo.lfsize);
picindextbl=mymalloc(SRAMIN,2*totpicnum);
while(picfileinfo.lfname==NULL||pname==NULL||picindextbl==NULL)
{
Show_Str(60,150,240,16,"?ú′?·???꧰ü!",16,0);
delay_ms(200);
LCD_Fill(60,150,240,146,WHITE);
delay_ms(200);
}
res=f_opendir(&picdir,"0:/PICTURE");
if(res==FR_OK)
{
curindex=0;
while(1)
{
temp=picdir.index;
res=f_readdir(&picdir,&picfileinfo);
if(res!=FR_OK||picfileinfo.fname[0]==0)break;
fn=(u8*)(*picfileinfo.lfname?picfileinfo.lfname:picfileinfo.fname);
res=f_typetell(fn);
if((res&0XF0)==0X50)
{
picindextbl[curindex]=temp;
curindex++;
}
}
}
Show_Str(60,150,240,16,"?aê???ê?...",16,0);
delay_ms(1500);
piclib_init();
curindex=0;
res=f_opendir(&picdir,(const TCHAR*)"0:/PICTURE");
while(res==FR_OK)
{
dir_sdi(&picdir,picindextbl[curindex]);
res=f_readdir(&picdir,&picfileinfo);
if(res!=FR_OK||picfileinfo.fname[0]==0)break;
fn=(u8*)(*picfileinfo.lfname?picfileinfo.lfname:picfileinfo.fname);
strcpy((char*)pname,"0:/PICTURE/");
strcat((char*)pname,(const char*)fn);
LCD_Clear(BLACK);
ai_load_picfile(pname,0,0,lcddev.width,lcddev.height);
Show_Str(2,2,240,16,pname,16,1);
t=0;
while(1)
{
key=KEY_Scan(0);
if(t>250)key=KEY_RIGHT;
if(key==KEY_LEFT)
{
if(curindex)curindex--;
else curindex=totpicnum-1;
break;
}else if(key==KEY_RIGHT)
{
curindex++;
if(curindex>=totpicnum)curindex=0;
break;
}else if(key==KEY_UP)
{
pause=!pause;
LED1=!pause;
}
if(pause==0)t++;
delay_ms(10);
}
res=0;
}
myfree(SRAMIN,picfileinfo.lfname);
myfree(SRAMIN,pname);
myfree(SRAMIN,picindextbl);
}
完整的pdf格式文档51黑下载地址(共25页):
STM32 SDIO的使用_AN1302.pdf
(1.66 MB, 下载次数: 27)
|