From f5bb468a6ecdcd6fc374a9dc0d4d70450450a74c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Hru=C5=A1ka?= Date: Wed, 10 Jun 2015 16:51:55 +0200 Subject: [PATCH] work on sd fat --- devel/sdcard/Makefile | 87 +++++++++++++++++-------- devel/sdcard/main.c | 120 +++++++++++++++++++++++++++++++++-- devel/sdcard/sdcard.pro | 9 ++- devel/sdcard/sdcard.pro.user | 6 +- 4 files changed, 185 insertions(+), 37 deletions(-) diff --git a/devel/sdcard/Makefile b/devel/sdcard/Makefile index 4dba31f..bdd9991 100644 --- a/devel/sdcard/Makefile +++ b/devel/sdcard/Makefile @@ -1,66 +1,99 @@ -## === CPU settings === +################################### +# Makefile for MightyPork/avr-lib # +# Revision 3 # +################################### + +## ===== CPU settings ===== + # CPU type MCU = atmega328p + # CPU frequency F_CPU = 16000000 + # Fuses LFUSE = 0xFF HFUSE = 0xDE EFUSE = 0x05 +OPTIMIZE = s + + +## ===== Source files ===== -## === Source files === # Main C file MAIN = main.c -# Extra C files in this folder -LOCAL_SOURCE = - -# Library directory (with C files) -EXTRA_SOURCE_DIR = lib/ +# Library directory (with C and H files) +LIB_DIR = lib/ # C files in the library directory -EXTRA_SOURCE_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c fat16.c -#Files that need config file: -# EXTRA_SOURCE_FILES += lcd.c -# EXTRA_SOURCE_FILES += color.c wsrgb.c -# EXTRA_SOURCE_FILES += debouce.c +LIB_C_FILES = uart.c iopins.c stream.c adc.c dht11.c sonar.c onewire.c spi.c sd.c sd_blockdev.c fat16.c sd_fat.c + +# Extra Files that need config file: +#LIB_C_FILES += lcd.c +#LIB_C_FILES += color.c wsrgb.c +#LIB_C_FILES += debouce.c + +LIB_H_FILES = adc.h calc.h dht11.h fat16.h fat16_internal.h iopins.h nsdelay.h onewire.h +LIB_H_FILES += sd.h sd_blockdev.h sd_fat.h blockdev.h sonar.h spi.h stream.h uart.h +LIB_H_FILES += lcd.h color.h wsrgb.h debounce.h + + + + +## ===== Programmer ===== -## === Programmer === PROGRAMMER_TYPE = arduino PROGRAMMER_ARGS = -b 57600 -P /dev/ttyUSB0 -## === C flags === +## ===== C flags ===== + +CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(LIB_DIR) +CFLAGS += -funsigned-char +CFLAGS += -funsigned-bitfields +CFLAGS += -fpack-struct +CFLAGS += -fshort-enums +CFLAGS += -finline-functions +CFLAGS += -ffunction-sections +CFLAGS += -fdata-sections +CFLAGS += -Wall +CFLAGS += -Wextra +CFLAGS += -Wno-main +CFLAGS += -Wno-comment +CFLAGS += -Wno-unused-but-set-variable +CFLAGS += -Wfatal-errors +CFLAGS += -Wl,--gc-sections +CFLAGS += -Wl,--relax +CFLAGS += -Wl,--relax -CFLAGS = -std=gnu99 -mmcu=$(MCU) -DF_CPU=$(F_CPU)UL -I. -I$(EXTRA_SOURCE_DIR) -CFLAGS += -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -CFLAGS += -Wall -Wno-main -Wno-strict-prototypes -Wno-comment -CFLAGS += -g2 -Wextra -Wfatal-errors -Wno-unused-but-set-variable -CFLAGS += -ffunction-sections -fdata-sections -Wl,--gc-sections -Wl,--relax -# CFLAGS += -lm ## Math -# CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## for floating-point printf -# CFLAGS += -Wl,-u,vfprintf -lprintf_min ## for smaller printf -CFLAGS_BUILD = $(CFLAGS) -Os +#CFLAGS += -lm ## Math +#CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm ## Floating-point printf +#CFLAGS += -Wl,-u,vfprintf -lprintf_min ## Smaller printf + +CFLAGS_BUILD = $(CFLAGS) -O$(OPTIMIZE) # --------------------------------------------------------------------------- + ## Defined programs / locations CC = avr-gcc OBJCOPY = avr-objcopy OBJDUMP = avr-objdump AVRSIZE = avr-size AVRDUDE = avrdude +UART_TERM = gtkterm -p /dev/ttyUSB0 ## === File lists === TARGET = $(strip $(basename $(MAIN))) SRC1 = $(TARGET).c SRC = $(SRC1) -EXTRA_SOURCE = $(addprefix $(EXTRA_SOURCE_DIR), $(EXTRA_SOURCE_FILES)) +EXTRA_SOURCE = $(addprefix $(LIB_DIR), $(LIB_C_FILES)) +LIB_H_FILES_FILES = $(addprefix $(LIB_DIR), $(LIB_H_FILES)) SRC += $(EXTRA_SOURCE) -SRC += $(LOCAL_SOURCE) HEADERS = $(SRC:.c=.h) OBJ = $(SRC:.c=.o) @@ -73,7 +106,7 @@ pre: $(TARGET).pre %.hex: %.elf $(OBJCOPY) -R .eeprom -O ihex $< $@ -%.elf: $(SRC) +%.elf: $(SRC) $(LIB_H_FILES_FILES) Makefile $(CC) $(CFLAGS_BUILD) $(SRC) --output $@ %.pre: $(SRC1) @@ -131,7 +164,7 @@ shell: fser: all flash ser ser: - gtkterm -p /dev/ttyUSB0 + $(UART_TERM) # === fuses === diff --git a/devel/sdcard/main.c b/devel/sdcard/main.c index 960cb0d..9692f0a 100644 --- a/devel/sdcard/main.c +++ b/devel/sdcard/main.c @@ -4,10 +4,11 @@ #include "lib/uart.h" #include "lib/stream.h" #include "lib/sd.h" +#include "lib/sd_fat.h" +#include "lib/sd_blockdev.h" -void main() +void test_lowlevel() { - uart_init(9600); uart_puts_P(PSTR("*** SD CARD SPI TEST ***\r\n")); if (!sd_init()) @@ -16,11 +17,9 @@ void main() while (1); } - uint8_t text[512]; - put_str_P(uart, PSTR("\r\nReading...\r\n")); if (sd_read(0, 0, text, 0, 512)) { @@ -56,9 +55,120 @@ void main() } else { - put_str_P(uart, PSTR("Failed to read.")); + put_str_P(uart, PSTR("Failed to read.\r\n")); + while (1); + } +} + + +void try_reading_dirs(FAT16_FILE* f ) +{ + char buf[200]; + + do + { + if (!fat16_is_regular(f)) continue; + + if (f->type == FT_SUBDIR) + { + vt_color_fg(VT_MAGENTA); + uart_puts_P(PSTR("\r\n--- DIRECTORY: ")); + uart_puts(fat16_dispname(f, buf)); + uart_puts_P(PSTR(" ---\r\n")); + vt_color_reset(); + + FSAVEPOS p = fat16_savepos(f); + + if (fat16_opendir(f)) + { + do + { + if (!fat16_is_regular(f)) continue; + + vt_color_fg(VT_GREEN); + uart_puts_P(PSTR(">> --- File: ")); + uart_puts(fat16_dispname(f, buf)); + uart_puts_P(PSTR(" ---\r\n")); + vt_color_reset(); + } + while (fat16_next(f)); + } + + fat16_reopen(f, &p); + } + else + { + vt_color_fg(VT_YELLOW); + uart_puts_P(PSTR("--- File: ")); + uart_puts(fat16_dispname(f, buf)); + uart_puts_P(PSTR(" ---\r\n")); + vt_color_reset(); + + uint16_t len = 0; + if ((len = fat16_read(f, buf, 199))) + { + buf[len] = 0; + uart_puts(buf); + put_nl(uart); + } + else + { + uart_puts_P(PSTR("COULD NOT READ.\n")); + } + } + } + while (fat16_next(f)); +} + +// Init a file pointer */ +FAT16_FILE _f; +FAT16_FILE* f = &_f; + +void main() +{ + uart_init(9600); + vt_init(); + + vt_color_fg(VT_CYAN); + vt_attr(VT_BOLD, 1); + uart_puts_P(PSTR("\r\n*** FAT16 on SD card test ***\r\n")); + vt_color_reset(); + vt_attr_reset(); + + if (!(sdfat_init())) + { + uart_puts_P(PSTR("Failed to init.\r\n")); while (1); } + uart_puts_P(PSTR("SD card with FAT16 inited.\r\n")); + + + + // Read and print disk label + char lbl[16]; + sdfat_disk_label(lbl); + put_str(uart, lbl); + put_nl(uart); + + sdfat_root(f); + + try_reading_dirs(f); + +// if (fat16_find(f, "TESTFIL2.TXT")) +// { +// fat16_seek(f, f->size); +// fat16_write(f, "ONE MORE THING.\n", 16); +// sdfat_flush(); + +// uart_puts_P(PSTR("Write OK!\r\n")); +// } +// else +// { +// uart_puts_P(PSTR("Failed to open file.\r\n")); +// while (1); +// } + + while (1); } diff --git a/devel/sdcard/sdcard.pro b/devel/sdcard/sdcard.pro index b856652..c94d544 100644 --- a/devel/sdcard/sdcard.pro +++ b/devel/sdcard/sdcard.pro @@ -22,7 +22,9 @@ SOURCES += main.c \ sd/spi.c \ lib/fat16.c \ lib/sd.c \ - lib/spi.c + lib/spi.c \ + lib/sd_blockdev.c \ + lib/sd_fat.c include(deployment.pri) qtcAddDeployment() @@ -48,7 +50,10 @@ HEADERS += \ lib/fat16.h \ lib/fat16_internal.h \ lib/spi.h \ - lib/sd.h + lib/sd.h \ + lib/blockdev.h \ + lib/sd_blockdev.h \ + lib/sd_fat.h DISTFILES += \ Makefile \ diff --git a/devel/sdcard/sdcard.pro.user b/devel/sdcard/sdcard.pro.user index 28194b6..ad0c3d3 100644 --- a/devel/sdcard/sdcard.pro.user +++ b/devel/sdcard/sdcard.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId @@ -76,7 +76,7 @@ -r false - -B + 1 @@ -178,7 +178,7 @@ true - flash -B + flash make %{buildDir} Custom Process Step