View previous topic :: View next topic |
Author |
Message |
Antoni
Joined: 01 Jul 2005 Posts: 0
|
MMC+PIC16F876A initialitze and SPI_READ problems |
Posted: Fri Jul 01, 2005 3:26 am |
|
|
Hi.
I'm trying to cominicate a PIC16F87A with a MMC. I found a circuit in this page:
http://www.captain.at/electronics/pic-mmc/
And I'm using part of a code I found in:
http://www.microchipc.com/sourcecode/index.htm
I'm using the CCS PIC-C compiler.
You can see the circuit I'm using and the part of the progrm is this:
The comentaris are in catalan... I'm sorry.
This is the init fuction:
int mmc_init()
{
int i;
SETUP_SPI(SPI_MASTER | SPI_H_TO_L | SPI_MASTER_CLK_DIV_16 | SPI_SAMPLE_AT_END);
*0x94 |= 0x40; // establim CKE = 1 - clock en idle nivell baix
*0x14 &= 0xEF; // estalbim CKP = 0 - dada valida en flanc de pujada.
OUTPUT_HIGH(PIN_C2); // establim CS = 1 (off)
for(i=0;i<10;i++) // Començam inicialitzacio enviat clks on
{
SPI_WRITE(0xFF);
}
OUTPUT_LOW(PIN_C2); // Posam CS = 0 (on) la tarja passarà a mode SPI quan rebi un reset
SPI_WRITE(0x40); // enviam un RESET
SPI_WRITE(0x00); // tots els arguments son 0
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x95); // checksum precalculat ja que encara estem en mode MMC
puts("Enviant reset a la tarja\n");
if(mmc_response_R1()!=2) return 1; // Sila tarja ens ha enviat 0x01 vol dir que es troba en IDLE_STATE despres del reset.
//En cas contrari hi ha haut un error
puts("Hem rebut resposta desde la tarja despres del reset\n");
i = 0;
while((i < 255) && (mmc_response_R1()!=0)) // En viem un seguit de CMD1 fisn tenir la resposta correcte (0x00) o error de TIMEOUT
{
SPI_WRITE(0x41); // enviem CMD1 per sortir de IDLE_STATE
SPI_WRITE(0x00); // tots els argument son 0
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0xFF); // no es recareix checksum pero sempre enviarem 0xFF
i++;
}
if(i >= 254)
{
puts("Hi ha hagut un error de timeout\n");
return 1; // Si i>= 254 hem agut desperar massa per una resposta 0x00 desde la MMC. Error de timeout.
}
puts("Hem rebut la resposta corecte en la sortida de idle idle desde MMC\n");
OUTPUT_HIGH(PIN_C2); // establim CS = 1 (off)
SPI_WRITE(0xFF); // extra clocks per donar temps a la MMC que acabi de fer el que li fa.
OUTPUT_LOW(PIN_C2); // establim CS = 0 (on)
SPI_WRITE(0x50); // enviem CMD16 (SET_BLOCKLEN)
SPI_WRITE(0x00);
SPI_WRITE(0x00);
SPI_WRITE(0x02); // 0x200=512 bytes
SPI_WRITE(0x00); //
SPI_WRITE(0xFF); // no es recareix checksum pero sempre enviarem 0xFF
if(mmc_response_R1()!=0) return 1;
OUTPUT_HIGH(PIN_C2); // establim CS = 1 (off)
puts("Establert el tamany de la llongitud de bloc\n");
return 0;
}
int mmc_response_R1()
{
int R1=0xFF, count=0x10; //[spam] un contador suficientment llarg
while (count>0) //Blucle que va consultant la resposta R1
{
R1=SPI_READ(0x01);
printf("La resposta R1= %u\n",R1); //Escivim la resposta obtinguda per pantalla
--count;
if(R1==0x01)
{
puts("La Tarja esta en IDLE STATE\n");
return 2; //Si estem an IDLE_STATE retornem el valor 2.
}
if(R1==0x00)
{
puts("Sa rebut R1 correcte");
return 0;
}
}
return 1;
}
int mmc_response_WDATA()
{
int WDATA;
WDATA=SPI_READ(0x00);
printf("La resposta WDATA= %u",WDATA);
if((WDATA&&0xF8)==0x58)
{
puts("S'ha acceptat les dades escrites\n");
return 0;
}
if((WDATA&&0xF8)==0x28)
{
puts("NO s'ha acceptat les dades escrites\n");
return 1;
}
if((WDATA&&0xF8)==0x48)
{
puts("NO s'ha acceptat les dades escrites degut a un error d'escriptura\n");
return 1;
}
else
{
puts("Malament en quant a la escriptura\n");
return 1;
}
}
int get_START_DATA_BLOCK()
{
int STDATA;
unsigned long count=0xFF;
while(STDATA!=0xFE||count==0)
{
STDATA=SPI_READ(0xFF);
--count;
}
if(STDATA==0xFE)
{
puts("S'ha rebut l'START BYTE del data token\n");
return 0;
}
else
{
puts("No s'ha rebut com cal l'START BYTE del data token\n");
return 1;
}
The problem is that when I inisialitze my MMC, just after I send the RESET comand, I recive R1=01111111. I use the SPI_READ(0xFF).
SPI_SETUP(SPI_MASTER | SPI_H_TO_L | SPI_MASTER_CLK_DIV_16 | SPI_SAMPLE_AT_END);
I don't have pull-up resistors at data lines, I thinck is not necessary.
Does some body knows what the 0xFF mean?
Is possible I'm not well sychronized reading the MMC?
I ned it for my fianal project of electronic degree and I'm going mad!!!! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Fri Jul 01, 2005 4:02 am |
|
|
Quote: | I don't have pull-up resistors at data lines, I thinck is not necessary. | You know your circuit is different from the specifications....
Your circuit is not working........
Don't you think it is a good thing to fix your circuit first????
On power up of the MMC, the card is working in MMC-mode which means it is driving the busses with open-collector outputs. That's why the pull-up resistors are required. If you want to become a good engineer you will always have to follow the specifications, even when you are lucky and your prototype is functioning with fewer parts than specified.
Use pull-up resistors of 100k on Data_In and Data_Out.
Maybe something else is causing your troubles, but without the modifications I'm not going to look into it. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
|
Antoni
Joined: 01 Jul 2005 Posts: 0
|
|
Posted: Tue Jul 12, 2005 4:09 am |
|
|
Thanks ckielstra.
I just put the pull-up resistors and it's worck fine.
Now I can finish my project. |
|
|
|