123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390 |
- #include "diskio.h"
- #include "nand_flash.h"
- #include "sdcard.h"
- #include "string.h"
- #include "usbh_bsp_msc.h"
- #define FS_SD 0
- #define FS_NAND 1
-
-
- #define FS_USB 2
- #define SD_BLOCKSIZE 512
- extern SD_CardInfo SDCardInfo;
- DSTATUS disk_status(
- BYTE pdrv
- )
- {
- DSTATUS status = STA_NOINIT;
- switch (pdrv)
- {
- case FS_SD:
- status &= ~STA_NOINIT;
- break;
- case FS_NAND:
- status = 0;
- break;
- case FS_USB:
- status = 0;
- break;
- }
- return status;
- }
- DSTATUS disk_initialize(
- BYTE pdrv
- )
- {
- DSTATUS status = STA_NOINIT;
- switch (pdrv)
- {
- case FS_SD:
- if (SD_Init() == SD_OK)
- {
- status &= ~STA_NOINIT;
- }
- else
- {
- status = STA_NOINIT;
- }
- break;
- case FS_NAND:
- if (nand_flash_init() == NAND_OK)
- {
- status = RES_OK;
- }
- else
- {
-
- printf("NAND_Init() Error! \r\n");
- status = RES_ERROR;
- }
- break;
- case FS_USB:
-
- if (HCD_IsDeviceConnected(&USB_OTG_Core))
- {
- status &= ~STA_NOINIT;
- }
- break;
- }
- return status;
- }
- DRESULT disk_read(
- BYTE pdrv,
- BYTE *buff,
- DWORD sector,
- UINT count
- )
- {
- DRESULT status = RES_PARERR;
- SD_Error SD_state = SD_OK;
- switch (pdrv)
- {
- case FS_SD:
- if ((DWORD)buff & 3)
- {
- DRESULT res = RES_OK;
- DWORD scratch[SD_BLOCKSIZE / 4];
- while (count--)
- {
- res = disk_read(FS_SD, (void *)scratch, sector++, 1);
- if (res != RES_OK)
- {
- break;
- }
- memcpy(buff, scratch, SD_BLOCKSIZE);
- buff += SD_BLOCKSIZE;
- }
- return res;
- }
- SD_state = SD_ReadMultiBlocks(buff, sector * SD_BLOCKSIZE, SD_BLOCKSIZE, count);
- if (SD_state == SD_OK)
- {
-
- SD_state = SD_WaitReadOperation();
- while (SD_GetStatus() != SD_TRANSFER_OK)
- ;
- }
- if (SD_state != SD_OK)
- status = RES_PARERR;
- else
- status = RES_OK;
- break;
- case FS_NAND:
- if (NAND_OK == NAND_ReadMultiSectors(buff, sector, 512, count))
- {
- status = RES_OK;
- }
- else
- {
- printf("NAND_ReadMultiSectors() Error! sector = %d, count = %d \r\n", sector, count);
- status = RES_ERROR;
- }
- break;
- case FS_USB:
- {
- BYTE res = USBH_MSC_OK;
- if (HCD_IsDeviceConnected(&USB_OTG_Core))
- {
- do
- {
- res = USBH_MSC_Read10(&USB_OTG_Core, buff, sector, 512 * count);
- USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);
- if (!HCD_IsDeviceConnected(&USB_OTG_Core))
- {
- break;
- }
- } while (res == USBH_MSC_BUSY);
- }
- if (res == USBH_MSC_OK)
- {
- status = RES_OK;
- }
- else
- {
- status = RES_ERROR;
- }
- }
- break;
- }
- return status;
- }
- #if _USE_WRITE
- DRESULT disk_write(
- BYTE pdrv,
- const BYTE *buff,
- DWORD sector,
- UINT count
- )
- {
- DRESULT status = RES_PARERR;
- SD_Error SD_state = SD_OK;
- if (!count)
- {
- return RES_PARERR;
- }
- switch (pdrv)
- {
- case FS_SD:
- if ((DWORD)buff & 3)
- {
- DRESULT res = RES_OK;
- DWORD scratch[SD_BLOCKSIZE / 4];
- while (count--)
- {
- memcpy(scratch, buff, SD_BLOCKSIZE);
- res = disk_write(FS_SD, (void *)scratch, sector++, 1);
- if (res != RES_OK)
- {
- break;
- }
- buff += SD_BLOCKSIZE;
- }
- return res;
- }
- SD_state = SD_WriteMultiBlocks((uint8_t *)buff, sector * SD_BLOCKSIZE, SD_BLOCKSIZE, count);
- if (SD_state == SD_OK)
- {
-
- SD_state = SD_WaitWriteOperation();
-
- while (SD_GetStatus() != SD_TRANSFER_OK)
- ;
- }
- if (SD_state != SD_OK)
- status = RES_PARERR;
- else
- status = RES_OK;
- break;
- case FS_NAND:
- if (NAND_OK == NAND_WriteMultiSectors((uint8_t *)buff, sector, 512, count))
- {
- status = RES_OK;
- }
- else
- {
- printf("NAND_ReadMultiSectors() Error! sector = %d, count = %d \r\n", sector, count);
- status = RES_ERROR;
- }
- break;
- case FS_USB:
- {
- BYTE res = USBH_MSC_OK;
- if (HCD_IsDeviceConnected(&USB_OTG_Core))
- {
- do
- {
- res = USBH_MSC_Write10(&USB_OTG_Core, (BYTE *)buff, sector, 512 * count);
- USBH_MSC_HandleBOTXfer(&USB_OTG_Core, &USB_Host);
- if (!HCD_IsDeviceConnected(&USB_OTG_Core))
- {
- break;
- }
- } while (res == USBH_MSC_BUSY);
- }
- if (res == USBH_MSC_OK)
- {
- status = RES_OK;
- }
- else
- {
- status = RES_ERROR;
- }
- }
- break;
- }
- return status;
- }
- #endif
- #if _USE_IOCTL
- DRESULT disk_ioctl(
- BYTE pdrv,
- BYTE cmd,
- void *buff
- )
- {
- DRESULT status = RES_PARERR;
- switch (pdrv)
- {
- case FS_SD:
- switch (cmd)
- {
-
- case GET_SECTOR_SIZE:
- *(WORD *)buff = SD_BLOCKSIZE;
- break;
-
- case GET_BLOCK_SIZE:
- *(DWORD *)buff = 1;
- break;
- case GET_SECTOR_COUNT:
- *(DWORD *)buff = SDCardInfo.CardCapacity / SDCardInfo.CardBlockSize;
- break;
- case CTRL_SYNC:
- break;
- }
- status = RES_OK;
- break;
- case FS_NAND:
- status = RES_OK;
- break;
- case FS_USB:
-
- switch (cmd)
- {
- case CTRL_SYNC:
- status = RES_OK;
- break;
- case GET_SECTOR_COUNT:
- *(DWORD *)buff = (DWORD)USBH_MSC_Param.MSCapacity;
- status = RES_OK;
- break;
- case GET_SECTOR_SIZE:
- *(WORD *)buff = 512;
- status = RES_OK;
- break;
- case GET_BLOCK_SIZE:
- *(DWORD *)buff = 512;
- status = RES_OK;
- break;
- default:
- status = RES_PARERR;
- break;
- }
- break;
- }
- return status;
- }
- #endif
|