From 35048d6afc052c5b9e7a560ee37c95c1d221367c Mon Sep 17 00:00:00 2001 From: Paul Goeser Date: Sat, 14 Jan 2012 13:53:56 +0100 Subject: [PATCH] made slow/fast mode for mmc --- gg.c | 9 +++--- mmc.c | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++-- mmc.h | 6 +++- 3 files changed, 106 insertions(+), 8 deletions(-) diff --git a/gg.c b/gg.c index ee69926..ab2ecdf 100644 --- a/gg.c +++ b/gg.c @@ -22,7 +22,7 @@ void timer_init_tiny26(void) cnt_to_next = 1;/* overflows to next sample */ TCCR1A = (1 << COM1B1) | (1 << PWM1B);/* pwm enable, not inverted */ - TCCR1B = (1 << CS11) | (1 << CS10);/* fast pwm, prescaler / 2 */ + TCCR1B = (1 << CS11);/* fast pwm, prescaler / 2 */ DDRB |= (1 << PB3); TIMSK = 1 << TOIE1;/* interrupt on overflow */ @@ -99,11 +99,12 @@ int main(void) timer_init_tiny26(); sei(); - -/* unsigned long block = 0; +/* + unsigned long block = 0; for(;; block++) { - mmc_read_to_ringbuffer(block, &rb); + //mmc_read_to_ringbuffer(block, &rb); + mmc_read_to_ringbuffer(0, &rb); } // */ // debug = 0; diff --git a/mmc.c b/mmc.c index 8f5e4e2..1c5db8f 100644 --- a/mmc.c +++ b/mmc.c @@ -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< 0) //Lesen des Pegels von MMC_DI + { + Byte |= 1; + } + MMC_Write |=(1<>= 1) //das Byte wird Bitweise nacheinander Gesendet MSB First + { + MMC_Write &= ~(1<