//char mmcWriteBlock (const unsigned long address) char mmcWriteBlock (const unsigned long address, const unsigned long count, unsigned char *pBuffer) { char rvalue = MMC_RESPONSE_ERROR; // MMC_SUCCESS; if (mmcSetBlockLength (count) == MMC_SUCCESS) { // Set the block length to read MMC_CS_LOW (); mmcSendCmd (MMC_WRITE_BLOCK,address, 0xFF); // Send write command // Check if the MMC acknowledged the write block command // it will do this by sending an affirmative response // in the R1 format (0x00 is no errors) if (mmcGetXXResponse(MMC_R1_RESPONSE) == MMC_R1_RESPONSE) { spiSendByte(DUMMY_CHAR); spiSendByte(0xfe); // Send the data token to signify the start of the data spiSendFrame(pBuffer, count); // Clock the actual data transfer and transmit the bytes // Put CRC bytes (not really needed by us, but required by MMC) spiSendByte(DUMMY_CHAR); spiSendByte(DUMMY_CHAR); // Read the data response xxx0<status>1 // Status = 010: Data accepted // Status 101: Data rejected due to a CRC error // Status 110: Data rejected due to a write error. rvalue = mmcCheckBusy(); if(rvalue==MMC_SUCCESS) { // check status after write for any possible errors during write (see sandisk.pdf, p.63) MMC_CS_HIGH(); spiSendByte(DUMMY_CHAR); MMC_CS_LOW(); mmcSendCmd(MMC_SEND_STATUS, 0, 0xFF); if (mmcGetR2Response() != 0x0000) { rvalue = MMC_WRITE_ERROR; } } } else { // The MMC never acknowledge the write command rvalue = MMC_RESPONSE_ERROR; // 2 } } else { rvalue = MMC_BLOCK_SET_ERROR; // 1 } MMC_CS_HIGH(); spiSendByte(DUMMY_CHAR); // Send 8 Clock pulses of delay. return rvalue; } // mmc_write_block
//--------------------------------------------------------------------- //char mmcWriteBlock (const unsigned long address) unsigned char mmcWriteBlock (const unsigned long address, const unsigned long count, unsigned char *pBuffer) { unsigned long i = 0; unsigned char rvalue = MMC_RESPONSE_ERROR; // MMC_SUCCESS; // char c = 0x00; // Set the block length to read if (mmcSetBlockLength (count) == MMC_SUCCESS) // block length could be set { // SS = LOW (on) CS_LOW (); // send write command mmcSendCmd (MMC_WRITE_BLOCK,address, 0xFF); // check if the MMC acknowledged the write block command // it will do this by sending an affirmative response // in the R1 format (0x00 is no errors) if (mmcGetXXResponse(MMC_R1_RESPONSE) == MMC_R1_RESPONSE) { spiSendByte(0xff); // send the data token to signify the start of the data spiSendByte(0xfe); // clock the actual data transfer and transmitt the bytes #ifndef withDMA for (i = 0; i < count; i++) spiSendByte(pBuffer[i]); #else /* Get the block */ /* DMA trigger is UART send */ DMACTL0 &= ~(DMA0TSEL_15); DMACTL0 |= (DMA0TSEL_9); /* Source DMA address: the data buffer. */ DMA0SA = (unsigned short)pBuffer; /* Destination DMA address: the UART send register. */ DMA0DA = U1TXBUF_; /* The size of the block to be transferred */ DMA0SZ = count; /* Configure the DMA transfer*/ DMA0CTL = DMAREQ | /* start transfer */ DMADT_0 | /* Single transfer mode */ DMASBDB | /* Byte mode */ DMAEN | /* Enable DMA */ DMASRCINCR1 | DMASRCINCR0; /* Increment the source address */ #endif // put CRC bytes (not really needed by us, but required by MMC) spiSendByte(0xff); spiSendByte(0xff); // read the data response xxx0<status>1 : status 010: Data accected, status 101: Data // rejected due to a crc error, status 110: Data rejected due to a Write error. mmcCheckBusy(); rvalue = MMC_SUCCESS; } else { // the MMC never acknowledge the write command rvalue = MMC_RESPONSE_ERROR; // 2 } } else { rvalue = MMC_BLOCK_SET_ERROR; // 1 } // give the MMC the required clocks to finish up what ever it needs to do // for (i = 0; i < 9; ++i) // spiSendByte(0xff); CS_HIGH (); // Send 8 Clock pulses of delay. spiSendByte(0xff); return rvalue; } // mmc_write_block