case DEV_MMC :
// translate the arguments here
//result = MMC_disk_read(buff, sector, count);
// translate the reslut code here
{
sd_error_enum sta=SD_OK;
//uint32_t lsector=sector;
//uint16_t n;
//lsector《《=9;
if(count》1)
{
sta=sd_mul
tiblocks_read((uint32_t *)buff,(uint32_t)sector《《9,512,(uint32_t)count);//¶à¸öectorµÄ¶Á²Ù×÷
}else
{
sta=sd_block_read((uint32_t *)buff,(uint32_t)sector《《9,512);
}
//INTX_DISABLE();//¹Ø±Õ×ÜÖжÏ(POLLINGģʽ,ÑϽûÖжϴò¶ÏSDIO¶Áд²Ù×÷!!!)
if(sta==SD_OK)
return RES_OK;
else return RES_PARERR;
}
// case DEV_USB :
// // translate the arguments here
// //result = USB_disk_read(buff, sector, count);
// // translate the reslut code here
// return res;
// return RES_OK;
}
return RES_PARERR;
}
/*-----------------------------------------------------------------------*/
/* Write Sector(s) */
/*-----------------------------------------------------------------------*/
#if FF_FS_READONLY == 0
DRESULT disk_write (
BYTE pdrv, /* Physical drive nmuber to identify the drive */
const BYTE *buff, /* Data to be written */
LBA_t sector, /* Start sector in LBA */
UINT count /* Number of sectors to write */
)
{
//DRESULT res=RES_OK;
// int result;
if (NULL==buff)return RES_PARERR;
if (!count)return RES_PARERR;//count²»ÄܵÈÓÚ0£¬·ñÔò·µ»Ø²ÎÊý´íÎó
switch (pdrv) {
// case DEV_RAM :
// // translate the arguments here
// //result = RAM_disk_write(buff, sector, count);
// // translate the reslut code here
// return res;
case DEV_MMC :
// translate the arguments here
//result = MMC_disk_write(buff, sector, count);
// translate the reslut code here
{
sd_error_enum sta=SD_OK;
if(count》1)
{
sta=sd_multiblocks_write((uint32_t *)buff,(uint32_t)sector《《9,512,(uint32_t)count);//¶à¸öectorµÄ¶Á²Ù×÷
}else
{
sta=sd_block_write((uint32_t *)buff,(uint32_t)sector《《9,512);
}
if(sta==SD_OK)
return RES_OK;
else return RES_PARERR;
}
// case DEV_USB :
// // translate the arguments here
// //result = USB_disk_write(buff, sector, count);
// // translate the reslut code here
// return res;
}
return RES_PARERR;
}
#endif
Ffconf.h配置如下:
#define FF_FS_READONLY 0
#define FF_FS_MINIMIZE 0
#define FF_USE_FIND 1
#define FF_USE_MKFS 0
#define FF_USE_FASTSEEK 1
#define FF_USE_EXPAND 0
#define FF_USE_CHMOD 0
#define FF_USE_LABEL 0
#define FF_USE_FORWARD 0
#define FF_USE_STRFUNC 0
#define FF_PRINT_LLI 0
#define FF_PRINT_FLOAT 0
#define FF_STRF_ENCODE 0
#define FF_CODE_PAGE 437
#define FF_USE_LFN 3
#define FF_MAX_LFN 255
#define FF_LFN_UNICODE 0
#define FF_LFN_BUF 255
#define FF_SFN_BUF 12
#define FF_FS_RPATH 0
#define FF_VOLUMES 2
#define FF_STR_VOLUME_ID 0
#define FF_VOLUME_STRS “RAM”,“NAND”,“CF”,“SD”,“SD2”,“USB”,“USB2”,“USB3”
#define FF_MULTI_PARTITION 0
#define FF_MIN_SS 512
#define FF_MAX_SS 512
#define FF_FS_TINY 0
#define FF_FS_EXFAT 1
//其它默认就行。
测试代码,main函数
int main(void)
{
sd_error_enum sd_error;
uint16_t i = 5;
#ifdef DATA_PRINT
uint8_t *pdata;
#endif /* DATA_PRINT */
nvic_config();
/* configure systick */
systick_config();
uart_init();
/* enable the LEDs GPIO clock */
rcu_periph_clock_enable(RCU_GPIOC);
/* configure LED1 GPIO port */
gpio_mode_set(GPIOC, GPIO_MODE_OUTPUT, GPIO_PUPD_NONE, GPIO_PIN_6);
gpio_output_options_set(GPIOC, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_6);
/* reset LED1 GPIO pin */
gpio_bit_reset(GPIOC, GPIO_PIN_6);
do{
/* initialize the card, get the card information and configurate the bus mode and transfer mode */
sd_error = sd_config();
}while((SD_OK != sd_error) && (--i));
if(i){
printf(“
Card init success!
”);
}else{
printf(“
Card init failed!
”);
while (1){
}
}
/* get the information of the card and print it out by USART */
card_info_get();
/* init the write buffer */
for(i=0; i《512; i++){
buf_write[i] = i;
}
printf(“
Card test:”);
/* single block operation test */
sd_error = sd_block_write(buf_write, 100*512, 512);
if(SD_OK != sd_error){
printf(“
Block write fail!”);
while (1){
}
}else{
printf(“
Block write success!”);
}
sd_error = sd_block_read(buf_read, 100*512, 512);
if(SD_OK != sd_error){
printf(“
Block read fail!”);
while (1){
}
}else{
printf(“
Block read success!”);
#ifdef DATA_PRINT
pdata = (uint8_t *)buf_read;
/* print data by USART */
printf(“
”);
for(i = 0; i 《 128; i++){
printf(“ %3d %3d %3d %3d ”, *pdata, *(pdata+1), *(pdata+2), *(pdata+3));
pdata += 4;
if(0 == (i + 1) % 4){
printf(“
”);
}
}
#endif /* DATA_PRINT */
}
/* lock and unlock operation test */
if(SD_CCC_LOCK_CARD & sd_cardinfo.card_csd.ccc){
/* lock the card */
sd_error = sd_lock_unlock(SD_LOCK);
if(SD_OK != sd_error){
printf(“
Lock failed!”);
while (1){
}
}else{
printf(“
The card is locked!”);
}
sd_error = sd_erase(100*512, 101*512);
if(SD_OK != sd_error){
printf(“
Erase failed!”);
}else{
printf(“
Erase success!”);
}
/* unlock the card */
sd_error = sd_lock_unlock(SD_UNLOCK);
if(SD_OK != sd_error){
printf(“
Unlock failed!”);
while (1){
}
}else{
printf(“
The card is unlocked!”);
}
sd_error = sd_erase(100*512, 101*512);
if(SD_OK != sd_error){
printf(“
Erase failed!”);
}else{
printf(“
Erase success!”);
}
sd_error = sd_block_read(buf_read, 100*512, 512);
if(SD_OK != sd_error){
printf(“
Block read fail!”);
while (1){
}
}else{
printf(“
Block read success!”);
#ifdef DATA_PRINT
pdata = (uint8_t *)buf_read;
/* print data by USART */
printf(“
”);
for(i = 0; i 《 128; i++){
printf(“ %3d %3d %3d %3d ”, *pdata, *(pdata+1), *(pdata+2), *(pdata+3));
pdata += 4;
if(0 == (i + 1) % 4){
printf(“
”);
}
}
#endif /* DATA_PRINT */
}
}
/* multiple blocks operation test */
sd_error = sd_multiblocks_write(buf_write, 200*512, 512, 3);
if(SD_OK != sd_error){
printf(“
Multiple block write fail!”);
while (1){
}
}else{
printf(“
Multiple block write success!”);
}
sd_error = sd_multiblocks_read(buf_read, 200*512, 512, 3);
if(SD_OK != sd_error){
printf(“
Multiple block read fail!”);
while (1){
}
}else{
printf(“
Multiple block read success!”);
#ifdef DATA_PRINT
pdata = (uint8_t *)buf_read;
/* print data by USART */
printf(“
”);
for(i = 0; i 《 512; i++){
printf(“ %3d %3d %3d %3d ”, *pdata, *(pdata+1), *(pdata+2), *(pdata+3));
pdata += 4;
if(0 == (i + 1) % 4){
printf(“
”);
}
}
#endif /* DATA_PRINT */
}
res=f_mount(&fs, “1:”, 1);
if(res==FR_NO_FILESYSTEM)
{printf(“
NO file system in SD:%d
”,res);while(1);}
else if(res == FR_OK)
{
printf(“
file systme existed in SD”);
}else {printf(“
FILE mount Error For SD:%d”,res);
printf(“
Initiization error maybe”);}
res=f_open(&nfile1,“1:/kwin08.txt”,FA_READ | FA_WRITE | FA_OPEN_APPEND);
printf(“
f_open:%d”,res);
if(FR_OK == res)
{
printf(“
Create nfile1 OK”);
res = f_write(&nfile1,tx,sizeof(tx),&bw);
if(FR_OK == res){printf(“
write done1:%d”,bw);}
// res = f_write(&nfile1,tx,sizeof(tx),&bw);
// if(FR_OK == res){printf(“
write done2:%d”,bw);}
}
for(uint16_t i=0;i《512;i++)
{
mybuff[i]=0;
}
nfile1.fptr=0;
res=f_read(&nfile1,mybuff,sizeof(mybuff),&br);
printf(“
f_read:%d-byete:%d”,res,br);
printf(“
begin:
”);
for(uint16_t i=0;i《512;i++)
{
printf(“%c”,mybuff[i]);
}
f_close(&nfile1);
while(1) {
/* turn on LED1 */
gpio_bit_set(GPIOC, GPIO_PIN_6);
delay_1ms(1000);
printf(“LED ON
”);
/* turn off LED1 */
gpio_bit_reset(GPIOC, GPIO_PIN_6);
delay_1ms(1000);
printf(“LED OFF
”);
}
}
测试效果
程序每次重新启动写入一行“Hello my file”。然后进入LED闪烁,并串口打印LED状态。
SD卡中历次创建的文件及写入内容。