add first sreg code

This commit is contained in:
David Voswinkel 2009-06-23 23:56:02 +02:00
parent f3cc60e77b
commit c4631f2991
7 changed files with 384 additions and 500 deletions

View File

@ -179,13 +179,64 @@ uint8_t usbFunctionRead(uint8_t * data, uint8_t len)
int main(void)
{
uint8_t i;
wdt_enable(WDTO_1S);
uint32_t addr;
wdt_enable(WDTO_8S);
uart_init();
stdout = &uart_stdout;
sram_init();
printf("SRAM Init\n");
spi_init();
printf("SPI Init\n");
system_init();
printf("Sytem Init\n");
avr_bus_active();
DDRB|= (1 << PB1);
PORTB|= (1 << PB1);
while(1)
wdt_reset();
#if 0
avr_bus_active();
printf("set sreg 0xff55aa\n");
sreg_set(0xff55aa);
counter_load();
sram_write(0xff55aa,0x55);
while(1)
wdt_reset();
addr = 0x3fffff;
sreg_set(0x00000);
counter_load();
wdt_reset();
while(addr--){
counter_up();
}
printf("done\n");
while(1){
wdt_reset();
i = 10;
while(i--)
_delay_ms(100);
//printf("counter up\n");
}
#endif
avr_bus_active();
#if 1
addr = 0x00;
i = 0;
while (addr++ <= 0xff){
sram_write(addr,i++);
}
#endif
addr = 0x00;
while (addr++ <= 0xff){
printf("read addr=0x%08lx %x\n",addr,sram_read(addr));
}
while(1);
usbInit();
printf("USB Init\n");
usbDeviceDisconnect(); /* enforce re-enumeration, do this while
@ -195,8 +246,15 @@ int main(void)
i = 10;
while (--i) { /* fake USB disconnect for > 250 ms */
wdt_reset();
_delay_ms(10);
led_on();
_delay_ms(35);
led_off();
_delay_ms(65);
}
led_on();
usbDeviceConnect();
printf("USB connect\n");
sei();

View File

@ -1,67 +1,110 @@
#include <stdlib.h>
#include <stdint.h>
#include <avr/io.h>
#include <avr/wdt.h>
#include <util/delay.h> /* for _delay_ms() */
#include "sram.h"
#include "uart.h"
#include "debug.h"
void spi_init(void)
{
/*
* Set MOSI and SCK output, all others input
*/
SPI_DIR |= ((1 << S_MOSI) | (1 << S_SCK) | (1 << S_LATCH));
SPI_DIR &= ~(1 << S_MISO);
SPI_PORT |= (1 << S_MISO);
/*
* Enable SPI, Master
*/
SPCR = ((1 << SPE) | (1 << MSTR));
}
void spi_master_transmit(unsigned char cData)
void system_init(void)
{
/*
* Start transmission
*/
SPDR = cData;
/*-------------------------------------------------*/
DDRA = 0x00;
PORTA = 0x00;
/*-------------------------------------------------*/
DDRC |= ( (1 << AVR_ADDR_LATCH_PIN)
| (1 << AVR_ADDR_SCK_PIN)
| (1 << AVR_ADDR_SER_PIN)
| (1 << AVR_ADDR_LOAD_PIN)
| (1 << AVR_ADDR_DOWN_PIN)
| (1 << AVR_ADDR_UP_PIN));
DDRC &= ~ (1 << SNES_WR_PIN);
PORTC &= ~((1 << AVR_ADDR_LATCH_PIN)
| (1 << AVR_ADDR_SCK_PIN));
PORTC |= ( (1 << AVR_ADDR_DOWN_PIN)
| (1 << AVR_ADDR_UP_PIN)
| (1 << AVR_ADDR_LOAD_PIN)
| (1 << SNES_WR_PIN));
/*-------------------------------------------------*/
DDRB |= ( (1 << AVR_RD_PIN)
| (1 << AVR_WR_PIN)
| (1 << AVR_CS_PIN)
| (1 << SNES_IRQ_PIN));
PORTB |= ( (1 << AVR_RD_PIN)
| (1 << AVR_WR_PIN)
| (1 << AVR_CS_PIN)
| (1 << SNES_IRQ_PIN));
/*-------------------------------------------------*/
DDRD |= ( (1 << AVR_SNES_SW_PIN)
| (1 << HI_LOROM_SW_PIN)
| (1 << SNES_WR_EN_PIN));
/*
* Wait for transmission complete
*/
while (!(SPSR & (1 << SPIF)));
PORTD |= (1 << HI_LOROM_SW_PIN);
PORTD &= ~((1 << AVR_SNES_SW_PIN)
| (1 << SNES_WR_EN_PIN));
/*-------------------------------------------------*/
}
void sreg_set(uint32_t addr)
{
uint8_t i = 24;
printf("sreg addr=0x%08lx ",addr);
while(i--) {
if ((addr & ( 1L << i))){
printf("1");
AVR_ADDR_SER_PORT |= ( 1 << AVR_ADDR_SER_PIN);
} else {
AVR_ADDR_SER_PORT &= ~( 1 << AVR_ADDR_SER_PIN);
printf("0");
}
AVR_ADDR_SCK_PORT |= (1 << AVR_ADDR_SCK_PIN);
AVR_ADDR_SCK_PORT &= ~(1 << AVR_ADDR_SCK_PIN);
}
printf("\n");
AVR_ADDR_LATCH_PORT |= (1 << AVR_ADDR_LATCH_PIN);
AVR_ADDR_LATCH_PORT &= ~(1 << AVR_ADDR_LATCH_PIN);
counter_load();
}
void sram_set_addr(uint32_t addr)
{
spi_master_transmit((uint8_t) (addr >> 16));
spi_master_transmit((uint8_t) (addr >> 8));
spi_master_transmit((uint8_t) (addr >> 0));
LATCH_PORT |= (1 << S_LATCH);
LATCH_PORT &= ~(1 << S_LATCH);
}
uint8_t sram_read(uint32_t addr)
{
uint8_t byte;
RAM_DIR = 0x00;
RAM_PORT = 0xff;
CTRL_PORT |= (1 << R_RD);
CTRL_PORT |= (1 << R_WR);
spi_master_transmit((uint8_t) (addr >> 16));
spi_master_transmit((uint8_t) (addr >> 8));
spi_master_transmit((uint8_t) (addr >> 0));
LATCH_PORT |= (1 << S_LATCH);
LATCH_PORT &= ~(1 << S_LATCH);
CTRL_PORT &= ~(1 << R_RD);
avr_data_in();
AVR_WR_PORT |= (1 << AVR_WR_PIN);
AVR_RD_PORT |= (1 << AVR_RD_PIN);
AVR_CS_PORT &= ~(1 << AVR_CS_PIN);
_delay_ms(1);
sreg_set(addr);
AVR_RD_PORT &= ~(1 << AVR_RD_PIN);
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
@ -70,65 +113,39 @@ uint8_t sram_read(uint32_t addr)
asm volatile ("nop");
asm volatile ("nop");
asm volatile ("nop");
byte = RAM_REG;
CTRL_PORT |= (1 << R_RD);
RAM_DIR = 0x00;
RAM_PORT = 0x00;
byte = AVR_DATA_PIN;
#if 0
printf("read %x\n",byte);
while(1)
wdt_reset();
#endif
AVR_RD_PORT |= (1 << AVR_RD_PIN);
AVR_CS_PORT |= (1 << AVR_CS_PIN);
avr_data_in();
return byte;
}
void sram_write(uint32_t addr, uint8_t data)
{
RAM_DIR = 0xff;
CTRL_PORT |= (1 << R_RD);
CTRL_PORT |= (1 << R_WR);
spi_master_transmit((uint8_t) (addr >> 16));
spi_master_transmit((uint8_t) (addr >> 8));
spi_master_transmit((uint8_t) (addr >> 0));
LATCH_PORT |= (1 << S_LATCH);
LATCH_PORT &= ~(1 << S_LATCH);
CTRL_PORT &= ~(1 << R_WR);
RAM_PORT = data;
CTRL_PORT |= (1 << R_WR);
RAM_DIR = 0x00;
RAM_PORT = 0x00;
}
void sram_init(void)
{
RAM_DIR = 0x00;
RAM_PORT = 0x00;
CTRL_DIR |= ((1 << R_WR) | (1 << R_RD));
CTRL_PORT |= (1 << R_RD);
CTRL_PORT |= (1 << R_WR);
LED_PORT |= (1 << D_LED0);
}
void sram_snes_mode01(void)
{
CTRL_PORT |= (1 << R_WR);
CTRL_PORT &= ~(1 << R_RD);
}
void sram_snes_mode02(void)
{
CTRL_DIR |= (1 << R_WR);
CTRL_PORT |= (1 << R_WR);
// CTRL_PORT &= ~(1<<R_RD);
CTRL_DIR &= ~(1 << R_RD);
CTRL_PORT &= ~(1 << R_RD);
avr_data_out();
AVR_CS_PORT &= ~(1 << AVR_CS_PIN);
AVR_WR_PORT |= (1 << AVR_WR_PIN);
AVR_RD_PORT |= (1 << AVR_RD_PIN);
sreg_set(addr);
AVR_WR_PORT &= ~(1 << AVR_WR_PIN);
AVR_DATA_PORT = data;
AVR_WR_PORT |= (1 << AVR_WR_PIN);
AVR_CS_PORT |= (1 << AVR_CS_PIN);
avr_data_in();
}

View File

@ -3,48 +3,132 @@
#include <avr/io.h>
//SREG defines
#define S_MOSI PB5
#define S_MISO PB6
#define S_SCK PB7
#define S_LATCH PB4
//DEBUG defines
#define D_LED0 PD6
//SRAM defines
#define R_WR PB1
#define R_RD PB0
#define RAM_PORT PORTA
#define RAM_DIR DDRA
#define RAM_REG PINA
#define CTRL_PORT PORTB
#define CTRL_DIR DDRB
#define LATCH_PORT PORTB
#define LATCH_DIR DDRB
#define SPI_PORT PORTB
#define SPI_DIR DDRB
#define LED_PORT PORTD
#define LED_DIR DDRD
#define ROMSIZE 4
#define BLOCKS (ROMSIZE << 8)
#define MEMSIZE 0x80000
void spi_init(void);
void spi_master_transmit(unsigned char cData);
void sram_set_addr(uint32_t addr);
#define LED_PORT PORTC
#define LED_DIR DDRC
#define LED_PIN PC7
#define led_on() ((LED_PORT &=~ (1 << LED_PIN)),\
(LED_DIR &=~ (1 << LED_PIN)))
#define led_off() ((LED_PORT &=~ (1 << LED_PIN)),\
(LED_DIR |= (1 << LED_PIN)))
/* Port C*/
#define AVR_ADDR_PORT PORTC
#define AVR_ADDR_DIR DDRC
#define AVR_ADDR_LATCH_PORT PORTC
#define AVR_ADDR_LATCH_DIR DDRC
#define AVR_ADDR_LATCH_PIN PC6
#define AVR_ADDR_SCK_PORT PORTC
#define AVR_ADDR_SCK_DIR DDRC
#define AVR_ADDR_SCK_PIN PC5
#define AVR_ADDR_SER_PORT PORTC
#define AVR_ADDR_SER_DIR DDRC
#define AVR_ADDR_SER_PIN PC4
#define AVR_ADDR_LOAD_PORT PORTC
#define AVR_ADDR_LOAD_DIR DDRC
#define AVR_ADDR_LOAD_PIN PC2
#define counter_load() ((AVR_ADDR_LOAD_PORT &= ~(1 << AVR_ADDR_LOAD_PIN)),\
(AVR_ADDR_LOAD_PORT |= (1 << AVR_ADDR_LOAD_PIN)))
#define AVR_ADDR_DOWN_PORT PORTC
#define AVR_ADDR_DOWN_DIR DDRC
#define AVR_ADDR_DOWN_PIN PC1
#define counter_down() ((AVR_ADDR_DOWN_PORT &= ~(1 << AVR_ADDR_DOWN_PIN)),\
(AVR_ADDR_DOWN_PORT |= (1 << AVR_ADDR_DOWN_PIN)))
#define AVR_ADDR_UP_PORT PORTC
#define AVR_ADDR_UP_DIR DDRC
#define AVR_ADDR_UP_PIN PC0
#define counter_up() ((AVR_ADDR_UP_PORT &= ~(1 << AVR_ADDR_UP_PIN)),\
(AVR_ADDR_UP_PORT |= (1 << AVR_ADDR_UP_PIN)))
#define SNES_WR_PORT PORTC
#define SNES_WR_DIR DDRC
#define SNES_WR_PIN PC3
/* Port B*/
#define AVR_PORT PORTB
#define AVR_DIR DDRB
#define AVR_RD_PORT PORTB
#define AVR_RD_DIR DDRB
#define AVR_RD_PIN PB2
#define AVR_WR_PORT PORTB
#define AVR_WR_DIR DDRB
#define AVR_WR_PIN PB1
#define AVR_CS_PORT PORTB
#define AVR_CS_DIR DDRB
#define AVR_CS_PIN PB0
#define SNES_IRQ_PORT PORTB
#define SNES_IRQ_DIR DDRB
#define SNES_IRQ_PIN PB3
#define snes_irq_off() (SNES_IRQ_PORT |= (1 << SNES_IRQ_PIN))
#define snes_irq_on() (SNES_IRQ_PORT &= ~(1 << SNES_IRQ_PIN))
/* Port A*/
#define AVR_DATA_PORT PORTA
#define AVR_DATA_DIR DDRA
#define AVR_DATA_PIN PINA
#define avr_data_in() ((AVR_DATA_DIR = 0x00),\
(AVR_DATA_PORT = 0x00))
#define avr_data_out() (AVR_DATA_DIR = 0xff)
/* Port D*/
#define AVR_SNES_PORT PORTD
#define AVR_SNES_DIR DDRD
#define AVR_SNES_SW_PORT PORTD
#define AVR_SNES_SW_DIR DDRD
#define AVR_SNES_SW_PIN PD5
#define avr_bus_active() ((AVR_SNES_SW_PORT &= ~(1 << AVR_SNES_SW_PIN)),\
(HI_LOROM_SW_PORT |= (1 << HI_LOROM_SW_PIN)))
#define snes_bus_active() (AVR_SNES_SW_PORT |= (1 << AVR_SNES_SW_PIN))
#define HI_LOROM_SW_PORT PORTD
#define HI_LOROM_SW_DIR DDRD
#define HI_LOROM_SW_PIN PD6
#define snes_hirom() (HI_LOROM_SW_PORT &= ~(1 << HI_LOROM_SW_PIN))
#define snes_lorom() (HI_LOROM_SW_PORT |= (1 << HI_LOROM_SW_PIN))
#define SNES_WR_EN_PORT PORTD
#define SNES_WR_EN_DIR DDRD
#define SNES_WR_EN_PIN PD7
#define snes_wr_disable() (SNES_WR_EN_PORT &= ~(1 << SNES_WR_EN_PIN))
#define snes_wr_enable() (SNES_WR_EN_PORT |= (1 << SNES_WR_EN_PIN))
void system_init(void);
void sreg_set(uint32_t addr);
uint8_t sram_read(uint32_t addr);
void sram_write(uint32_t addr, uint8_t data);
void sram_init(void);
void sram_snes_mode01(void);
void sram_snes_mode02(void);
void sram_clear(uint32_t addr, uint32_t len);
void sram_copy(uint32_t addr,uint8_t *src, uint32_t len);
void sram_read_buffer(uint32_t addr,uint8_t *dst, uint32_t len);

View File

@ -42,7 +42,7 @@ MCU = atmega644
# Main Oscillator Frequency
# This is only used to define F_OSC in all assembler and c-sources.
F_OSC = 8000000
F_OSC = 20000000
# Output format. (can be srec, ihex, binary)
FORMAT = ihex
@ -177,11 +177,90 @@ LDFLAGS += $(PRINTF_LIB) $(SCANF_LIB) $(MATH_LIB)
#
# Type: avrdude -c ?
# to get a full listing.
##############################################################################
# Fuse values for particular devices
##############################################################################
# If your device is not listed here, go to
# http://palmavr.sourceforge.net/cgi-bin/fc.cgi
# and choose options for external crystal clock and no clock divider
#
AVRDUDE_PROGRAMMER = stk500v2
################################## ATMega8 ##################################
# ATMega8 FUSE_L (Fuse low byte):
# 0x9f = 1 0 0 1 1 1 1 1
# ^ ^ \ / \--+--/
# | | | +------- CKSEL 3..0 (external >8M crystal)
# | | +--------------- SUT 1..0 (crystal osc, BOD enabled)
# | +------------------ BODEN (BrownOut Detector enabled)
# +-------------------- BODLEVEL (2.7V)
# ATMega8 FUSE_H (Fuse high byte):
# 0xc9 = 1 1 0 0 1 0 0 1 <-- BOOTRST (boot reset vector at 0x0000)
# ^ ^ ^ ^ ^ ^ ^------ BOOTSZ0
# | | | | | +-------- BOOTSZ1
# | | | | + --------- EESAVE (don't preserve EEPROM over chip erase)
# | | | +-------------- CKOPT (full output swing)
# | | +---------------- SPIEN (allow serial programming)
# | +------------------ WDTON (WDT not always on)
# +-------------------- RSTDISBL (reset pin is enabled)
#
############################## ATMega48/88/168 ##############################
# ATMega*8 FUSE_L (Fuse low byte):
# 0xdf = 1 1 0 1 1 1 1 1
# ^ ^ \ / \--+--/
# | | | +------- CKSEL 3..0 (external >8M crystal)
# | | +--------------- SUT 1..0 (crystal osc, BOD enabled)
# | +------------------ CKOUT (if 0: Clock output enabled)
# +-------------------- CKDIV8 (if 0: divide by 8)
# ATMega*8 FUSE_H (Fuse high byte):
# 0xde = 1 1 0 1 1 1 1 0
# ^ ^ ^ ^ ^ \-+-/
# | | | | | +------ BODLEVEL 0..2 (110 = 1.8 V)
# | | | | + --------- EESAVE (preserve EEPROM over chip erase)
# | | | +-------------- WDTON (if 0: watchdog always on)
# | | +---------------- SPIEN (allow serial programming)
# | +------------------ DWEN (debug wire enable)
# +-------------------- RSTDISBL (reset pin is enabled)
#
############################## ATTiny25/45/85 ###############################
# ATMega*5 FUSE_L (Fuse low byte):
# 0xef = 1 1 1 0 1 1 1 1
# ^ ^ \+/ \--+--/
# | | | +------- CKSEL 3..0 (clock selection -> crystal @ 12 MHz)
# | | +--------------- SUT 1..0 (BOD enabled, fast rising power)
# | +------------------ CKOUT (clock output on CKOUT pin -> disabled)
# +-------------------- CKDIV8 (divide clock by 8 -> don't divide)
# ATMega*5 FUSE_H (Fuse high byte):
# 0xdd = 1 1 0 1 1 1 0 1
# ^ ^ ^ ^ ^ \-+-/
# | | | | | +------ BODLEVEL 2..0 (brownout trigger level -> 2.7V)
# | | | | +---------- EESAVE (preserve EEPROM on Chip Erase -> not preserved)
# | | | +-------------- WDTON (watchdog timer always on -> disable)
# | | +---------------- SPIEN (enable serial programming -> enabled)
# | +------------------ DWEN (debug wire enable)
# +-------------------- RSTDISBL (disable external reset -> enabled)
#
################################ ATTiny2313 #################################
# ATTiny2313 FUSE_L (Fuse low byte):
# 0xef = 1 1 1 0 1 1 1 1
# ^ ^ \+/ \--+--/
# | | | +------- CKSEL 3..0 (clock selection -> crystal @ 12 MHz)
# | | +--------------- SUT 1..0 (BOD enabled, fast rising power)
# | +------------------ CKOUT (clock output on CKOUT pin -> disabled)
# +-------------------- CKDIV8 (divide clock by 8 -> don't divide)
# ATTiny2313 FUSE_H (Fuse high byte):
# 0xdb = 1 1 0 1 1 0 1 1
# ^ ^ ^ ^ \-+-/ ^
# | | | | | +---- RSTDISBL (disable external reset -> enabled)
# | | | | +-------- BODLEVEL 2..0 (brownout trigger level -> 2.7V)
# | | | +-------------- WDTON (watchdog timer always on -> disable)
# | | +---------------- SPIEN (enable serial programming -> enabled)
# | +------------------ EESAVE (preserve EEPROM on Chip Erase -> not preserved)
# +-------------------- DWEN (debug wire enable)
#
AVRDUDE_PROGRAMMER = usbasp
# com1 = serial port. Use lpt1 to connect to parallel port.
AVRDUDE_PORT = /dev/ttyUSB0 # programmer connected to serial device
AVRDUDE_PORT = # programmer connected to serial device
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
@ -201,7 +280,7 @@ AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
# to submit bug reports.
#AVRDUDE_VERBOSE = -v -v
AVRDUDE_FLAGS = -p $(MCU) -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS = -p $(MCU) -c $(AVRDUDE_PROGRAMMER)
AVRDUDE_FLAGS += $(AVRDUDE_NO_VERIFY)
AVRDUDE_FLAGS += $(AVRDUDE_VERBOSE)
AVRDUDE_FLAGS += $(AVRDUDE_ERASE_COUNTER)

View File

@ -102,7 +102,8 @@ void SRAM_Write(uint32_t addr, uint8_t data)
int main(void)
{
DDRB|= (1 << PB1);
PORTB|= (1 << PB1);
while(1);
return(0);
}

View File

@ -1,298 +0,0 @@
/*#######################################################################################
Connect AVR to MMC/SD
Copyright (C) 2004 Ulrich Radig
Bei Fragen und Verbesserungen wendet euch per EMail an
mail@ulrichradig.de
oder im Forum meiner Web Page : www.ulrichradig.de
Dieses Programm ist freie Software. Sie können es unter den Bedingungen der
GNU General Public License, wie von der Free Software Foundation veröffentlicht,
weitergeben und/oder modifizieren, entweder gemäß Version 2 der Lizenz oder
(nach Ihrer Option) jeder späteren Version.
Die Veröffentlichung dieses Programms erfolgt in der Hoffnung,
daß es Ihnen von Nutzen sein wird, aber OHNE IRGENDEINE GARANTIE,
sogar ohne die implizite Garantie der MARKTREIFE oder der VERWENDBARKEIT
FÜR EINEN BESTIMMTEN ZWECK. Details finden Sie in der GNU General Public License.
Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
Programm erhalten haben.
Falls nicht, schreiben Sie an die Free Software Foundation,
Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
#######################################################################################*/
#include "mmc.h"
#include <util/delay.h>
//############################################################################
//Routine zur Initialisierung der MMC/SD-Karte (SPI-MODE)
unsigned char mmc_init ()
//############################################################################
{
unsigned int Timeout = 0,i;
//Konfiguration des Ports an der die MMC/SD-Karte angeschlossen wurde
DDRC |= ( (1<<MMC_DO) | (1<<MMC_CS) | (1<<MMC_CLK) );
DDRC &= ~(1<<MMC_DI);
PORTC |= ( (1<<MMC_DO) | (1<<MMC_DI) | (1<<MMC_CS) );
//Wartet eine kurze Zeit
_delay_ms(10);
//Initialisiere MMC/SD-Karte in den SPI-Mode
for(i=0; i<250; i++)
{
PORTC ^= (1<<MMC_CLK);
_delay_us(4);
}
PORTC &= ~(1<<MMC_CLK);
_delay_us(10);
PORTC &= ~(1<<MMC_CS);
_delay_us(3);
//Sendet Commando CMD0 an MMC/SD-Karte
unsigned char CMD[] = {0x40,0x00,0x00,0x00,0x00,0x95};
while(mmc_write_command (CMD) !=1)
{
if (Timeout++ > 20)
{
MMC_Disable();
return(1); //Abbruch bei Commando1 (Return Code1)
}
}
//Sendet Commando CMD1 an MMC/SD-Karte
Timeout = 0;
CMD[0] = 0x41;//Commando 1
CMD[5] = 0xFF;
while( mmc_write_command (CMD) !=0)
{
if (Timeout++ > 800)
{
MMC_Disable();
return(9); //Abbruch bei Commando2 (Return Code2)
}
}
return(0);
}
//############################################################################
//Sendet ein Commando an die MMC/SD-Karte
unsigned char mmc_write_command (unsigned char *cmd)
//############################################################################
{
unsigned char tmp = 0xff;
unsigned int Timeout = 0;
//sendet 6 Byte Commando
for (unsigned char a = 0;a<0x06;a++) //sendet 6 Byte Commando zur MMC/SD-Karte
{
mmc_write_byte(*cmd++);
}
//Wartet auf ein gültige Antwort von der MMC/SD-Karte
while (tmp == 0xff)
{
tmp = mmc_read_byte();
if (Timeout++ > 50)
{
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)
//############################################################################
{
uint8_t Byte=0,j;
for(j=0; j<8; j++){
Byte = (Byte<<1);
PORTC |= (1<<MMC_CLK);
_delay_us(4);
if(PINC & (1<<MMC_DI)){
Byte |= 1;
}
else{
Byte &= ~1;
}
PORTC &= ~(1<<MMC_CLK);
_delay_us(4);
}
return (Byte);
}
//############################################################################
//Routine zum Senden eines Bytes zur MMC-Karte
void mmc_write_byte (unsigned char Byte)
//############################################################################
{
uint8_t i;
for(i=0; i<8; i++){
if(Byte & 0x80){
PORTC |= (1<<MMC_DO);
}
else{
PORTC &= ~(1<<MMC_DO);
}
Byte = (Byte<<1);
PORTC |= (1<<MMC_CLK);
_delay_us(4);
PORTC &= ~(1<<MMC_CLK);
_delay_us(4);
}
PORTC |= (1<<MMC_DO);
}
//############################################################################
//Routine zum schreiben eines Blocks(512Byte) auf die MMC/SD-Karte
unsigned char mmc_write_sector (unsigned long addr,unsigned char *Buffer)
//############################################################################
{
unsigned char tmp;
//Commando 24 zum schreiben eines Blocks auf die MMC/SD - Karte
unsigned char cmd[] = {0x58,0x00,0x00,0x00,0x00,0xFF};
/*Die Adressierung der MMC/SD-Karte wird in Bytes angegeben,
addr wird von Blocks zu Bytes umgerechnet danach werden
diese in das Commando eingefügt*/
addr = addr << 9; //addr = addr * 512
cmd[1] = ((addr & 0xFF000000) >>24 );
cmd[2] = ((addr & 0x00FF0000) >>16 );
cmd[3] = ((addr & 0x0000FF00) >>8 );
//Sendet Commando cmd24 an MMC/SD-Karte (Write 1 Block/512 Bytes)
tmp = mmc_write_command (cmd);
if (tmp != 0)
{
return(tmp);
}
//Wartet einen Moment und sendet einen Clock an die MMC/SD-Karte
for (unsigned char a=0;a<100;a++)
{
mmc_read_byte();
}
//Sendet Start Byte an MMC/SD-Karte
mmc_write_byte(0xFE);
//Schreiben des Bolcks (512Bytes) auf MMC/SD-Karte
for (unsigned int a=0;a<512;a++)
{
mmc_write_byte(*Buffer++);
}
//CRC-Byte schreiben
mmc_write_byte(0xFF); //Schreibt Dummy CRC
mmc_write_byte(0xFF); //CRC Code wird nicht benutzt
//Fehler beim schreiben? (Data Response XXX00101 = OK)
if((mmc_read_byte()&0x1F) != 0x05) return(1);
//Wartet auf MMC/SD-Karte Bussy
while (mmc_read_byte() != 0xff){};
return(0);
}
//############################################################################
//Routine zum lesen des CID Registers von der MMC/SD-Karte (16Bytes)
void mmc_read_block(unsigned char *cmd,unsigned char *Buffer,unsigned int Bytes)
//############################################################################
{
//Sendet Commando cmd an MMC/SD-Karte
if (mmc_write_command (cmd) != 0)
{
return;
}
//Wartet auf Start Byte von der MMC/SD-Karte (FEh/Start Byte)
while (mmc_read_byte() != 0xfe){};
//Lesen des Bolcks (normal 512Bytes) von MMC/SD-Karte
for (unsigned int a=0;a<Bytes;a++)
{
*Buffer++ = mmc_read_byte();
}
//CRC-Byte auslesen
mmc_read_byte();//CRC - Byte wird nicht ausgewertet
mmc_read_byte();//CRC - Byte wird nicht ausgewertet
return;
}
//############################################################################
//Routine zum lesen eines Blocks(512Byte) von der MMC/SD-Karte
unsigned char mmc_read_sector (unsigned long addr,unsigned char *Buffer)
//############################################################################
{
//Commando 16 zum lesen eines Blocks von der MMC/SD - Karte
unsigned char cmd[] = {0x51,0x00,0x00,0x00,0x00,0xFF};
/*Die Adressierung der MMC/SD-Karte wird in Bytes angegeben,
addr wird von Blocks zu Bytes umgerechnet danach werden
diese in das Commando eingefügt*/
addr = addr << 9; //addr = addr * 512
cmd[1] = ((addr & 0xFF000000) >>24 );
cmd[2] = ((addr & 0x00FF0000) >>16 );
cmd[3] = ((addr & 0x0000FF00) >>8 );
mmc_read_block(cmd,Buffer,512);
return(0);
}
//############################################################################
//Routine zum lesen des CID Registers von der MMC/SD-Karte (16Bytes)
unsigned char mmc_read_cid (unsigned char *Buffer)
//############################################################################
{
//Commando zum lesen des CID Registers
unsigned char cmd[] = {0x4A,0x00,0x00,0x00,0x00,0xFF};
mmc_read_block(cmd,Buffer,16);
return(0);
}
//############################################################################
//Routine zum lesen des CSD Registers von der MMC/SD-Karte (16Bytes)
unsigned char mmc_read_csd (unsigned char *Buffer)
//############################################################################
{
//Commando zum lesen des CSD Registers
unsigned char cmd[] = {0x49,0x00,0x00,0x00,0x00,0xFF};
mmc_read_block(cmd,Buffer,16);
return(0);
}

View File

@ -1,57 +0,0 @@
/*#######################################################################################
Connect ARM to MMC/SD
Copyright (C) 2004 Ulrich Radig
#######################################################################################*/
#ifndef _MMC_H_
#define _MMC_H_
#include <avr/io.h>
//#define SPI_Mode 1 //1 = Hardware SPI | 0 = Software SPI
#define SPI_Mode 0
#define MMC_Write PORTC //Port an der die MMC/SD-Karte angeschlossen ist also des SPI
#define MMC_Read PINC
#define MMC_Direction_REG DDRC
#if defined (__AVR_ATmega8__)
#define MMC_CS PC0
#define MMC_DO PC1
#define MMC_DI PC2
#define MMC_CLK PC3
#define SPI_SS 4 //Nicht Benutz muß aber definiert werden
#endif
//Prototypes
extern unsigned char mmc_read_byte(void);
extern void mmc_write_byte(unsigned char);
extern void mmc_read_block(unsigned char *,unsigned char *,unsigned in);
extern unsigned char mmc_init(void);
extern unsigned char mmc_read_sector (unsigned long,unsigned char *);
extern unsigned char mmc_write_sector (unsigned long,unsigned char *);
extern unsigned char mmc_write_command (unsigned char *);
extern unsigned char mmc_read_csd (unsigned char *);
extern unsigned char mmc_read_cid (unsigned char *);
//set MMC_Chip_Select to high (MMC/SD-Karte Inaktiv)
#define MMC_Disable() MMC_Write|= (1<<MMC_CS);
//set MMC_Chip_Select to low (MMC/SD-Karte Aktiv)
#define MMC_Enable() MMC_Write&=~(1<<MMC_CS);
#define nop() __asm__ __volatile__ ("nop" ::)
#endif //_MMC_H_