|
|
|
|
@ -57,12 +57,12 @@ unsigned char mmc_init ()
|
|
|
|
|
//Initialisiere MMC/SD-Karte in den SPI-Mode
|
|
|
|
|
for (unsigned char b = 0;b<0x0f;b++) //Sendet min 74+ Clocks an die MMC/SD-Karte
|
|
|
|
|
{
|
|
|
|
|
mmc_write_byte(0xff);
|
|
|
|
|
mmc_write_byte_slow(0xff);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Sendet Commando CMD0 an MMC/SD-Karte
|
|
|
|
|
unsigned char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
|
|
|
|
|
while(mmc_write_command (CMD) != 1)
|
|
|
|
|
while(mmc_write_command_slow (CMD) != 1)
|
|
|
|
|
{
|
|
|
|
|
if (Timeout++ > 200)
|
|
|
|
|
{
|
|
|
|
|
@ -75,7 +75,7 @@ unsigned char mmc_init ()
|
|
|
|
|
Timeout = 0;
|
|
|
|
|
CMD[0] = 0x41;//Commando 1
|
|
|
|
|
CMD[5] = 0xFF;
|
|
|
|
|
while( mmc_write_command (CMD) !=0)
|
|
|
|
|
while( mmc_write_command_slow (CMD) !=0)
|
|
|
|
|
{
|
|
|
|
|
if (Timeout++ > 400)
|
|
|
|
|
{
|
|
|
|
|
@ -140,6 +140,41 @@ unsigned char mmc_write_command (unsigned char *cmd)
|
|
|
|
|
return(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//############################################################################
|
|
|
|
|
//Sendet ein Commando an die MMC/SD-Karte
|
|
|
|
|
unsigned char mmc_write_command_slow(unsigned char *cmd)
|
|
|
|
|
//############################################################################
|
|
|
|
|
{
|
|
|
|
|
unsigned char tmp = 0xff;
|
|
|
|
|
unsigned int Timeout = 0;
|
|
|
|
|
|
|
|
|
|
//set MMC_Chip_Select to high (MMC/SD-Karte Inaktiv)
|
|
|
|
|
MMC_Disable();
|
|
|
|
|
|
|
|
|
|
//sendet 8 Clock Impulse
|
|
|
|
|
mmc_write_byte_slow(0xFF);
|
|
|
|
|
|
|
|
|
|
//set MMC_Chip_Select to low (MMC/SD-Karte Aktiv)
|
|
|
|
|
MMC_Enable();
|
|
|
|
|
|
|
|
|
|
//sendet 6 Byte Commando
|
|
|
|
|
for (unsigned char a = 0;a<0x06;a++) //sendet 6 Byte Commando zur MMC/SD-Karte
|
|
|
|
|
{
|
|
|
|
|
mmc_write_byte_slow(*cmd++);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//Wartet auf ein gültige Antwort von der MMC/SD-Karte
|
|
|
|
|
while (tmp == 0xff)
|
|
|
|
|
{
|
|
|
|
|
tmp = mmc_read_byte_slow();
|
|
|
|
|
if (Timeout++ > 500)
|
|
|
|
|
{
|
|
|
|
|
break; //Abbruch da die MMC/SD-Karte nicht Antwortet
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return(tmp);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//############################################################################
|
|
|
|
|
//Routine zum Empfangen eines Bytes von der MMC-Karte
|
|
|
|
|
unsigned char mmc_read_byte (void)
|
|
|
|
|
@ -168,6 +203,35 @@ unsigned char mmc_read_byte (void)
|
|
|
|
|
return (Byte);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//############################################################################
|
|
|
|
|
//Routine zum Empfangen eines Bytes von der MMC-Karte
|
|
|
|
|
unsigned char mmc_read_byte_slow (void)
|
|
|
|
|
//############################################################################
|
|
|
|
|
{
|
|
|
|
|
unsigned char Byte = 0;
|
|
|
|
|
#if SPI_Mode //Routine für Hardware SPI
|
|
|
|
|
SPDR = 0xff;
|
|
|
|
|
while(!(SPSR & (1 << SPIF))) {;}
|
|
|
|
|
Byte = SPDR;
|
|
|
|
|
#else //Routine für Software SPI
|
|
|
|
|
for(unsigned char a=8; a>0; a--) //das Byte wird Bitweise nacheinander Empangen MSB First
|
|
|
|
|
{
|
|
|
|
|
Byte <<= 1;
|
|
|
|
|
MMC_Write &=~(1<<SPI_Clock); //erzeugt ein Clock Impuls (Low)
|
|
|
|
|
WAIT_HALF_CLOCK_SLOW;
|
|
|
|
|
|
|
|
|
|
if(bit_is_set(MMC_Read,SPI_DI) > 0) //Lesen des Pegels von MMC_DI
|
|
|
|
|
{
|
|
|
|
|
Byte |= 1;
|
|
|
|
|
}
|
|
|
|
|
MMC_Write |=(1<<SPI_Clock); //setzt Clock Impuls wieder auf (High)
|
|
|
|
|
WAIT_HALF_CLOCK_SLOW;
|
|
|
|
|
}
|
|
|
|
|
#endif
|
|
|
|
|
return (Byte);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//############################################################################
|
|
|
|
|
//Routine zum Senden eines Bytes zur MMC-Karte
|
|
|
|
|
@ -198,6 +262,35 @@ void mmc_write_byte (unsigned char Byte)
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//############################################################################
|
|
|
|
|
//Routine zum Senden eines Bytes zur MMC-Karte
|
|
|
|
|
void mmc_write_byte_slow(unsigned char Byte)
|
|
|
|
|
//############################################################################
|
|
|
|
|
{
|
|
|
|
|
#if SPI_Mode //Routine für Hardware SPI
|
|
|
|
|
SPDR = Byte; //Sendet ein Byte
|
|
|
|
|
while(!(SPSR & (1 << SPIF))) {;}//Wartet bis Byte gesendet wurde
|
|
|
|
|
#else //Routine für Software SPI
|
|
|
|
|
for(uint8_t current_bit = 1 << 7; current_bit; current_bit >>= 1) //das Byte wird Bitweise nacheinander Gesendet MSB First
|
|
|
|
|
{
|
|
|
|
|
MMC_Write &= ~(1<<SPI_Clock); //erzeugt ein Clock Impuls (LOW)
|
|
|
|
|
if(Byte & current_bit) //Ist Bit a in Byte gesetzt
|
|
|
|
|
{
|
|
|
|
|
MMC_Write |= (1<<SPI_DO); //Set Output High
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
MMC_Write &= ~(1<<SPI_DO); //Set Output Low
|
|
|
|
|
}
|
|
|
|
|
WAIT_HALF_CLOCK_SLOW;
|
|
|
|
|
MMC_Write |= (1<<SPI_Clock); //setzt Clock Impuls wieder auf (High)
|
|
|
|
|
// bei dieser flanke werden die daten von der karte gelesen
|
|
|
|
|
WAIT_HALF_CLOCK_SLOW;
|
|
|
|
|
}
|
|
|
|
|
MMC_Write |= (1<<SPI_DO); //setzt Output wieder auf High
|
|
|
|
|
#endif
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
|
//############################################################################
|
|
|
|
|
//Routine zum schreiben eines Blocks(512Byte) auf die MMC/SD-Karte
|
|
|
|
|
|