/* MMC.cpp - /* Daniel Riiff /* /* Contains the implementation of commands needed /* to interface with the SPI interface of MMC card */ #include #include #include "SPImaster.h" #include "MMC.h" using namespace std; bool InitMMC(void){ //raise SS and MOSI for 80 clock cycles //SendByte(0xff) 10 times with SS high //RAISE SS int delay=0, trials=0; char response=0x01; //initialization sequence on PowerUp RaiseSS(); for(int i=0;i<=9;i++) SendByte(0xff); LowerSS(); //Send Command 0 to put MMC in SPI mode SendCmd(0x00,0,0x95); //Now wait for READY RESPONSE //response=MMCgetResponse(); if(MMCgetResponse()!=0x01) return false; while(response==0x01){ cout<<"Sending Command 1"<=0;i--){ temp=(char)(argument>>(8*i)); cmd[4-i]=(temp); } cmd[5]=(CRC); for(i=0;i<6;i++) SendByte(cmd[i]); } //WRITE a BLOCK starting at address bool MMCwriteBlock(char *Block, int address){ char busy,dataResp; //Block size is 512 bytes exactly //First Lower SS LowerSS(); //Then send write command SendCmd(24,address,0xff); if(MMCgetResponse()==00){ //command was a success - now send data //start with DATA TOKEN = 0xFE SendByte(0xfe); //now send data for(int count=0;count<512;count++){ SendByte(Block[count]); } //data block sent - now send checksum SendByte(0xff); SendByte(0xff); //Now read in the DATA RESPONSE token dataResp=SendByte(0xff); //Following the DATA RESPONSE token //are a number of BUSY bytes //a zero byte indicates the MMC is busy busy=SendByte(0xff); while(busy==0){ busy=SendByte(0xff); } dataResp=dataResp&0x0f; //mask the high byte of the DATA RESPONSE token RaiseSS(); SendByte(0xff); if(dataResp==0x0b){ cout<<"DATA WAS NOT ACCEPTED BY CARD -- CRC ERROR"<