接續上一篇
FATFS是功能完整的檔案系統
但需要移稙的檔案也就比較多
1. disk_status
/*-----------------------------------------------------------------------*/ /* Get disk status */ /*-----------------------------------------------------------------------*/ /* Physical drive number (0) */ DSTATUS disk_status(BYTE drv) { if (drv) return STA_NOINIT; /* Supports only drive 0 */ return Stat; /* Return disk status */ }
2. disk_initialize
/*-----------------------------------------------------------------------*/ /* Initialize disk drive */ /*-----------------------------------------------------------------------*/ /* Physical drive number (0) */ DSTATUS disk_initialize (BYTE drv) { BYTE n, cmd, ty, ocr[4]; if(drv) return STA_NOINIT; /* Supports only drive 0 */ init_spi(); /* Initialize SPI */ if(Stat & STA_NODISK) return Stat; /* Is card existing in the soket? */ FCLK_SLOW(); for (n = 10; n; n--) spi_wr(0xFF); /* Send 80 dummy clocks */ ty = 0; if (send_cmd(CMD0, 0) == 1) { /* Put the card SPI/Idle state */ Timer1 = 1000; /* Initialization timeout = 1 sec */ if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2? */ for (n = 0; n < 4; n++) ocr[n] = spi_wr(0xFF); /* Get 32 bit return value of R7 resp */ if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* Is the card supports vcc of 2.7-3.6V? */ while (Timer1 && send_cmd(ACMD41, 1UL << 30)); /* Wait for end of initialization with ACMD41(HCS) */ if (Timer1 && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */ for (n = 0; n < 4; n++) ocr[n] = spi_wr(0xFF); if(ocr[0] & 0x40) ty = CT_SD2 | CT_BLOCK; else ty = CT_SD2; //ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* Card id SDv2 */ } } } else { /* Not SDv2 card */ if (send_cmd(ACMD41, 0) <= 1) { /* SDv1 or MMC? */ ty = CT_SD1; cmd = ACMD41; /* SDv1 (ACMD41(0)) */ } else { ty = CT_MMC; cmd = CMD1; /* MMCv3 (CMD1(0)) */ } while (Timer1 && send_cmd(cmd, 0)) ; /* Wait for end of initialization */ if (!Timer1 || send_cmd(CMD16, 512) != 0) /* Set block length: 512 */ ty = 0; } } CardType = ty; /* Card type */ deselect(); if (ty) { /* OK */ FCLK_FAST(); /* Set fast clock */ Stat &= ~STA_NOINIT; /* Clear STA_NOINIT flag */ } else { /* Failed */ Stat = STA_NOINIT; } return Stat; }
3. disk_read
/*-----------------------------------------------------------------------*/ /* Read sector(s) */ /*-----------------------------------------------------------------------*/ /* Physical drive number (0) */ /* Pointer to the data buffer to store read data */ /* Start sector number (LBA) */ /* Number of sectors to read (1..128) */ DRESULT disk_read(BYTE drv,BYTE *buff,DWORD sector,UINT count) { if (drv || !count) return RES_PARERR; /* Check parameter */ if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */ if (!(CardType & CT_BLOCK)) sector *= 512; /* LBA ot BA conversion (byte addressing cards) */ if (count == 1) { /* Single sector read */ if ((send_cmd(CMD17, sector) == 0)&& rcvr_datablock(buff, 512)) /* READ_SINGLE_BLOCK */ count = 0; } else { /* Multiple sector read */ if (send_cmd(CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */ do{ if (!rcvr_datablock(buff, 512)) break; buff += 512; } while (--count); send_cmd(CMD12, 0); /* STOP_TRANSMISSION */ } } deselect(); if(count) return RES_ERROR; else return RES_OK; }
4. disk_write
/*-----------------------------------------------------------------------*/ /* Write sector(s) */ /*-----------------------------------------------------------------------*/ /* Physical drive number (0) */ /* Ponter to the data to write */ /* Start sector number (LBA) */ /* Number of sectors to write (1..128) */ #if _USE_WRITE DRESULT disk_write(BYTE drv,const BYTE *buff,DWORD sector,UINT count) { if (drv || !count) return RES_PARERR; /* Check parameter */ if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check drive status */ if (Stat & STA_PROTECT) return RES_WRPRT; /* Check write protect */ if (!(CardType & CT_BLOCK)) sector *= 512; /* LBA ==> BA conversion (byte addressing cards) */ if (count == 1) { /* Single sector write */ if ((send_cmd(CMD24, sector) == 0)&& xmit_datablock(buff, 0xFE)) /* WRITE_BLOCK */ count = 0; } else { /* Multiple sector write */ if (CardType & CT_SDC) send_cmd(ACMD23, count); /* Predefine number of sectors */ if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */ do{ if (!xmit_datablock(buff, 0xFC)) break; buff += 512; } while (--count); if (!xmit_datablock(0, 0xFD)) /* STOP_TRAN token */ count = 1; } } deselect(); if(count) return RES_ERROR; else return RES_OK; } #endif
5. disk_ioctl
/*-----------------------------------------------------------------------*/ /* Miscellaneous drive controls other than data read/write */ /*-----------------------------------------------------------------------*/ /* Physical drive number (0) */ /* Control command code */ /* Pointer to the conrtol data */ #if _USE_IOCTL DRESULT disk_ioctl(BYTE drv,BYTE cmd,void *buff) { DRESULT res; BYTE n, csd[16]; DWORD *dp, st, ed, csize; if (drv) return RES_PARERR; /* Check parameter */ if (Stat & STA_NOINIT) return RES_NOTRDY; /* Check if drive is ready */ res = RES_ERROR; switch (cmd) { case CTRL_SYNC : /* Wait for end of internal write process of the drive */ if (select()) res = RES_OK; break; case GET_SECTOR_COUNT : /* Get drive capacity in unit of sector (DWORD) */ if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */ csize = csd[9] + ((WORD)csd[8] << 8) + ((DWORD)(csd[7] & 63) << 16) + 1; *(DWORD*)buff = csize << 10; } else { /* SDC ver 1.XX or MMC ver 3 */ n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2; csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1; *(DWORD*)buff = csize << (n - 9); } res = RES_OK; } break; case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */ if (CardType & CT_SD2) { /* SDC ver 2.00 */ if (send_cmd(ACMD13, 0) == 0) { /* Read SD status */ spi_wr(0xFF); if (rcvr_datablock(csd, 16)) { /* Read partial block */ for (n = 64 - 16; n; n--) spi_wr(0xFF); /* Purge trailing data */ *(DWORD*)buff = 16UL << (csd[10] >> 4); res = RES_OK; } } } else { /* SDC ver 1.XX or MMC */ if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { /* Read CSD */ if (CardType & CT_SD1) { /* SDC ver 1.XX */ *(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1); } else { /* MMC */ *(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1); } res = RES_OK; } } break; case CTRL_ERASE_SECTOR : /* Erase a block of sectors (used when _USE_ERASE == 1) */ if (!(CardType & CT_SDC)) break; /* Check if the card is SDC */ if (disk_ioctl(drv, MMC_GET_CSD, csd)) break; /* Get CSD */ if (!(csd[0] >> 6) && !(csd[10] & 0x40)) break; /* Check if sector erase can be applied to the card */ dp = buff; st = dp[0]; ed = dp[1]; /* Load sector block */ if (!(CardType & CT_BLOCK)) { st *= 512; ed *= 512; } if (send_cmd(CMD32, st) == 0 && send_cmd(CMD33, ed) == 0 && send_cmd(CMD38, 0) == 0 && wait_ready(30000)) /* Erase sector block */ res = RES_OK; /* FatFs does not check result of this command */ break; default: res = RES_PARERR; } deselect(); return res; } #endif
6. disk_fattime
DWORD get_fattime(void) { //RTCTIME rtc; /* Get local time */ // if (!rtc_gettime(&rtc)) return 0; /* Pack date and time into a DWORD variable */ return ((DWORD)(2014 - 1980) << 25) | ((DWORD)3 << 21) | ((DWORD)27 << 16) | ((DWORD)16 << 11) | ((DWORD)22 << 5) | ((DWORD)30 >> 1); }
沒用真的RTC下去做測試
Demo code 一樣有KEIL 和 IAR
FATFS for nuc122
沒有留言:
張貼留言