/** Perform MMC block write from diskSect */ unsigned char WritePhysicalSector() { unsigned char c; //RED_LED = LED_ON; sectorAddress.l = sectorAddress.l * 2; //convert to bytes (combined with 8bit shift) c=MmcCommand(0x40 | 24, sectorAddress.b.b2, sectorAddress.b.b1, sectorAddress.b.b0, 0); sectorAddress.l = sectorAddress.l >> 1; //convert back to blocks //ConsolePutChar('w'); //ConsolePutHex8(c); if (c!=0x00) return (c); //Error - MMC did not go to write mode /* while (c!=0x00) { //wait for BUSY token, if you get 0x01(idle), it's an ERROR! c = SPIGetChar(); ConsolePutHex8(c); } */ dataBufPtr = diskSect.raw.buf; SPIPutCharWithoutWaiting(0xFE); SPIWait(); for (c=0;c<128;c++){ SPIPutCharWithoutWaiting(*dataBufPtr++); SPIWait(); SPIPutCharWithoutWaiting(*dataBufPtr++); SPIWait(); SPIPutCharWithoutWaiting(*dataBufPtr++); SPIWait(); SPIPutCharWithoutWaiting(*dataBufPtr++); SPIWait(); } //ConsolePutChar('-'); c = SPIGetChar(); //crc 1st byte (sends 0xff) c = SPIGetChar(); //crc 2nd byte (sends 0xff) c = SPIGetChar(); //ConsolePutHex8(c); //This prints xxx00101, (usually e5) when data ok // while (SPIGetChar()!=0xff) //busy wait moved to mmcCommand // ; // Wait until MMC not busy. SPI8Clocks(16); DeselectMSD(); SPI8Clocks(16); RED_LED_OFF(); return 0; }
/******************************************************************************* * Function Name : Mp3ReadRegister * Description : Read VS1003 register * Input : addressbyte--the vs1003 register address * Output : None * Return : The register value *******************************************************************************/ u16 Mp3ReadRegister(unsigned char addressbyte) { u16 resultvalue = 0; SDI_ChipSelect(RESET); while(DREQ); SCI_ChipSelect(SET); //XCS = 0 SPIPutChar(VS_READ_COMMAND); //���͜��Ĵ������� SPIPutChar(addressbyte); //���͟Ĵ����ľ�֡ resultvalue = SPIGetChar() << 8;//��ȥ��8Ν���� resultvalue |= SPIGetChar(); //��ȥ��8Ν���� while(DREQ); SCI_ChipSelect(RESET); return resultvalue; //����16Ν�Ĵ�����־ }
/** MMC Wait For Data start token 0xfe. * First any 0xFF's are read off the mmc. * The first non-ff octet is examined. If it's the 0xfe data start token, * everything is fine. If not, then some error has occurred. * Since we're in an embedded system, it's unclear what we should do then. * Current approach is to say all ok but make read block return 0xff's * by dropping the MMC card offline. Before that we read "lots" of * octets from the MMC to flush any pending data. Finally we return "ALL OK". * It's not disasterous at least as long as we don't WRITE to MMC. * 12/2005: Well, now we do write to mmc... */ unsigned char MmcWaitForData(){ unsigned char c; unsigned int i; i = 60000; //try max. 60000 bus cycles // Wait until something else than 0xff is read from the bus do { c=SPIGetChar(); --i; } while ((c == 0xff) && (i)); // Something was received from the bus? Might it actually be te // desired 0xFE data start token? if (c != 0xfe){ // No data start token, read fail. In an OS an error message would display. // Since we're in an embedded system, it's unclear what we should do now. // Current approach is to say all ok but make read block return 0xff's. // It's not disasterous at least as long as we don't WRITE to MMC. // Flush any data that might be pending from the MMC. #ifdef MMCLONGDEBUG { unsigned int i; ConsoleWrite("\r\nMMCWaitForData failed. \r\n"); ConsoleWrite("\r\nExpected 0xFE token, received: "); for (i=0;i<550;i++){ ConsolePutHex8(c); c=SPIGetChar(); } } #else Serial.print("\r\n NoData "); SPI8Clocks(200); /* Flush MMC by sending lots of FF's to it */ SPI8Clocks(200); SPI8Clocks(200); #endif DeselectMSD(); return 5; //Return error } return 0; }
/** Perform the actual reading of 512 octets from MMC. * Note: This is the fast routine to read complete disk block * If you have DMA, write this to use it! */ void PerformBlockRead(){ //SPI_DMACmd(SPI2, SPI_DMAReq_Rx, ENABLE); //ADD DMA //while(!DMA_GetFlagStatus(DMA_FLAG_TC4)); /* Use shared global buffer pointer for speed*/ /* Loop unrolled 16 times for SPEED! :) */ dataBufPtr = diskSect.raw.buf; while (dataBufPtr < diskSect.raw.buf+512){ *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); *dataBufPtr++ = SPIGetChar(); //LED_Sel(); } }