o add ff lib
o add ff lib samples o add ff linux sample
This commit is contained in:
52
tools/ffsample/avr/Makefile_ata
Normal file
52
tools/ffsample/avr/Makefile_ata
Normal file
@@ -0,0 +1,52 @@
|
||||
TARGET = avr_ata
|
||||
CSRC = main.c uart.c ff.c ata.c rtc.c
|
||||
ASRC = xitoa.S
|
||||
MCU_TARGET = atmega64
|
||||
OPTIMIZE = -Os -mcall-prologues
|
||||
DEFS =
|
||||
LIBS =
|
||||
DEBUG = dwarf-2
|
||||
|
||||
CC = avr-gcc
|
||||
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
|
||||
ALL_ASFLAGS = -mmcu=$(MCU_TARGET) -I. -x assembler-with-cpp $(ASFLAGS)
|
||||
CFLAGS = -g$(DEBUG) -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
|
||||
LDFLAGS = -Wl,-Map,$(TARGET).map
|
||||
OBJ = $(CSRC:.c=.o) $(ASRC:.S=.o)
|
||||
|
||||
OBJCOPY = avr-objcopy
|
||||
OBJDUMP = avr-objdump
|
||||
SIZE = avr-size
|
||||
|
||||
|
||||
|
||||
all: $(TARGET).elf lst text size
|
||||
|
||||
$(TARGET).elf: $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
|
||||
|
||||
|
||||
clean:
|
||||
rm -rf *.o $(TARGET).elf *.eps *.bak *.a
|
||||
rm -rf *.lst *.map $(EXTRA_CLEAN_FILES)
|
||||
rm -rf $(TARGET).hex
|
||||
|
||||
size: $(TARGET).elf
|
||||
$(SIZE) -C --mcu=$(MCU_TARGET) $(TARGET).elf
|
||||
|
||||
lst: $(TARGET).lst
|
||||
%.lst: %.elf
|
||||
$(OBJDUMP) -h -S $< > $@
|
||||
|
||||
%.o : %.S
|
||||
$(CC) -c $(ALL_ASFLAGS) $< -o $@
|
||||
|
||||
|
||||
|
||||
text: hex
|
||||
hex: $(TARGET).hex
|
||||
|
||||
%.hex: %.elf
|
||||
$(OBJCOPY) -j .text -j .data -O ihex $< $@
|
||||
|
||||
|
||||
52
tools/ffsample/avr/Makefile_cfc
Normal file
52
tools/ffsample/avr/Makefile_cfc
Normal file
@@ -0,0 +1,52 @@
|
||||
TARGET = avr_cfc
|
||||
CSRC = main.c uart.c ff.c cfc.c rtc.c
|
||||
ASRC = xitoa.S
|
||||
MCU_TARGET = atmega64
|
||||
OPTIMIZE = -Os -mcall-prologues
|
||||
DEFS =
|
||||
LIBS =
|
||||
DEBUG = dwarf-2
|
||||
|
||||
CC = avr-gcc
|
||||
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
|
||||
ALL_ASFLAGS = -mmcu=$(MCU_TARGET) -I. -x assembler-with-cpp $(ASFLAGS)
|
||||
CFLAGS = -g$(DEBUG) -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
|
||||
LDFLAGS = -Wl,-Map,$(TARGET).map
|
||||
OBJ = $(CSRC:.c=.o) $(ASRC:.S=.o)
|
||||
|
||||
OBJCOPY = avr-objcopy
|
||||
OBJDUMP = avr-objdump
|
||||
SIZE = avr-size
|
||||
|
||||
|
||||
|
||||
all: $(TARGET).elf lst text size
|
||||
|
||||
$(TARGET).elf: $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
|
||||
|
||||
|
||||
clean:
|
||||
rm -rf *.o $(TARGET).elf *.eps *.bak *.a
|
||||
rm -rf *.lst *.map $(EXTRA_CLEAN_FILES)
|
||||
rm -rf $(TARGET).hex
|
||||
|
||||
size: $(TARGET).elf
|
||||
$(SIZE) -C --mcu=$(MCU_TARGET) $(TARGET).elf
|
||||
|
||||
lst: $(TARGET).lst
|
||||
%.lst: %.elf
|
||||
$(OBJDUMP) -h -S $< > $@
|
||||
|
||||
%.o : %.S
|
||||
$(CC) -c $(ALL_ASFLAGS) $< -o $@
|
||||
|
||||
|
||||
|
||||
text: hex
|
||||
hex: $(TARGET).hex
|
||||
|
||||
%.hex: %.elf
|
||||
$(OBJCOPY) -j .text -j .data -O ihex $< $@
|
||||
|
||||
|
||||
52
tools/ffsample/avr/Makefile_cfmm
Normal file
52
tools/ffsample/avr/Makefile_cfmm
Normal file
@@ -0,0 +1,52 @@
|
||||
TARGET = avr_cfmm
|
||||
CSRC = main.c uart.c ff.c cfmm.c rtc.c
|
||||
ASRC = xitoa.S
|
||||
MCU_TARGET = atmega64
|
||||
OPTIMIZE = -Os -mcall-prologues
|
||||
DEFS =
|
||||
LIBS =
|
||||
DEBUG = dwarf-2
|
||||
|
||||
CC = avr-gcc
|
||||
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
|
||||
ALL_ASFLAGS = -mmcu=$(MCU_TARGET) -I. -x assembler-with-cpp $(ASFLAGS)
|
||||
CFLAGS = -g$(DEBUG) -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
|
||||
LDFLAGS = -Wl,-Map,$(TARGET).map
|
||||
OBJ = $(CSRC:.c=.o) $(ASRC:.S=.o)
|
||||
|
||||
OBJCOPY = avr-objcopy
|
||||
OBJDUMP = avr-objdump
|
||||
SIZE = avr-size
|
||||
|
||||
|
||||
|
||||
all: $(TARGET).elf lst text size
|
||||
|
||||
$(TARGET).elf: $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
|
||||
|
||||
|
||||
clean:
|
||||
rm -rf *.o $(TARGET).elf *.eps *.bak *.a
|
||||
rm -rf *.lst *.map $(EXTRA_CLEAN_FILES)
|
||||
rm -rf $(TARGET).hex
|
||||
|
||||
size: $(TARGET).elf
|
||||
$(SIZE) -C --mcu=$(MCU_TARGET) $(TARGET).elf
|
||||
|
||||
lst: $(TARGET).lst
|
||||
%.lst: %.elf
|
||||
$(OBJDUMP) -h -S $< > $@
|
||||
|
||||
%.o : %.S
|
||||
$(CC) -c $(ALL_ASFLAGS) $< -o $@
|
||||
|
||||
|
||||
|
||||
text: hex
|
||||
hex: $(TARGET).hex
|
||||
|
||||
%.hex: %.elf
|
||||
$(OBJCOPY) -j .text -j .data -O ihex $< $@
|
||||
|
||||
|
||||
52
tools/ffsample/avr/Makefile_mmc
Normal file
52
tools/ffsample/avr/Makefile_mmc
Normal file
@@ -0,0 +1,52 @@
|
||||
TARGET = avr_mmc
|
||||
CSRC = main.c uart.c ff.c mmc.c rtc.c
|
||||
ASRC = xitoa.S
|
||||
MCU_TARGET = atmega64
|
||||
OPTIMIZE = -Os -mcall-prologues
|
||||
DEFS =
|
||||
LIBS =
|
||||
DEBUG = dwarf-2
|
||||
|
||||
CC = avr-gcc
|
||||
ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs
|
||||
ALL_ASFLAGS = -mmcu=$(MCU_TARGET) -I. -x assembler-with-cpp $(ASFLAGS)
|
||||
CFLAGS = -g$(DEBUG) -Wall $(OPTIMIZE) -mmcu=$(MCU_TARGET) $(DEFS)
|
||||
LDFLAGS = -Wl,-Map,$(TARGET).map
|
||||
OBJ = $(CSRC:.c=.o) $(ASRC:.S=.o)
|
||||
|
||||
OBJCOPY = avr-objcopy
|
||||
OBJDUMP = avr-objdump
|
||||
SIZE = avr-size
|
||||
|
||||
|
||||
|
||||
all: $(TARGET).elf lst text size
|
||||
|
||||
$(TARGET).elf: $(OBJ)
|
||||
$(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS)
|
||||
|
||||
|
||||
clean:
|
||||
rm -rf *.o $(TARGET).elf *.eps *.bak *.a
|
||||
rm -rf *.lst *.map $(EXTRA_CLEAN_FILES)
|
||||
rm -rf $(TARGET).hex
|
||||
|
||||
size: $(TARGET).elf
|
||||
$(SIZE) -C --mcu=$(MCU_TARGET) $(TARGET).elf
|
||||
|
||||
lst: $(TARGET).lst
|
||||
%.lst: %.elf
|
||||
$(OBJDUMP) -h -S $< > $@
|
||||
|
||||
%.o : %.S
|
||||
$(CC) -c $(ALL_ASFLAGS) $< -o $@
|
||||
|
||||
|
||||
|
||||
text: hex
|
||||
hex: $(TARGET).hex
|
||||
|
||||
%.hex: %.elf
|
||||
$(OBJCOPY) -j .text -j .data -O ihex $< $@
|
||||
|
||||
|
||||
453
tools/ffsample/avr/ata.c
Normal file
453
tools/ffsample/avr/ata.c
Normal file
@@ -0,0 +1,453 @@
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* ATA control module (C)ChaN, 2007 */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <string.h>
|
||||
#include "diskio.h"
|
||||
|
||||
|
||||
/* ATA command */
|
||||
#define CMD_RESET 0x08 /* DEVICE RESET */
|
||||
#define CMD_READ 0x20 /* READ SECTOR(S) */
|
||||
#define CMD_WRITE 0x30 /* WRITE SECTOR(S) */
|
||||
#define CMD_IDENTIFY 0xEC /* DEVICE IDENTIFY */
|
||||
#define CMD_SETFEATURES 0xEF /* SET FEATURES */
|
||||
|
||||
/* ATA register bit definitions */
|
||||
#define LBA 0x40
|
||||
#define BSY 0x80
|
||||
#define DRDY 0x40
|
||||
#define DF 0x20
|
||||
#define DRQ 0x08
|
||||
#define ERR 0x01
|
||||
#define SRST 0x40
|
||||
#define nIEN 0x20
|
||||
|
||||
/* Contorl Ports */
|
||||
#define CTRL_PORT PORTA
|
||||
#define CTRL_DDR DDRA
|
||||
#define DAT1_PORT PORTC
|
||||
#define DAT1_DDR DDRC
|
||||
#define DAT1_PIN PINC
|
||||
#define DAT0_PORT PORTD
|
||||
#define DAT0_DDR DDRD
|
||||
#define DAT0_PIN PIND
|
||||
|
||||
/* Bit definitions for Control Port */
|
||||
#define CTL_READ 0x20
|
||||
#define CTL_WRITE 0x40
|
||||
#define CTL_RESET 0x80
|
||||
#define REG_DATA 0xF0
|
||||
#define REG_ERROR 0xF1
|
||||
#define REG_FEATURES 0xF1
|
||||
#define REG_COUNT 0xF2
|
||||
#define REG_SECTOR 0xF3
|
||||
#define REG_CYLL 0xF4
|
||||
#define REG_CYLH 0xF5
|
||||
#define REG_DEV 0xF6
|
||||
#define REG_COMMAND 0xF7
|
||||
#define REG_STATUS 0xF7
|
||||
#define REG_DEVCTRL 0xEE
|
||||
#define REG_ALTSTAT 0xEE
|
||||
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
Module Private Functions
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
static volatile
|
||||
DSTATUS Stat = STA_NOINIT; /* Disk status */
|
||||
|
||||
static volatile
|
||||
WORD Timer; /* 100Hz decrement timer */
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Read an ATA register */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
BYTE read_ata (
|
||||
BYTE reg /* Register to be read */
|
||||
)
|
||||
{
|
||||
BYTE rd;
|
||||
|
||||
|
||||
CTRL_PORT = reg;
|
||||
CTRL_PORT &= ~CTL_READ;
|
||||
CTRL_PORT &= ~CTL_READ;
|
||||
CTRL_PORT &= ~CTL_READ;
|
||||
rd = DAT0_PIN;
|
||||
CTRL_PORT |= CTL_READ;
|
||||
return rd;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Write a byte to an ATA register */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
void write_ata (
|
||||
BYTE reg, /* Register to be written */
|
||||
BYTE dat /* Data to be written */
|
||||
)
|
||||
{
|
||||
CTRL_PORT = reg;
|
||||
DAT0_PORT = dat;
|
||||
DAT0_DDR = 0xFF;
|
||||
CTRL_PORT &= ~CTL_WRITE;
|
||||
CTRL_PORT &= ~CTL_WRITE;
|
||||
CTRL_PORT |= CTL_WRITE;
|
||||
DAT0_PORT = 0xFF;
|
||||
DAT0_DDR = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Read a part of data block */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
void read_part (
|
||||
BYTE *buff, /* Data buffer to store read data */
|
||||
BYTE ofs, /* Offset of the part of data in unit of word */
|
||||
BYTE count /* Number of word to pick up */
|
||||
)
|
||||
{
|
||||
BYTE c = 0, dl, dh;
|
||||
|
||||
|
||||
CTRL_PORT = REG_DATA; /* Select Data register */
|
||||
do {
|
||||
CTRL_PORT &= ~CTL_READ; /* IORD = L */
|
||||
CTRL_PORT &= ~CTL_READ; /* delay */
|
||||
dl = DAT0_PIN; /* Read even data */
|
||||
dh = DAT1_PIN; /* Read odd data */
|
||||
CTRL_PORT |= CTL_READ; /* IORD = H */
|
||||
if (count && (c >= ofs)) { /* Pick up a part of block */
|
||||
*buff++ = dl;
|
||||
*buff++ = dh;
|
||||
count--;
|
||||
}
|
||||
} while (++c);
|
||||
read_ata(REG_ALTSTAT);
|
||||
read_ata(REG_STATUS);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Wait for Data Ready */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
BOOL wait_data (void)
|
||||
{
|
||||
WORD w;
|
||||
BYTE s;
|
||||
|
||||
|
||||
cli(); Timer = 1000; sei(); /* Time out = 10 sec */
|
||||
do {
|
||||
cli(); w = Timer; sei();
|
||||
if (!w) return FALSE; /* Abort when timeout occured */
|
||||
s = read_ata(REG_STATUS); /* Get status */
|
||||
} while ((s & (BSY|DRQ)) != DRQ); /* Wait for BSY goes low and DRQ goes high */
|
||||
|
||||
read_ata(REG_ALTSTAT);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
Public Functions
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Initialize Disk Drive */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DSTATUS disk_initialize (
|
||||
BYTE drv /* Physical drive nmuber (0) */
|
||||
)
|
||||
{
|
||||
WORD w;
|
||||
|
||||
|
||||
if (drv) return STA_NOINIT; /* Supports only single drive */
|
||||
Stat |= STA_NOINIT;
|
||||
|
||||
/* Initialize the ATA control port */
|
||||
DAT0_PORT = 0xFF;
|
||||
DAT1_PORT = 0xFF;
|
||||
DAT0_DDR = 0;
|
||||
DAT1_DDR = 0;
|
||||
CTRL_PORT = CTL_READ | CTL_WRITE; /* Assert RESET */
|
||||
CTRL_DDR = 0xFF;
|
||||
|
||||
for (Timer = 2; Timer; ); /* 20ms */
|
||||
CTRL_PORT |= CTL_RESET; /* Deassert RESET */
|
||||
for (Timer = 1; Timer; ); /* 10ms */
|
||||
write_ata(REG_DEV, 0); /* Select Device 0 */
|
||||
|
||||
cli(); Timer = 1000; sei();
|
||||
do {
|
||||
cli(); w = Timer; sei();
|
||||
if (!w) return Stat;
|
||||
} while (!(read_ata(REG_STATUS) & (BSY | DRQ)));
|
||||
|
||||
write_ata(REG_DEVCTRL, SRST | nIEN); /* Software reset */
|
||||
for (Timer = 2; Timer; );
|
||||
write_ata(REG_DEVCTRL, nIEN);
|
||||
for (Timer = 2; Timer; );
|
||||
|
||||
cli(); Timer = 1000; sei();
|
||||
do {
|
||||
cli(); w = Timer; sei();
|
||||
if (!w) return Stat;
|
||||
} while ((read_ata(REG_STATUS) & (DRDY|BSY)) != DRDY);
|
||||
|
||||
write_ata(REG_FEATURES, 3); /* Select PIO default mode without IORDY */
|
||||
write_ata(REG_COUNT, 1);
|
||||
write_ata(REG_COMMAND, CMD_SETFEATURES);
|
||||
Timer = 100;
|
||||
do {
|
||||
if (!Timer) return Stat;
|
||||
} while (read_ata(REG_STATUS) & BSY);
|
||||
|
||||
|
||||
Stat &= ~STA_NOINIT; /* When device goes ready, clear STA_NOINIT */
|
||||
|
||||
return Stat;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Return Disk Status */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DSTATUS disk_status (
|
||||
BYTE drv /* Physical drive nmuber (0) */
|
||||
)
|
||||
{
|
||||
if (drv) return STA_NOINIT; /* Supports only single drive */
|
||||
return Stat;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Read Sector(s) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DRESULT disk_read (
|
||||
BYTE drv, /* Physical drive nmuber (0) */
|
||||
BYTE *buff, /* Data buffer to store read data */
|
||||
DWORD sector, /* Sector number (LBA) */
|
||||
BYTE count /* Sector count (1..255) */
|
||||
)
|
||||
{
|
||||
BYTE c, iord_l, iord_h;
|
||||
|
||||
|
||||
if (drv || !count) return RES_PARERR;
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
|
||||
/* Issue Read Setor(s) command */
|
||||
write_ata(REG_COUNT, count);
|
||||
write_ata(REG_SECTOR, (BYTE)sector);
|
||||
write_ata(REG_CYLL, (BYTE)(sector >> 8));
|
||||
write_ata(REG_CYLH, (BYTE)(sector >> 16));
|
||||
write_ata(REG_DEV, ((BYTE)(sector >> 24) & 0x0F) | LBA);
|
||||
write_ata(REG_COMMAND, CMD_READ);
|
||||
|
||||
do {
|
||||
if (!wait_data()) return RES_ERROR; /* Wait data ready */
|
||||
CTRL_PORT = REG_DATA;
|
||||
iord_h = REG_DATA;
|
||||
iord_l = REG_DATA & ~CTL_READ;
|
||||
c = 128;
|
||||
do {
|
||||
CTRL_PORT = iord_l; /* IORD = L */
|
||||
CTRL_PORT = iord_l; /* delay */
|
||||
*buff++ = DAT0_PIN; /* Get even data */
|
||||
*buff++ = DAT1_PIN; /* Get odd data */
|
||||
CTRL_PORT = iord_h; /* IORD = H */
|
||||
CTRL_PORT = iord_l; /* IORD = L */
|
||||
CTRL_PORT = iord_l; /* delay */
|
||||
*buff++ = DAT0_PIN; /* Get even data */
|
||||
*buff++ = DAT1_PIN; /* Get odd data */
|
||||
CTRL_PORT = iord_h; /* IORD = H */
|
||||
} while (--c);
|
||||
} while (--count);
|
||||
|
||||
read_ata(REG_ALTSTAT);
|
||||
read_ata(REG_STATUS);
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Write Sector(s) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
#if _READONLY == 0
|
||||
DRESULT disk_write (
|
||||
BYTE drv, /* Physical drive nmuber (0) */
|
||||
const BYTE *buff, /* Data to be written */
|
||||
DWORD sector, /* Sector number (LBA) */
|
||||
BYTE count /* Sector count (1..255) */
|
||||
)
|
||||
{
|
||||
BYTE s, c, iowr_l, iowr_h;
|
||||
|
||||
|
||||
if (drv || !count) return RES_PARERR;
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
|
||||
/* Issue Write Setor(s) command */
|
||||
write_ata(REG_COUNT, count);
|
||||
write_ata(REG_SECTOR, (BYTE)sector);
|
||||
write_ata(REG_CYLL, (BYTE)(sector >> 8));
|
||||
write_ata(REG_CYLH, (BYTE)(sector >> 16));
|
||||
write_ata(REG_DEV, ((BYTE)(sector >> 24) & 0x0F) | LBA);
|
||||
write_ata(REG_COMMAND, CMD_WRITE);
|
||||
|
||||
do {
|
||||
if (!wait_data()) return RES_ERROR;
|
||||
CTRL_PORT = REG_DATA;
|
||||
iowr_h = REG_DATA;
|
||||
iowr_l = REG_DATA & ~CTL_WRITE;
|
||||
DAT0_DDR = 0xFF; /* Set D0-D15 as output */
|
||||
DAT1_DDR = 0xFF;
|
||||
c = 128;
|
||||
do {
|
||||
DAT0_PORT = *buff++; /* Set even data */
|
||||
DAT1_PORT = *buff++; /* Set odd data */
|
||||
CTRL_PORT = iowr_l; /* IOWR = L */
|
||||
CTRL_PORT = iowr_h; /* IOWR = H */
|
||||
DAT0_PORT = *buff++; /* Set even data */
|
||||
DAT1_PORT = *buff++; /* Set odd data */
|
||||
CTRL_PORT = iowr_l; /* IOWR = L */
|
||||
CTRL_PORT = iowr_h; /* IOWR = H */
|
||||
} while (--c);
|
||||
DAT0_PORT = 0xFF; /* Set D0-D15 as input */
|
||||
DAT1_PORT = 0xFF;
|
||||
DAT0_DDR = 0;
|
||||
DAT1_DDR = 0;
|
||||
} while (--count);
|
||||
|
||||
Timer = 100;
|
||||
do {
|
||||
if (!Timer) return RES_ERROR;
|
||||
s = read_ata(REG_STATUS);
|
||||
} while (s & BSY);
|
||||
if (s & ERR) return RES_ERROR;
|
||||
|
||||
read_ata(REG_ALTSTAT);
|
||||
read_ata(REG_STATUS);
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
#endif /* _READONLY == 0 */
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Miscellaneous Functions */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
#if _USE_IOCTL != 0
|
||||
DRESULT disk_ioctl (
|
||||
BYTE drv, /* Physical drive nmuber (0) */
|
||||
BYTE ctrl, /* Control code */
|
||||
void *buff /* Buffer to send/receive data block */
|
||||
)
|
||||
{
|
||||
BYTE n, dl, dh, ofs, w, *ptr = buff;
|
||||
|
||||
|
||||
if (drv) return RES_PARERR;
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
|
||||
switch (ctrl) {
|
||||
case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
|
||||
ofs = 60; w = 2; n = 0;
|
||||
break;
|
||||
|
||||
case GET_SECTOR_SIZE : /* Get sectors on the disk (WORD) */
|
||||
*(WORD*)buff = 512;
|
||||
return RES_OK;
|
||||
|
||||
case GET_BLOCK_SIZE : /* Get erase block size in sectors (DWORD) */
|
||||
*(DWORD*)buff = 1;
|
||||
return RES_OK;
|
||||
|
||||
case CTRL_SYNC : /* Nothing to do */
|
||||
return RES_OK;
|
||||
|
||||
case ATA_GET_REV : /* Get firmware revision (8 chars) */
|
||||
ofs = 23; w = 4; n = 4;
|
||||
break;
|
||||
|
||||
case ATA_GET_MODEL : /* Get model name (40 chars) */
|
||||
ofs = 27; w = 20; n = 20;
|
||||
break;
|
||||
|
||||
case ATA_GET_SN : /* Get serial number (20 chars) */
|
||||
ofs = 10; w = 10; n = 10;
|
||||
break;
|
||||
|
||||
default:
|
||||
return RES_PARERR;
|
||||
}
|
||||
|
||||
write_ata(REG_COMMAND, CMD_IDENTIFY);
|
||||
if (!wait_data()) return RES_ERROR;
|
||||
read_part(ptr, ofs, w);
|
||||
while (n--) {
|
||||
dl = *ptr; dh = *(ptr+1);
|
||||
*ptr++ = dh; *ptr++ = dl;
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
#endif /* _USE_IOCTL != 0 */
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Device timer interrupt procedure */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* This function must be called in period of 10ms */
|
||||
|
||||
void disk_timerproc (void)
|
||||
{
|
||||
WORD n;
|
||||
|
||||
|
||||
n = Timer; /* 100Hz decrement timer */
|
||||
if (n) Timer = --n;
|
||||
}
|
||||
|
||||
BIN
tools/ffsample/avr/avr_ata.png
Normal file
BIN
tools/ffsample/avr/avr_ata.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
BIN
tools/ffsample/avr/avr_cfc.png
Normal file
BIN
tools/ffsample/avr/avr_cfc.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 12 KiB |
BIN
tools/ffsample/avr/avr_mmc.png
Normal file
BIN
tools/ffsample/avr/avr_mmc.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 11 KiB |
464
tools/ffsample/avr/cfc.c
Normal file
464
tools/ffsample/avr/cfc.c
Normal file
@@ -0,0 +1,464 @@
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* CompactFlash control module (C)ChaN, 2007 */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <string.h>
|
||||
#include "diskio.h"
|
||||
|
||||
|
||||
/* ATA command */
|
||||
#define CMD_RESET 0x08 /* DEVICE RESET */
|
||||
#define CMD_READ 0x20 /* READ SECTOR(S) */
|
||||
#define CMD_WRITE 0x30 /* WRITE SECTOR(S) */
|
||||
#define CMD_IDENTIFY 0xEC /* DEVICE IDENTIFY */
|
||||
#define CMD_SETFEATURES 0xEF /* SET FEATURES */
|
||||
|
||||
/* ATA register bit definitions */
|
||||
#define LBA 0xE0
|
||||
#define BUSY 0x80
|
||||
#define DRDY 0x40
|
||||
#define DF 0x20
|
||||
#define DRQ 0x08
|
||||
#define ERR 0x01
|
||||
#define SRST 0x40
|
||||
#define nIEN 0x20
|
||||
|
||||
/* Contorl Ports */
|
||||
#define CTRL_PORT PORTA
|
||||
#define CTRL_DDR DDRA
|
||||
#define SOCK_PORT PORTC
|
||||
#define SOCK_DDR DDRC
|
||||
#define SOCK_PIN PINC
|
||||
#define DAT0_PORT PORTD
|
||||
#define DAT0_DDR DDRD
|
||||
#define DAT0_PIN PIND
|
||||
#define SOCKINS 0x03
|
||||
#define SOCKPWR 0x04
|
||||
|
||||
/* Bit definitions for Control Port */
|
||||
#define CTL_READ 0x20
|
||||
#define CTL_WRITE 0x40
|
||||
#define CTL_RESET 0x80
|
||||
#define REG_DATA 0xF0
|
||||
#define REG_ERROR 0xF1
|
||||
#define REG_FEATURES 0xF1
|
||||
#define REG_COUNT 0xF2
|
||||
#define REG_SECTOR 0xF3
|
||||
#define REG_CYLL 0xF4
|
||||
#define REG_CYLH 0xF5
|
||||
#define REG_DEV 0xF6
|
||||
#define REG_COMMAND 0xF7
|
||||
#define REG_STATUS 0xF7
|
||||
#define REG_DEVCTRL 0xEE
|
||||
#define REG_ALTSTAT 0xEE
|
||||
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
Module Private Functions
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
static volatile
|
||||
DSTATUS Stat = STA_NOINIT; /* Disk status */
|
||||
|
||||
static volatile
|
||||
BYTE Timer; /* 100Hz decrement timer */
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Read an ATA register */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
BYTE read_ata (
|
||||
BYTE reg /* Register to be read */
|
||||
)
|
||||
{
|
||||
BYTE rd;
|
||||
|
||||
|
||||
CTRL_PORT = reg;
|
||||
CTRL_PORT &= ~CTL_READ;
|
||||
CTRL_PORT &= ~CTL_READ;
|
||||
CTRL_PORT &= ~CTL_READ;
|
||||
rd = DAT0_PIN;
|
||||
CTRL_PORT |= CTL_READ;
|
||||
return rd;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Write a byte to an ATA register */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
void write_ata (
|
||||
BYTE reg, /* Register to be written */
|
||||
BYTE dat /* Data to be written */
|
||||
)
|
||||
{
|
||||
CTRL_PORT = reg;
|
||||
DAT0_PORT = dat;
|
||||
DAT0_DDR = 0xFF;
|
||||
CTRL_PORT &= ~CTL_WRITE;
|
||||
CTRL_PORT &= ~CTL_WRITE;
|
||||
CTRL_PORT |= CTL_WRITE;
|
||||
DAT0_PORT = 0xFF;
|
||||
DAT0_DDR = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Read a part of data block */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
void read_part (
|
||||
BYTE *buff, /* Data buffer to store read data */
|
||||
BYTE ofs, /* Offset of the part of data in unit of word */
|
||||
BYTE count /* Number of word to pick up */
|
||||
)
|
||||
{
|
||||
BYTE c = 0, dl, dh;
|
||||
|
||||
|
||||
CTRL_PORT = REG_DATA; /* Select Data register */
|
||||
do {
|
||||
CTRL_PORT &= ~CTL_READ; /* IORD = L */
|
||||
CTRL_PORT &= ~CTL_READ; /* delay */
|
||||
dl = DAT0_PIN; /* Read Even data */
|
||||
CTRL_PORT |= CTL_READ; /* IORD = H */
|
||||
CTRL_PORT &= ~CTL_READ; /* IORD = L */
|
||||
CTRL_PORT &= ~CTL_READ; /* delay */
|
||||
dh = DAT0_PIN; /* Read Odd data */
|
||||
CTRL_PORT |= CTL_READ; /* IORD = H */
|
||||
if (count && (c >= ofs)) { /* Pick up a part of block */
|
||||
*buff++ = dl;
|
||||
*buff++ = dh;
|
||||
count--;
|
||||
}
|
||||
} while (++c);
|
||||
read_ata(REG_ALTSTAT);
|
||||
read_ata(REG_STATUS);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Wait for Data Ready */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
BOOL wait_data (void)
|
||||
{
|
||||
BYTE s;
|
||||
|
||||
|
||||
Timer = 100; /* Time out = 1 sec */
|
||||
do {
|
||||
if (!Timer) return FALSE; /* Abort when timeout occured */
|
||||
s = read_ata(REG_STATUS); /* Get status */
|
||||
} while ((s & (BUSY|DRQ)) != DRQ); /* Wait for BUSY goes low and DRQ goes high */
|
||||
|
||||
read_ata(REG_ALTSTAT);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
Public Functions
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Initialize Disk Drive */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DSTATUS disk_initialize (
|
||||
BYTE drv /* Physical drive nmuber (0) */
|
||||
)
|
||||
{
|
||||
if (drv) return STA_NOINIT; /* Supports only single drive */
|
||||
|
||||
Stat |= STA_NOINIT;
|
||||
|
||||
SOCK_PORT = 0xFF; /* Power OFF */
|
||||
SOCK_DDR = SOCKPWR;
|
||||
DAT0_PORT = 0;
|
||||
CTRL_DDR = 0;
|
||||
for (Timer = 10; Timer; ); /* 100ms */
|
||||
|
||||
if (Stat & STA_NODISK) return Stat; /* Exit when socket is empty */
|
||||
|
||||
/* Initialize CFC control port */
|
||||
SOCK_PORT &= ~SOCKPWR; /* Power ON */
|
||||
for (Timer = 1; Timer; ); /* 10ms */
|
||||
CTRL_PORT = CTL_READ | CTL_WRITE; /* Enable control signals */
|
||||
CTRL_DDR = 0xFF;
|
||||
DAT0_PORT = 0xFF; /* Pull-up D0-D7 */
|
||||
for (Timer = 5; Timer; ); /* 50ms */
|
||||
CTRL_PORT |= CTL_RESET; /* RESET = H */
|
||||
for (Timer = 5; Timer; ); /* 50ms */
|
||||
write_ata(REG_DEV, LBA); /* Select Device 0 */
|
||||
Timer = 200;
|
||||
do { /* Wait for card goes ready */
|
||||
if (!Timer) return Stat;
|
||||
} while (read_ata(REG_STATUS) & BUSY);
|
||||
|
||||
write_ata(REG_DEVCTRL, SRST | nIEN); /* Software reset */
|
||||
for (Timer = 2; Timer; ); /* 20ms */
|
||||
write_ata(REG_DEVCTRL, nIEN); /* Release software reset */
|
||||
for (Timer = 2; Timer; ); /* 20ms */
|
||||
Timer = 200;
|
||||
do { /* Wait for card goes ready */
|
||||
if (!Timer) return Stat;
|
||||
} while ((read_ata(REG_STATUS) & (DRDY|BUSY)) != DRDY);
|
||||
|
||||
write_ata(REG_FEATURES, 0x01); /* Select 8-bit PIO transfer mode */
|
||||
write_ata(REG_COMMAND, CMD_SETFEATURES);
|
||||
Timer = 100;
|
||||
do {
|
||||
if (!Timer) return Stat;
|
||||
} while (read_ata(REG_STATUS) & BUSY);
|
||||
|
||||
Stat &= ~STA_NOINIT; /* When device goes ready, clear STA_NOINIT */
|
||||
|
||||
return Stat;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Return Disk Status */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DSTATUS disk_status (
|
||||
BYTE drv /* Physical drive nmuber (0) */
|
||||
)
|
||||
{
|
||||
if (drv) return STA_NOINIT; /* Supports only single drive */
|
||||
return Stat;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Read Sector(s) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DRESULT disk_read (
|
||||
BYTE drv, /* Physical drive nmuber (0) */
|
||||
BYTE *buff, /* Data buffer to store read data */
|
||||
DWORD sector, /* Sector number (LBA) */
|
||||
BYTE count /* Sector count (1..255) */
|
||||
)
|
||||
{
|
||||
BYTE c, iord_l, iord_h;
|
||||
|
||||
|
||||
if (drv || !count) return RES_PARERR;
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
|
||||
/* Issue Read Setor(s) command */
|
||||
write_ata(REG_COUNT, count);
|
||||
write_ata(REG_SECTOR, (BYTE)sector);
|
||||
write_ata(REG_CYLL, (BYTE)(sector >> 8));
|
||||
write_ata(REG_CYLH, (BYTE)(sector >> 16));
|
||||
write_ata(REG_DEV, ((BYTE)(sector >> 24) & 0x0F) | LBA);
|
||||
write_ata(REG_COMMAND, CMD_READ);
|
||||
|
||||
do {
|
||||
if (!wait_data()) return RES_ERROR; /* Wait data ready */
|
||||
CTRL_PORT = REG_DATA;
|
||||
iord_h = REG_DATA;
|
||||
iord_l = REG_DATA & ~CTL_READ;
|
||||
c = 0;
|
||||
do {
|
||||
CTRL_PORT = iord_l; /* IORD = L */
|
||||
CTRL_PORT = iord_l; /* delay */
|
||||
CTRL_PORT = iord_l; /* delay */
|
||||
*buff++ = DAT0_PIN; /* Get even data */
|
||||
CTRL_PORT = iord_h; /* IORD = H */
|
||||
CTRL_PORT = iord_l; /* IORD = L */
|
||||
CTRL_PORT = iord_l; /* delay */
|
||||
CTRL_PORT = iord_l; /* delay */
|
||||
*buff++ = DAT0_PIN; /* Get Odd data */
|
||||
CTRL_PORT = iord_h; /* IORD = H */
|
||||
} while (--c);
|
||||
} while (--count);
|
||||
|
||||
read_ata(REG_ALTSTAT);
|
||||
read_ata(REG_STATUS);
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Write Sector(s) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
#if _READONLY == 0
|
||||
DRESULT disk_write (
|
||||
BYTE drv, /* Physical drive nmuber (0) */
|
||||
const BYTE *buff, /* Data to be written */
|
||||
DWORD sector, /* Sector number (LBA) */
|
||||
BYTE count /* Sector count (1..255) */
|
||||
)
|
||||
{
|
||||
BYTE s, c, iowr_l, iowr_h;
|
||||
|
||||
|
||||
if (drv || !count) return RES_PARERR;
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
|
||||
/* Issue Write Setor(s) command */
|
||||
write_ata(REG_COUNT, count);
|
||||
write_ata(REG_SECTOR, (BYTE)sector);
|
||||
write_ata(REG_CYLL, (BYTE)(sector >> 8));
|
||||
write_ata(REG_CYLH, (BYTE)(sector >> 16));
|
||||
write_ata(REG_DEV, ((BYTE)(sector >> 24) & 0x0F) | LBA);
|
||||
write_ata(REG_COMMAND, CMD_WRITE);
|
||||
|
||||
do {
|
||||
if (!wait_data()) return RES_ERROR;
|
||||
CTRL_PORT = REG_DATA;
|
||||
iowr_h = REG_DATA;
|
||||
iowr_l = REG_DATA & ~CTL_WRITE;
|
||||
DAT0_DDR = 0xFF; /* Set D0-D7 as output */
|
||||
c = 0;
|
||||
do {
|
||||
DAT0_PORT = *buff++; /* Set even data */
|
||||
CTRL_PORT = iowr_l; /* IOWR = L */
|
||||
CTRL_PORT = iowr_h; /* IOWR = H */
|
||||
DAT0_PORT = *buff++; /* Set odd data */
|
||||
CTRL_PORT = iowr_l; /* IOWR = L */
|
||||
CTRL_PORT = iowr_h; /* IOWR = H */
|
||||
} while (--c);
|
||||
DAT0_PORT = 0xFF; /* Set D0-D7 as input */
|
||||
DAT0_DDR = 0;
|
||||
} while (--count);
|
||||
|
||||
Timer = 100;
|
||||
do {
|
||||
if (!Timer) return RES_ERROR;
|
||||
s = read_ata(REG_STATUS);
|
||||
} while (s & BUSY);
|
||||
if (s & ERR) return RES_ERROR;
|
||||
|
||||
read_ata(REG_ALTSTAT);
|
||||
read_ata(REG_STATUS);
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
#endif /* _READONLY */
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Miscellaneous Functions */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
#if _USE_IOCTL != 0
|
||||
DRESULT disk_ioctl (
|
||||
BYTE drv, /* Physical drive nmuber (0) */
|
||||
BYTE ctrl, /* Control code */
|
||||
void *buff /* Buffer to send/receive data block */
|
||||
)
|
||||
{
|
||||
BYTE n, w, ofs, dl, dh, *ptr = buff;
|
||||
|
||||
|
||||
if (drv) return RES_PARERR;
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
|
||||
switch (ctrl) {
|
||||
case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
|
||||
ofs = 60; w = 2; n = 0;
|
||||
break;
|
||||
|
||||
case GET_SECTOR_SIZE : /* Get sectors on the disk (WORD) */
|
||||
*(WORD*)buff = 512;
|
||||
return RES_OK;
|
||||
|
||||
case GET_BLOCK_SIZE : /* Get erase block size in sectors (DWORD) */
|
||||
*(DWORD*)buff = 32;
|
||||
return RES_OK;
|
||||
|
||||
case CTRL_SYNC : /* Nothing to do */
|
||||
return RES_OK;
|
||||
|
||||
case ATA_GET_REV : /* Get firmware revision (8 chars) */
|
||||
ofs = 23; w = 4; n = 4;
|
||||
break;
|
||||
|
||||
case ATA_GET_MODEL : /* Get model name (40 chars) */
|
||||
ofs = 27; w = 20; n = 20;
|
||||
break;
|
||||
|
||||
case ATA_GET_SN : /* Get serial number (20 chars) */
|
||||
ofs = 10; w = 10; n = 10;
|
||||
break;
|
||||
|
||||
default:
|
||||
return RES_PARERR;
|
||||
}
|
||||
|
||||
write_ata(REG_COMMAND, CMD_IDENTIFY);
|
||||
if (!wait_data()) return RES_ERROR;
|
||||
read_part(ptr, ofs, w);
|
||||
while (n--) {
|
||||
dl = *ptr; dh = *(ptr+1);
|
||||
*ptr++ = dh; *ptr++ = dl;
|
||||
}
|
||||
|
||||
return RES_OK;
|
||||
}
|
||||
#endif /* _USE_IOCTL != 0 */
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Device timer interrupt procedure */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* This function must be called in period of 10ms */
|
||||
|
||||
|
||||
void disk_timerproc (void)
|
||||
{
|
||||
static BYTE pv;
|
||||
BYTE n;
|
||||
|
||||
|
||||
n = Timer; /* 100Hz decrement timer */
|
||||
if (n) Timer = --n;
|
||||
|
||||
n = pv;
|
||||
pv = SOCK_PIN & SOCKINS; /* Sapmle socket switch */
|
||||
|
||||
if (n == pv) { /* Have contacts stabled? */
|
||||
if (pv & SOCKINS) { /* CD1 or CD2 is high (Socket empty) */
|
||||
Stat |= (STA_NODISK | STA_NOINIT);
|
||||
DAT0_DDR = 0; DAT0_PORT = 0; /* Float D0-D7 */
|
||||
CTRL_DDR = CTL_RESET; CTRL_PORT = 0; /* Assert RESET# */
|
||||
SOCK_PORT |= SOCKPWR; /* Power OFF */
|
||||
} else { /* CD1 and CD2 are low (Card inserted) */
|
||||
Stat &= ~STA_NODISK;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
1038
tools/ffsample/avr/cfmm.c
Normal file
1038
tools/ffsample/avr/cfmm.c
Normal file
File diff suppressed because it is too large
Load Diff
81
tools/ffsample/avr/diskio.h
Normal file
81
tools/ffsample/avr/diskio.h
Normal file
@@ -0,0 +1,81 @@
|
||||
/*-----------------------------------------------------------------------
|
||||
/ Low level disk interface modlue include file R0.05 (C)ChaN, 2007
|
||||
/-----------------------------------------------------------------------*/
|
||||
|
||||
#ifndef _DISKIO
|
||||
|
||||
#define _READONLY 0 /* 1: Read-only mode */
|
||||
#define _USE_IOCTL 1
|
||||
|
||||
#include "integer.h"
|
||||
|
||||
|
||||
/* Status of Disk Functions */
|
||||
typedef BYTE DSTATUS;
|
||||
|
||||
/* Results of Disk Functions */
|
||||
typedef enum {
|
||||
RES_OK = 0, /* 0: Successful */
|
||||
RES_ERROR, /* 1: R/W Error */
|
||||
RES_WRPRT, /* 2: Write Protected */
|
||||
RES_NOTRDY, /* 3: Not Ready */
|
||||
RES_PARERR /* 4: Invalid Parameter */
|
||||
} DRESULT;
|
||||
|
||||
|
||||
/*---------------------------------------*/
|
||||
/* Prototypes for disk control functions */
|
||||
|
||||
DSTATUS disk_initialize (BYTE);
|
||||
DSTATUS disk_status (BYTE);
|
||||
DRESULT disk_read (BYTE, BYTE*, DWORD, BYTE);
|
||||
#if _READONLY == 0
|
||||
DRESULT disk_write (BYTE, const BYTE*, DWORD, BYTE);
|
||||
#endif
|
||||
DRESULT disk_ioctl (BYTE, BYTE, void*);
|
||||
void disk_timerproc (void);
|
||||
|
||||
|
||||
|
||||
|
||||
/* Disk Status Bits (DSTATUS) */
|
||||
|
||||
#define STA_NOINIT 0x01 /* Drive not initialized */
|
||||
#define STA_NODISK 0x02 /* No medium in the drive */
|
||||
#define STA_PROTECT 0x04 /* Write protected */
|
||||
|
||||
|
||||
|
||||
/* Command code for disk_ioctrl() */
|
||||
|
||||
/* Generic command */
|
||||
#define CTRL_SYNC 0 /* Mandatory for write functions */
|
||||
#define GET_SECTOR_COUNT 1 /* Mandatory for only f_mkfs() */
|
||||
#define GET_SECTOR_SIZE 2
|
||||
#define GET_BLOCK_SIZE 3 /* Mandatory for only f_mkfs() */
|
||||
#define CTRL_POWER 4
|
||||
#define CTRL_LOCK 5
|
||||
#define CTRL_EJECT 6
|
||||
/* MMC/SDC command */
|
||||
#define MMC_GET_TYPE 10
|
||||
#define MMC_GET_CSD 11
|
||||
#define MMC_GET_CID 12
|
||||
#define MMC_GET_OCR 13
|
||||
#define MMC_GET_SDSTAT 14
|
||||
/* ATA/CF command */
|
||||
#define ATA_GET_REV 20
|
||||
#define ATA_GET_MODEL 21
|
||||
#define ATA_GET_SN 22
|
||||
|
||||
|
||||
|
||||
/* Card type flags (CardType) */
|
||||
#define CT_MMC 0x01
|
||||
#define CT_SD1 0x02
|
||||
#define CT_SD2 0x04
|
||||
#define CT_SDC (CT_SD1|CT_SD2)
|
||||
#define CT_BLOCK 0x08
|
||||
|
||||
|
||||
#define _DISKIO
|
||||
#endif
|
||||
2936
tools/ffsample/avr/ff.c
Normal file
2936
tools/ffsample/avr/ff.c
Normal file
File diff suppressed because it is too large
Load Diff
547
tools/ffsample/avr/ff.h
Normal file
547
tools/ffsample/avr/ff.h
Normal file
@@ -0,0 +1,547 @@
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ FatFs - FAT file system module include file R0.07a (C)ChaN, 2009
|
||||
/----------------------------------------------------------------------------/
|
||||
/ FatFs module is an open source software to implement FAT file system to
|
||||
/ small embedded systems. This is a free software and is opened for education,
|
||||
/ research and commercial developments under license policy of following trems.
|
||||
/
|
||||
/ Copyright (C) 2009, ChaN, all right reserved.
|
||||
/
|
||||
/ * The FatFs module is a free software and there is NO WARRANTY.
|
||||
/ * No restriction on use. You can use, modify and redistribute it for
|
||||
/ personal, non-profit or commercial use UNDER YOUR RESPONSIBILITY.
|
||||
/ * Redistributions of source code must retain the above copyright notice.
|
||||
/----------------------------------------------------------------------------*/
|
||||
|
||||
#include "integer.h"
|
||||
|
||||
/*---------------------------------------------------------------------------/
|
||||
/ FatFs Configuration Options
|
||||
/
|
||||
/ CAUTION! Do not forget to make clean the project after any changes to
|
||||
/ the configuration options.
|
||||
/
|
||||
/----------------------------------------------------------------------------*/
|
||||
#ifndef _FATFS
|
||||
#define _FATFS
|
||||
|
||||
#define _WORD_ACCESS 1
|
||||
/* The _WORD_ACCESS option defines which access method is used to the word
|
||||
/ data in the FAT structure.
|
||||
/
|
||||
/ 0: Byte-by-byte access. Always compatible with all platforms.
|
||||
/ 1: Word access. Do not choose this unless following condition is met.
|
||||
/
|
||||
/ When the byte order on the memory is big-endian or address miss-aligned
|
||||
/ word access results incorrect behavior, the _WORD_ACCESS must be set to 0.
|
||||
/ If it is not the case, the value can also be set to 1 to improve the
|
||||
/ performance and code efficiency. */
|
||||
|
||||
|
||||
#define _FS_READONLY 0
|
||||
/* Setting _FS_READONLY to 1 defines read only configuration. This removes
|
||||
/ writing functions, f_write, f_sync, f_unlink, f_mkdir, f_chmod, f_rename,
|
||||
/ f_truncate and useless f_getfree. */
|
||||
|
||||
|
||||
#define _FS_MINIMIZE 0
|
||||
/* The _FS_MINIMIZE option defines minimization level to remove some functions.
|
||||
/
|
||||
/ 0: Full function.
|
||||
/ 1: f_stat, f_getfree, f_unlink, f_mkdir, f_chmod, f_truncate and f_rename
|
||||
/ are removed.
|
||||
/ 2: f_opendir and f_readdir are removed in addition to level 1.
|
||||
/ 3: f_lseek is removed in addition to level 2. */
|
||||
|
||||
|
||||
#define _FS_TINY 1
|
||||
/* When _FS_TINY is set to 1, FatFs uses the sector buffer in the file system
|
||||
/ object instead of the sector buffer in the individual file object for file
|
||||
/ data transfer. This reduces memory consumption 512 bytes each file object. */
|
||||
|
||||
|
||||
#define _USE_STRFUNC 0
|
||||
/* To enable string functions, set _USE_STRFUNC to 1 or 2. */
|
||||
|
||||
|
||||
#define _USE_MKFS 1
|
||||
/* To enable f_mkfs function, set _USE_MKFS to 1 and set _FS_READONLY to 0 */
|
||||
|
||||
|
||||
#define _USE_FORWARD 0
|
||||
/* To enable f_forward function, set _USE_FORWARD to 1 and set _FS_TINY to 1. */
|
||||
|
||||
|
||||
#define _DRIVES 2
|
||||
/* Number of volumes (logical drives) to be used. */
|
||||
|
||||
|
||||
#define _MAX_SS 512
|
||||
/* Maximum sector size to be handled. (512/1024/2048/4096) */
|
||||
/* 512 for memroy card and hard disk, 1024 for floppy disk, 2048 for MO disk */
|
||||
|
||||
|
||||
#define _MULTI_PARTITION 0
|
||||
/* When _MULTI_PARTITION is set to 0, each volume is bound to the same physical
|
||||
/ drive number and can mount only first primaly partition. When it is set to 1,
|
||||
/ each volume is tied to the partitions listed in Drives[]. */
|
||||
|
||||
|
||||
#define _CODE_PAGE 932
|
||||
/* The _CODE_PAGE specifies the OEM code page to be used on the target system.
|
||||
/ When it is non LFN configuration, there is no difference between SBCS code
|
||||
/ pages. When LFN is enabled, the code page must always be set correctly.
|
||||
/ 437 - U.S.
|
||||
/ 720 - Arabic
|
||||
/ 737 - Greek
|
||||
/ 775 - Baltic
|
||||
/ 850 - Multilingual Latin 1
|
||||
/ 852 - Latin 2
|
||||
/ 855 - Cyrillic
|
||||
/ 857 - Turkish
|
||||
/ 858 - Multilingual Latin 1 + Euro
|
||||
/ 862 - Hebrew
|
||||
/ 866 - Russian
|
||||
/ 874 - Thai
|
||||
/ 932 - Japanese Shift-JIS (DBCS)
|
||||
/ 936 - Simplified Chinese GBK (DBCS)
|
||||
/ 949 - Korean (DBCS)
|
||||
/ 950 - Traditional Chinese Big5 (DBCS)
|
||||
/ 1258 - Vietnam
|
||||
*/
|
||||
|
||||
|
||||
#define _USE_LFN 0
|
||||
#define _MAX_LFN 255 /* Maximum LFN length to handle (max:255) */
|
||||
/* The _USE_LFN option switches the LFN support.
|
||||
/
|
||||
/ 0: Disable LFN.
|
||||
/ 1: Enable LFN with static working buffer on the bss. NOT REENTRANT.
|
||||
/ 2: Enable LFN with dynamic working buffer on the caller's STACK.
|
||||
/
|
||||
/ The working buffer occupies (_MAX_LFN + 1) * 2 bytes. When enable LFN,
|
||||
/ a Unicode - OEM code conversion function ff_convert() must be added to
|
||||
/ the project. */
|
||||
|
||||
|
||||
#define _FS_REENTRANT 0
|
||||
#define _TIMEOUT 1000 /* Timeout period in unit of time ticks */
|
||||
#define _SYNC_t HANDLE /* Type of sync object used on the OS. */
|
||||
/* e.g. HANDLE, OS_EVENT*, ID and etc.. */
|
||||
/* To make the FatFs module re-entrant, set _FS_REENTRANT to 1 and add user
|
||||
/ provided synchronization handlers, ff_req_grant, ff_rel_grant,
|
||||
/ ff_del_syncobj and ff_cre_syncobj function to the project. */
|
||||
|
||||
|
||||
|
||||
/* End of configuration options. Do not change followings without care. */
|
||||
/*--------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
|
||||
/* Definitions corresponds to multiple sector size */
|
||||
|
||||
#if _MAX_SS == 512
|
||||
#define SS(fs) 512
|
||||
#else
|
||||
#if _MAX_SS == 1024 || _MAX_SS == 2048 || _MAX_SS == 4096
|
||||
#define SS(fs) ((fs)->s_size)
|
||||
#else
|
||||
#error Sector size must be 512, 1024, 2048 or 4096.
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* File system object structure */
|
||||
|
||||
typedef struct _FATFS {
|
||||
BYTE fs_type; /* FAT sub type */
|
||||
BYTE drive; /* Physical drive number */
|
||||
BYTE csize; /* Number of sectors per cluster */
|
||||
BYTE n_fats; /* Number of FAT copies */
|
||||
BYTE wflag; /* win[] dirty flag (1:must be written back) */
|
||||
BYTE pad1;
|
||||
WORD id; /* File system mount ID */
|
||||
WORD n_rootdir; /* Number of root directory entries (0 on FAT32) */
|
||||
#if _FS_REENTRANT
|
||||
_SYNC_t sobj; /* Identifier of sync object */
|
||||
#endif
|
||||
#if _MAX_SS != 512U
|
||||
WORD s_size; /* Sector size */
|
||||
#endif
|
||||
#if !_FS_READONLY
|
||||
BYTE fsi_flag; /* fsinfo dirty flag (1:must be written back) */
|
||||
BYTE pad2;
|
||||
DWORD last_clust; /* Last allocated cluster */
|
||||
DWORD free_clust; /* Number of free clusters */
|
||||
DWORD fsi_sector; /* fsinfo sector */
|
||||
#endif
|
||||
DWORD sects_fat; /* Sectors per fat */
|
||||
DWORD max_clust; /* Maximum cluster# + 1. Number of clusters is max_clust - 2 */
|
||||
DWORD fatbase; /* FAT start sector */
|
||||
DWORD dirbase; /* Root directory start sector (Cluster# on FAT32) */
|
||||
DWORD database; /* Data start sector */
|
||||
DWORD winsect; /* Current sector appearing in the win[] */
|
||||
BYTE win[_MAX_SS];/* Disk access window for Directory/FAT */
|
||||
} FATFS;
|
||||
|
||||
|
||||
|
||||
/* Directory object structure */
|
||||
|
||||
typedef struct _DIR {
|
||||
WORD id; /* Owner file system mount ID */
|
||||
WORD index; /* Current index number */
|
||||
FATFS* fs; /* Pointer to the owner file system object */
|
||||
DWORD sclust; /* Table start cluster (0:Static table) */
|
||||
DWORD clust; /* Current cluster */
|
||||
DWORD sect; /* Current sector */
|
||||
BYTE* dir; /* Pointer to the current SFN entry in the win[] */
|
||||
BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
|
||||
#if _USE_LFN
|
||||
WCHAR* lfn; /* Pointer to the LFN working buffer */
|
||||
WORD lfn_idx; /* Last matched LFN index (0xFFFF:No LFN) */
|
||||
#endif
|
||||
} DIR;
|
||||
|
||||
|
||||
|
||||
/* File object structure */
|
||||
|
||||
typedef struct _FIL {
|
||||
FATFS* fs; /* Pointer to the owner file system object */
|
||||
WORD id; /* Owner file system mount ID */
|
||||
BYTE flag; /* File status flags */
|
||||
BYTE csect; /* Sector address in the cluster */
|
||||
DWORD fptr; /* File R/W pointer */
|
||||
DWORD fsize; /* File size */
|
||||
DWORD org_clust; /* File start cluster */
|
||||
DWORD curr_clust; /* Current cluster */
|
||||
DWORD dsect; /* Current data sector */
|
||||
#if !_FS_READONLY
|
||||
DWORD dir_sect; /* Sector containing the directory entry */
|
||||
BYTE* dir_ptr; /* Ponter to the directory entry in the window */
|
||||
#endif
|
||||
#if !_FS_TINY
|
||||
BYTE buf[_MAX_SS];/* File R/W buffer */
|
||||
#endif
|
||||
} FIL;
|
||||
|
||||
|
||||
|
||||
/* File status structure */
|
||||
|
||||
typedef struct _FILINFO {
|
||||
DWORD fsize; /* File size */
|
||||
WORD fdate; /* Last modified date */
|
||||
WORD ftime; /* Last modified time */
|
||||
BYTE fattrib; /* Attribute */
|
||||
char fname[13]; /* Short file name (8.3 format) */
|
||||
#if _USE_LFN
|
||||
char *lfname; /* Pointer to the LFN buffer */
|
||||
int lfsize; /* Size of LFN buffer [bytes] */
|
||||
#endif
|
||||
} FILINFO;
|
||||
|
||||
|
||||
|
||||
/* DBCS code ranges */
|
||||
|
||||
#if _CODE_PAGE == 932 /* CP932 (Japanese Shift-JIS) */
|
||||
#define _DF1S 0x81 /* DBC 1st byte range 1 start */
|
||||
#define _DF1E 0x9F /* DBC 1st byte range 1 end */
|
||||
#define _DF2S 0xE0 /* DBC 1st byte range 2 start */
|
||||
#define _DF2E 0xFC /* DBC 1st byte range 2 end */
|
||||
#define _DS1S 0x40 /* DBC 2nd byte range 1 start */
|
||||
#define _DS1E 0x7E /* DBC 2nd byte range 1 end */
|
||||
#define _DS2S 0x80 /* DBC 2nd byte range 2 start */
|
||||
#define _DS2E 0xFC /* DBC 2nd byte range 2 end */
|
||||
|
||||
#elif _CODE_PAGE == 936 /* CP936 (Simplified Chinese GBK) */
|
||||
#define _DF1S 0x81
|
||||
#define _DF1E 0xFE
|
||||
#define _DS1S 0x40
|
||||
#define _DS1E 0x7E
|
||||
#define _DS2S 0x80
|
||||
#define _DS2E 0xFE
|
||||
|
||||
#elif _CODE_PAGE == 949 /* CP949 (Korean) */
|
||||
#define _DF1S 0x81
|
||||
#define _DF1E 0xFE
|
||||
#define _DS1S 0x41
|
||||
#define _DS1E 0x5A
|
||||
#define _DS2S 0x61
|
||||
#define _DS2E 0x7A
|
||||
#define _DS3S 0x81
|
||||
#define _DS3E 0xFE
|
||||
|
||||
#elif _CODE_PAGE == 950 /* CP950 (Traditional Chinese Big5) */
|
||||
#define _DF1S 0x81
|
||||
#define _DF1E 0xFE
|
||||
#define _DS1S 0x40
|
||||
#define _DS1E 0x7E
|
||||
#define _DS2S 0xA1
|
||||
#define _DS2E 0xFE
|
||||
|
||||
#else /* SBCS code pages */
|
||||
#define _DF1S 0
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* Character code support macros */
|
||||
|
||||
#define IsUpper(c) (((c)>='A')&&((c)<='Z'))
|
||||
#define IsLower(c) (((c)>='a')&&((c)<='z'))
|
||||
#define IsDigit(c) (((c)>='0')&&((c)<='9'))
|
||||
|
||||
#if _DF1S /* DBCS configuration */
|
||||
|
||||
#if _DF2S /* Two 1st byte areas */
|
||||
#define IsDBCS1(c) (((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E) || ((BYTE)(c) >= _DF2S && (BYTE)(c) <= _DF2E))
|
||||
#else /* One 1st byte area */
|
||||
#define IsDBCS1(c) ((BYTE)(c) >= _DF1S && (BYTE)(c) <= _DF1E)
|
||||
#endif
|
||||
|
||||
#if _DS3S /* Three 2nd byte areas */
|
||||
#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E) || ((BYTE)(c) >= _DS3S && (BYTE)(c) <= _DS3E))
|
||||
#else /* Two 2nd byte areas */
|
||||
#define IsDBCS2(c) (((BYTE)(c) >= _DS1S && (BYTE)(c) <= _DS1E) || ((BYTE)(c) >= _DS2S && (BYTE)(c) <= _DS2E))
|
||||
#endif
|
||||
|
||||
#else /* SBCS configuration */
|
||||
|
||||
#define IsDBCS1(c) 0
|
||||
#define IsDBCS2(c) 0
|
||||
|
||||
#endif /* _DF1S */
|
||||
|
||||
|
||||
|
||||
/* Definitions corresponds to multi partition */
|
||||
|
||||
#if _MULTI_PARTITION /* Multiple partition configuration */
|
||||
|
||||
typedef struct _PARTITION {
|
||||
BYTE pd; /* Physical drive# */
|
||||
BYTE pt; /* Partition # (0-3) */
|
||||
} PARTITION;
|
||||
|
||||
extern
|
||||
const PARTITION Drives[]; /* Logical drive# to physical location conversion table */
|
||||
#define LD2PD(drv) (Drives[drv].pd) /* Get physical drive# */
|
||||
#define LD2PT(drv) (Drives[drv].pt) /* Get partition# */
|
||||
|
||||
#else /* Single partition configuration */
|
||||
|
||||
#define LD2PD(drv) (drv) /* Physical drive# is equal to the logical drive# */
|
||||
#define LD2PT(drv) 0 /* Always mounts the 1st partition */
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/* File function return code (FRESULT) */
|
||||
|
||||
typedef enum {
|
||||
FR_OK = 0, /* 0 */
|
||||
FR_DISK_ERR, /* 1 */
|
||||
FR_INT_ERR, /* 2 */
|
||||
FR_NOT_READY, /* 3 */
|
||||
FR_NO_FILE, /* 4 */
|
||||
FR_NO_PATH, /* 5 */
|
||||
FR_INVALID_NAME, /* 6 */
|
||||
FR_DENIED, /* 7 */
|
||||
FR_EXIST, /* 8 */
|
||||
FR_INVALID_OBJECT, /* 9 */
|
||||
FR_WRITE_PROTECTED, /* 10 */
|
||||
FR_INVALID_DRIVE, /* 11 */
|
||||
FR_NOT_ENABLED, /* 12 */
|
||||
FR_NO_FILESYSTEM, /* 13 */
|
||||
FR_MKFS_ABORTED, /* 14 */
|
||||
FR_TIMEOUT /* 15 */
|
||||
} FRESULT;
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------*/
|
||||
/* FatFs module application interface */
|
||||
|
||||
FRESULT f_mount (BYTE, FATFS*); /* Mount/Unmount a logical drive */
|
||||
FRESULT f_open (FIL*, const char*, BYTE); /* Open or create a file */
|
||||
FRESULT f_read (FIL*, void*, UINT, UINT*); /* Read data from a file */
|
||||
FRESULT f_write (FIL*, const void*, UINT, UINT*); /* Write data to a file */
|
||||
FRESULT f_lseek (FIL*, DWORD); /* Move file pointer of a file object */
|
||||
FRESULT f_close (FIL*); /* Close an open file object */
|
||||
FRESULT f_opendir (DIR*, const char*); /* Open an existing directory */
|
||||
FRESULT f_readdir (DIR*, FILINFO*); /* Read a directory item */
|
||||
FRESULT f_stat (const char*, FILINFO*); /* Get file status */
|
||||
FRESULT f_getfree (const char*, DWORD*, FATFS**); /* Get number of free clusters on the drive */
|
||||
FRESULT f_truncate (FIL*); /* Truncate file */
|
||||
FRESULT f_sync (FIL*); /* Flush cached data of a writing file */
|
||||
FRESULT f_unlink (const char*); /* Delete an existing file or directory */
|
||||
FRESULT f_mkdir (const char*); /* Create a new directory */
|
||||
FRESULT f_chmod (const char*, BYTE, BYTE); /* Change attriburte of the file/dir */
|
||||
FRESULT f_utime (const char*, const FILINFO*); /* Change timestamp of the file/dir */
|
||||
FRESULT f_rename (const char*, const char*); /* Rename/Move a file or directory */
|
||||
FRESULT f_forward (FIL*, UINT(*)(const BYTE*,UINT), UINT, UINT*); /* Forward data to the stream */
|
||||
FRESULT f_mkfs (BYTE, BYTE, WORD); /* Create a file system on the drive */
|
||||
|
||||
#if _USE_STRFUNC
|
||||
int f_putc (int, FIL*); /* Put a character to the file */
|
||||
int f_puts (const char*, FIL*); /* Put a string to the file */
|
||||
int f_printf (FIL*, const char*, ...); /* Put a formatted string to the file */
|
||||
char* f_gets (char*, int, FIL*); /* Get a string from the file */
|
||||
#define f_eof(fp) (((fp)->fptr == (fp)->fsize) ? 1 : 0)
|
||||
#define f_error(fp) (((fp)->flag & FA__ERROR) ? 1 : 0)
|
||||
#ifndef EOF
|
||||
#define EOF -1
|
||||
#endif
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------*/
|
||||
/* User defined functions */
|
||||
|
||||
/* Real time clock */
|
||||
#if !_FS_READONLY
|
||||
DWORD get_fattime (void); /* 31-25: Year(0-127 org.1980), 24-21: Month(1-12), 20-16: Day(1-31) */
|
||||
/* 15-11: Hour(0-23), 10-5: Minute(0-59), 4-0: Second(0-29 *2) */
|
||||
#endif
|
||||
|
||||
/* Unicode - OEM code conversion */
|
||||
#if _USE_LFN
|
||||
WCHAR ff_convert (WCHAR, UINT);
|
||||
#endif
|
||||
|
||||
/* Sync functions */
|
||||
#if _FS_REENTRANT
|
||||
BOOL ff_cre_syncobj(BYTE, _SYNC_t*);
|
||||
BOOL ff_del_syncobj(_SYNC_t);
|
||||
BOOL ff_req_grant(_SYNC_t);
|
||||
void ff_rel_grant(_SYNC_t);
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------*/
|
||||
/* Flags and offset address */
|
||||
|
||||
|
||||
/* File access control and file status flags (FIL.flag) */
|
||||
|
||||
#define FA_READ 0x01
|
||||
#define FA_OPEN_EXISTING 0x00
|
||||
#if _FS_READONLY == 0
|
||||
#define FA_WRITE 0x02
|
||||
#define FA_CREATE_NEW 0x04
|
||||
#define FA_CREATE_ALWAYS 0x08
|
||||
#define FA_OPEN_ALWAYS 0x10
|
||||
#define FA__WRITTEN 0x20
|
||||
#define FA__DIRTY 0x40
|
||||
#endif
|
||||
#define FA__ERROR 0x80
|
||||
|
||||
|
||||
/* FAT sub type (FATFS.fs_type) */
|
||||
|
||||
#define FS_FAT12 1
|
||||
#define FS_FAT16 2
|
||||
#define FS_FAT32 3
|
||||
|
||||
|
||||
/* File attribute bits for directory entry */
|
||||
|
||||
#define AM_RDO 0x01 /* Read only */
|
||||
#define AM_HID 0x02 /* Hidden */
|
||||
#define AM_SYS 0x04 /* System */
|
||||
#define AM_VOL 0x08 /* Volume label */
|
||||
#define AM_LFN 0x0F /* LFN entry */
|
||||
#define AM_DIR 0x10 /* Directory */
|
||||
#define AM_ARC 0x20 /* Archive */
|
||||
#define AM_MASK 0x3F /* Mask of defined bits */
|
||||
|
||||
|
||||
/* FatFs refers the members in the FAT structures with byte offset instead
|
||||
/ of structure member because there are incompatibility of the packing option
|
||||
/ between various compilers. */
|
||||
|
||||
#define BS_jmpBoot 0
|
||||
#define BS_OEMName 3
|
||||
#define BPB_BytsPerSec 11
|
||||
#define BPB_SecPerClus 13
|
||||
#define BPB_RsvdSecCnt 14
|
||||
#define BPB_NumFATs 16
|
||||
#define BPB_RootEntCnt 17
|
||||
#define BPB_TotSec16 19
|
||||
#define BPB_Media 21
|
||||
#define BPB_FATSz16 22
|
||||
#define BPB_SecPerTrk 24
|
||||
#define BPB_NumHeads 26
|
||||
#define BPB_HiddSec 28
|
||||
#define BPB_TotSec32 32
|
||||
#define BS_55AA 510
|
||||
|
||||
#define BS_DrvNum 36
|
||||
#define BS_BootSig 38
|
||||
#define BS_VolID 39
|
||||
#define BS_VolLab 43
|
||||
#define BS_FilSysType 54
|
||||
|
||||
#define BPB_FATSz32 36
|
||||
#define BPB_ExtFlags 40
|
||||
#define BPB_FSVer 42
|
||||
#define BPB_RootClus 44
|
||||
#define BPB_FSInfo 48
|
||||
#define BPB_BkBootSec 50
|
||||
#define BS_DrvNum32 64
|
||||
#define BS_BootSig32 66
|
||||
#define BS_VolID32 67
|
||||
#define BS_VolLab32 71
|
||||
#define BS_FilSysType32 82
|
||||
|
||||
#define FSI_LeadSig 0
|
||||
#define FSI_StrucSig 484
|
||||
#define FSI_Free_Count 488
|
||||
#define FSI_Nxt_Free 492
|
||||
|
||||
#define MBR_Table 446
|
||||
|
||||
#define DIR_Name 0
|
||||
#define DIR_Attr 11
|
||||
#define DIR_NTres 12
|
||||
#define DIR_CrtTime 14
|
||||
#define DIR_CrtDate 16
|
||||
#define DIR_FstClusHI 20
|
||||
#define DIR_WrtTime 22
|
||||
#define DIR_WrtDate 24
|
||||
#define DIR_FstClusLO 26
|
||||
#define DIR_FileSize 28
|
||||
#define LDIR_Ord 0
|
||||
#define LDIR_Attr 11
|
||||
#define LDIR_Type 12
|
||||
#define LDIR_Chksum 13
|
||||
#define LDIR_FstClusLO 26
|
||||
|
||||
|
||||
|
||||
/*--------------------------------*/
|
||||
/* Multi-byte word access macros */
|
||||
|
||||
#if _WORD_ACCESS == 1 /* Enable word access to the FAT structure */
|
||||
#define LD_WORD(ptr) (WORD)(*(WORD*)(BYTE*)(ptr))
|
||||
#define LD_DWORD(ptr) (DWORD)(*(DWORD*)(BYTE*)(ptr))
|
||||
#define ST_WORD(ptr,val) *(WORD*)(BYTE*)(ptr)=(WORD)(val)
|
||||
#define ST_DWORD(ptr,val) *(DWORD*)(BYTE*)(ptr)=(DWORD)(val)
|
||||
#else /* Use byte-by-byte access to the FAT structure */
|
||||
#define LD_WORD(ptr) (WORD)(((WORD)*(BYTE*)((ptr)+1)<<8)|(WORD)*(BYTE*)(ptr))
|
||||
#define LD_DWORD(ptr) (DWORD)(((DWORD)*(BYTE*)((ptr)+3)<<24)|((DWORD)*(BYTE*)((ptr)+2)<<16)|((WORD)*(BYTE*)((ptr)+1)<<8)|*(BYTE*)(ptr))
|
||||
#define ST_WORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8)
|
||||
#define ST_DWORD(ptr,val) *(BYTE*)(ptr)=(BYTE)(val); *(BYTE*)((ptr)+1)=(BYTE)((WORD)(val)>>8); *(BYTE*)((ptr)+2)=(BYTE)((DWORD)(val)>>16); *(BYTE*)((ptr)+3)=(BYTE)((DWORD)(val)>>24)
|
||||
#endif
|
||||
|
||||
|
||||
#endif /* _FATFS */
|
||||
37
tools/ffsample/avr/integer.h
Normal file
37
tools/ffsample/avr/integer.h
Normal file
@@ -0,0 +1,37 @@
|
||||
/*-------------------------------------------*/
|
||||
/* Integer type definitions for FatFs module */
|
||||
/*-------------------------------------------*/
|
||||
|
||||
#ifndef _INTEGER
|
||||
|
||||
#if 0
|
||||
#include <windows.h>
|
||||
#else
|
||||
|
||||
/* These types must be 16-bit, 32-bit or larger integer */
|
||||
typedef int INT;
|
||||
typedef unsigned int UINT;
|
||||
|
||||
/* These types must be 8-bit integer */
|
||||
typedef signed char CHAR;
|
||||
typedef unsigned char UCHAR;
|
||||
typedef unsigned char BYTE;
|
||||
|
||||
/* These types must be 16-bit integer */
|
||||
typedef short SHORT;
|
||||
typedef unsigned short USHORT;
|
||||
typedef unsigned short WORD;
|
||||
typedef unsigned short WCHAR;
|
||||
|
||||
/* These types must be 32-bit integer */
|
||||
typedef long LONG;
|
||||
typedef unsigned long ULONG;
|
||||
typedef unsigned long DWORD;
|
||||
|
||||
/* Boolean type */
|
||||
typedef enum { FALSE = 0, TRUE } BOOL;
|
||||
|
||||
#endif
|
||||
|
||||
#define _INTEGER
|
||||
#endif
|
||||
556
tools/ffsample/avr/main.c
Normal file
556
tools/ffsample/avr/main.c
Normal file
@@ -0,0 +1,556 @@
|
||||
/*----------------------------------------------------------------------*/
|
||||
/* FAT file system sample project for FatFs R0.06 (C)ChaN, 2008 */
|
||||
/*----------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/pgmspace.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include <string.h>
|
||||
#include "uart.h"
|
||||
#include "xitoa.h"
|
||||
#include "ff.h"
|
||||
#include "diskio.h"
|
||||
#include "rtc.h"
|
||||
|
||||
|
||||
|
||||
DWORD acc_size; /* Work register for fs command */
|
||||
WORD acc_files, acc_dirs;
|
||||
FILINFO finfo;
|
||||
|
||||
BYTE line[120]; /* Console input buffer */
|
||||
|
||||
FATFS fatfs[2]; /* File system object for each logical drive */
|
||||
BYTE Buff[1024]; /* Working buffer */
|
||||
|
||||
|
||||
volatile WORD Timer; /* 100Hz increment timer */
|
||||
|
||||
|
||||
|
||||
#if _MULTI_PARTITION != 0
|
||||
const PARTITION Drives[] = { {0,0}, {0,1} };
|
||||
#endif
|
||||
|
||||
/*---------------------------------------------------------*/
|
||||
/* 100Hz timer interrupt generated by OC2 */
|
||||
/*---------------------------------------------------------*/
|
||||
|
||||
|
||||
ISR(TIMER2_COMP_vect)
|
||||
{
|
||||
Timer++; /* Performance counter for this module */
|
||||
disk_timerproc(); /* Drive timer procedure of low level disk I/O module */
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*---------------------------------------------------------*/
|
||||
/* User Provided Timer Function for FatFs module */
|
||||
/*---------------------------------------------------------*/
|
||||
/* This is a real time clock service to be called from */
|
||||
/* FatFs module. Any valid time must be returned even if */
|
||||
/* the system does not support a real time clock. */
|
||||
/* This is not required in read-only configuration. */
|
||||
|
||||
|
||||
DWORD get_fattime ()
|
||||
{
|
||||
RTC rtc;
|
||||
|
||||
|
||||
rtc_gettime(&rtc);
|
||||
|
||||
return ((DWORD)(rtc.year - 1980) << 25)
|
||||
| ((DWORD)rtc.month << 21)
|
||||
| ((DWORD)rtc.mday << 16)
|
||||
| ((DWORD)rtc.hour << 11)
|
||||
| ((DWORD)rtc.min << 5)
|
||||
| ((DWORD)rtc.sec >> 1);
|
||||
}
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------------*/
|
||||
/* Monitor */
|
||||
|
||||
|
||||
static
|
||||
void put_dump (const BYTE *buff, uint32_t ofs, BYTE cnt)
|
||||
{
|
||||
BYTE n;
|
||||
|
||||
|
||||
xprintf(PSTR("%08lX "), ofs);
|
||||
for(n = 0; n < cnt; n++)
|
||||
xprintf(PSTR(" %02X"), buff[n]);
|
||||
xputc(' ');
|
||||
for(n = 0; n < cnt; n++) {
|
||||
if ((buff[n] < 0x20)||(buff[n] >= 0x7F))
|
||||
xputc('.');
|
||||
else
|
||||
xputc(buff[n]);
|
||||
}
|
||||
xputc('\n');
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
void get_line (char *buff, int len)
|
||||
{
|
||||
char c;
|
||||
int idx = 0;
|
||||
|
||||
|
||||
for (;;) {
|
||||
c = uart_get();
|
||||
if (c == '\r') break;
|
||||
if ((c == '\b') && idx) {
|
||||
idx--; uart_put(c);
|
||||
}
|
||||
if (((BYTE)c >= ' ') && (idx < len - 1)) {
|
||||
buff[idx++] = c; uart_put(c);
|
||||
}
|
||||
}
|
||||
buff[idx] = 0;
|
||||
uart_put(c);
|
||||
uart_put('\n');
|
||||
}
|
||||
|
||||
|
||||
static
|
||||
FRESULT scan_files (char* path)
|
||||
{
|
||||
DIR dirs;
|
||||
FRESULT res;
|
||||
int i;
|
||||
|
||||
if ((res = f_opendir(&dirs, path)) == FR_OK) {
|
||||
i = strlen(path);
|
||||
while (((res = f_readdir(&dirs, &finfo)) == FR_OK) && finfo.fname[0]) {
|
||||
if (finfo.fattrib & AM_DIR) {
|
||||
acc_dirs++;
|
||||
*(path+i) = '/'; strcpy(path+i+1, &finfo.fname[0]);
|
||||
res = scan_files(path);
|
||||
*(path+i) = '\0';
|
||||
if (res != FR_OK) break;
|
||||
} else {
|
||||
acc_files++;
|
||||
acc_size += finfo.fsize;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
static
|
||||
void put_rc (FRESULT rc)
|
||||
{
|
||||
const prog_char *p;
|
||||
static const prog_char str[] =
|
||||
"OK\0" "DISK_ERR\0" "INT_ERR\0" "NOT_READY\0" "NO_FILE\0" "NO_PATH\0"
|
||||
"INVALID_NAME\0" "DENIED\0" "EXIST\0" "INVALID_OBJECT\0" "WRITE_PROTECTED\0"
|
||||
"INVALID_DRIVE\0" "NOT_ENABLED\0" "NO_FILE_SYSTEM\0" "MKFS_ABORTED\0" "TIMEOUT\0";
|
||||
FRESULT i;
|
||||
|
||||
for (p = str, i = 0; i != rc && pgm_read_byte_near(p); i++) {
|
||||
while(pgm_read_byte_near(p++));
|
||||
}
|
||||
xprintf(PSTR("rc=%u FR_%S\n"), (WORD)rc, p);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
static
|
||||
void IoInit ()
|
||||
{
|
||||
PORTA = 0b11111111; // Port A
|
||||
|
||||
PORTB = 0b10110000; // Port B
|
||||
DDRB = 0b11000000;
|
||||
|
||||
PORTC = 0b11111111; // Port C
|
||||
|
||||
PORTD = 0b11111111; // Port D
|
||||
|
||||
PORTE = 0b11110010; // Port E
|
||||
DDRE = 0b10000010;
|
||||
|
||||
PORTF = 0b11111111; // Port F
|
||||
|
||||
PORTG = 0b11111; // Port G
|
||||
|
||||
uart_init(); // Initialize UART driver
|
||||
|
||||
/*
|
||||
OCR1A = 51; // Timer1: LCD bias generator (OC1B)
|
||||
OCR1B = 51;
|
||||
TCCR1A = 0b00010000;
|
||||
TCCR1B = 0b00001010;
|
||||
*/
|
||||
OCR2 = 90-1; // Timer2: 100Hz interval (OC2)
|
||||
TCCR2 = 0b00001101;
|
||||
|
||||
TIMSK = 0b10000000; // Enable TC2.oc, interrupt
|
||||
|
||||
rtc_init(); // Initialize RTC
|
||||
|
||||
sei();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Main */
|
||||
|
||||
|
||||
int main (void)
|
||||
{
|
||||
char *ptr, *ptr2;
|
||||
DWORD p1, p2, p3;
|
||||
BYTE res, b1;
|
||||
WORD w1;
|
||||
UINT s1, s2, cnt;
|
||||
DWORD ofs, sect = 0;
|
||||
RTC rtc;
|
||||
FATFS *fs;
|
||||
DIR dir; /* Directory object */
|
||||
FIL file1, file2; /* File object */
|
||||
|
||||
|
||||
IoInit();
|
||||
|
||||
/* Join xitoa module to uart module */
|
||||
xfunc_out = (void (*)(char))uart_put;
|
||||
|
||||
xputs(PSTR("FatFs module test monitor\n"));
|
||||
|
||||
for (;;) {
|
||||
xputc('>');
|
||||
get_line(line, sizeof(line));
|
||||
ptr = line;
|
||||
|
||||
switch (*ptr++) {
|
||||
|
||||
case 'd' :
|
||||
switch (*ptr++) {
|
||||
case 'd' : /* dd <phy_drv#> [<sector>] - Dump secrtor */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
if (!xatoi(&ptr, &p2)) p2 = sect;
|
||||
res = disk_read((BYTE)p1, Buff, p2, 1);
|
||||
if (res) { xprintf(PSTR("rc=%d\n"), (WORD)res); break; }
|
||||
sect = p2 + 1;
|
||||
xprintf(PSTR("Sector:%lu\n"), p2);
|
||||
for (ptr=Buff, ofs = 0; ofs < 0x200; ptr+=16, ofs+=16)
|
||||
put_dump(ptr, ofs, 16);
|
||||
break;
|
||||
|
||||
case 'i' : /* di <phy_drv#> - Initialize disk */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
xprintf(PSTR("rc=%d\n"), (WORD)disk_initialize((BYTE)p1));
|
||||
break;
|
||||
|
||||
case 's' : /* ds <phy_drv#> - Show disk status */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
if (disk_ioctl((BYTE)p1, GET_SECTOR_COUNT, &p2) == RES_OK)
|
||||
{ xprintf(PSTR("Drive size: %lu sectors\n"), p2); }
|
||||
if (disk_ioctl((BYTE)p1, GET_SECTOR_SIZE, &w1) == RES_OK)
|
||||
{ xprintf(PSTR("Sector size: %u\n"), w1); }
|
||||
if (disk_ioctl((BYTE)p1, GET_BLOCK_SIZE, &p2) == RES_OK)
|
||||
{ xprintf(PSTR("Erase block size: %lu sectors\n"), p2); }
|
||||
if (disk_ioctl((BYTE)p1, MMC_GET_TYPE, &b1) == RES_OK)
|
||||
{ xprintf(PSTR("Card type: %u\n"), b1); }
|
||||
if (disk_ioctl((BYTE)p1, MMC_GET_CSD, Buff) == RES_OK)
|
||||
{ xputs(PSTR("CSD:\n")); put_dump(Buff, 0, 16); }
|
||||
if (disk_ioctl((BYTE)p1, MMC_GET_CID, Buff) == RES_OK)
|
||||
{ xputs(PSTR("CID:\n")); put_dump(Buff, 0, 16); }
|
||||
if (disk_ioctl((BYTE)p1, MMC_GET_OCR, Buff) == RES_OK)
|
||||
{ xputs(PSTR("OCR:\n")); put_dump(Buff, 0, 4); }
|
||||
if (disk_ioctl((BYTE)p1, MMC_GET_SDSTAT, Buff) == RES_OK) {
|
||||
xputs(PSTR("SD Status:\n"));
|
||||
for (s1 = 0; s1 < 64; s1 += 16) put_dump(Buff+s1, s1, 16);
|
||||
}
|
||||
if (disk_ioctl((BYTE)p1, ATA_GET_MODEL, line) == RES_OK)
|
||||
{ line[40] = '\0'; xprintf(PSTR("Model: %s\n"), line); }
|
||||
if (disk_ioctl((BYTE)p1, ATA_GET_SN, line) == RES_OK)
|
||||
{ line[20] = '\0'; xprintf(PSTR("S/N: %s\n"), line); }
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'b' :
|
||||
switch (*ptr++) {
|
||||
case 'd' : /* bd <addr> - Dump R/W buffer */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
for (ptr=&Buff[p1], ofs = p1, cnt = 32; cnt; cnt--, ptr+=16, ofs+=16)
|
||||
put_dump(ptr, ofs, 16);
|
||||
break;
|
||||
|
||||
case 'e' : /* be <addr> [<data>] ... - Edit R/W buffer */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
if (xatoi(&ptr, &p2)) {
|
||||
do {
|
||||
Buff[p1++] = (BYTE)p2;
|
||||
} while (xatoi(&ptr, &p2));
|
||||
break;
|
||||
}
|
||||
for (;;) {
|
||||
xprintf(PSTR("%04X %02X-"), (WORD)(p1), (WORD)Buff[p1]);
|
||||
get_line(line, sizeof(line));
|
||||
ptr = line;
|
||||
if (*ptr == '.') break;
|
||||
if (*ptr < ' ') { p1++; continue; }
|
||||
if (xatoi(&ptr, &p2))
|
||||
Buff[p1++] = (BYTE)p2;
|
||||
else
|
||||
xputs(PSTR("???\n"));
|
||||
}
|
||||
break;
|
||||
|
||||
case 'r' : /* br <phy_drv#> <sector> [<n>] - Read disk into R/W buffer */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
if (!xatoi(&ptr, &p2)) break;
|
||||
if (!xatoi(&ptr, &p3)) p3 = 1;
|
||||
xprintf(PSTR("rc=%u\n"), (WORD)disk_read((BYTE)p1, Buff, p2, p3));
|
||||
break;
|
||||
|
||||
case 'w' : /* bw <phy_drv#> <sector> [<n>] - Write R/W buffer into disk */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
if (!xatoi(&ptr, &p2)) break;
|
||||
if (!xatoi(&ptr, &p3)) p3 = 1;
|
||||
xprintf(PSTR("rc=%u\n"), (WORD)disk_write((BYTE)p1, Buff, p2, p3));
|
||||
break;
|
||||
|
||||
case 'f' : /* bf <n> - Fill working buffer */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
memset(Buff, (BYTE)p1, sizeof(Buff));
|
||||
break;
|
||||
|
||||
}
|
||||
break;
|
||||
|
||||
case 'f' :
|
||||
switch (*ptr++) {
|
||||
|
||||
case 'i' : /* fi <log drv#> - Initialize logical drive */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
put_rc(f_mount((BYTE)p1, &fatfs[p1]));
|
||||
break;
|
||||
|
||||
case 's' : /* fs [<path>] - Show logical drive status */
|
||||
while (*ptr == ' ') ptr++;
|
||||
res = f_getfree(ptr, &p2, &fs);
|
||||
if (res) { put_rc(res); break; }
|
||||
xprintf(PSTR("FAT type = %u\nBytes/Cluster = %lu\nNumber of FATs = %u\n"
|
||||
"Root DIR entries = %u\nSectors/FAT = %lu\nNumber of clusters = %lu\n"
|
||||
"FAT start (lba) = %lu\nDIR start (lba,clustor) = %lu\nData start (lba) = %lu\n"),
|
||||
(WORD)fs->fs_type, (DWORD)fs->csize * 512, (WORD)fs->n_fats,
|
||||
fs->n_rootdir, (DWORD)fs->sects_fat, (DWORD)fs->max_clust - 2,
|
||||
fs->fatbase, fs->dirbase, fs->database
|
||||
);
|
||||
acc_size = acc_files = acc_dirs = 0;
|
||||
res = scan_files(ptr);
|
||||
if (res) { put_rc(res); break; }
|
||||
xprintf(PSTR("%u files, %lu bytes.\n%u folders.\n"
|
||||
"%lu KB total disk space.\n%lu KB available.\n"),
|
||||
acc_files, acc_size, acc_dirs,
|
||||
(fs->max_clust - 2) * (fs->csize / 2), p2 * (fs->csize / 2)
|
||||
);
|
||||
break;
|
||||
|
||||
case 'l' : /* fl [<path>] - Directory listing */
|
||||
while (*ptr == ' ') ptr++;
|
||||
res = f_opendir(&dir, ptr);
|
||||
if (res) { put_rc(res); break; }
|
||||
p1 = s1 = s2 = 0;
|
||||
for(;;) {
|
||||
res = f_readdir(&dir, &finfo);
|
||||
if ((res != FR_OK) || !finfo.fname[0]) break;
|
||||
if (finfo.fattrib & AM_DIR) {
|
||||
s2++;
|
||||
} else {
|
||||
s1++; p1 += finfo.fsize;
|
||||
}
|
||||
xprintf(PSTR("%c%c%c%c%c %u/%02u/%02u %02u:%02u %9lu %s\n"),
|
||||
(finfo.fattrib & AM_DIR) ? 'D' : '-',
|
||||
(finfo.fattrib & AM_RDO) ? 'R' : '-',
|
||||
(finfo.fattrib & AM_HID) ? 'H' : '-',
|
||||
(finfo.fattrib & AM_SYS) ? 'S' : '-',
|
||||
(finfo.fattrib & AM_ARC) ? 'A' : '-',
|
||||
(finfo.fdate >> 9) + 1980, (finfo.fdate >> 5) & 15, finfo.fdate & 31,
|
||||
(finfo.ftime >> 11), (finfo.ftime >> 5) & 63,
|
||||
finfo.fsize, &(finfo.fname[0]));
|
||||
}
|
||||
xprintf(PSTR("%4u File(s),%10lu bytes total\n%4u Dir(s)"), s1, p1, s2);
|
||||
if (f_getfree(ptr, &p1, &fs) == FR_OK)
|
||||
xprintf(PSTR(", %10luK bytes free\n"), p1 * fs->csize / 2);
|
||||
break;
|
||||
|
||||
case 'o' : /* fo <mode> <name> - Open a file */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
while (*ptr == ' ') ptr++;
|
||||
put_rc(f_open(&file1, ptr, (BYTE)p1));
|
||||
break;
|
||||
|
||||
case 'c' : /* fc - Close a file */
|
||||
put_rc(f_close(&file1));
|
||||
break;
|
||||
|
||||
case 'e' : /* fe - Seek file pointer */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
res = f_lseek(&file1, p1);
|
||||
put_rc(res);
|
||||
if (res == FR_OK)
|
||||
xprintf(PSTR("fptr = %lu(0x%lX)\n"), file1.fptr, file1.fptr);
|
||||
break;
|
||||
|
||||
case 'r' : /* fr <len> - read file */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
p2 = 0;
|
||||
Timer = 0;
|
||||
while (p1) {
|
||||
if (p1 >= sizeof(Buff)) { cnt = sizeof(Buff); p1 -= sizeof(Buff); }
|
||||
else { cnt = (WORD)p1; p1 = 0; }
|
||||
res = f_read(&file1, Buff, cnt, &s2);
|
||||
if (res != FR_OK) { put_rc(res); break; }
|
||||
p2 += s2;
|
||||
if (cnt != s2) break;
|
||||
}
|
||||
s2 = Timer;
|
||||
xprintf(PSTR("%lu bytes read with %lu bytes/sec.\n"), p2, p2 * 100 / s2);
|
||||
break;
|
||||
|
||||
case 'd' : /* fd <len> - read and dump file from current fp */
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
ofs = file1.fptr;
|
||||
while (p1) {
|
||||
if (p1 >= 16) { cnt = 16; p1 -= 16; }
|
||||
else { cnt = (WORD)p1; p1 = 0; }
|
||||
res = f_read(&file1, Buff, cnt, &cnt);
|
||||
if (res != FR_OK) { put_rc(res); break; }
|
||||
if (!cnt) break;
|
||||
put_dump(Buff, ofs, cnt);
|
||||
ofs += 16;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'w' : /* fw <len> <val> - write file */
|
||||
if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2)) break;
|
||||
memset(Buff, (BYTE)p2, sizeof(Buff));
|
||||
p2 = 0;
|
||||
Timer = 0;
|
||||
while (p1) {
|
||||
if (p1 >= sizeof(Buff)) { cnt = sizeof(Buff); p1 -= sizeof(Buff); }
|
||||
else { cnt = (WORD)p1; p1 = 0; }
|
||||
res = f_write(&file1, Buff, cnt, &s2);
|
||||
if (res != FR_OK) { put_rc(res); break; }
|
||||
p2 += s2;
|
||||
if (cnt != s2) break;
|
||||
}
|
||||
s2 = Timer;
|
||||
xprintf(PSTR("%lu bytes written with %lu bytes/sec.\n"), p2, p2 * 100 / s2);
|
||||
break;
|
||||
|
||||
case 'v' : /* fv - Truncate file */
|
||||
put_rc(f_truncate(&file1));
|
||||
break;
|
||||
|
||||
case 'n' : /* fn <old_name> <new_name> - Change file/dir name */
|
||||
while (*ptr == ' ') ptr++;
|
||||
ptr2 = strchr(ptr, ' ');
|
||||
if (!ptr2) break;
|
||||
*ptr2++ = 0;
|
||||
while (*ptr2 == ' ') ptr2++;
|
||||
put_rc(f_rename(ptr, ptr2));
|
||||
break;
|
||||
|
||||
case 'u' : /* fu <name> - Unlink a file or dir */
|
||||
while (*ptr == ' ') ptr++;
|
||||
put_rc(f_unlink(ptr));
|
||||
break;
|
||||
|
||||
case 'k' : /* fk <name> - Create a directory */
|
||||
while (*ptr == ' ') ptr++;
|
||||
put_rc(f_mkdir(ptr));
|
||||
break;
|
||||
|
||||
case 'a' : /* fa <atrr> <mask> <name> - Change file/dir attribute */
|
||||
if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2)) break;
|
||||
while (*ptr == ' ') ptr++;
|
||||
put_rc(f_chmod(ptr, p1, p2));
|
||||
break;
|
||||
|
||||
case 't' : /* ft <year> <month> <day> <hour> <min> <sec> <name> */
|
||||
if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break;
|
||||
finfo.fdate = ((p1 - 1980) << 9) | ((p2 & 15) << 5) | (p3 & 31);
|
||||
if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break;
|
||||
finfo.ftime = ((p1 & 31) << 11) | ((p1 & 63) << 5) | ((p1 >> 1) & 31);
|
||||
put_rc(f_utime(ptr, &finfo));
|
||||
break;
|
||||
|
||||
case 'x' : /* fx <src_name> <dst_name> - Copy file */
|
||||
while (*ptr == ' ') ptr++;
|
||||
ptr2 = strchr(ptr, ' ');
|
||||
if (!ptr2) break;
|
||||
*ptr2++ = 0;
|
||||
xprintf(PSTR("Opening \"%s\""), ptr);
|
||||
res = f_open(&file1, ptr, FA_OPEN_EXISTING | FA_READ);
|
||||
if (res) {
|
||||
put_rc(res);
|
||||
break;
|
||||
}
|
||||
xprintf(PSTR("\nCreating \"%s\""), ptr2);
|
||||
res = f_open(&file2, ptr2, FA_CREATE_ALWAYS | FA_WRITE);
|
||||
if (res) {
|
||||
put_rc(res);
|
||||
f_close(&file1);
|
||||
break;
|
||||
}
|
||||
xprintf(PSTR("\nCopying..."));
|
||||
p1 = 0;
|
||||
for (;;) {
|
||||
res = f_read(&file1, Buff, sizeof(Buff), &s1);
|
||||
if (res || s1 == 0) break; /* error or eof */
|
||||
res = f_write(&file2, Buff, s1, &s2);
|
||||
p1 += s2;
|
||||
if (res || s2 < s1) break; /* error or disk full */
|
||||
}
|
||||
if (res) put_rc(res);
|
||||
xprintf(PSTR("\n%lu bytes copied.\n"), p1);
|
||||
f_close(&file1);
|
||||
f_close(&file2);
|
||||
break;
|
||||
#if _USE_MKFS
|
||||
case 'm' : /* fm <logi drv#> <part type> <bytes/clust> - Create file system */
|
||||
if (!xatoi(&ptr, &p1) || !xatoi(&ptr, &p2) || !xatoi(&ptr, &p3)) break;
|
||||
xprintf(PSTR("The drive %u will be formatted. Are you sure? (Y/n)="), (WORD)p1);
|
||||
get_line(ptr, sizeof(line));
|
||||
if (*ptr == 'Y') put_rc(f_mkfs((BYTE)p1, (BYTE)p2, (WORD)p3));
|
||||
break;
|
||||
#endif
|
||||
}
|
||||
break;
|
||||
|
||||
case 't' : /* t [<year> <mon> <mday> <hour> <min> <sec>] */
|
||||
if (xatoi(&ptr, &p1)) {
|
||||
rtc.year = (WORD)p1;
|
||||
xatoi(&ptr, &p1); rtc.month = (BYTE)p1;
|
||||
xatoi(&ptr, &p1); rtc.mday = (BYTE)p1;
|
||||
xatoi(&ptr, &p1); rtc.hour = (BYTE)p1;
|
||||
xatoi(&ptr, &p1); rtc.min = (BYTE)p1;
|
||||
if (!xatoi(&ptr, &p1)) break;
|
||||
rtc.sec = (BYTE)p1;
|
||||
rtc_settime(&rtc);
|
||||
}
|
||||
rtc_gettime(&rtc);
|
||||
xprintf(PSTR("%u/%u/%u %02u:%02u:%02u\n"), rtc.year, rtc.month, rtc.mday, rtc.hour, rtc.min, rtc.sec);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
600
tools/ffsample/avr/mmc.c
Normal file
600
tools/ffsample/avr/mmc.c
Normal file
@@ -0,0 +1,600 @@
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* MMC/SDSC/SDHC (in SPI mode) control module (C)ChaN, 2007 */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Only rcvr_spi(), xmit_spi(), disk_timerproc() and some macros */
|
||||
/* are platform dependent. */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
|
||||
#include <avr/io.h>
|
||||
#include "diskio.h"
|
||||
|
||||
|
||||
/* Definitions for MMC/SDC command */
|
||||
#define CMD0 (0x40+0) /* GO_IDLE_STATE */
|
||||
#define CMD1 (0x40+1) /* SEND_OP_COND (MMC) */
|
||||
#define ACMD41 (0xC0+41) /* SEND_OP_COND (SDC) */
|
||||
#define CMD8 (0x40+8) /* SEND_IF_COND */
|
||||
#define CMD9 (0x40+9) /* SEND_CSD */
|
||||
#define CMD10 (0x40+10) /* SEND_CID */
|
||||
#define CMD12 (0x40+12) /* STOP_TRANSMISSION */
|
||||
#define ACMD13 (0xC0+13) /* SD_STATUS (SDC) */
|
||||
#define CMD16 (0x40+16) /* SET_BLOCKLEN */
|
||||
#define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */
|
||||
#define CMD18 (0x40+18) /* READ_MULTIPLE_BLOCK */
|
||||
#define CMD23 (0x40+23) /* SET_BLOCK_COUNT (MMC) */
|
||||
#define ACMD23 (0xC0+23) /* SET_WR_BLK_ERASE_COUNT (SDC) */
|
||||
#define CMD24 (0x40+24) /* WRITE_BLOCK */
|
||||
#define CMD25 (0x40+25) /* WRITE_MULTIPLE_BLOCK */
|
||||
#define CMD55 (0x40+55) /* APP_CMD */
|
||||
#define CMD58 (0x40+58) /* READ_OCR */
|
||||
|
||||
|
||||
/* Port Controls (Platform dependent) */
|
||||
#define SELECT() PORTB &= ~1 /* MMC CS = L */
|
||||
#define DESELECT() PORTB |= 1 /* MMC CS = H */
|
||||
|
||||
#define SOCKPORT PINB /* Socket contact port */
|
||||
#define SOCKWP 0x20 /* Write protect switch (PB5) */
|
||||
#define SOCKINS 0x10 /* Card detect switch (PB4) */
|
||||
|
||||
#define FCLK_SLOW() /* Set slow clock (100k-400k) */
|
||||
#define FCLK_FAST() /* Set fast clock (depends on the CSD) */
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
Module Private Functions
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
static volatile
|
||||
DSTATUS Stat = STA_NOINIT; /* Disk status */
|
||||
|
||||
static volatile
|
||||
BYTE Timer1, Timer2; /* 100Hz decrement timer */
|
||||
|
||||
static
|
||||
BYTE CardType; /* Card type flags */
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Transmit a byte to MMC via SPI (Platform dependent) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
#define xmit_spi(dat) SPDR=(dat); loop_until_bit_is_set(SPSR,SPIF)
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Receive a byte from MMC via SPI (Platform dependent) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
BYTE rcvr_spi (void)
|
||||
{
|
||||
SPDR = 0xFF;
|
||||
loop_until_bit_is_set(SPSR, SPIF);
|
||||
return SPDR;
|
||||
}
|
||||
|
||||
/* Alternative macro to receive data fast */
|
||||
#define rcvr_spi_m(dst) SPDR=0xFF; loop_until_bit_is_set(SPSR,SPIF); *(dst)=SPDR
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Wait for card ready */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
BYTE wait_ready (void)
|
||||
{
|
||||
BYTE res;
|
||||
|
||||
|
||||
Timer2 = 50; /* Wait for ready in timeout of 500ms */
|
||||
rcvr_spi();
|
||||
do
|
||||
res = rcvr_spi();
|
||||
while ((res != 0xFF) && Timer2);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Deselect the card and release SPI bus */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
void release_spi (void)
|
||||
{
|
||||
DESELECT();
|
||||
rcvr_spi();
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Power Control (Platform dependent) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* When the target system does not support socket power control, there */
|
||||
/* is nothing to do in these functions and chk_power always returns 1. */
|
||||
|
||||
static
|
||||
void power_on (void)
|
||||
{
|
||||
PORTE &= ~0x80; /* Socket power ON */
|
||||
for (Timer1 = 3; Timer1; ); /* Wait for 30ms */
|
||||
PORTB = 0b10110101; /* Enable drivers */
|
||||
DDRB = 0b11000111;
|
||||
SPCR = 0b01010000; /* Initialize SPI port (Mode 0) */
|
||||
SPSR = 0b00000001;
|
||||
}
|
||||
|
||||
static
|
||||
void power_off (void)
|
||||
{
|
||||
SELECT(); /* Wait for card ready */
|
||||
wait_ready();
|
||||
release_spi();
|
||||
|
||||
SPCR = 0; /* Disable SPI function */
|
||||
DDRB = 0b11000000; /* Disable drivers */
|
||||
PORTB = 0b10110000;
|
||||
PORTE |= 0x80; /* Socket power OFF */
|
||||
Stat |= STA_NOINIT; /* Set STA_NOINIT */
|
||||
}
|
||||
|
||||
static
|
||||
int chk_power(void) /* Socket power state: 0=off, 1=on */
|
||||
{
|
||||
return (PORTE & 0x80) ? 0 : 1;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Receive a data packet from MMC */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
BOOL rcvr_datablock (
|
||||
BYTE *buff, /* Data buffer to store received data */
|
||||
UINT btr /* Byte count (must be multiple of 4) */
|
||||
)
|
||||
{
|
||||
BYTE token;
|
||||
|
||||
|
||||
Timer1 = 10;
|
||||
do { /* Wait for data packet in timeout of 100ms */
|
||||
token = rcvr_spi();
|
||||
} while ((token == 0xFF) && Timer1);
|
||||
if(token != 0xFE) return FALSE; /* If not valid data token, retutn with error */
|
||||
|
||||
do { /* Receive the data block into buffer */
|
||||
rcvr_spi_m(buff++);
|
||||
rcvr_spi_m(buff++);
|
||||
rcvr_spi_m(buff++);
|
||||
rcvr_spi_m(buff++);
|
||||
} while (btr -= 4);
|
||||
rcvr_spi(); /* Discard CRC */
|
||||
rcvr_spi();
|
||||
|
||||
return TRUE; /* Return with success */
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Send a data packet to MMC */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
#if _READONLY == 0
|
||||
static
|
||||
BOOL xmit_datablock (
|
||||
const BYTE *buff, /* 512 byte data block to be transmitted */
|
||||
BYTE token /* Data/Stop token */
|
||||
)
|
||||
{
|
||||
BYTE resp, wc;
|
||||
|
||||
|
||||
if (wait_ready() != 0xFF) return FALSE;
|
||||
|
||||
xmit_spi(token); /* Xmit data token */
|
||||
if (token != 0xFD) { /* Is data token */
|
||||
wc = 0;
|
||||
do { /* Xmit the 512 byte data block to MMC */
|
||||
xmit_spi(*buff++);
|
||||
xmit_spi(*buff++);
|
||||
} while (--wc);
|
||||
xmit_spi(0xFF); /* CRC (Dummy) */
|
||||
xmit_spi(0xFF);
|
||||
resp = rcvr_spi(); /* Reveive data response */
|
||||
if ((resp & 0x1F) != 0x05) /* If not accepted, return with error */
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
#endif /* _READONLY */
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Send a command packet to MMC */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
static
|
||||
BYTE send_cmd (
|
||||
BYTE cmd, /* Command byte */
|
||||
DWORD arg /* Argument */
|
||||
)
|
||||
{
|
||||
BYTE n, res;
|
||||
|
||||
|
||||
if (cmd & 0x80) { /* ACMD<n> is the command sequense of CMD55-CMD<n> */
|
||||
cmd &= 0x7F;
|
||||
res = send_cmd(CMD55, 0);
|
||||
if (res > 1) return res;
|
||||
}
|
||||
|
||||
/* Select the card and wait for ready */
|
||||
DESELECT();
|
||||
SELECT();
|
||||
if (wait_ready() != 0xFF) return 0xFF;
|
||||
|
||||
/* Send command packet */
|
||||
xmit_spi(cmd); /* Start + Command index */
|
||||
xmit_spi((BYTE)(arg >> 24)); /* Argument[31..24] */
|
||||
xmit_spi((BYTE)(arg >> 16)); /* Argument[23..16] */
|
||||
xmit_spi((BYTE)(arg >> 8)); /* Argument[15..8] */
|
||||
xmit_spi((BYTE)arg); /* Argument[7..0] */
|
||||
n = 0x01; /* Dummy CRC + Stop */
|
||||
if (cmd == CMD0) n = 0x95; /* Valid CRC for CMD0(0) */
|
||||
if (cmd == CMD8) n = 0x87; /* Valid CRC for CMD8(0x1AA) */
|
||||
xmit_spi(n);
|
||||
|
||||
/* Receive command response */
|
||||
if (cmd == CMD12) rcvr_spi(); /* Skip a stuff byte when stop reading */
|
||||
n = 10; /* Wait for a valid response in timeout of 10 attempts */
|
||||
do
|
||||
res = rcvr_spi();
|
||||
while ((res & 0x80) && --n);
|
||||
|
||||
return res; /* Return with the response value */
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*--------------------------------------------------------------------------
|
||||
|
||||
Public Functions
|
||||
|
||||
---------------------------------------------------------------------------*/
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Initialize Disk Drive */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DSTATUS disk_initialize (
|
||||
BYTE drv /* Physical drive nmuber (0) */
|
||||
)
|
||||
{
|
||||
BYTE n, cmd, ty, ocr[4];
|
||||
|
||||
|
||||
if (drv) return STA_NOINIT; /* Supports only single drive */
|
||||
if (Stat & STA_NODISK) return Stat; /* No card in the socket */
|
||||
|
||||
power_on(); /* Force socket power on */
|
||||
FCLK_SLOW();
|
||||
for (n = 10; n; n--) rcvr_spi(); /* 80 dummy clocks */
|
||||
|
||||
ty = 0;
|
||||
if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */
|
||||
Timer1 = 100; /* Initialization timeout of 1000 msec */
|
||||
if (send_cmd(CMD8, 0x1AA) == 1) { /* SDHC */
|
||||
for (n = 0; n < 4; n++) ocr[n] = rcvr_spi(); /* Get trailing return value of R7 resp */
|
||||
if (ocr[2] == 0x01 && ocr[3] == 0xAA) { /* The card can work at vdd range of 2.7-3.6V */
|
||||
while (Timer1 && send_cmd(ACMD41, 1UL << 30)); /* Wait for leaving idle state (ACMD41 with HCS bit) */
|
||||
if (Timer1 && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */
|
||||
for (n = 0; n < 4; n++) ocr[n] = rcvr_spi();
|
||||
ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2;
|
||||
}
|
||||
}
|
||||
} else { /* SDSC or MMC */
|
||||
if (send_cmd(ACMD41, 0) <= 1) {
|
||||
ty = CT_SD1; cmd = ACMD41; /* SDSC */
|
||||
} else {
|
||||
ty = CT_MMC; cmd = CMD1; /* MMC */
|
||||
}
|
||||
while (Timer1 && send_cmd(cmd, 0)); /* Wait for leaving idle state */
|
||||
if (!Timer1 || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */
|
||||
ty = 0;
|
||||
}
|
||||
}
|
||||
CardType = ty;
|
||||
release_spi();
|
||||
|
||||
if (ty) { /* Initialization succeded */
|
||||
Stat &= ~STA_NOINIT; /* Clear STA_NOINIT */
|
||||
FCLK_FAST();
|
||||
} else { /* Initialization failed */
|
||||
power_off();
|
||||
}
|
||||
|
||||
return Stat;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Get Disk Status */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DSTATUS disk_status (
|
||||
BYTE drv /* Physical drive nmuber (0) */
|
||||
)
|
||||
{
|
||||
if (drv) return STA_NOINIT; /* Supports only single drive */
|
||||
return Stat;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Read Sector(s) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
DRESULT disk_read (
|
||||
BYTE drv, /* Physical drive nmuber (0) */
|
||||
BYTE *buff, /* Pointer to the data buffer to store read data */
|
||||
DWORD sector, /* Start sector number (LBA) */
|
||||
BYTE count /* Sector count (1..255) */
|
||||
)
|
||||
{
|
||||
if (drv || !count) return RES_PARERR;
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
|
||||
if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */
|
||||
|
||||
if (count == 1) { /* Single block read */
|
||||
if ((send_cmd(CMD17, sector) == 0) /* READ_SINGLE_BLOCK */
|
||||
&& rcvr_datablock(buff, 512))
|
||||
count = 0;
|
||||
}
|
||||
else { /* Multiple block read */
|
||||
if (send_cmd(CMD18, sector) == 0) { /* READ_MULTIPLE_BLOCK */
|
||||
do {
|
||||
if (!rcvr_datablock(buff, 512)) break;
|
||||
buff += 512;
|
||||
} while (--count);
|
||||
send_cmd(CMD12, 0); /* STOP_TRANSMISSION */
|
||||
}
|
||||
}
|
||||
release_spi();
|
||||
|
||||
return count ? RES_ERROR : RES_OK;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Write Sector(s) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
#if _READONLY == 0
|
||||
DRESULT disk_write (
|
||||
BYTE drv, /* Physical drive nmuber (0) */
|
||||
const BYTE *buff, /* Pointer to the data to be written */
|
||||
DWORD sector, /* Start sector number (LBA) */
|
||||
BYTE count /* Sector count (1..255) */
|
||||
)
|
||||
{
|
||||
if (drv || !count) return RES_PARERR;
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
if (Stat & STA_PROTECT) return RES_WRPRT;
|
||||
|
||||
if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */
|
||||
|
||||
if (count == 1) { /* Single block write */
|
||||
if ((send_cmd(CMD24, sector) == 0) /* WRITE_BLOCK */
|
||||
&& xmit_datablock(buff, 0xFE))
|
||||
count = 0;
|
||||
}
|
||||
else { /* Multiple block write */
|
||||
if (CardType & CT_SDC) send_cmd(ACMD23, count);
|
||||
if (send_cmd(CMD25, sector) == 0) { /* WRITE_MULTIPLE_BLOCK */
|
||||
do {
|
||||
if (!xmit_datablock(buff, 0xFC)) break;
|
||||
buff += 512;
|
||||
} while (--count);
|
||||
if (!xmit_datablock(0, 0xFD)) /* STOP_TRAN token */
|
||||
count = 1;
|
||||
}
|
||||
}
|
||||
release_spi();
|
||||
|
||||
return count ? RES_ERROR : RES_OK;
|
||||
}
|
||||
#endif /* _READONLY == 0 */
|
||||
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Miscellaneous Functions */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
|
||||
#if _USE_IOCTL != 0
|
||||
DRESULT disk_ioctl (
|
||||
BYTE drv, /* Physical drive nmuber (0) */
|
||||
BYTE ctrl, /* Control code */
|
||||
void *buff /* Buffer to send/receive control data */
|
||||
)
|
||||
{
|
||||
DRESULT res;
|
||||
BYTE n, csd[16], *ptr = buff;
|
||||
WORD csize;
|
||||
|
||||
|
||||
if (drv) return RES_PARERR;
|
||||
|
||||
res = RES_ERROR;
|
||||
|
||||
if (ctrl == CTRL_POWER) {
|
||||
switch (*ptr) {
|
||||
case 0: /* Sub control code == 0 (POWER_OFF) */
|
||||
if (chk_power())
|
||||
power_off(); /* Power off */
|
||||
res = RES_OK;
|
||||
break;
|
||||
case 1: /* Sub control code == 1 (POWER_ON) */
|
||||
power_on(); /* Power on */
|
||||
res = RES_OK;
|
||||
break;
|
||||
case 2: /* Sub control code == 2 (POWER_GET) */
|
||||
*(ptr+1) = (BYTE)chk_power();
|
||||
res = RES_OK;
|
||||
break;
|
||||
default :
|
||||
res = RES_PARERR;
|
||||
}
|
||||
}
|
||||
else {
|
||||
if (Stat & STA_NOINIT) return RES_NOTRDY;
|
||||
|
||||
switch (ctrl) {
|
||||
case CTRL_SYNC : /* Make sure that no pending write process */
|
||||
SELECT();
|
||||
if (wait_ready() == 0xFF)
|
||||
res = RES_OK;
|
||||
break;
|
||||
|
||||
case GET_SECTOR_COUNT : /* Get number of sectors on the disk (DWORD) */
|
||||
if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) {
|
||||
if ((csd[0] >> 6) == 1) { /* SDC ver 2.00 */
|
||||
csize = csd[9] + ((WORD)csd[8] << 8) + 1;
|
||||
*(DWORD*)buff = (DWORD)csize << 10;
|
||||
} else { /* SDC ver 1.XX or MMC*/
|
||||
n = (csd[5] & 15) + ((csd[10] & 128) >> 7) + ((csd[9] & 3) << 1) + 2;
|
||||
csize = (csd[8] >> 6) + ((WORD)csd[7] << 2) + ((WORD)(csd[6] & 3) << 10) + 1;
|
||||
*(DWORD*)buff = (DWORD)csize << (n - 9);
|
||||
}
|
||||
res = RES_OK;
|
||||
}
|
||||
break;
|
||||
|
||||
case GET_SECTOR_SIZE : /* Get R/W sector size (WORD) */
|
||||
*(WORD*)buff = 512;
|
||||
res = RES_OK;
|
||||
break;
|
||||
|
||||
case GET_BLOCK_SIZE : /* Get erase block size in unit of sector (DWORD) */
|
||||
if (CardType & CT_SD2) { /* SDC ver 2.00 */
|
||||
if (send_cmd(ACMD13, 0) == 0) { /* Read SD status */
|
||||
rcvr_spi();
|
||||
if (rcvr_datablock(csd, 16)) { /* Read partial block */
|
||||
for (n = 64 - 16; n; n--) rcvr_spi(); /* Purge trailing data */
|
||||
*(DWORD*)buff = 16UL << (csd[10] >> 4);
|
||||
res = RES_OK;
|
||||
}
|
||||
}
|
||||
} else { /* SDC ver 1.XX or MMC */
|
||||
if ((send_cmd(CMD9, 0) == 0) && rcvr_datablock(csd, 16)) { /* Read CSD */
|
||||
if (CardType & CT_SD1) { /* SDC ver 1.XX */
|
||||
*(DWORD*)buff = (((csd[10] & 63) << 1) + ((WORD)(csd[11] & 128) >> 7) + 1) << ((csd[13] >> 6) - 1);
|
||||
} else { /* MMC */
|
||||
*(DWORD*)buff = ((WORD)((csd[10] & 124) >> 2) + 1) * (((csd[11] & 3) << 3) + ((csd[11] & 224) >> 5) + 1);
|
||||
}
|
||||
res = RES_OK;
|
||||
}
|
||||
}
|
||||
break;
|
||||
|
||||
case MMC_GET_TYPE : /* Get card type flags (1 byte) */
|
||||
*ptr = CardType;
|
||||
res = RES_OK;
|
||||
break;
|
||||
|
||||
case MMC_GET_CSD : /* Receive CSD as a data block (16 bytes) */
|
||||
if (send_cmd(CMD9, 0) == 0 /* READ_CSD */
|
||||
&& rcvr_datablock(ptr, 16))
|
||||
res = RES_OK;
|
||||
break;
|
||||
|
||||
case MMC_GET_CID : /* Receive CID as a data block (16 bytes) */
|
||||
if (send_cmd(CMD10, 0) == 0 /* READ_CID */
|
||||
&& rcvr_datablock(ptr, 16))
|
||||
res = RES_OK;
|
||||
break;
|
||||
|
||||
case MMC_GET_OCR : /* Receive OCR as an R3 resp (4 bytes) */
|
||||
if (send_cmd(CMD58, 0) == 0) { /* READ_OCR */
|
||||
for (n = 4; n; n--) *ptr++ = rcvr_spi();
|
||||
res = RES_OK;
|
||||
}
|
||||
break;
|
||||
|
||||
case MMC_GET_SDSTAT : /* Receive SD statsu as a data block (64 bytes) */
|
||||
if (send_cmd(ACMD13, 0) == 0) { /* SD_STATUS */
|
||||
rcvr_spi();
|
||||
if (rcvr_datablock(ptr, 64))
|
||||
res = RES_OK;
|
||||
}
|
||||
break;
|
||||
|
||||
default:
|
||||
res = RES_PARERR;
|
||||
}
|
||||
|
||||
release_spi();
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
#endif /* _USE_IOCTL != 0 */
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* Device Timer Interrupt Procedure (Platform dependent) */
|
||||
/*-----------------------------------------------------------------------*/
|
||||
/* This function must be called in period of 10ms */
|
||||
|
||||
void disk_timerproc (void)
|
||||
{
|
||||
static BYTE pv;
|
||||
BYTE n, s;
|
||||
|
||||
|
||||
n = Timer1; /* 100Hz decrement timer */
|
||||
if (n) Timer1 = --n;
|
||||
n = Timer2;
|
||||
if (n) Timer2 = --n;
|
||||
|
||||
n = pv;
|
||||
pv = SOCKPORT & (SOCKWP | SOCKINS); /* Sample socket switch */
|
||||
|
||||
if (n == pv) { /* Have contacts stabled? */
|
||||
s = Stat;
|
||||
|
||||
if (pv & SOCKWP) /* WP is H (write protected) */
|
||||
s |= STA_PROTECT;
|
||||
else /* WP is L (write enabled) */
|
||||
s &= ~STA_PROTECT;
|
||||
|
||||
if (pv & SOCKINS) /* INS = H (Socket empty) */
|
||||
s |= (STA_NODISK | STA_NOINIT);
|
||||
else /* INS = L (Card inserted) */
|
||||
s &= ~STA_NODISK;
|
||||
|
||||
Stat = s;
|
||||
}
|
||||
}
|
||||
|
||||
249
tools/ffsample/avr/rtc.c
Normal file
249
tools/ffsample/avr/rtc.c
Normal file
@@ -0,0 +1,249 @@
|
||||
/*--------------------------------------------------------------------------*/
|
||||
/* RTC controls */
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <string.h>
|
||||
#include "rtc.h"
|
||||
|
||||
|
||||
|
||||
#define SCL_LOW() DDRE |= 0x04 /* SCL = LOW */
|
||||
#define SCL_HIGH() DDRE &= 0xFB /* SCL = High-Z */
|
||||
#define SCL_VAL ((PINE & 0x04) ? 1 : 0) /* SCL input level */
|
||||
#define SDA_LOW() DDRE |= 0x08 /* SDA = LOW */
|
||||
#define SDA_HIGH() DDRE &= 0xF7 /* SDA = High-Z */
|
||||
#define SDA_VAL ((PINE & 0x08) ? 1 : 0) /* SDA input level */
|
||||
|
||||
|
||||
|
||||
static
|
||||
void iic_delay (void)
|
||||
{
|
||||
int n;
|
||||
BYTE d;
|
||||
|
||||
for (n = 4; n; n--) d = PINE;
|
||||
}
|
||||
|
||||
|
||||
/* Generate start condition on the IIC bus */
|
||||
static
|
||||
void iic_start (void)
|
||||
{
|
||||
SDA_HIGH();
|
||||
iic_delay();
|
||||
SCL_HIGH();
|
||||
iic_delay();
|
||||
SDA_LOW();
|
||||
iic_delay();
|
||||
SCL_LOW();
|
||||
iic_delay();
|
||||
}
|
||||
|
||||
|
||||
/* Generate stop condition on the IIC bus */
|
||||
static
|
||||
void iic_stop (void)
|
||||
{
|
||||
SDA_LOW();
|
||||
iic_delay();
|
||||
SCL_HIGH();
|
||||
iic_delay();
|
||||
SDA_HIGH();
|
||||
iic_delay();
|
||||
}
|
||||
|
||||
|
||||
/* Send a byte to the IIC bus */
|
||||
static
|
||||
BOOL iic_send (BYTE dat)
|
||||
{
|
||||
BYTE b = 0x80;
|
||||
BOOL ack;
|
||||
|
||||
|
||||
do {
|
||||
if (dat & b) { /* SDA = Z/L */
|
||||
SDA_HIGH();
|
||||
} else {
|
||||
SDA_LOW();
|
||||
}
|
||||
iic_delay();
|
||||
SCL_HIGH();
|
||||
iic_delay();
|
||||
SCL_LOW();
|
||||
iic_delay();
|
||||
} while (b >>= 1);
|
||||
SDA_HIGH();
|
||||
iic_delay();
|
||||
SCL_HIGH();
|
||||
ack = SDA_VAL ? FALSE : TRUE; /* Sample ACK */
|
||||
iic_delay();
|
||||
SCL_LOW();
|
||||
iic_delay();
|
||||
return ack;
|
||||
}
|
||||
|
||||
|
||||
/* Receive a byte from the IIC bus */
|
||||
static
|
||||
BYTE iic_rcvr (BOOL ack)
|
||||
{
|
||||
UINT d = 1;
|
||||
|
||||
|
||||
do {
|
||||
d <<= 1;
|
||||
SCL_HIGH();
|
||||
if (SDA_VAL) d++;
|
||||
iic_delay();
|
||||
SCL_LOW();
|
||||
iic_delay();
|
||||
} while (d < 0x100);
|
||||
if (ack) { /* SDA = ACK */
|
||||
SDA_LOW();
|
||||
} else {
|
||||
SDA_HIGH();
|
||||
}
|
||||
iic_delay();
|
||||
SCL_HIGH();
|
||||
iic_delay();
|
||||
SCL_LOW();
|
||||
SDA_HIGH();
|
||||
iic_delay();
|
||||
|
||||
return (BYTE)d;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
BOOL rtc_read (
|
||||
UINT adr, /* Read start address */
|
||||
UINT cnt, /* Read byte count */
|
||||
void* buff /* Read data buffer */
|
||||
)
|
||||
{
|
||||
BYTE *rbuff = buff;
|
||||
int n;
|
||||
|
||||
|
||||
if (!cnt) return FALSE;
|
||||
|
||||
n = 10;
|
||||
do { /* Select DS1338 (0xD0) */
|
||||
iic_start();
|
||||
} while (!iic_send(0xD0) && --n);
|
||||
if (!n) return FALSE;
|
||||
|
||||
if (iic_send((BYTE)adr)) { /* Set start address */
|
||||
iic_start(); /* Reselect DS1338 in read mode (0xD1) */
|
||||
if (iic_send(0xD1)) {
|
||||
do { /* Receive data */
|
||||
cnt--;
|
||||
*rbuff++ = iic_rcvr(cnt ? TRUE : FALSE);
|
||||
} while (cnt);
|
||||
}
|
||||
}
|
||||
|
||||
iic_stop(); /* Deselect device */
|
||||
|
||||
return cnt ? FALSE : TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
BOOL rtc_write (
|
||||
UINT adr, /* Write start address */
|
||||
UINT cnt, /* Write byte count */
|
||||
const void* buff /* Data to be written */
|
||||
)
|
||||
{
|
||||
const BYTE *wbuff = buff;
|
||||
int n;
|
||||
|
||||
|
||||
if (!cnt) return FALSE;
|
||||
|
||||
n = 10;
|
||||
do { /* Select DS1338 (0xD0) */
|
||||
iic_start();
|
||||
} while (!iic_send(0xD0) && --n);
|
||||
if (!n) return FALSE;
|
||||
|
||||
if (iic_send((BYTE)adr)) { /* Set start address */
|
||||
do { /* Send data */
|
||||
if (!iic_send(*wbuff++)) break;
|
||||
} while (--cnt);
|
||||
}
|
||||
|
||||
iic_stop(); /* Deselect device */
|
||||
|
||||
return cnt ? FALSE : TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
BOOL rtc_gettime (RTC *rtc)
|
||||
{
|
||||
BYTE buf[8];
|
||||
|
||||
|
||||
if (!rtc_read(0, 7, buf)) return FALSE;
|
||||
|
||||
rtc->sec = (buf[0] & 0x0F) + ((buf[0] >> 4) & 7) * 10;
|
||||
rtc->min = (buf[1] & 0x0F) + (buf[1] >> 4) * 10;
|
||||
rtc->hour = (buf[2] & 0x0F) + ((buf[2] >> 4) & 3) * 10;
|
||||
rtc->wday = (buf[2] & 0x07);
|
||||
rtc->mday = (buf[4] & 0x0F) + ((buf[4] >> 4) & 3) * 10;
|
||||
rtc->month = (buf[5] & 0x0F) + ((buf[5] >> 4) & 1) * 10;
|
||||
rtc->year = 2000 + (buf[6] & 0x0F) + (buf[6] >> 4) * 10;
|
||||
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
BOOL rtc_settime (const RTC *rtc)
|
||||
{
|
||||
|
||||
BYTE buf[8];
|
||||
|
||||
|
||||
buf[0] = rtc->sec / 10 * 16 + rtc->sec % 10;
|
||||
buf[1] = rtc->min / 10 * 16 + rtc->min % 10;
|
||||
buf[2] = rtc->hour / 10 * 16 + rtc->hour % 10;
|
||||
buf[3] = rtc->wday & 7;
|
||||
buf[4] = rtc->mday / 10 * 16 + rtc->mday % 10;
|
||||
buf[5] = rtc->month / 10 * 16 + rtc->month % 10;
|
||||
buf[6] = (rtc->year - 2000) / 10 * 16 + (rtc->year - 2000) % 10;
|
||||
return rtc_write(0, 7, buf);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
BOOL rtc_init (void)
|
||||
{
|
||||
BYTE buf[8]; /* RTC R/W buffer */
|
||||
UINT adr;
|
||||
|
||||
|
||||
/* Read RTC registers */
|
||||
if (!rtc_read(0, 8, buf)) return FALSE; /* IIC error */
|
||||
|
||||
if (buf[7] & 0x20) { /* When data has been volatiled, set default time */
|
||||
/* Clear nv-ram. Reg[8..63] */
|
||||
memset(buf, 0, 8);
|
||||
for (adr = 8; adr < 64; adr += 8)
|
||||
rtc_write(adr, 8, buf);
|
||||
/* Reset time to Jan 1, '08. Reg[0..7] */
|
||||
buf[4] = 1; buf[5] = 1; buf[6] = 8;
|
||||
rtc_write(0, 8, buf);
|
||||
}
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
18
tools/ffsample/avr/rtc.h
Normal file
18
tools/ffsample/avr/rtc.h
Normal file
@@ -0,0 +1,18 @@
|
||||
#include "integer.h"
|
||||
|
||||
typedef struct {
|
||||
WORD year; /* 2000..2099 */
|
||||
BYTE month; /* 1..12 */
|
||||
BYTE mday; /* 1.. 31 */
|
||||
BYTE wday; /* 1..7 */
|
||||
BYTE hour; /* 0..23 */
|
||||
BYTE min; /* 0..59 */
|
||||
BYTE sec; /* 0..59 */
|
||||
} RTC;
|
||||
|
||||
BOOL rtc_init (void); /* Initialize RTC */
|
||||
BOOL rtc_gettime (RTC*); /* Get time */
|
||||
BOOL rtc_settime (const RTC*); /* Set time */
|
||||
BOOL rtc_write (UINT, UINT, const void*); /* Write RTC regs */
|
||||
BOOL rtc_read (UINT, UINT, void*); /* Read RTC regs */
|
||||
|
||||
128
tools/ffsample/avr/uart.c
Normal file
128
tools/ffsample/avr/uart.c
Normal file
@@ -0,0 +1,128 @@
|
||||
/*------------------------------------------------*/
|
||||
/* UART functions */
|
||||
|
||||
|
||||
#include <avr/io.h>
|
||||
#include <avr/interrupt.h>
|
||||
#include "uart.h"
|
||||
|
||||
#define SYSCLK 9216000
|
||||
#define BAUD 115200
|
||||
|
||||
|
||||
typedef struct _fifo {
|
||||
uint8_t idx_w;
|
||||
uint8_t idx_r;
|
||||
uint8_t count;
|
||||
uint8_t buff[64];
|
||||
} FIFO;
|
||||
|
||||
|
||||
static volatile
|
||||
FIFO txfifo, rxfifo;
|
||||
|
||||
|
||||
|
||||
/* Initialize UART */
|
||||
|
||||
void uart_init()
|
||||
{
|
||||
rxfifo.idx_r = 0;
|
||||
rxfifo.idx_w = 0;
|
||||
rxfifo.count = 0;
|
||||
txfifo.idx_r = 0;
|
||||
txfifo.idx_w = 0;
|
||||
txfifo.count = 0;
|
||||
|
||||
UBRR0L = SYSCLK/BAUD/16-1;
|
||||
UCSR0B = _BV(RXEN0)|_BV(RXCIE0)|_BV(TXEN0);
|
||||
}
|
||||
|
||||
|
||||
/* Get a received character */
|
||||
|
||||
uint8_t uart_test ()
|
||||
{
|
||||
return rxfifo.count;
|
||||
}
|
||||
|
||||
|
||||
uint8_t uart_get ()
|
||||
{
|
||||
uint8_t d, i;
|
||||
|
||||
|
||||
i = rxfifo.idx_r;
|
||||
while(rxfifo.count == 0);
|
||||
d = rxfifo.buff[i++];
|
||||
cli();
|
||||
rxfifo.count--;
|
||||
sei();
|
||||
if(i >= sizeof(rxfifo.buff))
|
||||
i = 0;
|
||||
rxfifo.idx_r = i;
|
||||
|
||||
return d;
|
||||
}
|
||||
|
||||
|
||||
/* Put a character to transmit */
|
||||
|
||||
void uart_put (uint8_t d)
|
||||
{
|
||||
uint8_t i;
|
||||
|
||||
|
||||
i = txfifo.idx_w;
|
||||
while(txfifo.count >= sizeof(txfifo.buff));
|
||||
txfifo.buff[i++] = d;
|
||||
cli();
|
||||
txfifo.count++;
|
||||
UCSR0B = _BV(RXEN0)|_BV(RXCIE0)|_BV(TXEN0)|_BV(UDRIE0);
|
||||
sei();
|
||||
if(i >= sizeof(txfifo.buff))
|
||||
i = 0;
|
||||
txfifo.idx_w = i;
|
||||
}
|
||||
|
||||
|
||||
/* UART RXC interrupt */
|
||||
|
||||
SIGNAL(SIG_UART0_RECV)
|
||||
{
|
||||
uint8_t d, n, i;
|
||||
|
||||
|
||||
d = UDR0;
|
||||
n = rxfifo.count;
|
||||
if(n < sizeof(rxfifo.buff)) {
|
||||
rxfifo.count = ++n;
|
||||
i = rxfifo.idx_w;
|
||||
rxfifo.buff[i++] = d;
|
||||
if(i >= sizeof(rxfifo.buff))
|
||||
i = 0;
|
||||
rxfifo.idx_w = i;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* UART UDRE interrupt */
|
||||
|
||||
SIGNAL(SIG_UART0_DATA)
|
||||
{
|
||||
uint8_t n, i;
|
||||
|
||||
|
||||
n = txfifo.count;
|
||||
if(n) {
|
||||
txfifo.count = --n;
|
||||
i = txfifo.idx_r;
|
||||
UDR0 = txfifo.buff[i++];
|
||||
if(i >= sizeof(txfifo.buff))
|
||||
i = 0;
|
||||
txfifo.idx_r = i;
|
||||
}
|
||||
if(n == 0)
|
||||
UCSR0B = _BV(RXEN0)|_BV(RXCIE0)|_BV(TXEN0);
|
||||
}
|
||||
|
||||
4
tools/ffsample/avr/uart.h
Normal file
4
tools/ffsample/avr/uart.h
Normal file
@@ -0,0 +1,4 @@
|
||||
void uart_init(void); /* Initialize UART and Flush FIFOs */
|
||||
uint8_t uart_get (void); /* Get a byte from UART Rx FIFO */
|
||||
uint8_t uart_test(void); /* Check number of data in UART Rx FIFO */
|
||||
void uart_put (uint8_t); /* Put a byte into UART Tx FIFO */
|
||||
419
tools/ffsample/avr/xitoa.S
Normal file
419
tools/ffsample/avr/xitoa.S
Normal file
@@ -0,0 +1,419 @@
|
||||
;---------------------------------------------------------------------------;
|
||||
; Extended itoa, puts, printf and atoi (C)ChaN, 2006
|
||||
;
|
||||
; Module size: 277/261 words (max)
|
||||
;
|
||||
|
||||
#define USE_XPUTS
|
||||
#define USE_XITOA
|
||||
#define USE_XPRINTF
|
||||
#define CR_CRLF
|
||||
#define USE_XATOI
|
||||
|
||||
|
||||
|
||||
.nolist
|
||||
#include <avr/io.h> // Include device specific definitions.
|
||||
.list
|
||||
|
||||
#ifdef SPM_PAGESIZE // Recent devices have "lpm Rd,Z+" and "movw".
|
||||
.macro _LPMI reg
|
||||
lpm \reg, Z+
|
||||
.endm
|
||||
.macro _MOVW dh,dl, sh,sl
|
||||
movw \dl, \sl
|
||||
.endm
|
||||
#else // Earlier devices do not have "lpm Rd,Z+" nor "movw".
|
||||
.macro _LPMI reg
|
||||
lpm
|
||||
mov \reg, r0
|
||||
adiw ZL, 1
|
||||
.endm
|
||||
.macro _MOVW dh,dl, sh,sl
|
||||
mov \dl, \sl
|
||||
mov \dh, \sh
|
||||
.endm
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
;---------------------------------------------------------------------------
|
||||
; Stub function to forward to user output function
|
||||
;
|
||||
;Prototype: void xputc (char chr // a character to be output
|
||||
; );
|
||||
;Size: 15/15 words
|
||||
|
||||
.section .bss
|
||||
|
||||
.global xfunc_out ; xfunc_out must be initialized before using this module.
|
||||
xfunc_out: .ds.w 1
|
||||
|
||||
.section .text
|
||||
|
||||
|
||||
.global xputc
|
||||
.func xputc
|
||||
xputc:
|
||||
#ifdef CR_CRLF
|
||||
cpi r24, 10 ;LF --> CRLF
|
||||
brne 1f ;
|
||||
ldi r24, 13 ;
|
||||
rcall 1f ;
|
||||
ldi r24, 10 ;/
|
||||
1:
|
||||
#endif
|
||||
push ZH
|
||||
push ZL
|
||||
lds ZL, xfunc_out+0 ;Pointer to the registered output function.
|
||||
lds ZH, xfunc_out+1 ;/
|
||||
icall
|
||||
pop ZL
|
||||
pop ZH
|
||||
ret
|
||||
.endfunc
|
||||
|
||||
|
||||
|
||||
;---------------------------------------------------------------------------
|
||||
; Direct ROM string output
|
||||
;
|
||||
;Prototype: void xputs (const prog_char *str // rom string to be output
|
||||
; );
|
||||
;Size: 10/7 words
|
||||
|
||||
#ifdef USE_XPUTS
|
||||
.global xputs
|
||||
.func xputs
|
||||
xputs:
|
||||
_MOVW ZH,ZL, r25,r24 ; Z = pointer to rom string
|
||||
1: _LPMI r24
|
||||
cpi r24, 0
|
||||
breq 2f
|
||||
rcall xputc
|
||||
rjmp 1b
|
||||
2: ret
|
||||
.endfunc
|
||||
#endif
|
||||
|
||||
|
||||
;---------------------------------------------------------------------------
|
||||
; Extended direct numeral string output (32bit version)
|
||||
;
|
||||
;Prototype: void xitoa (long value, // value to be output
|
||||
; char radix, // radix
|
||||
; char width); // minimum width
|
||||
;Size: 59/59 words
|
||||
;
|
||||
|
||||
#ifdef USE_XITOA
|
||||
.global xitoa
|
||||
.func xitoa
|
||||
xitoa:
|
||||
;r25:r22 = value, r20 = base, r18 = digits
|
||||
clr r31 ;r31 = stack level
|
||||
ldi r30, ' ' ;r30 = sign
|
||||
ldi r19, ' ' ;r19 = filler
|
||||
sbrs r20, 7 ;When base indicates signd format and the value
|
||||
rjmp 0f ;is minus, add a '-'.
|
||||
neg r20 ;
|
||||
sbrs r25, 7 ;
|
||||
rjmp 0f ;
|
||||
ldi r30, '-' ;
|
||||
com r22 ;
|
||||
com r23 ;
|
||||
com r24 ;
|
||||
com r25 ;
|
||||
adc r22, r1 ;
|
||||
adc r23, r1 ;
|
||||
adc r24, r1 ;
|
||||
adc r25, r1 ;/
|
||||
0: sbrs r18, 7 ;When digits indicates zero filled,
|
||||
rjmp 1f ;filler is '0'.
|
||||
neg r18 ;
|
||||
ldi r19, '0' ;/
|
||||
;----- string conversion loop
|
||||
1: ldi r21, 32 ;r26 = r25:r22 % r20
|
||||
clr r26 ;r25:r22 /= r20
|
||||
2: lsl r22 ;
|
||||
rol r23 ;
|
||||
rol r24 ;
|
||||
rol r25 ;
|
||||
rol r26 ;
|
||||
cp r26, r20 ;
|
||||
brcs 3f ;
|
||||
sub r26, r20 ;
|
||||
inc r22 ;
|
||||
3: dec r21 ;
|
||||
brne 2b ;/
|
||||
cpi r26, 10 ;r26 is a numeral digit '0'-'F'
|
||||
brcs 4f ;
|
||||
subi r26, -7 ;
|
||||
4: subi r26, -'0' ;/
|
||||
push r26 ;Stack it
|
||||
inc r31 ;/
|
||||
cp r22, r1 ;Repeat until r25:r22 gets zero
|
||||
cpc r23, r1 ;
|
||||
cpc r24, r1 ;
|
||||
cpc r25, r1 ;
|
||||
brne 1b ;/
|
||||
|
||||
cpi r30, '-' ;Minus sign if needed
|
||||
brne 5f ;
|
||||
push r30 ;
|
||||
inc r31 ;/
|
||||
5: cp r31, r18 ;Filler
|
||||
brcc 6f ;
|
||||
push r19 ;
|
||||
inc r31 ;
|
||||
rjmp 5b ;/
|
||||
|
||||
6: pop r24 ;Flush stacked digits and exit
|
||||
rcall xputc ;
|
||||
dec r31 ;
|
||||
brne 6b ;/
|
||||
|
||||
ret
|
||||
.endfunc
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
;---------------------------------------------------------------------------;
|
||||
; Formatted string output (16/32bit version)
|
||||
;
|
||||
;Prototype:
|
||||
; void xprintf (const prog_char *format, ...);
|
||||
;Size: 104/94 words
|
||||
;
|
||||
|
||||
#ifdef USE_XPRINTF
|
||||
.global xprintf
|
||||
.func xprintf
|
||||
xprintf:
|
||||
push YH
|
||||
push YL
|
||||
in YL, _SFR_IO_ADDR(SPL)
|
||||
#ifdef SPH
|
||||
in YH, _SFR_IO_ADDR(SPH)
|
||||
#else
|
||||
clr YH
|
||||
#endif
|
||||
#if FLASHEND > 0x1FFFF
|
||||
adiw YL, 6 ;Y = pointer to arguments
|
||||
#else
|
||||
adiw YL, 5 ;Y = pointer to arguments
|
||||
#endif
|
||||
ld ZL, Y+ ;Z = pointer to format string
|
||||
ld ZH, Y+ ;/
|
||||
|
||||
0: _LPMI r24 ;Get a format char
|
||||
cpi r24, 0 ;End of format string?
|
||||
breq 90f ;/
|
||||
cpi r24, '%' ;Is format?
|
||||
breq 20f ;/
|
||||
1: rcall xputc ;Put a normal character
|
||||
rjmp 0b ;/
|
||||
90: pop YL
|
||||
pop YH
|
||||
ret
|
||||
|
||||
20: ldi r18, 0 ;r18: digits
|
||||
clt ;T: filler
|
||||
_LPMI r21 ;Get flags
|
||||
cpi r21, '%' ;Is a %?
|
||||
breq 1b ;/
|
||||
cpi r21, '0' ;Zero filled?
|
||||
brne 23f ;
|
||||
set ;/
|
||||
22: _LPMI r21 ;Get width
|
||||
23: cpi r21, '9'+1 ;
|
||||
brcc 24f ;
|
||||
subi r21, '0' ;
|
||||
brcs 90b ;
|
||||
lsl r18 ;
|
||||
mov r0, r18 ;
|
||||
lsl r18 ;
|
||||
lsl r18 ;
|
||||
add r18, r0 ;
|
||||
add r18, r21 ;
|
||||
rjmp 22b ;/
|
||||
|
||||
24: brtc 25f ;get value (low word)
|
||||
neg r18 ;
|
||||
25: ld r24, Y+ ;
|
||||
ld r25, Y+ ;/
|
||||
cpi r21, 'c' ;Is type character?
|
||||
breq 1b ;/
|
||||
cpi r21, 's' ;Is type RAM string?
|
||||
breq 50f ;/
|
||||
cpi r21, 'S' ;Is type ROM string?
|
||||
breq 60f ;/
|
||||
_MOVW r23,r22,r25,r24 ;r25:r22 = value
|
||||
clr r24 ;
|
||||
clr r25 ;
|
||||
clt ;/
|
||||
cpi r21, 'l' ;Is long int?
|
||||
brne 26f ;
|
||||
ld r24, Y+ ;get value (high word)
|
||||
ld r25, Y+ ;
|
||||
set ;
|
||||
_LPMI r21 ;/
|
||||
26: cpi r21, 'd' ;Is type signed decimal?
|
||||
brne 27f ;/
|
||||
ldi r20, -10 ;
|
||||
brts 40f ;
|
||||
sbrs r23, 7 ;
|
||||
rjmp 40f ;
|
||||
ldi r24, -1 ;
|
||||
ldi r25, -1 ;
|
||||
rjmp 40f ;/
|
||||
27: cpi r21, 'u' ;Is type unsigned decimal?
|
||||
ldi r20, 10 ;
|
||||
breq 40f ;/
|
||||
cpi r21, 'X' ;Is type hexdecimal?
|
||||
ldi r20, 16 ;
|
||||
breq 40f ;/
|
||||
cpi r21, 'b' ;Is type binary?
|
||||
ldi r20, 2 ;
|
||||
breq 40f ;/
|
||||
rjmp 90b ;abort
|
||||
40: push ZH ;Output the value
|
||||
push ZL ;
|
||||
rcall xitoa ;
|
||||
42: pop ZL ;
|
||||
pop ZH ;
|
||||
rjmp 0b ;/
|
||||
|
||||
50: push ZH ;Put a string on the RAM
|
||||
push ZL
|
||||
_MOVW ZH,ZL, r25,r24
|
||||
51: ld r24, Z+
|
||||
cpi r24, 0
|
||||
breq 42b
|
||||
rcall xputc
|
||||
rjmp 51b
|
||||
|
||||
60: push ZH ;Put a string on the ROM
|
||||
push ZL
|
||||
rcall xputs
|
||||
rjmp 42b
|
||||
|
||||
.endfunc
|
||||
#endif
|
||||
|
||||
|
||||
|
||||
;---------------------------------------------------------------------------
|
||||
; Extended numeral string input
|
||||
;
|
||||
;Prototype:
|
||||
; char xatoi ( /* 1: Successful, 0: Failed */
|
||||
; const char **str, /* pointer to pointer to source string */
|
||||
; long *res /* result */
|
||||
; );
|
||||
;Size: 94/91 words
|
||||
;
|
||||
|
||||
#ifdef USE_XATOI
|
||||
.global xatoi
|
||||
.func xatoi
|
||||
xatoi:
|
||||
_MOVW r1, r0, r23, r22
|
||||
_MOVW XH, XL, r25, r24
|
||||
ld ZL, X+
|
||||
ld ZH, X+
|
||||
clr r18 ;r21:r18 = 0;
|
||||
clr r19 ;
|
||||
clr r20 ;
|
||||
clr r21 ;/
|
||||
clt ;T = 0;
|
||||
|
||||
ldi r25, 10 ;r25 = 10;
|
||||
rjmp 41f ;/
|
||||
40: adiw ZL, 1 ;Z++;
|
||||
41: ld r22, Z ;r22 = *Z;
|
||||
cpi r22, ' ' ;if(r22 == ' ') continue
|
||||
breq 40b ;/
|
||||
brcs 70f ;if(r22 < ' ') error;
|
||||
cpi r22, '-' ;if(r22 == '-') {
|
||||
brne 42f ; T = 1;
|
||||
set ; continue;
|
||||
rjmp 40b ;}
|
||||
42: cpi r22, '9'+1 ;if(r22 > '9') error;
|
||||
brcc 70f ;/
|
||||
cpi r22, '0' ;if(r22 < '0') error;
|
||||
brcs 70f ;/
|
||||
brne 51f ;if(r22 > '0') cv_start;
|
||||
ldi r25, 8 ;r25 = 8;
|
||||
adiw ZL, 1 ;r22 = *(++Z);
|
||||
ld r22, Z ;/
|
||||
cpi r22, ' '+1 ;if(r22 <= ' ') exit;
|
||||
brcs 80f ;/
|
||||
cpi r22, 'b' ;if(r22 == 'b') {
|
||||
brne 43f ; r25 = 2;
|
||||
ldi r25, 2 ; cv_start;
|
||||
rjmp 50f ;}
|
||||
43: cpi r22, 'x' ;if(r22 != 'x') error;
|
||||
brne 51f ;/
|
||||
ldi r25, 16 ;r25 = 16;
|
||||
|
||||
50: adiw ZL, 1 ;Z++;
|
||||
ld r22, Z ;r22 = *Z;
|
||||
51: cpi r22, ' '+1 ;if(r22 <= ' ') break;
|
||||
brcs 80f ;/
|
||||
cpi r22, 'a' ;if(r22 >= 'a') r22 =- 0x20;
|
||||
brcs 52f ;
|
||||
subi r22, 0x20 ;/
|
||||
52: subi r22, '0' ;if((r22 -= '0') < 0) error;
|
||||
brcs 70f ;/
|
||||
cpi r22, 10 ;if(r22 >= 10) {
|
||||
brcs 53f ; r22 -= 7;
|
||||
subi r22, 7 ; if(r22 < 10)
|
||||
cpi r22, 10 ;
|
||||
brcs 70f ;}
|
||||
53: cp r22, r25 ;if(r22 >= r25) error;
|
||||
brcc 70f ;/
|
||||
60: ldi r24, 33 ;r21:r18 *= r25;
|
||||
sub r23, r23 ;
|
||||
61: brcc 62f ;
|
||||
add r23, r25 ;
|
||||
62: lsr r23 ;
|
||||
ror r21 ;
|
||||
ror r20 ;
|
||||
ror r19 ;
|
||||
ror r18 ;
|
||||
dec r24 ;
|
||||
brne 61b ;/
|
||||
add r18, r22 ;r21:r18 += r22;
|
||||
adc r19, r24 ;
|
||||
adc r20, r24 ;
|
||||
adc r21, r24 ;/
|
||||
rjmp 50b ;repeat
|
||||
|
||||
70: ldi r24, 0
|
||||
rjmp 81f
|
||||
80: ldi r24, 1
|
||||
81: brtc 82f
|
||||
clr r22
|
||||
com r18
|
||||
com r19
|
||||
com r20
|
||||
com r21
|
||||
adc r18, r22
|
||||
adc r19, r22
|
||||
adc r20, r22
|
||||
adc r21, r22
|
||||
82: st -X, ZH
|
||||
st -X, ZL
|
||||
_MOVW XH, XL, r1, r0
|
||||
st X+, r18
|
||||
st X+, r19
|
||||
st X+, r20
|
||||
st X+, r21
|
||||
clr r1
|
||||
ret
|
||||
.endfunc
|
||||
#endif
|
||||
|
||||
|
||||
96
tools/ffsample/avr/xitoa.h
Normal file
96
tools/ffsample/avr/xitoa.h
Normal file
@@ -0,0 +1,96 @@
|
||||
/*---------------------------------------------------------------------------
|
||||
Extended itoa, puts and printf (C)ChaN, 2006
|
||||
|
||||
-----------------------------------------------------------------------------*/
|
||||
|
||||
#ifndef XITOA
|
||||
#define XITOA
|
||||
|
||||
#include <avr/pgmspace.h>
|
||||
|
||||
extern void (*xfunc_out)(char);
|
||||
|
||||
/* This is a pointer to user defined output function. It must be initialized
|
||||
before using this modle.
|
||||
*/
|
||||
|
||||
void xputc(char chr);
|
||||
|
||||
/* This is a stub function to forward outputs to user defined output function.
|
||||
All outputs from this module are output via this function.
|
||||
*/
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
void xputs(const prog_char *string);
|
||||
|
||||
/* The string placed in the ROM is forwarded to xputc() directly.
|
||||
*/
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
void xitoa(long value, char radix, char width);
|
||||
|
||||
/* Extended itoa().
|
||||
|
||||
value radix width output
|
||||
100 10 6 " 100"
|
||||
100 10 -6 "000100"
|
||||
100 10 0 "100"
|
||||
4294967295 10 0 "4294967295"
|
||||
4294967295 -10 0 "-1"
|
||||
655360 16 -8 "000A0000"
|
||||
1024 16 0 "400"
|
||||
0x55 2 -8 "01010101"
|
||||
*/
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
void xprintf(const prog_char *format, ...);
|
||||
|
||||
/* Format string is placed in the ROM. The format flags is similar to printf().
|
||||
|
||||
%[flag][width][size]type
|
||||
|
||||
flag
|
||||
A '0' means filled with '0' when output is shorter than width.
|
||||
' ' is used in default. This is effective only numeral type.
|
||||
width
|
||||
Minimum width in decimal number. This is effective only numeral type.
|
||||
Default width is zero.
|
||||
size
|
||||
A 'l' means the argument is long(32bit). Default is short(16bit).
|
||||
This is effective only numeral type.
|
||||
type
|
||||
'c' : Character, argument is the value
|
||||
's' : String placed on the RAM, argument is the pointer
|
||||
'S' : String placed on the ROM, argument is the pointer
|
||||
'd' : Signed decimal, argument is the value
|
||||
'u' : Unsigned decimal, argument is the value
|
||||
'X' : Hex decimal, argument is the value
|
||||
'b' : Binary, argument is the value
|
||||
'%' : '%'
|
||||
|
||||
*/
|
||||
|
||||
|
||||
/*-----------------------------------------------------------------------------*/
|
||||
char xatoi(char **str, long *ret);
|
||||
|
||||
/* Get value of the numeral string.
|
||||
|
||||
str
|
||||
Pointer to pointer to source string
|
||||
|
||||
"0b11001010" binary
|
||||
"0377" octal
|
||||
"0xff800" hexdecimal
|
||||
"1250000" decimal
|
||||
"-25000" decimal
|
||||
|
||||
ret
|
||||
Pointer to return value
|
||||
*/
|
||||
|
||||
#endif /* XITOA */
|
||||
|
||||
Reference in New Issue
Block a user