DRESULT disk_write ( BYTE drv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to write (1..255) */ ) { SD_Error Status; if (!count) return RES_PARERR; //count不能等于0,否则返回参数错误 if(drv==0) { switch(SD_Mode) { case 0: //dma方式 if(count==1) // 1个sector的写操作 { Status = SD_WriteBlock(sector << 9,(u32 *)(&buff[0]),BlockSize);//sector<<9 扇区地址转为字节地址 一个扇区512字节 // printf("DISKIO.C Disk_Write() : %d\r\n ",Status); } else //多个sector的写操作 { Status =SD_WriteMultiBlocks(sector << 9,(u32 *)(&buff[0]),BlockSize,count); } break; case 1: //中断方式 if(count==1) // 1个sector的写操作 { Status = SD_WriteBlock(sector << 9 ,(u32 *)(&buff[0]),BlockSize); } else //多个sector的写操作 { Status = SD_WriteMultiBlocks(sector << 9 ,(u32 *)(&buff[0]),BlockSize,count); } break; default : Status=SD_ERROR; } //处理返回值,将sdcard.c的返回值转成ff.c的返回值 if(Status == SD_OK) { //printf("SD OK Status : %d\r\n",Status); return RES_OK; } else { //printf("Error Status : %d\r\n",Status); return RES_ERROR;} } else//仅支持磁盘0的操作 { 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) */ ) { SD_Error Status; if(pdrv || !count) return RES_PARERR; if(count==1) Status = SD_WriteBlock((u8 *)buff ,sector << 9 , SDCardInfo.CardBlockSize); else { Status = SD_WriteMultiBlocks((u8 *)buff ,sector << 9 ,SDCardInfo.CardBlockSize,count); Status = SD_WaitWriteOperation(); } /* Check if the Transfer is finished */ Status = SD_WaitReadOperation(); while(SD_GetStatus() != SD_TRANSFER_OK); if(Status == SD_OK) return RES_OK; else return RES_ERROR; }
DRESULT disk_write ( BYTE drv, /* Physical drive number (0) */ const BYTE *buff, /* Pointer to the data to be written */ DWORD sector, /* Start sector number (LBA) */ BYTE count /* Sector count (1..255) */ ) { SD_Error sdstatus = SD_OK; uint32_t timeout = 300000; SD_WriteMultiBlocks((BYTE *)buff, (uint32_t )(sector * 512), 512, count); /* Check if the Transfer is finished */ sdstatus = SD_WaitWriteOperation(); while(SD_GetStatus() != SD_TRANSFER_OK) { if (timeout-- == 0) { return RES_ERROR; } } if (sdstatus == SD_OK) { return RES_OK; } return RES_NOTRDY; }
/******************************************************************************* * Function Name : MAL_Write * Description : Write sectors * Input : None * Output : None * Return : None *******************************************************************************/ uint16_t MAL_Write(uint8_t lun, uint32_t Memory_Offset, uint32_t *Writebuff, uint16_t Transfer_Length) { switch (lun) { case 0: Status = SD_WriteMultiBlocks((uint8_t*)Writebuff, Memory_Offset, Transfer_Length,1); #if defined(USE_STM3210E_EVAL) || defined(USE_STM32L152D_EVAL) Status = SD_WaitWriteOperation(); while(SD_GetStatus() != SD_TRANSFER_OK); if ( Status != SD_OK ) { return MAL_FAIL; } #endif /* USE_STM3210E_EVAL ||USE_STM32L152D_EVAL*/ break; #ifdef USE_STM3210E_EVAL case 1: NAND_Write(Memory_Offset, Writebuff, Transfer_Length); break; #endif /* USE_STM3210E_EVAL */ default: return MAL_FAIL; } return MAL_OK; }
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 */ ) { if(count>1) { SD_WriteMultiBlocks((uint8_t *)buff, sector*BLOCK_SIZE, BLOCK_SIZE, count); // check transfer finished SD_WaitWriteOperation(); // wait the DMA transfer finished while(SD_GetStatus() != SD_TRANSFER_OK); } else { SD_WriteBlock((uint8_t *)buff, sector*BLOCK_SIZE, BLOCK_SIZE); // check transfer finished SD_WaitReadOperation(); // wait the DMA transfer finished while(SD_GetStatus() != SD_TRANSFER_OK); } return RES_OK; }
DRESULT disk_write ( BYTE drv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to write (1..255) */ ) { //memset(buff2, 0, sizeof(buff2)); if(count==1) { //memcpy(buff2,buff,SECTOR_SIZE); err1 = SD_WriteBlock((uint8_t *)(&buff[0]),sector ,SECTOR_SIZE); SD_WaitWriteOperation(); while(SD_GetStatus() != SD_TRANSFER_OK); } else { //memcpy(buff2,buff,SECTOR_SIZE * count); err1 = SD_WriteMultiBlocks((uint8_t *)(&buff[0]),sector ,SECTOR_SIZE,count); SD_WaitWriteOperation(); while(SD_GetStatus() != SD_TRANSFER_OK); } if( err1 == SD_OK) return RES_OK; else return RES_ERROR; }
DRESULT disk_write ( BYTE drv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to write (1..255) */ ) { SD_Error Status = SD_OK; #if 1 switch(drv) { case SD_CARD://SD卡 //SD卡开始---------------------------------------- while (count--) { Status = SD_WriteBlock((uint8_t *)buff, sector << 9 ,SECTOR_SIZE); if (Status != SD_OK) { break; } } //SD卡结束---------------------------------------- break; case EX_FLASH://外部flash for(;count>0;count--) { SPI_Flash_Write((u8*)buff,sector*SECTOR_SIZE,SECTOR_SIZE); sector++; buff+=SECTOR_SIZE; } Status=SD_OK; break; default: Status=SD_ERROR; } #else /* SD_WriteMultiBlocks() 偶尔会执行出错 */ if (count == 1) { Status = SD_WriteBlock((uint8_t *)buff, sector << 9 ,SECTOR_SIZE); } else { Status = SD_WriteMultiBlocks((uint8_t *)buff, sector << 9 ,SECTOR_SIZE, count); } #endif if (Status == SD_OK) { return RES_OK; } else { printf("Err: SD_WriteBlocks(,%d,%d)\r\n",sector,count); return RES_ERROR; } }
/** * @brief Write data to the medium * @param lun : logical unit number * @param buf : Pointer to the buffer to write from * @param blk_addr : address of 1st block to be written * @param blk_len : nmber of blocks to be read * @retval Status */ int8_t STORAGE_Write(uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) { if (filerGetMSCState() != FILER_STATE_MSC_ACTIVE || !SD_Initialized() || !SD_TransferComplete()) return (-1); sdioSetCallback(SCSI_ProcessWriteComplete, blk_len * 512); while (SD_WriteMultiBlocks(buf, blk_addr, 512, blk_len) != SD_OK) ; return (0); }
DRESULT disk_write ( BYTE drv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to write (1..255) */ ) { u8 res=0; if (!count)return RES_PARERR;//count不能等于0,否则返回参数错误 switch(drv) { case SD_CARD://SD卡 if (count > 1) { SD_WriteMultiBlocks((uint8_t *)buff, sector*BLOCK_SIZE, BLOCK_SIZE, count); /* Check if the Transfer is finished */ SD_WaitWriteOperation(); //等待dma传输结束 while(SD_GetStatus() != SD_TRANSFER_OK); //等待sdio到sd卡传输结束 } else { SD_WriteBlock((uint8_t *)buff,sector*BLOCK_SIZE, BLOCK_SIZE); /* Check if the Transfer is finished */ SD_WaitWriteOperation(); //等待dma传输结束 while(SD_GetStatus() != SD_TRANSFER_OK); //等待sdio到sd卡传输结束 } break; case EX_FLASH://外部flash for(;count>0;count--) { SPI_Flash_Write((u8*)buff,sector*BLOCK_SIZE,BLOCK_SIZE); sector++; buff+=BLOCK_SIZE; } res=0; break; default: res=1; } //处理返回值,将SPI_SD_driver.c的返回值转成ff.c的返回值 if(res == 0x00)return RES_OK; else return RES_ERROR; }
DRESULT disk_write ( BYTE drv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to write (1..255) */ ) { SD_Error Status; if(count==1)Status = SD_WriteBlock((uint8_t *)buff ,sector << 9 , SDCardInfo.CardBlockSize); else { Status = SD_WriteMultiBlocks((uint8_t *)buff ,sector << 9 ,SDCardInfo.CardBlockSize,count); Status = SD_WaitWriteOperation(); } while(SD_GetStatus() != SD_TRANSFER_OK); if(Status == SD_OK)return RES_OK; else return RES_ERROR; }
DRESULT disk_write ( BYTE drv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to write (1..255) */ ) { if ( count == 1 ) /* 1个sector的写操作 */ { SD_WriteBlock((u8 *)(&buff[0]),sector <<9 ,SECTOR_SIZE); } else /* 多个sector的写操作 */ { SD_WriteMultiBlocks((u8 *)(&buff[0]),sector <<9,SECTOR_SIZE,count); } SD_WaitWriteOperation(); while(SD_GetStatus() != SD_TRANSFER_OK); return RES_OK; }
/** * @brief Write data to the medium * @param lun : logical unit number * @param buf : Pointer to the buffer to write from * @param blk_addr : address of 1st block to be written * @param blk_len : nmber of blocks to be read * @retval Status */ int8_t STORAGE_Write (uint8_t lun, uint8_t *buf, uint32_t blk_addr, uint16_t blk_len) { if( SD_WriteMultiBlocks (buf, blk_addr * 512, 512, blk_len) != 0) { return -1; } #ifndef USE_STM3210C_EVAL SD_WaitWriteOperation(); while (SD_GetStatus() != SD_TRANSFER_OK); #endif return (0); }
/** * @brief Tests the SD card Multiple Blocks operations. * @param None * @retval None */ void SD_MultiBlockTest(void) { /*--------------- Multiple Block Read/Write ---------------------*/ /* Fill the buffer to send */ Fill_Buffer(Buffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0); if (Status == SD_OK) { /* Write multiple block of many bytes on address 0 */ Status = SD_WriteMultiBlocks(Buffer_MultiBlock_Tx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS); /* Check if the Transfer is finished */ Status = SD_WaitWriteOperation(); while(SD_GetStatus() != SD_TRANSFER_OK); } if (Status == SD_OK) { /* Read block of many bytes from address 0 */ Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS); /* Check if the Transfer is finished */ Status = SD_WaitReadOperation(); while(SD_GetStatus() != SD_TRANSFER_OK); } /* Check the correctness of written data */ if (Status == SD_OK) { TransferStatus2 = Buffercmp(Buffer_MultiBlock_Tx, Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE); } if(TransferStatus2 == PASSED) { STM_EVAL_LEDOn(LED3); LCD_DisplayStringLine(LCD_LINE_2, "Passed"); } else { STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOn(LED4); LCD_DisplayStringLine(LCD_LINE_2, "failed"); } }
DRESULT disk_write ( BYTE drv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to write (1..255) */ ) { SD_Error Status; if( !count ) { return RES_PARERR; /* count不能等于0,否则返回参数错误 */ } switch (drv) { case 0: if(count==1) /* 1个sector的写操作 */ { Status = SD_WriteBlock( (uint8_t *)(&buff[0]) ,sector << 9 , SDCardInfo.CardBlockSize); } else /* 多个sector的写操作 */ { Status = SD_WriteMultiBlocks( (uint8_t *)(&buff[0]) ,sector << 9 ,SDCardInfo.CardBlockSize,count); } /* Check if the Transfer is finished */ //Status = SD_WaitReadOperation(); while(SD_GetStatus() != SD_TRANSFER_OK); if(Status == SD_OK) { return RES_OK; } else { return RES_ERROR; } case 2: break; default : break; } return RES_ERROR; }
DRESULT disk_write ( BYTE drv, /* Physical drive nmuber (0..) */ const BYTE *buff, /* Data to be written */ DWORD sector, /* Sector address (LBA) */ BYTE count /* Number of sectors to write (1..255) */ ) { //memset(buff2, 0, sizeof(buff2)); if(count==1) { //memcpy(buff2,buff,SECTOR_SIZE); SD_WriteBlock(sector << 9 ,(u32 *)(&buff[0]),BlockSize); } else { //memcpy(buff2,buff,SECTOR_SIZE * count); SD_WriteMultiBlocks(sector << 9 ,(u32 *)(&buff[0]),BlockSize,count); } return RES_OK; }
/** * @brief Tests the SD card Multiple Blocks operations. * @param None * @retval None */ static void SD_MultiBlockTest (void) { /* Fill the buffer to send */ Fill_Buffer (aBuffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0); if (Status == SD_OK) { /* Write multiple block of many bytes on address 0 */ Status = SD_WriteMultiBlocks (aBuffer_MultiBlock_Tx, 0, BLOCK_SIZE, NUMBER_OF_BLOCKS); /* Check if the Transfer is finished */ Status = SD_WaitWriteOperation (); while (SD_GetStatus () != SD_TRANSFER_OK) ; } if (Status == SD_OK) { /* Read block of many bytes from address 0 */ Status = SD_ReadMultiBlocks (aBuffer_MultiBlock_Rx, 0, BLOCK_SIZE, NUMBER_OF_BLOCKS); /* Check if the Transfer is finished */ Status = SD_WaitReadOperation (); while (SD_GetStatus () != SD_TRANSFER_OK) ; } /* Check the correctness of written data */ if (Status == SD_OK) { TransferStatus2 = Buffercmp (aBuffer_MultiBlock_Tx, aBuffer_MultiBlock_Rx, MULTI_BUFFER_SIZE); } if (TransferStatus2 == PASSED) { logf ("Multiple block test passed\r\n"); } else { logf ("Multiple block test failed\r\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) */ ) { SD_Error Status; if( !count ) { return RES_PARERR; } switch (pdrv) { case SD_CARD: if(count==1) { rt_enter_critical(); Status = SD_WriteBlock( (uint8_t *)(&buff[0]) ,sector << 9 , 512); rt_exit_critical(); } else { rt_enter_critical(); Status = SD_WriteMultiBlocks( (uint8_t *)(&buff[0]) ,sector << 9 ,512,count); rt_exit_critical(); } /* Check if the Transfer is finished */ Status = SD_WaitWriteOperation(); rt_enter_critical(); while(SD_GetStatus() != SD_TRANSFER_OK); rt_exit_critical(); if(Status == SD_OK) { return RES_OK; } else { return RES_ERROR; } // case ATA : // // translate the arguments here // // result = ATA_disk_write(buff, sector, count); // // // 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; }
bool SdioSecureDigitalCard::writeBlocks(void *pDest_,uint32_t blockIndex_,uint32_t numBlocks_) { return handleWriteReturn(SD_WriteMultiBlocks(static_cast<uint8_t *> (pDest_),blockIndex_,getBlockSizeInBytes(),numBlocks_),E_FAILED_TO_WRITE_MULTIBLOCK); }
/** * @brief Main program. * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* Interrupt Config */ NVIC_Configuration(); /*-------------------------- SD Init ----------------------------- */ Status = SD_Init(); /*------------------- Block Erase -------------------------------*/ if (Status == SD_OK) { /* Erase NumberOfBlocks Blocks of WRITE_BL_LEN(512 Bytes) */ Status = SD_Erase(0x00, (BLOCK_SIZE * NUMBER_OF_BLOCKS)); } if (Status == SD_OK) { Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS); } if (Status == SD_OK) { EraseStatus = eBuffercmp(Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE); } /*------------------- Block Read/Write --------------------------*/ /* Fill the buffer to send */ Fill_Buffer(Buffer_Block_Tx, BLOCK_SIZE, 0xFFFF); if (Status == SD_OK) { /* Write block of 512 bytes on address 0 */ Status = SD_WriteBlock(Buffer_Block_Tx, 0x00, BLOCK_SIZE); } if (Status == SD_OK) { /* Read block of 512 bytes from address 0 */ Status = SD_ReadBlock(Buffer_Block_Rx, 0x00, BLOCK_SIZE); } if (Status == SD_OK) { /* Check the corectness of written dada */ TransferStatus1 = Buffercmp(Buffer_Block_Tx, Buffer_Block_Rx, BLOCK_SIZE); } /*--------------- Multiple Block Read/Write ---------------------*/ /* Fill the buffer to send */ Fill_Buffer(Buffer_MultiBlock_Tx, MULTI_BUFFER_SIZE, 0x0); if (Status == SD_OK) { /* Write multiple block of many bytes on address 0 */ Status = SD_WriteMultiBlocks(Buffer_MultiBlock_Tx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS); } if (Status == SD_OK) { /* Read block of many bytes from address 0 */ Status = SD_ReadMultiBlocks(Buffer_MultiBlock_Rx, 0x00, BLOCK_SIZE, NUMBER_OF_BLOCKS); } if (Status == SD_OK) { /* Check the corectness of written dada */ TransferStatus2 = Buffercmp(Buffer_MultiBlock_Tx, Buffer_MultiBlock_Rx, MULTI_BUFFER_SIZE); } /* Infinite loop */ while (1) {} }