MMC kart simülasyon problemi

Başlatan berkay_91, 21 Ocak 2015, 23:39:07

berkay_91

mrb, MMC kart için bulduğum kodları proteusta simüle ettim ve resimdeki gibi bir hata verdi


/*
 * sd_card.c
 *
 * Created: 15.12.2014 23:53:18
 *  Author: BERKAY
 */ 

#define F_CPU 1000000UL
#include <stdio.h>
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include "lcd.h"

#define DI 6      // Port B bit 6 (pin7): data in (data from MMC)
#define DT 5      // Port B bit 5 (pin6): data out (data to MMC)
#define CLK 7     // Port B bit 7 (pin8): clock
#define CS 4      // Port B bit 4 (pin5): chip select for MMC

volatile char chars[512];
volatile int x;

FILE lcd_str = FDEV_SETUP_STREAM(lcd_putc, NULL, _FDEV_SETUP_WRITE);

void ini_SPI(void) {
	
	DDRB &= ~(_BV(DI));       //input
	DDRB |= _BV(CLK);         //outputs
	DDRB |= _BV(DT);          //outputs
	DDRB |= _BV(CS);          //outputs
	SPCR |= _BV(SPE);         //SPI enable
	SPCR |= _BV(MSTR);        //Master SPI mode
	//SPCR &= ~(_BV(SPR1));     //fosc/16
	//SPCR |= _BV(SPR0);        //fosc/16
	//SPSR &= ~(_BV(SPI2X));    //speed is not doubled
	PORTB |= (1<<4);      //Enable CS pin for the SD card
	
	sei();
}


char SPI_sendchar(char chr) {
	
	PORTB &= ~(1<<PB4);
	
	SPDR = chr;
	while(!(SPSR & (1<<SPIF)));
	
	PORTB |= (1<<PB4);
}


char Command(char cmd, uint16_t ArgH, uint16_t ArgL, char crc ) {
	
	SPI_sendchar(0xFF);
	SPI_sendchar(cmd);
	SPI_sendchar((uint8_t)(ArgH >> 8));
	SPI_sendchar((uint8_t)ArgH);
	SPI_sendchar((uint8_t)(ArgL >> 8));
	SPI_sendchar((uint8_t)ArgL);
	SPI_sendchar(crc);
	SPI_sendchar(0xFF);
	return SPI_sendchar(0xFF);                // Returns the last byte received
}


void ini_SD(void) {
	
	char i;
	PORTB |= _BV(CS);                    //disable CS
	for(i=0; i < 10; i++)
	SPI_sendchar(0x1F);                // Send 10 * 5 = 50 clock pulses 250 kHz
	PORTB &= ~(_BV(CS));                 //enable CS
	for(i=0; i < 2; i++)
	SPI_sendchar(0x1F);                // Send 2 * 5 = 10 clock pulses 250 kHz
	Command(0x40,0,0,0x95);              // reset
	idle_no:
	if (Command(0x41,0,0,0xFF) !=0)
	goto idle_no;                        //idle = L?
	SPCR &= ~(_BV(SPR0));                //fosc/4
}


int write(void) {
	
	int i;
	uint8_t wbr;
	
	//Set write mode 512 bytes
	if (Command(0x58,0,25,0xFF) !=0) {
		//Determine value of the response byte 0 = no errors
		return 1;
		//return value 1 = error
	}
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFE);
	//recommended by posting a terminator sequence [2]
	
	//write data from chars [512] tab
	uint16_t ix;
	char r1 =  Command(0x58,0,25,0xFF);
	for (ix = 0; ix < 50000; ix++) {
		if (r1 == (char)0x00) break;
		r1 = SPI_sendchar(0xFF);
	}
	if (r1 != (char)0x00) {
		return 1;
		//return value 1 = error
	}
	//recommended by the control loop [2]
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFF);
	wbr = SPI_sendchar(0xFF);
	//write block response and testing error
	wbr &= 0x1F;
	//zeroing top three indeterminate bits 0b.0001.1111
	if (wbr != 0x05) { // 0x05 = 0b.0000.0101
	//write error or CRC error
	return 1;
}
while(SPI_sendchar(0xFF) != (char)0xFF);
//wait for the completion of a write operation to the card
return 0;

}


int read(void) {
	
	int i;
	uint16_t ix;
	char r1 =  Command(0x51,0,25,0xFF);
	for (ix = 0; ix < 50000; ix++) {
		if (r1 == (char)0x00) break;
		r1 = SPI_sendchar(0xFF);
	}
	if (r1 != (char)0x00) {
		return 1;
	}
	//read from the card will start after the framework
	while(SPI_sendchar(0xFF) != (char)0xFE);
	for(i=0; i < 25; i++) {
		while(!(SPSR & (1<<SPIF)));
		chars[i] = SPDR;
		SPDR = SPI_sendchar(0xFF);
	}
	SPI_sendchar(0xFF);
	SPI_sendchar(0xFF);
	return 0;
}


int main(void)
{
	DDRD=0XFF; // for LCD
	
	_delay_us(10);
	
	lcd_init(LCD_DISP_ON); 

	stdout = stdin = &lcd_str;
	
	ini_SPI();
	ini_SD();
	
    
	lcd_clrscr();
	
	lcd_gotoxy(4,0);
	lcd_puts("SD Card"); 
	
    while(1){
		
	   
    }
}