diff --git a/bootloader_zevero_sd_m644p_make/.cproject b/bootloader_zevero_sd_m644p_make/.cproject new file mode 100644 index 0000000..d50f016 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/.cproject @@ -0,0 +1,65 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + make + + program + true + true + true + + + + + + + + + + diff --git a/bootloader_zevero_sd_m644p_make/.gitignore b/bootloader_zevero_sd_m644p_make/.gitignore new file mode 100644 index 0000000..3c2fcf6 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/.gitignore @@ -0,0 +1,10 @@ +*.o +*.eps +*.bak +*.a +*.bin +*.elf +*.hex +*.lst +*.map +/nbproject \ No newline at end of file diff --git a/bootloader_zevero_sd_m644p_make/.project b/bootloader_zevero_sd_m644p_make/.project new file mode 100644 index 0000000..faafa10 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/.project @@ -0,0 +1,27 @@ + + + bootloader_zevero_sd_m644p_make + + + + + + org.eclipse.cdt.managedbuilder.core.genmakebuilder + clean,full,incremental, + + + + + org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder + full,incremental, + + + + + + org.eclipse.cdt.core.cnature + org.eclipse.cdt.managedbuilder.core.managedBuildNature + org.eclipse.cdt.managedbuilder.core.ScannerConfigNature + de.innot.avreclipse.core.avrnature + + diff --git a/bootloader_zevero_sd_m644p_make/LICENSE b/bootloader_zevero_sd_m644p_make/LICENSE new file mode 100644 index 0000000..0c0bbe3 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/LICENSE @@ -0,0 +1,24 @@ +Copyright (c) 2015, zevero +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +* Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +* Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + diff --git a/bootloader_zevero_sd_m644p_make/Makefile b/bootloader_zevero_sd_m644p_make/Makefile new file mode 100644 index 0000000..a87bb6c --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/Makefile @@ -0,0 +1,70 @@ +#------------------------------------------------------------------ +# Makefile for stand-alone MMC boot strap loader +#------------------------------------------------------------------ +# Change these defs for the target device + +MCU_TARGET = atmega644p # Target device to be used (32K or larger) +BOOT_ADR = 0xF000 # Boot loader start address [byte] NOT [word] as in http://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega644p +F_CPU = 16000000 # CPU clock frequency [Hz] NOT critical: it just should be higher than the actual Hz +SD_CS_PORT = PORTB # Data Register of the SD CS pin +SD_CS_DDR = DDRB # Data Direction Register of the SD CS pin +SD_CS_BIT = 0 # Bit of the SD CS pin +USE_LED = 1 # Debug with two (defined in asmfunc.S) +USE_UART = 0 # Debug on Serial. 0 ... deactivate or divider of http://wormfood.net/avrbaudcalc.php for baud rate! +#------------------------------------------------------------------ +ifeq ($(strip $(USE_UART)),0) +CSRC = main.c pff/src/pff.c diskio.c +else +CSRC = main.c pff/src/pff.c diskio.c uart/uart.c +endif + +TARGET = bootloader_zevero_sd_m644p +ASRC = asmfunc.S +OPTIMIZE = -Os -mcall-prologues -ffunction-sections -fdata-sections +DEFS = -DBOOT_ADR=$(BOOT_ADR) -DF_CPU=$(F_CPU) -DUSE_LED=$(USE_LED) -DUSE_UART=$(USE_UART) -DSD_CS_PORT=$(SD_CS_PORT) -DSD_CS_DDR=$(SD_CS_DDR) -DSD_CS_BIT=$(SD_CS_BIT) +LIBS = +DEBUG = dwarf-2 + +ASFLAGS = -Wa,-adhlns=$(<:.S=.lst),-gstabs $(DEFS) +ALL_ASFLAGS = -mmcu=$(MCU_TARGET) -I. -x assembler-with-cpp $(ASFLAGS) +CFLAGS = -g$(DEBUG) -Wall $(OPTIMIZE) $(ADDED_CFLAGS) -mmcu=$(MCU_TARGET) -std=c99 $(DEFS) +LDFLAGS = -Wl,-Map,$(TARGET).map -Wl,--gc-sections -Wl,--section-start,.text=$(BOOT_ADR) +OBJ = $(CSRC:.c=.o) $(ASRC:.S=.o) + +CC = avr-gcc +OBJCOPY = avr-objcopy +OBJDUMP = avr-objdump +SIZE = avr-size + + +all: clean $(TARGET).elf lst text bin size + +$(TARGET).elf: $(OBJ) + $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $^ $(LIBS) + +clean: + rm -rf *.o $(TARGET).elf *.eps *.bak *.a *.bin + rm -rf pff/src/*.o uart/*.o + 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: $(TARGET).hex +%.hex: %.elf + $(OBJCOPY) -j .text -j .data -j .fuse -O ihex $< $@ +# --- make bin just to check size :) +bin: $(TARGET).bin +%.bin: %.hex + $(OBJCOPY) -I ihex -O binary $< $@ + +print-% : ; @echo $* = $($*) #test any var with make print-XXX + diff --git a/bootloader_zevero_sd_m644p_make/README.md b/bootloader_zevero_sd_m644p_make/README.md new file mode 100644 index 0000000..dd2b29a --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/README.md @@ -0,0 +1,86 @@ +avr_boot +======== + +SD card bootloader for atmega processors + +As easy as it can get! I spent days with this. Hopefully you wont! + +- for any ATMega with 4096kb Bootloader +- uses Petit FatFs R0.03 for FAT12, FAT16, FAT32 +- looks for FIRMWARE.BIN and flashes it nearly instantly +- without any interference to your application +- no CRC Check and no version bytes in EEPROM (see KISS) + +### Boards Manager installation + +avr_boot is integrated in Arduino IDE version 1.6.4 or greater!!! [See here for instructions.](https://github.com/zevero/avr_boot/tree/gh-pages) + +### Manual installation + +This is with avr-gcc and avrdude under linux with an Atmega1284p and AVRISP mkII! Adaption to your case (WinAvr, another Atmega, another flash-tool) will not be complicated... + +- adapt Makefile + - MCU_TARGET: Your atmegaXXX + - BOOT_ADR: in bytes not words! + - F_CPU: CPU Frequency (not critical. A higher value will work as well) + - SD_CS_PORT: Data Register of the SD CS pin(see the datasheet for your microcontroller) + - SD_CS_DDR: Data Direction Register of the SD CS pin + - SD_CS_BIT: Bit of the SD CS pin + - USE_LED: For debugging 0...deactivate or 1...active + - USE_UART: For debugging 0...deactivate or divider (UBRR) for baudate see http://wormfood.net/avrbaudcalc.php +- update spi_pins.h with the SPI pins of your microcontroller if not already defined +- if using USE_LED adapt LED-pins in asmfunc.S +- if you want to add FAT12 adapt pff/src/pffconfh.h (default ist FAT16 + FAT32) +- if you want to support lower case filenames adapt pff/src/pffconfh.h (default is uppercase) +- if you prefer another filename instead of FIRMWARE.BIN adapt main.c +- make (you may need to do "sudo apt-get install avr-libc gcc-avr") +- set fuses: avrdude -c avrispmkII -p m1284p -U hfuse:w:0xda:m + - find high fuse in http://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega1284p +- flash: avrdude -c avrispmkII -p m1284p -Uflash:w:./avr_boot.hex:i -Ulock:w:0x3F:m + +### Put your sketch on SD card + +- in Arduino IDE go to File > Preferences and check "Show verbose output during compiliation" +- compile sketch and find the location of your /tmp/buildxxx/sketch.cpp.hex +- make bin file: avr-objcopy -I ihex -O binary sketch.cpp.hex FIRMWARE.BIN +- copy the file into the root of an SD (FAT16/FAT32) +- put it into the SD slot of your ATmega +- reset it +- it might already have happend! + +### Bootloader sizes +Compiled under linux for atmega328p / atmega1284p + - 3674 / 3724 bytes + - 3984 / 4034 bytes debugging with USE_LED + - 3992 / 4052 bytes debugging with USE_UART + +### Tested successfully on + - ATmega168 + - ATmega328P + - ATmega32u4 + - ATmega1284P + - ATmega2560 (see issue #2) + +### Serial support - Help wanted +it should not be impossible to fit a normal serial bootloader (with automatic baudrate detection?) into the remaining bytes ... help is appreciated! + +### KISS +If you wish you *can* add CRC Check or versioning with EEPROM *but* I prefere to keep things simple. avr_boot will reflash your FIRMWARE.BIN as long as it is present. +Is this a problem? No! It happens nearly instantly and only differing bytes are flashed really. +You may consider putting your logic into your application and perform a CRC Check after the fact to inform the user and delete or rename FIRMWARE.BIN + +### Thanks to +- https://github.com/per1234 - Boards Manager Installation and help with differenct MCUs +- http://elm-chan.org/fsw/ff/00index_p.html +- Wilfried Klaas for the MCSDepthLogger https://github.com/willie68/OpenSeaMapLogger +- https://github.com/mharizanov/avr_boot +- https://github.com/osbock/avr_boot +- and others??? + +### Alternatives + +- https://spaces.atmel.com/gf/project/sdbootloader/ +- https://github.com/thseiler/embedded/tree/master/avr/2boots +- http://www.mikrocontroller.net/articles/MMC/SD_Bootloader_f%C3%BCr_AT_Mega + +... call me stupid, but I passed several days debugging those - without success ... diff --git a/bootloader_zevero_sd_m644p_make/asmfunc.S b/bootloader_zevero_sd_m644p_make/asmfunc.S new file mode 100644 index 0000000..099a8d1 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/asmfunc.S @@ -0,0 +1,271 @@ +;---------------------------------------------------------------------------; +; MMC hardware controls and Flash controls (C)ChaN, 2010 +;---------------------------------------------------------------------------; +; Hardware dependent macros to be modified //do this in Makefile +#include "spi_pins.h" + +; ALL Pins given as Port (A,B,C,...) plus number + +; LED Pins +;#define DDR_SS _SFR_IO_ADDR(DDRD), 5 // SS pin (PIN, PORT) +;#define PORT_SS _SFR_IO_ADDR(PORTD), 5 +#define DDR_SS _SFR_IO_ADDR(DDRC), 3 // SS pin (PIN, PORT) +#define PORT_SS _SFR_IO_ADDR(PORTC), 3 + +;#define DDR_PW _SFR_IO_ADDR(DDRD), 6 // Power pin (PIN, PORT) +;#define PORT_PW _SFR_IO_ADDR(PORTD), 6 + +;SD CARD PINS +#define DDR_CS _SFR_IO_ADDR(SD_CS_DDR), SD_CS_BIT +#define PORT_CS _SFR_IO_ADDR(SD_CS_PORT), SD_CS_BIT + +;---------------------------------------------------------------------------; +.nolist +#include +.list +.text + +.global init_leds +.func init_leds +init_leds: + sbi DDR_SS +; sbi DDR_PW + ret +.endfunc + + +.global led_write_on +.func led_write_on +led_write_on: + sbi PORT_SS + ret +.endfunc + +.global led_write_off +.func led_write_off +led_write_off: + cbi PORT_SS + ret +.endfunc + +;.global led_power_on +;.func led_power_on +;led_power_on: +; sbi PORT_PW +; ret +;.endfunc + +;.;global led_power_off +;.;func led_power_off +;led_power_off: +; cbi PORT_PW +; ret +;.endfunc + +;.;global led_power_toggle +;.func led_power_toggle +;led_power_toggle: +; sbis PORT_PW +; jmp led_power_on +; jmp led_power_off +;.endfunc + +.global led_write_toggle +.func led_write_toggle +led_write_toggle: + sbis PORT_SS + jmp led_write_on + jmp led_write_off +.endfunc + +;---------------------------------------------------------------------------; +; Initialize MMC port +; +; void init_spi (void); + +.global init_spi +.func init_spi +init_spi: + sbi DDR_CS ; CS: output + sbi DDR_DI ; DI: output + sbi DDR_CK ; SCLK: output + sbi PORT_DO ; DO: pull-up + ret +.endfunc + + + +;---------------------------------------------------------------------------; +; Delay 100 microseconds +; +; void dly_us (UINT n); + +.global dly_100us +.func dly_100us +dly_100us: + ldi r24, lo8(F_CPU / 100000) /* Loop counter */ +1: sbiw r30, 1 /* 10 clocks per loop */ + sbiw r30, 1 + sbiw r30, 1 + nop + dec r24 + brne 1b + ret +.endfunc + + + +;---------------------------------------------------------------------------; +; Select MMC +; +; void select (void); + +.global select +.func select +select: + rcall deselect + cbi PORT_CS + rjmp rcv_spi +.endfunc + + + +;---------------------------------------------------------------------------; +; Deselect MMC +; +; void deselect (void); + +.global deselect +.func deselect +deselect: + sbi PORT_CS + ; Goto next function +.endfunc + + + +;---------------------------------------------------------------------------; +; Receive a byte +; +; BYTE rcv_spi (void); + +.global rcv_spi +.func rcv_spi +rcv_spi: + ldi r24, 0xFF ; Send 0xFF to receive data + ; Goto next function +.endfunc + + + +;---------------------------------------------------------------------------; +; Transmit a byte +; +; void xmit_spi (BYTE); + +.global xmit_spi +.func xmit_spi +xmit_spi: + ldi r25, 8 +1: sbrc r24, 7 ; DI = Bit to sent + sbi PORT_DI ; + sbrs r24, 7 ; + cbi PORT_DI ; / + lsl r24 ; Get DO from MMC + sbic PIN_DO ; + inc r24 ; / + sbi PORT_CK ; A positive pulse to SCLK + cbi PORT_CK ; / + dec r25 ; Repeat 8 times + brne 1b ; / + ret +.endfunc + + + +;--------------------------------------------------------------------------- +; Erase a flash page +; +; void flash_erase (DWORD flash_addr); + +#ifndef SPMCSR +#define SPMCSR SPMCR +#endif + +.global flash_erase +.func flash_erase +flash_erase: + + movw ZL, r22 +#if FLASHEND >= 0x10000 + out _SFR_IO_ADDR(RAMPZ), r24 +#endif + + ; Initiate erase operation + ldi r24, 0b00000011 + sts _SFR_MEM_ADDR(SPMCSR), r24 + spm + + ; Wait for end of erase operation +1: lds r24, _SFR_MEM_ADDR(SPMCSR) + sbrc r24, 0 + rjmp 1b + + ; Re-enable read access to the flash + ldi r24, 0b00010001 + sts _SFR_MEM_ADDR(SPMCSR), r24 + spm + +9: ret +.endfunc + + + +;--------------------------------------------------------------------------- +; Write a flash page +; +; void flash_write (DWORD flash_addr, const BYTE* data); + +.global flash_write +.func flash_write +flash_write: + push r0 + push r1 + +#if FLASHEND >= 0x10000 + out _SFR_IO_ADDR(RAMPZ), r24 +#endif + + ; Fill page buffer + movw ZL, r22 + movw XL, r20 + ldi r25, lo8(SPM_PAGESIZE/2) +1: ld r0, X+ + ld r1, X+ + ldi r24, 0b00000001 + sts _SFR_MEM_ADDR(SPMCSR), r24 + spm + adiw ZL, 2 + dec r25 + brne 1b + + ; Initiate write operation + movw ZL, r22 + ldi r24, 0b00000101 + sts _SFR_MEM_ADDR(SPMCSR), r24 + spm + + ; Wait for end of write operation +2: lds r24, _SFR_MEM_ADDR(SPMCSR) + sbrc r24, 0 + rjmp 2b + + ; Re-enable read access to the flash + ldi r24, 0b00010001 + sts _SFR_MEM_ADDR(SPMCSR), r24 + spm + +9: pop r1 + pop r0 + ret +.endfunc diff --git a/bootloader_zevero_sd_m644p_make/bootloader_zevero_sd_m644p.hex.bootloader b/bootloader_zevero_sd_m644p_make/bootloader_zevero_sd_m644p.hex.bootloader new file mode 100644 index 0000000..efd7b70 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/bootloader_zevero_sd_m644p.hex.bootloader @@ -0,0 +1,245 @@ +:10F000000C943E780C9468780C9468780C9468782A +:10F010000C9468780C9468780C9468780C946878F0 +:10F020000C9468780C9468780C9468780C946878E0 +:10F030000C9468780C9468780C9468780C946878D0 +:10F040000C9468780C9468780C9468780C946878C0 +:10F050000C9468780C9468780C9468780C946878B0 +:10F060000C9468780C9468780C9468780C946878A0 +:10F070000C9468780C9468780C94687811241FBEFE +:10F08000CFEFD0E1DEBFCDBF87EF84BF0FB6F894DE +:10F09000A895809160008861809360001092600064 +:10F0A0000FBE11E0A0E0B1E0E2E1FFEF02C0059089 +:10F0B0000D92AE30B107D9F722E0AEE0B1E001C069 +:10F0C0001D92AB33B207E1F70E9449790C94877F18 +:10F0D0000C9400783B9A0895439A089543980895B4 +:10F0E000439B0C946C780C946E78209A259A279AFE +:10F0F0002E9A089580EA31973197319700008A95CA +:10F10000D1F7089502D0289801C0289A8FEF98E08F +:10F1100087FD2D9A87FF2D98880F1E9983952F9A2A +:10F120002F989A95A9F70895FB0183E080935700E3 +:10F13000E8958091570080FDFCCF81E180935700D6 +:10F14000E89508950F921F92FB01DA0190E80D9067 +:10F150001D9081E080935700E89532969A95B9F713 +:10F16000FB0185E080935700E8958091570080FD72 +:10F17000FCCF81E180935700E8951F900F90089590 +:10F18000CF92DF92EF92FF921F93CF93DF9300D045 +:10F19000CDB7DEB712E30E9470782FEF81EE94E0D6 +:10F1A000215080409040E1F700C00000115099F7D5 +:10F1B000C12CD12C760140E051E06FEF70E08BE381 +:10F1C00091E00E94807FAE014F5F5F4F60E071E091 +:10F1D0008BE391E00E94287DABE3B1E0C6018B5345 +:10F1E0009140FC01EA0FFB1FE4912D91E2131AC03C +:10F1F00022E0AB33B207A9F70E946C788FEFD81AE0 +:10F20000E80AF80AC11490EFD906E104F10499F66E +:10F210000F900F90DF91CF911F91FF90EF90DF90B3 +:10F22000CF9008950E946E78C701B6010E9494782D +:10F230004BE351E0C701B6010E94A278DFCFCF9324 +:10F2400081E191E00E94A37A811121C080E091E0E8 +:10F250000E94907B81111BC00E94C0780E946E7832 +:10F260002FEF87EA91E6215080409040E1F700C0FF +:10F270000000C8E20E9470782FEF80E792E02150F2 +:10F2800080409040E1F700C00000C15099F7CF9155 +:10F2900008950E946A7800E010E00E946C782FEFD9 +:10F2A00083EC99E0215080409040E1F700C00000DD +:10F2B0000E946E780E941F79F801859194910196C1 +:10F2C00011F0F8010995CAE00E947078FFEF23EC75 +:10F2D00089E0F15020408040E1F700C00000C150BB +:10F2E00099F79FEFE3E2F4EF9150E040F040E1F74F +:10F2F00000C00000D2CF0F931F93E0910E01F09158 +:10F300000F019B01AC01225031094109510986814D +:10F310009781A085B1850297A109B10928173907FE +:10F320004A075B0768F4A281B0E00E942F7F028940 +:10F33000138924893589600F711F821F931F03C0B1 +:10F3400060E070E0CB011F910F9108950F931F9320 +:10F35000CF93DF93DC01E0910E01F0910F01119644 +:10F360001C921E9214966D917D918D919C91179790 +:10F37000613071058105910569F10681178120854C +:10F380003185601771078207930720F561157105B4 +:10F390008105910539F42081233021F46685778534 +:10F3A00080899189ED01688779878A879B876115B4 +:10F3B00071058105910519F00E947B7904C066856D +:10F3C0007785808991896C877D878E879F8780E08C +:10F3D00090E002C081E090E0DF91CF911F910F910A +:10F3E0000895A4E0B0E0E7EFF9E70C94447FE091E2 +:10F3F0000E01F0910F01623071058105910508F44D +:10F4000059C0C680D780E084F1846C157D058E05D7 +:10F410009F0508F04FC02081223019F0233001F100 +:10F4200049C09B013327220F331FBB27A92F982FD9 +:10F43000872FC284D384E484F584AC01BD014C0DD4 +:10F440005D1D6E1D7F1D02E010E0CE0101960E9441 +:10F45000BE7E892B79F569817A8180E090E02EC0AB +:10F460009B012F773327220F331F220F331FDC011D +:10F47000CB0157E0B695A795979587955A95D1F703 +:10F48000C284D384E484F584AC01BD014C0D5D1DC0 +:10F490006E1D7F1D04E010E0CE0101960E94BE7E2D +:10F4A000892B41F489819A81AB81BC81BC01CD015A +:10F4B0009F7004C061E070E080E090E02496E8E096 +:10F4C0000C94607FA0E0B0E0E8E6FAE70C94447F9B +:10F4D000EC016A017B0102E010E02EEF31E00E94B6 +:10F4E000BE7E892B31F58881998185359A4A19F537 +:10F4F00026E330E0B701A601CE010E94BE7E892B33 +:10F5000039F4888199818634914411F480E016C0E1 +:10F5100002E010E022E530E0B701A601CE010E9432 +:10F52000BE7E892B51F4888199818634914429F4D7 +:10F53000EDCF83E003C082E001C081E0CDB7DEB74C +:10F54000E8E00C94607FA4E2B0E0E9EAFAE70C940A +:10F550003A7F1C0110920F0110920E010E94237E2F +:10F5600080FDA0C040E050E0BA01CE0101960E94AB +:10F57000627A813009F500E110E02EEB31E040E0E5 +:10F5800050E0BA01CE0101960E94BE7E892B19F08F +:10F5900081E090E0C1C08D81811103C086E090E0E0 +:10F5A000BBC049845A846B847C84B301A201CE0120 +:10F5B00001960E94627A03C0412C512C32018330A3 +:10F5C00039F38111EBCF04E210E02DE030E0B3011C +:10F5D000A201CE0101960E94BE7E892BC9F62A8522 +:10F5E0003B8540E050E0211531054105510521F4EE +:10F5F000288D398D4A8D5B8DAC81B0E00E942F7FC4 +:10F600004B015C010A811B8120E030E0400E511E5D +:10F61000621E731EF1014286538664867586498197 +:10F620004283CD80DE80D582C4828F819885A0E020 +:10F63000B0E00097A105B10521F48C899D89AE89C0 +:10F64000BF8954E0D694C7945A95E1F7E12CF12C88 +:10F65000801B910BA20BB30B88199909AA09BB094E +:10F66000BC01CD016C197D098E099F09242F30E062 +:10F6700040E050E00E940D7FDA01C9010296A11D11 +:10F68000B11DF10186839783A087B187BA01A901D3 +:10F69000465F5F40610971094F3F5F4E610571052B +:10F6A000A0F103C082E090E037C0873F9F4FA105E3 +:10F6B000B10508F473CF83E0F1018083833029F42E +:10F6C00088A199A1AAA1BBA107C0D501C401840D3D +:10F6D000951DA61DB71DF10186879787A08BB18B5D +:10F6E000C40CD51CE61CF71CC80CD91CEA1CFB1C5E +:10F6F000F101C28AD38AE48AF58A118230920F011D +:10F7000020920E0180E090E007C0873F9F4FA10547 +:10F71000B10588F682E0D0CFA496E2E10C94567F42 +:10F720004F925F926F927F929F92AF92BF92CF92D1 +:10F73000DF92EF92FF920F931F93CF93DF93CDB79A +:10F74000DEB7EC970FB6F894DEBF0FBECDBFC0900A +:10F750000E01D0900F01C114D10409F452C1D60199 +:10F7600011961C929E012F5C3F4F3CA32BA3FC01E2 +:10F770002E2F9F2F81918032D9F3E22EF92E8F32D6 +:10F7800019F4BFEFEB1AFB0A1DA21EA21FA218A6B6 +:10F79000F7018081803238F0A0E29A2E5E01FCE011 +:10F7A000AF0EB11C6AC0CE0181960E94A679198263 +:10F7B000009709F4F1C036C180E090E028E031E024 +:10F7C000390FD701A90FB11D9C91913208F060C08B +:10F7D000E30EF11C81E0913208F080E08387CE01D6 +:10F7E00081960E94A679009709F01CC129A13AA12F +:10F7F0002F703327F5E0220F331FFA95E1F74DA55F +:10F800005EA56FA578A900E210E0CE0101960E94E6 +:10F81000BE7E892B09F0FAC08981882309F4F9C0DA +:10F820004C8543FD53C06BA17CA1FE0131969B0129 +:10F83000EA15FB05E9F5DB011B968C918111ACC043 +:10F8400044FFE7C0E0910E01F0910F018081833009 +:10F8500009F09EC08D899E89A0E0B0E0DC01992767 +:10F8600088274B8D5C8D60E070E0842B952BA62B58 +:10F87000B72B8DA39EA3AFA3B8A7EBA1FCA1DF017B +:10F88000CF010B96A817B90709F496CF9D92FACF2E +:10F890009F3209F49DCF9E3209F4BEC0821708F052 +:10F8A00097CFDF01A80FB11D9C938F5F932F87CF58 +:10F8B0008191D9015D919D01082E000C990B851B4A +:10F8C000910957FD9395892B09F4B2CF60900E01F1 +:10F8D00070900F0109A11AA10F5F1F4F09F499C081 +:10F8E0008DA59EA5AFA5B8A90097A105B10509F4FE +:10F8F00090C098012F703327232B09F046C0019642 +:10F90000A11DB11D8DA79EA7AFA7B8AB69A57AA50C +:10F910008BA59CA5611571058105910541F4F30145 +:10F92000848195810817190708F073C02EC0D30190 +:10F9300012962C9130E0A901415051099801E4E060 +:10F9400036952795EA95E1F724233523232BE9F40F +:10F950000E94F179623071058105910508F456C065 +:10F96000F3014680578060847184641575058605AF +:10F97000970508F04EC069A77AA78BA79CA70E949D +:10F980007B796DA77EA78FA798AB1AA309A32ECF6B +:10F9900080E090E0DC0165CF89818823A1F18C852E +:10F9A00084FD31C0E0910E01F0910F018081833020 +:10F9B00041F48D899E89A0E0B0E0DC019927882779 +:10F9C00003C080E090E0DC014B8D5C8D60E070E076 +:10F9D000842B952BA62BB72BF601868F978FA0A390 +:10F9E000B1A38D8D9E8DAF8DB8A1828F938FA48F83 +:10F9F000B58F168A178A108E118E81E0818380E080 +:10FA000003C085E001C083E090E00CC081E090E09D +:10FA100009C083E090E006C0283009F0D9CE88E024 +:10FA20002BE044CFEC960FB6F894DEBF0FBECDBFEF +:10FA3000DF91CF911F910F91FF90EF90DF90CF90CA +:10FA4000BF90AF909F907F906F905F904F90089580 +:10FA5000A0E0B0E0EEE2FDE70C943C7F4C016B01CE +:10FA60005A01C0910E01D0910F01FA01118210824A +:10FA7000209709F4A1C0898180FFA1C08A8D9B8D48 +:10FA8000AC8DBD8D4E895F89688D798D841B950BFA +:10FA9000A60BB70BA60160E070E084179507A607D8 +:10FAA000B70708F46C013401412C32E0532EC11425 +:10FAB000D10409F47EC08E899F89A88DB98DAC01CF +:10FAC000BD01517066277727452B462B472BD1F573 +:10FAD000FA80FA94AC01BD0129E0769567955795B7 +:10FAE00047952A95D1F7F422C1F4892B8A2B8B2BC9 +:10FAF00029F46E8D7F8D88A199A106C06AA17BA192 +:10FB00008CA19DA10E94F17962307105810591055A +:10FB1000A0F16AA37BA38CA39DA36AA17BA18CA166 +:10FB20009DA10E947B79611571058105910529F1DF +:10FB3000DC01CB018F0D911DA11DB11D8EA39FA3D3 +:10FB4000A8A7B9A72E893F893170C201821B930BE8 +:10FB500076018C159D0508F47C014EA15FA168A576 +:10FB600079A58114910411F0C30102C080E090E0F6 +:10FB700087010E94BE7E892B21F0198281E090E0EE +:10FB800020C08E899F89A88DB98D8E0D9F1DA11DC6 +:10FB9000B11D8E8B9F8BA88FB98F6E0C7F1CCE18DA +:10FBA000DF08F50180819181E80EF91EF182E08283 +:10FBB0007ECF80E090E005C085E090E002C084E068 +:10FBC00090E0CDB7DEB7E0E10C94587FFF920F9341 +:10FBD0001F93CF93DF93C82FD42F152F062FF72E07 +:10FBE00087FF09C040E050E0BA0187E70E94E67D48 +:10FBF000823020F5CF770E9482788C2F0E94877800 +:10FC00008F2D0E948778802F0E948778812F0E94F5 +:10FC100087788D2F0E948778C03421F0C83421F472 +:10FC200087E803C085E901C081E00E948778CAE0C7 +:10FC30000E94867887FF02C0C150D1F7CDB7DEB7EA +:10FC4000E5E00C94637FA4E0B0E0E9E2FEE70C9409 +:10FC5000457F0E94757814E60E947A781150E1F78A +:10FC60001AE00E9485781150E1F740E050E0BA01B7 +:10FC700080E40E94E67D813009F07AC04AEA51E0D2 +:10FC800060E070E088E40E94E67D813051F58E01ED +:10FC90000F5F1F4F780124E0D22ED00E0E9486788D +:10FCA000F70181937F01DE12F9CF8B81813009F05A +:10FCB0005FC08C818A3A09F05BC090E1E92E97E23F +:10FCC000F92E40E050E060E070E489EE0E94E67DAD +:10FCD000882339F10E947A78F1E0EF1AF10889F768 +:10FCE00047C040E050E0BA0189EE0E94E67D8230D4 +:10FCF00018F011E001E402C012E009EE80E1E82E04 +:10FD000087E2F82E40E050E0BA01802F0E94E67DA5 +:10FD1000882329F10E947A78F1E0EF1AF10891F72F +:10FD200027C040E050E0BA018AE70E94E67D8111D9 +:10FD30001FC00E948678F80181938F01DE12F9CFEF +:10FD4000898186FD02C014E001C01CE010931001FF +:10FD50000E94857881E0112369F080E00BC040E0CB +:10FD600052E060E070E080E50E94E67D882371F358 +:10FD700010E0ECCF2496E7E00C94617FA0E0B0E0C7 +:10FD8000E4ECFEE70C94447FD82EC92E79018091D3 +:10FD9000100183FD07C089E0440F551F661F771FC0 +:10FDA0008A95D1F781E50E94E67D882319F0C1E0AC +:10FDB000D0E02BC0C0E4DCE90E9486788F3F19F4C4 +:10FDC0002197D1F7F4CF8E3F91F7C2E0D2E0C01B6C +:10FDD000D10BCE19DF09E114F10419F4ED2CFC2C40 +:10FDE00006C00E94867881E0E81AF108F4CF0E94EC +:10FDF0008678F70181937F0101501109C1F70E94B4 +:10FE000086782197E1F7C0E0D0E00E948578CE01A6 +:10FE1000CDB7DEB7E8E00C94607FA1E21A2EAA1BF2 +:10FE2000BB1BFD010DC0AA1FBB1FEE1FFF1FA217AA +:10FE3000B307E407F50720F0A21BB30BE40BF50BA7 +:10FE4000661F771F881F991F1A9469F76095709530 +:10FE5000809590959B01AC01BD01CF0108950E9452 +:10FE6000717FA59F900DB49F900DA49F800D911D53 +:10FE7000112408952F923F924F925F926F927F923A +:10FE80008F929F92AF92BF92CF92DF92EF92FF92AA +:10FE90000F931F93CF93DF93CDB7DEB7CA1BDB0B56 +:10FEA0000FB6F894DEBF0FBECDBF09942A883988FB +:10FEB00048885F846E847D848C849B84AA84B98402 +:10FEC000C884DF80EE80FD800C811B81AA81B9810E +:10FED000CE0FD11D0FB6F894DEBF0FBECDBFED0122 +:10FEE0000895A29FB001B39FC001A39F700D811D13 +:10FEF0001124911DB29F700D811D1124911D089533 +:10FF0000DC0101C06D9341505040E0F70895F89432 +:02FF1000FFCF21 +:0EFF1200363434424F4F542E42494E00000008 +:040000030000F00009 +:00000001FF diff --git a/bootloader_zevero_sd_m644p_make/diskio.c b/bootloader_zevero_sd_m644p_make/diskio.c new file mode 100644 index 0000000..4608c5e --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/diskio.c @@ -0,0 +1,226 @@ +/*-----------------------------------------------------------------------*/ +/* Low level disk I/O module skeleton for Petit FatFs (C)ChaN, 2014 */ +/*-----------------------------------------------------------------------*/ + +#include "pff/src/pff.h" +#include "pff/src/diskio.h" + +void init_spi (void); /* Initialize SPI port (asmfunc.S) */ +void deselect (void); /* Select MMC (asmfunc.S) */ +void select (void); /* Deselect MMC (asmfunc.S) */ +void xmit_spi (BYTE d); /* Send a byte to the MMC (asmfunc.S) */ +BYTE rcv_spi (void); /* Send a 0xFF to the MMC and get the received byte (asmfunc.S) */ +void dly_100us (void); /* Delay 100 microseconds (asmfunc.S) */ + + + +/*-------------------------------------------------------------------------- + + Module Private Functions + +---------------------------------------------------------------------------*/ + +/* 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 CMD16 (0x40+16) /* SET_BLOCKLEN */ +#define CMD17 (0x40+17) /* READ_SINGLE_BLOCK */ +#define CMD24 (0x40+24) /* WRITE_BLOCK */ +#define CMD55 (0x40+55) /* APP_CMD */ +#define CMD58 (0x40+58) /* READ_OCR */ + + +/* Card type flags (CardType) */ +#define CT_MMC 0x01 /* MMC ver 3 */ +#define CT_SD1 0x02 /* SD ver 1 */ +#define CT_SD2 0x04 /* SD ver 2 */ +#define CT_BLOCK 0x08 /* Block addressing */ + + +static +BYTE CardType; + + +/*-----------------------------------------------------------------------*/ +/* Send a command packet to MMC */ +/*-----------------------------------------------------------------------*/ + +static +BYTE send_cmd ( + BYTE cmd, /* 1st byte (Start + Index) */ + DWORD arg /* Argument (32 bits) */ +) +{ + BYTE n, res; + + + if (cmd & 0x80) { /* ACMD is the command sequense of CMD55-CMD */ + cmd &= 0x7F; + res = send_cmd(CMD55, 0); + if (res > 1) return res; + } + + /* Select the card */ + select(); + + /* Send a 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 a command response */ + n = 10; /* Wait for a valid response in timeout of 10 attempts */ + do { + res = rcv_spi(); + } while ((res & 0x80) && --n); + + return res; /* Return with the response value */ +} + + + + +/*-------------------------------------------------------------------------- + + Public Functions + +---------------------------------------------------------------------------*/ +/*-----------------------------------------------------------------------*/ +/* Initialize Disk Drive */ +/*-----------------------------------------------------------------------*/ + +DSTATUS disk_initialize (void) +{ + DSTATUS stat; + + BYTE n, cmd, ty, ocr[4]; + UINT tmr; + + + init_spi(); /* Initialize ports to control MMC */ + for (n = 100; n; n--) dly_100us(); /* 10ms delay */ + for (n = 10; n; n--) deselect(); /* 80 Dummy clocks with CS=H */ + + ty = 0; + if (send_cmd(CMD0, 0) == 1) { /* Enter Idle state */ + if (send_cmd(CMD8, 0x1AA) == 1) { /* SDv2 */ + for (n = 0; n < 4; n++) ocr[n] = rcv_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 */ + for (tmr = 10000; tmr && send_cmd(ACMD41, 1UL << 30); tmr--) dly_100us(); /* Wait for leaving idle state (ACMD41 with HCS bit) */ + if (tmr && send_cmd(CMD58, 0) == 0) { /* Check CCS bit in the OCR */ + for (n = 0; n < 4; n++) ocr[n] = rcv_spi(); + ty = (ocr[0] & 0x40) ? CT_SD2 | CT_BLOCK : CT_SD2; /* SDv2 (HC or SC) */ + } + } + } else { /* SDv1 or MMCv3 */ + if (send_cmd(ACMD41, 0) <= 1) { + ty = CT_SD1; cmd = ACMD41; /* SDv1 */ + } else { + ty = CT_MMC; cmd = CMD1; /* MMCv3 */ + } + for (tmr = 10000; tmr && send_cmd(cmd, 0); tmr--) dly_100us(); /* Wait for leaving idle state */ + if (!tmr || send_cmd(CMD16, 512) != 0) /* Set R/W block length to 512 */ + ty = 0; + } + } + CardType = ty; + deselect(); + + stat = ty ? 0 : STA_NOINIT; + + return stat; +} + + + +/*-----------------------------------------------------------------------*/ +/* Read Partial Sector */ +/*-----------------------------------------------------------------------*/ + +DRESULT disk_readp ( + BYTE* buff, /* Pointer to the destination object */ + DWORD sector, /* Sector number (LBA) */ + UINT offset, /* Offset in the sector */ + UINT count /* Byte count (bit15:destination) */ +) +{ + DRESULT res; + BYTE rc; + WORD bc; + + if (!(CardType & CT_BLOCK)) sector *= 512; /* Convert to byte address if needed */ + + res = RES_ERROR; + if (send_cmd(CMD17, sector) == 0) { /* READ_SINGLE_BLOCK */ + + bc = 40000; + do { /* Wait for data packet */ + rc = rcv_spi(); + } while (rc == 0xFF && --bc); + + if (rc == 0xFE) { /* A data packet arrived */ + bc = 514 - offset - count; + + /* Skip leading bytes */ + if (offset) { + do rcv_spi(); while (--offset); + } + + /* Receive a part of the sector */ + do { + *buff++ = rcv_spi(); + } while (--count); + + /* Skip trailing bytes and CRC */ + do rcv_spi(); while (--bc); + + res = RES_OK; + } + } + + deselect(); + + return res; +} + + + +/*-----------------------------------------------------------------------*/ +/* Write Partial Sector */ +/*-----------------------------------------------------------------------*/ +/* +DRESULT disk_writep ( + const BYTE* buff, // Pointer to the data to be written, NULL:Initiate/Finalize write operation + DWORD sc // Sector number (LBA) or Number of bytes to send +) +{ + DRESULT res; + + + if (!buff) { + if (sc) { + + // Initiate write process + + } else { + + // Finalize write process + + } + } else { + + // Send data to the disk + + } + + return res; +} +*/ diff --git a/bootloader_zevero_sd_m644p_make/m644p_zevero_sd_m644p_fuses.txt b/bootloader_zevero_sd_m644p_make/m644p_zevero_sd_m644p_fuses.txt new file mode 100644 index 0000000..5409b45 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/m644p_zevero_sd_m644p_fuses.txt @@ -0,0 +1,29 @@ +3) Working FUSE for M644p (with bootloader Zevero-SD) +I.e - Arduino Bootloader to Flash from SD Card: https://github.com/zevero/avr_boot + +PS. SET FUSES: +============== + LOW = 0xFF + HIGH = 0xDA + EXTD = 0xFD +======================================== +It means: +Ext. XTAL High FREQ START-UP 16K CK + 65ms +JTAG DISABLED +BROWN-OUT 2.7V ON +BOOT-LOADER ENABLED +BOOT Flash Section Size = 2048 words (4096 bytes); Boot Start address = 0x7800 words (0xF000 bytes) +========================================= + +AVRDUDE arguments: +-U lfuse:w:0xff:m -U hfuse:w:0xda:m -U efuse:w:0xfd:m + +Full commands to write from console: +avrdude -Pusb -cavrispmkii -patmega644p -B5 -U lfuse:w:0xff:m -U hfuse:w:0xda:m -U efuse:w:0xfd:m + + +Detection avrdude CPU: B5 (5us bit clock period for programmer SPI) - VERY IMPORTANT (1Mhz CPU M1284p/M644p FREQ DEFAULT) +avrdude -Pusb -cavrispmkii -patmega644p -B5 + +Check m644p fuse here: +http://eleccelerator.com/fusecalc/fusecalc.php?chip=atmega644p diff --git a/bootloader_zevero_sd_m644p_make/main.c b/bootloader_zevero_sd_m644p_make/main.c new file mode 100644 index 0000000..1d51476 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/main.c @@ -0,0 +1,259 @@ +/*-------------------------------------------------------------------------/ +/ Stand-alone MMC boot loader R0.01 +/--------------------------------------------------------------------------/ +/ +/ Copyright (C) 2010, ChaN, all right reserved. +/ +/ * This software 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 products UNDER YOUR RESPONSIBILITY. +/ * Redistributions of source code must retain the above copyright notice. +/ +/--------------------------------------------------------------------------/ +/ Dec 6, 2010 R0.01 First release +/--------------------------------------------------------------------------/ +/ This is a stand-alone MMC/SD boot loader for megaAVRs. It requires a 4KB +/ boot section for code, four GPIO pins for MMC/SD as shown in sch.jpg and +/ nothing else. To port the boot loader into your project, follow the +/ instruction described below. +/ +/ 1. Setup the hardware. Attach a memory card socket to the any GPIO port +/ where you like. Select boot size at least 4KB for the boot loader with +/ BOOTSZ fuses and enable boot loader with BOOTRST fuse. +/ +/ 2. Setup the software. Change the four port definitions in the asmfunc.S. +/ Change MCU_TARGET, BOOT_ADR and MCU_FREQ in the Makefile. The BOOT_ADR +/ is a BYTE address of boot section in the flash. Build the boot loader +/ and write it to the device with a programmer. +/ +/ 3. Build the application program and output it in binary form instead of +/ hex format. Rename the file "app.bin" and put it into the memory card. +/ +/ 4. Insert the card and turn the target power on. When the boot loader found +/ the application file, the file is written into the flash memory prior to +/ start the application program. On-board LED lights (if exist) during +/ the flash programming operation. +/ +/-------------------------------------------------------------------------*/ + +const char filename[13] ="644BOOT.BIN\0"; // EDIT FILENAME HERE +#include +#include +#include +#include +#include +#include "pff/src/pff.h" +#include //Watchdog +// The following code is recommended in http://avr-libc.nongnu.org/user-manual/group__avr__watchdog.html but is disabled for now because avr_boot doesn't currently do anything with mcusr_mirror so for now we will only reset MCUSR and disable WDT. +//uint8_t mcusr_mirror __attribute__ ((section (".noinit")));void get_mcusr(void) __attribute__((naked)) __attribute__((section(".init3")));void get_mcusr(void){mcusr_mirror = MCUSR;MCUSR = 0;wdt_disable();} +void disable_watchdog(void) __attribute__((naked)) __attribute__((section(".init3"))); +void disable_watchdog(void) +{ +#if defined(MCUCSR) + MCUCSR = ~(_BV(WDRF)); //Some MCUs require the watchdog reset flag to be cleared before WDT can be disabled. & operation is skipped to spare few bytes as bits in MCUSR can only be cleared. +#else + MCUSR = ~(_BV(WDRF)); //Some MCUs require the watchdog reset flag to be cleared before WDT can be disabled. & operation is skipped to spare few bytes as bits in MCUSR can only be cleared. +#endif + wdt_disable(); //immediately disable watchdog in case it was running in the application to avoid perpetual reset loop +} + +#if BOOT_ADR > 0xFFFF + #define PGM_READ_BYTE(x) pgm_read_byte_far(x) +#else + #define PGM_READ_BYTE(x) pgm_read_byte(x) +#endif + +#if USE_UART + #include "uart/uart.h" +#endif + +#if USE_LED +void init_leds(); +void led_power_on(); +void led_power_off(); +void led_power_toggle(); +void led_write_on(); +void led_write_off(); +void led_write_toggle(); +#endif +void flash_erase (DWORD); /* Erase a flash page (asmfunc.S) */ +void flash_write (DWORD, const BYTE*); /* Program a flash page (asmfunc.S) */ + + +FATFS Fatfs; // Petit-FatFs work area +BYTE Buff[SPM_PAGESIZE]; // Page data buffer + + + +static uint8_t pagecmp(const DWORD fa, uint8_t buff[SPM_PAGESIZE]) +{ + UINT i; + uint8_t b_flash,b_buff; + for (i = 0; i < SPM_PAGESIZE; i++) { + b_flash = PGM_READ_BYTE(fa+i); + b_buff = buff[i]; + if ( b_flash != b_buff) { + #if USE_UART //output first difference + UART_puthex32(fa);UART_puts(PSTR(":")); + UART_puthex(b_flash);UART_puts(PSTR(" ")); + UART_puthex(b_buff); UART_newline(); + #endif + return 1; + } + } + #if USE_UART //output first difference + UART_puthex32(fa);UART_puts(PSTR(":")); + UART_puts(PSTR("="));UART_newline(); + #endif + return 0; +} + +void doFlash() { + DWORD fa; /* Flash address */ + UINT br; /* Bytes read */ + #if USE_LED + uint8_t i; + for(i=0;i<50;i++) { led_write_toggle();_delay_ms(100);} //Start Programming: Flash WRITE Wildly for 5 secs + #endif + + + for (fa = 0; fa < BOOT_ADR; fa += SPM_PAGESIZE) { /* Update all application pages */ + + memset(Buff, 0xFF, SPM_PAGESIZE); /* Clear buffer */ + pf_read(Buff, SPM_PAGESIZE, &br); /* Load a page data */ + + if (pagecmp(fa, Buff)) { /* Only flash if page is changed */ + #if USE_LED + led_write_off(); + //led_power_on(); + #endif + flash_erase(fa); /* Erase a page */ + flash_write(fa, Buff); /* Write it if the data is available */ + + } else { + + #if USE_LED + //led_power_off(); + led_write_on(); + #endif + } + } +} + +void checkFile() { + uint8_t fresult; + + fresult = pf_mount(&Fatfs); /* Initialize file system */ + + if (fresult != FR_OK) { /* File System could not be mounted */ + #if USE_UART + UART_puts(PSTR("File not mounted")); + UART_newline(); + #endif + + #if USE_LED + //uint8_t i; + //led_write_on(); + //for(i=0;i<2*fresult;i++) { led_power_toggle();_delay_ms(500);}//Give error number while Write led is on + //led_write_off(); + #endif + return; + } +/* + + WORD flashver = eeprom_read_word((const uint16_t *)E2END - 1); + if (flashver > 999) { + flashver = 0; + } + BYTE y, tmp; + WORD x; + BYTE found = 0; + + for (x = flashver+10; x > flashver; x--) { + y = x / 100; + filename[5] = y + 0x30; + tmp = x % 100; + + y = tmp / 10; + filename[6] = y + 0x30; + tmp = x % 10; + + filename[7] = tmp + 0x30; + + if (pf_open(filename) == FR_OK) { // File opens normally + found = 1; + doProgram(); + } + led_power_toggle(); + } + + if (found == 0) {*/ + + fresult = pf_open(filename); + + if (fresult != FR_OK) { /* File could not be opened */ + + #if USE_UART + UART_puts(PSTR("File not open")); + UART_newline(); + #endif + #if USE_LED + //uint8_t i; + //led_power_on(); + //for(i=0;i<2*fresult;i++) { led_write_toggle();_delay_ms(500);}//Give error number while Power led is on + //led_power_off(); + #endif + return; + } + + doFlash(); + + #if USE_LED + led_write_off(); + //led_power_off(); + _delay_ms(2000); + uint8_t i; + //for(i=0;i<40;i++) { led_power_toggle();_delay_ms(50);}//SUCCESS FLASH WILDLY for 2 secs + for(i=0;i<40;i++) { led_write_toggle();_delay_ms(50);}//SUCCESS FLASH WILDLY for 2 secs + #endif + +} + + + + + + +int main (void) +{ + #if USE_LED + init_leds(); + uint8_t i=0; + #endif + + #if USE_UART + UART_init(); + UART_puts(PSTR("AVR_BOOT")); + UART_newline(); + #endif + while (1) { + #if USE_LED + //led_power_on();_delay_ms(200);led_power_off(); //Test Power Led + led_write_on();_delay_ms(200);led_write_off(); //Test Write Led + #endif + + checkFile(); + + if (pgm_read_word(0) != 0xFFFF) ((void(*)(void))0)(); //EXIT BOOTLOADER + + #if USE_UART + UART_puts(PSTR("retry")); + UART_newline(); + #endif + #if USE_LED + //for(i=0;i<10;i++) { led_power_toggle();_delay_ms(200);} //SOMETHING WENT WRONG: Flash Power LED + for(i=0;i<10;i++) { led_write_toggle();_delay_ms(200);} //SOMETHING WENT WRONG: Flash Write LED + #endif + _delay_ms(5000); // Retry + } +} diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/00index_p.html b/bootloader_zevero_sd_m644p_make/pff/doc/00index_p.html new file mode 100644 index 0000000..46847cd --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/00index_p.html @@ -0,0 +1,77 @@ + + + + + + + + + + + +Petit FAT File System Module + + + +

Petit FAT File System Module

+
+ +
+layer +

Petit FatFs is a sub-set of FatFs module for tiny 8-bit microcontrollers. It is written in compliance with ANSI C and completely separated from the disk I/O layer. It can be incorporated into the tiny microcontrollers with limited memory even if the RAM size is less than sector size. Also full featured FAT file system module is available here↗.

+ +

Features

+
    +
  • Very small RAM consumption (44 bytes work area + certain stack).
  • +
  • Very small code size (2K-4K bytes).
  • +
  • FAT12, FAT16 and FAT32.
  • +
  • Single volume and Single file.
  • +
  • File write function with some restrictions.
  • +
+
+ + +
+

Application Interface

+

Petit FatFs module provides following functions.

+ +
+ + +
+

Disk I/O Interface

+

Since the Petit FatFs module is completely separated from disk I/O layer, it requires following functions to lower layer to read data from storage device. The low level disk I/O module is not a part of Petit FatFs module and it must be provided by user. The sample drivers are also available in the resources.

+ +
+ + +
+

Resources

+

The Petit FatFs module is a free software and is opened for education, research and development. You can use, modify and/or redistribute it for personal, non-profit or commercial use without any restriction under your responsibility. For further information, refer to the application note.

+ +
+ + +
+

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/css_e.css b/bootloader_zevero_sd_m644p_make/pff/doc/css_e.css new file mode 100644 index 0000000..2fa0cc1 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/css_e.css @@ -0,0 +1,65 @@ +* {margin: 0; padding: 0; border-width: 0;} +body {margin: 8px; background-color: #e0ffff; font-color: black; font-family: serif; line-height: 133%; max-width: 1024px;} +a:link {color: blue;} +a:visited {color: darkmagenta;} +a:hover {background-color: #a0ffff;} +a:active {color: darkmagenta; overflow: hidden; outline:none; position: relative; top: 1px; left: 1px;} +abbr {border-width: 1px;} + +p {margin: 0 0 0.3em 1em;} +i {margin: 0 0.3em 0 0;} +b {margin: 0 0.1em;} +em {font-style: normal; font-weight: bold; margin: 0 0.1em;} +strong {} +pre {border: 1px dashed gray; margin: 0.5em 1em; padding: 0.5em; line-height: 1.2em; font-size: 85%; font-family: "Consolas", "Courier New", monospace; background-color: white;} +pre span.c {color: green;} +pre span.k {color: blue;} +pre span.arg {font-style: italic;} +tt {margin: 0 0.2em; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; } +tt.arg {font-style: italic;} +ol {margin: 0.5em 2.5em;} +ul {margin: 0.5em 2em;} +dl {margin: 0.5em 1em;} +dd {margin: 0 2em;} +dt {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;} +dl.par dt {margin: 0.5em 0 0 0 ; font-style: italic; } +dl.ret dt {margin: 0.5em 0 0 0 ; font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;} +hr {border-width: 1px; margin: 1em;} +div.abst {font-family: sans-serif;} +div.para {clear: both; font-family: serif;} +div.ret a {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace; } +.equ {text-indent: 0; margin: 1em 2em 1em;} +.indent {margin-left: 2em;} +.rset {float: right; margin: 0.3em 0 0.5em 0.5em;} +.lset {float: left; margin: 0.3em 0.5em 0.5em 0.5em;} +ul.flat li {list-style-type: none; margin: 0;} +a.imglnk img {border: 1px solid;} +.iequ {white-space: nowrap; font-weight: bold;} +.clr {clear: both;} +.it {font-style: italic;} +.mfd {font-size: 0.7em; padding: 0 1px; border: 1px solid; white-space : nowrap} +.ral {text-align: right; } +.lal {text-align: left; } +.cal {text-align: center; } + +h1 {line-height: 1em; font-size: 2em; font-family: sans-serif; padding: 0.3em 0 0.3em;} +p.hdd {float: right; text-align: right; margin-top: 0.5em;} +hr.hds {clear: both; margin-bottom: 1em;} + +h2 {font-size: 2em; font-family: sans-serif; background-color: #d8d8FF; padding: 0.5em 0.5em; margin: 0 0 0.5em;} +h3 {font-size: 1.5em; font-family: sans-serif; margin: 1.5em 0 0.5em;} +h4 {font-size: 1.2em; font-family: sans-serif; margin: 1em 0 0.2em;} +h5 {font-size: 1em; font-family: sans-serif; margin: 0.5em 0 0em;} +small {font-size: 80%;} +.indent {margin-left: 2em;} + +/* Tables */ +table {margin: 0.5em 1em; border-collapse: collapse; border: 2px solid black; } +th {background-color: white; border-style: solid; border-width: 1px 1px 2px; border-color: black; padding: 0 3px; vertical-align: top; white-space: nowrap;} +td {background-color: white; border: 1px solid black; padding: 0 3px; vertical-align: top; line-height: 1.3em;} +table.lst td:first-child {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;} +table.lst2 td {font-size: 0.85em; font-family: "Consolas", "Courier New", monospace;} +table caption {font-family: sans-serif; font-weight: bold;} +tr.lst3 td { border-width: 2px 1px 1px; } + +p.foot {clear: both; text-indent: 0; margin: 1em 0.5em 1em;} diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/css_p.css b/bootloader_zevero_sd_m644p_make/pff/doc/css_p.css new file mode 100644 index 0000000..f8589aa --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/css_p.css @@ -0,0 +1 @@ +body {margin: 8px; background-color: #ffecf0; font-color: black; font-family: serif; line-height: 133%; max-width: 1024px;} diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/img/layers3.png b/bootloader_zevero_sd_m644p_make/pff/doc/img/layers3.png new file mode 100644 index 0000000..ac439b0 Binary files /dev/null and b/bootloader_zevero_sd_m644p_make/pff/doc/img/layers3.png differ diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/img/rwtest3.png b/bootloader_zevero_sd_m644p_make/pff/doc/img/rwtest3.png new file mode 100644 index 0000000..cbaa3d1 Binary files /dev/null and b/bootloader_zevero_sd_m644p_make/pff/doc/img/rwtest3.png differ diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/appnote.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/appnote.html new file mode 100644 index 0000000..3b6d6dc --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/appnote.html @@ -0,0 +1,91 @@ + + + + + + + + +FatFs Module Application Note + + + +

Petit FatFs Module Application Note

+
+ +

Basic Considerations

+

The FatFs module is assuming following conditions on portability.

+
    +
  • ANSI C
    +The FatFs module is a middleware written in ANSI C (C89). There is no platform dependence, so long as the compiler is in compliance with ANSI C.
  • +
  • Size of integer types
    +The FatFs module assumes that size of char/short/long are 8/16/32 bit and int is 16 or 32 bit. These correspondence are defined in integer.h. This will not be a problem on most compilers. When any conflict with existing definitions is occured, you must resolve it with care.
  • +
+ + +
+

Memory Usage (R0.03)

+ + + + + + + + + + + +
AVRx86
Compilergcc(WinAVR)VC6
_WORD_ACCESS11
Code (default)21001720
Code (!_USE_READ)-444-246
Code (_USE_DIR)+1002+420
Code (_USE_LSEEK)+490+228
Code (_USE_WRITE)+518+351
RAM (bss)24
RAM (work)4244
+

This is the size of the Petit FatFs module itself. In addition to this, a low level disk I/O module will be required for a complete function. The size of MMC/SDC module on AVR becomes approximate 620 bytes without write function and 840 bytes with write function.

+
+ +
+

Module Size Reduction

+

Follwing table shows which function is removed by configuration options for the module size reduction.

+ + + + + + + + + + +
Function_USE_READ_USE_DIR_USE_LSEEK_USE_WRITE
0000
pf_mount
pf_open
pf_readx
pf_lseekx
pf_opendirx
pf_readdirx
pf_writex
+
+ +
+

Performance effective file access

+

For good performance on reading a file on the small embedded system, application programmer should consider what process is done in the file system module.

+

The Petit FatFs reads the disk sectors without a sector buffer. This means the file system reads a part of the sector contains the required data every reference point even if they are in the same sector. However the generic storage device are not byte addressable so that the disk I/O layer will read the entire sector and pick up the data bytes from the read data steram.

+

When read 512 byte data from a file at a time, the data sector will be read only a time. When read that data in byte-by-byte, the data sector will be read 512 times. Therefore the byte-by-byte read request will drastically decrease the read performance. To avoid this stupid read controls, the file data should be read in long block as possible. Sector alignment access is not impotant on the Petit FatFs.

+

The tiny microcontrollers targeted by Petit FatFs has a limited size of RAM. It may not able to allocate a certain size of read buffer and most type of text processing will require byte-by-byte read operation. The Petit FatFs supports data forwarding feature for such purpose.

+ +
+ +
+

About FatFs License

+

Petit FatFs has being developped as a personal project of author, ChaN. It is free from the code anyone else wrote. Following code block shows a copy of the license document that included in the source files.

+
/*----------------------------------------------------------------------------/
+/  Petit FatFs - FAT file system module  R0.03                  (C)ChaN, 2014
+/-----------------------------------------------------------------------------/
+/ Petit FatFs module is a generic FAT file system module for small embedded
+/ systems. This is a free software that opened for education, research and
+/ commercial developments under license policy of following trems.
+/
+/  Copyright (C) 2014, ChaN, all right reserved.
+/
+/ * The Petit 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 products UNDER YOUR RESPONSIBILITY.
+/ * Redistributions of source code must retain the above copyright notice.
+/
+/-----------------------------------------------------------------------------/
+

Therefore FatFs license is one of the BSD-style licenses but there is a significant feature. Because FatFs is for embedded projects, the conditions of redistributions in binary form, such as embedded code, hex file, binary library and any form without source code, are not specified in order to extend usability to commercial use. The documentation of the distributions need not include about Petit FatFs and its license document, and it may also. This is equivalent to the BSD 1-Clause License. Of course Petit FatFs is compatible with the projects under GNU GPL. When redistribute the Petit FatFs with any modification, the license can also be changed to GNU GPL or BSD-style license.

+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/dinit.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/dinit.html new file mode 100644 index 0000000..6c38a23 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/dinit.html @@ -0,0 +1,40 @@ + + + + + + + + +Petit FatFs - disk_initialize + + + + +
+

disk_initialize

+

The disk_initialize function initializes the disk drive.

+
+DSTATUS disk_initialize (void)
+
+
+ +
+

Return Values

+

The disk status is returned in combination of following flags.

+
+
STA_NOINIT
+
Indicates that the disk drive has not been initialized. This flag is set on: system reset, disk removal and disk_initialize function failed, and cleared on: disk_initialize function succeeded.
+
STA_NODISK
+
Indicates that no medium in the drive. This is always cleared on fixed disk drive. This flag is not referred by Petit FatFs.
+
+
+ +
+

Description

+

The disk_initialize function initializes the storage device. If the function succeeded, STA_NOINIT flag in the return value is cleared.

+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/dreadp.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/dreadp.html new file mode 100644 index 0000000..41309a4 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/dreadp.html @@ -0,0 +1,59 @@ + + + + + + + + +Petit FatFs - disk_readp + + + + +
+

disk_readp

+

The disk_readp function reads a partial sector of the device.

+
+DRESULT disk_readp (
+  BYTE* buff,    /* [OUT] Pointer to the read buffer */
+  DWORD sector,  /* [IN]  Sector number */
+  UINT offset,   /* [IN]  Byte offset in the sector to start to read */
+  UINT count     /* [IN]  Number of bytes to read */
+);
+
+
+ +
+

Parameters

+
+
buff
+
Pointer to the read buffer to store the read data. If a NULL is given, read bytes will be forwarded to the outgoing stream instead of the memory.
+
sector
+
Specifies the sector number to be read in logical block address (LBA).
+
offset
+
Specifies the byte offset in the sector to start to read. The value can be 0 to 511.
+
count
+
Specifies number of bytes to read. The value can be 0 to 512 and offset + count must not exceed 512.
+
+
+ + +
+

Return Value

+
+
RES_OK (0)
+
The function succeeded.
+
RES_ERROR
+
Any hard error occured during the disk read operation and could not recover it.
+
RES_PARERR
+
Invalid parameter.
+
RES_NOTRDY
+
The device has not been initialized.
+
+
+ + +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/dwritep.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/dwritep.html new file mode 100644 index 0000000..5361a5f --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/dwritep.html @@ -0,0 +1,73 @@ + + + + + + + + +Petit FatFs - disk_writep + + + + +
+

disk_writep

+

The disk_writep function writes data to the sector.

+
+DRESULT disk_writep (
+  BYTE* buff,  /* [IN] Pointer to the data to be written */
+  DWORD sc,    /* [IN] Sector number or Number of bytes to wtite */
+);
+
+
+ +
+

Parameters

+
+
buff
+
Pointer to the data to be written to the sector. If a NULL is given, the function initiate/finalize a write transaction to the sector.
+
sc
+
Specifies nubmer of bytes to write if buff is not a NULL. If buff is a NULL and sc is not a zero, the function initiates a write transactin to the sector. If buff and sc are zero, the function finalize the current sector write transactin.
+
+
+ + +
+

Return Value

+
+
RES_OK (0)
+
The function succeeded.
+
RES_ERROR
+
Any hard error occured during the write operation and could not recover it or the medium is write protected.
+
RES_PARERR
+
Invalid parameter.
+
RES_NOTRDY
+
The device has not been initialized.
+
+
+ + +
+

Description

+

A sector write operation is done in following sequence.

+
    +
  1. disk_writep(0, sector_number); Initiate a sector write transaction.
  2. +
  3. disk_writep(data, byte_to_write); Start to write data to the sector.
  4. +
  5. disk_writep(data, byte_to_write); And data can be written upto 512 bytes with one or more calls.
  6. +
  7. disk_writep(data, byte_to_write); ...
  8. +
  9. disk_writep(0, 0); Finalize the write transaction. If number of bytes sent is less than 512, left bytes in the sector is filled by zero.
  10. +
+

If a write transaction is in progress, disk_readp() function will fail and disk_initialize() function finalize the current write transaction.

+
+ + +
+

Remarks

+

This funciton is needed when _USE_WRITE == 1.

+
+ + +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/filename.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/filename.html new file mode 100644 index 0000000..b751cda --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/filename.html @@ -0,0 +1,22 @@ + + + + + + + + +Petit FatFs - Path Names + + + + +
+

Format of the path names

+

The path name format on the Petit FatFs module is similer to MS-DOS as follows.

+
"[/]directory/file"
+

The Petit FatFs module supports only 8.3 format file name. The sub-directories are separated with a /. The path name is terminated with a nul character, control character or white space. Heading spaces are ignored and skipped. When _USE_LCC == 1, lower case characters are allowed for the path name.

+

The Petit FatFs module does not have a concept of current directory like OS oriented file system. All objects on the volume are always specified in full path name following from the root directory. Heading separator is ignored and it can be exist or omitted.

+
+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/lseek.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/lseek.html new file mode 100644 index 0000000..2b65826 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/lseek.html @@ -0,0 +1,79 @@ + + + + + + + + +Petit FatFs - pf_lseek + + + + +
+

pf_lseek

+

The pf_lseek function moves the file read/write pointer of the open file.

+ +
+FRESULT pf_lseek (
+  DWORD ofs       /* [IN] File offset in unit of byte */
+);
+
+
+ +
+

Parameters

+
+
ofs
+
Number of bytes where from start of the file
+
+
+ + +
+

Return Values

+
+
FR_OK (0)
+
The function succeeded.
+
FR_DISK_ERR
+
The function failed due to an error in the disk function, a wrong FAT structure or an internal error.
+
FR_NOT_OPENED
+
The file has not been opened.
+
+
+ + +
+

Description

+

The pf_lseek() function moves the file read/write pointer of the open file. The offset can be specified in only origin from top of the file.

+
+ + +
+

Example

+
+    /* Move to offset of 5000 from top of the file */
+    res = pf_lseek(5000);
+
+    /* Forward 3000 bytes */
+    res = pf_lseek(fs.fptr + 3000);
+
+    /* Rewind 2000 bytes (take care on wraparound) */
+    res = pf_lseek(fs.fptr - 2000);
+
+
+ +
+

QuickInfo

+

Available when _USE_LSEEK == 1.

+
+ +
+

References

+

pf_open, FATFS

+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/mount.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/mount.html new file mode 100644 index 0000000..f787cca --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/mount.html @@ -0,0 +1,65 @@ + + + + + + + + +Petit FatFs - pf_mount + + + + +
+

pf_mount

+

The pf_mount fucntion mounts a volume.

+
+FRESULT pf_mount (
+  FATFS*  fs  /* [IN] Pointer to the work area */
+);
+
+
+ +
+

Parameters

+
+
fs
+
Pointer to the work area (file system object) to be registered.
+
+
+ +
+

Return Values

+
+
FR_OK (0)
+
The function succeeded.
+
FR_NOT_READY
+
The drive could not be initialized due to a disk error or no medium.
+
FR_DISK_ERR
+
An error occured in the disk function.
+
FR_NO_FILESYSTEM
+
There is no valid FAT partition on the disk.
+
+
+ + +
+

Description

+

The pf_mount() function registers a work area to the Petit FatFs module. The volume is mounted on registration. The volume must be mounted with this function prior to any other file function and after every media changes.

+
+ + +
+

QuickInfo

+

Always available.

+
+ +
+

References

+

FATFS

+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/open.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/open.html new file mode 100644 index 0000000..8deb830 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/open.html @@ -0,0 +1,103 @@ + + + + + + + + +Petit FatFs - pf_open + + + + +
+

pf_open

+

The pf_open function opens an existing file.

+
+FRESULT pf_open (
+  const char* path  /* [IN] Pointer to the file neme */
+);
+
+
+ +
+

Parameters

+
+
path
+
Pointer to a null-terminated string that specifies the file name to open.
+
+
+ + +
+

Return Values

+
+
FR_OK (0)
+
The function succeeded.
+
FR_NO_FILE
+
Could not find the file or path.
+
FR_DISK_ERR
+
The function failed due to a hard error in the disk function, a wrong FAT structure or an internal error.
+
FR_NOT_ENABLED
+
The volume has not been mounted.
+
+
+ + +
+

Description

+

The file must be opend prior to use pf_read() and pf_lseek() function. The open file is valid until next open.

+
+ + +
+

Example

+
+int main (void)
+{
+    FATFS fs;          /* Work area (file system object) for the volume */
+    BYTE buff[16];     /* File read buffer */
+    UINT br;           /* File read count */
+    FRESULT res;       /* Petit FatFs function common result code */
+
+
+    /* Mount the volume */
+    pf_mount(&fs);
+    if (res) die(res);
+
+    /* Open a file */
+    res = pf_open("srcfile.dat");
+    if (res) die(res);
+
+    /* Read data to the memory */
+    res = pf_read(buff, 16, &br);    /* Read data to the buff[] */
+    if (res) die(res);               /* Check error */
+    if (br != 16) die(255);          /* Check EOF */
+
+    ....
+
+    /* Forward data to the outgoing stream */
+    do
+        res = pf_read(0, 512, &br);  /* Send data to the stream */
+    while (res || br != 512);        /* Break on error or eof */
+
+    ....
+
+}
+
+
+ +
+

QuickInfo

+

Always available.

+
+ +
+

References

+

pf_read, FATFS

+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/opendir.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/opendir.html new file mode 100644 index 0000000..adf7a96 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/opendir.html @@ -0,0 +1,72 @@ + + + + + + + + +Petit FatFs - pf_opendir + + + + +
+

pf_opendir

+

The pf_opendir function opens a directory.

+
+FRESULT pf_opendir (
+  DIR* dp,          /* [OUT] Pointer to the blank directory object structure */
+  const char* path  /* [IN]  Pointer to the directory name */
+);
+
+
+ +
+

Parameters

+
+
dp
+
Pointer to the blank directory object to be created.
+
path
+
Pinter to the null-terminated string that specifies the directory name to be opened.
+
+
+ + +
+

Return Values

+
+
FR_OK (0)
+
The function succeeded and the directory object is created. It is used for subsequent calls to read the directory entries.
+
FR_NO_FILE
+
Could not find the path.
+
FR_NOT_READY
+
The disk drive cannot work due to no medium in the drive or any other reason.
+
FR_DISK_ERR
+
The function failed due to a hard error in the disk function, a wrong FAT structure or an internal error.
+
FR_NOT_ENABLED
+
The volume has no work area.
+
+
+ + +
+

Description

+

The pf_opendir() function opens an exsisting directory and creates the directory object for subsequent calls. The directory object structure can be discarded at any time without any procedure.

+
+ + +
+

QuickInfo

+

Available when _USE_DIR == 1.

+
+ + +
+

References

+

f_readdir, DIR

+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/read.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/read.html new file mode 100644 index 0000000..7118841 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/read.html @@ -0,0 +1,73 @@ + + + + + + + + +Petit FatFs - pf_read + + + + +
+

pf_read

+

The pf_read function reads data from the file.

+
+FRESULT pf_read (
+  void* buff,  /* [OUT] Pointer to the read buffer */
+  UINT btr,    /* [IN]  Number of bytes to read */
+  UINT* br     /* [OUT] Number of bytes read */
+);
+
+
+ +
+

Parameters

+
+
buff
+
Pointer to the buffer to store the read data. A NULL specifies the destination is an outgoing stream.
+
btr
+
Number of bytes to read.
+
br
+
Pointer to the variable to return number of bytes read.
+
+
+ + +
+

Return Values

+
+
FR_OK (0)
+
The function succeeded.
+
FR_DISK_ERR
+
The function failed due to a hard error in the disk function, a wrong FAT structure or an internal error.
+
FR_NOT_OPENED
+
The file has not been opened.
+
FR_NOT_ENABLED
+
The volume has not been mounted.
+
+
+ + +
+

Description

+

The file read/write pointer in the file system object advances in number of bytes read. After the function succeeded, *br should be checked to detect end of file. In case of *br < btr, it means the read pointer reached end of file during read operation.

+

If a NULL is given to the buff, the read bytes will be forwarded to the outgoing stream instead of the memory. The streaming function will be typically built-in the disk_readp() function.

+
+ +
+

QuickInfo

+

Available when _USE_READ == 1.

+
+ + +
+

References

+

pf_open, pf_write, FATFS

+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/readdir.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/readdir.html new file mode 100644 index 0000000..4ac4997 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/readdir.html @@ -0,0 +1,100 @@ + + + + + + + + +Petit FatFs - pf_readdir + + + + +
+

pf_readdir

+

The pf_readdir function reads directory entries.

+
+FRESULT pf_readdir (
+  DIR* dp,      /* [IN]  Pointer to the open directory object */
+  FILINFO* fno  /* [OUT] Pointer to the file information structure */
+);
+
+
+ +
+

Parameters

+
+
dp
+
Pointer to the open directory object.
+
fno
+
Pointer to the file information structure to store the read item.
+
+
+ + +
+

Return Values

+
+
FR_OK (0)
+
The function succeeded.
+
FR_DISK_ERR
+
The function failed due to an error in the disk function, a wrong FAT structure or an internal error.
+
FR_NOT_OPENED
+
The directory object has not been opened.
+
+
+ + +
+

Description

+

The pf_readdir() function reads directory entries in sequence. All items in the directory can be read by calling this function repeatedly. When all directory entries have been read and no item to read, the function returns a null string into member f_name[] in the file information structure without error. When a null pointer is given to the fno, the read index of the directory object will be rewinded.

+
+ + +
+

Sample Code

+
+FRESULT scan_files (char* path)
+{
+    FRESULT res;
+    FILINFO fno;
+    DIR dir;
+    int i;
+
+
+    res = pf_opendir(&dir, path);
+    if (res == FR_OK) {
+        i = strlen(path);
+        for (;;) {
+            res = pf_readdir(&dir, &fno);
+            if (res != FR_OK || fno.fname[0] == 0) break;
+            if (fno.fattrib & AM_DIR) {
+                sprintf(&path[i], "/%s", fno.fname);
+                res = scan_files(path);
+                if (res != FR_OK) break;
+                path[i] = 0;
+            } else {
+                printf("%s/%s\n", path, fno.fname);
+            }
+        }
+    }
+
+    return res;
+}
+
+
+ +
+

QuickInfo

+

Available when _USE_DIR == 1.

+
+ +
+

References

+

pf_opendir, FILINFO, DIR

+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/sdir.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/sdir.html new file mode 100644 index 0000000..ee1bfbb --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/sdir.html @@ -0,0 +1,30 @@ + + + + + + + + +Petit FatFs - DIR + + + + +
+

DIR

+

The DIR structure is used for the work area to read a directory by pf_oepndir, pf_readdir function.

+
+typedef struct {
+    WORD    index;     /* Current read/write index number */
+    BYTE*   fn;        /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */
+    CLUST   sclust;    /* Table start cluster (0:Static table) */
+    CLUST   clust;     /* Current cluster */
+    DWORD   sect;      /* Current sector */
+} DIR;
+
+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/sfatfs.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/sfatfs.html new file mode 100644 index 0000000..f0776dc --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/sfatfs.html @@ -0,0 +1,39 @@ + + + + + + + + +Petit FatFs - FATFS + + + + +
+

FATFS

+

The FATFS structure holds dynamic work area of the logical drive and a file. It is given by application program and registerd/unregisterd to the Petit FatFs module with pf_mount function. There is no member that can be changed by application programs.

+
+typedef struct {
+    BYTE    fs_type;     /* FAT sub type */
+    BYTE    csize;       /* Number of sectors per cluster */
+    BYTE    flag;        /* File status flags */
+    BYTE    pad1;
+    WORD    n_rootdir;   /* Number of root directory entries (0 on FAT32) */
+    CLUST   n_fatent;    /* Number of FAT entries (= number of clusters + 2) */
+    DWORD   fatbase;     /* FAT start sector */
+    DWORD   dirbase;     /* Root directory start sector (Cluster# on FAT32) */
+    DWORD   database;    /* Data start sector */
+    DWORD   fptr;        /* File read/write pointer */
+    DWORD   fsize;       /* File size */
+    CLUST   org_clust;   /* File start cluster */
+    CLUST   curr_clust;  /* File current cluster */
+    DWORD   dsect;       /* File current data sector */
+} FATFS;
+
+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/sfileinfo.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/sfileinfo.html new file mode 100644 index 0000000..a16ce16 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/sfileinfo.html @@ -0,0 +1,62 @@ + + + + + + + + +Petit FatFs - FILINFO + + + + +
+

FILINFO

+

The FILINFO structure holds a file information returned by pf_readdir function.

+
+typedef struct {
+    DWORD   fsize;        /* File size */
+    WORD    fdate;        /* Last modified date */
+    WORD    ftime;        /* Last modified time */
+    BYTE    fattrib;      /* Attribute */
+    char    fname[13];    /* File name */
+} FILINFO;
+
+
+ +

Members

+
+
fsize
+
Indicates size of the file in unit of byte. This is always zero when it is a directory.
+
fdate
+
Indicates the date that the file was modified or the directory was created.
+
+
bit15:9
+
Year origin from 1980 (0..127)
+
bit8:5
+
Month (1..12)
+
bit4:0
+
Day (1..31)
+
+
+
ftime
+
Indicates the time that the file was modified or the directory was created.
+
+
bit15:11
+
Hour (0..23)
+
bit10:5
+
Minute (0..59)
+
bit4:0
+
Second / 2 (0..29)
+
+
+
fattrib
+
Indicates the file/directory attribute in combination of AM_DIR, AM_RDO, AM_HID, AM_SYS and AM_ARC.
+
fname[]
+
Indicates the file/directory name in 8.3 format null-terminated string.
+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/doc/pf/write.html b/bootloader_zevero_sd_m644p_make/pff/doc/pf/write.html new file mode 100644 index 0000000..81f3f56 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/doc/pf/write.html @@ -0,0 +1,86 @@ + + + + + + + + +Petit FatFs - pf_write + + + + +
+

pf_write

+

The pf_write function writes data to the file.

+
+FRESULT pf_write (
+  const void* buff, /* [IN]  Pointer to the data to be written */
+  UINT btw,         /* [IN]  Number of bytes to write */
+  UINT* bw          /* [OUT] Pointer to the variable to return number of bytes written */
+);
+
+
+ +
+

Parameters

+
+
buff
+
Pointer to the data to be wtitten. A NULL specifies to finalize the current write operation.
+
btw
+
Number of bytes to write.
+
bw
+
Pointer to the variable to return number of bytes read.
+
+
+ + +
+

Return Values

+
+
FR_OK (0)
+
The function succeeded.
+
FR_DISK_ERR
+
The function failed due to a hard error in the disk function, write protected, a wrong FAT structure or an internal error.
+
FR_NOT_OPENED
+
The file has not been opened.
+
FR_NOT_ENABLED
+
The volume has not been mounted.
+
+
+ + +
+

Description

+

The write function has some restrictions listed below:

+
    +
  • Cannot create file. Only existing file can be written.
  • +
  • Cannot expand file size.
  • +
  • Cannot update time stamp of the file.
  • +
  • Write operation can start/stop on the sector boundary.
  • +
  • Read-only attribute of the file cannot block write operation.
  • +
+

File write operation must be done in following sequence.

+
    +
  1. pf_lseek(ofs); read/write pointer must be moved to sector bundary prior to initiate write operation or it will be rounded-down to the sector boundary.
  2. +
  3. pf_write(buff, btw, &bw); Initiate write operation. Write first data to the file.
  4. +
  5. pf_write(buff, btw, &bw); Write next data. Any other file function cannot be used while a write operation is in progress.
  6. +
  7. pf_write(0, 0, &bw); Finalize the write operation. If read/write pointer is not on the sector boundary, left bytes in the sector will be filled with zero.
  8. +
+

The read/write pointer in the file system object advances in number of bytes written. After the function succeeded, *bw should be checked to detect end of file. In case of *bw < btw, it means the read/write pointer reached end of file during the write operation. Once a write operation is initiated, it must be finalized or the written data can be lost.

+
+ +
+

QuickInfo

+

Available when _USE_WRITE == 1.

+
+ +
+

References

+

pf_open, FATFS

+
+ +

Return

+ + diff --git a/bootloader_zevero_sd_m644p_make/pff/src/00readme.txt b/bootloader_zevero_sd_m644p_make/pff/src/00readme.txt new file mode 100644 index 0000000..974a17b --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/src/00readme.txt @@ -0,0 +1,46 @@ +Petit FatFs Module Source Files R0.03 (C)ChaN, 2014 + + +FILES + + pff.h Common include file for Petit FatFs and application module. + pff.c Petit FatFs module. + diskio.h Common include file for Petit FatFs and disk I/O module. + diskio.c Skeleton of low level disk I/O module. + integer.h Alternative type definitions for integer variables. + + Low level disk I/O module is not included in this archive because the Petit + FatFs module is only a generic file system layer and not depend on any + specific storage device. You have to provide a low level disk I/O module that + written to control your storage device. + + + +AGREEMENTS + + Petit 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) 2014, ChaN, all right reserved. + + * The Petit 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. + + + +REVISION HISTORY + + Jun 15, 2009 R0.01a First release (Branched from FatFs R0.07b) + + Dec 14, 2009 R0.02 Added multiple code page support. + Added write funciton. + Changed stream read mode interface. + Dec 07,'2010 R0.02a Added some configuration options. + Fixed fails to open objects with DBCS character. + + Jun 10, 2014 R0.03 Separated out configuration options to pffconf.h. + Added _USE_LCC option. + Added _FS_FAT16 option. diff --git a/bootloader_zevero_sd_m644p_make/pff/src/diskio.c b/bootloader_zevero_sd_m644p_make/pff/src/diskio.c new file mode 100644 index 0000000..5b303b8 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/src/diskio.c @@ -0,0 +1,73 @@ +/*-----------------------------------------------------------------------*/ +/* Low level disk I/O module skeleton for Petit FatFs (C)ChaN, 2014 */ +/*-----------------------------------------------------------------------*/ + +#include "diskio.h" + + +/*-----------------------------------------------------------------------*/ +/* Initialize Disk Drive */ +/*-----------------------------------------------------------------------*/ + +DSTATUS disk_initialize (void) +{ + DSTATUS stat; + + // Put your code here + + return stat; +} + + + +/*-----------------------------------------------------------------------*/ +/* Read Partial Sector */ +/*-----------------------------------------------------------------------*/ + +DRESULT disk_readp ( + BYTE* buff, /* Pointer to the destination object */ + DWORD sector, /* Sector number (LBA) */ + UINT offset, /* Offset in the sector */ + UINT count /* Byte count (bit15:destination) */ +) +{ + DRESULT res; + + // Put your code here + + return res; +} + + + +/*-----------------------------------------------------------------------*/ +/* Write Partial Sector */ +/*-----------------------------------------------------------------------*/ + +DRESULT disk_writep ( + BYTE* buff, /* Pointer to the data to be written, NULL:Initiate/Finalize write operation */ + DWORD sc /* Sector number (LBA) or Number of bytes to send */ +) +{ + DRESULT res; + + + if (!buff) { + if (sc) { + + // Initiate write process + + } else { + + // Finalize write process + + } + } else { + + // Send data to the disk + + } + + return res; +} + diff --git a/bootloader_zevero_sd_m644p_make/pff/src/diskio.h b/bootloader_zevero_sd_m644p_make/pff/src/diskio.h new file mode 100644 index 0000000..bb75d3e --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/src/diskio.h @@ -0,0 +1,42 @@ +/*----------------------------------------------------------------------- +/ PFF - Low level disk interface modlue include file (C)ChaN, 2014 +/-----------------------------------------------------------------------*/ + +#ifndef _DISKIO_DEFINED +#define _DISKIO_DEFINED + +#ifdef __cplusplus +extern "C" { +#endif + +#include "integer.h" + + +/* Status of Disk Functions */ +typedef BYTE DSTATUS; + + +/* Results of Disk Functions */ +typedef enum { + RES_OK = 0, /* 0: Function succeeded */ + RES_ERROR, /* 1: Disk error */ + RES_NOTRDY, /* 2: Not ready */ + RES_PARERR /* 3: Invalid parameter */ +} DRESULT; + + +/*---------------------------------------*/ +/* Prototypes for disk control functions */ + +DSTATUS disk_initialize (void); +DRESULT disk_readp (BYTE* buff, DWORD sector, UINT offser, UINT count); +DRESULT disk_writep (const BYTE* buff, DWORD sc); + +#define STA_NOINIT 0x01 /* Drive not initialized */ +#define STA_NODISK 0x02 /* No medium in the drive */ + +#ifdef __cplusplus +} +#endif + +#endif /* _DISKIO_DEFINED */ diff --git a/bootloader_zevero_sd_m644p_make/pff/src/integer.h b/bootloader_zevero_sd_m644p_make/pff/src/integer.h new file mode 100644 index 0000000..074a46b --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/src/integer.h @@ -0,0 +1,33 @@ +/*-------------------------------------------*/ +/* Integer type definitions for FatFs module */ +/*-------------------------------------------*/ + +#ifndef _FF_INTEGER +#define _FF_INTEGER + +#ifdef _WIN32 /* FatFs development platform */ + +#include +#include + +#else /* Embedded platform */ + +/* This type MUST be 8 bit */ +typedef unsigned char BYTE; + +/* These types MUST be 16 bit */ +typedef short SHORT; +typedef unsigned short WORD; +typedef unsigned short WCHAR; + +/* These types MUST be 16 bit or 32 bit */ +typedef int INT; +typedef unsigned int UINT; + +/* These types MUST be 32 bit */ +typedef long LONG; +typedef unsigned long DWORD; + +#endif + +#endif diff --git a/bootloader_zevero_sd_m644p_make/pff/src/pff.c b/bootloader_zevero_sd_m644p_make/pff/src/pff.c new file mode 100644 index 0000000..f8f6329 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/src/pff.c @@ -0,0 +1,1108 @@ +/*----------------------------------------------------------------------------/ +/ Petit FatFs - FAT file system module R0.03 (C)ChaN, 2014 +/-----------------------------------------------------------------------------/ +/ Petit FatFs module is a generic FAT file system module for small embedded +/ systems. This is a free software that opened for education, research and +/ commercial developments under license policy of following trems. +/ +/ Copyright (C) 2014, ChaN, all right reserved. +/ +/ * The Petit 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 products UNDER YOUR RESPONSIBILITY. +/ * Redistributions of source code must retain the above copyright notice. +/ +/-----------------------------------------------------------------------------/ +/ Jun 15,'09 R0.01a First release. +/ +/ Dec 14,'09 R0.02 Added multiple code page support. +/ Added write funciton. +/ Changed stream read mode interface. +/ Dec 07,'10 R0.02a Added some configuration options. +/ Fixed fails to open objects with DBCS character. + +/ Jun 10,'14 R0.03 Separated out configuration options to pffconf.h. +/ Added _USE_LCC option. +/ Added _FS_FAT16 option. +/----------------------------------------------------------------------------*/ + +#include "pff.h" /* Petit FatFs configurations and declarations */ +#include "diskio.h" /* Declarations of low level disk I/O functions */ + + + +/*-------------------------------------------------------------------------- + + Module Private Definitions + +---------------------------------------------------------------------------*/ + + +#if _PFATFS != 4004 /* Revision ID */ +#error Wrong include file (pff.h). +#endif + +#if _FS_FAT32 +#if !_FS_FAT16 && !_FS_FAT12 +#define _FS_32ONLY 1 +#else +#define _FS_32ONLY 0 +#endif +#else +#if !_FS_FAT16 && !_FS_FAT12 +#error Wrong _FS_FATxx setting. +#endif +#define _FS_32ONLY 0 +#endif + +#define ABORT(err) {fs->flag = 0; return err;} + + + +/*--------------------------------------------------------*/ +/* DBCS code ranges and SBCS extend char conversion table */ +/*--------------------------------------------------------*/ + +#if _CODE_PAGE == 932 /* 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 /* 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 /* 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 /* Traditional Chinese Big5 */ +#define _DF1S 0x81 +#define _DF1E 0xFE +#define _DS1S 0x40 +#define _DS1E 0x7E +#define _DS2S 0xA1 +#define _DS2E 0xFE + +#elif _CODE_PAGE == 437 /* U.S. (OEM) */ +#define _EXCVT {0x80,0x9A,0x90,0x41,0x8E,0x41,0x8F,0x80,0x45,0x45,0x45,0x49,0x49,0x49,0x8E,0x8F,0x90,0x92,0x92,0x4F,0x99,0x4F,0x55,0x55,0x59,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ + 0x41,0x49,0x4F,0x55,0xA5,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0x21,0xAE,0xAF,0xB0,0xB1,0xB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,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ + 0x41,0xE1,0x41,0xE3,0xE4,0xE5,0xE6,0x43,0x45,0x45,0x45,0x45,0xEC,0xED,0x49,0x49,0xF0,0xF1,0xF2,0xF3,0x4F,0xF5,0xF6,0xF7,0xF8,0x55,0xFA,0x55,0x55,0xFD,0xFE,0xFF} + +#elif _CODE_PAGE == 1257 /* Baltic (Windows) */ +#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0x9C,0x9D,0x9E,0x9F, \ + 0xA0,0xA1,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xA8,0xB9,0xAA,0xBB,0xBC,0xBD,0xBE,0xAF, \ + 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ + 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xFF} + +#elif _CODE_PAGE == 1258 /* Vietnam (OEM, Windows) */ +#define _EXCVT {0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8B,0x8C,0x8D,0x8E,0x8F,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A,0x9B,0xAC,0x9D,0x9E,0x9F, \ + 0xA0,0x21,0xA2,0xA3,0xA4,0xA5,0xA6,0xA7,0xA8,0xA9,0xAA,0xAB,0xAC,0xAD,0xAE,0xAF,0xB0,0xB1,0xB2,0xB3,0xB4,0xB5,0xB6,0xB7,0xB8,0xB9,0xBA,0xBB,0xBC,0xBD,0xBE,0xBF, \ + 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xCC,0xCD,0xCE,0xCF,0xD0,0xD1,0xD2,0xD3,0xD4,0xD5,0xD6,0xD7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xDE,0xDF, \ + 0xC0,0xC1,0xC2,0xC3,0xC4,0xC5,0xC6,0xC7,0xC8,0xC9,0xCA,0xCB,0xEC,0xCD,0xCE,0xCF,0xD0,0xD1,0xF2,0xD3,0xD4,0xD5,0xD6,0xF7,0xD8,0xD9,0xDA,0xDB,0xDC,0xDD,0xFE,0x9F} + +#else +#error Unknown code page. + +#endif + + + +/* Character code support macros */ + +#define IsUpper(c) (((c)>='A')&&((c)<='Z')) +#define IsLower(c) (((c)>='a')&&((c)<='z')) + +#ifndef _EXCVT /* DBCS configuration */ + +#ifdef _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 + +#ifdef _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 /* _EXCVT */ + + +/* 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 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 + + + +/*-------------------------------------------------------------------------- + + Private Functions + +---------------------------------------------------------------------------*/ + + +static +FATFS *FatFs; /* Pointer to the file system object (logical drive) */ + + +/* Fill memory */ +static +void mem_set (void* dst, int val, int cnt) { + char *d = (char*)dst; + while (cnt--) *d++ = (char)val; +} + +/* Compare memory to memory */ +static +int mem_cmp (const void* dst, const void* src, int cnt) { + const char *d = (const char *)dst, *s = (const char *)src; + int r = 0; + while (cnt-- && (r = *d++ - *s++) == 0) ; + return r; +} + + + +/*-----------------------------------------------------------------------*/ +/* FAT access - Read value of a FAT entry */ +/*-----------------------------------------------------------------------*/ + +static +CLUST get_fat ( /* 1:IO error, Else:Cluster status */ + CLUST clst /* Cluster# to get the link information */ +) +{ + BYTE buf[4]; + FATFS *fs = FatFs; + + if (clst < 2 || clst >= fs->n_fatent) /* Range check */ + return 1; + + switch (fs->fs_type) { +#if _FS_FAT12 + case FS_FAT12 : { + UINT wc, bc, ofs; + + bc = (UINT)clst; bc += bc / 2; + ofs = bc % 512; bc /= 512; + if (ofs != 511) { + if (disk_readp(buf, fs->fatbase + bc, ofs, 2)) break; + } else { + if (disk_readp(buf, fs->fatbase + bc, 511, 1)) break; + if (disk_readp(buf+1, fs->fatbase + bc + 1, 0, 1)) break; + } + wc = LD_WORD(buf); + return (clst & 1) ? (wc >> 4) : (wc & 0xFFF); + } +#endif +#if _FS_FAT16 + case FS_FAT16 : + if (disk_readp(buf, fs->fatbase + clst / 256, ((UINT)clst % 256) * 2, 2)) break; + return LD_WORD(buf); +#endif +#if _FS_FAT32 + case FS_FAT32 : + if (disk_readp(buf, fs->fatbase + clst / 128, ((UINT)clst % 128) * 4, 4)) break; + return LD_DWORD(buf) & 0x0FFFFFFF; +#endif + } + + return 1; /* An error occured at the disk I/O layer */ +} + + + + +/*-----------------------------------------------------------------------*/ +/* Get sector# from cluster# / Get cluster field from directory entry */ +/*-----------------------------------------------------------------------*/ + +static +DWORD clust2sect ( /* !=0: Sector number, 0: Failed - invalid cluster# */ + CLUST clst /* Cluster# to be converted */ +) +{ + FATFS *fs = FatFs; + + + clst -= 2; + if (clst >= (fs->n_fatent - 2)) return 0; /* Invalid cluster# */ + return (DWORD)clst * fs->csize + fs->database; +} + + +static +CLUST get_clust ( + BYTE* dir /* Pointer to directory entry */ +) +{ + FATFS *fs = FatFs; + CLUST clst = 0; + + + if (_FS_32ONLY || (_FS_FAT32 && fs->fs_type == FS_FAT32)) { + clst = LD_WORD(dir+DIR_FstClusHI); + clst <<= 16; + } + clst |= LD_WORD(dir+DIR_FstClusLO); + + return clst; +} + + +/*-----------------------------------------------------------------------*/ +/* Directory handling - Rewind directory index */ +/*-----------------------------------------------------------------------*/ + +static +FRESULT dir_rewind ( + DIR *dj /* Pointer to directory object */ +) +{ + CLUST clst; + FATFS *fs = FatFs; + + + dj->index = 0; + clst = dj->sclust; + if (clst == 1 || clst >= fs->n_fatent) /* Check start cluster range */ + return FR_DISK_ERR; + if (_FS_FAT32 && !clst && (_FS_32ONLY || fs->fs_type == FS_FAT32)) /* Replace cluster# 0 with root cluster# if in FAT32 */ + clst = (CLUST)fs->dirbase; + dj->clust = clst; /* Current cluster */ + dj->sect = (_FS_32ONLY || clst) ? clust2sect(clst) : fs->dirbase; /* Current sector */ + + return FR_OK; /* Seek succeeded */ +} + + + + +/*-----------------------------------------------------------------------*/ +/* Directory handling - Move directory index next */ +/*-----------------------------------------------------------------------*/ + +static +FRESULT dir_next ( /* FR_OK:Succeeded, FR_NO_FILE:End of table */ + DIR *dj /* Pointer to directory object */ +) +{ + CLUST clst; + WORD i; + FATFS *fs = FatFs; + + + i = dj->index + 1; + if (!i || !dj->sect) /* Report EOT when index has reached 65535 */ + return FR_NO_FILE; + + if (!(i % 16)) { /* Sector changed? */ + dj->sect++; /* Next sector */ + + if (dj->clust == 0) { /* Static table */ + if (i >= fs->n_rootdir) /* Report EOT when end of table */ + return FR_NO_FILE; + } + else { /* Dynamic table */ + if (((i / 16) & (fs->csize - 1)) == 0) { /* Cluster changed? */ + clst = get_fat(dj->clust); /* Get next cluster */ + if (clst <= 1) return FR_DISK_ERR; + if (clst >= fs->n_fatent) /* When it reached end of dynamic table */ + return FR_NO_FILE; /* Report EOT */ + dj->clust = clst; /* Initialize data for new cluster */ + dj->sect = clust2sect(clst); + } + } + } + + dj->index = i; + + return FR_OK; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Directory handling - Find an object in the directory */ +/*-----------------------------------------------------------------------*/ + +static +FRESULT dir_find ( + DIR *dj, /* Pointer to the directory object linked to the file name */ + BYTE *dir /* 32-byte working buffer */ +) +{ + FRESULT res; + BYTE c; + + + res = dir_rewind(dj); /* Rewind directory object */ + if (res != FR_OK) return res; + + do { + res = disk_readp(dir, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */ + ? FR_DISK_ERR : FR_OK; + if (res != FR_OK) break; + c = dir[DIR_Name]; /* First character */ + if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */ + if (!(dir[DIR_Attr] & AM_VOL) && !mem_cmp(dir, dj->fn, 11)) /* Is it a valid entry? */ + break; + res = dir_next(dj); /* Next entry */ + } while (res == FR_OK); + + return res; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Read an object from the directory */ +/*-----------------------------------------------------------------------*/ +#if _USE_DIR +static +FRESULT dir_read ( + DIR *dj, /* Pointer to the directory object to store read object name */ + BYTE *dir /* 32-byte working buffer */ +) +{ + FRESULT res; + BYTE a, c; + + + res = FR_NO_FILE; + while (dj->sect) { + res = disk_readp(dir, dj->sect, (dj->index % 16) * 32, 32) /* Read an entry */ + ? FR_DISK_ERR : FR_OK; + if (res != FR_OK) break; + c = dir[DIR_Name]; + if (c == 0) { res = FR_NO_FILE; break; } /* Reached to end of table */ + a = dir[DIR_Attr] & AM_MASK; + if (c != 0xE5 && c != '.' && !(a & AM_VOL)) /* Is it a valid entry? */ + break; + res = dir_next(dj); /* Next entry */ + if (res != FR_OK) break; + } + + if (res != FR_OK) dj->sect = 0; + + return res; +} +#endif + + + +/*-----------------------------------------------------------------------*/ +/* Pick a segment and create the object name in directory form */ +/*-----------------------------------------------------------------------*/ + + +static +FRESULT create_name ( + DIR *dj, /* Pointer to the directory object */ + const char **path /* Pointer to pointer to the segment in the path string */ +) +{ + BYTE c, ni, si, i, *sfn; + const char *p; +#if _USE_LCC +#ifdef _EXCVT + static const BYTE cvt[] = _EXCVT; +#endif +#endif + + /* Create file name in directory form */ + sfn = dj->fn; + mem_set(sfn, ' ', 11); + si = i = 0; ni = 8; + p = *path; + for (;;) { + c = p[si++]; + if (c <= ' ' || c == '/') break; /* Break on end of segment */ + if (c == '.' || i >= ni) { + if (ni != 8 || c != '.') break; + i = 8; ni = 11; + continue; + } +#if _USE_LCC +#ifdef _EXCVT + if (c >= 0x80) /* To upper extended char (SBCS) */ + c = cvt[c - 0x80]; +#endif + if (IsDBCS1(c) && i < ni - 1) { /* DBC 1st byte? */ + BYTE d = p[si++]; /* Get 2nd byte */ + sfn[i++] = c; + sfn[i++] = d; + } else +#endif + { /* Single byte code */ + if (_USE_LCC && IsLower(c)) c -= 0x20; /* toupper */ + sfn[i++] = c; + } + } + *path = &p[si]; /* Rerurn pointer to the next segment */ + + sfn[11] = (c <= ' ') ? 1 : 0; /* Set last segment flag if end of path */ + + return FR_OK; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Get file information from directory entry */ +/*-----------------------------------------------------------------------*/ +#if _USE_DIR +static +void get_fileinfo ( /* No return code */ + DIR *dj, /* Pointer to the directory object */ + BYTE *dir, /* 32-byte working buffer */ + FILINFO *fno /* Pointer to store the file information */ +) +{ + BYTE i, c; + char *p; + + + p = fno->fname; + if (dj->sect) { + for (i = 0; i < 8; i++) { /* Copy file name body */ + c = dir[i]; + if (c == ' ') break; + if (c == 0x05) c = 0xE5; + *p++ = c; + } + if (dir[8] != ' ') { /* Copy file name extension */ + *p++ = '.'; + for (i = 8; i < 11; i++) { + c = dir[i]; + if (c == ' ') break; + *p++ = c; + } + } + fno->fattrib = dir[DIR_Attr]; /* Attribute */ + fno->fsize = LD_DWORD(dir+DIR_FileSize); /* Size */ + fno->fdate = LD_WORD(dir+DIR_WrtDate); /* Date */ + fno->ftime = LD_WORD(dir+DIR_WrtTime); /* Time */ + } + *p = 0; +} +#endif /* _USE_DIR */ + + + +/*-----------------------------------------------------------------------*/ +/* Follow a file path */ +/*-----------------------------------------------------------------------*/ + +static +FRESULT follow_path ( /* FR_OK(0): successful, !=0: error code */ + DIR *dj, /* Directory object to return last directory and found object */ + BYTE *dir, /* 32-byte working buffer */ + const char *path /* Full-path string to find a file or directory */ +) +{ + FRESULT res; + + + while (*path == ' ') path++; /* Strip leading spaces */ + if (*path == '/') path++; /* Strip heading separator if exist */ + dj->sclust = 0; /* Set start directory (always root dir) */ + + if ((BYTE)*path < ' ') { /* Null path means the root directory */ + res = dir_rewind(dj); + dir[0] = 0; + + } else { /* Follow path */ + for (;;) { + res = create_name(dj, &path); /* Get a segment */ + if (res != FR_OK) break; + res = dir_find(dj, dir); /* Find it */ + if (res != FR_OK) break; /* Could not find the object */ + if (dj->fn[11]) break; /* Last segment match. Function completed. */ + if (!(dir[DIR_Attr] & AM_DIR)) { /* Cannot follow path because it is a file */ + res = FR_NO_FILE; break; + } + dj->sclust = get_clust(dir); /* Follow next */ + } + } + + return res; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Check a sector if it is an FAT boot record */ +/*-----------------------------------------------------------------------*/ + +static +BYTE check_fs ( /* 0:The FAT boot record, 1:Valid boot record but not an FAT, 2:Not a boot record, 3:Error */ + BYTE *buf, /* Working buffer */ + DWORD sect /* Sector# (lba) to check if it is an FAT boot record or not */ +) +{ + if (disk_readp(buf, sect, 510, 2)) /* Read the boot record */ + return 3; + if (LD_WORD(buf) != 0xAA55) /* Check record signature */ + return 2; + + if (!_FS_32ONLY && !disk_readp(buf, sect, BS_FilSysType, 2) && LD_WORD(buf) == 0x4146) /* Check FAT12/16 */ + return 0; + if (_FS_FAT32 && !disk_readp(buf, sect, BS_FilSysType32, 2) && LD_WORD(buf) == 0x4146) /* Check FAT32 */ + return 0; + return 1; +} + + + + +/*-------------------------------------------------------------------------- + + Public Functions + +--------------------------------------------------------------------------*/ + + + +/*-----------------------------------------------------------------------*/ +/* Mount/Unmount a Locical Drive */ +/*-----------------------------------------------------------------------*/ + +FRESULT pf_mount ( + FATFS *fs /* Pointer to new file system object */ +) +{ + BYTE fmt, buf[36]; + DWORD bsect, fsize, tsect, mclst; + + + FatFs = 0; + + if (disk_initialize() & STA_NOINIT) /* Check if the drive is ready or not */ + return FR_NOT_READY; + + /* Search FAT partition on the drive */ + bsect = 0; + fmt = check_fs(buf, bsect); /* Check sector 0 as an SFD format */ + if (fmt == 1) { /* Not an FAT boot record, it may be FDISK format */ + /* Check a partition listed in top of the partition table */ + if (disk_readp(buf, bsect, MBR_Table, 16)) { /* 1st partition entry */ + fmt = 3; + } else { + if (buf[4]) { /* Is the partition existing? */ + bsect = LD_DWORD(&buf[8]); /* Partition offset in LBA */ + fmt = check_fs(buf, bsect); /* Check the partition */ + } + } + } + if (fmt == 3) return FR_DISK_ERR; + if (fmt) return FR_NO_FILESYSTEM; /* No valid FAT patition is found */ + + /* Initialize the file system object */ + if (disk_readp(buf, bsect, 13, sizeof (buf))) return FR_DISK_ERR; + + fsize = LD_WORD(buf+BPB_FATSz16-13); /* Number of sectors per FAT */ + if (!fsize) fsize = LD_DWORD(buf+BPB_FATSz32-13); + + fsize *= buf[BPB_NumFATs-13]; /* Number of sectors in FAT area */ + fs->fatbase = bsect + LD_WORD(buf+BPB_RsvdSecCnt-13); /* FAT start sector (lba) */ + fs->csize = buf[BPB_SecPerClus-13]; /* Number of sectors per cluster */ + fs->n_rootdir = LD_WORD(buf+BPB_RootEntCnt-13); /* Nmuber of root directory entries */ + tsect = LD_WORD(buf+BPB_TotSec16-13); /* Number of sectors on the file system */ + if (!tsect) tsect = LD_DWORD(buf+BPB_TotSec32-13); + mclst = (tsect /* Last cluster# + 1 */ + - LD_WORD(buf+BPB_RsvdSecCnt-13) - fsize - fs->n_rootdir / 16 + ) / fs->csize + 2; + fs->n_fatent = (CLUST)mclst; + + fmt = 0; /* Determine the FAT sub type */ + if (_FS_FAT12 && mclst < 0xFF7) + fmt = FS_FAT12; + if (_FS_FAT16 && mclst >= 0xFF8 && mclst < 0xFFF7) + fmt = FS_FAT16; + if (_FS_FAT32 && mclst >= 0xFFF7) + fmt = FS_FAT32; + if (!fmt) return FR_NO_FILESYSTEM; + fs->fs_type = fmt; + + if (_FS_32ONLY || (_FS_FAT32 && fmt == FS_FAT32)) + fs->dirbase = LD_DWORD(buf+(BPB_RootClus-13)); /* Root directory start cluster */ + else + fs->dirbase = fs->fatbase + fsize; /* Root directory start sector (lba) */ + fs->database = fs->fatbase + fsize + fs->n_rootdir / 16; /* Data start sector (lba) */ + + fs->flag = 0; + FatFs = fs; + + return FR_OK; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Open or Create a File */ +/*-----------------------------------------------------------------------*/ + +FRESULT pf_open ( + const char *path /* Pointer to the file name */ +) +{ + FRESULT res; + DIR dj; + BYTE sp[12], dir[32]; + FATFS *fs = FatFs; + + + if (!fs) return FR_NOT_ENABLED; /* Check file system */ + + fs->flag = 0; + dj.fn = sp; + res = follow_path(&dj, dir, path); /* Follow the file path */ + if (res != FR_OK) return res; /* Follow failed */ + if (!dir[0] || (dir[DIR_Attr] & AM_DIR)) /* It is a directory */ + return FR_NO_FILE; + + fs->org_clust = get_clust(dir); /* File start cluster */ + fs->fsize = LD_DWORD(dir+DIR_FileSize); /* File size */ + fs->fptr = 0; /* File pointer */ + fs->flag = FA_OPENED; + + return FR_OK; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Read File */ +/*-----------------------------------------------------------------------*/ +#if _USE_READ + +FRESULT pf_read ( + void* buff, /* Pointer to the read buffer (NULL:Forward data to the stream)*/ + UINT btr, /* Number of bytes to read */ + UINT* br /* Pointer to number of bytes read */ +) +{ + DRESULT dr; + CLUST clst; + DWORD sect, remain; + UINT rcnt; + BYTE cs, *rbuff = buff; + FATFS *fs = FatFs; + + + *br = 0; + if (!fs) return FR_NOT_ENABLED; /* Check file system */ + if (!(fs->flag & FA_OPENED)) /* Check if opened */ + return FR_NOT_OPENED; + + remain = fs->fsize - fs->fptr; + if (btr > remain) btr = (UINT)remain; /* Truncate btr by remaining bytes */ + + while (btr) { /* Repeat until all data transferred */ + if ((fs->fptr % 512) == 0) { /* On the sector boundary? */ + cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1)); /* Sector offset in the cluster */ + if (!cs) { /* On the cluster boundary? */ + if (fs->fptr == 0) /* On the top of the file? */ + clst = fs->org_clust; + else + clst = get_fat(fs->curr_clust); + if (clst <= 1) ABORT(FR_DISK_ERR); + fs->curr_clust = clst; /* Update current cluster */ + } + sect = clust2sect(fs->curr_clust); /* Get current sector */ + if (!sect) ABORT(FR_DISK_ERR); + fs->dsect = sect + cs; + } + rcnt = 512 - (UINT)fs->fptr % 512; /* Get partial sector data from sector buffer */ + if (rcnt > btr) rcnt = btr; + dr = disk_readp(!buff ? 0 : rbuff, fs->dsect, (UINT)fs->fptr % 512, rcnt); + if (dr) ABORT(FR_DISK_ERR); + fs->fptr += rcnt; rbuff += rcnt; /* Update pointers and counters */ + btr -= rcnt; *br += rcnt; + } + + return FR_OK; +} +#endif + + + +/*-----------------------------------------------------------------------*/ +/* Write File */ +/*-----------------------------------------------------------------------*/ +#if _USE_WRITE + +FRESULT pf_write ( + const void* buff, /* Pointer to the data to be written */ + UINT btw, /* Number of bytes to write (0:Finalize the current write operation) */ + UINT* bw /* Pointer to number of bytes written */ +) +{ + CLUST clst; + DWORD sect, remain; + const BYTE *p = buff; + BYTE cs; + UINT wcnt; + FATFS *fs = FatFs; + + + *bw = 0; + if (!fs) return FR_NOT_ENABLED; /* Check file system */ + if (!(fs->flag & FA_OPENED)) /* Check if opened */ + return FR_NOT_OPENED; + + if (!btw) { /* Finalize request */ + if ((fs->flag & FA__WIP) && disk_writep(0, 0)) ABORT(FR_DISK_ERR); + fs->flag &= ~FA__WIP; + return FR_OK; + } else { /* Write data request */ + if (!(fs->flag & FA__WIP)) /* Round-down fptr to the sector boundary */ + fs->fptr &= 0xFFFFFE00; + } + remain = fs->fsize - fs->fptr; + if (btw > remain) btw = (UINT)remain; /* Truncate btw by remaining bytes */ + + while (btw) { /* Repeat until all data transferred */ + if ((UINT)fs->fptr % 512 == 0) { /* On the sector boundary? */ + cs = (BYTE)(fs->fptr / 512 & (fs->csize - 1)); /* Sector offset in the cluster */ + if (!cs) { /* On the cluster boundary? */ + if (fs->fptr == 0) /* On the top of the file? */ + clst = fs->org_clust; + else + clst = get_fat(fs->curr_clust); + if (clst <= 1) ABORT(FR_DISK_ERR); + fs->curr_clust = clst; /* Update current cluster */ + } + sect = clust2sect(fs->curr_clust); /* Get current sector */ + if (!sect) ABORT(FR_DISK_ERR); + fs->dsect = sect + cs; + if (disk_writep(0, fs->dsect)) ABORT(FR_DISK_ERR); /* Initiate a sector write operation */ + fs->flag |= FA__WIP; + } + wcnt = 512 - (UINT)fs->fptr % 512; /* Number of bytes to write to the sector */ + if (wcnt > btw) wcnt = btw; + if (disk_writep(p, wcnt)) ABORT(FR_DISK_ERR); /* Send data to the sector */ + fs->fptr += wcnt; p += wcnt; /* Update pointers and counters */ + btw -= wcnt; *bw += wcnt; + if ((UINT)fs->fptr % 512 == 0) { + if (disk_writep(0, 0)) ABORT(FR_DISK_ERR); /* Finalize the currtent secter write operation */ + fs->flag &= ~FA__WIP; + } + } + + return FR_OK; +} +#endif + + + +/*-----------------------------------------------------------------------*/ +/* Seek File R/W Pointer */ +/*-----------------------------------------------------------------------*/ +#if _USE_LSEEK + +FRESULT pf_lseek ( + DWORD ofs /* File pointer from top of file */ +) +{ + CLUST clst; + DWORD bcs, sect, ifptr; + FATFS *fs = FatFs; + + + if (!fs) return FR_NOT_ENABLED; /* Check file system */ + if (!(fs->flag & FA_OPENED)) /* Check if opened */ + return FR_NOT_OPENED; + + if (ofs > fs->fsize) ofs = fs->fsize; /* Clip offset with the file size */ + ifptr = fs->fptr; + fs->fptr = 0; + if (ofs > 0) { + bcs = (DWORD)fs->csize * 512; /* Cluster size (byte) */ + if (ifptr > 0 && + (ofs - 1) / bcs >= (ifptr - 1) / bcs) { /* When seek to same or following cluster, */ + fs->fptr = (ifptr - 1) & ~(bcs - 1); /* start from the current cluster */ + ofs -= fs->fptr; + clst = fs->curr_clust; + } else { /* When seek to back cluster, */ + clst = fs->org_clust; /* start from the first cluster */ + fs->curr_clust = clst; + } + while (ofs > bcs) { /* Cluster following loop */ + clst = get_fat(clst); /* Follow cluster chain */ + if (clst <= 1 || clst >= fs->n_fatent) ABORT(FR_DISK_ERR); + fs->curr_clust = clst; + fs->fptr += bcs; + ofs -= bcs; + } + fs->fptr += ofs; + sect = clust2sect(clst); /* Current sector */ + if (!sect) ABORT(FR_DISK_ERR); + fs->dsect = sect + (fs->fptr / 512 & (fs->csize - 1)); + } + + return FR_OK; +} +#endif + + + +/*-----------------------------------------------------------------------*/ +/* Create a Directroy Object */ +/*-----------------------------------------------------------------------*/ +#if _USE_DIR + +FRESULT pf_opendir ( + DIR *dj, /* Pointer to directory object to create */ + const char *path /* Pointer to the directory path */ +) +{ + FRESULT res; + BYTE sp[12], dir[32]; + FATFS *fs = FatFs; + + + if (!fs) { /* Check file system */ + res = FR_NOT_ENABLED; + } else { + dj->fn = sp; + res = follow_path(dj, dir, path); /* Follow the path to the directory */ + if (res == FR_OK) { /* Follow completed */ + if (dir[0]) { /* It is not the root dir */ + if (dir[DIR_Attr] & AM_DIR) /* The object is a directory */ + dj->sclust = get_clust(dir); + else /* The object is not a directory */ + res = FR_NO_FILE; + } + if (res == FR_OK) + res = dir_rewind(dj); /* Rewind dir */ + } + } + + return res; +} + + + + +/*-----------------------------------------------------------------------*/ +/* Read Directory Entry in Sequense */ +/*-----------------------------------------------------------------------*/ + +FRESULT pf_readdir ( + DIR *dj, /* Pointer to the open directory object */ + FILINFO *fno /* Pointer to file information to return */ +) +{ + FRESULT res; + BYTE sp[12], dir[32]; + FATFS *fs = FatFs; + + + if (!fs) { /* Check file system */ + res = FR_NOT_ENABLED; + } else { + dj->fn = sp; + if (!fno) { + res = dir_rewind(dj); + } else { + res = dir_read(dj, dir); /* Get current directory item */ + if (res == FR_NO_FILE) res = FR_OK; + if (res == FR_OK) { /* A valid entry is found */ + get_fileinfo(dj, dir, fno); /* Get the object information */ + res = dir_next(dj); /* Increment read index for next */ + if (res == FR_NO_FILE) res = FR_OK; + } + } + } + + return res; +} + +#endif /* _USE_DIR */ + diff --git a/bootloader_zevero_sd_m644p_make/pff/src/pff.h b/bootloader_zevero_sd_m644p_make/pff/src/pff.h new file mode 100644 index 0000000..76157d0 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/src/pff.h @@ -0,0 +1,159 @@ +/*---------------------------------------------------------------------------/ +/ Petit FatFs - FAT file system module include file R0.03 (C)ChaN, 2014 +/----------------------------------------------------------------------------/ +/ Petit 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) 2014, ChaN, all right reserved. +/ +/ * The Petit 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. +/ +/----------------------------------------------------------------------------*/ + +#ifndef _PFATFS +#define _PFATFS 4004 /* Revision ID */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "integer.h" +#include "pffconf.h" + +#if _PFATFS != _PFFCONF +#error Wrong configuration file (pffconf.h). +#endif + +#if _FS_FAT32 +#define CLUST DWORD +#else +#define CLUST WORD +#endif + + +/* File system object structure */ + +typedef struct { + BYTE fs_type; /* FAT sub type */ + BYTE flag; /* File status flags */ + BYTE csize; /* Number of sectors per cluster */ + BYTE pad1; + WORD n_rootdir; /* Number of root directory entries (0 on FAT32) */ + CLUST n_fatent; /* Number of FAT entries (= number of clusters + 2) */ + DWORD fatbase; /* FAT start sector */ + DWORD dirbase; /* Root directory start sector (Cluster# on FAT32) */ + DWORD database; /* Data start sector */ + DWORD fptr; /* File R/W pointer */ + DWORD fsize; /* File size */ + CLUST org_clust; /* File start cluster */ + CLUST curr_clust; /* File current cluster */ + DWORD dsect; /* File current data sector */ +} FATFS; + + + +/* Directory object structure */ + +typedef struct { + WORD index; /* Current read/write index number */ + BYTE* fn; /* Pointer to the SFN (in/out) {file[8],ext[3],status[1]} */ + CLUST sclust; /* Table start cluster (0:Static table) */ + CLUST clust; /* Current cluster */ + DWORD sect; /* Current sector */ +} DIR; + + + +/* File status structure */ + +typedef struct { + DWORD fsize; /* File size */ + WORD fdate; /* Last modified date */ + WORD ftime; /* Last modified time */ + BYTE fattrib; /* Attribute */ + char fname[13]; /* File name */ +} FILINFO; + + + +/* File function return code (FRESULT) */ + +typedef enum { + FR_OK = 0, /* 0 */ + FR_DISK_ERR, /* 1 */ + FR_NOT_READY, /* 2 */ + FR_NO_FILE, /* 3 */ + FR_NOT_OPENED, /* 4 */ + FR_NOT_ENABLED, /* 5 */ + FR_NO_FILESYSTEM /* 6 */ +} FRESULT; + + + +/*--------------------------------------------------------------*/ +/* Petit FatFs module application interface */ + +FRESULT pf_mount (FATFS* fs); /* Mount/Unmount a logical drive */ +FRESULT pf_open (const char* path); /* Open a file */ +FRESULT pf_read (void* buff, UINT btr, UINT* br); /* Read data from the open file */ +FRESULT pf_write (const void* buff, UINT btw, UINT* bw); /* Write data to the open file */ +FRESULT pf_lseek (DWORD ofs); /* Move file pointer of the open file */ +FRESULT pf_opendir (DIR* dj, const char* path); /* Open a directory */ +FRESULT pf_readdir (DIR* dj, FILINFO* fno); /* Read a directory item from the open directory */ + + + +/*--------------------------------------------------------------*/ +/* Flags and offset address */ + +/* File status flag (FATFS.flag) */ + +#define FA_OPENED 0x01 +#define FA_WPRT 0x02 +#define FA__WIP 0x40 + + +/* 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 */ + + +/*--------------------------------*/ +/* 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 + + +#ifdef __cplusplus +} +#endif + +#endif /* _PFATFS */ diff --git a/bootloader_zevero_sd_m644p_make/pff/src/pffconf.h b/bootloader_zevero_sd_m644p_make/pff/src/pffconf.h new file mode 100644 index 0000000..d6b984e --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/pff/src/pffconf.h @@ -0,0 +1,89 @@ +/*---------------------------------------------------------------------------/ +/ Petit FatFs - Configuration file R0.03 (C)ChaN, 2014 +/---------------------------------------------------------------------------*/ + +#ifndef _PFFCONF +#define _PFFCONF 4004 /* Revision ID */ + +/*---------------------------------------------------------------------------/ +/ Function Configurations +/---------------------------------------------------------------------------*/ + +#define _USE_READ 1 /* Enable pf_read() function */ +#define _USE_DIR 0 /* Enable pf_opendir() and pf_readdir() function */ +#define _USE_LSEEK 0 /* Enable pf_lseek() function */ +#define _USE_WRITE 0 /* Enable pf_write() function */ + +#define _FS_FAT12 0 /* Enable FAT12 */ +#define _FS_FAT16 1 /* Enable FAT16 */ +#define _FS_FAT32 1 /* Enable FAT32 */ + + +/*---------------------------------------------------------------------------/ +/ Locale and Namespace Configurations +/---------------------------------------------------------------------------*/ + +#define _USE_LCC 0 /* Allow lower case characters for path name */ + +#define _CODE_PAGE 437 +/* The _CODE_PAGE specifies the code page to be used on the target system. +/ SBCS code pages with _USE_LCC == 1 requiers a 128 byte of case conversion +/ table. This might occupy RAM on some platforms, e.g. avr-gcc. +/ When _USE_LCC == 0, _CODE_PAGE has no effect. +/ +/ 932 - Japanese Shift_JIS (DBCS, OEM, Windows) +/ 936 - Simplified Chinese GBK (DBCS, OEM, Windows) +/ 949 - Korean (DBCS, OEM, Windows) +/ 950 - Traditional Chinese Big5 (DBCS, OEM, Windows) +/ 1250 - Central Europe (Windows) +/ 1251 - Cyrillic (Windows) +/ 1252 - Latin 1 (Windows) +/ 1253 - Greek (Windows) +/ 1254 - Turkish (Windows) +/ 1255 - Hebrew (Windows) +/ 1256 - Arabic (Windows) +/ 1257 - Baltic (Windows) +/ 1258 - Vietnam (OEM, Windows) +/ 437 - U.S. (OEM) +/ 720 - Arabic (OEM) +/ 737 - Greek (OEM) +/ 775 - Baltic (OEM) +/ 850 - Multilingual Latin 1 (OEM) +/ 858 - Multilingual Latin 1 + Euro (OEM) +/ 852 - Latin 2 (OEM) +/ 855 - Cyrillic (OEM) +/ 866 - Russian (OEM) +/ 857 - Turkish (OEM) +/ 862 - Hebrew (OEM) +/ 874 - Thai (OEM, Windows) +*/ + + +/*---------------------------------------------------------------------------/ +/ System Configurations +/---------------------------------------------------------------------------*/ + +#define _WORD_ACCESS 1 +/* The _WORD_ACCESS option is an only platform dependent option. It defines +/ which access method is used to the word data on the FAT volume. +/ +/ 0: Byte-by-byte access. Always compatible with all platforms. +/ 1: Word access. Do not choose this unless under both the following conditions. +/ +/ * Address misaligned memory access is always allowed for ALL instructions. +/ * Byte order on the memory is little-endian. +/ +/ If it is the case, _WORD_ACCESS can also be set to 1 to improve performance and +/ reduce code size. Following table shows an example of some processor types. +/ +/ ARM7TDMI 0 ColdFire 0 V850E 0 +/ Cortex-M3 0 Z80 0/1 V850ES 0/1 +/ Cortex-M0 0 RX600(LE) 0/1 TLCS-870 0/1 +/ AVR 0/1 RX600(BE) 0 TLCS-900 0/1 +/ AVR32 0 RL78 0 R32C 0 +/ PIC18 0/1 SH-2 0 M16C 0/1 +/ PIC24 0 H8S 0 MSP430 0 +/ PIC32 0 H8/300H 0 x86 0/1 +*/ + +#endif /* _PFFCONF */ diff --git a/bootloader_zevero_sd_m644p_make/spi_pins.h b/bootloader_zevero_sd_m644p_make/spi_pins.h new file mode 100644 index 0000000..bda946b --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/spi_pins.h @@ -0,0 +1,76 @@ +// MOSI, MISO, and SCK pin definitions for all standard ATmega microcontrollers +#ifndef SPI_PINS_H +#define SPI_PINS_H + +#if defined(__AVR_ATmega64__) || defined(__AVR_ATmega64A__) || defined(__AVR_ATmega128__) || defined(__AVR_ATmega128A__) || \ +defined(__AVR_ATmega640__) || defined(__AVR_ATmega1280__) || defined(__AVR_ATmega2560__) || \ +defined(__AVR_ATmega1281__) || defined(__AVR_ATmega2561__) || \ +defined(__AVR_ATmega165__) || defined(__AVR_ATmega165A__) || defined(__AVR_ATmega165P__) || defined(__AVR_ATmega165PA__) || defined(__AVR_ATmega325__) ||defined(__AVR_ATmega325A__) || defined(__AVR_ATmega325P__) || defined(__AVR_ATmega325PA__) || defined(__AVR_ATmega645__) || defined(__AVR_ATmega645A__) || defined(__AVR_ATmega645P__) || \ +defined(__AVR_ATmega3250__) || defined(__AVR_ATmega3250A__) || defined(__AVR_ATmega3250P__) || defined(__AVR_ATmega3250PA__) || defined(__AVR_ATmega6450__) || defined(__AVR_ATmega6450A__) || defined(__AVR_ATmega6450P__) || \ +defined(__AVR_ATmega169__) || defined(__AVR_ATmega169A__) || defined(__AVR_ATmega169P__) || defined(__AVR_ATmega169PA__) || defined(__AVR_ATmega329__) || defined(__AVR_ATmega329A__) || defined(__AVR_ATmega329P__) || defined(__AVR_ATmega329PA__) || defined(__AVR_ATmega3290__) || defined(__AVR_ATmega3290A__) || defined(__AVR_ATmega3290P__) || defined(__AVR_ATmega3290PA__) || defined(__AVR_ATmega649__) || defined(__AVR_ATmega649A__) || defined(__AVR_ATmega649P__) || defined(__AVR_ATmega6490__) || defined(__AVR_ATmega6490A__) || defined(__AVR_ATmega6490P__) || \ +defined(__AVR_ATmega8U2__) || defined(__AVR_ATmega16U2__) || defined(__AVR_ATmega16U4__) || defined(__AVR_ATmega32U2__) || defined(__AVR_ATmega32U4__) || defined(__AVR_ATmega32U6__) || \ +defined(__AVR_AT90CAN32__ ) || defined(__AVR_AT90CAN64__ ) || defined(__AVR_AT90CAN128__ ) || \ +defined(__AVR_AT90USB82__) || defined(__AVR_AT90USB162__) || defined(__AVR_AT90USB646__) || defined(__AVR_AT90USB647__) || defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) +#define DDR_DI _SFR_IO_ADDR(DDRB), 2 //MMC DI MOSI pin (DDR, PORT) +#define PORT_DI _SFR_IO_ADDR(PORTB), 2 + +#define PIN_DO _SFR_IO_ADDR(PINB), 3 //MMC DO MISO pin (PIN, PORT) +#define PORT_DO _SFR_IO_ADDR(PORTB), 3 + +#define DDR_CK _SFR_IO_ADDR(DDRB), 1 //MMC SCK pin (DDR, PORT) +#define PORT_CK _SFR_IO_ADDR(PORTB), 1 + + +#elif defined(__AVR_ATmega8__) || defined(__AVR_ATmega8A__) || defined(__AVR_ATmega48__) || defined(__AVR_ATmega48A__) || defined(__AVR_ATmega48P__) || defined(__AVR_ATmega48PA__) || defined(__AVR_ATmega48PB__) || defined(__AVR_ATmega88__) || defined(__AVR_ATmega88A__) || defined(__AVR_ATmega88P__) || defined(__AVR_ATmega88PA__) || defined(__AVR_ATmega88PB__) || defined(__AVR_ATmega168__) || defined(__AVR_ATmega168A__) || defined(__AVR_ATmega168P__) || defined(__AVR_ATmega168PA__) || defined(__AVR_ATmega168PB__) ||defined(__AVR_ATmega328__) || defined(__AVR_ATmega328P__) || defined(__AVR_ATmega328PB__) +#define DDR_DI _SFR_IO_ADDR(DDRB), 3 +#define PORT_DI _SFR_IO_ADDR(PORTB), 3 + +#define PIN_DO _SFR_IO_ADDR(PINB), 4 +#define PORT_DO _SFR_IO_ADDR(PORTB), 4 + +#define DDR_CK _SFR_IO_ADDR(DDRB), 5 +#define PORT_CK _SFR_IO_ADDR(PORTB), 5 + + +#elif defined(__AVR_ATmega16__) || defined(__AVR_ATmega16A__) || defined(__AVR_ATmega32__) || defined(__AVR_ATmega32A__) || \ +defined(__AVR_ATmega161__) || defined(__AVR_ATmega162__) || defined(__AVR_ATmega163__) || \ +defined(__AVR_ATmega164A__) || defined(__AVR_ATmega164P__) || defined(__AVR_ATmega164PA__) || defined(__AVR_ATmega323__) || defined(__AVR_ATmega324A__) || defined(__AVR_ATmega324P__) || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega324PB__) || defined(__AVR_ATmega324PA__) || defined(__AVR_ATmega644__) || defined(__AVR_ATmega644A__) || defined(__AVR_ATmega644P__) || defined(__AVR_ATmega644PA__) || defined(__AVR_ATmega1284__) || defined(__AVR_ATmega1284P__) || \ +defined(__AVR_ATmega8515__) || defined(__AVR_ATmega8535__) +#define DDR_DI _SFR_IO_ADDR(DDRB), 5 +#define PORT_DI _SFR_IO_ADDR(PORTB), 5 + +#define PIN_DO _SFR_IO_ADDR(PINB), 6 +#define PORT_DO _SFR_IO_ADDR(PORTB), 6 + +#define DDR_CK _SFR_IO_ADDR(DDRB), 7 +#define PORT_CK _SFR_IO_ADDR(PORTB), 7 + + +#elif defined(__AVR_ATmega16M1__) || defined(__AVR_ATmega32M1__) || defined(__AVR_ATmega64M1__) || defined(__AVR_ATmega32C1__) || defined(__AVR_ATmega64C1__) || \ +defined(__AVR_AT90PWM1__) || defined(__AVR_AT90PWM2__) || defined(__AVR_AT90PWM2B__) || defined(__AVR_AT90PWM3__) || defined(__AVR_AT90PWM3B__) || defined(__AVR_AT90PWM216__) || defined(__AVR_AT90PWM316__) +#define DDR_DI _SFR_IO_ADDR(DDRB), 1 +#define PORT_DI _SFR_IO_ADDR(PORTB), 1 + +#define PIN_DO _SFR_IO_ADDR(PINB), 0 +#define PORT_DO _SFR_IO_ADDR(PORTB), 0 + +#define DDR_CK _SFR_IO_ADDR(DDRB), 7 +#define PORT_CK _SFR_IO_ADDR(PORTB), 7 + + +#elif defined(__AVR_AT90PWM81__) || defined(__AVR_AT90PWM161__) +#define DDR_DI _SFR_IO_ADDR(DDRB), 4 +#define PORT_DI _SFR_IO_ADDR(PORTB), 4 + +#define PIN_DO _SFR_IO_ADDR(PINB), 6 +#define PORT_DO _SFR_IO_ADDR(PORTB), 6 + +#define DDR_CK _SFR_IO_ADDR(DDRB), 5 +#define PORT_CK _SFR_IO_ADDR(PORTB), 5 + + +#else +#error SPI pins not defined for your processor. You must modify spi_pins.h +#endif + +#endif //SPI_PINS_H diff --git a/bootloader_zevero_sd_m644p_make/uart/uart.c b/bootloader_zevero_sd_m644p_make/uart/uart.c new file mode 100644 index 0000000..12f9956 --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/uart/uart.c @@ -0,0 +1,107 @@ +/* +** This is a simple set of UART routines for debug purposes +** It's Tx only and the baud rate is fixed (see UART_init) +*/ + +#include "uart.h" +#define UCSRA UCSR0A +#define UCSRB UCSR0B +#define UCSRC UCSR0C +#define UBRRH UBRR0H +#define UBRRL UBRR0L +#define UDRE UDRE0 +#define UDR UDR0 +#define RXC RXC0 + +#if BOOT_ADR > 0xFFFF + #define PGM_READ_BYTE(x) pgm_read_byte_far(x) +#else + #define PGM_READ_BYTE(x) pgm_read_byte(x) +#endif + +// Just enable the UART Tx and set baud rate for 38400 on 3.6864MHz (STK500) + +void UART_init(void) { + UCSRB = (1 << TXEN0); + UBRRL = USE_UART; // SEE HERE: http://wormfood.net/avrbaudcalc.php +} + +// The classic Tx one character routine +void UART_put(uint8_t c) { + while (!(UCSRA & (1 << UDRE))); + UDR = c; +} + +// classic Tx a C-string routine +// As there is no .data (in the bootloader) it only makes sense for theis to use PSTR() +void UART_puts(const char * str) { + char c; + do { + c = PGM_READ_BYTE(str++); + if (c) { + UART_put(c); + } + } while (c != 0); +} + +// Just outputs "\r\n" +void UART_newline(void){ + UART_put('\r'); + UART_put('\n'); +} + +// used in printing a 2 digit hex number, outputs one of the two nibbles +// the parameter is expected to be 0..F +void UART_putnibble(uint8_t c) { + if (c < 10) { + UART_put('0' + c); + } + else { + UART_put('A' + c - 10); + } +} + +// print both nibbles of an 8 bit hex number +void UART_puthex(uint8_t c) { + UART_putnibble(c >> 4); + UART_putnibble(c & 0x0F); +} + +// print both bytes of a 16 bit hex number +void UART_puthex16(uint16_t n) { + UART_puthex(n >> 8); + UART_puthex(n & 0xFF); +} +// print both bytes of a 32 bit hex number +void UART_puthex32(uint32_t n) { + UART_puthex(n >> 24); + UART_puthex(n >> 16 & 0xFF); + UART_puthex(n >> 8 & 0xFF); + UART_puthex(n & 0xFF); +} + +// this expect the first parameter to be a string in dlash (that is PSTR()) +// and then the second to be a value to print out in hex. typically used in +// the form UART_putsP(PSTR("SecPerClus = "), SecPerClus) +void UART_putsP(const char * str, uint16_t n) { + UART_puts(str); + UART_puthex16(n); + UART_newline(); +} + +// dump the 512 bytes at the given address in the form: +// CD BF 10 E0 A0 E6 B0 E0 E4 E5 F0 E0 02 C0 05 90 Ϳ ������� � � +void UART_dumpsector(uint8_t * Buff) { + for (uint16_t i=0; i<512; i++) { + if ((i % 16) == 0) { + UART_put(' '); + for(uint16_t j=(i -16); j<=i; j++) { + UART_put(((Buff[j]>=(uint8_t)32) && (Buff[j]<(uint8_t)127)) ? Buff[j] : '.'); + } + UART_newline(); + } + UART_puthex(Buff[i]); + UART_put(' '); + } + UART_newline(); +} diff --git a/bootloader_zevero_sd_m644p_make/uart/uart.h b/bootloader_zevero_sd_m644p_make/uart/uart.h new file mode 100644 index 0000000..015783e --- /dev/null +++ b/bootloader_zevero_sd_m644p_make/uart/uart.h @@ -0,0 +1,32 @@ +#include +#include + +/* +** This is a simple set of UART routines for debug purposes +** It's Tx only and the baud rate is fixed (see UART_init) +*/ + +// Just enable the UART Tx and set baud rate for 38400 on 3.6864MHz (STK500) +void UART_init(void); +// The classic Tx one character routine +void UART_put(uint8_t c); +// classic Tx a C-string routine (string in PROGMEM) +void UART_puts(const char * str); +// this expect the first parameter to be a string in dlash (that is PSTR()) +// and then the second to be a value to print out in hex. typically used in +// the form UART_putsP(PSTR("SecPerClus = "), SecPerClus) +void UART_putsP(const char * str, uint16_t n); +// Just outputs "\r\n" +void UART_newline(void); +// used in printing a 2 digit hex number, outputs one of the two nibbles +// the parameter is expected to be 0..F +void UART_putnibble(uint8_t c); +// print both nibbles of an 8 bit hex number +void UART_puthex(uint8_t c); +// print both bytes of a 16 bit hex number +void UART_puthex16(uint16_t n); +// print both bytes of a 32 bit hex number +void UART_puthex32(uint32_t n); +// dump the 512 bytes at the given address in the form: +// CD BF 10 E0 A0 E6 B0 E0 E4 E5 F0 E0 02 C0 05 90 Ϳ ������� � � +void UART_dumpsector(uint8_t * Buff);