DRESULT disk_write ( BYTE pdrv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ UINT count /* Number of sectors to write (1..128) */ ) { u8 res; // uint32_t result; if (!count)return RES_PARERR;//count????0,???????? // int i; switch (pdrv) { case SDCARD : res=SD_WriteDisk((u8*)buff,sector,count); while(res)//??? { SD_Init(); //?????SD? ForDelay_nms(1); res=SD_WriteDisk((u8*)buff,sector,count); // printf("sd wr error:%d\r\n",res); } return RES_OK; } return RES_PARERR; }
DRESULT disk_write ( BYTE pdrv, /* Physical drive nmuber to identify the drive */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address in LBA */ UINT count /* Number of sectors to write */ ) { DRESULT res; uint8_t result; if(!count){ return RES_PARERR; } switch (pdrv) { case SD_CARD: result = SD_WriteDisk((u8*)buff,sector,count); while(result){ SD_Init(); result = SD_WriteDisk((u8*)buff, sector, count); } break; default: result = 1; } if (result == 0x00){ return RES_OK; } else{ return RES_ERROR; } }
/******************************************************************************* * Function Name : Write_Memory * Description : Handle the Write operation to the STORAGE card. * Input : None. * Output : None. * Return : None. *******************************************************************************/ int8_t STORAGE_Write (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) { uint32_t res=0;// int i; USB_STATUS_REG|=0X01;//标记正在写数据 res=SD_WriteDisk (buf,blk_addr,blk_len); if(res)USB_STATUS_REG|=0X04;//SD卡写错误! return res; }
DRESULT disk_write ( BYTE pdrv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ UINT count /* Number of sectors to write (1..128) */ ) { u8 res=0; if (!count)return RES_PARERR; switch(pdrv) { #ifdef USE_SD_CARD case SD_CARD: res=SD_WriteDisk((u8*)buff,sector,count); while(res) { SD_Init(); res=SD_WriteDisk((u8*)buff,sector,count); //printf("sd wr error:%d\r\n",res); } break; #endif case EX_FLASH: for(;count>0;count--) { W25QXX_Write((u8*)buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE); sector++; buff+=FLASH_SECTOR_SIZE; } res=0; break; case USB_DISK: res=USBH_UDISK_Write((u8*)buff,sector,count); break; default: res=1; } if(res == 0x00)return RES_OK; else return RES_ERROR; }
DRESULT disk_write ( BYTE pdrv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ UINT count /* Number of sectors to write (1..128) */ ) { u8 res=0; if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误 switch(pdrv) { case SD_CARD://SD卡 res=SD_WriteDisk((u8*)buff,sector,count); while(res)//写出错 { SD_Init(); //重新初始化SD卡 res=SD_WriteDisk((u8*)buff,sector,count); //printf("sd wr error:%d\r\n",res); } break; #if SPIFlash case EX_FLASH://外部flash for(;count>0;count--) { W25QXX_Write((u8*)buff,sector*FLASH_SECTOR_SIZE,FLASH_SECTOR_SIZE); sector++; buff+=FLASH_SECTOR_SIZE; } res=0; break; #endif default: res=1; } //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值 if(res == 0x00)return RES_OK; else return RES_ERROR; }
DRESULT disk_write ( BYTE pdrv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to write (1..128) */ ) { DRESULT res; int result; u8 retry = 0X1F; //写入失败的时候,重试次数 if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误 switch (pdrv) { case SD_CARD : // translate the arguments here while (retry--) { result = SD_WriteDisk((u8 *)buff, sector, count); if (result == SD_OK) { res = RES_OK; break; } } // translate the reslut code here return res; // case MMC : // // translate the arguments here // result = MMC_disk_write(buff, sector, count); // // translate the reslut code here // return res; // case USB : // // translate the arguments here // result = USB_disk_write(buff, sector, count); // // translate the reslut code here // return res; } return RES_PARERR; }
/******************************************************************************* * 函数名:主函数 * 功 能: * 参 数: * 返 回: *******************************************************************************/ int main(void) { SystemInit(); Delay_init(72); NVIC_Configuration(); USART1_Init(9600); Delayms(5000); while( SD_Initialize() != 0 ) { USART1_PrintString("SD Initialize Fail \r\n"); } USART1_PrintString("SD Initialize OK \r\n"); sector_num = SD_GetSectorNum(); USART1_PrintString("The SD sector num is: "); USART1_PrintChar(sector_num>>24); USART1_PrintChar(sector_num>>16); USART1_PrintChar(sector_num>>8); USART1_PrintChar(sector_num>>0); USART1_PrintString("\r\n"); USART1_PrintString("The SD size is: "); sector_num >>= 12; USART1_PrintChar(sector_num>>24); USART1_PrintChar(sector_num>>16); USART1_PrintChar(sector_num>>8); USART1_PrintChar(sector_num>>0); if( 0 == SD_WriteDisk(buf1, 1, 1) ) { USART1_PrintString("Write OK \r\n"); } while(1) { Delayms(4000); if(0 == SD_ReadDisk(buf,0,2)) { for(n=0; n<1024; n++) { USART1_PrintChar(buf[n]); } } } }
DRESULT disk_write ( BYTE pdrv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ UINT count /* Number of sectors to write (1..128) */ ) { u8 res=0; if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误 switch(pdrv) { case SD_CARD://SD卡 res=SD_WriteDisk((u8*)buff,sector,count); break; default: res=1; } //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值 if(res == 0x00)return RES_OK; else return RES_ERROR; }