"Mark Stokes" <[email protected]> wrote: >> Does anybody have an experience in coupling MultiMediaCard (MMC) and >> msp430 via SPI ? > > Not yet, but I will in the next few weeks. For the mean time, check > out sandisk's web page. You can freely download a full copy of their > mmc spec which talks all about how to do it. > Let me know if you get it working before me. > -Mark
I have a piece of code that puts the card in SPI mode and reads the CSD. It isn't commented very well, but it works. See attachment. Andreas -- AVR-Tutorial, Forum, über 300 Links: http://www.mikrocontroller.net
/* * CVS: $Id: mmc.h,v 1.2 2003/03/06 16:11:46 andreas Exp $ * * */ #define MMC_GO_IDLE_STATE 0 #define MMC_SEND_OP_COND 1 #define MMC_SEND_CSD 9 #define MMC_SEND_CID 10 #define MMC_STOP_TRANSMISSION 12 #define MMC_SEND_STATUS 13 #define MMC_SET_BLOCKLEN 16 #define MMC_READ_SINGLE_BLOCK 17 #define MMC_READ_MULTIPLE_BLOCK 18 #define MMC_WRITE BLOCK 24 #define MMC_WRITE_MULTIPLE_BLOCK 25 #define MMC_PROGRAM_CSD 27 #define MMC_SET_WRITE_PROT 28 #define MMC_CLR_WRITE_PROT 29 #define MMC_SEND_WRITE_PROT 30 #define MMC_TAG_SECTOR_START 32 #define MMC_TAG_SECTOR_END 33 #define MMC_UNTAG_SECTOR 34 #define MMC_TAG_EREASE_GROUP_START 35 #define MMC_TAG_EREASE_GROUP_END 36 #define MMC_UNTAG_EREASE_GROUP 37 #define MMC_EREASE 38 #define MMC_READ_OCR 58 #define MMC_CRC_ON_OFF 59
/*
* CVS: $Id: main.c,v 1.3 2003/03/06 17:18:55 andreas Exp $
*
*/
#include <io.h>
#include <msp430/usart.h>
#include <stdio.h>
#include <stdlib.h>
#include "mmc.h"
#define EN_LOW() P3OUT &= ~0x01
#define EN_HIGH() P3OUT |= 0x01
#define SPI_RXC (IFG2 & URXIFG0)
#define SPI_TXC (IFG2 & UTXIFG0)
#define DUMMY 0xFF
char buf[16]="Test";
unsigned char cmd0buf[6] = {0x40,0x00,0x00,0x00,0x00,0x95};
unsigned int sendcmd(int command);
int main(void)
{
unsigned int i;
WDTCTL = WDTPW|WDTHOLD; // Stop WDT
ME2 |= USPIE0; // Enable USART0 SPI mode
UTCTL0 = CKPL|SSEL1|SSEL0|STC; // SMCLK, 3-pin mode
UCTL0 = CHAR+SYNC+MM; // 8-bit SPI Master **SWRST**
UBR00 = 0x02; // UCLK/2
UBR10 = 0x00; // 0
UMCTL0 = 0x00; // no modulation
P3SEL |= 0x0E; // P3.1-3 SPI option select
P3DIR |= 0x01; // P3.0 output direction
P3OUT = 0x00;
EN_HIGH();
for(i = 0; i < 10; i++) { //80 cycles
TXBUF0=DUMMY;
while ((IFG2 & UTXIFG0) == 0);
}
EN_LOW();
while(sendcmd(0)!=1); // wait until cmd0 returns 1
while(sendcmd(1)!=0); //wait until ready
// Read CSD
sendcmd(MMC_SEND_CSD);
{
TXBUF0=DUMMY; //8 clock cycles
while(!SPI_TXC);
} while(RXBUF0!=0xFE);
for(i=0;i<16;i++) {
TXBUF0=DUMMY; //send dummy byte to read response
while(!SPI_TXC);
while(!SPI_RXC);
buf[i]=RXBUF0;
}
for(;;); //infinite loop
}
//send command and return response
unsigned int sendcmd(int command)
{
int count;
unsigned int response;
TXBUF0 = 0x40+command;
while (!SPI_TXC);
for(count=0;count<4;count++) {
TXBUF0 = 0x00;
while (!SPI_TXC);
}
if(command == 0) {
TXBUF = 0x95; // CRC for cmd0
} else {
TXBUF0 = 0xFF; // CRC not used, send 0xFF
}
while (!SPI_TXC);
TXBUF0=DUMMY; //8 clock cycles
while (!SPI_TXC);
while (!SPI_RXC);
response = RXBUF0;
TXBUF0=DUMMY; //send dummy byte to read response
while (!SPI_TXC);
while(!SPI_RXC);
response=RXBUF0;
//untested but should work
if(command==MMC_SEND_STATUS) {
TXBUF0=DUMMY; //send dummy byte to read response
while (!SPI_TXC);
while(!SPI_RXC);
response|=(RXBUF0<<8)&0xFF00;
}
TXBUF0=DUMMY; //8 clock cycles
while(!SPI_TXC);
return response;
}
makefile
Description: Binary data
