made slow/fast mode for mmc

master
Paul Goeser 14 years ago
parent d743e894d2
commit 35048d6afc

@ -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;

99
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<<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

@ -14,7 +14,8 @@ Copyright (C) 2004 Ulrich Radig
//#define SPI_Mode 1 //1 = Hardware SPI | 0 = Software SPI
#define SPI_Mode 0
#define WAIT_HALF_CLOCK _delay_us(18)
#define WAIT_HALF_CLOCK _delay_us(2)
#define WAIT_HALF_CLOCK_SLOW _delay_us(20)
#define MMC_Write PORTA //Port an der die MMC/SD-Karte angeschlossen ist also des SPI
#define MMC_Read PINA
@ -55,8 +56,10 @@ Copyright (C) 2004 Ulrich Radig
//Prototypes
extern unsigned char mmc_read_byte(void);
extern unsigned char mmc_read_byte_slow(void);
extern void mmc_write_byte(unsigned char);
extern void mmc_write_byte_slow(unsigned char);
extern void mmc_read_block(unsigned char *,unsigned char *,unsigned in);
@ -75,6 +78,7 @@ extern void mmc_read_to_ringbuffer(unsigned long, ringbuf_t *);
extern unsigned char mmc_write_sector (unsigned long,unsigned char *);
extern unsigned char mmc_write_command (unsigned char *);
extern unsigned char mmc_write_command_slow (unsigned char *);
extern unsigned char mmc_read_csd (unsigned char *);

Loading…
Cancel
Save