From 98b18dfcbf50a7a174171f93ea69f7b6e29601e0 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:38:24 +0400 Subject: [PATCH 01/66] Initial commit of a fully reworked project structure This is the first patch in series. Signed-off-by: Andrew 'Necromant' Andrianov --- Makefile | 327 +-- Makefile.old | 248 ++ README => README.old | 20 +- boards/maple/Makefile | 1 + boards/maple/board.c | 74 + boards/maple/config.h | 68 + boards/te-stm32f103ret6kit/Makefile | 1 + boards/te-stm32f103ret6kit/board.c | 100 + .../te-stm32f103ret6kit/config.h | 12 +- src/Makefile | 0 src/common/Makefile | 7 + common.h => src/common/common.h | 0 dfu.c => src/common/dfu.c | 75 +- dfu.h => src/common/dfu.h | 6 +- hardware.c => src/common/hardware.c | 80 +- hardware.h => src/common/hardware.h | 6 +- main.c => src/common/main.c | 18 +- usb.c => src/common/usb.c | 18 +- usb.h => src/common/usb.h | 10 +- usb_callbacks.c => src/common/usb_callbacks.c | 2 +- .../common/usb_descriptor.c | 25 +- .../common/usb_descriptor.h | 0 src/startup/Makefile | 2 + {stm32_lib => src/startup}/c_only_md.ld | 344 +-- {stm32_lib => src/startup}/c_only_md_RAM.ld | 338 +-- {stm32_lib => src/startup}/c_only_startup.s | 733 +++--- .../startup}/c_only_startup_user.s | 254 +-- {stm32_lib => src/startup}/cortexm3_macro.h | 106 +- {stm32_lib => src/startup}/cortexm3_macro.s | 0 {stm32_lib => src/startup}/stm32f10x_type.h | 160 +- src/usb_lib/Makefile | 6 + {usb_lib => src/usb_lib}/usb_conf.h | 168 +- {usb_lib => src/usb_lib}/usb_core.c | 2032 ++++++++--------- {usb_lib => src/usb_lib}/usb_core.h | 488 ++-- {usb_lib => src/usb_lib}/usb_def.h | 160 +- {usb_lib => src/usb_lib}/usb_init.c | 128 +- {usb_lib => src/usb_lib}/usb_init.h | 98 +- {usb_lib => src/usb_lib}/usb_int.c | 384 ++-- {usb_lib => src/usb_lib}/usb_int.h | 66 +- {usb_lib => src/usb_lib}/usb_lib.h | 74 +- {usb_lib => src/usb_lib}/usb_mem.c | 146 +- {usb_lib => src/usb_lib}/usb_mem.h | 64 +- {usb_lib => src/usb_lib}/usb_regs.c | 1496 ++++++------ {usb_lib => src/usb_lib}/usb_regs.h | 1238 +++++----- {usb_lib => src/usb_lib}/usb_type.h | 144 +- 45 files changed, 5018 insertions(+), 4709 deletions(-) create mode 100644 Makefile.old rename README => README.old (88%) create mode 100644 boards/maple/Makefile create mode 100644 boards/maple/board.c create mode 100644 boards/maple/config.h create mode 100644 boards/te-stm32f103ret6kit/Makefile create mode 100644 boards/te-stm32f103ret6kit/board.c rename config.h => boards/te-stm32f103ret6kit/config.h (88%) create mode 100644 src/Makefile create mode 100644 src/common/Makefile rename common.h => src/common/common.h (100%) rename dfu.c => src/common/dfu.c (87%) rename dfu.h => src/common/dfu.h (98%) rename hardware.c => src/common/hardware.c (79%) rename hardware.h => src/common/hardware.h (98%) rename main.c => src/common/main.c (93%) rename usb.c => src/common/usb.c (97%) rename usb.h => src/common/usb.h (94%) rename usb_callbacks.c => src/common/usb_callbacks.c (98%) rename usb_descriptor.c => src/common/usb_descriptor.c (93%) rename usb_descriptor.h => src/common/usb_descriptor.h (100%) create mode 100644 src/startup/Makefile rename {stm32_lib => src/startup}/c_only_md.ld (88%) rename {stm32_lib => src/startup}/c_only_md_RAM.ld (87%) rename {stm32_lib => src/startup}/c_only_startup.s (92%) rename {stm32_lib => src/startup}/c_only_startup_user.s (88%) rename {stm32_lib => src/startup}/cortexm3_macro.h (97%) rename {stm32_lib => src/startup}/cortexm3_macro.s (100%) rename {stm32_lib => src/startup}/stm32f10x_type.h (97%) create mode 100644 src/usb_lib/Makefile rename {usb_lib => src/usb_lib}/usb_conf.h (97%) rename {usb_lib => src/usb_lib}/usb_core.c (96%) rename {usb_lib => src/usb_lib}/usb_core.h (97%) rename {usb_lib => src/usb_lib}/usb_def.h (97%) rename {usb_lib => src/usb_lib}/usb_init.c (97%) rename {usb_lib => src/usb_lib}/usb_init.h (98%) rename {usb_lib => src/usb_lib}/usb_int.c (96%) rename {usb_lib => src/usb_lib}/usb_int.h (98%) rename {usb_lib => src/usb_lib}/usb_lib.h (97%) rename {usb_lib => src/usb_lib}/usb_mem.c (97%) rename {usb_lib => src/usb_lib}/usb_mem.h (98%) rename {usb_lib => src/usb_lib}/usb_regs.c (91%) rename {usb_lib => src/usb_lib}/usb_regs.h (94%) rename {usb_lib => src/usb_lib}/usb_type.h (97%) diff --git a/Makefile b/Makefile index 465c8d1..06acefe 100644 --- a/Makefile +++ b/Makefile @@ -1,249 +1,112 @@ -# Makefile skeleton adapted from Peter Harrison's - www.micromouse.com +unexport +CROSS_COMPILE?=arm-ncrmnt-eabi- +SRCDIR=. +OBJDIR=./build -# MCU name and submodel MCU = cortex-m3 SUBMDL = stm32f103 - -# toolchain (using code sourcery now) -TCHAIN = arm-none-eabi THUMB = -mthumb THUMB_IW = -mthumb-interwork -# Target file name (without extension). -BUILDDIR = build -TARGET = $(BUILDDIR)/maple_boot - -ST_LIB = stm32_lib -ST_USB = usb_lib - -# Optimization level [0,1,2,3,s] -OPT = 0 -DEBUG = -g -#DEBUG = dwarf-2 +TARGET=$(OBJDIR)/ucboot-$(BOARD) -INCDIRS = ./$(ST_LIB) ./$(ST_USB) - -CFLAGS = $(DEBUG) CFLAGS += -O$(OPT) CFLAGS += -ffunction-sections -fdata-sections CFLAGS += -Wall -Wimplicit CFLAGS += -Wcast-align CFLAGS += -Wpointer-arith -Wswitch CFLAGS += -Wredundant-decls -Wreturn-type -Wshadow -Wunused -CFLAGS += -Wa,-adhlns=$(BUILDDIR)/$(subst $(suffix $<),.lst,$<) -CFLAGS += $(patsubst %,-I%,$(INCDIRS)) +CFLAGS += -Wa,-adhlns=$(OBJDIR)/$(subst $(suffix $<),.lst,$<) +CFLAGS += -I$(OBJDIR)/ -# Aeembler Flags -ASFLAGS = -Wa,-adhlns=$(BUILDDIR)/$(<:.s=.lst)#,--g$(DEBUG) +GENDEPFLAGS = -MD -MP -MF $(@).d +CFLAGS += -mcpu=$(MCU) $(THUMB_IW) $(GENDEPFLAGS) +ASFLAGS = -Wa,-adhlns=$(OBJDIR)/$(<:.s=.lst)#,--g$(DEBUG) +ASFLAGS += -mcpu=$(MCU) $(THUMB_IW) -x assembler-with-cpp +ASFLAGS += $(GENDEPFLAGS) LDFLAGS = -nostartfiles -Wl,-Map=$(TARGET).map,--cref,--gc-sections LDFLAGS += -lc -lgcc - -# Set the linker script -LDFLAGS +=-T$(ST_LIB)/c_only_md.ld - -# Define programs and commands. -SHELL = sh -CC = $(TCHAIN)-gcc -CPP = $(TCHAIN)-g++ -AR = $(TCHAIN)-ar -OBJCOPY = $(TCHAIN)-objcopy -OBJDUMP = $(TCHAIN)-objdump -SIZE = $(TCHAIN)-size -NM = $(TCHAIN)-nm -REMOVE = rm -f -REMOVEDIR = rm -r -COPY = cp - -# Define Messages -# English -MSG_ERRORS_NONE = Errors: none -MSG_BEGIN = "-------- begin --------" -MSG_ETAGS = Created TAGS File -MSG_END = -------- end -------- -MSG_SIZE_BEFORE = Size before: -MSG_SIZE_AFTER = Size after: -MSG_FLASH = Creating load file for Flash: -MSG_EXTENDED_LISTING = Creating Extended Listing: -MSG_SYMBOL_TABLE = Creating Symbol Table: -MSG_LINKING = Linking: -MSG_COMPILING = Compiling C: -MSG_ASSEMBLING = Assembling: -MSG_CLEANING = Cleaning project: - -# Combine all necessary flags and optional flags. -# Add target processor to flags. -GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d -ALL_CFLAGS = -mcpu=$(MCU) $(THUMB_IW) -I. $(CFLAGS) $(GENDEPFLAGS) -ALL_ASFLAGS = -mcpu=$(MCU) $(THUMB_IW) -I. -x assembler-with-cpp $(ASFLAGS) - -# --------------------------------------------- # -# file management -ASRC = $(ST_LIB)/c_only_startup.s $(ST_LIB)/cortexm3_macro.s - -STM32SRCS = - -_STM32USBSRCS = usb_regs.c \ -usb_int.c \ -usb_init.c \ -usb_core.c \ -usb_mem.c - -STM32USBSRCS = $(patsubst %, $(ST_USB)/%,$(_STM32USBSRCS)) - -SRCS = usb.c usb_callbacks.c usb_descriptor.c main.c hardware.c dfu.c - - -SRC = $(SRCS) $(STM32SRCS) $(STM32USBSRCS) - -# Define all object files. -_COBJ = $(SRC:.c=.o) -_AOBJ = $(ASRC:.s=.o) -COBJ = $(patsubst %, $(BUILDDIR)/%,$(_COBJ)) -AOBJ = $(patsubst %, $(BUILDDIR)/%,$(_AOBJ)) - -# Define all listing files. -_LST = $(ASRC:.s=.lst) -_LST += $(SRC:.c=.lst) -LST = $(patsubst %, $(BUILDDIR)/%,$(_LST)) - -# Display size of file. -HEXSIZE = $(SIZE) --target=binary $(TARGET).hex -ELFSIZE = $(SIZE) -A $(TARGET).elf - -# go! -all: begin gccversion build sizeafter finished end -build: elf bin lss sym - -bin: $(TARGET).bin -elf: $(TARGET).elf -lss: $(TARGET).lss -sym: $(TARGET).sym -dfu: $(TARGET).bin - sudo dfu-util -d 0110:1001 -a 0 -D $(TARGET).bin - -begin: - mkdir -p build/stm32_lib - mkdir -p build/usb_lib - @echo -- - @echo $(MSG_BEGIN) - @echo $(COBJ) - -finished: - @echo $(MSG_ERRORS_NONE) -tags: - etags `find . -name "*.c" -o -name "*.cpp" -o -name "*.h"` - @echo $(MSG_ETAGS) -end: - @echo $(MSG_END) - @echo -sizeafter: - @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi -gccversion: - @$(CC) --version - -program: - @echo "Flash-programming with OpenOCD" - cp $(TARGET).bin flash/tmpflash.bin - cd flash && openocd -f flash.cfg - -program_serial: - @echo "Flash-programming with stm32loader.py" - ./flash/stm32loader.py -p /dev/ttyUSB0 -evw build/maple_boot.bin - -debug: $(TARGET).bin - @echo "Flash-programming with OpenOCD - DEBUG" - cp $(TARGET).bin flash/tmpflash.bin - cd flash && openocd -f debug.cfg - -install: $(TARGET).bin - cp $(TARGET).bin build/main.bin - openocd -f flash/perry_flash.cfg - -run: $(TARGET).bin - openocd -f flash/run.cfg - -# Create final output file (.hex) from ELF output file. -%.hex: %.elf - @echo - @echo $(MSG_FLASH) $@ - $(OBJCOPY) -O binary $< $@ - -# Create final output file (.bin) from ELF output file. -%.bin: %.elf - @echo - @echo $(MSG_FLASH) $@ - $(OBJCOPY) -O binary $< $@ - - -# Create extended listing file from ELF output file. -# testing: option -C -%.lss: %.elf - @echo - @echo $(MSG_EXTENDED_LISTING) $@ - $(OBJDUMP) -h -S -D $< > $@ - - -# Create a symbol table from ELF output file. -%.sym: %.elf - @echo - @echo $(MSG_SYMBOL_TABLE) $@ - $(NM) -n $< > $@ - - -# Link: create ELF output file from object files. -.SECONDARY : $(TARGET).elf -.PRECIOUS : $(COBJ) $(AOBJ) - -%.elf: $(COBJ) $(AOBJ) - @echo - @echo $(MSG_LINKING) $@ - $(CC) $(THUMB) $(ALL_CFLAGS) $(AOBJ) $(COBJ) --output $@ $(LDFLAGS) - -# Compile: create object files from C source files. ARM/Thumb -$(COBJ) : $(BUILDDIR)/%.o : %.c - @echo - @echo $(MSG_COMPILING) $< - $(CC) -c $(THUMB) $(ALL_CFLAGS) $< -o $@ - -# Assemble: create object files from assembler source files. ARM/Thumb -$(AOBJ) : $(BUILDDIR)/%.o : %.s - @echo - @echo $(MSG_ASSEMBLING) $< - $(CC) -c $(THUMB) $(ALL_ASFLAGS) $< -o $@ - -clean: begin clean_list finished end - -clean_list : - @echo - @echo $(MSG_CLEANING) - $(REMOVE) $(TARGET).hex - $(REMOVE) $(TARGET).bin - $(REMOVE) $(TARGET).obj - $(REMOVE) $(TARGET).elf - $(REMOVE) $(TARGET).map - $(REMOVE) $(TARGET).obj - $(REMOVE) $(TARGET).a90 - $(REMOVE) $(TARGET).sym - $(REMOVE) $(TARGET).lnk - $(REMOVE) $(TARGET).lss - $(REMOVE) $(COBJ) - $(REMOVE) $(AOBJ) - $(REMOVE) $(LST) - $(REMOVE) flash/tmpflash.bin -# $(REMOVE) $(SRC:.c=.s) -# $(REMOVE) $(SRC:.c=.d) - $(REMOVE) .dep/* - -# Include the dependency files. --include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) - - -# Listing of phony targets. -.PHONY : all begin finish tags end sizeafter gccversion \ -build elf hex bin lss sym clean clean_list program cscope - -cscope: - rm -rf *.cscope - find . -iname "*.[hcs]" | grep -v examples | xargs cscope -R -b - +#TODO:... +LDFLAGS +=-T$(SRCDIR)/src/startup/c_only_md.ld + +CROSS_CC=$(CROSS_COMPILE)gcc +CROSS_AS=$(CROSS_COMPILE)as +CROSS_LD=$(CROSS_COMPILE)ld +CROSS_OBJCOPY=$(CROSS_COMPILE)objcopy +CROSS_OBJDUMP=$(CROSS_COMPILE)objdump + +# Pretty colors +tb_ylw=$(shell echo -e '\e[1;33m') +tb_blu=$(shell echo -e '\e[1;34m') +tb_pur=$(shell echo -e '\e[1;35m') +col_rst=$(shell echo -e '\e[0m') + +ifneq ($(V),y) + SILENT_CC = @echo ' $(tb_ylw)[CC]$(col_rst) ' $(@F); + SILENT_AS = @echo ' $(tb_blu)[AS]$(col_rst) ' $(@F); + SILENT_LD = @echo ' $(tb_pur)[LD]$(col_rst) ' $(@F); + SILENT_OBJCOPY = @echo ' $(tb_pur)[OBJCOPY]$(col_rst) ' $(@F); + SILENT_OBJDUMP = @echo ' $(tb_pur)[OBJDUMP]$(col_rst) ' $(OBJDUMP); + #Shut up this crap + MAKEFLAGS+=--no-print-directory +endif + +# This is a dirty hack, since I don't want to +# recurse properly into dirs with recusive make invocation +# and the only method of reading submakes descending into subdirs, +# without recusive invocation I came up with looks like shit +# Anyway, this would be a bit too much for such a project + +define suck_directory +-include $(SRCDIR)/$(1)/Makefile +mkdirs+=$(OBJDIR)/$(1) +objects+=$$(addprefix $(OBJDIR)/$(1)/,$$(objects-$(2))) +endef + +$(eval $(call suck_directory,src/common,common)) +$(eval $(call suck_directory,src/usb_lib,usb_lib)) +$(eval $(call suck_directory,src/startup,startup)) +$(eval $(call suck_directory,boards/$(BOARD),y)) + +all: + @$(foreach d,$(shell ls $(SRCDIR)/boards/),make BOARD=$(d) build;) + +-include $(addsuffix .d,$(objects)) + +$(OBJDIR)/%.o: $(SRCDIR)/%.c + $(SILENT_CC) $(CROSS_CC) $(CFLAGS) -c -o $(@) $< + +$(OBJDIR)/%.o: $(SRCDIR)/%.S + $(SILENT_AS) $(CROSS_CC) $(ASFLAGS) -c -o $(@) $< + +$(OBJDIR)/%.o: $(SRCDIR)/%.s + $(SILENT_AS) $(CROSS_CC) $(ASFLAGS) -c -o $(@) $< + +$(OBJDIR)/%.elf: $(objects) + $(SILENT_LD) $(CROSS_CC) $(CFLAGS) --output $(@) $(LDFLAGS) $(objects) + +$(OBJDIR)/%.bin: $(OBJDIR)/%.elf + $(SILENT_OBJCOPY) $(CROSS_OBJCOPY) -O binary $< $@ + +#config.h symlink voodoo ensures we'll get stuff properly rebuilt +warmup: + @mkdir -p $(OBJDIR) + @$(foreach d,$(mkdirs),mkdir -p $(d);) + @ln -sf $(abspath $(SRCDIR)/boards/$(BOARD)/config.h) $(OBJDIR)/config.h + @touch $(OBJDIR)/config.h + +build: warmup $(OBJDIR)/ucboot-$(BOARD).bin + @echo "Build for board $(BOARD) complete" + +clean: + @rm -Rfv $(OBJDIR) +help: + @echo "To build for a board, run 'make BOARD=boardname build'" + @echo "To build for all avaliable boards run make" + @echo "Use CROSS_COMPILE to set toolchain prefix" + @echo "USE V=y for verbose build" + @echo "Have fun" + +.PRECIOUS: $(objects) $(TARGET).bin $(TARGET).elf +.PHONY: warmup \ No newline at end of file diff --git a/Makefile.old b/Makefile.old new file mode 100644 index 0000000..7cd4f50 --- /dev/null +++ b/Makefile.old @@ -0,0 +1,248 @@ +# Makefile skeleton adapted from Peter Harrison's - www.micromouse.com + +# MCU name and submodel +MCU = cortex-m3 +SUBMDL = stm32f103 + +# toolchain (using code sourcery now) +TCHAIN = arm-none-eabi +THUMB = -mthumb +THUMB_IW = -mthumb-interwork + +# Target file name (without extension). +BUILDDIR = build +TARGET = $(BUILDDIR)/maple_boot + +ST_LIB = stm32_lib +ST_USB = usb_lib + +# Optimization level [0,1,2,3,s] +OPT = 0 +DEBUG = -g +#DEBUG = dwarf-2 + +INCDIRS = ./$(ST_LIB) ./$(ST_USB) + +CFLAGS = $(DEBUG) +CFLAGS += -O$(OPT) +CFLAGS += -ffunction-sections -fdata-sections +CFLAGS += -Wall -Wimplicit +CFLAGS += -Wcast-align +CFLAGS += -Wpointer-arith -Wswitch +CFLAGS += -Wredundant-decls -Wreturn-type -Wshadow -Wunused +CFLAGS += -Wa,-adhlns=$(BUILDDIR)/$(subst $(suffix $<),.lst,$<) +CFLAGS += $(patsubst %,-I%,$(INCDIRS)) + +# Aeembler Flags +ASFLAGS = -Wa,-adhlns=$(BUILDDIR)/$(<:.s=.lst)#,--g$(DEBUG) + +LDFLAGS = -nostartfiles -Wl,-Map=$(TARGET).map,--cref,--gc-sections +LDFLAGS += -lc -lgcc + +# Set the linker script +LDFLAGS +=-T$(ST_LIB)/c_only_md.ld + +# Define programs and commands. +SHELL = sh +CC = $(TCHAIN)-gcc +CPP = $(TCHAIN)-g++ +AR = $(TCHAIN)-ar +OBJCOPY = $(TCHAIN)-objcopy +OBJDUMP = $(TCHAIN)-objdump +SIZE = $(TCHAIN)-size +NM = $(TCHAIN)-nm +REMOVE = rm -f +REMOVEDIR = rm -r +COPY = cp + +# Define Messages +# English +MSG_ERRORS_NONE = Errors: none +MSG_BEGIN = "-------- begin --------" +MSG_ETAGS = Created TAGS File +MSG_END = -------- end -------- +MSG_SIZE_BEFORE = Size before: +MSG_SIZE_AFTER = Size after: +MSG_FLASH = Creating load file for Flash: +MSG_EXTENDED_LISTING = Creating Extended Listing: +MSG_SYMBOL_TABLE = Creating Symbol Table: +MSG_LINKING = Linking: +MSG_COMPILING = Compiling C: +MSG_ASSEMBLING = Assembling: +MSG_CLEANING = Cleaning project: + +# Combine all necessary flags and optional flags. +# Add target processor to flags. +GENDEPFLAGS = -MD -MP -MF .dep/$(@F).d +ALL_CFLAGS = -mcpu=$(MCU) $(THUMB_IW) -I. $(CFLAGS) $(GENDEPFLAGS) +ALL_ASFLAGS = -mcpu=$(MCU) $(THUMB_IW) -I. -x assembler-with-cpp $(ASFLAGS) + +# --------------------------------------------- # +# file management +ASRC = $(ST_LIB)/c_only_startup.s $(ST_LIB)/cortexm3_macro.s + +STM32SRCS = + +_STM32USBSRCS = usb_regs.c \ +usb_int.c \ +usb_init.c \ +usb_core.c \ +usb_mem.c + +STM32USBSRCS = $(patsubst %, $(ST_USB)/%,$(_STM32USBSRCS)) + +SRCS = usb.c usb_callbacks.c usb_descriptor.c main.c hardware.c dfu.c + + +SRC = $(SRCS) $(STM32SRCS) $(STM32USBSRCS) + +# Define all object files. +_COBJ = $(SRC:.c=.o) +_AOBJ = $(ASRC:.s=.o) +COBJ = $(patsubst %, $(BUILDDIR)/%,$(_COBJ)) +AOBJ = $(patsubst %, $(BUILDDIR)/%,$(_AOBJ)) + +# Define all listing files. +_LST = $(ASRC:.s=.lst) +_LST += $(SRC:.c=.lst) +LST = $(patsubst %, $(BUILDDIR)/%,$(_LST)) + +# Display size of file. +HEXSIZE = $(SIZE) --target=binary $(TARGET).hex +ELFSIZE = $(SIZE) -A $(TARGET).elf + +# go! +all: begin gccversion build sizeafter finished end +build: elf bin lss sym + +bin: $(TARGET).bin +elf: $(TARGET).elf +lss: $(TARGET).lss +sym: $(TARGET).sym +dfu: $(TARGET).bin + sudo dfu-util -d 0110:1001 -a 0 -D $(TARGET).bin + +begin: + mkdir -p build/stm32_lib + mkdir -p build/usb_lib + @echo -- + @echo $(MSG_BEGIN) + @echo $(COBJ) + +finished: + @echo $(MSG_ERRORS_NONE) +tags: + etags `find . -name "*.c" -o -name "*.cpp" -o -name "*.h"` + @echo $(MSG_ETAGS) +end: + @echo $(MSG_END) + @echo +sizeafter: + @if [ -f $(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi +gccversion: + @$(CC) --version + +program: + @echo "Flash-programming with OpenOCD" + cp $(TARGET).bin flash/tmpflash.bin + cd flash && openocd -f flash.cfg + +program_serial: + @echo "Flash-programming with stm32loader.py" + ./flash/stm32loader.py -p /dev/ttyUSB0 -evw build/maple_boot.bin + +debug: $(TARGET).bin + @echo "Flash-programming with OpenOCD - DEBUG" + cp $(TARGET).bin flash/tmpflash.bin + cd flash && openocd -f debug.cfg + +install: $(TARGET).bin + cp $(TARGET).bin build/main.bin + openocd -f flash/perry_flash.cfg + +run: $(TARGET).bin + openocd -f flash/run.cfg + +# Create final output file (.hex) from ELF output file. +%.hex: %.elf + @echo + @echo $(MSG_FLASH) $@ + $(OBJCOPY) -O binary $< $@ + +# Create final output file (.bin) from ELF output file. +%.bin: %.elf + @echo + @echo $(MSG_FLASH) $@ + $(OBJCOPY) -O binary $< $@ + + +# Create extended listing file from ELF output file. +# testing: option -C +%.lss: %.elf + @echo + @echo $(MSG_EXTENDED_LISTING) $@ + $(OBJDUMP) -h -S -D $< > $@ + + +# Create a symbol table from ELF output file. +%.sym: %.elf + @echo + @echo $(MSG_SYMBOL_TABLE) $@ + $(NM) -n $< > $@ + + +# Link: create ELF output file from object files. +.SECONDARY : $(TARGET).elf +.PRECIOUS : $(COBJ) $(AOBJ) + +%.elf: $(COBJ) $(AOBJ) + @echo + @echo $(MSG_LINKING) $@ + $(CC) $(THUMB) $(ALL_CFLAGS) $(AOBJ) $(COBJ) --output $@ $(LDFLAGS) + +# Compile: create object files from C source files. ARM/Thumb +$(COBJ) : $(BUILDDIR)/%.o : %.c + @echo + @echo $(MSG_COMPILING) $< + $(CC) -c $(THUMB) $(ALL_CFLAGS) $< -o $@ + +# Assemble: create object files from assembler source files. ARM/Thumb +$(AOBJ) : $(BUILDDIR)/%.o : %.s + @echo + @echo $(MSG_ASSEMBLING) $< + $(CC) -c $(THUMB) $(ALL_ASFLAGS) $< -o $@ + +clean: begin clean_list finished end + +clean_list : + @echo + @echo $(MSG_CLEANING) + $(REMOVE) $(TARGET).hex + $(REMOVE) $(TARGET).bin + $(REMOVE) $(TARGET).obj + $(REMOVE) $(TARGET).elf + $(REMOVE) $(TARGET).map + $(REMOVE) $(TARGET).obj + $(REMOVE) $(TARGET).a90 + $(REMOVE) $(TARGET).sym + $(REMOVE) $(TARGET).lnk + $(REMOVE) $(TARGET).lss + $(REMOVE) $(COBJ) + $(REMOVE) $(AOBJ) + $(REMOVE) $(LST) + $(REMOVE) flash/tmpflash.bin +# $(REMOVE) $(SRC:.c=.s) +# $(REMOVE) $(SRC:.c=.d) + $(REMOVE) .dep/* + +# Include the dependency files. +-include $(shell mkdir .dep 2>/dev/null) $(wildcard .dep/*) + + +# Listing of phony targets. +.PHONY : all begin finish tags end sizeafter gccversion \ +build elf hex bin lss sym clean clean_list program cscope + +cscope: + rm -rf *.cscope + find . -iname "*.[hcs]" | grep -v examples | xargs cscope -R -b diff --git a/README b/README.old similarity index 88% rename from README rename to README.old index bb6d719..5ec0a5c 100644 --- a/README +++ b/README.old @@ -4,23 +4,23 @@ FILES ------------------------------------------------------------------------- stm32lib/* - all the (possibly consolidated) stm32 lib and usb example code -usb.c - - USB-specific hardware setup. Interrupts, clocks, etc. handling USB when - not "Attached". some low-level callbacks (low power mode, init, reset, +usb.c + - USB-specific hardware setup. Interrupts, clocks, etc. handling USB when + not "Attached". some low-level callbacks (low power mode, init, reset, resume, etc). -usb_callbacks.c - - aka endpoints: handling data transfer when "Configured". calls out to +usb_callbacks.c + - aka endpoints: handling data transfer when "Configured". calls out to application specific callbacks (eg DFU or serial shit) -usb_descriptor.c +usb_descriptor.c - aka application descriptor; big static struct and callbacks for sending - the descriptor. + the descriptor. main.c - - main loop and calling any hardware init stuff. timing hacks for EEPROM + - main loop and calling any hardware init stuff. timing hacks for EEPROM writes not to block usb interrupts. logic to handle 2 second timeout then - jump to user code. + jump to user code. hardware.c - init routines to setup clocks, interrupts, also destructor functions. @@ -32,7 +32,7 @@ dfu.c TODO -------------------------------------------------------------------------- - * tap reset then quickly tap The Button leaves the board in dfu wait loop + * tap reset then quickly tap The Button leaves the board in dfu wait loop forever instead of just 2 seconds * use sizeof() for usb application descriptor diff --git a/boards/maple/Makefile b/boards/maple/Makefile new file mode 100644 index 0000000..88de6c6 --- /dev/null +++ b/boards/maple/Makefile @@ -0,0 +1 @@ +objects-y+=board.o diff --git a/boards/maple/board.c b/boards/maple/board.c new file mode 100644 index 0000000..4e77ad2 --- /dev/null +++ b/boards/maple/board.c @@ -0,0 +1,74 @@ +#include "stm32f10x_type.h" +#include "cortexm3_macro.h" +#include "common.h" +#include "hardware.h" + + +static void setupLED (void) { + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup APB2 (GPIOA) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000004; + SET_REG(RCC_APB2ENR,rwmVal); + + /* Setup GPIOA Pin 5 as PP Out */ + SET_REG(GPIO_CRL(GPIOA), 0x00100000); + + rwmVal = GET_REG(GPIO_CRL(GPIOA)); + rwmVal &= 0xFF0FFFFF; + rwmVal |= 0x00100000; + SET_REG(GPIO_CRL(GPIOA),rwmVal); + + setPin(GPIOA,5); +} + +static void setupCLK (void) { + /* enable HSE */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); + while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ + + /* enable flash prefetch buffer */ + SET_REG(FLASH_ACR, 0x00000012); + + /* Configure PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ + while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ + + /* Set SYSCLK as PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); + while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ +} + +static void setupBUTTON (void) { + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup APB2 (GPIOC) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000010; + SET_REG(RCC_APB2ENR,rwmVal); + + /* Setup GPIOC Pin 9 as PP Out */ + rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal &= 0xFFFFFF0F; + rwmVal |= 0x00000040; + SET_REG(GPIO_CRH(GPIOC),rwmVal); + +} + +/* executed before actual jump to user code */ +void boardTeardown() +{ + setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin +} + +/* This is a common routine to setup the board */ +void boardInit() +{ + setupCLK(); + setupLED(); + setupBUTTON(); +} diff --git a/boards/maple/config.h b/boards/maple/config.h new file mode 100644 index 0000000..84fb47a --- /dev/null +++ b/boards/maple/config.h @@ -0,0 +1,68 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ****************************************************************************/ + +/** + * @file config.h + * + * @brief bootloader settings and macro defines + * + * + */ + +#ifndef __CONFIG_H +#define __CONFIG_H + +//WTF WAS THAT DOING HERE?? +//#include "common.h" + +#define INHIBIT_LEDS + +#define LED_BANK GPIOA +#define LED 5 +#define BLINK_FAST 0x50000 +#define BLINK_SLOW 0x100000 + +#define BUTTON_BANK GPIOC + +#define BUTTON 9 + +#define STARTUP_BLINKS 5 +#define BOOTLOADER_WAIT 6 + +#define USER_CODE_RAM ((u32)0x20000C00) +#define USER_CODE_FLASH ((u32)0x08005000) + +#define VEND_ID0 0xAF +#define VEND_ID1 0x1E +#define PROD_ID0 0x03 +#define PROD_ID1 0x00 + +/* while this is '1' we're looping in the bootloader */ + +#define bootloaderCondition (delay_count++ < BOOTLOADER_WAIT) \ + || no_user_jump + +#define bootloaderExitCondition 0 + +#endif diff --git a/boards/te-stm32f103ret6kit/Makefile b/boards/te-stm32f103ret6kit/Makefile new file mode 100644 index 0000000..88de6c6 --- /dev/null +++ b/boards/te-stm32f103ret6kit/Makefile @@ -0,0 +1 @@ +objects-y+=board.o diff --git a/boards/te-stm32f103ret6kit/board.c b/boards/te-stm32f103ret6kit/board.c new file mode 100644 index 0000000..e6d61ee --- /dev/null +++ b/boards/te-stm32f103ret6kit/board.c @@ -0,0 +1,100 @@ +#include "stm32f10x_type.h" +#include "cortexm3_macro.h" +#include "common.h" +#include "hardware.h" + + +static inline void setupLED (void) { + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup APB2 (GPIOA) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000004; + SET_REG(RCC_APB2ENR,rwmVal); + + /* Setup GPIOA Pin 5 as PP Out */ + SET_REG(GPIO_CRL(GPIOA), 0x00100000); + + rwmVal = GET_REG(GPIO_CRL(GPIOA)); + rwmVal &= 0xFF0FFFFF; + rwmVal |= 0x00100000; + SET_REG(GPIO_CRL(GPIOA),rwmVal); + + setPin(GPIOA,5); +} + +static inline void setupCLK (void) { + /* enable HSE */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); + while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ + + /* enable flash prefetch buffer */ + SET_REG(FLASH_ACR, 0x00000012); + + /* Configure PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ + while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ + + /* Set SYSCLK as PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); + while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ +} + +static inline void setupBUTTON (void) { + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup APB2 (GPIOC) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000010; + SET_REG(RCC_APB2ENR,rwmVal); + + /* Setup GPIOC Pin 9 as PP Out */ + rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal &= 0xFFFFFF0F; + rwmVal |= 0x00000040; + SET_REG(GPIO_CRH(GPIOC),rwmVal); + +} + + +static void inline setupUSB (void) { + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup the USB DISC Pin */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000010; + SET_REG(RCC_APB2ENR,rwmVal); + + // todo, macroize usb_disc pin + /* Setup GPIOC Pin 12 as OD out */ + rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal &= 0xFFF0FFFF; + rwmVal |= 0x00050000; + setPin (GPIOC,12); + SET_REG(GPIO_CRH(GPIOC),rwmVal); + + pRCC->APB1ENR |= 0x00800000; + + /* initialize the usb application */ + resetPin (GPIOC,12); /* present ourselves to the host */ + +} + + +/* executed before actual jump to user code */ +void boardTeardown() +{ + setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin +} + +/* This is a common routine to setup the board */ +void boardInit() +{ + setupCLK(); + setupLED(); + setupBUTTON(); + setupUSB(); +} diff --git a/config.h b/boards/te-stm32f103ret6kit/config.h similarity index 88% rename from config.h rename to boards/te-stm32f103ret6kit/config.h index d2d980c..33f2837 100644 --- a/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -33,7 +33,10 @@ #ifndef __CONFIG_H #define __CONFIG_H -#include "common.h" +//WTF WAS THAT DOING HERE?? +//#include "common.h" + +#define INHIBIT_LEDS #define LED_BANK GPIOA #define LED 5 @@ -54,4 +57,11 @@ #define PROD_ID0 0x03 #define PROD_ID1 0x00 +/* while this is '1' we're looping in the bootloader */ + +#define bootloaderCondition (delay_count++ < BOOTLOADER_WAIT) \ + || no_user_jump + +#define bootloaderExitCondition 0 + #endif diff --git a/src/Makefile b/src/Makefile new file mode 100644 index 0000000..e69de29 diff --git a/src/common/Makefile b/src/common/Makefile new file mode 100644 index 0000000..7dfec83 --- /dev/null +++ b/src/common/Makefile @@ -0,0 +1,7 @@ +CFLAGS+=-I$(SRCDIR)/src/common/ +objects-common += dfu.o \ + hardware.o \ + main.o \ + usb.o \ + usb_callbacks.o \ + usb_descriptor.o diff --git a/common.h b/src/common/common.h similarity index 100% rename from common.h rename to src/common/common.h diff --git a/dfu.c b/src/common/dfu.c similarity index 87% rename from dfu.c rename to src/common/dfu.c index f1b90a5..70a3b1a 100644 --- a/dfu.c +++ b/src/common/dfu.c @@ -27,7 +27,7 @@ * * @brief The principle dfu state machine as well as the data * transfer callbacks accessed by the usb library - * + * * */ @@ -35,14 +35,14 @@ #include "usb.h" /* DFU globals */ -u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ -DFUStatus dfuAppStatus; /* includes state */ -bool userFlash = FALSE; +static u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ +static DFUStatus dfuAppStatus; /* includes state */ +static bool userFlash = FALSE; bool dfuBusy = FALSE; -u8 recvBuffer[wTransferSize]; -u32 userFirmwareLen = 0; -u16 thisBlockLen = 0; +static u8 recvBuffer[wTransferSize]; +static u32 userFirmwareLen = 0; +static u16 thisBlockLen = 0; PLOT code_copy_lock; @@ -50,9 +50,9 @@ PLOT code_copy_lock; /* todo: force dfu globals to be singleton to avoid re-inits? */ void dfuInit(void) { dfuAppStatus.bStatus = OK; - dfuAppStatus.bwPollTimeout0 = 0x00; - dfuAppStatus.bwPollTimeout1 = 0x00; - dfuAppStatus.bwPollTimeout2 = 0x00; + dfuAppStatus.bwPollTimeout0 = 0x00; + dfuAppStatus.bwPollTimeout1 = 0x00; + dfuAppStatus.bwPollTimeout2 = 0x00; dfuAppStatus.bState = dfuIDLE; dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */ userFirmwareLen = 0; @@ -69,7 +69,7 @@ bool dfuUpdateByRequest(void) { dfuBusy = TRUE; u8 startState = dfuAppStatus.bState; - dfuAppStatus.bStatus = OK; + dfuAppStatus.bStatus = OK; /* often leaner to nest if's then embed a switch/case */ if (startState == dfuIDLE) { /* device running inside DFU mode */ @@ -84,7 +84,7 @@ bool dfuUpdateByRequest(void) { if (pInformation->Current_AlternateSetting == 1) { userAppAddr = USER_CODE_FLASH; userFlash = TRUE; - + /* make sure the flash is setup properly, unlock it */ setupFLASH(); flashUnlock(); @@ -95,42 +95,42 @@ bool dfuUpdateByRequest(void) { } } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errNOTDONE; + dfuAppStatus.bStatus = errNOTDONE; } } else if (pInformation->USBbRequest == DFU_UPLOAD) { dfuAppStatus.bState = dfuUPLOAD_IDLE; } else if (pInformation->USBbRequest == DFU_ABORT) { dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */ + dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */ } else if (pInformation->USBbRequest == DFU_GETSTATUS) { dfuAppStatus.bState = dfuIDLE; } else if (pInformation->USBbRequest == DFU_GETSTATE) { dfuAppStatus.bState = dfuIDLE; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } } else if (startState == dfuDNLOAD_SYNC) { /* device received block, waiting for DFU_GETSTATUS request */ - if (pInformation->USBbRequest == DFU_GETSTATUS) { + if (pInformation->USBbRequest == DFU_GETSTATUS) { /* todo, add routine to wait for last block write to finish */ if (userFlash) { if (code_copy_lock==WAIT) { code_copy_lock=BEGINNING; - dfuAppStatus.bwPollTimeout0 = 0xFF; /* is this enough? */ - dfuAppStatus.bwPollTimeout1 = 0x01; /* is this enough? */ + dfuAppStatus.bwPollTimeout0 = 0xFF; /* is this enough? */ + dfuAppStatus.bwPollTimeout1 = 0x01; /* is this enough? */ dfuAppStatus.bState=dfuDNBUSY; } else if (code_copy_lock==BEGINNING) { - dfuAppStatus.bState=dfuDNLOAD_SYNC; + dfuAppStatus.bState=dfuDNLOAD_SYNC; } else if (code_copy_lock==MIDDLE) { dfuAppStatus.bState=dfuDNLOAD_SYNC; } else if (code_copy_lock==END) { - dfuAppStatus.bwPollTimeout0 = 0x00; + dfuAppStatus.bwPollTimeout0 = 0x00; code_copy_lock=WAIT; dfuAppStatus.bState=dfuDNLOAD_IDLE; } @@ -144,7 +144,7 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState = dfuDNLOAD_SYNC; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } } else if (startState == dfuDNBUSY) { @@ -165,10 +165,10 @@ bool dfuUpdateByRequest(void) { } else { /* todo, support "disagreement" if device expects more data than this */ dfuAppStatus.bState = dfuMANIFEST_SYNC; - + /* relock the flash */ flashLock(); - } + } } else if (pInformation->USBbRequest == DFU_ABORT) { dfuAppStatus.bState = dfuIDLE; } else if (pInformation->USBbRequest == DFU_GETSTATUS) { @@ -177,12 +177,12 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState = dfuIDLE; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } } else if (startState == dfuMANIFEST_SYNC) { /* device has received last block, waiting DFU_GETSTATUS request */ - + if (pInformation->USBbRequest == DFU_GETSTATUS) { dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; dfuAppStatus.bStatus = OK; @@ -190,7 +190,7 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState = dfuMANIFEST_SYNC; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } } else if (startState == dfuMANIFEST) { @@ -210,10 +210,10 @@ bool dfuUpdateByRequest(void) { } else if (startState == dfuUPLOAD_IDLE) { /* device expecting further dfu_upload requests */ - if (pInformation->USBbRequest == DFU_UPLOAD) { + if (pInformation->USBbRequest == DFU_UPLOAD) { /* todo, add routine to wait for last block write to finish */ dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } else if (pInformation->USBbRequest == DFU_ABORT) { dfuAppStatus.bState = dfuIDLE; } else if (pInformation->USBbRequest == DFU_GETSTATUS) { @@ -222,14 +222,14 @@ bool dfuUpdateByRequest(void) { dfuAppStatus.bState = dfuUPLOAD_IDLE; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } - + } else if (startState == dfuERROR) { /* status is in error, awaiting DFU_CLRSTATUS request */ - if (pInformation->USBbRequest == DFU_GETSTATUS) { + if (pInformation->USBbRequest == DFU_GETSTATUS) { /* todo, add routine to wait for last block write to finish */ dfuAppStatus.bState = dfuERROR; } else if (pInformation->USBbRequest == DFU_GETSTATE) { @@ -237,16 +237,16 @@ bool dfuUpdateByRequest(void) { } else if (pInformation->USBbRequest == DFU_CLRSTATUS) { /* todo handle any cleanup we need here */ dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; + dfuAppStatus.bStatus = OK; } else { dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } + dfuAppStatus.bStatus = errSTALLEDPKT; + } } else { /* some kind of error... */ dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; + dfuAppStatus.bStatus = errSTALLEDPKT; } if (dfuAppStatus.bStatus == OK) { @@ -271,7 +271,7 @@ void dfuUpdateByReset(void) { /* do nothing...might be normal usb bus activity */ } else { /* we reset from the dfu, reset everything and startover, - which is the correct operation if this is an erroneous + which is the correct operation if this is an erroneous event or properly following a MANIFEST */ dfuAppStatus.bState = dfuIDLE; dfuAppStatus.bStatus = OK; @@ -297,7 +297,7 @@ u8* dfuCopyStatus(u16 length) { pInformation->Ctrl_Info.Usb_wLength = 6; return NULL; } else { - return(&dfuAppStatus); + return( (u8*) &dfuAppStatus); } } @@ -369,4 +369,3 @@ void dfuFinishUpload() { /* otherwise do nothing, dfu state machine resets itself */ } } - diff --git a/dfu.h b/src/common/dfu.h similarity index 98% rename from dfu.h rename to src/common/dfu.h index baf8b87..252a6db 100644 --- a/dfu.h +++ b/src/common/dfu.h @@ -100,8 +100,8 @@ void dfuInit(void); /* singleton dfu initializer */ /* should consume dfuEvent type, but for now we can use pInfo (see comment above) */ bool dfuUpdateByRequest(void); /* returns if new status is OK */ -void dfuUpdateByReset(void); -void dfuUpdateByTimeout(void); +void dfuUpdateByReset(void); +void dfuUpdateByTimeout(void); /* usb callbacks */ u8* dfuCopyState(u16); @@ -112,7 +112,7 @@ u8* dfuCopyUPLOAD(u16); void dfuCopyBufferToExec(void); bool checkTestFile(void); -u8 dfuGetState(void); +u8 dfuGetState(void); void dfuSetState(u8); bool dfuUploadStarted(); void dfuFinishUpload(); diff --git a/hardware.c b/src/common/hardware.c similarity index 79% rename from hardware.c rename to src/common/hardware.c index 56cf06f..0dcb479 100644 --- a/hardware.c +++ b/src/common/hardware.c @@ -64,8 +64,8 @@ void strobePin(u32 bank, u8 pin, u8 count, u32 rate) { asm volatile ("nop"); } resetPin(bank,pin); - } -} + } +} void systemReset(void) { SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00000001); @@ -77,60 +77,7 @@ void systemReset(void) { SET_REG(RCC_CIR, 0x00000000); /* disable all RCC interrupts */ } -void setupCLK (void) { - /* enable HSE */ - SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); - while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ - - /* enable flash prefetch buffer */ - SET_REG(FLASH_ACR, 0x00000012); - - /* Configure PLL */ - SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ - SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ - while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ - - /* Set SYSCLK as PLL */ - SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); - while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ -} - -void setupLED (void) { - // todo, swap out hardcoded pin/bank with macro - u32 rwmVal; /* read-write-modify place holder var */ - - /* Setup APB2 (GPIOA) */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000004; - SET_REG(RCC_APB2ENR,rwmVal); - - /* Setup GPIOA Pin 5 as PP Out */ - SET_REG(GPIO_CRL(GPIOA), 0x00100000); - - rwmVal = GET_REG(GPIO_CRL(GPIOA)); - rwmVal &= 0xFF0FFFFF; - rwmVal |= 0x00100000; - SET_REG(GPIO_CRL(GPIOA),rwmVal); - - setPin(GPIOA,5); -} - -void setupBUTTON (void) { - // todo, swap out hardcoded pin/bank with macro - u32 rwmVal; /* read-write-modify place holder var */ - - /* Setup APB2 (GPIOC) */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; - SET_REG(RCC_APB2ENR,rwmVal); - /* Setup GPIOC Pin 9 as PP Out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFFFFF0F; - rwmVal |= 0x00000040; - SET_REG(GPIO_CRH(GPIOC),rwmVal); - -} void setupFLASH() { /* configure the HSI oscillator */ @@ -157,18 +104,17 @@ bool checkUserCode (u32 usrAddr) { void jumpToUser (u32 usrAddr) { typedef void (*funcPtr)(void); - u32 jumpAddr = *(vu32*) (usrAddr + 0x04); /* reset ptr in vector table */ + u32 jumpAddr = *(vu32*) (usrAddr + 0x04); /* reset ptr in vector table */ funcPtr usrMain = (funcPtr) jumpAddr; /* tear down all the dfu related setup */ // disable usb interrupts, clear them, turn off usb, set the disc pin - // todo pick exactly what we want to do here, now its just a conservative + // todo pick exactly what we want to do here, now its just a conservative flashLock(); usbDsbISR(); nvicDisableInterrupts(); - setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin + boardTeardown(); systemReset(); // resets clocks and periphs, not core regs - __MSR_MSP(*(vu32*) usrAddr); /* set the users stack ptr */ @@ -186,25 +132,25 @@ void nvicInit(NVIC_InitTypeDef* NVIC_InitStruct) { NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE; - /* Compute the Corresponding IRQ Priority --------------------------------*/ + /* Compute the Corresponding IRQ Priority --------------------------------*/ tmppriority = (0x700 - (rSCB->AIRCR & (u32)0x700))>> 0x08; tmppre = (0x4 - tmppriority); tmpsub = tmpsub >> tmppriority; - + tmppriority = (u32)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; tmppriority = tmppriority << 0x04; tmppriority = ((u32)tmppriority) << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); - + tmpreg = rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)]; tmpmask = (u32)0xFF << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); tmpreg &= ~tmpmask; - tmppriority &= tmpmask; + tmppriority &= tmpmask; tmpreg |= tmppriority; rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)] = tmpreg; - + /* Enable the Selected IRQ Channels --------------------------------------*/ rNVIC->ISER[(NVIC_InitStruct->NVIC_IRQChannel >> 0x05)] = (u32)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (u8)0x1F); @@ -222,7 +168,7 @@ void nvicDisableInterrupts() { void systemHardReset(void) { SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE; - + /* Reset */ rSCB->AIRCR = (u32)AIRCR_RESET_REQ; @@ -298,7 +244,3 @@ void flashUnlock() { SET_REG(FLASH_KEYR,FLASH_KEY1); SET_REG(FLASH_KEYR,FLASH_KEY2); } - - - - diff --git a/hardware.h b/src/common/hardware.h similarity index 98% rename from hardware.h rename to src/common/hardware.h index ee3eebd..d29f9b0 100644 --- a/hardware.h +++ b/src/common/hardware.h @@ -165,6 +165,9 @@ typedef struct { } SCB_TypeDef; +void boardInit(); +void boardTeardown(); + void setPin (u32 bank, u8 pin); void resetPin (u32 bank, u8 pin); bool readPin (u32 bank, u8 pin); @@ -172,10 +175,7 @@ void strobePin (u32 bank, u8 pin, u8 count, u32 rate); void systemHardReset(void); void systemReset (void); -void setupCLK (void); -void setupLED (void); void setupFLASH (void); -void setupBUTTON (void); bool checkUserCode (u32 usrAddr); void jumpToUser (u32 usrAddr); diff --git a/main.c b/src/common/main.c similarity index 93% rename from main.c rename to src/common/main.c index c97cb7c..fe73b9c 100644 --- a/main.c +++ b/src/common/main.c @@ -25,9 +25,9 @@ /** * @file main.c * - * @brief main loop and calling any hardware init stuff. timing hacks for EEPROM + * @brief main loop and calling any hardware init stuff. timing hacks for EEPROM * writes not to block usb interrupts. logic to handle 2 second timeout then - * jump to user code. + * jump to user code. * */ @@ -35,10 +35,8 @@ int main() { systemReset(); // peripherals but not PC - setupCLK(); - setupLED(); + boardInit(); setupUSB(); - setupBUTTON(); setupFLASH(); strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); @@ -47,16 +45,15 @@ int main() { bool no_user_jump = !checkUserCode(USER_CODE_FLASH) && !checkUserCode(USER_CODE_RAM) || readPin(BUTTON_BANK,BUTTON); int delay_count = 0; - while ((delay_count++ < BOOTLOADER_WAIT) - || no_user_jump) { - + while (bootloaderCondition) { strobePin(LED_BANK,LED,1,BLINK_SLOW); - if (dfuUploadStarted()) { dfuFinishUpload(); // systemHardReset from DFU once done } + if (bootloaderExitCondition) + break; } - + if (checkUserCode(USER_CODE_RAM)) { jumpToUser(USER_CODE_RAM); } else if (checkUserCode(USER_CODE_FLASH)) { @@ -66,5 +63,4 @@ int main() { strobePin(LED_BANK,LED,5,BLINK_FAST); systemHardReset(); } - } diff --git a/usb.c b/src/common/usb.c similarity index 97% rename from usb.c rename to src/common/usb.c index aa999a6..883b436 100644 --- a/usb.c +++ b/src/common/usb.c @@ -26,7 +26,7 @@ * @file usb.c * * @brief usb-specific hardware setup, NVIC, clocks, and usb activities - * in the pre-attached state. includes some of the lower level callbacks + * in the pre-attached state. includes some of the lower level callbacks * needed by the usb library, like suspend,resume,init,etc */ @@ -63,13 +63,13 @@ vu32 bDeviceState = UNCONNECTED; vu16 wIstr; vu8 bIntPackSOF = 0; -DEVICE Device_Table = +DEVICE Device_Table = { NUM_ENDPTS, 1 }; -DEVICE_PROP Device_Property = +DEVICE_PROP Device_Property = { usbInit, usbReset, @@ -86,7 +86,7 @@ DEVICE_PROP Device_Property = bMaxPacketSize }; -USER_STANDARD_REQUESTS User_Standard_Requests = +USER_STANDARD_REQUESTS User_Standard_Requests = { usbGetConfiguration, usbSetConfiguration, @@ -100,7 +100,7 @@ USER_STANDARD_REQUESTS User_Standard_Requests = }; void (*pEpInt_IN[7])(void) = -{ +{ nothingProc, nothingProc, nothingProc, @@ -232,7 +232,7 @@ RESULT usbPowerOff(void) { _SetISTR(0); _SetCNTR(CNTR_FRES + CNTR_PDWN); - /* note that all weve done here is powerdown the + /* note that all weve done here is powerdown the usb peripheral. we have no disabled the clocks, pulled the usb_disc pin back up, or reset the application state machines */ @@ -367,9 +367,9 @@ u8* usbGetFunctionalDescriptor(u16 len) { /***** start of USER STANDARD REQUESTS ****** - * - * These are the USER STANDARD REQUESTS, they are handled - * in the core but we are given these callbacks at the + * + * These are the USER STANDARD REQUESTS, they are handled + * in the core but we are given these callbacks at the * application level *******************************************/ diff --git a/usb.h b/src/common/usb.h similarity index 94% rename from usb.h rename to src/common/usb.h index 5c6a4e1..e1563d6 100644 --- a/usb.h +++ b/src/common/usb.h @@ -42,7 +42,7 @@ #define NUM_ENDPTS 0x01 /* do we gracefully implement usb suspend? */ -#define F_SUSPEND_ENABLED 1 +#define F_SUSPEND_ENABLED 0 /* defines which interrupts are handled */ #define ISR_MSK (CNTR_CTRM | \ @@ -76,7 +76,7 @@ typedef enum _DEVICE_STATE CONFIGURED } DEVICE_STATE; -void setupUSB(void); + void usbAppInit(void); /* singleton usb initializer */ void usbSuspend(void); @@ -111,7 +111,7 @@ void usbSetEndpointFeature(void); void usbSetDeviceFeature(void); void usbSetDeviceAddress(void); -/* the small number of comm emulator functions to +/* the small number of comm emulator functions to eventually be migrated into their own usart sources */ u8* vcomGetLineCoding(u16 length); @@ -119,8 +119,8 @@ u8* vcomSetLineCoding(u16 length); void vcomEp1In(void); void vcomEp3Out(void); -/* Interrupt setup/handling exposed only so that - its obvious from main what interrupts are overloaded +/* Interrupt setup/handling exposed only so that + its obvious from main what interrupts are overloaded from c_only_startup.s (see the top of main.c) */ void usbDsbISR(void); void usbEnbISR(void); diff --git a/usb_callbacks.c b/src/common/usb_callbacks.c similarity index 98% rename from usb_callbacks.c rename to src/common/usb_callbacks.c index 09b0ae4..6881efe 100644 --- a/usb_callbacks.c +++ b/src/common/usb_callbacks.c @@ -25,7 +25,7 @@ /** * @file usb_callbacks.c * - * @brief aka endpoints: handling data transfer when "Configured". calls out to + * @brief aka endpoints: handling data transfer when "Configured". calls out to * application specific callbacks (eg DFU) * */ diff --git a/usb_descriptor.c b/src/common/usb_descriptor.c similarity index 93% rename from usb_descriptor.c rename to src/common/usb_descriptor.c index 34140d2..f83b03f 100644 --- a/usb_descriptor.c +++ b/src/common/usb_descriptor.c @@ -27,14 +27,14 @@ * @file usb_descriptor.c * * @brief aka application descriptor; big static struct and callbacks for sending - * the descriptor. + * the descriptor. * */ #include "usb_descriptor.h" -u8 u8_usbDeviceDescriptorDFU[18] = +u8 u8_usbDeviceDescriptorDFU[18] = { 0x12, /* bLength */ 0x01, /* bDescriptorType */ @@ -58,13 +58,13 @@ u8 u8_usbDeviceDescriptorDFU[18] = 0x01 /*bNumConfigurations */ }; -ONE_DESCRIPTOR usbDeviceDescriptorDFU = +ONE_DESCRIPTOR usbDeviceDescriptorDFU = { u8_usbDeviceDescriptorDFU, 0x12 }; -u8 u8_usbFunctionalDescriptor[9] = +u8 u8_usbFunctionalDescriptor[9] = { /******************** DFU Functional Descriptor********************/ 0x09, /*blength = 7 Bytes*/ @@ -78,14 +78,14 @@ u8 u8_usbFunctionalDescriptor[9] = 0x01 }; -ONE_DESCRIPTOR usbFunctionalDescriptor = +ONE_DESCRIPTOR usbFunctionalDescriptor = { u8_usbFunctionalDescriptor, 0x09 }; -u8 u8_usbConfigDescriptorDFU[36] = +u8 u8_usbConfigDescriptorDFU[36] = { 0x09, /* bLength: Configuation Descriptor size */ 0x02, /* bDescriptorType: Configuration */ @@ -138,7 +138,7 @@ u8 u8_usbConfigDescriptorDFU[36] = /*36*/ }; -ONE_DESCRIPTOR usbConfigDescriptorDFU = +ONE_DESCRIPTOR usbConfigDescriptorDFU = { u8_usbConfigDescriptorDFU, 0x24 @@ -152,28 +152,28 @@ u8 u8_usbStringLangId[0x04] = 0x04 /* LangID = 0x0409: U.S. English */ }; -u8 u8_usbStringVendor[0x12] = +u8 u8_usbStringVendor[0x12] = { 0x12, 0x03, 'L',0,'e',0,'a',0,'f',0,'L',0,'a',0,'b',0,'s',0 }; -u8 u8_usbStringProduct[0x14] = +u8 u8_usbStringProduct[0x14] = { 0x14, 0x03, 'M',0,'a',0,'p',0,'l',0,'e',0,' ',0,'0',0,'0',0,'3',0 }; -u8 u8_usbStringSerial[0x10] = +u8 u8_usbStringSerial[0x10] = { 0x10, 0x03, 'L',0,'L',0,'M',0,' ',0,'0',0,'0',0,'3',0 }; -u8 u8_usbStringAlt0[0x36] = +u8 u8_usbStringAlt0[0x36] = { 0x36, 0x03, @@ -182,7 +182,7 @@ u8 u8_usbStringAlt0[0x36] = '2',0,'0',0,'0',0,'0',0,'0',0,'C',0,'0',0,'0',0 }; -u8 u8_usbStringAlt1[0x3A] = +u8 u8_usbStringAlt1[0x3A] = { 0x3A, 0x03, @@ -203,4 +203,3 @@ ONE_DESCRIPTOR usbStringDescriptor[STR_DESC_LEN] = { (u8*)u8_usbStringAlt0, 0x36 }, { (u8*)u8_usbStringAlt1, 0x3A } }; - diff --git a/usb_descriptor.h b/src/common/usb_descriptor.h similarity index 100% rename from usb_descriptor.h rename to src/common/usb_descriptor.h diff --git a/src/startup/Makefile b/src/startup/Makefile new file mode 100644 index 0000000..c5cb1ea --- /dev/null +++ b/src/startup/Makefile @@ -0,0 +1,2 @@ +CFLAGS+=-I$(SRCDIR)/src/startup/ +objects-startup+=c_only_startup.o cortexm3_macro.o diff --git a/stm32_lib/c_only_md.ld b/src/startup/c_only_md.ld similarity index 88% rename from stm32_lib/c_only_md.ld rename to src/startup/c_only_md.ld index 4e10c37..09d57ce 100644 --- a/stm32_lib/c_only_md.ld +++ b/src/startup/c_only_md.ld @@ -1,172 +1,172 @@ -/* -Default linker script for STM32F10x_128K_20K -Original Copyright RAISONANCE S.A.S. 2008 -Modified P Harrison May 2009 -*/ - -/* - * Default stack sizes. - * - * These are used by the startup in order to allocate stacks for the different modes. - * PROVIDE" allows to easily override these values from an object file or the commmand line. - */ - -__Stack_Size = 1024 ; -PROVIDE ( _Stack_Size = __Stack_Size ) ; -__Stack_Init = _estack - __Stack_Size ; -PROVIDE ( _Stack_Init = __Stack_Init ) ; - -/* - *There will be a link error if there is not this amount of RAM free at the end. - */ -_Minimum_Stack_Size = 0x100 ; - - -MEMORY -{ - RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K -} - -/* higher address of the user mode stack */ -_estack = 0x20005000; - -SECTIONS -{ -/* - * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, - * which goes to FLASH - */ - .isr_vector : - { - . = ALIGN(4); - KEEP(*(.isr_vector)) /* Startup code */ - . = ALIGN(4); - } >FLASH - -/* - * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, - * which goes to FLASH - */ - .flashtext : - { - . = ALIGN(4); - KEEP (*(.flashtext)) /* Startup code */ - . = ALIGN(4); - } >FLASH - - /* - * the program code is stored in the .text section, which goes to Flash - */ - .text : - { - . = ALIGN(4); - *(.text) /* remaining code */ - *(.text.*) /* remaining code */ - *(.rodata) /* read-only data (constants) */ - *(.rodata*) - *(.glue_7) - *(.glue_7t) - . = ALIGN(4); - _etext = .; - _sidata = _etext; - } >FLASH - -/* - * This is the initialized data section. It is stored in RAM but the initial values - * are held in flash and copied to RAM by the startup code - */ - -/* we copy the important program globals vector in RAM as well, so that users can fool with it */ - .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ - { - . = ALIGN(4); - _sdata = . ; /* Used by the startup in order to initialize the .data section */ - KEEP( *(.data) ) - KEEP( *(.data.*) ) - . = ALIGN(4); - _edata = . ; /* Used by the startup in order to initialize the .data section */ - } >RAM - - - -/* - * This is the uninitialized data section. Date here is stored in RAM and will be - * set to zero by the startup code. - */ - .bss : - { - . = ALIGN(4); - _sbss = .; /* Used by the startup in order to initialize the .bss section */ - *(.bss) - *(COMMON) - . = ALIGN(4); - _ebss = . ; /* Used by the startup in order to initialize the .bss section */ - } >RAM - -PROVIDE ( end = _ebss ); -PROVIDE ( _end = _ebss ); - -/* - * This is the user stack section - * This is just to check that there is enough RAM left for the User mode stack - * It should generate an error if it's full. - */ - ._usrstack : - { - . = ALIGN(4); - _susrstack = . ; - . = . + _Minimum_Stack_Size ; - . = ALIGN(4); - _eusrstack = . ; - } >RAM - -/* - * after that it's only debugging information. - */ - -/* remove the debugging information from the standard libraries */ -DISCARD : - { - libc.a ( * ) - libm.a ( * ) - libgcc.a ( * ) - } - -/* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } -/* - * DWARF debug sections. - * Symbols in the DWARF debugging sections are relative to the beginning - * of the section so we begin them at 0. - */ - -/* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } -/* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } -/* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } -/* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } -/* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } -} +/* +Default linker script for STM32F10x_128K_20K +Original Copyright RAISONANCE S.A.S. 2008 +Modified P Harrison May 2009 +*/ + +/* + * Default stack sizes. + * + * These are used by the startup in order to allocate stacks for the different modes. + * PROVIDE" allows to easily override these values from an object file or the commmand line. + */ + +__Stack_Size = 1024 ; +PROVIDE ( _Stack_Size = __Stack_Size ) ; +__Stack_Init = _estack - __Stack_Size ; +PROVIDE ( _Stack_Init = __Stack_Init ) ; + +/* + *There will be a link error if there is not this amount of RAM free at the end. + */ +_Minimum_Stack_Size = 0x100 ; + + +MEMORY +{ + RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K +} + +/* higher address of the user mode stack */ +_estack = 0x20005000; + +SECTIONS +{ +/* + * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, + * which goes to FLASH + */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } >FLASH + +/* + * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, + * which goes to FLASH + */ + .flashtext : + { + . = ALIGN(4); + KEEP (*(.flashtext)) /* Startup code */ + . = ALIGN(4); + } >FLASH + + /* + * the program code is stored in the .text section, which goes to Flash + */ + .text : + { + . = ALIGN(4); + *(.text) /* remaining code */ + *(.text.*) /* remaining code */ + *(.rodata) /* read-only data (constants) */ + *(.rodata*) + *(.glue_7) + *(.glue_7t) + . = ALIGN(4); + _etext = .; + _sidata = _etext; + } >FLASH + +/* + * This is the initialized data section. It is stored in RAM but the initial values + * are held in flash and copied to RAM by the startup code + */ + +/* we copy the important program globals vector in RAM as well, so that users can fool with it */ + .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ + { + . = ALIGN(4); + _sdata = . ; /* Used by the startup in order to initialize the .data section */ + KEEP( *(.data) ) + KEEP( *(.data.*) ) + . = ALIGN(4); + _edata = . ; /* Used by the startup in order to initialize the .data section */ + } >RAM + + + +/* + * This is the uninitialized data section. Date here is stored in RAM and will be + * set to zero by the startup code. + */ + .bss : + { + . = ALIGN(4); + _sbss = .; /* Used by the startup in order to initialize the .bss section */ + *(.bss) + *(COMMON) + . = ALIGN(4); + _ebss = . ; /* Used by the startup in order to initialize the .bss section */ + } >RAM + +PROVIDE ( end = _ebss ); +PROVIDE ( _end = _ebss ); + +/* + * This is the user stack section + * This is just to check that there is enough RAM left for the User mode stack + * It should generate an error if it's full. + */ + ._usrstack : + { + . = ALIGN(4); + _susrstack = . ; + . = . + _Minimum_Stack_Size ; + . = ALIGN(4); + _eusrstack = . ; + } >RAM + +/* + * after that it's only debugging information. + */ + +/* remove the debugging information from the standard libraries */ +DISCARD : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + +/* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } +/* + * DWARF debug sections. + * Symbols in the DWARF debugging sections are relative to the beginning + * of the section so we begin them at 0. + */ + +/* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } +/* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } +/* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } +/* DWARF 2 */ + .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } +/* SGI/MIPS DWARF 2 extensions */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } +} diff --git a/stm32_lib/c_only_md_RAM.ld b/src/startup/c_only_md_RAM.ld similarity index 87% rename from stm32_lib/c_only_md_RAM.ld rename to src/startup/c_only_md_RAM.ld index 2f47891..f85ee43 100644 --- a/stm32_lib/c_only_md_RAM.ld +++ b/src/startup/c_only_md_RAM.ld @@ -1,169 +1,169 @@ -/* -Default linker script for STM32F10x_128K_20K -Original Copyright RAISONANCE S.A.S. 2008 -Modified P Harrison May 2009 -*/ - -/* - * Default stack sizes. - * - * These are used by the startup in order to allocate stacks for the different modes. - * PROVIDE" allows to easily override these values from an object file or the commmand line. - */ - -__Stack_Size = 1024 ; -PROVIDE ( _Stack_Size = __Stack_Size ) ; -__Stack_Init = _estack - __Stack_Size ; -PROVIDE ( _Stack_Init = __Stack_Init ) ; - -/* - *There will be a link error if there is not this amount of RAM free at the end. - */ -_Minimum_Stack_Size = 0x100 ; - - -MEMORY -{ - RAM (xrw) : ORIGIN = 0x20000C00, LENGTH = 17K -} - -/* higher address of the user mode stack */ -_estack = 0x20005000; -_magicRate = 0x5000; -SECTIONS -{ -/* - * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, - * which goes to FLASH - */ - .isr_vector : - { - . = ALIGN(4); - KEEP(*(.isr_vector)) /* Startup code */ - . = ALIGN(4); - } >RAM - -/* - * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, - * which goes to FLASH - */ - .flashtext : - { - . = ALIGN(4); - KEEP (*(.flashtext)) /* Startup code */ - . = ALIGN(4); - } >RAM - - /* - * the program code is stored in the .text section, which goes to Flash - */ - .text : - { - . = ALIGN(4); - *(.text) /* remaining code */ - *(.text.*) /* remaining code */ - *(.rodata) /* read-only data (constants) */ - *(.rodata*) - *(.glue_7) - *(.glue_7t) - . = ALIGN(4); - _etext = .; - _sidata = _etext; /* Uused by the startup in order to initialize the .data secion */ - } >RAM - -/* - * This is the initialized data section. It is stored in RAM but the initial values - * are held in flash and copied to RAM by the startup code - */ - .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ - { - . = ALIGN(4); - _sdata = . ; /* Used by the startup in order to initialize the .data section */ - KEEP( *(.data) ) - KEEP( *(.data.*) ) - . = ALIGN(4); - _edata = . ; /* Used by the startup in order to initialize the .data section */ - } >RAM - - - -/* - * This is the uninitialized data section. Date here is stored in RAM and will be - * set to zero by the startup code. - */ - .bss : - { - . = ALIGN(4); - _sbss = .; /* Used by the startup in order to initialize the .bss section */ - *(.bss) - *(COMMON) - . = ALIGN(4); - _ebss = . ; /* Used by the startup in order to initialize the .bss section */ - } >RAM - -PROVIDE ( end = _ebss ); -PROVIDE ( _end = _ebss ); - -/* - * This is the user stack section - * This is just to check that there is enough RAM left for the User mode stack - * It should generate an error if it's full. - */ - ._usrstack : - { - . = ALIGN(4); - _susrstack = . ; - . = . + _Minimum_Stack_Size ; - . = ALIGN(4); - _eusrstack = . ; - } >RAM - -/* - * after that it's only debugging information. - */ - -/* remove the debugging information from the standard libraries */ -DISCARD : - { - libc.a ( * ) - libm.a ( * ) - libgcc.a ( * ) - } - -/* Stabs debugging sections. */ - .stab 0 : { *(.stab) } - .stabstr 0 : { *(.stabstr) } - .stab.excl 0 : { *(.stab.excl) } - .stab.exclstr 0 : { *(.stab.exclstr) } - .stab.index 0 : { *(.stab.index) } - .stab.indexstr 0 : { *(.stab.indexstr) } - .comment 0 : { *(.comment) } -/* - * DWARF debug sections. - * Symbols in the DWARF debugging sections are relative to the beginning - * of the section so we begin them at 0. - */ - -/* DWARF 1 */ - .debug 0 : { *(.debug) } - .line 0 : { *(.line) } -/* GNU DWARF 1 extensions */ - .debug_srcinfo 0 : { *(.debug_srcinfo) } - .debug_sfnames 0 : { *(.debug_sfnames) } -/* DWARF 1.1 and DWARF 2 */ - .debug_aranges 0 : { *(.debug_aranges) } - .debug_pubnames 0 : { *(.debug_pubnames) } -/* DWARF 2 */ - .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } - .debug_abbrev 0 : { *(.debug_abbrev) } - .debug_line 0 : { *(.debug_line) } - .debug_frame 0 : { *(.debug_frame) } - .debug_str 0 : { *(.debug_str) } - .debug_loc 0 : { *(.debug_loc) } - .debug_macinfo 0 : { *(.debug_macinfo) } -/* SGI/MIPS DWARF 2 extensions */ - .debug_weaknames 0 : { *(.debug_weaknames) } - .debug_funcnames 0 : { *(.debug_funcnames) } - .debug_typenames 0 : { *(.debug_typenames) } - .debug_varnames 0 : { *(.debug_varnames) } -} +/* +Default linker script for STM32F10x_128K_20K +Original Copyright RAISONANCE S.A.S. 2008 +Modified P Harrison May 2009 +*/ + +/* + * Default stack sizes. + * + * These are used by the startup in order to allocate stacks for the different modes. + * PROVIDE" allows to easily override these values from an object file or the commmand line. + */ + +__Stack_Size = 1024 ; +PROVIDE ( _Stack_Size = __Stack_Size ) ; +__Stack_Init = _estack - __Stack_Size ; +PROVIDE ( _Stack_Init = __Stack_Init ) ; + +/* + *There will be a link error if there is not this amount of RAM free at the end. + */ +_Minimum_Stack_Size = 0x100 ; + + +MEMORY +{ + RAM (xrw) : ORIGIN = 0x20000C00, LENGTH = 17K +} + +/* higher address of the user mode stack */ +_estack = 0x20005000; +_magicRate = 0x5000; +SECTIONS +{ +/* + * for Cortex devices, the beginning of the startup code is stored in the .isr_vector section, + * which goes to FLASH + */ + .isr_vector : + { + . = ALIGN(4); + KEEP(*(.isr_vector)) /* Startup code */ + . = ALIGN(4); + } >RAM + +/* + * for some STRx devices, the beginning of the startup code is stored in the .flashtext section, + * which goes to FLASH + */ + .flashtext : + { + . = ALIGN(4); + KEEP (*(.flashtext)) /* Startup code */ + . = ALIGN(4); + } >RAM + + /* + * the program code is stored in the .text section, which goes to Flash + */ + .text : + { + . = ALIGN(4); + *(.text) /* remaining code */ + *(.text.*) /* remaining code */ + *(.rodata) /* read-only data (constants) */ + *(.rodata*) + *(.glue_7) + *(.glue_7t) + . = ALIGN(4); + _etext = .; + _sidata = _etext; /* Uused by the startup in order to initialize the .data secion */ + } >RAM + +/* + * This is the initialized data section. It is stored in RAM but the initial values + * are held in flash and copied to RAM by the startup code + */ + .data : AT ( _sidata ) /* AT makes the LMA follow on in the binary image */ + { + . = ALIGN(4); + _sdata = . ; /* Used by the startup in order to initialize the .data section */ + KEEP( *(.data) ) + KEEP( *(.data.*) ) + . = ALIGN(4); + _edata = . ; /* Used by the startup in order to initialize the .data section */ + } >RAM + + + +/* + * This is the uninitialized data section. Date here is stored in RAM and will be + * set to zero by the startup code. + */ + .bss : + { + . = ALIGN(4); + _sbss = .; /* Used by the startup in order to initialize the .bss section */ + *(.bss) + *(COMMON) + . = ALIGN(4); + _ebss = . ; /* Used by the startup in order to initialize the .bss section */ + } >RAM + +PROVIDE ( end = _ebss ); +PROVIDE ( _end = _ebss ); + +/* + * This is the user stack section + * This is just to check that there is enough RAM left for the User mode stack + * It should generate an error if it's full. + */ + ._usrstack : + { + . = ALIGN(4); + _susrstack = . ; + . = . + _Minimum_Stack_Size ; + . = ALIGN(4); + _eusrstack = . ; + } >RAM + +/* + * after that it's only debugging information. + */ + +/* remove the debugging information from the standard libraries */ +DISCARD : + { + libc.a ( * ) + libm.a ( * ) + libgcc.a ( * ) + } + +/* Stabs debugging sections. */ + .stab 0 : { *(.stab) } + .stabstr 0 : { *(.stabstr) } + .stab.excl 0 : { *(.stab.excl) } + .stab.exclstr 0 : { *(.stab.exclstr) } + .stab.index 0 : { *(.stab.index) } + .stab.indexstr 0 : { *(.stab.indexstr) } + .comment 0 : { *(.comment) } +/* + * DWARF debug sections. + * Symbols in the DWARF debugging sections are relative to the beginning + * of the section so we begin them at 0. + */ + +/* DWARF 1 */ + .debug 0 : { *(.debug) } + .line 0 : { *(.line) } +/* GNU DWARF 1 extensions */ + .debug_srcinfo 0 : { *(.debug_srcinfo) } + .debug_sfnames 0 : { *(.debug_sfnames) } +/* DWARF 1.1 and DWARF 2 */ + .debug_aranges 0 : { *(.debug_aranges) } + .debug_pubnames 0 : { *(.debug_pubnames) } +/* DWARF 2 */ + .debug_info 0 : { *(.debug_info .gnu.linkonce.wi.*) } + .debug_abbrev 0 : { *(.debug_abbrev) } + .debug_line 0 : { *(.debug_line) } + .debug_frame 0 : { *(.debug_frame) } + .debug_str 0 : { *(.debug_str) } + .debug_loc 0 : { *(.debug_loc) } + .debug_macinfo 0 : { *(.debug_macinfo) } +/* SGI/MIPS DWARF 2 extensions */ + .debug_weaknames 0 : { *(.debug_weaknames) } + .debug_funcnames 0 : { *(.debug_funcnames) } + .debug_typenames 0 : { *(.debug_typenames) } + .debug_varnames 0 : { *(.debug_varnames) } +} diff --git a/stm32_lib/c_only_startup.s b/src/startup/c_only_startup.s similarity index 92% rename from stm32_lib/c_only_startup.s rename to src/startup/c_only_startup.s index 7e5d92d..7e6a17a 100644 --- a/stm32_lib/c_only_startup.s +++ b/src/startup/c_only_startup.s @@ -1,367 +1,366 @@ -/** - ****************************************************************************** - * @file startup_stm32f10x_md.s - * @author MCD Application Team - * @version V3.1.0 - * @date 06/19/2009 - * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. - * This module performs: - * - Set the initial SP - * - Set the initial PC == Reset_Handler, - * - Set the vector table entries with the exceptions ISR address - * - Branches to main in the C library (which eventually - * calls main()). - * After Reset the Cortex-M3 processor is in Thread mode, - * priority is Privileged, and the Stack is set to Main. - ******************************************************************************* - * @copy - * - * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS - * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE - * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY - * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING - * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE - * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. - * - *

© COPYRIGHT 2009 STMicroelectronics

- */ - - .syntax unified - .cpu cortex-m3 - .fpu softvfp - .thumb - -.global g_pfnVectors -.global SystemInit_ExtMemCtl_Dummy -.global Default_Handler - -/* start address for the initialization values of the .data section. -defined in linker script */ -.word _sidata -/* start address for the .data section. defined in linker script */ -.word _sdata -/* end address for the .data section. defined in linker script */ -.word _edata -/* start address for the .bss section. defined in linker script */ -.word _sbss -/* end address for the .bss section. defined in linker script */ -.word _ebss - -.equ BootRAM, 0xF108F85F -/** - * @brief This is the code that gets called when the processor first - * starts execution following a reset event. Only the absolutely - * necessary set is performed, after which the application - * supplied main() routine is called. - * @param None - * @retval : None -*/ - - .section .text.Reset_Handler - .weak Reset_Handler - .type Reset_Handler, %function -Reset_Handler: - -/* Copy the data segment initializers from flash to SRAM */ - movs r1, #0 - b LoopCopyDataInit - -CopyDataInit: - ldr r3, =_sidata - ldr r3, [r3, r1] - str r3, [r0, r1] - adds r1, r1, #4 - -LoopCopyDataInit: - ldr r0, =_sdata - ldr r3, =_edata - adds r2, r0, r1 - cmp r2, r3 - bcc CopyDataInit - ldr r2, =_sbss - b LoopFillZerobss -/* Zero fill the bss segment. */ -FillZerobss: - movs r3, #0 - str r3, [r2], #4 - -LoopFillZerobss: - ldr r3, = _ebss - cmp r2, r3 - bcc FillZerobss -/* Call the application's entry point.*/ - bl main - bx lr -.size Reset_Handler, .-Reset_Handler - -/** - * @brief This is the code that gets called when the processor receives an - * unexpected interrupt. This simply enters an infinite loop, preserving - * the system state for examination by a debugger. - * - * @param None - * @retval : None -*/ - .section .text.Default_Handler,"ax",%progbits -Default_Handler: -Infinite_Loop: - b Infinite_Loop - .size Default_Handler, .-Default_Handler -/****************************************************************************** -* -* The minimal vector table for a Cortex M3. Note that the proper constructs -* must be placed on this to ensure that it ends up at physical address -* 0x0000.0000. -* -******************************************************************************/ - .section .isr_vector,"a",%progbits - .type g_pfnVectors, %object - .size g_pfnVectors, .-g_pfnVectors - - -g_pfnVectors: - .word _estack - .word Reset_Handler - .word NMI_Handler - .word HardFault_Handler - .word MemManage_Handler - .word BusFault_Handler - .word UsageFault_Handler - .word 0 - .word 0 - .word 0 - .word 0 - .word SVC_Handler - .word DebugMon_Handler - .word 0 - .word PendSV_Handler - .word SysTick_Handler - .word WWDG_IRQHandler - .word PVD_IRQHandler - .word TAMPER_IRQHandler - .word RTC_IRQHandler - .word FLASH_IRQHandler - .word RCC_IRQHandler - .word EXTI0_IRQHandler - .word EXTI1_IRQHandler - .word EXTI2_IRQHandler - .word EXTI3_IRQHandler - .word EXTI4_IRQHandler - .word DMA1_Channel1_IRQHandler - .word DMA1_Channel2_IRQHandler - .word DMA1_Channel3_IRQHandler - .word DMA1_Channel4_IRQHandler - .word DMA1_Channel5_IRQHandler - .word DMA1_Channel6_IRQHandler - .word DMA1_Channel7_IRQHandler - .word ADC1_2_IRQHandler - .word USB_HP_CAN1_TX_IRQHandler - .word USB_LP_CAN1_RX0_IRQHandler - .word CAN1_RX1_IRQHandler - .word CAN1_SCE_IRQHandler - .word EXTI9_5_IRQHandler - .word TIM1_BRK_IRQHandler - .word TIM1_UP_IRQHandler - .word TIM1_TRG_COM_IRQHandler - .word TIM1_CC_IRQHandler - .word TIM2_IRQHandler - .word TIM3_IRQHandler - .word TIM4_IRQHandler - .word I2C1_EV_IRQHandler - .word I2C1_ER_IRQHandler - .word I2C2_EV_IRQHandler - .word I2C2_ER_IRQHandler - .word SPI1_IRQHandler - .word SPI2_IRQHandler - .word USART1_IRQHandler - .word USART2_IRQHandler - .word USART3_IRQHandler - .word EXTI15_10_IRQHandler - .word RTCAlarm_IRQHandler - .word USBWakeUp_IRQHandler -/* - .word TIM8_BRK - .word TIM8_UP - .word TIM8_TRG_COM - .word TIM8_CC - .word ADC3 - .word FSMC - .word SDIO - .word TIM5 - .word SPI3 - .word UART4 - .word UART5 - .word TIM6 - .word TIM7 - .word DMA2_Channel1 - .word DMA2_Channel2 - .word DMA2_Channel3 - .word DMA2_Channel5 -*/ - .word BootRAM /* @0x108. This is for boot in RAM mode for - STM32F10x Medium Density devices. */ - -/******************************************************************************* -* -* Provide weak aliases for each Exception handler to the Default_Handler. -* As they are weak aliases, any function with the same name will override -* this definition. -* -*******************************************************************************/ - - .weak NMI_Handler - .thumb_set NMI_Handler,Default_Handler - - .weak HardFault_Handler - .thumb_set HardFault_Handler,Default_Handler - - .weak MemManage_Handler - .thumb_set MemManage_Handler,Default_Handler - - .weak BusFault_Handler - .thumb_set BusFault_Handler,Default_Handler - - .weak UsageFault_Handler - .thumb_set UsageFault_Handler,Default_Handler - - .weak SVC_Handler - .thumb_set SVC_Handler,Default_Handler - - .weak DebugMon_Handler - .thumb_set DebugMon_Handler,Default_Handler - - .weak PendSV_Handler - .thumb_set PendSV_Handler,Default_Handler - - .weak SysTick_Handler - .thumb_set SysTick_Handler,Default_Handler - - .weak WWDG_IRQHandler - .thumb_set WWDG_IRQHandler,Default_Handler - - .weak PVD_IRQHandler - .thumb_set PVD_IRQHandler,Default_Handler - - .weak TAMPER_IRQHandler - .thumb_set TAMPER_IRQHandler,Default_Handler - - .weak RTC_IRQHandler - .thumb_set RTC_IRQHandler,Default_Handler - - .weak FLASH_IRQHandler - .thumb_set FLASH_IRQHandler,Default_Handler - - .weak RCC_IRQHandler - .thumb_set RCC_IRQHandler,Default_Handler - - .weak EXTI0_IRQHandler - .thumb_set EXTI0_IRQHandler,Default_Handler - - .weak EXTI1_IRQHandler - .thumb_set EXTI1_IRQHandler,Default_Handler - - .weak EXTI2_IRQHandler - .thumb_set EXTI2_IRQHandler,Default_Handler - - .weak EXTI3_IRQHandler - .thumb_set EXTI3_IRQHandler,Default_Handler - - .weak EXTI4_IRQHandler - .thumb_set EXTI4_IRQHandler,Default_Handler - - .weak DMA1_Channel1_IRQHandler - .thumb_set DMA1_Channel1_IRQHandler,Default_Handler - - .weak DMA1_Channel2_IRQHandler - .thumb_set DMA1_Channel2_IRQHandler,Default_Handler - - .weak DMA1_Channel3_IRQHandler - .thumb_set DMA1_Channel3_IRQHandler,Default_Handler - - .weak DMA1_Channel4_IRQHandler - .thumb_set DMA1_Channel4_IRQHandler,Default_Handler - - .weak DMA1_Channel5_IRQHandler - .thumb_set DMA1_Channel5_IRQHandler,Default_Handler - - .weak DMA1_Channel6_IRQHandler - .thumb_set DMA1_Channel6_IRQHandler,Default_Handler - - .weak DMA1_Channel7_IRQHandler - .thumb_set DMA1_Channel7_IRQHandler,Default_Handler - - .weak ADC1_2_IRQHandler - .thumb_set ADC1_2_IRQHandler,Default_Handler - - .weak USB_HP_CAN1_TX_IRQHandler - .thumb_set USB_HP_CAN1_TX_IRQHandler,Default_Handler - - .weak USB_LP_CAN1_RX0_IRQHandler - .thumb_set USB_LP_CAN1_RX0_IRQHandler,Default_Handler - - .weak CAN1_RX1_IRQHandler - .thumb_set CAN1_RX1_IRQHandler,Default_Handler - - .weak CAN1_SCE_IRQHandler - .thumb_set CAN1_SCE_IRQHandler,Default_Handler - - .weak EXTI9_5_IRQHandler - .thumb_set EXTI9_5_IRQHandler,Default_Handler - - .weak TIM1_BRK_IRQHandler - .thumb_set TIM1_BRK_IRQHandler,Default_Handler - - .weak TIM1_UP_IRQHandler - .thumb_set TIM1_UP_IRQHandler,Default_Handler - - .weak TIM1_TRG_COM_IRQHandler - .thumb_set TIM1_TRG_COM_IRQHandler,Default_Handler - - .weak TIM1_CC_IRQHandler - .thumb_set TIM1_CC_IRQHandler,Default_Handler - - .weak TIM2_IRQHandler - .thumb_set TIM2_IRQHandler,Default_Handler - - .weak TIM3_IRQHandler - .thumb_set TIM3_IRQHandler,Default_Handler - - .weak TIM4_IRQHandler - .thumb_set TIM4_IRQHandler,Default_Handler - - .weak I2C1_EV_IRQHandler - .thumb_set I2C1_EV_IRQHandler,Default_Handler - - .weak I2C1_ER_IRQHandler - .thumb_set I2C1_ER_IRQHandler,Default_Handler - - .weak I2C2_EV_IRQHandler - .thumb_set I2C2_EV_IRQHandler,Default_Handler - - .weak I2C2_ER_IRQHandler - .thumb_set I2C2_ER_IRQHandler,Default_Handler - - .weak SPI1_IRQHandler - .thumb_set SPI1_IRQHandler,Default_Handler - - .weak SPI2_IRQHandler - .thumb_set SPI2_IRQHandler,Default_Handler - - .weak USART1_IRQHandler - .thumb_set USART1_IRQHandler,Default_Handler - - .weak USART2_IRQHandler - .thumb_set USART2_IRQHandler,Default_Handler - - .weak USART3_IRQHandler - .thumb_set USART3_IRQHandler,Default_Handler - - .weak EXTI15_10_IRQHandler - .thumb_set EXTI15_10_IRQHandler,Default_Handler - - .weak RTCAlarm_IRQHandler - .thumb_set RTCAlarm_IRQHandler,Default_Handler - - .weak USBWakeUp_IRQHandler - .thumb_set USBWakeUp_IRQHandler,Default_Handler - +/** + ****************************************************************************** + * @file startup_stm32f10x_md.s + * @author MCD Application Team + * @version V3.1.0 + * @date 06/19/2009 + * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. + * This module performs: + * - Set the initial SP + * - Set the initial PC == Reset_Handler, + * - Set the vector table entries with the exceptions ISR address + * - Branches to main in the C library (which eventually + * calls main()). + * After Reset the Cortex-M3 processor is in Thread mode, + * priority is Privileged, and the Stack is set to Main. + ******************************************************************************* + * @copy + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2009 STMicroelectronics

+ */ + + .syntax unified + .cpu cortex-m3 + .fpu softvfp + .thumb + +.global g_pfnVectors +.global SystemInit_ExtMemCtl_Dummy +.global Default_Handler + +/* start address for the initialization values of the .data section. +defined in linker script */ +.word _sidata +/* start address for the .data section. defined in linker script */ +.word _sdata +/* end address for the .data section. defined in linker script */ +.word _edata +/* start address for the .bss section. defined in linker script */ +.word _sbss +/* end address for the .bss section. defined in linker script */ +.word _ebss + +.equ BootRAM, 0xF108F85F +/** + * @brief This is the code that gets called when the processor first + * starts execution following a reset event. Only the absolutely + * necessary set is performed, after which the application + * supplied main() routine is called. + * @param None + * @retval : None +*/ + + .section .text.Reset_Handler + .weak Reset_Handler + .type Reset_Handler, %function +Reset_Handler: + +/* Copy the data segment initializers from flash to SRAM */ + movs r1, #0 + b LoopCopyDataInit + +CopyDataInit: + ldr r3, =_sidata + ldr r3, [r3, r1] + str r3, [r0, r1] + adds r1, r1, #4 + +LoopCopyDataInit: + ldr r0, =_sdata + ldr r3, =_edata + adds r2, r0, r1 + cmp r2, r3 + bcc CopyDataInit + ldr r2, =_sbss + b LoopFillZerobss +/* Zero fill the bss segment. */ +FillZerobss: + movs r3, #0 + str r3, [r2], #4 + +LoopFillZerobss: + ldr r3, = _ebss + cmp r2, r3 + bcc FillZerobss +/* Call the application's entry point.*/ + bl main + bx lr +.size Reset_Handler, .-Reset_Handler + +/** + * @brief This is the code that gets called when the processor receives an + * unexpected interrupt. This simply enters an infinite loop, preserving + * the system state for examination by a debugger. + * + * @param None + * @retval : None +*/ + .section .text.Default_Handler,"ax",%progbits +Default_Handler: +Infinite_Loop: + b Infinite_Loop + .size Default_Handler, .-Default_Handler +/****************************************************************************** +* +* The minimal vector table for a Cortex M3. Note that the proper constructs +* must be placed on this to ensure that it ends up at physical address +* 0x0000.0000. +* +******************************************************************************/ + .section .isr_vector,"a",%progbits + .type g_pfnVectors, %object + .size g_pfnVectors, .-g_pfnVectors + + +g_pfnVectors: + .word _estack + .word Reset_Handler + .word NMI_Handler + .word HardFault_Handler + .word MemManage_Handler + .word BusFault_Handler + .word UsageFault_Handler + .word 0 + .word 0 + .word 0 + .word 0 + .word SVC_Handler + .word DebugMon_Handler + .word 0 + .word PendSV_Handler + .word SysTick_Handler + .word WWDG_IRQHandler + .word PVD_IRQHandler + .word TAMPER_IRQHandler + .word RTC_IRQHandler + .word FLASH_IRQHandler + .word RCC_IRQHandler + .word EXTI0_IRQHandler + .word EXTI1_IRQHandler + .word EXTI2_IRQHandler + .word EXTI3_IRQHandler + .word EXTI4_IRQHandler + .word DMA1_Channel1_IRQHandler + .word DMA1_Channel2_IRQHandler + .word DMA1_Channel3_IRQHandler + .word DMA1_Channel4_IRQHandler + .word DMA1_Channel5_IRQHandler + .word DMA1_Channel6_IRQHandler + .word DMA1_Channel7_IRQHandler + .word ADC1_2_IRQHandler + .word USB_HP_CAN1_TX_IRQHandler + .word USB_LP_CAN1_RX0_IRQHandler + .word CAN1_RX1_IRQHandler + .word CAN1_SCE_IRQHandler + .word EXTI9_5_IRQHandler + .word TIM1_BRK_IRQHandler + .word TIM1_UP_IRQHandler + .word TIM1_TRG_COM_IRQHandler + .word TIM1_CC_IRQHandler + .word TIM2_IRQHandler + .word TIM3_IRQHandler + .word TIM4_IRQHandler + .word I2C1_EV_IRQHandler + .word I2C1_ER_IRQHandler + .word I2C2_EV_IRQHandler + .word I2C2_ER_IRQHandler + .word SPI1_IRQHandler + .word SPI2_IRQHandler + .word USART1_IRQHandler + .word USART2_IRQHandler + .word USART3_IRQHandler + .word EXTI15_10_IRQHandler + .word RTCAlarm_IRQHandler + .word USBWakeUp_IRQHandler +/* + .word TIM8_BRK + .word TIM8_UP + .word TIM8_TRG_COM + .word TIM8_CC + .word ADC3 + .word FSMC + .word SDIO + .word TIM5 + .word SPI3 + .word UART4 + .word UART5 + .word TIM6 + .word TIM7 + .word DMA2_Channel1 + .word DMA2_Channel2 + .word DMA2_Channel3 + .word DMA2_Channel5 +*/ + .word BootRAM /* @0x108. This is for boot in RAM mode for + STM32F10x Medium Density devices. */ + +/******************************************************************************* +* +* Provide weak aliases for each Exception handler to the Default_Handler. +* As they are weak aliases, any function with the same name will override +* this definition. +* +*******************************************************************************/ + + .weak NMI_Handler + .thumb_set NMI_Handler,Default_Handler + + .weak HardFault_Handler + .thumb_set HardFault_Handler,Default_Handler + + .weak MemManage_Handler + .thumb_set MemManage_Handler,Default_Handler + + .weak BusFault_Handler + .thumb_set BusFault_Handler,Default_Handler + + .weak UsageFault_Handler + .thumb_set UsageFault_Handler,Default_Handler + + .weak SVC_Handler + .thumb_set SVC_Handler,Default_Handler + + .weak DebugMon_Handler + .thumb_set DebugMon_Handler,Default_Handler + + .weak PendSV_Handler + .thumb_set PendSV_Handler,Default_Handler + + .weak SysTick_Handler + .thumb_set SysTick_Handler,Default_Handler + + .weak WWDG_IRQHandler + .thumb_set WWDG_IRQHandler,Default_Handler + + .weak PVD_IRQHandler + .thumb_set PVD_IRQHandler,Default_Handler + + .weak TAMPER_IRQHandler + .thumb_set TAMPER_IRQHandler,Default_Handler + + .weak RTC_IRQHandler + .thumb_set RTC_IRQHandler,Default_Handler + + .weak FLASH_IRQHandler + .thumb_set FLASH_IRQHandler,Default_Handler + + .weak RCC_IRQHandler + .thumb_set RCC_IRQHandler,Default_Handler + + .weak EXTI0_IRQHandler + .thumb_set EXTI0_IRQHandler,Default_Handler + + .weak EXTI1_IRQHandler + .thumb_set EXTI1_IRQHandler,Default_Handler + + .weak EXTI2_IRQHandler + .thumb_set EXTI2_IRQHandler,Default_Handler + + .weak EXTI3_IRQHandler + .thumb_set EXTI3_IRQHandler,Default_Handler + + .weak EXTI4_IRQHandler + .thumb_set EXTI4_IRQHandler,Default_Handler + + .weak DMA1_Channel1_IRQHandler + .thumb_set DMA1_Channel1_IRQHandler,Default_Handler + + .weak DMA1_Channel2_IRQHandler + .thumb_set DMA1_Channel2_IRQHandler,Default_Handler + + .weak DMA1_Channel3_IRQHandler + .thumb_set DMA1_Channel3_IRQHandler,Default_Handler + + .weak DMA1_Channel4_IRQHandler + .thumb_set DMA1_Channel4_IRQHandler,Default_Handler + + .weak DMA1_Channel5_IRQHandler + .thumb_set DMA1_Channel5_IRQHandler,Default_Handler + + .weak DMA1_Channel6_IRQHandler + .thumb_set DMA1_Channel6_IRQHandler,Default_Handler + + .weak DMA1_Channel7_IRQHandler + .thumb_set DMA1_Channel7_IRQHandler,Default_Handler + + .weak ADC1_2_IRQHandler + .thumb_set ADC1_2_IRQHandler,Default_Handler + + .weak USB_HP_CAN1_TX_IRQHandler + .thumb_set USB_HP_CAN1_TX_IRQHandler,Default_Handler + + .weak USB_LP_CAN1_RX0_IRQHandler + .thumb_set USB_LP_CAN1_RX0_IRQHandler,Default_Handler + + .weak CAN1_RX1_IRQHandler + .thumb_set CAN1_RX1_IRQHandler,Default_Handler + + .weak CAN1_SCE_IRQHandler + .thumb_set CAN1_SCE_IRQHandler,Default_Handler + + .weak EXTI9_5_IRQHandler + .thumb_set EXTI9_5_IRQHandler,Default_Handler + + .weak TIM1_BRK_IRQHandler + .thumb_set TIM1_BRK_IRQHandler,Default_Handler + + .weak TIM1_UP_IRQHandler + .thumb_set TIM1_UP_IRQHandler,Default_Handler + + .weak TIM1_TRG_COM_IRQHandler + .thumb_set TIM1_TRG_COM_IRQHandler,Default_Handler + + .weak TIM1_CC_IRQHandler + .thumb_set TIM1_CC_IRQHandler,Default_Handler + + .weak TIM2_IRQHandler + .thumb_set TIM2_IRQHandler,Default_Handler + + .weak TIM3_IRQHandler + .thumb_set TIM3_IRQHandler,Default_Handler + + .weak TIM4_IRQHandler + .thumb_set TIM4_IRQHandler,Default_Handler + + .weak I2C1_EV_IRQHandler + .thumb_set I2C1_EV_IRQHandler,Default_Handler + + .weak I2C1_ER_IRQHandler + .thumb_set I2C1_ER_IRQHandler,Default_Handler + + .weak I2C2_EV_IRQHandler + .thumb_set I2C2_EV_IRQHandler,Default_Handler + + .weak I2C2_ER_IRQHandler + .thumb_set I2C2_ER_IRQHandler,Default_Handler + + .weak SPI1_IRQHandler + .thumb_set SPI1_IRQHandler,Default_Handler + + .weak SPI2_IRQHandler + .thumb_set SPI2_IRQHandler,Default_Handler + + .weak USART1_IRQHandler + .thumb_set USART1_IRQHandler,Default_Handler + + .weak USART2_IRQHandler + .thumb_set USART2_IRQHandler,Default_Handler + + .weak USART3_IRQHandler + .thumb_set USART3_IRQHandler,Default_Handler + + .weak EXTI15_10_IRQHandler + .thumb_set EXTI15_10_IRQHandler,Default_Handler + + .weak RTCAlarm_IRQHandler + .thumb_set RTCAlarm_IRQHandler,Default_Handler + + .weak USBWakeUp_IRQHandler + .thumb_set USBWakeUp_IRQHandler,Default_Handler diff --git a/stm32_lib/c_only_startup_user.s b/src/startup/c_only_startup_user.s similarity index 88% rename from stm32_lib/c_only_startup_user.s rename to src/startup/c_only_startup_user.s index 3b2f82f..1ebf876 100644 --- a/stm32_lib/c_only_startup_user.s +++ b/src/startup/c_only_startup_user.s @@ -1,128 +1,126 @@ -/** - ****************************************************************************** - * @file startup_stm32f10x_md.s - * @author MCD Application Team - * @version V3.1.0 - * @date 06/19/2009 - * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. - * This module performs: - * - Set the initial SP - * - Set the initial PC == Reset_Handler, - * - Set the vector table entries with the exceptions ISR address - * - Branches to main in the C library (which eventually - * calls main()). - * After Reset the Cortex-M3 processor is in Thread mode, - * priority is Privileged, and the Stack is set to Main. - ******************************************************************************* - * @copy - * - * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS - * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE - * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY - * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING - * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE - * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. - * - *

© COPYRIGHT 2009 STMicroelectronics

- */ - - .syntax unified - .cpu cortex-m3 - .fpu softvfp - .thumb - -.global g_pfnVectors -.global SystemInit_ExtMemCtl_Dummy -.global Default_Handler - -/* start address for the initialization values of the .data section. -defined in linker script */ -.word _sidata -/* start address for the .data section. defined in linker script */ -.word _sdata -/* end address for the .data section. defined in linker script */ -.word _edata -/* start address for the .bss section. defined in linker script */ -.word _sbss -/* end address for the .bss section. defined in linker script */ -.word _ebss -.word _magicRate - -.equ BootRAM, 0xF108F85F -/** - * @brief This is the code that gets called when the processor first - * starts execution following a reset event. Only the absolutely - * necessary set is performed, after which the application - * supplied main() routine is called. - * @param None - * @retval : None -*/ - - .section .text.Reset_Handler - .weak Reset_Handler - .type Reset_Handler, %function -Reset_Handler: - -/* Copy the data segment initializers from flash to SRAM */ - movs r1, #0 - b LoopCopyDataInit - -CopyDataInit: - ldr r3, =_sidata - ldr r3, [r3, r1] - str r3, [r0, r1] - adds r1, r1, #4 - -LoopCopyDataInit: - ldr r0, =_sdata - ldr r3, =_edata - adds r2, r0, r1 - cmp r2, r3 - bcc CopyDataInit - ldr r2, =_sbss - b LoopFillZerobss -/* Zero fill the bss segment. */ -FillZerobss: - movs r3, #0 - str r3, [r2], #4 - -LoopFillZerobss: - ldr r3, = _ebss - cmp r2, r3 - bcc FillZerobss -/* Call the application's entry point.*/ - bl main - bx lr -.size Reset_Handler, .-Reset_Handler - -/** - * @brief This is the code that gets called when the processor receives an - * unexpected interrupt. This simply enters an infinite loop, preserving - * the system state for examination by a debugger. - * - * @param None - * @retval : None -*/ - .section .text.Default_Handler,"ax",%progbits -Default_Handler: -Infinite_Loop: - b Infinite_Loop - .size Default_Handler, .-Default_Handler -/****************************************************************************** -* -* The minimal vector table for a Cortex M3. Note that the proper constructs -* must be placed on this to ensure that it ends up at physical address -* 0x0000.0000. -* -******************************************************************************/ - .section .isr_vector,"a",%progbits - .type g_pfnVectors, %object - .size g_pfnVectors, .-g_pfnVectors - - -g_pfnVectors: - .word _estack - .word Reset_Handler - .word _magicRate - - +/** + ****************************************************************************** + * @file startup_stm32f10x_md.s + * @author MCD Application Team + * @version V3.1.0 + * @date 06/19/2009 + * @brief STM32F10x Medium Density Devices vector table for RIDE7 toolchain. + * This module performs: + * - Set the initial SP + * - Set the initial PC == Reset_Handler, + * - Set the vector table entries with the exceptions ISR address + * - Branches to main in the C library (which eventually + * calls main()). + * After Reset the Cortex-M3 processor is in Thread mode, + * priority is Privileged, and the Stack is set to Main. + ******************************************************************************* + * @copy + * + * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS + * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE + * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY + * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING + * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE + * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. + * + *

© COPYRIGHT 2009 STMicroelectronics

+ */ + + .syntax unified + .cpu cortex-m3 + .fpu softvfp + .thumb + +.global g_pfnVectors +.global SystemInit_ExtMemCtl_Dummy +.global Default_Handler + +/* start address for the initialization values of the .data section. +defined in linker script */ +.word _sidata +/* start address for the .data section. defined in linker script */ +.word _sdata +/* end address for the .data section. defined in linker script */ +.word _edata +/* start address for the .bss section. defined in linker script */ +.word _sbss +/* end address for the .bss section. defined in linker script */ +.word _ebss +.word _magicRate + +.equ BootRAM, 0xF108F85F +/** + * @brief This is the code that gets called when the processor first + * starts execution following a reset event. Only the absolutely + * necessary set is performed, after which the application + * supplied main() routine is called. + * @param None + * @retval : None +*/ + + .section .text.Reset_Handler + .weak Reset_Handler + .type Reset_Handler, %function +Reset_Handler: + +/* Copy the data segment initializers from flash to SRAM */ + movs r1, #0 + b LoopCopyDataInit + +CopyDataInit: + ldr r3, =_sidata + ldr r3, [r3, r1] + str r3, [r0, r1] + adds r1, r1, #4 + +LoopCopyDataInit: + ldr r0, =_sdata + ldr r3, =_edata + adds r2, r0, r1 + cmp r2, r3 + bcc CopyDataInit + ldr r2, =_sbss + b LoopFillZerobss +/* Zero fill the bss segment. */ +FillZerobss: + movs r3, #0 + str r3, [r2], #4 + +LoopFillZerobss: + ldr r3, = _ebss + cmp r2, r3 + bcc FillZerobss +/* Call the application's entry point.*/ + bl main + bx lr +.size Reset_Handler, .-Reset_Handler + +/** + * @brief This is the code that gets called when the processor receives an + * unexpected interrupt. This simply enters an infinite loop, preserving + * the system state for examination by a debugger. + * + * @param None + * @retval : None +*/ + .section .text.Default_Handler,"ax",%progbits +Default_Handler: +Infinite_Loop: + b Infinite_Loop + .size Default_Handler, .-Default_Handler +/****************************************************************************** +* +* The minimal vector table for a Cortex M3. Note that the proper constructs +* must be placed on this to ensure that it ends up at physical address +* 0x0000.0000. +* +******************************************************************************/ + .section .isr_vector,"a",%progbits + .type g_pfnVectors, %object + .size g_pfnVectors, .-g_pfnVectors + + +g_pfnVectors: + .word _estack + .word Reset_Handler + .word _magicRate diff --git a/stm32_lib/cortexm3_macro.h b/src/startup/cortexm3_macro.h similarity index 97% rename from stm32_lib/cortexm3_macro.h rename to src/startup/cortexm3_macro.h index b26807f..6adbc3b 100644 --- a/stm32_lib/cortexm3_macro.h +++ b/src/startup/cortexm3_macro.h @@ -1,53 +1,53 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : cortexm3_macro.h -* Author : MCD Application Team -* Version : V2.0.3 -* Date : 09/22/2008 -* Description : Header file for cortexm3_macro.s. -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __CORTEXM3_MACRO_H -#define __CORTEXM3_MACRO_H - -/* Includes ------------------------------------------------------------------*/ -#include "stm32f10x_type.h" - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -void __WFI(void); -void __WFE(void); -void __SEV(void); -void __ISB(void); -void __DSB(void); -void __DMB(void); -void __SVC(void); -u32 __MRS_CONTROL(void); -void __MSR_CONTROL(u32 Control); -u32 __MRS_PSP(void); -void __MSR_PSP(u32 TopOfProcessStack); -u32 __MRS_MSP(void); -void __MSR_MSP(u32 TopOfMainStack); -void __RESETPRIMASK(void); -void __SETPRIMASK(void); -u32 __READ_PRIMASK(void); -void __RESETFAULTMASK(void); -void __SETFAULTMASK(void); -u32 __READ_FAULTMASK(void); -void __BASEPRICONFIG(u32 NewPriority); -u32 __GetBASEPRI(void); -u16 __REV_HalfWord(u16 Data); -u32 __REV_Word(u32 Data); - -#endif /* __CORTEXM3_MACRO_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : cortexm3_macro.h +* Author : MCD Application Team +* Version : V2.0.3 +* Date : 09/22/2008 +* Description : Header file for cortexm3_macro.s. +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __CORTEXM3_MACRO_H +#define __CORTEXM3_MACRO_H + +/* Includes ------------------------------------------------------------------*/ +#include "stm32f10x_type.h" + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void __WFI(void); +void __WFE(void); +void __SEV(void); +void __ISB(void); +void __DSB(void); +void __DMB(void); +void __SVC(void); +u32 __MRS_CONTROL(void); +void __MSR_CONTROL(u32 Control); +u32 __MRS_PSP(void); +void __MSR_PSP(u32 TopOfProcessStack); +u32 __MRS_MSP(void); +void __MSR_MSP(u32 TopOfMainStack); +void __RESETPRIMASK(void); +void __SETPRIMASK(void); +u32 __READ_PRIMASK(void); +void __RESETFAULTMASK(void); +void __SETFAULTMASK(void); +u32 __READ_FAULTMASK(void); +void __BASEPRICONFIG(u32 NewPriority); +u32 __GetBASEPRI(void); +u16 __REV_HalfWord(u16 Data); +u32 __REV_Word(u32 Data); + +#endif /* __CORTEXM3_MACRO_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/stm32_lib/cortexm3_macro.s b/src/startup/cortexm3_macro.s similarity index 100% rename from stm32_lib/cortexm3_macro.s rename to src/startup/cortexm3_macro.s diff --git a/stm32_lib/stm32f10x_type.h b/src/startup/stm32f10x_type.h similarity index 97% rename from stm32_lib/stm32f10x_type.h rename to src/startup/stm32f10x_type.h index 1ca3e52..5563de3 100644 --- a/stm32_lib/stm32f10x_type.h +++ b/src/startup/stm32f10x_type.h @@ -1,80 +1,80 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : stm32f10x_type.h -* Author : MCD Application Team -* Version : V2.0.3 -* Date : 09/22/2008 -* Description : This file contains all the common data types used for the -* STM32F10x firmware library. -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __STM32F10x_TYPE_H -#define __STM32F10x_TYPE_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -typedef signed long s32; -typedef signed short s16; -typedef signed char s8; - -typedef signed long const sc32; /* Read Only */ -typedef signed short const sc16; /* Read Only */ -typedef signed char const sc8; /* Read Only */ - -typedef volatile signed long vs32; -typedef volatile signed short vs16; -typedef volatile signed char vs8; - -typedef volatile signed long const vsc32; /* Read Only */ -typedef volatile signed short const vsc16; /* Read Only */ -typedef volatile signed char const vsc8; /* Read Only */ - -typedef unsigned long u32; -typedef unsigned short u16; -typedef unsigned char u8; - -typedef unsigned long const uc32; /* Read Only */ -typedef unsigned short const uc16; /* Read Only */ -typedef unsigned char const uc8; /* Read Only */ - -typedef volatile unsigned long vu32; -typedef volatile unsigned short vu16; -typedef volatile unsigned char vu8; - -typedef volatile unsigned long const vuc32; /* Read Only */ -typedef volatile unsigned short const vuc16; /* Read Only */ -typedef volatile unsigned char const vuc8; /* Read Only */ - -typedef enum {FALSE = 0, TRUE = !FALSE} bool; - -typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus; - -typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; -#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE)) - -typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus; - -#define U8_MAX ((u8)255) -#define S8_MAX ((s8)127) -#define S8_MIN ((s8)-128) -#define U16_MAX ((u16)65535u) -#define S16_MAX ((s16)32767) -#define S16_MIN ((s16)-32768) -#define U32_MAX ((u32)4294967295uL) -#define S32_MAX ((s32)2147483647) -#define S32_MIN ((s32)-2147483648) - -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ - -#endif /* __STM32F10x_TYPE_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : stm32f10x_type.h +* Author : MCD Application Team +* Version : V2.0.3 +* Date : 09/22/2008 +* Description : This file contains all the common data types used for the +* STM32F10x firmware library. +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __STM32F10x_TYPE_H +#define __STM32F10x_TYPE_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef signed long s32; +typedef signed short s16; +typedef signed char s8; + +typedef signed long const sc32; /* Read Only */ +typedef signed short const sc16; /* Read Only */ +typedef signed char const sc8; /* Read Only */ + +typedef volatile signed long vs32; +typedef volatile signed short vs16; +typedef volatile signed char vs8; + +typedef volatile signed long const vsc32; /* Read Only */ +typedef volatile signed short const vsc16; /* Read Only */ +typedef volatile signed char const vsc8; /* Read Only */ + +typedef unsigned long u32; +typedef unsigned short u16; +typedef unsigned char u8; + +typedef unsigned long const uc32; /* Read Only */ +typedef unsigned short const uc16; /* Read Only */ +typedef unsigned char const uc8; /* Read Only */ + +typedef volatile unsigned long vu32; +typedef volatile unsigned short vu16; +typedef volatile unsigned char vu8; + +typedef volatile unsigned long const vuc32; /* Read Only */ +typedef volatile unsigned short const vuc16; /* Read Only */ +typedef volatile unsigned char const vuc8; /* Read Only */ + +typedef enum {FALSE = 0, TRUE = !FALSE} bool; + +typedef enum {RESET = 0, SET = !RESET} FlagStatus, ITStatus; + +typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; +#define IS_FUNCTIONAL_STATE(STATE) (((STATE) == DISABLE) || ((STATE) == ENABLE)) + +typedef enum {ERROR = 0, SUCCESS = !ERROR} ErrorStatus; + +#define U8_MAX ((u8)255) +#define S8_MAX ((s8)127) +#define S8_MIN ((s8)-128) +#define U16_MAX ((u16)65535u) +#define S16_MAX ((s16)32767) +#define S16_MIN ((s16)-32768) +#define U32_MAX ((u32)4294967295uL) +#define S32_MAX ((s32)2147483647) +#define S32_MIN ((s32)-2147483648) + +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +#endif /* __STM32F10x_TYPE_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/src/usb_lib/Makefile b/src/usb_lib/Makefile new file mode 100644 index 0000000..064105d --- /dev/null +++ b/src/usb_lib/Makefile @@ -0,0 +1,6 @@ +CFLAGS+=-I$(SRCDIR)/src/usb_lib/ +objects-usb_lib += usb_regs.o \ + usb_int.o \ + usb_init.o \ + usb_core.o \ + usb_mem.o diff --git a/usb_lib/usb_conf.h b/src/usb_lib/usb_conf.h similarity index 97% rename from usb_lib/usb_conf.h rename to src/usb_lib/usb_conf.h index e9b7f14..80883a9 100644 --- a/usb_lib/usb_conf.h +++ b/src/usb_lib/usb_conf.h @@ -1,86 +1,82 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_conf.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Device Firmware Upgrade (DFU) configuration file -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_CONF_H -#define __USB_CONF_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -/* External variables --------------------------------------------------------*/ -/*-------------------------------------------------------------*/ -/* EP_NUM */ -/* defines how many endpoints are used by the device */ -/*-------------------------------------------------------------*/ -#define EP_NUM (1) - -/*-------------------------------------------------------------*/ -/* -------------- Buffer Description Table -----------------*/ -/*-------------------------------------------------------------*/ -/* buffer table base address */ -/* buffer table base address */ -#define BTABLE_ADDRESS (0x00) - -/* EP0 */ -/* rx/tx buffer base address */ -#define ENDP0_RXADDR (0x10) -#define ENDP0_TXADDR (0x50) - - -/*-------------------------------------------------------------*/ -/* ------------------- ISTR events -------------------------*/ -/*-------------------------------------------------------------*/ -/* IMR_MSK */ -/* mask defining which events has to be handled */ -/* by the device application software */ -#define IMR_MSK (CNTR_CTRM | \ - CNTR_WKUPM | \ - CNTR_SUSPM | \ - CNTR_ERRM | \ - CNTR_SOFM | \ - CNTR_ESOFM | \ - CNTR_RESETM \ - ) - -/* CTR service routines */ -/* associated to defined endpoints */ -#define EP1_IN_Callback NOP_Process -#define EP2_IN_Callback NOP_Process -#define EP3_IN_Callback NOP_Process -#define EP4_IN_Callback NOP_Process -#define EP5_IN_Callback NOP_Process -#define EP6_IN_Callback NOP_Process -#define EP7_IN_Callback NOP_Process - - -#define EP1_OUT_Callback NOP_Process -#define EP2_OUT_Callback NOP_Process -#define EP3_OUT_Callback NOP_Process -#define EP4_OUT_Callback NOP_Process -#define EP5_OUT_Callback NOP_Process -#define EP6_OUT_Callback NOP_Process -#define EP7_OUT_Callback NOP_Process - - -#endif /*__USB_CONF_H*/ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ - - - - +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_conf.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Device Firmware Upgrade (DFU) configuration file +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_CONF_H +#define __USB_CONF_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +/* External variables --------------------------------------------------------*/ +/*-------------------------------------------------------------*/ +/* EP_NUM */ +/* defines how many endpoints are used by the device */ +/*-------------------------------------------------------------*/ +#define EP_NUM (1) + +/*-------------------------------------------------------------*/ +/* -------------- Buffer Description Table -----------------*/ +/*-------------------------------------------------------------*/ +/* buffer table base address */ +/* buffer table base address */ +#define BTABLE_ADDRESS (0x00) + +/* EP0 */ +/* rx/tx buffer base address */ +#define ENDP0_RXADDR (0x10) +#define ENDP0_TXADDR (0x50) + + +/*-------------------------------------------------------------*/ +/* ------------------- ISTR events -------------------------*/ +/*-------------------------------------------------------------*/ +/* IMR_MSK */ +/* mask defining which events has to be handled */ +/* by the device application software */ +#define IMR_MSK (CNTR_CTRM | \ + CNTR_WKUPM | \ + CNTR_SUSPM | \ + CNTR_ERRM | \ + CNTR_SOFM | \ + CNTR_ESOFM | \ + CNTR_RESETM \ + ) + +/* CTR service routines */ +/* associated to defined endpoints */ +#define EP1_IN_Callback NOP_Process +#define EP2_IN_Callback NOP_Process +#define EP3_IN_Callback NOP_Process +#define EP4_IN_Callback NOP_Process +#define EP5_IN_Callback NOP_Process +#define EP6_IN_Callback NOP_Process +#define EP7_IN_Callback NOP_Process + + +#define EP1_OUT_Callback NOP_Process +#define EP2_OUT_Callback NOP_Process +#define EP3_OUT_Callback NOP_Process +#define EP4_OUT_Callback NOP_Process +#define EP5_OUT_Callback NOP_Process +#define EP6_OUT_Callback NOP_Process +#define EP7_OUT_Callback NOP_Process + + +#endif /*__USB_CONF_H*/ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_core.c b/src/usb_lib/usb_core.c similarity index 96% rename from usb_lib/usb_core.c rename to src/usb_lib/usb_core.c index f637165..211f221 100644 --- a/usb_lib/usb_core.c +++ b/src/usb_lib/usb_core.c @@ -1,1016 +1,1016 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_core.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Standard protocol processing (USB v2.0) -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -#define ValBit(VAR,Place) (VAR & (1 << Place)) -#define SetBit(VAR,Place) (VAR |= (1 << Place)) -#define ClrBit(VAR,Place) (VAR &= ((1 << Place) ^ 255)) - -#define Send0LengthData() { _SetEPTxCount(ENDP0, 0); \ - vSetEPTxStatus(EP_TX_VALID); \ - } - -#define vSetEPRxStatus(st) (SaveRState = st) -#define vSetEPTxStatus(st) (SaveTState = st) - -#define USB_StatusIn() Send0LengthData() -#define USB_StatusOut() vSetEPRxStatus(EP_RX_VALID) - -#define StatusInfo0 StatusInfo.bw.bb1 /* Reverse bb0 & bb1 */ -#define StatusInfo1 StatusInfo.bw.bb0 - -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -u16_u8 StatusInfo; -bool Data_Mul_MaxPacketSize = FALSE; -/* Private function prototypes -----------------------------------------------*/ -static void DataStageOut(void); -static void DataStageIn(void); -static void NoData_Setup0(void); -static void Data_Setup0(void); -/* Private functions ---------------------------------------------------------*/ - -/******************************************************************************* -* Function Name : Standard_GetConfiguration. -* Description : Return the current configuration variable address. -* Input : Length - How many bytes are needed. -* Output : None. -* Return : Return 1 , if the request is invalid when "Length" is 0. -* Return "Buffer" if the "Length" is not 0. -*******************************************************************************/ -u8 *Standard_GetConfiguration(u16 Length) -{ - if (Length == 0) - { - pInformation->Ctrl_Info.Usb_wLength = - sizeof(pInformation->Current_Configuration); - return 0; - } - pUser_Standard_Requests->User_GetConfiguration(); - return (u8 *)&pInformation->Current_Configuration; -} - -/******************************************************************************* -* Function Name : Standard_SetConfiguration. -* Description : This routine is called to set the configuration value -* Then each class should configure device themself. -* Input : None. -* Output : None. -* Return : Return USB_SUCCESS, if the request is performed. -* Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_SetConfiguration(void) -{ - - if ((pInformation->USBwValue0 <= - Device_Table.Total_Configuration) && (pInformation->USBwValue1 == 0) - && (pInformation->USBwIndex == 0)) /*call Back usb spec 2.0*/ - { - pInformation->Current_Configuration = pInformation->USBwValue0; - pUser_Standard_Requests->User_SetConfiguration(); - return USB_SUCCESS; - } - else - { - return USB_UNSUPPORT; - } -} - -/******************************************************************************* -* Function Name : Standard_GetInterface. -* Description : Return the Alternate Setting of the current interface. -* Input : Length - How many bytes are needed. -* Output : None. -* Return : Return 0, if the request is invalid when "Length" is 0. -* Return "Buffer" if the "Length" is not 0. -*******************************************************************************/ -u8 *Standard_GetInterface(u16 Length) -{ - if (Length == 0) - { - pInformation->Ctrl_Info.Usb_wLength = - sizeof(pInformation->Current_AlternateSetting); - return 0; - } - pUser_Standard_Requests->User_GetInterface(); - return (u8 *)&pInformation->Current_AlternateSetting; -} - -/******************************************************************************* -* Function Name : Standard_SetInterface. -* Description : This routine is called to set the interface. -* Then each class should configure the interface them self. -* Input : None. -* Output : None. -* Return : - Return USB_SUCCESS, if the request is performed. -* - Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_SetInterface(void) -{ - RESULT Re; - /*Test if the specified Interface and Alternate Setting are supported by - the application Firmware*/ - Re = (*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, pInformation->USBwValue0); - - if (pInformation->Current_Configuration != 0) - { - if ((Re != USB_SUCCESS) || (pInformation->USBwIndex1 != 0) - || (pInformation->USBwValue1 != 0)) - { - return USB_UNSUPPORT; - } - else if (Re == USB_SUCCESS) - { - pUser_Standard_Requests->User_SetInterface(); - pInformation->Current_Interface = pInformation->USBwIndex0; - pInformation->Current_AlternateSetting = pInformation->USBwValue0; - return USB_SUCCESS; - } - - } - - return USB_UNSUPPORT; -} - -/******************************************************************************* -* Function Name : Standard_GetStatus. -* Description : Copy the device request data to "StatusInfo buffer". -* Input : - Length - How many bytes are needed. -* Output : None. -* Return : Return 0, if the request is at end of data block, -* or is invalid when "Length" is 0. -*******************************************************************************/ -u8 *Standard_GetStatus(u16 Length) -{ - if (Length == 0) - { - pInformation->Ctrl_Info.Usb_wLength = 2; - return 0; - } - - StatusInfo.w = 0; - /* Reset Status Information */ - - if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - { - /*Get Device Status */ - u8 Feature = pInformation->Current_Feature; - - /* Remote Wakeup enabled */ - if (ValBit(Feature, 5)) - { - SetBit(StatusInfo0, 1); - } - - /* Bus-powered */ - if (ValBit(Feature, 6)) - { - ClrBit(StatusInfo0, 0); - } - else /* Self-powered */ - { - SetBit(StatusInfo0, 0); - } - } - /*Interface Status*/ - else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) - { - return (u8 *)&StatusInfo; - } - /*Get EndPoint Status*/ - else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) - { - u8 Related_Endpoint; - u8 wIndex0 = pInformation->USBwIndex0; - - Related_Endpoint = (wIndex0 & 0x0f); - if (ValBit(wIndex0, 7)) - { - /* IN endpoint */ - if (_GetTxStallStatus(Related_Endpoint)) - { - SetBit(StatusInfo0, 0); /* IN Endpoint stalled */ - } - } - else - { - /* OUT endpoint */ - if (_GetRxStallStatus(Related_Endpoint)) - { - SetBit(StatusInfo0, 0); /* OUT Endpoint stalled */ - } - } - - } - else - { - return NULL; - } - pUser_Standard_Requests->User_GetStatus(); - return (u8 *)&StatusInfo; -} - -/******************************************************************************* -* Function Name : Standard_ClearFeature. -* Description : Clear or disable a specific feature. -* Input : None. -* Output : None. -* Return : - Return USB_SUCCESS, if the request is performed. -* - Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_ClearFeature(void) -{ - u32 Type_Rec = Type_Recipient; - u32 Status; - - - if (Type_Rec == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - {/*Device Clear Feature*/ - ClrBit(pInformation->Current_Feature, 5); - return USB_SUCCESS; - } - else if (Type_Rec == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) - {/*EndPoint Clear Feature*/ - DEVICE* pDev; - u32 Related_Endpoint; - u32 wIndex0; - u32 rEP; - - if ((pInformation->USBwValue != ENDPOINT_STALL) - || (pInformation->USBwIndex1 != 0)) - { - return USB_UNSUPPORT; - } - - pDev = &Device_Table; - wIndex0 = pInformation->USBwIndex0; - rEP = wIndex0 & ~0x80; - Related_Endpoint = ENDP0 + rEP; - - if (ValBit(pInformation->USBwIndex0, 7)) - { - /*Get Status of endpoint & stall the request if the related_ENdpoint - is Disabled*/ - Status = _GetEPTxStatus(Related_Endpoint); - } - else - { - Status = _GetEPRxStatus(Related_Endpoint); - } - - if ((rEP >= pDev->Total_Endpoint) || (Status == 0) - || (pInformation->Current_Configuration == 0)) - { - return USB_UNSUPPORT; - } - - - if (wIndex0 & 0x80) - { - /* IN endpoint */ - if (_GetTxStallStatus(Related_Endpoint )) - { - ClearDTOG_TX(Related_Endpoint); - SetEPTxStatus(Related_Endpoint, EP_TX_VALID); - } - } - else - { - /* OUT endpoint */ - if (_GetRxStallStatus(Related_Endpoint)) - { - if (Related_Endpoint == ENDP0) - { - /* After clear the STALL, enable the default endpoint receiver */ - SetEPRxCount(Related_Endpoint, Device_Property.MaxPacketSize); - _SetEPRxStatus(Related_Endpoint, EP_RX_VALID); - } - else - { - ClearDTOG_RX(Related_Endpoint); - _SetEPRxStatus(Related_Endpoint, EP_RX_VALID); - } - } - } - pUser_Standard_Requests->User_ClearFeature(); - return USB_SUCCESS; - } - - return USB_UNSUPPORT; -} - -/******************************************************************************* -* Function Name : Standard_SetEndPointFeature -* Description : Set or enable a specific feature of EndPoint -* Input : None. -* Output : None. -* Return : - Return USB_SUCCESS, if the request is performed. -* - Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_SetEndPointFeature(void) -{ - u32 wIndex0; - u32 Related_Endpoint; - u32 rEP; - u32 Status; - - wIndex0 = pInformation->USBwIndex0; - rEP = wIndex0 & ~0x80; - Related_Endpoint = ENDP0 + rEP; - - if (ValBit(pInformation->USBwIndex0, 7)) - { - /* get Status of endpoint & stall the request if the related_ENdpoint - is Disabled*/ - Status = _GetEPTxStatus(Related_Endpoint); - } - else - { - Status = _GetEPRxStatus(Related_Endpoint); - } - - if (Related_Endpoint >= Device_Table.Total_Endpoint - || pInformation->USBwValue != 0 || Status == 0 - || pInformation->Current_Configuration == 0) - { - return USB_UNSUPPORT; - } - else - { - if (wIndex0 & 0x80) - { - /* IN endpoint */ - _SetEPTxStatus(Related_Endpoint, EP_TX_STALL); - } - - else - { - /* OUT endpoint */ - _SetEPRxStatus(Related_Endpoint, EP_RX_STALL); - } - } - pUser_Standard_Requests->User_SetEndPointFeature(); - return USB_SUCCESS; -} - -/******************************************************************************* -* Function Name : Standard_SetDeviceFeature. -* Description : Set or enable a specific feature of Device. -* Input : None. -* Output : None. -* Return : - Return USB_SUCCESS, if the request is performed. -* - Return USB_UNSUPPORT, if the request is invalid. -*******************************************************************************/ -RESULT Standard_SetDeviceFeature(void) -{ - SetBit(pInformation->Current_Feature, 5); - pUser_Standard_Requests->User_SetDeviceFeature(); - return USB_SUCCESS; -} - -/******************************************************************************* -* Function Name : Standard_GetDescriptorData. -* Description : Standard_GetDescriptorData is used for descriptors transfer. -* : This routine is used for the descriptors resident in Flash -* or RAM -* pDesc can be in either Flash or RAM -* The purpose of this routine is to have a versatile way to -* response descriptors request. It allows user to generate -* certain descriptors with software or read descriptors from -* external storage part by part. -* Input : - Length - Length of the data in this transfer. -* - pDesc - A pointer points to descriptor struct. -* The structure gives the initial address of the descriptor and -* its original size. -* Output : None. -* Return : Address of a part of the descriptor pointed by the Usb_ -* wOffset The buffer pointed by this address contains at least -* Length bytes. -*******************************************************************************/ -u8 *Standard_GetDescriptorData(u16 Length, ONE_DESCRIPTOR *pDesc) -{ - u32 wOffset; - - wOffset = pInformation->Ctrl_Info.Usb_wOffset; - if (Length == 0) - { - pInformation->Ctrl_Info.Usb_wLength = pDesc->Descriptor_Size - wOffset; - return 0; - } - - return pDesc->Descriptor + wOffset; -} - -/******************************************************************************* -* Function Name : DataStageOut. -* Description : Data stage of a Control Write Transfer. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void DataStageOut(void) -{ - ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info; - u32 save_rLength; - - save_rLength = pEPinfo->Usb_rLength; - - if (pEPinfo->CopyData && save_rLength) - { - u8 *Buffer; - u32 Length; - - Length = pEPinfo->PacketSize; - if (Length > save_rLength) - { - Length = save_rLength; - } - - Buffer = (*pEPinfo->CopyData)(Length); - pEPinfo->Usb_rLength -= Length; - pEPinfo->Usb_rOffset += Length; - - PMAToUserBufferCopy(Buffer, GetEPRxAddr(ENDP0), Length); - } - - if (pEPinfo->Usb_rLength != 0) - { - vSetEPRxStatus(EP_RX_VALID);/* re-enable for next data reception */ - SetEPTxCount(ENDP0, 0); - vSetEPTxStatus(EP_TX_VALID);/* Expect the host to abort the data OUT stage */ - } - /* Set the next State*/ - if (pEPinfo->Usb_rLength >= pEPinfo->PacketSize) - { - pInformation->ControlState = OUT_DATA; - } - else - { - if (pEPinfo->Usb_rLength > 0) - { - pInformation->ControlState = LAST_OUT_DATA; - } - else if (pEPinfo->Usb_rLength == 0) - { - pInformation->ControlState = WAIT_STATUS_IN; - USB_StatusIn(); - } - } -} - -/******************************************************************************* -* Function Name : DataStageIn. -* Description : Data stage of a Control Read Transfer. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void DataStageIn(void) -{ - ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info; - u32 save_wLength = pEPinfo->Usb_wLength; - u32 ControlState = pInformation->ControlState; - - u8 *DataBuffer; - u32 Length; - - if ((save_wLength == 0) && (ControlState == LAST_IN_DATA)) - { - if(Data_Mul_MaxPacketSize == TRUE) - { - /* No more data to send and empty packet */ - Send0LengthData(); - ControlState = LAST_IN_DATA; - Data_Mul_MaxPacketSize = FALSE; - } - else - { - /* No more data to send so STALL the TX Status*/ - ControlState = WAIT_STATUS_OUT; - vSetEPTxStatus(EP_TX_STALL); - } - - goto Expect_Status_Out; - } - - Length = pEPinfo->PacketSize; - ControlState = (save_wLength <= Length) ? LAST_IN_DATA : IN_DATA; - - if (Length > save_wLength) - { - Length = save_wLength; - } - - DataBuffer = (*pEPinfo->CopyData)(Length); - - UserToPMABufferCopy(DataBuffer, GetEPTxAddr(ENDP0), Length); - - SetEPTxCount(ENDP0, Length); - - pEPinfo->Usb_wLength -= Length; - pEPinfo->Usb_wOffset += Length; - vSetEPTxStatus(EP_TX_VALID); - - USB_StatusOut();/* Expect the host to abort the data IN stage */ - -Expect_Status_Out: - pInformation->ControlState = ControlState; -} - -/******************************************************************************* -* Function Name : NoData_Setup0. -* Description : Proceed the processing of setup request without data stage. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void NoData_Setup0(void) -{ - RESULT Result = USB_UNSUPPORT; - u32 RequestNo = pInformation->USBbRequest; - u32 ControlState; - - if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - { - /* Device Request*/ - /* SET_CONFIGURATION*/ - if (RequestNo == SET_CONFIGURATION) - { - Result = Standard_SetConfiguration(); - } - - /*SET ADDRESS*/ - else if (RequestNo == SET_ADDRESS) - { - if ((pInformation->USBwValue0 > 127) || (pInformation->USBwValue1 != 0) - || (pInformation->USBwIndex != 0) - || (pInformation->Current_Configuration != 0)) - /* Device Address should be 127 or less*/ - { - ControlState = STALLED; - goto exit_NoData_Setup0; - } - else - { - Result = USB_SUCCESS; - } - } - /*SET FEATURE for Device*/ - else if (RequestNo == SET_FEATURE) - { - if ((pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP) - && (pInformation->USBwIndex == 0) - && (ValBit(pInformation->Current_Feature, 5))) - { - Result = Standard_SetDeviceFeature(); - } - else - { - Result = USB_UNSUPPORT; - } - } - /*Clear FEATURE for Device */ - else if (RequestNo == CLEAR_FEATURE) - { - if (pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP - && pInformation->USBwIndex == 0 - && ValBit(pInformation->Current_Feature, 5)) - { - Result = Standard_ClearFeature(); - } - else - { - Result = USB_UNSUPPORT; - } - } - - } - - /* Interface Request*/ - else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) - { - /*SET INTERFACE*/ - if (RequestNo == SET_INTERFACE) - { - Result = Standard_SetInterface(); - } - } - - /* EndPoint Request*/ - else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) - { - /*CLEAR FEATURE for EndPoint*/ - if (RequestNo == CLEAR_FEATURE) - { - Result = Standard_ClearFeature(); - } - /* SET FEATURE for EndPoint*/ - else if (RequestNo == SET_FEATURE) - { - Result = Standard_SetEndPointFeature(); - } - } - else - { - Result = USB_UNSUPPORT; - } - - - if (Result != USB_SUCCESS) - { - Result = (*pProperty->Class_NoData_Setup)(RequestNo); - if (Result == USB_NOT_READY) - { - ControlState = PAUSE; - goto exit_NoData_Setup0; - } - } - - if (Result != USB_SUCCESS) - { - ControlState = STALLED; - goto exit_NoData_Setup0; - } - - ControlState = WAIT_STATUS_IN;/* After no data stage SETUP */ - - USB_StatusIn(); - -exit_NoData_Setup0: - pInformation->ControlState = ControlState; - return; -} - -/******************************************************************************* -* Function Name : Data_Setup0. -* Description : Proceed the processing of setup request with data stage. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void Data_Setup0(void) -{ - u8 *(*CopyRoutine)(u16); - RESULT Result; - u32 Request_No = pInformation->USBbRequest; - - u32 Related_Endpoint, Reserved; - u32 wOffset, Status; - - - - CopyRoutine = NULL; - wOffset = 0; - - if (Request_No == GET_DESCRIPTOR) - { - if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - { - u8 wValue1 = pInformation->USBwValue1; - if (wValue1 == DEVICE_DESCRIPTOR) - { - CopyRoutine = pProperty->GetDeviceDescriptor; - } - else if (wValue1 == CONFIG_DESCRIPTOR) - { - CopyRoutine = pProperty->GetConfigDescriptor; - } - else if (wValue1 == STRING_DESCRIPTOR) - { - CopyRoutine = pProperty->GetStringDescriptor; - } else if (wValue1 == 0x21) /* added to support functional descriptors */ - { - CopyRoutine = pProperty->GetFunctionalDescriptor; - } /* End of GET_DESCRIPTOR */ - } - } - - /*GET STATUS*/ - else if ((Request_No == GET_STATUS) && (pInformation->USBwValue == 0) - && (pInformation->USBwLength == 0x0002) - && (pInformation->USBwIndex1 == 0)) - { - /* GET STATUS for Device*/ - if ((Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - && (pInformation->USBwIndex == 0)) - { - CopyRoutine = Standard_GetStatus; - } - - /* GET STATUS for Interface*/ - else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) - { - if (((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS) - && (pInformation->Current_Configuration != 0)) - { - CopyRoutine = Standard_GetStatus; - } - } - - /* GET STATUS for EndPoint*/ - else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) - { - Related_Endpoint = (pInformation->USBwIndex0 & 0x0f); - Reserved = pInformation->USBwIndex0 & 0x70; - - if (ValBit(pInformation->USBwIndex0, 7)) - { - /*Get Status of endpoint & stall the request if the related_ENdpoint - is Disabled*/ - Status = _GetEPTxStatus(Related_Endpoint); - } - else - { - Status = _GetEPRxStatus(Related_Endpoint); - } - - if ((Related_Endpoint < Device_Table.Total_Endpoint) && (Reserved == 0) - && (Status != 0)) - { - CopyRoutine = Standard_GetStatus; - } - } - - } - - /*GET CONFIGURATION*/ - else if (Request_No == GET_CONFIGURATION) - { - if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) - { - CopyRoutine = Standard_GetConfiguration; - } - } - /*GET INTERFACE*/ - else if (Request_No == GET_INTERFACE) - { - if ((Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) - && (pInformation->Current_Configuration != 0) && (pInformation->USBwValue == 0) - && (pInformation->USBwIndex1 == 0) && (pInformation->USBwLength == 0x0001) - && ((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS)) - { - CopyRoutine = Standard_GetInterface; - } - - } - - if (CopyRoutine) - { - pInformation->Ctrl_Info.Usb_wOffset = wOffset; - pInformation->Ctrl_Info.CopyData = CopyRoutine; - /* sb in the original the cast to word was directly */ - /* now the cast is made step by step */ - (*CopyRoutine)(0); - Result = USB_SUCCESS; - } - else - { - Result = (*pProperty->Class_Data_Setup)(pInformation->USBbRequest); - if (Result == USB_NOT_READY) - { - pInformation->ControlState = PAUSE; - return; - } - } - - if (pInformation->Ctrl_Info.Usb_wLength == 0xFFFF) - { - /* Data is not ready, wait it */ - pInformation->ControlState = PAUSE; - return; - } - if ((Result == USB_UNSUPPORT) || (pInformation->Ctrl_Info.Usb_wLength == 0)) - { - /* Unsupported request */ - pInformation->ControlState = STALLED; - return; - } - - - if (ValBit(pInformation->USBbmRequestType, 7)) - { - /* Device ==> Host */ - vu32 wLength = pInformation->USBwLength; - - /* Restrict the data length to be the one host asks */ - if (pInformation->Ctrl_Info.Usb_wLength > wLength) - { - pInformation->Ctrl_Info.Usb_wLength = wLength; - } - - else if (pInformation->Ctrl_Info.Usb_wLength < pInformation->USBwLength) - { - if (pInformation->Ctrl_Info.Usb_wLength < pProperty->MaxPacketSize) - { - Data_Mul_MaxPacketSize = FALSE; - } - else if ((pInformation->Ctrl_Info.Usb_wLength % pProperty->MaxPacketSize) == 0) - { - Data_Mul_MaxPacketSize = TRUE; - } - } - - pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize; - DataStageIn(); - } - else - { - pInformation->ControlState = OUT_DATA; - vSetEPRxStatus(EP_RX_VALID); /* enable for next data reception */ - } - - return; -} - -/******************************************************************************* -* Function Name : Setup0_Process -* Description : Get the device request data and dispatch to individual process. -* Input : None. -* Output : None. -* Return : Post0_Process. -*******************************************************************************/ -u8 Setup0_Process(void) -{ - - union - { - u8* b; - u16* w; - } pBuf; - - pBuf.b = PMAAddr + (u8 *)(_GetEPRxAddr(ENDP0) * 2); /* *2 for 32 bits addr */ - - if (pInformation->ControlState != PAUSE) - { - pInformation->USBbmRequestType = *pBuf.b++; /* bmRequestType */ - pInformation->USBbRequest = *pBuf.b++; /* bRequest */ - pBuf.w++; /* word not accessed because of 32 bits addressing */ - pInformation->USBwValue = ByteSwap(*pBuf.w++); /* wValue */ - pBuf.w++; /* word not accessed because of 32 bits addressing */ - pInformation->USBwIndex = ByteSwap(*pBuf.w++); /* wIndex */ - pBuf.w++; /* word not accessed because of 32 bits addressing */ - pInformation->USBwLength = *pBuf.w; /* wLength */ - } - - pInformation->ControlState = SETTING_UP; - if (pInformation->USBwLength == 0) - { - /* Setup with no data stage */ - NoData_Setup0(); - } - else - { - /* Setup with data stage */ - Data_Setup0(); - } - return Post0_Process(); -} - -/******************************************************************************* -* Function Name : In0_Process -* Description : Process the IN token on all default endpoint. -* Input : None. -* Output : None. -* Return : Post0_Process. -*******************************************************************************/ -u8 In0_Process(void) -{ - u32 ControlState = pInformation->ControlState; - - if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA)) - { - DataStageIn(); - /* ControlState may be changed outside the function */ - ControlState = pInformation->ControlState; - } - - else if (ControlState == WAIT_STATUS_IN) - { - if ((pInformation->USBbRequest == SET_ADDRESS) && - (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))) - { - SetDeviceAddress(pInformation->USBwValue0); - pUser_Standard_Requests->User_SetDeviceAddress(); - } - (*pProperty->Process_Status_IN)(); - ControlState = STALLED; - } - - else - { - ControlState = STALLED; - } - - pInformation->ControlState = ControlState; - - return Post0_Process(); -} - -/******************************************************************************* -* Function Name : Out0_Process -* Description : Process the OUT token on all default endpoint. -* Input : None. -* Output : None. -* Return : Post0_Process. -*******************************************************************************/ -u8 Out0_Process(void) -{ - u32 ControlState = pInformation->ControlState; - - if ((ControlState == OUT_DATA) || (ControlState == LAST_OUT_DATA)) - { - DataStageOut(); - ControlState = pInformation->ControlState; /* may be changed outside the function */ - } - - else if (ControlState == WAIT_STATUS_OUT) - { - (*pProperty->Process_Status_OUT)(); - ControlState = STALLED; - } - - else if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA)) - { - /* host aborts the transfer before finish */ - ControlState = STALLED; - } - - /* Unexpect state, STALL the endpoint */ - else - { - ControlState = STALLED; - } - - pInformation->ControlState = ControlState; - - return Post0_Process(); -} - -/******************************************************************************* -* Function Name : Post0_Process -* Description : Stall the Endpoint 0 in case of error. -* Input : None. -* Output : None. -* Return : - 0 if the control State is in PAUSE -* - 1 if not. -*******************************************************************************/ -u8 Post0_Process(void) -{ - SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); - - if (pInformation->ControlState == STALLED) - { - vSetEPRxStatus(EP_RX_STALL); - vSetEPTxStatus(EP_TX_STALL); - } - - return (pInformation->ControlState == PAUSE); -} - -/******************************************************************************* -* Function Name : SetDeviceAddress. -* Description : Set the device and all the used Endpoints addresses. -* Input : - Val: device adress. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetDeviceAddress(u8 Val) -{ - u32 i; - u32 nEP = Device_Table.Total_Endpoint; - - /* set address in every used endpoint */ - for (i = 0; i < nEP; i++) - { - _SetEPAddress((u8)i, (u8)i); - } /* for */ - _SetDADDR(Val | DADDR_EF); /* set device address and enable function */ -} - -/******************************************************************************* -* Function Name : NOP_Process -* Description : No operation function. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void NOP_Process(void) -{ -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_core.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Standard protocol processing (USB v2.0) +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +#define ValBit(VAR,Place) (VAR & (1 << Place)) +#define SetBit(VAR,Place) (VAR |= (1 << Place)) +#define ClrBit(VAR,Place) (VAR &= ((1 << Place) ^ 255)) + +#define Send0LengthData() { _SetEPTxCount(ENDP0, 0); \ + vSetEPTxStatus(EP_TX_VALID); \ + } + +#define vSetEPRxStatus(st) (SaveRState = st) +#define vSetEPTxStatus(st) (SaveTState = st) + +#define USB_StatusIn() Send0LengthData() +#define USB_StatusOut() vSetEPRxStatus(EP_RX_VALID) + +#define StatusInfo0 StatusInfo.bw.bb1 /* Reverse bb0 & bb1 */ +#define StatusInfo1 StatusInfo.bw.bb0 + +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +u16_u8 StatusInfo; +bool Data_Mul_MaxPacketSize = FALSE; +/* Private function prototypes -----------------------------------------------*/ +static void DataStageOut(void); +static void DataStageIn(void); +static void NoData_Setup0(void); +static void Data_Setup0(void); +/* Private functions ---------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : Standard_GetConfiguration. +* Description : Return the current configuration variable address. +* Input : Length - How many bytes are needed. +* Output : None. +* Return : Return 1 , if the request is invalid when "Length" is 0. +* Return "Buffer" if the "Length" is not 0. +*******************************************************************************/ +u8 *Standard_GetConfiguration(u16 Length) +{ + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = + sizeof(pInformation->Current_Configuration); + return 0; + } + pUser_Standard_Requests->User_GetConfiguration(); + return (u8 *)&pInformation->Current_Configuration; +} + +/******************************************************************************* +* Function Name : Standard_SetConfiguration. +* Description : This routine is called to set the configuration value +* Then each class should configure device themself. +* Input : None. +* Output : None. +* Return : Return USB_SUCCESS, if the request is performed. +* Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_SetConfiguration(void) +{ + + if ((pInformation->USBwValue0 <= + Device_Table.Total_Configuration) && (pInformation->USBwValue1 == 0) + && (pInformation->USBwIndex == 0)) /*call Back usb spec 2.0*/ + { + pInformation->Current_Configuration = pInformation->USBwValue0; + pUser_Standard_Requests->User_SetConfiguration(); + return USB_SUCCESS; + } + else + { + return USB_UNSUPPORT; + } +} + +/******************************************************************************* +* Function Name : Standard_GetInterface. +* Description : Return the Alternate Setting of the current interface. +* Input : Length - How many bytes are needed. +* Output : None. +* Return : Return 0, if the request is invalid when "Length" is 0. +* Return "Buffer" if the "Length" is not 0. +*******************************************************************************/ +u8 *Standard_GetInterface(u16 Length) +{ + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = + sizeof(pInformation->Current_AlternateSetting); + return 0; + } + pUser_Standard_Requests->User_GetInterface(); + return (u8 *)&pInformation->Current_AlternateSetting; +} + +/******************************************************************************* +* Function Name : Standard_SetInterface. +* Description : This routine is called to set the interface. +* Then each class should configure the interface them self. +* Input : None. +* Output : None. +* Return : - Return USB_SUCCESS, if the request is performed. +* - Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_SetInterface(void) +{ + RESULT Re; + /*Test if the specified Interface and Alternate Setting are supported by + the application Firmware*/ + Re = (*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, pInformation->USBwValue0); + + if (pInformation->Current_Configuration != 0) + { + if ((Re != USB_SUCCESS) || (pInformation->USBwIndex1 != 0) + || (pInformation->USBwValue1 != 0)) + { + return USB_UNSUPPORT; + } + else if (Re == USB_SUCCESS) + { + pUser_Standard_Requests->User_SetInterface(); + pInformation->Current_Interface = pInformation->USBwIndex0; + pInformation->Current_AlternateSetting = pInformation->USBwValue0; + return USB_SUCCESS; + } + + } + + return USB_UNSUPPORT; +} + +/******************************************************************************* +* Function Name : Standard_GetStatus. +* Description : Copy the device request data to "StatusInfo buffer". +* Input : - Length - How many bytes are needed. +* Output : None. +* Return : Return 0, if the request is at end of data block, +* or is invalid when "Length" is 0. +*******************************************************************************/ +u8 *Standard_GetStatus(u16 Length) +{ + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = 2; + return 0; + } + + StatusInfo.w = 0; + /* Reset Status Information */ + + if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + { + /*Get Device Status */ + u8 Feature = pInformation->Current_Feature; + + /* Remote Wakeup enabled */ + if (ValBit(Feature, 5)) + { + SetBit(StatusInfo0, 1); + } + + /* Bus-powered */ + if (ValBit(Feature, 6)) + { + ClrBit(StatusInfo0, 0); + } + else /* Self-powered */ + { + SetBit(StatusInfo0, 0); + } + } + /*Interface Status*/ + else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) + { + return (u8 *)&StatusInfo; + } + /*Get EndPoint Status*/ + else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) + { + u8 Related_Endpoint; + u8 wIndex0 = pInformation->USBwIndex0; + + Related_Endpoint = (wIndex0 & 0x0f); + if (ValBit(wIndex0, 7)) + { + /* IN endpoint */ + if (_GetTxStallStatus(Related_Endpoint)) + { + SetBit(StatusInfo0, 0); /* IN Endpoint stalled */ + } + } + else + { + /* OUT endpoint */ + if (_GetRxStallStatus(Related_Endpoint)) + { + SetBit(StatusInfo0, 0); /* OUT Endpoint stalled */ + } + } + + } + else + { + return NULL; + } + pUser_Standard_Requests->User_GetStatus(); + return (u8 *)&StatusInfo; +} + +/******************************************************************************* +* Function Name : Standard_ClearFeature. +* Description : Clear or disable a specific feature. +* Input : None. +* Output : None. +* Return : - Return USB_SUCCESS, if the request is performed. +* - Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_ClearFeature(void) +{ + u32 Type_Rec = Type_Recipient; + u32 Status; + + + if (Type_Rec == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + {/*Device Clear Feature*/ + ClrBit(pInformation->Current_Feature, 5); + return USB_SUCCESS; + } + else if (Type_Rec == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) + {/*EndPoint Clear Feature*/ + DEVICE* pDev; + u32 Related_Endpoint; + u32 wIndex0; + u32 rEP; + + if ((pInformation->USBwValue != ENDPOINT_STALL) + || (pInformation->USBwIndex1 != 0)) + { + return USB_UNSUPPORT; + } + + pDev = &Device_Table; + wIndex0 = pInformation->USBwIndex0; + rEP = wIndex0 & ~0x80; + Related_Endpoint = ENDP0 + rEP; + + if (ValBit(pInformation->USBwIndex0, 7)) + { + /*Get Status of endpoint & stall the request if the related_ENdpoint + is Disabled*/ + Status = _GetEPTxStatus(Related_Endpoint); + } + else + { + Status = _GetEPRxStatus(Related_Endpoint); + } + + if ((rEP >= pDev->Total_Endpoint) || (Status == 0) + || (pInformation->Current_Configuration == 0)) + { + return USB_UNSUPPORT; + } + + + if (wIndex0 & 0x80) + { + /* IN endpoint */ + if (_GetTxStallStatus(Related_Endpoint )) + { + ClearDTOG_TX(Related_Endpoint); + SetEPTxStatus(Related_Endpoint, EP_TX_VALID); + } + } + else + { + /* OUT endpoint */ + if (_GetRxStallStatus(Related_Endpoint)) + { + if (Related_Endpoint == ENDP0) + { + /* After clear the STALL, enable the default endpoint receiver */ + SetEPRxCount(Related_Endpoint, Device_Property.MaxPacketSize); + _SetEPRxStatus(Related_Endpoint, EP_RX_VALID); + } + else + { + ClearDTOG_RX(Related_Endpoint); + _SetEPRxStatus(Related_Endpoint, EP_RX_VALID); + } + } + } + pUser_Standard_Requests->User_ClearFeature(); + return USB_SUCCESS; + } + + return USB_UNSUPPORT; +} + +/******************************************************************************* +* Function Name : Standard_SetEndPointFeature +* Description : Set or enable a specific feature of EndPoint +* Input : None. +* Output : None. +* Return : - Return USB_SUCCESS, if the request is performed. +* - Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_SetEndPointFeature(void) +{ + u32 wIndex0; + u32 Related_Endpoint; + u32 rEP; + u32 Status; + + wIndex0 = pInformation->USBwIndex0; + rEP = wIndex0 & ~0x80; + Related_Endpoint = ENDP0 + rEP; + + if (ValBit(pInformation->USBwIndex0, 7)) + { + /* get Status of endpoint & stall the request if the related_ENdpoint + is Disabled*/ + Status = _GetEPTxStatus(Related_Endpoint); + } + else + { + Status = _GetEPRxStatus(Related_Endpoint); + } + + if (Related_Endpoint >= Device_Table.Total_Endpoint + || pInformation->USBwValue != 0 || Status == 0 + || pInformation->Current_Configuration == 0) + { + return USB_UNSUPPORT; + } + else + { + if (wIndex0 & 0x80) + { + /* IN endpoint */ + _SetEPTxStatus(Related_Endpoint, EP_TX_STALL); + } + + else + { + /* OUT endpoint */ + _SetEPRxStatus(Related_Endpoint, EP_RX_STALL); + } + } + pUser_Standard_Requests->User_SetEndPointFeature(); + return USB_SUCCESS; +} + +/******************************************************************************* +* Function Name : Standard_SetDeviceFeature. +* Description : Set or enable a specific feature of Device. +* Input : None. +* Output : None. +* Return : - Return USB_SUCCESS, if the request is performed. +* - Return USB_UNSUPPORT, if the request is invalid. +*******************************************************************************/ +RESULT Standard_SetDeviceFeature(void) +{ + SetBit(pInformation->Current_Feature, 5); + pUser_Standard_Requests->User_SetDeviceFeature(); + return USB_SUCCESS; +} + +/******************************************************************************* +* Function Name : Standard_GetDescriptorData. +* Description : Standard_GetDescriptorData is used for descriptors transfer. +* : This routine is used for the descriptors resident in Flash +* or RAM +* pDesc can be in either Flash or RAM +* The purpose of this routine is to have a versatile way to +* response descriptors request. It allows user to generate +* certain descriptors with software or read descriptors from +* external storage part by part. +* Input : - Length - Length of the data in this transfer. +* - pDesc - A pointer points to descriptor struct. +* The structure gives the initial address of the descriptor and +* its original size. +* Output : None. +* Return : Address of a part of the descriptor pointed by the Usb_ +* wOffset The buffer pointed by this address contains at least +* Length bytes. +*******************************************************************************/ +u8 *Standard_GetDescriptorData(u16 Length, ONE_DESCRIPTOR *pDesc) +{ + u32 wOffset; + + wOffset = pInformation->Ctrl_Info.Usb_wOffset; + if (Length == 0) + { + pInformation->Ctrl_Info.Usb_wLength = pDesc->Descriptor_Size - wOffset; + return 0; + } + + return pDesc->Descriptor + wOffset; +} + +/******************************************************************************* +* Function Name : DataStageOut. +* Description : Data stage of a Control Write Transfer. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void DataStageOut(void) +{ + ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info; + u32 save_rLength; + + save_rLength = pEPinfo->Usb_rLength; + + if (pEPinfo->CopyData && save_rLength) + { + u8 *Buffer; + u32 Length; + + Length = pEPinfo->PacketSize; + if (Length > save_rLength) + { + Length = save_rLength; + } + + Buffer = (*pEPinfo->CopyData)(Length); + pEPinfo->Usb_rLength -= Length; + pEPinfo->Usb_rOffset += Length; + + PMAToUserBufferCopy(Buffer, GetEPRxAddr(ENDP0), Length); + } + + if (pEPinfo->Usb_rLength != 0) + { + vSetEPRxStatus(EP_RX_VALID);/* re-enable for next data reception */ + SetEPTxCount(ENDP0, 0); + vSetEPTxStatus(EP_TX_VALID);/* Expect the host to abort the data OUT stage */ + } + /* Set the next State*/ + if (pEPinfo->Usb_rLength >= pEPinfo->PacketSize) + { + pInformation->ControlState = OUT_DATA; + } + else + { + if (pEPinfo->Usb_rLength > 0) + { + pInformation->ControlState = LAST_OUT_DATA; + } + else if (pEPinfo->Usb_rLength == 0) + { + pInformation->ControlState = WAIT_STATUS_IN; + USB_StatusIn(); + } + } +} + +/******************************************************************************* +* Function Name : DataStageIn. +* Description : Data stage of a Control Read Transfer. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void DataStageIn(void) +{ + ENDPOINT_INFO *pEPinfo = &pInformation->Ctrl_Info; + u32 save_wLength = pEPinfo->Usb_wLength; + u32 ControlState = pInformation->ControlState; + + u8 *DataBuffer; + u32 Length; + + if ((save_wLength == 0) && (ControlState == LAST_IN_DATA)) + { + if(Data_Mul_MaxPacketSize == TRUE) + { + /* No more data to send and empty packet */ + Send0LengthData(); + ControlState = LAST_IN_DATA; + Data_Mul_MaxPacketSize = FALSE; + } + else + { + /* No more data to send so STALL the TX Status*/ + ControlState = WAIT_STATUS_OUT; + vSetEPTxStatus(EP_TX_STALL); + } + + goto Expect_Status_Out; + } + + Length = pEPinfo->PacketSize; + ControlState = (save_wLength <= Length) ? LAST_IN_DATA : IN_DATA; + + if (Length > save_wLength) + { + Length = save_wLength; + } + + DataBuffer = (*pEPinfo->CopyData)(Length); + + UserToPMABufferCopy(DataBuffer, GetEPTxAddr(ENDP0), Length); + + SetEPTxCount(ENDP0, Length); + + pEPinfo->Usb_wLength -= Length; + pEPinfo->Usb_wOffset += Length; + vSetEPTxStatus(EP_TX_VALID); + + USB_StatusOut();/* Expect the host to abort the data IN stage */ + +Expect_Status_Out: + pInformation->ControlState = ControlState; +} + +/******************************************************************************* +* Function Name : NoData_Setup0. +* Description : Proceed the processing of setup request without data stage. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void NoData_Setup0(void) +{ + RESULT Result = USB_UNSUPPORT; + u32 RequestNo = pInformation->USBbRequest; + u32 ControlState; + + if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + { + /* Device Request*/ + /* SET_CONFIGURATION*/ + if (RequestNo == SET_CONFIGURATION) + { + Result = Standard_SetConfiguration(); + } + + /*SET ADDRESS*/ + else if (RequestNo == SET_ADDRESS) + { + if ((pInformation->USBwValue0 > 127) || (pInformation->USBwValue1 != 0) + || (pInformation->USBwIndex != 0) + || (pInformation->Current_Configuration != 0)) + /* Device Address should be 127 or less*/ + { + ControlState = STALLED; + goto exit_NoData_Setup0; + } + else + { + Result = USB_SUCCESS; + } + } + /*SET FEATURE for Device*/ + else if (RequestNo == SET_FEATURE) + { + if ((pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP) + && (pInformation->USBwIndex == 0) + && (ValBit(pInformation->Current_Feature, 5))) + { + Result = Standard_SetDeviceFeature(); + } + else + { + Result = USB_UNSUPPORT; + } + } + /*Clear FEATURE for Device */ + else if (RequestNo == CLEAR_FEATURE) + { + if (pInformation->USBwValue0 == DEVICE_REMOTE_WAKEUP + && pInformation->USBwIndex == 0 + && ValBit(pInformation->Current_Feature, 5)) + { + Result = Standard_ClearFeature(); + } + else + { + Result = USB_UNSUPPORT; + } + } + + } + + /* Interface Request*/ + else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) + { + /*SET INTERFACE*/ + if (RequestNo == SET_INTERFACE) + { + Result = Standard_SetInterface(); + } + } + + /* EndPoint Request*/ + else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) + { + /*CLEAR FEATURE for EndPoint*/ + if (RequestNo == CLEAR_FEATURE) + { + Result = Standard_ClearFeature(); + } + /* SET FEATURE for EndPoint*/ + else if (RequestNo == SET_FEATURE) + { + Result = Standard_SetEndPointFeature(); + } + } + else + { + Result = USB_UNSUPPORT; + } + + + if (Result != USB_SUCCESS) + { + Result = (*pProperty->Class_NoData_Setup)(RequestNo); + if (Result == USB_NOT_READY) + { + ControlState = PAUSE; + goto exit_NoData_Setup0; + } + } + + if (Result != USB_SUCCESS) + { + ControlState = STALLED; + goto exit_NoData_Setup0; + } + + ControlState = WAIT_STATUS_IN;/* After no data stage SETUP */ + + USB_StatusIn(); + +exit_NoData_Setup0: + pInformation->ControlState = ControlState; + return; +} + +/******************************************************************************* +* Function Name : Data_Setup0. +* Description : Proceed the processing of setup request with data stage. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void Data_Setup0(void) +{ + u8 *(*CopyRoutine)(u16); + RESULT Result; + u32 Request_No = pInformation->USBbRequest; + + u32 Related_Endpoint, Reserved; + u32 wOffset, Status; + + + + CopyRoutine = NULL; + wOffset = 0; + + if (Request_No == GET_DESCRIPTOR) + { + if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + { + u8 wValue1 = pInformation->USBwValue1; + if (wValue1 == DEVICE_DESCRIPTOR) + { + CopyRoutine = pProperty->GetDeviceDescriptor; + } + else if (wValue1 == CONFIG_DESCRIPTOR) + { + CopyRoutine = pProperty->GetConfigDescriptor; + } + else if (wValue1 == STRING_DESCRIPTOR) + { + CopyRoutine = pProperty->GetStringDescriptor; + } else if (wValue1 == 0x21) /* added to support functional descriptors */ + { + CopyRoutine = pProperty->GetFunctionalDescriptor; + } /* End of GET_DESCRIPTOR */ + } + } + + /*GET STATUS*/ + else if ((Request_No == GET_STATUS) && (pInformation->USBwValue == 0) + && (pInformation->USBwLength == 0x0002) + && (pInformation->USBwIndex1 == 0)) + { + /* GET STATUS for Device*/ + if ((Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + && (pInformation->USBwIndex == 0)) + { + CopyRoutine = Standard_GetStatus; + } + + /* GET STATUS for Interface*/ + else if (Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) + { + if (((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS) + && (pInformation->Current_Configuration != 0)) + { + CopyRoutine = Standard_GetStatus; + } + } + + /* GET STATUS for EndPoint*/ + else if (Type_Recipient == (STANDARD_REQUEST | ENDPOINT_RECIPIENT)) + { + Related_Endpoint = (pInformation->USBwIndex0 & 0x0f); + Reserved = pInformation->USBwIndex0 & 0x70; + + if (ValBit(pInformation->USBwIndex0, 7)) + { + /*Get Status of endpoint & stall the request if the related_ENdpoint + is Disabled*/ + Status = _GetEPTxStatus(Related_Endpoint); + } + else + { + Status = _GetEPRxStatus(Related_Endpoint); + } + + if ((Related_Endpoint < Device_Table.Total_Endpoint) && (Reserved == 0) + && (Status != 0)) + { + CopyRoutine = Standard_GetStatus; + } + } + + } + + /*GET CONFIGURATION*/ + else if (Request_No == GET_CONFIGURATION) + { + if (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT)) + { + CopyRoutine = Standard_GetConfiguration; + } + } + /*GET INTERFACE*/ + else if (Request_No == GET_INTERFACE) + { + if ((Type_Recipient == (STANDARD_REQUEST | INTERFACE_RECIPIENT)) + && (pInformation->Current_Configuration != 0) && (pInformation->USBwValue == 0) + && (pInformation->USBwIndex1 == 0) && (pInformation->USBwLength == 0x0001) + && ((*pProperty->Class_Get_Interface_Setting)(pInformation->USBwIndex0, 0) == USB_SUCCESS)) + { + CopyRoutine = Standard_GetInterface; + } + + } + + if (CopyRoutine) + { + pInformation->Ctrl_Info.Usb_wOffset = wOffset; + pInformation->Ctrl_Info.CopyData = CopyRoutine; + /* sb in the original the cast to word was directly */ + /* now the cast is made step by step */ + (*CopyRoutine)(0); + Result = USB_SUCCESS; + } + else + { + Result = (*pProperty->Class_Data_Setup)(pInformation->USBbRequest); + if (Result == USB_NOT_READY) + { + pInformation->ControlState = PAUSE; + return; + } + } + + if (pInformation->Ctrl_Info.Usb_wLength == 0xFFFF) + { + /* Data is not ready, wait it */ + pInformation->ControlState = PAUSE; + return; + } + if ((Result == USB_UNSUPPORT) || (pInformation->Ctrl_Info.Usb_wLength == 0)) + { + /* Unsupported request */ + pInformation->ControlState = STALLED; + return; + } + + + if (ValBit(pInformation->USBbmRequestType, 7)) + { + /* Device ==> Host */ + vu32 wLength = pInformation->USBwLength; + + /* Restrict the data length to be the one host asks */ + if (pInformation->Ctrl_Info.Usb_wLength > wLength) + { + pInformation->Ctrl_Info.Usb_wLength = wLength; + } + + else if (pInformation->Ctrl_Info.Usb_wLength < pInformation->USBwLength) + { + if (pInformation->Ctrl_Info.Usb_wLength < pProperty->MaxPacketSize) + { + Data_Mul_MaxPacketSize = FALSE; + } + else if ((pInformation->Ctrl_Info.Usb_wLength % pProperty->MaxPacketSize) == 0) + { + Data_Mul_MaxPacketSize = TRUE; + } + } + + pInformation->Ctrl_Info.PacketSize = pProperty->MaxPacketSize; + DataStageIn(); + } + else + { + pInformation->ControlState = OUT_DATA; + vSetEPRxStatus(EP_RX_VALID); /* enable for next data reception */ + } + + return; +} + +/******************************************************************************* +* Function Name : Setup0_Process +* Description : Get the device request data and dispatch to individual process. +* Input : None. +* Output : None. +* Return : Post0_Process. +*******************************************************************************/ +u8 Setup0_Process(void) +{ + + union + { + u8* b; + u16* w; + } pBuf; + + pBuf.b = PMAAddr + (u8 *)(_GetEPRxAddr(ENDP0) * 2); /* *2 for 32 bits addr */ + + if (pInformation->ControlState != PAUSE) + { + pInformation->USBbmRequestType = *pBuf.b++; /* bmRequestType */ + pInformation->USBbRequest = *pBuf.b++; /* bRequest */ + pBuf.w++; /* word not accessed because of 32 bits addressing */ + pInformation->USBwValue = ByteSwap(*pBuf.w++); /* wValue */ + pBuf.w++; /* word not accessed because of 32 bits addressing */ + pInformation->USBwIndex = ByteSwap(*pBuf.w++); /* wIndex */ + pBuf.w++; /* word not accessed because of 32 bits addressing */ + pInformation->USBwLength = *pBuf.w; /* wLength */ + } + + pInformation->ControlState = SETTING_UP; + if (pInformation->USBwLength == 0) + { + /* Setup with no data stage */ + NoData_Setup0(); + } + else + { + /* Setup with data stage */ + Data_Setup0(); + } + return Post0_Process(); +} + +/******************************************************************************* +* Function Name : In0_Process +* Description : Process the IN token on all default endpoint. +* Input : None. +* Output : None. +* Return : Post0_Process. +*******************************************************************************/ +u8 In0_Process(void) +{ + u32 ControlState = pInformation->ControlState; + + if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA)) + { + DataStageIn(); + /* ControlState may be changed outside the function */ + ControlState = pInformation->ControlState; + } + + else if (ControlState == WAIT_STATUS_IN) + { + if ((pInformation->USBbRequest == SET_ADDRESS) && + (Type_Recipient == (STANDARD_REQUEST | DEVICE_RECIPIENT))) + { + SetDeviceAddress(pInformation->USBwValue0); + pUser_Standard_Requests->User_SetDeviceAddress(); + } + (*pProperty->Process_Status_IN)(); + ControlState = STALLED; + } + + else + { + ControlState = STALLED; + } + + pInformation->ControlState = ControlState; + + return Post0_Process(); +} + +/******************************************************************************* +* Function Name : Out0_Process +* Description : Process the OUT token on all default endpoint. +* Input : None. +* Output : None. +* Return : Post0_Process. +*******************************************************************************/ +u8 Out0_Process(void) +{ + u32 ControlState = pInformation->ControlState; + + if ((ControlState == OUT_DATA) || (ControlState == LAST_OUT_DATA)) + { + DataStageOut(); + ControlState = pInformation->ControlState; /* may be changed outside the function */ + } + + else if (ControlState == WAIT_STATUS_OUT) + { + (*pProperty->Process_Status_OUT)(); + ControlState = STALLED; + } + + else if ((ControlState == IN_DATA) || (ControlState == LAST_IN_DATA)) + { + /* host aborts the transfer before finish */ + ControlState = STALLED; + } + + /* Unexpect state, STALL the endpoint */ + else + { + ControlState = STALLED; + } + + pInformation->ControlState = ControlState; + + return Post0_Process(); +} + +/******************************************************************************* +* Function Name : Post0_Process +* Description : Stall the Endpoint 0 in case of error. +* Input : None. +* Output : None. +* Return : - 0 if the control State is in PAUSE +* - 1 if not. +*******************************************************************************/ +u8 Post0_Process(void) +{ + SetEPRxCount(ENDP0, Device_Property.MaxPacketSize); + + if (pInformation->ControlState == STALLED) + { + vSetEPRxStatus(EP_RX_STALL); + vSetEPTxStatus(EP_TX_STALL); + } + + return (pInformation->ControlState == PAUSE); +} + +/******************************************************************************* +* Function Name : SetDeviceAddress. +* Description : Set the device and all the used Endpoints addresses. +* Input : - Val: device adress. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetDeviceAddress(u8 Val) +{ + u32 i; + u32 nEP = Device_Table.Total_Endpoint; + + /* set address in every used endpoint */ + for (i = 0; i < nEP; i++) + { + _SetEPAddress((u8)i, (u8)i); + } /* for */ + _SetDADDR(Val | DADDR_EF); /* set device address and enable function */ +} + +/******************************************************************************* +* Function Name : NOP_Process +* Description : No operation function. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void NOP_Process(void) +{ +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_core.h b/src/usb_lib/usb_core.h similarity index 97% rename from usb_lib/usb_core.h rename to src/usb_lib/usb_core.h index efeaad8..b7bc4c7 100644 --- a/usb_lib/usb_core.h +++ b/src/usb_lib/usb_core.h @@ -1,244 +1,244 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_core.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Standard protocol processing functions prototypes -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_CORE_H -#define __USB_CORE_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -typedef enum _CONTROL_STATE -{ - WAIT_SETUP, /* 0 */ - SETTING_UP, /* 1 */ - IN_DATA, /* 2 */ - OUT_DATA, /* 3 */ - LAST_IN_DATA, /* 4 */ - LAST_OUT_DATA, /* 5 */ - WAIT_STATUS_IN, /* 7 */ - WAIT_STATUS_OUT, /* 8 */ - STALLED, /* 9 */ - PAUSE /* 10 */ -} CONTROL_STATE; /* The state machine states of a control pipe */ - -typedef struct OneDescriptor -{ - u8 *Descriptor; - u16 Descriptor_Size; -} -ONE_DESCRIPTOR, *PONE_DESCRIPTOR; -/* All the request process routines return a value of this type - If the return value is not SUCCESS or NOT_READY, - the software will STALL the correspond endpoint */ -typedef enum _RESULT -{ - USB_SUCCESS = 0, /* Process sucessfully */ - USB_ERROR, - USB_UNSUPPORT, - USB_NOT_READY /* The process has not been finished, endpoint will be - NAK to further rquest */ -} RESULT; - - -/*-*-*-*-*-*-*-*-*-*-* Definitions for endpoint level -*-*-*-*-*-*-*-*-*-*-*-*/ -typedef struct _ENDPOINT_INFO -{ - /* When send data out of the device, - CopyData() is used to get data buffer 'Length' bytes data - if Length is 0, - CopyData() returns the total length of the data - if the request is not supported, returns 0 - (NEW Feature ) - if CopyData() returns -1, the calling routine should not proceed - further and will resume the SETUP process by the class device - if Length is not 0, - CopyData() returns a pointer to indicate the data location - Usb_wLength is the data remain to be sent, - Usb_wOffset is the Offset of original data - When receive data from the host, - CopyData() is used to get user data buffer which is capable - of Length bytes data to copy data from the endpoint buffer. - if Length is 0, - CopyData() returns the available data length, - if Length is not 0, - CopyData() returns user buffer address - Usb_rLength is the data remain to be received, - Usb_rPointer is the Offset of data buffer - */ - u16 Usb_wLength; - u16 Usb_wOffset; - u16 PacketSize; - u8 *(*CopyData)(u16 Length); -}ENDPOINT_INFO; - -/*-*-*-*-*-*-*-*-*-*-*-* Definitions for device level -*-*-*-*-*-*-*-*-*-*-*-*/ - -typedef struct _DEVICE -{ - u8 Total_Endpoint; /* Number of endpoints that are used */ - u8 Total_Configuration;/* Number of configuration available */ -} -DEVICE; - -typedef union -{ - u16 w; - struct BW - { - u8 bb1; - u8 bb0; - } - bw; -} u16_u8; - -typedef struct _DEVICE_INFO -{ - u8 USBbmRequestType; /* bmRequestType */ - u8 USBbRequest; /* bRequest */ - u16_u8 USBwValues; /* wValue */ - u16_u8 USBwIndexs; /* wIndex */ - u16_u8 USBwLengths; /* wLength */ - - u8 ControlState; /* of type CONTROL_STATE */ - u8 Current_Feature; - u8 Current_Configuration; /* Selected configuration */ - u8 Current_Interface; /* Selected interface of current configuration */ - u8 Current_AlternateSetting;/* Selected Alternate Setting of current - interface*/ - - ENDPOINT_INFO Ctrl_Info; -}DEVICE_INFO; - -typedef struct _DEVICE_PROP -{ - void (*Init)(void); /* Initialize the device */ - void (*Reset)(void); /* Reset routine of this device */ - - /* Device dependent process after the status stage */ - void (*Process_Status_IN)(void); - void (*Process_Status_OUT)(void); - - /* Procedure of process on setup stage of a class specified request with data stage */ - /* All class specified requests with data stage are processed in Class_Data_Setup - Class_Data_Setup() - responses to check all special requests and fills ENDPOINT_INFO - according to the request - If IN tokens are expected, then wLength & wOffset will be filled - with the total transferring bytes and the starting position - If OUT tokens are expected, then rLength & rOffset will be filled - with the total expected bytes and the starting position in the buffer - - If the request is valid, Class_Data_Setup returns SUCCESS, else UNSUPPORT - - CAUTION: - Since GET_CONFIGURATION & GET_INTERFACE are highly related to - the individual classes, they will be checked and processed here. - */ - RESULT (*Class_Data_Setup)(u8 RequestNo); - - /* Procedure of process on setup stage of a class specified request without data stage */ - /* All class specified requests without data stage are processed in Class_NoData_Setup - Class_NoData_Setup - responses to check all special requests and perform the request - - CAUTION: - Since SET_CONFIGURATION & SET_INTERFACE are highly related to - the individual classes, they will be checked and processed here. - */ - RESULT (*Class_NoData_Setup)(u8 RequestNo); - - /*Class_Get_Interface_Setting - This function is used by the file usb_core.c to test if the selected Interface - and Alternate Setting (u8 Interface, u8 AlternateSetting) are supported by - the application. - This function is writing by user. It should return "SUCCESS" if the Interface - and Alternate Setting are supported by the application or "UNSUPPORT" if they - are not supported. */ - - RESULT (*Class_Get_Interface_Setting)(u8 Interface, u8 AlternateSetting); - - u8* (*GetDeviceDescriptor)(u16 Length); - u8* (*GetConfigDescriptor)(u16 Length); - u8* (*GetStringDescriptor)(u16 Length); - u8* (*GetFunctionalDescriptor)(u16 Length); - - u8* RxEP_buffer; - u8 MaxPacketSize; - -}DEVICE_PROP; - -typedef struct _USER_STANDARD_REQUESTS -{ - void (*User_GetConfiguration)(void); /* Get Configuration */ - void (*User_SetConfiguration)(void); /* Set Configuration */ - void (*User_GetInterface)(void); /* Get Interface */ - void (*User_SetInterface)(void); /* Set Interface */ - void (*User_GetStatus)(void); /* Get Status */ - void (*User_ClearFeature)(void); /* Clear Feature */ - void (*User_SetEndPointFeature)(void); /* Set Endpoint Feature */ - void (*User_SetDeviceFeature)(void); /* Set Device Feature */ - void (*User_SetDeviceAddress)(void); /* Set Device Address */ -} -USER_STANDARD_REQUESTS; - -/* Exported constants --------------------------------------------------------*/ -#define Type_Recipient (pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) - -#define Usb_rLength Usb_wLength -#define Usb_rOffset Usb_wOffset - -#define USBwValue USBwValues.w -#define USBwValue0 USBwValues.bw.bb0 -#define USBwValue1 USBwValues.bw.bb1 -#define USBwIndex USBwIndexs.w -#define USBwIndex0 USBwIndexs.bw.bb0 -#define USBwIndex1 USBwIndexs.bw.bb1 -#define USBwLength USBwLengths.w -#define USBwLength0 USBwLengths.bw.bb0 -#define USBwLength1 USBwLengths.bw.bb1 - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -u8 Setup0_Process(void); -u8 Post0_Process(void); -u8 Out0_Process(void); -u8 In0_Process(void); - -RESULT Standard_SetEndPointFeature(void); -RESULT Standard_SetDeviceFeature(void); - -u8 *Standard_GetConfiguration(u16 Length); -RESULT Standard_SetConfiguration(void); -u8 *Standard_GetInterface(u16 Length); -RESULT Standard_SetInterface(void); -u8 *Standard_GetDescriptorData(u16 Length, PONE_DESCRIPTOR pDesc); - -u8 *Standard_GetStatus(u16 Length); -RESULT Standard_ClearFeature(void); -void SetDeviceAddress(u8); -void NOP_Process(void); - -extern DEVICE_PROP Device_Property; -extern USER_STANDARD_REQUESTS User_Standard_Requests; -extern DEVICE Device_Table; -extern DEVICE_INFO Device_Info; - -/* cells saving status during interrupt servicing */ -extern u16 SaveRState; -extern u16 SaveTState; - -#endif /* __USB_CORE_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_core.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Standard protocol processing functions prototypes +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_CORE_H +#define __USB_CORE_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef enum _CONTROL_STATE +{ + WAIT_SETUP, /* 0 */ + SETTING_UP, /* 1 */ + IN_DATA, /* 2 */ + OUT_DATA, /* 3 */ + LAST_IN_DATA, /* 4 */ + LAST_OUT_DATA, /* 5 */ + WAIT_STATUS_IN, /* 7 */ + WAIT_STATUS_OUT, /* 8 */ + STALLED, /* 9 */ + PAUSE /* 10 */ +} CONTROL_STATE; /* The state machine states of a control pipe */ + +typedef struct OneDescriptor +{ + u8 *Descriptor; + u16 Descriptor_Size; +} +ONE_DESCRIPTOR, *PONE_DESCRIPTOR; +/* All the request process routines return a value of this type + If the return value is not SUCCESS or NOT_READY, + the software will STALL the correspond endpoint */ +typedef enum _RESULT +{ + USB_SUCCESS = 0, /* Process sucessfully */ + USB_ERROR, + USB_UNSUPPORT, + USB_NOT_READY /* The process has not been finished, endpoint will be + NAK to further rquest */ +} RESULT; + + +/*-*-*-*-*-*-*-*-*-*-* Definitions for endpoint level -*-*-*-*-*-*-*-*-*-*-*-*/ +typedef struct _ENDPOINT_INFO +{ + /* When send data out of the device, + CopyData() is used to get data buffer 'Length' bytes data + if Length is 0, + CopyData() returns the total length of the data + if the request is not supported, returns 0 + (NEW Feature ) + if CopyData() returns -1, the calling routine should not proceed + further and will resume the SETUP process by the class device + if Length is not 0, + CopyData() returns a pointer to indicate the data location + Usb_wLength is the data remain to be sent, + Usb_wOffset is the Offset of original data + When receive data from the host, + CopyData() is used to get user data buffer which is capable + of Length bytes data to copy data from the endpoint buffer. + if Length is 0, + CopyData() returns the available data length, + if Length is not 0, + CopyData() returns user buffer address + Usb_rLength is the data remain to be received, + Usb_rPointer is the Offset of data buffer + */ + u16 Usb_wLength; + u16 Usb_wOffset; + u16 PacketSize; + u8 *(*CopyData)(u16 Length); +}ENDPOINT_INFO; + +/*-*-*-*-*-*-*-*-*-*-*-* Definitions for device level -*-*-*-*-*-*-*-*-*-*-*-*/ + +typedef struct _DEVICE +{ + u8 Total_Endpoint; /* Number of endpoints that are used */ + u8 Total_Configuration;/* Number of configuration available */ +} +DEVICE; + +typedef union +{ + u16 w; + struct BW + { + u8 bb1; + u8 bb0; + } + bw; +} u16_u8; + +typedef struct _DEVICE_INFO +{ + u8 USBbmRequestType; /* bmRequestType */ + u8 USBbRequest; /* bRequest */ + u16_u8 USBwValues; /* wValue */ + u16_u8 USBwIndexs; /* wIndex */ + u16_u8 USBwLengths; /* wLength */ + + u8 ControlState; /* of type CONTROL_STATE */ + u8 Current_Feature; + u8 Current_Configuration; /* Selected configuration */ + u8 Current_Interface; /* Selected interface of current configuration */ + u8 Current_AlternateSetting;/* Selected Alternate Setting of current + interface*/ + + ENDPOINT_INFO Ctrl_Info; +}DEVICE_INFO; + +typedef struct _DEVICE_PROP +{ + void (*Init)(void); /* Initialize the device */ + void (*Reset)(void); /* Reset routine of this device */ + + /* Device dependent process after the status stage */ + void (*Process_Status_IN)(void); + void (*Process_Status_OUT)(void); + + /* Procedure of process on setup stage of a class specified request with data stage */ + /* All class specified requests with data stage are processed in Class_Data_Setup + Class_Data_Setup() + responses to check all special requests and fills ENDPOINT_INFO + according to the request + If IN tokens are expected, then wLength & wOffset will be filled + with the total transferring bytes and the starting position + If OUT tokens are expected, then rLength & rOffset will be filled + with the total expected bytes and the starting position in the buffer + + If the request is valid, Class_Data_Setup returns SUCCESS, else UNSUPPORT + + CAUTION: + Since GET_CONFIGURATION & GET_INTERFACE are highly related to + the individual classes, they will be checked and processed here. + */ + RESULT (*Class_Data_Setup)(u8 RequestNo); + + /* Procedure of process on setup stage of a class specified request without data stage */ + /* All class specified requests without data stage are processed in Class_NoData_Setup + Class_NoData_Setup + responses to check all special requests and perform the request + + CAUTION: + Since SET_CONFIGURATION & SET_INTERFACE are highly related to + the individual classes, they will be checked and processed here. + */ + RESULT (*Class_NoData_Setup)(u8 RequestNo); + + /*Class_Get_Interface_Setting + This function is used by the file usb_core.c to test if the selected Interface + and Alternate Setting (u8 Interface, u8 AlternateSetting) are supported by + the application. + This function is writing by user. It should return "SUCCESS" if the Interface + and Alternate Setting are supported by the application or "UNSUPPORT" if they + are not supported. */ + + RESULT (*Class_Get_Interface_Setting)(u8 Interface, u8 AlternateSetting); + + u8* (*GetDeviceDescriptor)(u16 Length); + u8* (*GetConfigDescriptor)(u16 Length); + u8* (*GetStringDescriptor)(u16 Length); + u8* (*GetFunctionalDescriptor)(u16 Length); + + u8* RxEP_buffer; + u8 MaxPacketSize; + +}DEVICE_PROP; + +typedef struct _USER_STANDARD_REQUESTS +{ + void (*User_GetConfiguration)(void); /* Get Configuration */ + void (*User_SetConfiguration)(void); /* Set Configuration */ + void (*User_GetInterface)(void); /* Get Interface */ + void (*User_SetInterface)(void); /* Set Interface */ + void (*User_GetStatus)(void); /* Get Status */ + void (*User_ClearFeature)(void); /* Clear Feature */ + void (*User_SetEndPointFeature)(void); /* Set Endpoint Feature */ + void (*User_SetDeviceFeature)(void); /* Set Device Feature */ + void (*User_SetDeviceAddress)(void); /* Set Device Address */ +} +USER_STANDARD_REQUESTS; + +/* Exported constants --------------------------------------------------------*/ +#define Type_Recipient (pInformation->USBbmRequestType & (REQUEST_TYPE | RECIPIENT)) + +#define Usb_rLength Usb_wLength +#define Usb_rOffset Usb_wOffset + +#define USBwValue USBwValues.w +#define USBwValue0 USBwValues.bw.bb0 +#define USBwValue1 USBwValues.bw.bb1 +#define USBwIndex USBwIndexs.w +#define USBwIndex0 USBwIndexs.bw.bb0 +#define USBwIndex1 USBwIndexs.bw.bb1 +#define USBwLength USBwLengths.w +#define USBwLength0 USBwLengths.bw.bb0 +#define USBwLength1 USBwLengths.bw.bb1 + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +u8 Setup0_Process(void); +u8 Post0_Process(void); +u8 Out0_Process(void); +u8 In0_Process(void); + +RESULT Standard_SetEndPointFeature(void); +RESULT Standard_SetDeviceFeature(void); + +u8 *Standard_GetConfiguration(u16 Length); +RESULT Standard_SetConfiguration(void); +u8 *Standard_GetInterface(u16 Length); +RESULT Standard_SetInterface(void); +u8 *Standard_GetDescriptorData(u16 Length, PONE_DESCRIPTOR pDesc); + +u8 *Standard_GetStatus(u16 Length); +RESULT Standard_ClearFeature(void); +void SetDeviceAddress(u8); +void NOP_Process(void); + +extern DEVICE_PROP Device_Property; +extern USER_STANDARD_REQUESTS User_Standard_Requests; +extern DEVICE Device_Table; +extern DEVICE_INFO Device_Info; + +/* cells saving status during interrupt servicing */ +extern u16 SaveRState; +extern u16 SaveTState; + +#endif /* __USB_CORE_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_def.h b/src/usb_lib/usb_def.h similarity index 97% rename from usb_lib/usb_def.h rename to src/usb_lib/usb_def.h index 4261d46..cfc551c 100644 --- a/usb_lib/usb_def.h +++ b/src/usb_lib/usb_def.h @@ -1,80 +1,80 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_def.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Definitions related to USB Core -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_DEF_H -#define __USB_DEF_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -typedef enum _RECIPIENT_TYPE -{ - DEVICE_RECIPIENT, /* Recipient device */ - INTERFACE_RECIPIENT, /* Recipient interface */ - ENDPOINT_RECIPIENT, /* Recipient endpoint */ - OTHER_RECIPIENT -} RECIPIENT_TYPE; - - -typedef enum _STANDARD_REQUESTS -{ - GET_STATUS = 0, - CLEAR_FEATURE, - RESERVED1, - SET_FEATURE, - RESERVED2, - SET_ADDRESS, - GET_DESCRIPTOR, - SET_DESCRIPTOR, - GET_CONFIGURATION, - SET_CONFIGURATION, - GET_INTERFACE, - SET_INTERFACE, - TOTAL_sREQUEST, /* Total number of Standard request */ - SYNCH_FRAME = 12 -} STANDARD_REQUESTS; - -/* Definition of "USBwValue" */ -typedef enum _DESCRIPTOR_TYPE -{ - DEVICE_DESCRIPTOR = 1, - CONFIG_DESCRIPTOR, - STRING_DESCRIPTOR, - INTERFACE_DESCRIPTOR, - ENDPOINT_DESCRIPTOR -} DESCRIPTOR_TYPE; - -/* Feature selector of a SET_FEATURE or CLEAR_FEATURE */ -typedef enum _FEATURE_SELECTOR -{ - ENDPOINT_STALL, - DEVICE_REMOTE_WAKEUP -} FEATURE_SELECTOR; - -/* Exported constants --------------------------------------------------------*/ -/* Definition of "USBbmRequestType" */ -#define REQUEST_TYPE 0x60 /* Mask to get request type */ -#define STANDARD_REQUEST 0x00 /* Standard request */ -#define CLASS_REQUEST 0x20 /* Class request */ -#define VENDOR_REQUEST 0x40 /* Vendor request */ - -#define RECIPIENT 0x1F /* Mask to get recipient */ - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ - -#endif /* __USB_DEF_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_def.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Definitions related to USB Core +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_DEF_H +#define __USB_DEF_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef enum _RECIPIENT_TYPE +{ + DEVICE_RECIPIENT, /* Recipient device */ + INTERFACE_RECIPIENT, /* Recipient interface */ + ENDPOINT_RECIPIENT, /* Recipient endpoint */ + OTHER_RECIPIENT +} RECIPIENT_TYPE; + + +typedef enum _STANDARD_REQUESTS +{ + GET_STATUS = 0, + CLEAR_FEATURE, + RESERVED1, + SET_FEATURE, + RESERVED2, + SET_ADDRESS, + GET_DESCRIPTOR, + SET_DESCRIPTOR, + GET_CONFIGURATION, + SET_CONFIGURATION, + GET_INTERFACE, + SET_INTERFACE, + TOTAL_sREQUEST, /* Total number of Standard request */ + SYNCH_FRAME = 12 +} STANDARD_REQUESTS; + +/* Definition of "USBwValue" */ +typedef enum _DESCRIPTOR_TYPE +{ + DEVICE_DESCRIPTOR = 1, + CONFIG_DESCRIPTOR, + STRING_DESCRIPTOR, + INTERFACE_DESCRIPTOR, + ENDPOINT_DESCRIPTOR +} DESCRIPTOR_TYPE; + +/* Feature selector of a SET_FEATURE or CLEAR_FEATURE */ +typedef enum _FEATURE_SELECTOR +{ + ENDPOINT_STALL, + DEVICE_REMOTE_WAKEUP +} FEATURE_SELECTOR; + +/* Exported constants --------------------------------------------------------*/ +/* Definition of "USBbmRequestType" */ +#define REQUEST_TYPE 0x60 /* Mask to get request type */ +#define STANDARD_REQUEST 0x00 /* Standard request */ +#define CLASS_REQUEST 0x20 /* Class request */ +#define VENDOR_REQUEST 0x40 /* Vendor request */ + +#define RECIPIENT 0x1F /* Mask to get recipient */ + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ + +#endif /* __USB_DEF_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_init.c b/src/usb_lib/usb_init.c similarity index 97% rename from usb_lib/usb_init.c rename to src/usb_lib/usb_init.c index 5862145..679dabe 100644 --- a/usb_lib/usb_init.c +++ b/src/usb_lib/usb_init.c @@ -1,64 +1,64 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_init.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Initialization routines & global variables -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* The number of current endpoint, it will be used to specify an endpoint */ - u8 EPindex; -/* The number of current device, it is an index to the Device_Table */ -/* u8 Device_no; */ -/* Points to the DEVICE_INFO structure of current device */ -/* The purpose of this register is to speed up the execution */ -DEVICE_INFO *pInformation; -/* Points to the DEVICE_PROP structure of current device */ -/* The purpose of this register is to speed up the execution */ -DEVICE_PROP *pProperty; -/* Temporary save the state of Rx & Tx status. */ -/* Whenever the Rx or Tx state is changed, its value is saved */ -/* in this variable first and will be set to the EPRB or EPRA */ -/* at the end of interrupt process */ -u16 SaveState ; -u16 wInterrupt_Mask; -DEVICE_INFO Device_Info; -USER_STANDARD_REQUESTS *pUser_Standard_Requests; - -/* Extern variables ----------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ - -/******************************************************************************* -* Function Name : USB_Init -* Description : USB system initialization -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void USB_Init(void) -{ - pInformation = &Device_Info; - pInformation->ControlState = 2; - pProperty = &Device_Property; - pUser_Standard_Requests = &User_Standard_Requests; - /* Initialize devices one by one */ - - pProperty->Init(); -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_init.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Initialization routines & global variables +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* The number of current endpoint, it will be used to specify an endpoint */ + u8 EPindex; +/* The number of current device, it is an index to the Device_Table */ +/* u8 Device_no; */ +/* Points to the DEVICE_INFO structure of current device */ +/* The purpose of this register is to speed up the execution */ +DEVICE_INFO *pInformation; +/* Points to the DEVICE_PROP structure of current device */ +/* The purpose of this register is to speed up the execution */ +DEVICE_PROP *pProperty; +/* Temporary save the state of Rx & Tx status. */ +/* Whenever the Rx or Tx state is changed, its value is saved */ +/* in this variable first and will be set to the EPRB or EPRA */ +/* at the end of interrupt process */ +u16 SaveState ; +u16 wInterrupt_Mask; +DEVICE_INFO Device_Info; +USER_STANDARD_REQUESTS *pUser_Standard_Requests; + +/* Extern variables ----------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : USB_Init +* Description : USB system initialization +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void USB_Init(void) +{ + pInformation = &Device_Info; + pInformation->ControlState = 2; + pProperty = &Device_Property; + pUser_Standard_Requests = &User_Standard_Requests; + /* Initialize devices one by one */ + + pProperty->Init(); +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_init.h b/src/usb_lib/usb_init.h similarity index 98% rename from usb_lib/usb_init.h rename to src/usb_lib/usb_init.h index 76e9d8a..7e8487d 100644 --- a/usb_lib/usb_init.h +++ b/src/usb_lib/usb_init.h @@ -1,49 +1,49 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_init.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Initialization routines & global variables -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_INIT_H -#define __USB_INIT_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -void USB_Init(void); - -/* External variables --------------------------------------------------------*/ -/* The number of current endpoint, it will be used to specify an endpoint */ -extern u8 EPindex; -/* The number of current device, it is an index to the Device_Table */ -/*extern u8 Device_no; */ -/* Points to the DEVICE_INFO structure of current device */ -/* The purpose of this register is to speed up the execution */ -extern DEVICE_INFO* pInformation; -/* Points to the DEVICE_PROP structure of current device */ -/* The purpose of this register is to speed up the execution */ -extern DEVICE_PROP* pProperty; -/* Temporary save the state of Rx & Tx status. */ -/* Whenever the Rx or Tx state is changed, its value is saved */ -/* in this variable first and will be set to the EPRB or EPRA */ -/* at the end of interrupt process */ -extern USER_STANDARD_REQUESTS *pUser_Standard_Requests; - -extern u16 SaveState ; -extern u16 wInterrupt_Mask; - -#endif /* __USB_INIT_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_init.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Initialization routines & global variables +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_INIT_H +#define __USB_INIT_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void USB_Init(void); + +/* External variables --------------------------------------------------------*/ +/* The number of current endpoint, it will be used to specify an endpoint */ +extern u8 EPindex; +/* The number of current device, it is an index to the Device_Table */ +/*extern u8 Device_no; */ +/* Points to the DEVICE_INFO structure of current device */ +/* The purpose of this register is to speed up the execution */ +extern DEVICE_INFO* pInformation; +/* Points to the DEVICE_PROP structure of current device */ +/* The purpose of this register is to speed up the execution */ +extern DEVICE_PROP* pProperty; +/* Temporary save the state of Rx & Tx status. */ +/* Whenever the Rx or Tx state is changed, its value is saved */ +/* in this variable first and will be set to the EPRB or EPRA */ +/* at the end of interrupt process */ +extern USER_STANDARD_REQUESTS *pUser_Standard_Requests; + +extern u16 SaveState ; +extern u16 wInterrupt_Mask; + +#endif /* __USB_INIT_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_int.c b/src/usb_lib/usb_int.c similarity index 96% rename from usb_lib/usb_int.c rename to src/usb_lib/usb_int.c index f1d82be..28f80ce 100644 --- a/usb_lib/usb_int.c +++ b/src/usb_lib/usb_int.c @@ -1,192 +1,192 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_int.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Endpoint CTR (Low and High) interrupt's service routines -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -u16 SaveRState; -u16 SaveTState; - -/* Extern variables ----------------------------------------------------------*/ -extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */ -extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */ - -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ - -/******************************************************************************* -* Function Name : CTR_LP. -* Description : Low priority Endpoint Correct Transfer interrupt's service -* routine. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void CTR_LP(void) -{ - u32 wEPVal = 0; - /* stay in loop while pending ints */ - while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) - { - _SetISTR((u16)CLR_CTR); /* clear CTR flag */ - /* extract highest priority endpoint number */ - EPindex = (u8)(wIstr & ISTR_EP_ID); - if (EPindex == 0) - { - /* Decode and service control endpoint interrupt */ - /* calling related service routine */ - /* (Setup0_Process, In0_Process, Out0_Process) */ - - /* save RX & TX status */ - /* and set both to NAK */ - SaveRState = _GetEPRxStatus(ENDP0); - SaveTState = _GetEPTxStatus(ENDP0); - _SetEPRxStatus(ENDP0, EP_RX_NAK); - _SetEPTxStatus(ENDP0, EP_TX_NAK); - - - /* DIR bit = origin of the interrupt */ - - if ((wIstr & ISTR_DIR) == 0) - { - /* DIR = 0 */ - - /* DIR = 0 => IN int */ - /* DIR = 0 implies that (EP_CTR_TX = 1) always */ - - - _ClearEP_CTR_TX(ENDP0); - In0_Process(); - - /* before terminate set Tx & Rx status */ - _SetEPRxStatus(ENDP0, SaveRState); - _SetEPTxStatus(ENDP0, SaveTState); - return; - } - else - { - /* DIR = 1 */ - - /* DIR = 1 & CTR_RX => SETUP or OUT int */ - /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */ - - wEPVal = _GetENDPOINT(ENDP0); - if ((wEPVal & EP_CTR_TX) != 0) - { - _ClearEP_CTR_TX(ENDP0); - In0_Process(); - /* before terminate set Tx & Rx status */ - _SetEPRxStatus(ENDP0, SaveRState); - _SetEPTxStatus(ENDP0, SaveTState); - return; - } - else if ((wEPVal &EP_SETUP) != 0) - { - _ClearEP_CTR_RX(ENDP0); /* SETUP bit kept frozen while CTR_RX = 1 */ - Setup0_Process(); - /* before terminate set Tx & Rx status */ - _SetEPRxStatus(ENDP0, SaveRState); - _SetEPTxStatus(ENDP0, SaveTState); - return; - } - - else if ((wEPVal & EP_CTR_RX) != 0) - { - _ClearEP_CTR_RX(ENDP0); - Out0_Process(); - /* before terminate set Tx & Rx status */ - _SetEPRxStatus(ENDP0, SaveRState); - _SetEPTxStatus(ENDP0, SaveTState); - return; - } - } - }/* if(EPindex == 0) */ - else - { - /* Decode and service non control endpoints interrupt */ - - /* process related endpoint register */ - wEPVal = _GetENDPOINT(EPindex); - if ((wEPVal & EP_CTR_RX) != 0) - { - /* clear int flag */ - _ClearEP_CTR_RX(EPindex); - - /* call OUT service function */ - (*pEpInt_OUT[EPindex-1])(); - - } /* if((wEPVal & EP_CTR_RX) */ - - if ((wEPVal & EP_CTR_TX) != 0) - { - /* clear int flag */ - _ClearEP_CTR_TX(EPindex); - - /* call IN service function */ - (*pEpInt_IN[EPindex-1])(); - } /* if((wEPVal & EP_CTR_TX) != 0) */ - - }/* if(EPindex == 0) else */ - - }/* while(...) */ -} - -/******************************************************************************* -* Function Name : CTR_HP. -* Description : High Priority Endpoint Correct Transfer interrupt's service -* routine. -* Input : None. -* Output : None. -* Return : None. -*******************************************************************************/ -void CTR_HP(void) -{ - u32 wEPVal = 0; - - while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) - { - _SetISTR((u16)CLR_CTR); /* clear CTR flag */ - /* extract highest priority endpoint number */ - EPindex = (u8)(wIstr & ISTR_EP_ID); - /* process related endpoint register */ - wEPVal = _GetENDPOINT(EPindex); - if ((wEPVal & EP_CTR_RX) != 0) - { - /* clear int flag */ - _ClearEP_CTR_RX(EPindex); - - /* call OUT service function */ - (*pEpInt_OUT[EPindex-1])(); - - } /* if((wEPVal & EP_CTR_RX) */ - else if ((wEPVal & EP_CTR_TX) != 0) - { - /* clear int flag */ - _ClearEP_CTR_TX(EPindex); - - /* call IN service function */ - (*pEpInt_IN[EPindex-1])(); - - - } /* if((wEPVal & EP_CTR_TX) != 0) */ - - }/* while(...) */ -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_int.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Endpoint CTR (Low and High) interrupt's service routines +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +u16 SaveRState; +u16 SaveTState; + +/* Extern variables ----------------------------------------------------------*/ +extern void (*pEpInt_IN[7])(void); /* Handles IN interrupts */ +extern void (*pEpInt_OUT[7])(void); /* Handles OUT interrupts */ + +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : CTR_LP. +* Description : Low priority Endpoint Correct Transfer interrupt's service +* routine. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void CTR_LP(void) +{ + u32 wEPVal = 0; + /* stay in loop while pending ints */ + while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) + { + _SetISTR((u16)CLR_CTR); /* clear CTR flag */ + /* extract highest priority endpoint number */ + EPindex = (u8)(wIstr & ISTR_EP_ID); + if (EPindex == 0) + { + /* Decode and service control endpoint interrupt */ + /* calling related service routine */ + /* (Setup0_Process, In0_Process, Out0_Process) */ + + /* save RX & TX status */ + /* and set both to NAK */ + SaveRState = _GetEPRxStatus(ENDP0); + SaveTState = _GetEPTxStatus(ENDP0); + _SetEPRxStatus(ENDP0, EP_RX_NAK); + _SetEPTxStatus(ENDP0, EP_TX_NAK); + + + /* DIR bit = origin of the interrupt */ + + if ((wIstr & ISTR_DIR) == 0) + { + /* DIR = 0 */ + + /* DIR = 0 => IN int */ + /* DIR = 0 implies that (EP_CTR_TX = 1) always */ + + + _ClearEP_CTR_TX(ENDP0); + In0_Process(); + + /* before terminate set Tx & Rx status */ + _SetEPRxStatus(ENDP0, SaveRState); + _SetEPTxStatus(ENDP0, SaveTState); + return; + } + else + { + /* DIR = 1 */ + + /* DIR = 1 & CTR_RX => SETUP or OUT int */ + /* DIR = 1 & (CTR_TX | CTR_RX) => 2 int pending */ + + wEPVal = _GetENDPOINT(ENDP0); + if ((wEPVal & EP_CTR_TX) != 0) + { + _ClearEP_CTR_TX(ENDP0); + In0_Process(); + /* before terminate set Tx & Rx status */ + _SetEPRxStatus(ENDP0, SaveRState); + _SetEPTxStatus(ENDP0, SaveTState); + return; + } + else if ((wEPVal &EP_SETUP) != 0) + { + _ClearEP_CTR_RX(ENDP0); /* SETUP bit kept frozen while CTR_RX = 1 */ + Setup0_Process(); + /* before terminate set Tx & Rx status */ + _SetEPRxStatus(ENDP0, SaveRState); + _SetEPTxStatus(ENDP0, SaveTState); + return; + } + + else if ((wEPVal & EP_CTR_RX) != 0) + { + _ClearEP_CTR_RX(ENDP0); + Out0_Process(); + /* before terminate set Tx & Rx status */ + _SetEPRxStatus(ENDP0, SaveRState); + _SetEPTxStatus(ENDP0, SaveTState); + return; + } + } + }/* if(EPindex == 0) */ + else + { + /* Decode and service non control endpoints interrupt */ + + /* process related endpoint register */ + wEPVal = _GetENDPOINT(EPindex); + if ((wEPVal & EP_CTR_RX) != 0) + { + /* clear int flag */ + _ClearEP_CTR_RX(EPindex); + + /* call OUT service function */ + (*pEpInt_OUT[EPindex-1])(); + + } /* if((wEPVal & EP_CTR_RX) */ + + if ((wEPVal & EP_CTR_TX) != 0) + { + /* clear int flag */ + _ClearEP_CTR_TX(EPindex); + + /* call IN service function */ + (*pEpInt_IN[EPindex-1])(); + } /* if((wEPVal & EP_CTR_TX) != 0) */ + + }/* if(EPindex == 0) else */ + + }/* while(...) */ +} + +/******************************************************************************* +* Function Name : CTR_HP. +* Description : High Priority Endpoint Correct Transfer interrupt's service +* routine. +* Input : None. +* Output : None. +* Return : None. +*******************************************************************************/ +void CTR_HP(void) +{ + u32 wEPVal = 0; + + while (((wIstr = _GetISTR()) & ISTR_CTR) != 0) + { + _SetISTR((u16)CLR_CTR); /* clear CTR flag */ + /* extract highest priority endpoint number */ + EPindex = (u8)(wIstr & ISTR_EP_ID); + /* process related endpoint register */ + wEPVal = _GetENDPOINT(EPindex); + if ((wEPVal & EP_CTR_RX) != 0) + { + /* clear int flag */ + _ClearEP_CTR_RX(EPindex); + + /* call OUT service function */ + (*pEpInt_OUT[EPindex-1])(); + + } /* if((wEPVal & EP_CTR_RX) */ + else if ((wEPVal & EP_CTR_TX) != 0) + { + /* clear int flag */ + _ClearEP_CTR_TX(EPindex); + + /* call IN service function */ + (*pEpInt_IN[EPindex-1])(); + + + } /* if((wEPVal & EP_CTR_TX) != 0) */ + + }/* while(...) */ +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_int.h b/src/usb_lib/usb_int.h similarity index 98% rename from usb_lib/usb_int.h rename to src/usb_lib/usb_int.h index 1eb18a7..ddc8b33 100644 --- a/usb_lib/usb_int.h +++ b/src/usb_lib/usb_int.h @@ -1,33 +1,33 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_int.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Endpoint CTR (Low and High) interrupt's service routines -* prototypes -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_INT_H -#define __USB_INT_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -void CTR_LP(void); -void CTR_HP(void); - -/* External variables --------------------------------------------------------*/ - -#endif /* __USB_INT_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_int.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Endpoint CTR (Low and High) interrupt's service routines +* prototypes +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_INT_H +#define __USB_INT_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void CTR_LP(void); +void CTR_HP(void); + +/* External variables --------------------------------------------------------*/ + +#endif /* __USB_INT_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_lib.h b/src/usb_lib/usb_lib.h similarity index 97% rename from usb_lib/usb_lib.h rename to src/usb_lib/usb_lib.h index 5d14b5a..4437b8d 100644 --- a/usb_lib/usb_lib.h +++ b/src/usb_lib/usb_lib.h @@ -1,37 +1,37 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_lib.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : USB library include files -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_LIB_H -#define __USB_LIB_H - -/* Includes ------------------------------------------------------------------*/ -#include "usb_type.h" -#include "usb_regs.h" -#include "usb_def.h" -#include "usb_core.h" -#include "usb_init.h" -#include "usb_mem.h" -#include "usb_int.h" - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -/* External variables --------------------------------------------------------*/ - -#endif /* __USB_LIB_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_lib.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : USB library include files +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_LIB_H +#define __USB_LIB_H + +/* Includes ------------------------------------------------------------------*/ +#include "usb_type.h" +#include "usb_regs.h" +#include "usb_def.h" +#include "usb_core.h" +#include "usb_init.h" +#include "usb_mem.h" +#include "usb_int.h" + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +/* External variables --------------------------------------------------------*/ + +#endif /* __USB_LIB_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_mem.c b/src/usb_lib/usb_mem.c similarity index 97% rename from usb_lib/usb_mem.c rename to src/usb_lib/usb_mem.c index ee698c5..b72778c 100644 --- a/usb_lib/usb_mem.c +++ b/src/usb_lib/usb_mem.c @@ -1,73 +1,73 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_mem.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Utility functions for memory transfers to/from PMA -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* Extern variables ----------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ -/******************************************************************************* -* Function Name : UserToPMABufferCopy -* Description : Copy a buffer from user memory area to packet memory area (PMA) -* Input : - pbUsrBuf: pointer to user memory area. -* - wPMABufAddr: address into PMA. -* - wNBytes: no. of bytes to be copied. -* Output : None. -* Return : None . -*******************************************************************************/ -void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) -{ - u32 n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ - u32 i, temp1, temp2; - u16 *pdwVal; - pdwVal = (u16 *)(wPMABufAddr * 2 + PMAAddr); - for (i = n; i != 0; i--) - { - temp1 = (u16) * pbUsrBuf; - pbUsrBuf++; - temp2 = temp1 | (u16) * pbUsrBuf << 8; - *pdwVal++ = temp2; - pdwVal++; - pbUsrBuf++; - } -} -/******************************************************************************* -* Function Name : PMAToUserBufferCopy -* Description : Copy a buffer from user memory area to packet memory area (PMA) -* Input : - pbUsrBuf = pointer to user memory area. -* - wPMABufAddr = address into PMA. -* - wNBytes = no. of bytes to be copied. -* Output : None. -* Return : None. -*******************************************************************************/ -void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) -{ - u32 n = (wNBytes + 1) >> 1;/* /2*/ - u32 i; - u32 *pdwVal; - pdwVal = (u32 *)(wPMABufAddr * 2 + PMAAddr); - for (i = n; i != 0; i--) - { - *(u16*)pbUsrBuf++ = *pdwVal++; - pbUsrBuf++; - } -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_mem.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Utility functions for memory transfers to/from PMA +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Extern variables ----------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ +/******************************************************************************* +* Function Name : UserToPMABufferCopy +* Description : Copy a buffer from user memory area to packet memory area (PMA) +* Input : - pbUsrBuf: pointer to user memory area. +* - wPMABufAddr: address into PMA. +* - wNBytes: no. of bytes to be copied. +* Output : None. +* Return : None . +*******************************************************************************/ +void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) +{ + u32 n = (wNBytes + 1) >> 1; /* n = (wNBytes + 1) / 2 */ + u32 i, temp1, temp2; + u16 *pdwVal; + pdwVal = (u16 *)(wPMABufAddr * 2 + PMAAddr); + for (i = n; i != 0; i--) + { + temp1 = (u16) * pbUsrBuf; + pbUsrBuf++; + temp2 = temp1 | (u16) * pbUsrBuf << 8; + *pdwVal++ = temp2; + pdwVal++; + pbUsrBuf++; + } +} +/******************************************************************************* +* Function Name : PMAToUserBufferCopy +* Description : Copy a buffer from user memory area to packet memory area (PMA) +* Input : - pbUsrBuf = pointer to user memory area. +* - wPMABufAddr = address into PMA. +* - wNBytes = no. of bytes to be copied. +* Output : None. +* Return : None. +*******************************************************************************/ +void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes) +{ + u32 n = (wNBytes + 1) >> 1;/* /2*/ + u32 i; + u32 *pdwVal; + pdwVal = (u32 *)(wPMABufAddr * 2 + PMAAddr); + for (i = n; i != 0; i--) + { + *(u16*)pbUsrBuf++ = *pdwVal++; + pbUsrBuf++; + } +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_mem.h b/src/usb_lib/usb_mem.h similarity index 98% rename from usb_lib/usb_mem.h rename to src/usb_lib/usb_mem.h index d4cc4a6..f726b49 100644 --- a/usb_lib/usb_mem.h +++ b/src/usb_lib/usb_mem.h @@ -1,32 +1,32 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_mem.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Utility prototypes functions for memory/PMA transfers -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_MEM_H -#define __USB_MEM_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); -void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); - -/* External variables --------------------------------------------------------*/ - -#endif /*__USB_MEM_H*/ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_mem.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Utility prototypes functions for memory/PMA transfers +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_MEM_H +#define __USB_MEM_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +void UserToPMABufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); +void PMAToUserBufferCopy(u8 *pbUsrBuf, u16 wPMABufAddr, u16 wNBytes); + +/* External variables --------------------------------------------------------*/ + +#endif /*__USB_MEM_H*/ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_regs.c b/src/usb_lib/usb_regs.c similarity index 91% rename from usb_lib/usb_regs.c rename to src/usb_lib/usb_regs.c index c28ef5a..b36bff3 100644 --- a/usb_lib/usb_regs.c +++ b/src/usb_lib/usb_regs.c @@ -1,748 +1,748 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_regs.c -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Interface functions to USB cell registers -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Includes ------------------------------------------------------------------*/ -#include "usb_lib.h" - -/* Private typedef -----------------------------------------------------------*/ -/* Private define ------------------------------------------------------------*/ -/* Private macro -------------------------------------------------------------*/ -/* Private variables ---------------------------------------------------------*/ -/* Extern variables ----------------------------------------------------------*/ -/* Private function prototypes -----------------------------------------------*/ -/* Private functions ---------------------------------------------------------*/ - -/******************************************************************************* -* Function Name : SetCNTR. -* Description : Set the CNTR register value. -* Input : wRegValue: new register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetCNTR(u16 wRegValue) -{ - _SetCNTR(wRegValue); -} - -/******************************************************************************* -* Function Name : GetCNTR. -* Description : returns the CNTR register value. -* Input : None. -* Output : None. -* Return : CNTR register Value. -*******************************************************************************/ -u16 GetCNTR(void) -{ - return(_GetCNTR()); -} - -/******************************************************************************* -* Function Name : SetISTR. -* Description : Set the ISTR register value. -* Input : wRegValue: new register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetISTR(u16 wRegValue) -{ - _SetISTR(wRegValue); -} - -/******************************************************************************* -* Function Name : GetISTR -* Description : Returns the ISTR register value. -* Input : None. -* Output : None. -* Return : ISTR register Value -*******************************************************************************/ -u16 GetISTR(void) -{ - return(_GetISTR()); -} - -/******************************************************************************* -* Function Name : GetFNR -* Description : Returns the FNR register value. -* Input : None. -* Output : None. -* Return : FNR register Value -*******************************************************************************/ -u16 GetFNR(void) -{ - return(_GetFNR()); -} - -/******************************************************************************* -* Function Name : SetDADDR -* Description : Set the DADDR register value. -* Input : wRegValue: new register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetDADDR(u16 wRegValue) -{ - _SetDADDR(wRegValue); -} - -/******************************************************************************* -* Function Name : GetDADDR -* Description : Returns the DADDR register value. -* Input : None. -* Output : None. -* Return : DADDR register Value -*******************************************************************************/ -u16 GetDADDR(void) -{ - return(_GetDADDR()); -} - -/******************************************************************************* -* Function Name : SetBTABLE -* Description : Set the BTABLE. -* Input : wRegValue: New register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetBTABLE(u16 wRegValue) -{ - _SetBTABLE(wRegValue); -} - -/******************************************************************************* -* Function Name : GetBTABLE. -* Description : Returns the BTABLE register value. -* Input : None. -* Output : None. -* Return : BTABLE address. -*******************************************************************************/ -u16 GetBTABLE(void) -{ - return(_GetBTABLE()); -} - -/******************************************************************************* -* Function Name : SetENDPOINT -* Description : Setthe Endpoint register value. -* Input : bEpNum: Endpoint Number. -* wRegValue. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetENDPOINT(u8 bEpNum, u16 wRegValue) -{ - _SetENDPOINT(bEpNum, wRegValue); -} - -/******************************************************************************* -* Function Name : GetENDPOINT -* Description : Return the Endpoint register value. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint register value. -*******************************************************************************/ -u16 GetENDPOINT(u8 bEpNum) -{ - return(_GetENDPOINT(bEpNum)); -} - -/******************************************************************************* -* Function Name : SetEPType -* Description : sets the type in the endpoint register. -* Input : bEpNum: Endpoint Number. -* wType: type definition. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPType(u8 bEpNum, u16 wType) -{ - _SetEPType(bEpNum, wType); -} - -/******************************************************************************* -* Function Name : GetEPType -* Description : Returns the endpoint type. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint Type -*******************************************************************************/ -u16 GetEPType(u8 bEpNum) -{ - return(_GetEPType(bEpNum)); -} - -/******************************************************************************* -* Function Name : SetEPTxStatus -* Description : Set the status of Tx endpoint. -* Input : bEpNum: Endpoint Number. -* wState: new state. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPTxStatus(u8 bEpNum, u16 wState) -{ - _SetEPTxStatus(bEpNum, wState); -} - -/******************************************************************************* -* Function Name : SetEPRxStatus -* Description : Set the status of Rx endpoint. -* Input : bEpNum: Endpoint Number. -* wState: new state. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPRxStatus(u8 bEpNum, u16 wState) -{ - _SetEPRxStatus(bEpNum, wState); -} - -/******************************************************************************* -* Function Name : SetDouBleBuffEPStall -* Description : sets the status for Double Buffer Endpoint to STALL -* Input : bEpNum: Endpoint Number. -* bDir: Endpoint direction. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetDouBleBuffEPStall(u8 bEpNum, u8 bDir) -{ - u16 Endpoint_DTOG_Status; - Endpoint_DTOG_Status = GetENDPOINT(bEpNum); - if (bDir == EP_DBUF_OUT) - { /* OUT double buffered endpoint */ - _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPRX_DTOG1); - } - else if (bDir == EP_DBUF_IN) - { /* IN double buffered endpoint */ - _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPTX_DTOG1); - } -} - -/******************************************************************************* -* Function Name : GetEPTxStatus -* Description : Returns the endpoint Tx status. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint TX Status -*******************************************************************************/ -u16 GetEPTxStatus(u8 bEpNum) -{ - return(_GetEPTxStatus(bEpNum)); -} - -/******************************************************************************* -* Function Name : GetEPRxStatus -* Description : Returns the endpoint Rx status. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint RX Status -*******************************************************************************/ -u16 GetEPRxStatus(u8 bEpNum) -{ - return(_GetEPRxStatus(bEpNum)); -} - -/******************************************************************************* -* Function Name : SetEPTxValid -* Description : Valid the endpoint Tx Status. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPTxValid(u8 bEpNum) -{ - _SetEPTxStatus(bEpNum, EP_TX_VALID); -} - -/******************************************************************************* -* Function Name : SetEPRxValid -* Description : Valid the endpoint Rx Status. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPRxValid(u8 bEpNum) -{ - _SetEPRxStatus(bEpNum, EP_RX_VALID); -} - -/******************************************************************************* -* Function Name : SetEP_KIND -* Description : Clear the EP_KIND bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEP_KIND(u8 bEpNum) -{ - _SetEP_KIND(bEpNum); -} - -/******************************************************************************* -* Function Name : ClearEP_KIND -* Description : set the EP_KIND bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearEP_KIND(u8 bEpNum) -{ - _ClearEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : Clear_Status_Out -* Description : Clear the Status Out of the related Endpoint -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void Clear_Status_Out(u8 bEpNum) -{ - _ClearEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : Set_Status_Out -* Description : Set the Status Out of the related Endpoint -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void Set_Status_Out(u8 bEpNum) -{ - _SetEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : SetEPDoubleBuff -* Description : Enable the double buffer feature for the endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDoubleBuff(u8 bEpNum) -{ - _SetEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : ClearEPDoubleBuff -* Description : Disable the double buffer feature for the endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearEPDoubleBuff(u8 bEpNum) -{ - _ClearEP_KIND(bEpNum); -} -/******************************************************************************* -* Function Name : GetTxStallStatus -* Description : Returns the Stall status of the Tx endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Tx Stall status. -*******************************************************************************/ -u16 GetTxStallStatus(u8 bEpNum) -{ - return(_GetTxStallStatus(bEpNum)); -} -/******************************************************************************* -* Function Name : GetRxStallStatus -* Description : Returns the Stall status of the Rx endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Rx Stall status. -*******************************************************************************/ -u16 GetRxStallStatus(u8 bEpNum) -{ - return(_GetRxStallStatus(bEpNum)); -} -/******************************************************************************* -* Function Name : ClearEP_CTR_RX -* Description : Clear the CTR_RX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearEP_CTR_RX(u8 bEpNum) -{ - _ClearEP_CTR_RX(bEpNum); -} -/******************************************************************************* -* Function Name : ClearEP_CTR_TX -* Description : Clear the CTR_TX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearEP_CTR_TX(u8 bEpNum) -{ - _ClearEP_CTR_TX(bEpNum); -} -/******************************************************************************* -* Function Name : ToggleDTOG_RX -* Description : Toggle the DTOG_RX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ToggleDTOG_RX(u8 bEpNum) -{ - _ToggleDTOG_RX(bEpNum); -} -/******************************************************************************* -* Function Name : ToggleDTOG_TX -* Description : Toggle the DTOG_TX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ToggleDTOG_TX(u8 bEpNum) -{ - _ToggleDTOG_TX(bEpNum); -} -/******************************************************************************* -* Function Name : ClearDTOG_RX. -* Description : Clear the DTOG_RX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearDTOG_RX(u8 bEpNum) -{ - _ClearDTOG_RX(bEpNum); -} -/******************************************************************************* -* Function Name : ClearDTOG_TX. -* Description : Clear the DTOG_TX bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -void ClearDTOG_TX(u8 bEpNum) -{ - _ClearDTOG_TX(bEpNum); -} -/******************************************************************************* -* Function Name : SetEPAddress -* Description : Set the endpoint address. -* Input : bEpNum: Endpoint Number. -* bAddr: New endpoint address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPAddress(u8 bEpNum, u8 bAddr) -{ - _SetEPAddress(bEpNum, bAddr); -} -/******************************************************************************* -* Function Name : GetEPAddress -* Description : Get the endpoint address. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint address. -*******************************************************************************/ -u8 GetEPAddress(u8 bEpNum) -{ - return(_GetEPAddress(bEpNum)); -} -/******************************************************************************* -* Function Name : SetEPTxAddr -* Description : Set the endpoint Tx buffer address. -* Input : bEpNum: Endpoint Number. -* wAddr: new address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPTxAddr(u8 bEpNum, u16 wAddr) -{ - _SetEPTxAddr(bEpNum, wAddr); -} -/******************************************************************************* -* Function Name : SetEPRxAddr -* Description : Set the endpoint Rx buffer address. -* Input : bEpNum: Endpoint Number. -* wAddr: new address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPRxAddr(u8 bEpNum, u16 wAddr) -{ - _SetEPRxAddr(bEpNum, wAddr); -} -/******************************************************************************* -* Function Name : GetEPTxAddr -* Description : Returns the endpoint Tx buffer address. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Rx buffer address. -*******************************************************************************/ -u16 GetEPTxAddr(u8 bEpNum) -{ - return(_GetEPTxAddr(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPRxAddr. -* Description : Returns the endpoint Rx buffer address. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Rx buffer address. -*******************************************************************************/ -u16 GetEPRxAddr(u8 bEpNum) -{ - return(_GetEPRxAddr(bEpNum)); -} -/******************************************************************************* -* Function Name : SetEPTxCount. -* Description : Set the Tx count. -* Input : bEpNum: Endpoint Number. -* wCount: new count value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPTxCount(u8 bEpNum, u16 wCount) -{ - _SetEPTxCount(bEpNum, wCount); -} -/******************************************************************************* -* Function Name : SetEPCountRxReg. -* Description : Set the Count Rx Register value. -* Input : *pdwReg: point to the register. -* wCount: the new register value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPCountRxReg(u32 *pdwReg, u16 wCount) -{ - _SetEPCountRxReg(dwReg, wCount); -} -/******************************************************************************* -* Function Name : SetEPRxCount -* Description : Set the Rx count. -* Input : bEpNum: Endpoint Number. -* wCount: the new count value. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPRxCount(u8 bEpNum, u16 wCount) -{ - _SetEPRxCount(bEpNum, wCount); -} -/******************************************************************************* -* Function Name : GetEPTxCount -* Description : Get the Tx count. -* Input : bEpNum: Endpoint Number. -* Output : None -* Return : Tx count value. -*******************************************************************************/ -u16 GetEPTxCount(u8 bEpNum) -{ - return(_GetEPTxCount(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPRxCount -* Description : Get the Rx count. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Rx count value. -*******************************************************************************/ -u16 GetEPRxCount(u8 bEpNum) -{ - return(_GetEPRxCount(bEpNum)); -} -/******************************************************************************* -* Function Name : SetEPDblBuffAddr -* Description : Set the addresses of the buffer 0 and 1. -* Input : bEpNum: Endpoint Number. -* wBuf0Addr: new address of buffer 0. -* wBuf1Addr: new address of buffer 1. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuffAddr(u8 bEpNum, u16 wBuf0Addr, u16 wBuf1Addr) -{ - _SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr); -} -/******************************************************************************* -* Function Name : SetEPDblBuf0Addr -* Description : Set the Buffer 1 address. -* Input : bEpNum: Endpoint Number -* wBuf0Addr: new address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuf0Addr(u8 bEpNum, u16 wBuf0Addr) -{ - _SetEPDblBuf0Addr(bEpNum, wBuf0Addr); -} -/******************************************************************************* -* Function Name : SetEPDblBuf1Addr -* Description : Set the Buffer 1 address. -* Input : bEpNum: Endpoint Number -* wBuf1Addr: new address. -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuf1Addr(u8 bEpNum, u16 wBuf1Addr) -{ - _SetEPDblBuf1Addr(bEpNum, wBuf1Addr); -} -/******************************************************************************* -* Function Name : GetEPDblBuf0Addr -* Description : Returns the address of the Buffer 0. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -u16 GetEPDblBuf0Addr(u8 bEpNum) -{ - return(_GetEPDblBuf0Addr(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPDblBuf1Addr -* Description : Returns the address of the Buffer 1. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Address of the Buffer 1. -*******************************************************************************/ -u16 GetEPDblBuf1Addr(u8 bEpNum) -{ - return(_GetEPDblBuf1Addr(bEpNum)); -} -/******************************************************************************* -* Function Name : SetEPDblBuffCount -* Description : Set the number of bytes for a double Buffer -* endpoint. -* Input : bEpNum,bDir, wCount -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuffCount(u8 bEpNum, u8 bDir, u16 wCount) -{ - _SetEPDblBuffCount(bEpNum, bDir, wCount); -} -/******************************************************************************* -* Function Name : SetEPDblBuf0Count -* Description : Set the number of bytes in the buffer 0 of a double Buffer -* endpoint. -* Input : bEpNum, bDir, wCount -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuf0Count(u8 bEpNum, u8 bDir, u16 wCount) -{ - _SetEPDblBuf0Count(bEpNum, bDir, wCount); -} -/******************************************************************************* -* Function Name : SetEPDblBuf1Count -* Description : Set the number of bytes in the buffer 0 of a double Buffer -* endpoint. -* Input : bEpNum, bDir, wCount -* Output : None. -* Return : None. -*******************************************************************************/ -void SetEPDblBuf1Count(u8 bEpNum, u8 bDir, u16 wCount) -{ - _SetEPDblBuf1Count(bEpNum, bDir, wCount); -} -/******************************************************************************* -* Function Name : GetEPDblBuf0Count -* Description : Returns the number of byte received in the buffer 0 of a double -* Buffer endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint Buffer 0 count -*******************************************************************************/ -u16 GetEPDblBuf0Count(u8 bEpNum) -{ - return(_GetEPDblBuf0Count(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPDblBuf1Count -* Description : Returns the number of data received in the buffer 1 of a double -* Buffer endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint Buffer 1 count. -*******************************************************************************/ -u16 GetEPDblBuf1Count(u8 bEpNum) -{ - return(_GetEPDblBuf1Count(bEpNum)); -} -/******************************************************************************* -* Function Name : GetEPDblBufDir -* Description : gets direction of the double buffered endpoint -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : EP_DBUF_OUT, EP_DBUF_IN, -* EP_DBUF_ERR if the endpoint counter not yet programmed. -*******************************************************************************/ -EP_DBUF_DIR GetEPDblBufDir(u8 bEpNum) -{ - if ((u16)(*_pEPRxCount(bEpNum) & 0xFC00) != 0) - return(EP_DBUF_OUT); - else if (((u16)(*_pEPTxCount(bEpNum)) & 0x03FF) != 0) - return(EP_DBUF_IN); - else - return(EP_DBUF_ERR); -} -/******************************************************************************* -* Function Name : FreeUserBuffer -* Description : free buffer used from the application realizing it to the line - toggles bit SW_BUF in the double buffered endpoint register -* Input : bEpNum, bDir -* Output : None. -* Return : None. -*******************************************************************************/ -void FreeUserBuffer(u8 bEpNum, u8 bDir) -{ - if (bDir == EP_DBUF_OUT) - { /* OUT double buffered endpoint */ - _ToggleDTOG_TX(bEpNum); - } - else if (bDir == EP_DBUF_IN) - { /* IN double buffered endpoint */ - _ToggleDTOG_RX(bEpNum); - } -} - -/******************************************************************************* -* Function Name : ToWord -* Description : merge two byte in a word. -* Input : bh: byte high, bl: bytes low. -* Output : None. -* Return : resulted word. -*******************************************************************************/ -u16 ToWord(u8 bh, u8 bl) -{ - u16 wRet; - wRet = (u16)bl | ((u16)bh << 8); - return(wRet); -} -/******************************************************************************* -* Function Name : ByteSwap -* Description : Swap two byte in a word. -* Input : wSwW: word to Swap. -* Output : None. -* Return : resulted word. -*******************************************************************************/ -u16 ByteSwap(u16 wSwW) -{ - u8 bTemp; - u16 wRet; - bTemp = (u8)(wSwW & 0xff); - wRet = (wSwW >> 8) | ((u16)bTemp << 8); - return(wRet); -} - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_regs.c +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Interface functions to USB cell registers +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Includes ------------------------------------------------------------------*/ +#include "usb_lib.h" + +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +/* Extern variables ----------------------------------------------------------*/ +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ + +/******************************************************************************* +* Function Name : SetCNTR. +* Description : Set the CNTR register value. +* Input : wRegValue: new register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetCNTR(u16 wRegValue) +{ + _SetCNTR(wRegValue); +} + +/******************************************************************************* +* Function Name : GetCNTR. +* Description : returns the CNTR register value. +* Input : None. +* Output : None. +* Return : CNTR register Value. +*******************************************************************************/ +u16 GetCNTR(void) +{ + return(_GetCNTR()); +} + +/******************************************************************************* +* Function Name : SetISTR. +* Description : Set the ISTR register value. +* Input : wRegValue: new register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetISTR(u16 wRegValue) +{ + _SetISTR(wRegValue); +} + +/******************************************************************************* +* Function Name : GetISTR +* Description : Returns the ISTR register value. +* Input : None. +* Output : None. +* Return : ISTR register Value +*******************************************************************************/ +u16 GetISTR(void) +{ + return(_GetISTR()); +} + +/******************************************************************************* +* Function Name : GetFNR +* Description : Returns the FNR register value. +* Input : None. +* Output : None. +* Return : FNR register Value +*******************************************************************************/ +u16 GetFNR(void) +{ + return(_GetFNR()); +} + +/******************************************************************************* +* Function Name : SetDADDR +* Description : Set the DADDR register value. +* Input : wRegValue: new register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetDADDR(u16 wRegValue) +{ + _SetDADDR(wRegValue); +} + +/******************************************************************************* +* Function Name : GetDADDR +* Description : Returns the DADDR register value. +* Input : None. +* Output : None. +* Return : DADDR register Value +*******************************************************************************/ +u16 GetDADDR(void) +{ + return(_GetDADDR()); +} + +/******************************************************************************* +* Function Name : SetBTABLE +* Description : Set the BTABLE. +* Input : wRegValue: New register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetBTABLE(u16 wRegValue) +{ + _SetBTABLE(wRegValue); +} + +/******************************************************************************* +* Function Name : GetBTABLE. +* Description : Returns the BTABLE register value. +* Input : None. +* Output : None. +* Return : BTABLE address. +*******************************************************************************/ +u16 GetBTABLE(void) +{ + return(_GetBTABLE()); +} + +/******************************************************************************* +* Function Name : SetENDPOINT +* Description : Setthe Endpoint register value. +* Input : bEpNum: Endpoint Number. +* wRegValue. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetENDPOINT(u8 bEpNum, u16 wRegValue) +{ + _SetENDPOINT(bEpNum, wRegValue); +} + +/******************************************************************************* +* Function Name : GetENDPOINT +* Description : Return the Endpoint register value. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint register value. +*******************************************************************************/ +u16 GetENDPOINT(u8 bEpNum) +{ + return(_GetENDPOINT(bEpNum)); +} + +/******************************************************************************* +* Function Name : SetEPType +* Description : sets the type in the endpoint register. +* Input : bEpNum: Endpoint Number. +* wType: type definition. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPType(u8 bEpNum, u16 wType) +{ + _SetEPType(bEpNum, wType); +} + +/******************************************************************************* +* Function Name : GetEPType +* Description : Returns the endpoint type. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint Type +*******************************************************************************/ +u16 GetEPType(u8 bEpNum) +{ + return(_GetEPType(bEpNum)); +} + +/******************************************************************************* +* Function Name : SetEPTxStatus +* Description : Set the status of Tx endpoint. +* Input : bEpNum: Endpoint Number. +* wState: new state. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPTxStatus(u8 bEpNum, u16 wState) +{ + _SetEPTxStatus(bEpNum, wState); +} + +/******************************************************************************* +* Function Name : SetEPRxStatus +* Description : Set the status of Rx endpoint. +* Input : bEpNum: Endpoint Number. +* wState: new state. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPRxStatus(u8 bEpNum, u16 wState) +{ + _SetEPRxStatus(bEpNum, wState); +} + +/******************************************************************************* +* Function Name : SetDouBleBuffEPStall +* Description : sets the status for Double Buffer Endpoint to STALL +* Input : bEpNum: Endpoint Number. +* bDir: Endpoint direction. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetDouBleBuffEPStall(u8 bEpNum, u8 bDir) +{ + u16 Endpoint_DTOG_Status; + Endpoint_DTOG_Status = GetENDPOINT(bEpNum); + if (bDir == EP_DBUF_OUT) + { /* OUT double buffered endpoint */ + _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPRX_DTOG1); + } + else if (bDir == EP_DBUF_IN) + { /* IN double buffered endpoint */ + _SetENDPOINT(bEpNum, Endpoint_DTOG_Status & ~EPTX_DTOG1); + } +} + +/******************************************************************************* +* Function Name : GetEPTxStatus +* Description : Returns the endpoint Tx status. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint TX Status +*******************************************************************************/ +u16 GetEPTxStatus(u8 bEpNum) +{ + return(_GetEPTxStatus(bEpNum)); +} + +/******************************************************************************* +* Function Name : GetEPRxStatus +* Description : Returns the endpoint Rx status. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint RX Status +*******************************************************************************/ +u16 GetEPRxStatus(u8 bEpNum) +{ + return(_GetEPRxStatus(bEpNum)); +} + +/******************************************************************************* +* Function Name : SetEPTxValid +* Description : Valid the endpoint Tx Status. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPTxValid(u8 bEpNum) +{ + _SetEPTxStatus(bEpNum, EP_TX_VALID); +} + +/******************************************************************************* +* Function Name : SetEPRxValid +* Description : Valid the endpoint Rx Status. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPRxValid(u8 bEpNum) +{ + _SetEPRxStatus(bEpNum, EP_RX_VALID); +} + +/******************************************************************************* +* Function Name : SetEP_KIND +* Description : Clear the EP_KIND bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEP_KIND(u8 bEpNum) +{ + _SetEP_KIND(bEpNum); +} + +/******************************************************************************* +* Function Name : ClearEP_KIND +* Description : set the EP_KIND bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearEP_KIND(u8 bEpNum) +{ + _ClearEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : Clear_Status_Out +* Description : Clear the Status Out of the related Endpoint +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void Clear_Status_Out(u8 bEpNum) +{ + _ClearEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : Set_Status_Out +* Description : Set the Status Out of the related Endpoint +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void Set_Status_Out(u8 bEpNum) +{ + _SetEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : SetEPDoubleBuff +* Description : Enable the double buffer feature for the endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDoubleBuff(u8 bEpNum) +{ + _SetEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : ClearEPDoubleBuff +* Description : Disable the double buffer feature for the endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearEPDoubleBuff(u8 bEpNum) +{ + _ClearEP_KIND(bEpNum); +} +/******************************************************************************* +* Function Name : GetTxStallStatus +* Description : Returns the Stall status of the Tx endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Tx Stall status. +*******************************************************************************/ +u16 GetTxStallStatus(u8 bEpNum) +{ + return(_GetTxStallStatus(bEpNum)); +} +/******************************************************************************* +* Function Name : GetRxStallStatus +* Description : Returns the Stall status of the Rx endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Rx Stall status. +*******************************************************************************/ +u16 GetRxStallStatus(u8 bEpNum) +{ + return(_GetRxStallStatus(bEpNum)); +} +/******************************************************************************* +* Function Name : ClearEP_CTR_RX +* Description : Clear the CTR_RX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearEP_CTR_RX(u8 bEpNum) +{ + _ClearEP_CTR_RX(bEpNum); +} +/******************************************************************************* +* Function Name : ClearEP_CTR_TX +* Description : Clear the CTR_TX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearEP_CTR_TX(u8 bEpNum) +{ + _ClearEP_CTR_TX(bEpNum); +} +/******************************************************************************* +* Function Name : ToggleDTOG_RX +* Description : Toggle the DTOG_RX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ToggleDTOG_RX(u8 bEpNum) +{ + _ToggleDTOG_RX(bEpNum); +} +/******************************************************************************* +* Function Name : ToggleDTOG_TX +* Description : Toggle the DTOG_TX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ToggleDTOG_TX(u8 bEpNum) +{ + _ToggleDTOG_TX(bEpNum); +} +/******************************************************************************* +* Function Name : ClearDTOG_RX. +* Description : Clear the DTOG_RX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearDTOG_RX(u8 bEpNum) +{ + _ClearDTOG_RX(bEpNum); +} +/******************************************************************************* +* Function Name : ClearDTOG_TX. +* Description : Clear the DTOG_TX bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +void ClearDTOG_TX(u8 bEpNum) +{ + _ClearDTOG_TX(bEpNum); +} +/******************************************************************************* +* Function Name : SetEPAddress +* Description : Set the endpoint address. +* Input : bEpNum: Endpoint Number. +* bAddr: New endpoint address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPAddress(u8 bEpNum, u8 bAddr) +{ + _SetEPAddress(bEpNum, bAddr); +} +/******************************************************************************* +* Function Name : GetEPAddress +* Description : Get the endpoint address. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint address. +*******************************************************************************/ +u8 GetEPAddress(u8 bEpNum) +{ + return(_GetEPAddress(bEpNum)); +} +/******************************************************************************* +* Function Name : SetEPTxAddr +* Description : Set the endpoint Tx buffer address. +* Input : bEpNum: Endpoint Number. +* wAddr: new address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPTxAddr(u8 bEpNum, u16 wAddr) +{ + _SetEPTxAddr(bEpNum, wAddr); +} +/******************************************************************************* +* Function Name : SetEPRxAddr +* Description : Set the endpoint Rx buffer address. +* Input : bEpNum: Endpoint Number. +* wAddr: new address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPRxAddr(u8 bEpNum, u16 wAddr) +{ + _SetEPRxAddr(bEpNum, wAddr); +} +/******************************************************************************* +* Function Name : GetEPTxAddr +* Description : Returns the endpoint Tx buffer address. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Rx buffer address. +*******************************************************************************/ +u16 GetEPTxAddr(u8 bEpNum) +{ + return(_GetEPTxAddr(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPRxAddr. +* Description : Returns the endpoint Rx buffer address. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Rx buffer address. +*******************************************************************************/ +u16 GetEPRxAddr(u8 bEpNum) +{ + return(_GetEPRxAddr(bEpNum)); +} +/******************************************************************************* +* Function Name : SetEPTxCount. +* Description : Set the Tx count. +* Input : bEpNum: Endpoint Number. +* wCount: new count value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPTxCount(u8 bEpNum, u16 wCount) +{ + _SetEPTxCount(bEpNum, wCount); +} +/******************************************************************************* +* Function Name : SetEPCountRxReg. +* Description : Set the Count Rx Register value. +* Input : *pdwReg: point to the register. +* wCount: the new register value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPCountRxReg(u32 *pdwReg, u16 wCount) +{ + _SetEPCountRxReg(dwReg, wCount); +} +/******************************************************************************* +* Function Name : SetEPRxCount +* Description : Set the Rx count. +* Input : bEpNum: Endpoint Number. +* wCount: the new count value. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPRxCount(u8 bEpNum, u16 wCount) +{ + _SetEPRxCount(bEpNum, wCount); +} +/******************************************************************************* +* Function Name : GetEPTxCount +* Description : Get the Tx count. +* Input : bEpNum: Endpoint Number. +* Output : None +* Return : Tx count value. +*******************************************************************************/ +u16 GetEPTxCount(u8 bEpNum) +{ + return(_GetEPTxCount(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPRxCount +* Description : Get the Rx count. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Rx count value. +*******************************************************************************/ +u16 GetEPRxCount(u8 bEpNum) +{ + return(_GetEPRxCount(bEpNum)); +} +/******************************************************************************* +* Function Name : SetEPDblBuffAddr +* Description : Set the addresses of the buffer 0 and 1. +* Input : bEpNum: Endpoint Number. +* wBuf0Addr: new address of buffer 0. +* wBuf1Addr: new address of buffer 1. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuffAddr(u8 bEpNum, u16 wBuf0Addr, u16 wBuf1Addr) +{ + _SetEPDblBuffAddr(bEpNum, wBuf0Addr, wBuf1Addr); +} +/******************************************************************************* +* Function Name : SetEPDblBuf0Addr +* Description : Set the Buffer 1 address. +* Input : bEpNum: Endpoint Number +* wBuf0Addr: new address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuf0Addr(u8 bEpNum, u16 wBuf0Addr) +{ + _SetEPDblBuf0Addr(bEpNum, wBuf0Addr); +} +/******************************************************************************* +* Function Name : SetEPDblBuf1Addr +* Description : Set the Buffer 1 address. +* Input : bEpNum: Endpoint Number +* wBuf1Addr: new address. +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuf1Addr(u8 bEpNum, u16 wBuf1Addr) +{ + _SetEPDblBuf1Addr(bEpNum, wBuf1Addr); +} +/******************************************************************************* +* Function Name : GetEPDblBuf0Addr +* Description : Returns the address of the Buffer 0. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +u16 GetEPDblBuf0Addr(u8 bEpNum) +{ + return(_GetEPDblBuf0Addr(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPDblBuf1Addr +* Description : Returns the address of the Buffer 1. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Address of the Buffer 1. +*******************************************************************************/ +u16 GetEPDblBuf1Addr(u8 bEpNum) +{ + return(_GetEPDblBuf1Addr(bEpNum)); +} +/******************************************************************************* +* Function Name : SetEPDblBuffCount +* Description : Set the number of bytes for a double Buffer +* endpoint. +* Input : bEpNum,bDir, wCount +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuffCount(u8 bEpNum, u8 bDir, u16 wCount) +{ + _SetEPDblBuffCount(bEpNum, bDir, wCount); +} +/******************************************************************************* +* Function Name : SetEPDblBuf0Count +* Description : Set the number of bytes in the buffer 0 of a double Buffer +* endpoint. +* Input : bEpNum, bDir, wCount +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuf0Count(u8 bEpNum, u8 bDir, u16 wCount) +{ + _SetEPDblBuf0Count(bEpNum, bDir, wCount); +} +/******************************************************************************* +* Function Name : SetEPDblBuf1Count +* Description : Set the number of bytes in the buffer 0 of a double Buffer +* endpoint. +* Input : bEpNum, bDir, wCount +* Output : None. +* Return : None. +*******************************************************************************/ +void SetEPDblBuf1Count(u8 bEpNum, u8 bDir, u16 wCount) +{ + _SetEPDblBuf1Count(bEpNum, bDir, wCount); +} +/******************************************************************************* +* Function Name : GetEPDblBuf0Count +* Description : Returns the number of byte received in the buffer 0 of a double +* Buffer endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint Buffer 0 count +*******************************************************************************/ +u16 GetEPDblBuf0Count(u8 bEpNum) +{ + return(_GetEPDblBuf0Count(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPDblBuf1Count +* Description : Returns the number of data received in the buffer 1 of a double +* Buffer endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint Buffer 1 count. +*******************************************************************************/ +u16 GetEPDblBuf1Count(u8 bEpNum) +{ + return(_GetEPDblBuf1Count(bEpNum)); +} +/******************************************************************************* +* Function Name : GetEPDblBufDir +* Description : gets direction of the double buffered endpoint +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : EP_DBUF_OUT, EP_DBUF_IN, +* EP_DBUF_ERR if the endpoint counter not yet programmed. +*******************************************************************************/ +EP_DBUF_DIR GetEPDblBufDir(u8 bEpNum) +{ + if ((u16)(*_pEPRxCount(bEpNum) & 0xFC00) != 0) + return(EP_DBUF_OUT); + else if (((u16)(*_pEPTxCount(bEpNum)) & 0x03FF) != 0) + return(EP_DBUF_IN); + else + return(EP_DBUF_ERR); +} +/******************************************************************************* +* Function Name : FreeUserBuffer +* Description : free buffer used from the application realizing it to the line + toggles bit SW_BUF in the double buffered endpoint register +* Input : bEpNum, bDir +* Output : None. +* Return : None. +*******************************************************************************/ +void FreeUserBuffer(u8 bEpNum, u8 bDir) +{ + if (bDir == EP_DBUF_OUT) + { /* OUT double buffered endpoint */ + _ToggleDTOG_TX(bEpNum); + } + else if (bDir == EP_DBUF_IN) + { /* IN double buffered endpoint */ + _ToggleDTOG_RX(bEpNum); + } +} + +/******************************************************************************* +* Function Name : ToWord +* Description : merge two byte in a word. +* Input : bh: byte high, bl: bytes low. +* Output : None. +* Return : resulted word. +*******************************************************************************/ +u16 ToWord(u8 bh, u8 bl) +{ + u16 wRet; + wRet = (u16)bl | ((u16)bh << 8); + return(wRet); +} +/******************************************************************************* +* Function Name : ByteSwap +* Description : Swap two byte in a word. +* Input : wSwW: word to Swap. +* Output : None. +* Return : resulted word. +*******************************************************************************/ +u16 ByteSwap(u16 wSwW) +{ + u8 bTemp; + u16 wRet; + bTemp = (u8)(wSwW & 0xff); + wRet = (wSwW >> 8) | ((u16)bTemp << 8); + return(wRet); +} + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_regs.h b/src/usb_lib/usb_regs.h similarity index 94% rename from usb_lib/usb_regs.h rename to src/usb_lib/usb_regs.h index 8f32e44..6927f6e 100644 --- a/usb_lib/usb_regs.h +++ b/src/usb_lib/usb_regs.h @@ -1,619 +1,619 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_regs.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Interface prototype functions to USB cell registers -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_REGS_H -#define __USB_REGS_H - -/* Includes ------------------------------------------------------------------*/ -/* Exported types ------------------------------------------------------------*/ -typedef enum _EP_DBUF_DIR -{ - /* double buffered endpoint direction */ - EP_DBUF_ERR, - EP_DBUF_OUT, - EP_DBUF_IN -}EP_DBUF_DIR; - -/* endpoint buffer number */ -enum EP_BUF_NUM -{ - EP_NOBUF, - EP_BUF0, - EP_BUF1 -}; - -/* Exported constants --------------------------------------------------------*/ -#define RegBase (0x40005C00L) /* USB_IP Peripheral Registers base address */ -#define PMAAddr (0x40006000L) /* USB_IP Packet Memory Area base address */ - -/******************************************************************************/ -/* General registers */ -/******************************************************************************/ - -/* Control register */ -#define CNTR ((volatile unsigned *)(RegBase + 0x40)) -/* Interrupt status register */ -#define ISTR ((volatile unsigned *)(RegBase + 0x44)) -/* Frame number register */ -#define FNR ((volatile unsigned *)(RegBase + 0x48)) -/* Device address register */ -#define DADDR ((volatile unsigned *)(RegBase + 0x4C)) -/* Buffer Table address register */ -#define BTABLE ((volatile unsigned *)(RegBase + 0x50)) -/******************************************************************************/ -/* Endpoint registers */ -/******************************************************************************/ -#define EP0REG ((volatile unsigned *)(RegBase)) /* endpoint 0 register address */ - -/* endpoints enumeration */ -#define ENDP0 ((u8)0) -#define ENDP1 ((u8)1) -#define ENDP2 ((u8)2) -#define ENDP3 ((u8)3) -#define ENDP4 ((u8)4) -#define ENDP5 ((u8)5) -#define ENDP6 ((u8)6) -#define ENDP7 ((u8)7) -/******************************************************************************/ -/* ISTR interrupt events */ -/******************************************************************************/ -#define ISTR_CTR (0x8000) /* Correct TRansfer (clear-only bit) */ -#define ISTR_DOVR (0x4000) /* DMA OVeR/underrun (clear-only bit) */ -#define ISTR_ERR (0x2000) /* ERRor (clear-only bit) */ -#define ISTR_WKUP (0x1000) /* WaKe UP (clear-only bit) */ -#define ISTR_SUSP (0x0800) /* SUSPend (clear-only bit) */ -#define ISTR_RESET (0x0400) /* RESET (clear-only bit) */ -#define ISTR_SOF (0x0200) /* Start Of Frame (clear-only bit) */ -#define ISTR_ESOF (0x0100) /* Expected Start Of Frame (clear-only bit) */ - - -#define ISTR_DIR (0x0010) /* DIRection of transaction (read-only bit) */ -#define ISTR_EP_ID (0x000F) /* EndPoint IDentifier (read-only bit) */ - -#define CLR_CTR (~ISTR_CTR) /* clear Correct TRansfer bit */ -#define CLR_DOVR (~ISTR_DOVR) /* clear DMA OVeR/underrun bit*/ -#define CLR_ERR (~ISTR_ERR) /* clear ERRor bit */ -#define CLR_WKUP (~ISTR_WKUP) /* clear WaKe UP bit */ -#define CLR_SUSP (~ISTR_SUSP) /* clear SUSPend bit */ -#define CLR_RESET (~ISTR_RESET) /* clear RESET bit */ -#define CLR_SOF (~ISTR_SOF) /* clear Start Of Frame bit */ -#define CLR_ESOF (~ISTR_ESOF) /* clear Expected Start Of Frame bit */ - -/******************************************************************************/ -/* CNTR control register bits definitions */ -/******************************************************************************/ -#define CNTR_CTRM (0x8000) /* Correct TRansfer Mask */ -#define CNTR_DOVRM (0x4000) /* DMA OVeR/underrun Mask */ -#define CNTR_ERRM (0x2000) /* ERRor Mask */ -#define CNTR_WKUPM (0x1000) /* WaKe UP Mask */ -#define CNTR_SUSPM (0x0800) /* SUSPend Mask */ -#define CNTR_RESETM (0x0400) /* RESET Mask */ -#define CNTR_SOFM (0x0200) /* Start Of Frame Mask */ -#define CNTR_ESOFM (0x0100) /* Expected Start Of Frame Mask */ - - -#define CNTR_RESUME (0x0010) /* RESUME request */ -#define CNTR_FSUSP (0x0008) /* Force SUSPend */ -#define CNTR_LPMODE (0x0004) /* Low-power MODE */ -#define CNTR_PDWN (0x0002) /* Power DoWN */ -#define CNTR_FRES (0x0001) /* Force USB RESet */ - -/******************************************************************************/ -/* FNR Frame Number Register bit definitions */ -/******************************************************************************/ -#define FNR_RXDP (0x8000) /* status of D+ data line */ -#define FNR_RXDM (0x4000) /* status of D- data line */ -#define FNR_LCK (0x2000) /* LoCKed */ -#define FNR_LSOF (0x1800) /* Lost SOF */ -#define FNR_FN (0x07FF) /* Frame Number */ -/******************************************************************************/ -/* DADDR Device ADDRess bit definitions */ -/******************************************************************************/ -#define DADDR_EF (0x80) -#define DADDR_ADD (0x7F) -/******************************************************************************/ -/* Endpoint register */ -/******************************************************************************/ -/* bit positions */ -#define EP_CTR_RX (0x8000) /* EndPoint Correct TRansfer RX */ -#define EP_DTOG_RX (0x4000) /* EndPoint Data TOGGLE RX */ -#define EPRX_STAT (0x3000) /* EndPoint RX STATus bit field */ -#define EP_SETUP (0x0800) /* EndPoint SETUP */ -#define EP_T_FIELD (0x0600) /* EndPoint TYPE */ -#define EP_KIND (0x0100) /* EndPoint KIND */ -#define EP_CTR_TX (0x0080) /* EndPoint Correct TRansfer TX */ -#define EP_DTOG_TX (0x0040) /* EndPoint Data TOGGLE TX */ -#define EPTX_STAT (0x0030) /* EndPoint TX STATus bit field */ -#define EPADDR_FIELD (0x000F) /* EndPoint ADDRess FIELD */ - -/* EndPoint REGister MASK (no toggle fields) */ -#define EPREG_MASK (EP_CTR_RX|EP_SETUP|EP_T_FIELD|EP_KIND|EP_CTR_TX|EPADDR_FIELD) - -/* EP_TYPE[1:0] EndPoint TYPE */ -#define EP_TYPE_MASK (0x0600) /* EndPoint TYPE Mask */ -#define EP_BULK (0x0000) /* EndPoint BULK */ -#define EP_CONTROL (0x0200) /* EndPoint CONTROL */ -#define EP_ISOCHRONOUS (0x0400) /* EndPoint ISOCHRONOUS */ -#define EP_INTERRUPT (0x0600) /* EndPoint INTERRUPT */ -#define EP_T_MASK (~EP_T_FIELD & EPREG_MASK) - - -/* EP_KIND EndPoint KIND */ -#define EPKIND_MASK (~EP_KIND & EPREG_MASK) - -/* STAT_TX[1:0] STATus for TX transfer */ -#define EP_TX_DIS (0x0000) /* EndPoint TX DISabled */ -#define EP_TX_STALL (0x0010) /* EndPoint TX STALLed */ -#define EP_TX_NAK (0x0020) /* EndPoint TX NAKed */ -#define EP_TX_VALID (0x0030) /* EndPoint TX VALID */ -#define EPTX_DTOG1 (0x0010) /* EndPoint TX Data TOGgle bit1 */ -#define EPTX_DTOG2 (0x0020) /* EndPoint TX Data TOGgle bit2 */ -#define EPTX_DTOGMASK (EPTX_STAT|EPREG_MASK) - -/* STAT_RX[1:0] STATus for RX transfer */ -#define EP_RX_DIS (0x0000) /* EndPoint RX DISabled */ -#define EP_RX_STALL (0x1000) /* EndPoint RX STALLed */ -#define EP_RX_NAK (0x2000) /* EndPoint RX NAKed */ -#define EP_RX_VALID (0x3000) /* EndPoint RX VALID */ -#define EPRX_DTOG1 (0x1000) /* EndPoint RX Data TOGgle bit1 */ -#define EPRX_DTOG2 (0x2000) /* EndPoint RX Data TOGgle bit1 */ -#define EPRX_DTOGMASK (EPRX_STAT|EPREG_MASK) -/* Exported macro ------------------------------------------------------------*/ -/* SetCNTR */ -#define _SetCNTR(wRegValue) (*CNTR = (u16)wRegValue) - -/* SetISTR */ -#define _SetISTR(wRegValue) (*ISTR = (u16)wRegValue) - -/* SetDADDR */ -#define _SetDADDR(wRegValue) (*DADDR = (u16)wRegValue) - -/* SetBTABLE */ -#define _SetBTABLE(wRegValue)(*BTABLE = (u16)(wRegValue & 0xFFF8)) - -/* GetCNTR */ -#define _GetCNTR() ((u16) *CNTR) - -/* GetISTR */ -#define _GetISTR() ((u16) *ISTR) - -/* GetFNR */ -#define _GetFNR() ((u16) *FNR) - -/* GetDADDR */ -#define _GetDADDR() ((u16) *DADDR) - -/* GetBTABLE */ -#define _GetBTABLE() ((u16) *BTABLE) - -/* SetENDPOINT */ -#define _SetENDPOINT(bEpNum,wRegValue) (*(EP0REG + bEpNum)= \ - (u16)wRegValue) - -/* GetENDPOINT */ -#define _GetENDPOINT(bEpNum) ((u16)(*(EP0REG + bEpNum))) - -/******************************************************************************* -* Macro Name : SetEPType -* Description : sets the type in the endpoint register(bits EP_TYPE[1:0]) -* Input : bEpNum: Endpoint Number. -* wType -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPType(bEpNum,wType) (_SetENDPOINT(bEpNum,\ - ((_GetENDPOINT(bEpNum) & EP_T_MASK) | wType))) - -/******************************************************************************* -* Macro Name : GetEPType -* Description : gets the type in the endpoint register(bits EP_TYPE[1:0]) -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : Endpoint Type -*******************************************************************************/ -#define _GetEPType(bEpNum) (_GetENDPOINT(bEpNum) & EP_T_FIELD) - -/******************************************************************************* -* Macro Name : SetEPTxStatus -* Description : sets the status for tx transfer (bits STAT_TX[1:0]). -* Input : bEpNum: Endpoint Number. -* wState: new state -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPTxStatus(bEpNum,wState) {\ - register u16 _wRegVal; \ - _wRegVal = _GetENDPOINT(bEpNum) & EPTX_DTOGMASK;\ - /* toggle first bit ? */ \ - if((EPTX_DTOG1 & wState)!= 0) \ - _wRegVal ^= EPTX_DTOG1; \ - /* toggle second bit ? */ \ - if((EPTX_DTOG2 & wState)!= 0) \ - _wRegVal ^= EPTX_DTOG2; \ - _SetENDPOINT(bEpNum, _wRegVal); \ - } /* _SetEPTxStatus */ - -/******************************************************************************* -* Macro Name : SetEPRxStatus -* Description : sets the status for rx transfer (bits STAT_TX[1:0]) -* Input : bEpNum: Endpoint Number. -* wState: new state. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPRxStatus(bEpNum,wState) {\ - register u16 _wRegVal; \ - \ - _wRegVal = _GetENDPOINT(bEpNum) & EPRX_DTOGMASK;\ - /* toggle first bit ? */ \ - if((EPRX_DTOG1 & wState)!= 0) \ - _wRegVal ^= EPRX_DTOG1; \ - /* toggle second bit ? */ \ - if((EPRX_DTOG2 & wState)!= 0) \ - _wRegVal ^= EPRX_DTOG2; \ - _SetENDPOINT(bEpNum, _wRegVal); \ - } /* _SetEPRxStatus */ -/******************************************************************************* -* Macro Name : GetEPTxStatus / GetEPRxStatus -* Description : gets the status for tx/rx transfer (bits STAT_TX[1:0] -* /STAT_RX[1:0]) -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : status . -*******************************************************************************/ -#define _GetEPTxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPTX_STAT) - -#define _GetEPRxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPRX_STAT) - -/******************************************************************************* -* Macro Name : SetEPTxValid / SetEPRxValid -* Description : sets directly the VALID tx/rx-status into the enpoint register -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPTxValid(bEpNum) (_SetEPTxStatus(bEpNum, EP_TX_VALID)) - -#define _SetEPRxValid(bEpNum) (_SetEPRxStatus(bEpNum, EP_RX_VALID)) - -/******************************************************************************* -* Macro Name : GetTxStallStatus / GetRxStallStatus. -* Description : checks stall condition in an endpoint. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : TRUE = endpoint in stall condition. -*******************************************************************************/ -#define _GetTxStallStatus(bEpNum) (_GetEPTxStatus(bEpNum) \ - == EP_TX_STALL) -#define _GetRxStallStatus(bEpNum) (_GetEPRxStatus(bEpNum) \ - == EP_RX_STALL) - -/******************************************************************************* -* Macro Name : SetEP_KIND / ClearEP_KIND. -* Description : set & clear EP_KIND bit. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, \ - (_GetENDPOINT(bEpNum) | EP_KIND) & EPREG_MASK)) -#define _ClearEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, \ - (_GetENDPOINT(bEpNum) & EPKIND_MASK))) - -/******************************************************************************* -* Macro Name : Set_Status_Out / Clear_Status_Out. -* Description : Sets/clears directly STATUS_OUT bit in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _Set_Status_Out(bEpNum) _SetEP_KIND(bEpNum) -#define _Clear_Status_Out(bEpNum) _ClearEP_KIND(bEpNum) - -/******************************************************************************* -* Macro Name : SetEPDoubleBuff / ClearEPDoubleBuff. -* Description : Sets/clears directly EP_KIND bit in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPDoubleBuff(bEpNum) _SetEP_KIND(bEpNum) -#define _ClearEPDoubleBuff(bEpNum) _ClearEP_KIND(bEpNum) - -/******************************************************************************* -* Macro Name : ClearEP_CTR_RX / ClearEP_CTR_TX. -* Description : Clears bit CTR_RX / CTR_TX in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _ClearEP_CTR_RX(bEpNum) (_SetENDPOINT(bEpNum,\ - _GetENDPOINT(bEpNum) & 0x7FFF & EPREG_MASK)) -#define _ClearEP_CTR_TX(bEpNum) (_SetENDPOINT(bEpNum,\ - _GetENDPOINT(bEpNum) & 0xFF7F & EPREG_MASK)) - -/******************************************************************************* -* Macro Name : ToggleDTOG_RX / ToggleDTOG_TX . -* Description : Toggles DTOG_RX / DTOG_TX bit in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _ToggleDTOG_RX(bEpNum) (_SetENDPOINT(bEpNum, \ - EP_DTOG_RX | _GetENDPOINT(bEpNum) & EPREG_MASK)) -#define _ToggleDTOG_TX(bEpNum) (_SetENDPOINT(bEpNum, \ - EP_DTOG_TX | _GetENDPOINT(bEpNum) & EPREG_MASK)) - -/******************************************************************************* -* Macro Name : ClearDTOG_RX / ClearDTOG_TX. -* Description : Clears DTOG_RX / DTOG_TX bit in the endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _ClearDTOG_RX(bEpNum) if((_GetENDPOINT(bEpNum) & EP_DTOG_RX) != 0)\ - _ToggleDTOG_RX(bEpNum) -#define _ClearDTOG_TX(bEpNum) if((_GetENDPOINT(bEpNum) & EP_DTOG_TX) != 0)\ - _ToggleDTOG_TX(bEpNum) -/******************************************************************************* -* Macro Name : SetEPAddress. -* Description : Sets address in an endpoint register. -* Input : bEpNum: Endpoint Number. -* bAddr: Address. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPAddress(bEpNum,bAddr) _SetENDPOINT(bEpNum,\ - _GetENDPOINT(bEpNum) & EPREG_MASK | bAddr) - -/******************************************************************************* -* Macro Name : GetEPAddress. -* Description : Gets address in an endpoint register. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _GetEPAddress(bEpNum) ((u8)(_GetENDPOINT(bEpNum) & EPADDR_FIELD)) - -#define _pEPTxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8 )*2 + PMAAddr)) -#define _pEPTxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+2)*2 + PMAAddr)) -#define _pEPRxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+4)*2 + PMAAddr)) -#define _pEPRxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+6)*2 + PMAAddr)) - -/******************************************************************************* -* Macro Name : SetEPTxAddr / SetEPRxAddr. -* Description : sets address of the tx/rx buffer. -* Input : bEpNum: Endpoint Number. -* wAddr: address to be set (must be word aligned). -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPTxAddr(bEpNum,wAddr) (*_pEPTxAddr(bEpNum) = ((wAddr >> 1) << 1)) -#define _SetEPRxAddr(bEpNum,wAddr) (*_pEPRxAddr(bEpNum) = ((wAddr >> 1) << 1)) - -/******************************************************************************* -* Macro Name : GetEPTxAddr / GetEPRxAddr. -* Description : Gets address of the tx/rx buffer. -* Input : bEpNum: Endpoint Number. -* Output : None. -* Return : address of the buffer. -*******************************************************************************/ -#define _GetEPTxAddr(bEpNum) ((u16)*_pEPTxAddr(bEpNum)) -#define _GetEPRxAddr(bEpNum) ((u16)*_pEPRxAddr(bEpNum)) - -/******************************************************************************* -* Macro Name : SetEPCountRxReg. -* Description : Sets counter of rx buffer with no. of blocks. -* Input : pdwReg: pointer to counter. -* wCount: Counter. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _BlocksOf32(dwReg,wCount,wNBlocks) {\ - wNBlocks = wCount >> 5;\ - if((wCount & 0x1f) == 0)\ - wNBlocks--;\ - *pdwReg = (u32)((wNBlocks << 10) | 0x8000);\ - }/* _BlocksOf32 */ - -#define _BlocksOf2(dwReg,wCount,wNBlocks) {\ - wNBlocks = wCount >> 1;\ - if((wCount & 0x1) != 0)\ - wNBlocks++;\ - *pdwReg = (u32)(wNBlocks << 10);\ - }/* _BlocksOf2 */ - -#define _SetEPCountRxReg(dwReg,wCount) {\ - u16 wNBlocks;\ - if(wCount > 62){_BlocksOf32(dwReg,wCount,wNBlocks);}\ - else {_BlocksOf2(dwReg,wCount,wNBlocks);}\ - }/* _SetEPCountRxReg */ - - - -#define _SetEPRxDblBuf0Count(bEpNum,wCount) {\ - u32 *pdwReg = _pEPTxCount(bEpNum); \ - _SetEPCountRxReg(pdwReg, wCount);\ - } -/******************************************************************************* -* Macro Name : SetEPTxCount / SetEPRxCount. -* Description : sets counter for the tx/rx buffer. -* Input : bEpNum: endpoint number. -* wCount: Counter value. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPTxCount(bEpNum,wCount) (*_pEPTxCount(bEpNum) = wCount) -#define _SetEPRxCount(bEpNum,wCount) {\ - u32 *pdwReg = _pEPRxCount(bEpNum); \ - _SetEPCountRxReg(pdwReg, wCount);\ - } -/******************************************************************************* -* Macro Name : GetEPTxCount / GetEPRxCount. -* Description : gets counter of the tx buffer. -* Input : bEpNum: endpoint number. -* Output : None. -* Return : Counter value. -*******************************************************************************/ -#define _GetEPTxCount(bEpNum)((u16)(*_pEPTxCount(bEpNum)) & 0x3ff) -#define _GetEPRxCount(bEpNum)((u16)(*_pEPRxCount(bEpNum)) & 0x3ff) - -/******************************************************************************* -* Macro Name : SetEPDblBuf0Addr / SetEPDblBuf1Addr. -* Description : Sets buffer 0/1 address in a double buffer endpoint. -* Input : bEpNum: endpoint number. -* : wBuf0Addr: buffer 0 address. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPDblBuf0Addr(bEpNum,wBuf0Addr) {_SetEPTxAddr(bEpNum, wBuf0Addr);} -#define _SetEPDblBuf1Addr(bEpNum,wBuf1Addr) {_SetEPRxAddr(bEpNum, wBuf1Addr);} - -/******************************************************************************* -* Macro Name : SetEPDblBuffAddr. -* Description : Sets addresses in a double buffer endpoint. -* Input : bEpNum: endpoint number. -* : wBuf0Addr: buffer 0 address. -* : wBuf1Addr = buffer 1 address. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPDblBuffAddr(bEpNum,wBuf0Addr,wBuf1Addr) { \ - _SetEPDblBuf0Addr(bEpNum, wBuf0Addr);\ - _SetEPDblBuf1Addr(bEpNum, wBuf1Addr);\ - } /* _SetEPDblBuffAddr */ - -/******************************************************************************* -* Macro Name : GetEPDblBuf0Addr / GetEPDblBuf1Addr. -* Description : Gets buffer 0/1 address of a double buffer endpoint. -* Input : bEpNum: endpoint number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _GetEPDblBuf0Addr(bEpNum) (_GetEPTxAddr(bEpNum)) -#define _GetEPDblBuf1Addr(bEpNum) (_GetEPRxAddr(bEpNum)) - -/******************************************************************************* -* Macro Name : SetEPDblBuffCount / SetEPDblBuf0Count / SetEPDblBuf1Count. -* Description : Gets buffer 0/1 address of a double buffer endpoint. -* Input : bEpNum: endpoint number. -* : bDir: endpoint dir EP_DBUF_OUT = OUT -* EP_DBUF_IN = IN -* : wCount: Counter value -* Output : None. -* Return : None. -*******************************************************************************/ -#define _SetEPDblBuf0Count(bEpNum, bDir, wCount) { \ - if(bDir == EP_DBUF_OUT)\ - /* OUT endpoint */ \ - {_SetEPRxDblBuf0Count(bEpNum,wCount);} \ - else if(bDir == EP_DBUF_IN)\ - /* IN endpoint */ \ - *_pEPTxCount(bEpNum) = (u32)wCount; \ - } /* SetEPDblBuf0Count*/ - -#define _SetEPDblBuf1Count(bEpNum, bDir, wCount) { \ - if(bDir == EP_DBUF_OUT)\ - /* OUT endpoint */ \ - {_SetEPRxCount(bEpNum,wCount);}\ - else if(bDir == EP_DBUF_IN)\ - /* IN endpoint */\ - *_pEPRxCount(bEpNum) = (u32)wCount; \ - } /* SetEPDblBuf1Count */ - -#define _SetEPDblBuffCount(bEpNum, bDir, wCount) {\ - _SetEPDblBuf0Count(bEpNum, bDir, wCount); \ - _SetEPDblBuf1Count(bEpNum, bDir, wCount); \ - } /* _SetEPDblBuffCount */ - -/******************************************************************************* -* Macro Name : GetEPDblBuf0Count / GetEPDblBuf1Count. -* Description : Gets buffer 0/1 rx/tx counter for double buffering. -* Input : bEpNum: endpoint number. -* Output : None. -* Return : None. -*******************************************************************************/ -#define _GetEPDblBuf0Count(bEpNum) (_GetEPTxCount(bEpNum)) -#define _GetEPDblBuf1Count(bEpNum) (_GetEPRxCount(bEpNum)) - - -/* External variables --------------------------------------------------------*/ -extern volatile u16 wIstr; /* ISTR register last read value */ - -/* Exported functions ------------------------------------------------------- */ -void SetCNTR(u16 /*wRegValue*/); -void SetISTR(u16 /*wRegValue*/); -void SetDADDR(u16 /*wRegValue*/); -void SetBTABLE(u16 /*wRegValue*/); -u16 GetCNTR(void); -u16 GetISTR(void); -u16 GetFNR(void); -u16 GetDADDR(void); -u16 GetBTABLE(void); -void SetENDPOINT(u8 /*bEpNum*/, u16 /*wRegValue*/); -u16 GetENDPOINT(u8 /*bEpNum*/); -void SetEPType(u8 /*bEpNum*/, u16 /*wType*/); -u16 GetEPType(u8 /*bEpNum*/); -void SetEPTxStatus(u8 /*bEpNum*/, u16 /*wState*/); -void SetEPRxStatus(u8 /*bEpNum*/, u16 /*wState*/); -void SetDouBleBuffEPStall(u8 /*bEpNum*/, u8 bDir); -u16 GetEPTxStatus(u8 /*bEpNum*/); -u16 GetEPRxStatus(u8 /*bEpNum*/); -void SetEPTxValid(u8 /*bEpNum*/); -void SetEPRxValid(u8 /*bEpNum*/); -u16 GetTxStallStatus(u8 /*bEpNum*/); -u16 GetRxStallStatus(u8 /*bEpNum*/); -void SetEP_KIND(u8 /*bEpNum*/); -void ClearEP_KIND(u8 /*bEpNum*/); -void Set_Status_Out(u8 /*bEpNum*/); -void Clear_Status_Out(u8 /*bEpNum*/); -void SetEPDoubleBuff(u8 /*bEpNum*/); -void ClearEPDoubleBuff(u8 /*bEpNum*/); -void ClearEP_CTR_RX(u8 /*bEpNum*/); -void ClearEP_CTR_TX(u8 /*bEpNum*/); -void ToggleDTOG_RX(u8 /*bEpNum*/); -void ToggleDTOG_TX(u8 /*bEpNum*/); -void ClearDTOG_RX(u8 /*bEpNum*/); -void ClearDTOG_TX(u8 /*bEpNum*/); -void SetEPAddress(u8 /*bEpNum*/, u8 /*bAddr*/); -u8 GetEPAddress(u8 /*bEpNum*/); -void SetEPTxAddr(u8 /*bEpNum*/, u16 /*wAddr*/); -void SetEPRxAddr(u8 /*bEpNum*/, u16 /*wAddr*/); -u16 GetEPTxAddr(u8 /*bEpNum*/); -u16 GetEPRxAddr(u8 /*bEpNum*/); -void SetEPCountRxReg(u32 * /*pdwReg*/, u16 /*wCount*/); -void SetEPTxCount(u8 /*bEpNum*/, u16 /*wCount*/); -void SetEPRxCount(u8 /*bEpNum*/, u16 /*wCount*/); -u16 GetEPTxCount(u8 /*bEpNum*/); -u16 GetEPRxCount(u8 /*bEpNum*/); -void SetEPDblBuf0Addr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/); -void SetEPDblBuf1Addr(u8 /*bEpNum*/, u16 /*wBuf1Addr*/); -void SetEPDblBuffAddr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/, u16 /*wBuf1Addr*/); -u16 GetEPDblBuf0Addr(u8 /*bEpNum*/); -u16 GetEPDblBuf1Addr(u8 /*bEpNum*/); -void SetEPDblBuffCount(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); -void SetEPDblBuf0Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); -void SetEPDblBuf1Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); -u16 GetEPDblBuf0Count(u8 /*bEpNum*/); -u16 GetEPDblBuf1Count(u8 /*bEpNum*/); -EP_DBUF_DIR GetEPDblBufDir(u8 /*bEpNum*/); -void FreeUserBuffer(u8 bEpNum/*bEpNum*/, u8 bDir); -u16 ToWord(u8, u8); -u16 ByteSwap(u16); - -#endif /* __USB_REGS_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_regs.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Interface prototype functions to USB cell registers +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_REGS_H +#define __USB_REGS_H + +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef enum _EP_DBUF_DIR +{ + /* double buffered endpoint direction */ + EP_DBUF_ERR, + EP_DBUF_OUT, + EP_DBUF_IN +}EP_DBUF_DIR; + +/* endpoint buffer number */ +enum EP_BUF_NUM +{ + EP_NOBUF, + EP_BUF0, + EP_BUF1 +}; + +/* Exported constants --------------------------------------------------------*/ +#define RegBase (0x40005C00L) /* USB_IP Peripheral Registers base address */ +#define PMAAddr (0x40006000L) /* USB_IP Packet Memory Area base address */ + +/******************************************************************************/ +/* General registers */ +/******************************************************************************/ + +/* Control register */ +#define CNTR ((volatile unsigned *)(RegBase + 0x40)) +/* Interrupt status register */ +#define ISTR ((volatile unsigned *)(RegBase + 0x44)) +/* Frame number register */ +#define FNR ((volatile unsigned *)(RegBase + 0x48)) +/* Device address register */ +#define DADDR ((volatile unsigned *)(RegBase + 0x4C)) +/* Buffer Table address register */ +#define BTABLE ((volatile unsigned *)(RegBase + 0x50)) +/******************************************************************************/ +/* Endpoint registers */ +/******************************************************************************/ +#define EP0REG ((volatile unsigned *)(RegBase)) /* endpoint 0 register address */ + +/* endpoints enumeration */ +#define ENDP0 ((u8)0) +#define ENDP1 ((u8)1) +#define ENDP2 ((u8)2) +#define ENDP3 ((u8)3) +#define ENDP4 ((u8)4) +#define ENDP5 ((u8)5) +#define ENDP6 ((u8)6) +#define ENDP7 ((u8)7) +/******************************************************************************/ +/* ISTR interrupt events */ +/******************************************************************************/ +#define ISTR_CTR (0x8000) /* Correct TRansfer (clear-only bit) */ +#define ISTR_DOVR (0x4000) /* DMA OVeR/underrun (clear-only bit) */ +#define ISTR_ERR (0x2000) /* ERRor (clear-only bit) */ +#define ISTR_WKUP (0x1000) /* WaKe UP (clear-only bit) */ +#define ISTR_SUSP (0x0800) /* SUSPend (clear-only bit) */ +#define ISTR_RESET (0x0400) /* RESET (clear-only bit) */ +#define ISTR_SOF (0x0200) /* Start Of Frame (clear-only bit) */ +#define ISTR_ESOF (0x0100) /* Expected Start Of Frame (clear-only bit) */ + + +#define ISTR_DIR (0x0010) /* DIRection of transaction (read-only bit) */ +#define ISTR_EP_ID (0x000F) /* EndPoint IDentifier (read-only bit) */ + +#define CLR_CTR (~ISTR_CTR) /* clear Correct TRansfer bit */ +#define CLR_DOVR (~ISTR_DOVR) /* clear DMA OVeR/underrun bit*/ +#define CLR_ERR (~ISTR_ERR) /* clear ERRor bit */ +#define CLR_WKUP (~ISTR_WKUP) /* clear WaKe UP bit */ +#define CLR_SUSP (~ISTR_SUSP) /* clear SUSPend bit */ +#define CLR_RESET (~ISTR_RESET) /* clear RESET bit */ +#define CLR_SOF (~ISTR_SOF) /* clear Start Of Frame bit */ +#define CLR_ESOF (~ISTR_ESOF) /* clear Expected Start Of Frame bit */ + +/******************************************************************************/ +/* CNTR control register bits definitions */ +/******************************************************************************/ +#define CNTR_CTRM (0x8000) /* Correct TRansfer Mask */ +#define CNTR_DOVRM (0x4000) /* DMA OVeR/underrun Mask */ +#define CNTR_ERRM (0x2000) /* ERRor Mask */ +#define CNTR_WKUPM (0x1000) /* WaKe UP Mask */ +#define CNTR_SUSPM (0x0800) /* SUSPend Mask */ +#define CNTR_RESETM (0x0400) /* RESET Mask */ +#define CNTR_SOFM (0x0200) /* Start Of Frame Mask */ +#define CNTR_ESOFM (0x0100) /* Expected Start Of Frame Mask */ + + +#define CNTR_RESUME (0x0010) /* RESUME request */ +#define CNTR_FSUSP (0x0008) /* Force SUSPend */ +#define CNTR_LPMODE (0x0004) /* Low-power MODE */ +#define CNTR_PDWN (0x0002) /* Power DoWN */ +#define CNTR_FRES (0x0001) /* Force USB RESet */ + +/******************************************************************************/ +/* FNR Frame Number Register bit definitions */ +/******************************************************************************/ +#define FNR_RXDP (0x8000) /* status of D+ data line */ +#define FNR_RXDM (0x4000) /* status of D- data line */ +#define FNR_LCK (0x2000) /* LoCKed */ +#define FNR_LSOF (0x1800) /* Lost SOF */ +#define FNR_FN (0x07FF) /* Frame Number */ +/******************************************************************************/ +/* DADDR Device ADDRess bit definitions */ +/******************************************************************************/ +#define DADDR_EF (0x80) +#define DADDR_ADD (0x7F) +/******************************************************************************/ +/* Endpoint register */ +/******************************************************************************/ +/* bit positions */ +#define EP_CTR_RX (0x8000) /* EndPoint Correct TRansfer RX */ +#define EP_DTOG_RX (0x4000) /* EndPoint Data TOGGLE RX */ +#define EPRX_STAT (0x3000) /* EndPoint RX STATus bit field */ +#define EP_SETUP (0x0800) /* EndPoint SETUP */ +#define EP_T_FIELD (0x0600) /* EndPoint TYPE */ +#define EP_KIND (0x0100) /* EndPoint KIND */ +#define EP_CTR_TX (0x0080) /* EndPoint Correct TRansfer TX */ +#define EP_DTOG_TX (0x0040) /* EndPoint Data TOGGLE TX */ +#define EPTX_STAT (0x0030) /* EndPoint TX STATus bit field */ +#define EPADDR_FIELD (0x000F) /* EndPoint ADDRess FIELD */ + +/* EndPoint REGister MASK (no toggle fields) */ +#define EPREG_MASK (EP_CTR_RX|EP_SETUP|EP_T_FIELD|EP_KIND|EP_CTR_TX|EPADDR_FIELD) + +/* EP_TYPE[1:0] EndPoint TYPE */ +#define EP_TYPE_MASK (0x0600) /* EndPoint TYPE Mask */ +#define EP_BULK (0x0000) /* EndPoint BULK */ +#define EP_CONTROL (0x0200) /* EndPoint CONTROL */ +#define EP_ISOCHRONOUS (0x0400) /* EndPoint ISOCHRONOUS */ +#define EP_INTERRUPT (0x0600) /* EndPoint INTERRUPT */ +#define EP_T_MASK (~EP_T_FIELD & EPREG_MASK) + + +/* EP_KIND EndPoint KIND */ +#define EPKIND_MASK (~EP_KIND & EPREG_MASK) + +/* STAT_TX[1:0] STATus for TX transfer */ +#define EP_TX_DIS (0x0000) /* EndPoint TX DISabled */ +#define EP_TX_STALL (0x0010) /* EndPoint TX STALLed */ +#define EP_TX_NAK (0x0020) /* EndPoint TX NAKed */ +#define EP_TX_VALID (0x0030) /* EndPoint TX VALID */ +#define EPTX_DTOG1 (0x0010) /* EndPoint TX Data TOGgle bit1 */ +#define EPTX_DTOG2 (0x0020) /* EndPoint TX Data TOGgle bit2 */ +#define EPTX_DTOGMASK (EPTX_STAT|EPREG_MASK) + +/* STAT_RX[1:0] STATus for RX transfer */ +#define EP_RX_DIS (0x0000) /* EndPoint RX DISabled */ +#define EP_RX_STALL (0x1000) /* EndPoint RX STALLed */ +#define EP_RX_NAK (0x2000) /* EndPoint RX NAKed */ +#define EP_RX_VALID (0x3000) /* EndPoint RX VALID */ +#define EPRX_DTOG1 (0x1000) /* EndPoint RX Data TOGgle bit1 */ +#define EPRX_DTOG2 (0x2000) /* EndPoint RX Data TOGgle bit1 */ +#define EPRX_DTOGMASK (EPRX_STAT|EPREG_MASK) +/* Exported macro ------------------------------------------------------------*/ +/* SetCNTR */ +#define _SetCNTR(wRegValue) (*CNTR = (u16)wRegValue) + +/* SetISTR */ +#define _SetISTR(wRegValue) (*ISTR = (u16)wRegValue) + +/* SetDADDR */ +#define _SetDADDR(wRegValue) (*DADDR = (u16)wRegValue) + +/* SetBTABLE */ +#define _SetBTABLE(wRegValue)(*BTABLE = (u16)(wRegValue & 0xFFF8)) + +/* GetCNTR */ +#define _GetCNTR() ((u16) *CNTR) + +/* GetISTR */ +#define _GetISTR() ((u16) *ISTR) + +/* GetFNR */ +#define _GetFNR() ((u16) *FNR) + +/* GetDADDR */ +#define _GetDADDR() ((u16) *DADDR) + +/* GetBTABLE */ +#define _GetBTABLE() ((u16) *BTABLE) + +/* SetENDPOINT */ +#define _SetENDPOINT(bEpNum,wRegValue) (*(EP0REG + bEpNum)= \ + (u16)wRegValue) + +/* GetENDPOINT */ +#define _GetENDPOINT(bEpNum) ((u16)(*(EP0REG + bEpNum))) + +/******************************************************************************* +* Macro Name : SetEPType +* Description : sets the type in the endpoint register(bits EP_TYPE[1:0]) +* Input : bEpNum: Endpoint Number. +* wType +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPType(bEpNum,wType) (_SetENDPOINT(bEpNum,\ + ((_GetENDPOINT(bEpNum) & EP_T_MASK) | wType))) + +/******************************************************************************* +* Macro Name : GetEPType +* Description : gets the type in the endpoint register(bits EP_TYPE[1:0]) +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : Endpoint Type +*******************************************************************************/ +#define _GetEPType(bEpNum) (_GetENDPOINT(bEpNum) & EP_T_FIELD) + +/******************************************************************************* +* Macro Name : SetEPTxStatus +* Description : sets the status for tx transfer (bits STAT_TX[1:0]). +* Input : bEpNum: Endpoint Number. +* wState: new state +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPTxStatus(bEpNum,wState) {\ + register u16 _wRegVal; \ + _wRegVal = _GetENDPOINT(bEpNum) & EPTX_DTOGMASK;\ + /* toggle first bit ? */ \ + if((EPTX_DTOG1 & wState)!= 0) \ + _wRegVal ^= EPTX_DTOG1; \ + /* toggle second bit ? */ \ + if((EPTX_DTOG2 & wState)!= 0) \ + _wRegVal ^= EPTX_DTOG2; \ + _SetENDPOINT(bEpNum, _wRegVal); \ + } /* _SetEPTxStatus */ + +/******************************************************************************* +* Macro Name : SetEPRxStatus +* Description : sets the status for rx transfer (bits STAT_TX[1:0]) +* Input : bEpNum: Endpoint Number. +* wState: new state. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPRxStatus(bEpNum,wState) {\ + register u16 _wRegVal; \ + \ + _wRegVal = _GetENDPOINT(bEpNum) & EPRX_DTOGMASK;\ + /* toggle first bit ? */ \ + if((EPRX_DTOG1 & wState)!= 0) \ + _wRegVal ^= EPRX_DTOG1; \ + /* toggle second bit ? */ \ + if((EPRX_DTOG2 & wState)!= 0) \ + _wRegVal ^= EPRX_DTOG2; \ + _SetENDPOINT(bEpNum, _wRegVal); \ + } /* _SetEPRxStatus */ +/******************************************************************************* +* Macro Name : GetEPTxStatus / GetEPRxStatus +* Description : gets the status for tx/rx transfer (bits STAT_TX[1:0] +* /STAT_RX[1:0]) +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : status . +*******************************************************************************/ +#define _GetEPTxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPTX_STAT) + +#define _GetEPRxStatus(bEpNum) ((u16)_GetENDPOINT(bEpNum) & EPRX_STAT) + +/******************************************************************************* +* Macro Name : SetEPTxValid / SetEPRxValid +* Description : sets directly the VALID tx/rx-status into the enpoint register +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPTxValid(bEpNum) (_SetEPTxStatus(bEpNum, EP_TX_VALID)) + +#define _SetEPRxValid(bEpNum) (_SetEPRxStatus(bEpNum, EP_RX_VALID)) + +/******************************************************************************* +* Macro Name : GetTxStallStatus / GetRxStallStatus. +* Description : checks stall condition in an endpoint. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : TRUE = endpoint in stall condition. +*******************************************************************************/ +#define _GetTxStallStatus(bEpNum) (_GetEPTxStatus(bEpNum) \ + == EP_TX_STALL) +#define _GetRxStallStatus(bEpNum) (_GetEPRxStatus(bEpNum) \ + == EP_RX_STALL) + +/******************************************************************************* +* Macro Name : SetEP_KIND / ClearEP_KIND. +* Description : set & clear EP_KIND bit. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, \ + (_GetENDPOINT(bEpNum) | EP_KIND) & EPREG_MASK)) +#define _ClearEP_KIND(bEpNum) (_SetENDPOINT(bEpNum, \ + (_GetENDPOINT(bEpNum) & EPKIND_MASK))) + +/******************************************************************************* +* Macro Name : Set_Status_Out / Clear_Status_Out. +* Description : Sets/clears directly STATUS_OUT bit in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _Set_Status_Out(bEpNum) _SetEP_KIND(bEpNum) +#define _Clear_Status_Out(bEpNum) _ClearEP_KIND(bEpNum) + +/******************************************************************************* +* Macro Name : SetEPDoubleBuff / ClearEPDoubleBuff. +* Description : Sets/clears directly EP_KIND bit in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPDoubleBuff(bEpNum) _SetEP_KIND(bEpNum) +#define _ClearEPDoubleBuff(bEpNum) _ClearEP_KIND(bEpNum) + +/******************************************************************************* +* Macro Name : ClearEP_CTR_RX / ClearEP_CTR_TX. +* Description : Clears bit CTR_RX / CTR_TX in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _ClearEP_CTR_RX(bEpNum) (_SetENDPOINT(bEpNum,\ + _GetENDPOINT(bEpNum) & 0x7FFF & EPREG_MASK)) +#define _ClearEP_CTR_TX(bEpNum) (_SetENDPOINT(bEpNum,\ + _GetENDPOINT(bEpNum) & 0xFF7F & EPREG_MASK)) + +/******************************************************************************* +* Macro Name : ToggleDTOG_RX / ToggleDTOG_TX . +* Description : Toggles DTOG_RX / DTOG_TX bit in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _ToggleDTOG_RX(bEpNum) (_SetENDPOINT(bEpNum, \ + EP_DTOG_RX | _GetENDPOINT(bEpNum) & EPREG_MASK)) +#define _ToggleDTOG_TX(bEpNum) (_SetENDPOINT(bEpNum, \ + EP_DTOG_TX | _GetENDPOINT(bEpNum) & EPREG_MASK)) + +/******************************************************************************* +* Macro Name : ClearDTOG_RX / ClearDTOG_TX. +* Description : Clears DTOG_RX / DTOG_TX bit in the endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _ClearDTOG_RX(bEpNum) if((_GetENDPOINT(bEpNum) & EP_DTOG_RX) != 0)\ + _ToggleDTOG_RX(bEpNum) +#define _ClearDTOG_TX(bEpNum) if((_GetENDPOINT(bEpNum) & EP_DTOG_TX) != 0)\ + _ToggleDTOG_TX(bEpNum) +/******************************************************************************* +* Macro Name : SetEPAddress. +* Description : Sets address in an endpoint register. +* Input : bEpNum: Endpoint Number. +* bAddr: Address. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPAddress(bEpNum,bAddr) _SetENDPOINT(bEpNum,\ + _GetENDPOINT(bEpNum) & EPREG_MASK | bAddr) + +/******************************************************************************* +* Macro Name : GetEPAddress. +* Description : Gets address in an endpoint register. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _GetEPAddress(bEpNum) ((u8)(_GetENDPOINT(bEpNum) & EPADDR_FIELD)) + +#define _pEPTxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8 )*2 + PMAAddr)) +#define _pEPTxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+2)*2 + PMAAddr)) +#define _pEPRxAddr(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+4)*2 + PMAAddr)) +#define _pEPRxCount(bEpNum) ((u32 *)((_GetBTABLE()+bEpNum*8+6)*2 + PMAAddr)) + +/******************************************************************************* +* Macro Name : SetEPTxAddr / SetEPRxAddr. +* Description : sets address of the tx/rx buffer. +* Input : bEpNum: Endpoint Number. +* wAddr: address to be set (must be word aligned). +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPTxAddr(bEpNum,wAddr) (*_pEPTxAddr(bEpNum) = ((wAddr >> 1) << 1)) +#define _SetEPRxAddr(bEpNum,wAddr) (*_pEPRxAddr(bEpNum) = ((wAddr >> 1) << 1)) + +/******************************************************************************* +* Macro Name : GetEPTxAddr / GetEPRxAddr. +* Description : Gets address of the tx/rx buffer. +* Input : bEpNum: Endpoint Number. +* Output : None. +* Return : address of the buffer. +*******************************************************************************/ +#define _GetEPTxAddr(bEpNum) ((u16)*_pEPTxAddr(bEpNum)) +#define _GetEPRxAddr(bEpNum) ((u16)*_pEPRxAddr(bEpNum)) + +/******************************************************************************* +* Macro Name : SetEPCountRxReg. +* Description : Sets counter of rx buffer with no. of blocks. +* Input : pdwReg: pointer to counter. +* wCount: Counter. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _BlocksOf32(dwReg,wCount,wNBlocks) {\ + wNBlocks = wCount >> 5;\ + if((wCount & 0x1f) == 0)\ + wNBlocks--;\ + *pdwReg = (u32)((wNBlocks << 10) | 0x8000);\ + }/* _BlocksOf32 */ + +#define _BlocksOf2(dwReg,wCount,wNBlocks) {\ + wNBlocks = wCount >> 1;\ + if((wCount & 0x1) != 0)\ + wNBlocks++;\ + *pdwReg = (u32)(wNBlocks << 10);\ + }/* _BlocksOf2 */ + +#define _SetEPCountRxReg(dwReg,wCount) {\ + u16 wNBlocks;\ + if(wCount > 62){_BlocksOf32(dwReg,wCount,wNBlocks);}\ + else {_BlocksOf2(dwReg,wCount,wNBlocks);}\ + }/* _SetEPCountRxReg */ + + + +#define _SetEPRxDblBuf0Count(bEpNum,wCount) {\ + u32 *pdwReg = _pEPTxCount(bEpNum); \ + _SetEPCountRxReg(pdwReg, wCount);\ + } +/******************************************************************************* +* Macro Name : SetEPTxCount / SetEPRxCount. +* Description : sets counter for the tx/rx buffer. +* Input : bEpNum: endpoint number. +* wCount: Counter value. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPTxCount(bEpNum,wCount) (*_pEPTxCount(bEpNum) = wCount) +#define _SetEPRxCount(bEpNum,wCount) {\ + u32 *pdwReg = _pEPRxCount(bEpNum); \ + _SetEPCountRxReg(pdwReg, wCount);\ + } +/******************************************************************************* +* Macro Name : GetEPTxCount / GetEPRxCount. +* Description : gets counter of the tx buffer. +* Input : bEpNum: endpoint number. +* Output : None. +* Return : Counter value. +*******************************************************************************/ +#define _GetEPTxCount(bEpNum)((u16)(*_pEPTxCount(bEpNum)) & 0x3ff) +#define _GetEPRxCount(bEpNum)((u16)(*_pEPRxCount(bEpNum)) & 0x3ff) + +/******************************************************************************* +* Macro Name : SetEPDblBuf0Addr / SetEPDblBuf1Addr. +* Description : Sets buffer 0/1 address in a double buffer endpoint. +* Input : bEpNum: endpoint number. +* : wBuf0Addr: buffer 0 address. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPDblBuf0Addr(bEpNum,wBuf0Addr) {_SetEPTxAddr(bEpNum, wBuf0Addr);} +#define _SetEPDblBuf1Addr(bEpNum,wBuf1Addr) {_SetEPRxAddr(bEpNum, wBuf1Addr);} + +/******************************************************************************* +* Macro Name : SetEPDblBuffAddr. +* Description : Sets addresses in a double buffer endpoint. +* Input : bEpNum: endpoint number. +* : wBuf0Addr: buffer 0 address. +* : wBuf1Addr = buffer 1 address. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPDblBuffAddr(bEpNum,wBuf0Addr,wBuf1Addr) { \ + _SetEPDblBuf0Addr(bEpNum, wBuf0Addr);\ + _SetEPDblBuf1Addr(bEpNum, wBuf1Addr);\ + } /* _SetEPDblBuffAddr */ + +/******************************************************************************* +* Macro Name : GetEPDblBuf0Addr / GetEPDblBuf1Addr. +* Description : Gets buffer 0/1 address of a double buffer endpoint. +* Input : bEpNum: endpoint number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _GetEPDblBuf0Addr(bEpNum) (_GetEPTxAddr(bEpNum)) +#define _GetEPDblBuf1Addr(bEpNum) (_GetEPRxAddr(bEpNum)) + +/******************************************************************************* +* Macro Name : SetEPDblBuffCount / SetEPDblBuf0Count / SetEPDblBuf1Count. +* Description : Gets buffer 0/1 address of a double buffer endpoint. +* Input : bEpNum: endpoint number. +* : bDir: endpoint dir EP_DBUF_OUT = OUT +* EP_DBUF_IN = IN +* : wCount: Counter value +* Output : None. +* Return : None. +*******************************************************************************/ +#define _SetEPDblBuf0Count(bEpNum, bDir, wCount) { \ + if(bDir == EP_DBUF_OUT)\ + /* OUT endpoint */ \ + {_SetEPRxDblBuf0Count(bEpNum,wCount);} \ + else if(bDir == EP_DBUF_IN)\ + /* IN endpoint */ \ + *_pEPTxCount(bEpNum) = (u32)wCount; \ + } /* SetEPDblBuf0Count*/ + +#define _SetEPDblBuf1Count(bEpNum, bDir, wCount) { \ + if(bDir == EP_DBUF_OUT)\ + /* OUT endpoint */ \ + {_SetEPRxCount(bEpNum,wCount);}\ + else if(bDir == EP_DBUF_IN)\ + /* IN endpoint */\ + *_pEPRxCount(bEpNum) = (u32)wCount; \ + } /* SetEPDblBuf1Count */ + +#define _SetEPDblBuffCount(bEpNum, bDir, wCount) {\ + _SetEPDblBuf0Count(bEpNum, bDir, wCount); \ + _SetEPDblBuf1Count(bEpNum, bDir, wCount); \ + } /* _SetEPDblBuffCount */ + +/******************************************************************************* +* Macro Name : GetEPDblBuf0Count / GetEPDblBuf1Count. +* Description : Gets buffer 0/1 rx/tx counter for double buffering. +* Input : bEpNum: endpoint number. +* Output : None. +* Return : None. +*******************************************************************************/ +#define _GetEPDblBuf0Count(bEpNum) (_GetEPTxCount(bEpNum)) +#define _GetEPDblBuf1Count(bEpNum) (_GetEPRxCount(bEpNum)) + + +/* External variables --------------------------------------------------------*/ +extern volatile u16 wIstr; /* ISTR register last read value */ + +/* Exported functions ------------------------------------------------------- */ +void SetCNTR(u16 /*wRegValue*/); +void SetISTR(u16 /*wRegValue*/); +void SetDADDR(u16 /*wRegValue*/); +void SetBTABLE(u16 /*wRegValue*/); +u16 GetCNTR(void); +u16 GetISTR(void); +u16 GetFNR(void); +u16 GetDADDR(void); +u16 GetBTABLE(void); +void SetENDPOINT(u8 /*bEpNum*/, u16 /*wRegValue*/); +u16 GetENDPOINT(u8 /*bEpNum*/); +void SetEPType(u8 /*bEpNum*/, u16 /*wType*/); +u16 GetEPType(u8 /*bEpNum*/); +void SetEPTxStatus(u8 /*bEpNum*/, u16 /*wState*/); +void SetEPRxStatus(u8 /*bEpNum*/, u16 /*wState*/); +void SetDouBleBuffEPStall(u8 /*bEpNum*/, u8 bDir); +u16 GetEPTxStatus(u8 /*bEpNum*/); +u16 GetEPRxStatus(u8 /*bEpNum*/); +void SetEPTxValid(u8 /*bEpNum*/); +void SetEPRxValid(u8 /*bEpNum*/); +u16 GetTxStallStatus(u8 /*bEpNum*/); +u16 GetRxStallStatus(u8 /*bEpNum*/); +void SetEP_KIND(u8 /*bEpNum*/); +void ClearEP_KIND(u8 /*bEpNum*/); +void Set_Status_Out(u8 /*bEpNum*/); +void Clear_Status_Out(u8 /*bEpNum*/); +void SetEPDoubleBuff(u8 /*bEpNum*/); +void ClearEPDoubleBuff(u8 /*bEpNum*/); +void ClearEP_CTR_RX(u8 /*bEpNum*/); +void ClearEP_CTR_TX(u8 /*bEpNum*/); +void ToggleDTOG_RX(u8 /*bEpNum*/); +void ToggleDTOG_TX(u8 /*bEpNum*/); +void ClearDTOG_RX(u8 /*bEpNum*/); +void ClearDTOG_TX(u8 /*bEpNum*/); +void SetEPAddress(u8 /*bEpNum*/, u8 /*bAddr*/); +u8 GetEPAddress(u8 /*bEpNum*/); +void SetEPTxAddr(u8 /*bEpNum*/, u16 /*wAddr*/); +void SetEPRxAddr(u8 /*bEpNum*/, u16 /*wAddr*/); +u16 GetEPTxAddr(u8 /*bEpNum*/); +u16 GetEPRxAddr(u8 /*bEpNum*/); +void SetEPCountRxReg(u32 * /*pdwReg*/, u16 /*wCount*/); +void SetEPTxCount(u8 /*bEpNum*/, u16 /*wCount*/); +void SetEPRxCount(u8 /*bEpNum*/, u16 /*wCount*/); +u16 GetEPTxCount(u8 /*bEpNum*/); +u16 GetEPRxCount(u8 /*bEpNum*/); +void SetEPDblBuf0Addr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/); +void SetEPDblBuf1Addr(u8 /*bEpNum*/, u16 /*wBuf1Addr*/); +void SetEPDblBuffAddr(u8 /*bEpNum*/, u16 /*wBuf0Addr*/, u16 /*wBuf1Addr*/); +u16 GetEPDblBuf0Addr(u8 /*bEpNum*/); +u16 GetEPDblBuf1Addr(u8 /*bEpNum*/); +void SetEPDblBuffCount(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); +void SetEPDblBuf0Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); +void SetEPDblBuf1Count(u8 /*bEpNum*/, u8 /*bDir*/, u16 /*wCount*/); +u16 GetEPDblBuf0Count(u8 /*bEpNum*/); +u16 GetEPDblBuf1Count(u8 /*bEpNum*/); +EP_DBUF_DIR GetEPDblBufDir(u8 /*bEpNum*/); +void FreeUserBuffer(u8 bEpNum/*bEpNum*/, u8 bDir); +u16 ToWord(u8, u8); +u16 ByteSwap(u16); + +#endif /* __USB_REGS_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ diff --git a/usb_lib/usb_type.h b/src/usb_lib/usb_type.h similarity index 97% rename from usb_lib/usb_type.h rename to src/usb_lib/usb_type.h index 9e428b9..a40be61 100644 --- a/usb_lib/usb_type.h +++ b/src/usb_lib/usb_type.h @@ -1,72 +1,72 @@ -/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** -* File Name : usb_type.h -* Author : MCD Application Team -* Version : V2.2.1 -* Date : 09/22/2008 -* Description : Type definitions used by the USB Library -******************************************************************************** -* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS -* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. -* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, -* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE -* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING -* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. -*******************************************************************************/ - -/* Define to prevent recursive inclusion -------------------------------------*/ -#ifndef __USB_TYPE_H -#define __USB_TYPE_H - -/* Exported types ------------------------------------------------------------*/ -/* Exported constants --------------------------------------------------------*/ -#ifndef NULL -#define NULL ((void *)0) -#endif - -#ifndef __STM32F10x_TYPE_H - -typedef signed long s32; -typedef signed short s16; -typedef signed char s8; - -typedef volatile signed long vs32; -typedef volatile signed short vs16; -typedef volatile signed char vs8; - -typedef unsigned long u32; -typedef unsigned short u16; -typedef unsigned char u8; - -typedef unsigned long const uc32; /* Read Only */ -typedef unsigned short const uc16; /* Read Only */ -typedef unsigned char const uc8; /* Read Only */ - -typedef volatile unsigned long vu32; -typedef volatile unsigned short vu16; -typedef volatile unsigned char vu8; - -typedef volatile unsigned long const vuc32; /* Read Only */ -typedef volatile unsigned short const vuc16; /* Read Only */ -typedef volatile unsigned char const vuc8; /* Read Only */ - - -typedef enum -{ - FALSE = 0, TRUE = !FALSE -} -bool; - -typedef enum { RESET = 0, SET = !RESET } FlagStatus, ITStatus; - -typedef enum { DISABLE = 0, ENABLE = !DISABLE} FunctionalState; - -typedef enum { ERROR = 0, SUCCESS = !ERROR} ErrorStatus; -#endif - -/* Exported macro ------------------------------------------------------------*/ -/* Exported functions ------------------------------------------------------- */ -/* External variables --------------------------------------------------------*/ - -#endif /* __USB_TYPE_H */ - -/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ +/******************** (C) COPYRIGHT 2008 STMicroelectronics ******************** +* File Name : usb_type.h +* Author : MCD Application Team +* Version : V2.2.1 +* Date : 09/22/2008 +* Description : Type definitions used by the USB Library +******************************************************************************** +* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS +* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. +* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, +* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE +* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING +* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. +*******************************************************************************/ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __USB_TYPE_H +#define __USB_TYPE_H + +/* Exported types ------------------------------------------------------------*/ +/* Exported constants --------------------------------------------------------*/ +#ifndef NULL +#define NULL ((void *)0) +#endif + +#ifndef __STM32F10x_TYPE_H + +typedef signed long s32; +typedef signed short s16; +typedef signed char s8; + +typedef volatile signed long vs32; +typedef volatile signed short vs16; +typedef volatile signed char vs8; + +typedef unsigned long u32; +typedef unsigned short u16; +typedef unsigned char u8; + +typedef unsigned long const uc32; /* Read Only */ +typedef unsigned short const uc16; /* Read Only */ +typedef unsigned char const uc8; /* Read Only */ + +typedef volatile unsigned long vu32; +typedef volatile unsigned short vu16; +typedef volatile unsigned char vu8; + +typedef volatile unsigned long const vuc32; /* Read Only */ +typedef volatile unsigned short const vuc16; /* Read Only */ +typedef volatile unsigned char const vuc8; /* Read Only */ + + +typedef enum +{ + FALSE = 0, TRUE = !FALSE +} +bool; + +typedef enum { RESET = 0, SET = !RESET } FlagStatus, ITStatus; + +typedef enum { DISABLE = 0, ENABLE = !DISABLE} FunctionalState; + +typedef enum { ERROR = 0, SUCCESS = !ERROR} ErrorStatus; +#endif + +/* Exported macro ------------------------------------------------------------*/ +/* Exported functions ------------------------------------------------------- */ +/* External variables --------------------------------------------------------*/ + +#endif /* __USB_TYPE_H */ + +/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/ From 87dc369d7c7e0a04564145811ce67be3423773e0 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:39:27 +0400 Subject: [PATCH 02/66] Move usb_descriptors to per-board config. TE board works, maple is broken. Also fixes lots of compiler warnings. Signed-off-by: Andrew 'Necromant' Andrianov --- Makefile | 2 -- boards/te-stm32f103ret6kit/Makefile | 3 ++- boards/te-stm32f103ret6kit/board.c | 16 ++++++++-------- boards/te-stm32f103ret6kit/config.h | 6 +++--- .../te-stm32f103ret6kit}/usb_descriptor.c | 4 ++-- src/common/Makefile | 4 ++-- src/common/dfu.c | 2 +- src/common/hardware.h | 15 +++++++++++---- src/common/main.c | 7 +++++-- src/common/usb_descriptor.h | 3 +-- 10 files changed, 35 insertions(+), 27 deletions(-) rename {src/common => boards/te-stm32f103ret6kit}/usb_descriptor.c (98%) diff --git a/Makefile b/Makefile index 06acefe..bede7fc 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,3 @@ -unexport CROSS_COMPILE?=arm-ncrmnt-eabi- SRCDIR=. OBJDIR=./build @@ -13,7 +12,6 @@ TARGET=$(OBJDIR)/ucboot-$(BOARD) CFLAGS += -O$(OPT) CFLAGS += -ffunction-sections -fdata-sections CFLAGS += -Wall -Wimplicit -CFLAGS += -Wcast-align CFLAGS += -Wpointer-arith -Wswitch CFLAGS += -Wredundant-decls -Wreturn-type -Wshadow -Wunused CFLAGS += -Wa,-adhlns=$(OBJDIR)/$(subst $(suffix $<),.lst,$<) diff --git a/boards/te-stm32f103ret6kit/Makefile b/boards/te-stm32f103ret6kit/Makefile index 88de6c6..81a35d9 100644 --- a/boards/te-stm32f103ret6kit/Makefile +++ b/boards/te-stm32f103ret6kit/Makefile @@ -1 +1,2 @@ -objects-y+=board.o +CFLAGS+=-DSTM32F10X_MD +objects-y+=board.o usb_descriptor.o diff --git a/boards/te-stm32f103ret6kit/board.c b/boards/te-stm32f103ret6kit/board.c index e6d61ee..b95ae78 100644 --- a/boards/te-stm32f103ret6kit/board.c +++ b/boards/te-stm32f103ret6kit/board.c @@ -65,21 +65,21 @@ static void inline setupUSB (void) { /* Setup the USB DISC Pin */ rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; + rwmVal |= 0x8; SET_REG(RCC_APB2ENR,rwmVal); // todo, macroize usb_disc pin /* Setup GPIOC Pin 12 as OD out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFF0FFFF; - rwmVal |= 0x00050000; - setPin (GPIOC,12); - SET_REG(GPIO_CRH(GPIOC),rwmVal); + rwmVal = GET_REG(GPIO_CRL(GPIOB)); + rwmVal &= 0xFF0FFFFF; + rwmVal |= 0x00500000; + setPin (GPIOB,5); + SET_REG(GPIO_CRL(GPIOB),rwmVal); pRCC->APB1ENR |= 0x00800000; /* initialize the usb application */ - resetPin (GPIOC,12); /* present ourselves to the host */ + resetPin (GPIOB,5); /* present ourselves to the host */ } @@ -87,7 +87,7 @@ static void inline setupUSB (void) { /* executed before actual jump to user code */ void boardTeardown() { - setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin + resetPin(GPIOB,5); // disconnect usb from host. todo, macroize pin } /* This is a common routine to setup the board */ diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index 33f2837..26d369b 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -36,7 +36,7 @@ //WTF WAS THAT DOING HERE?? //#include "common.h" -#define INHIBIT_LEDS +#define NUM_ALT_SETTINGS 2 #define LED_BANK GPIOA #define LED 5 @@ -57,10 +57,10 @@ #define PROD_ID0 0x03 #define PROD_ID1 0x00 + /* while this is '1' we're looping in the bootloader */ -#define bootloaderCondition (delay_count++ < BOOTLOADER_WAIT) \ - || no_user_jump +#define bootloaderCondition (1) #define bootloaderExitCondition 0 diff --git a/src/common/usb_descriptor.c b/boards/te-stm32f103ret6kit/usb_descriptor.c similarity index 98% rename from src/common/usb_descriptor.c rename to boards/te-stm32f103ret6kit/usb_descriptor.c index f83b03f..af32975 100644 --- a/src/common/usb_descriptor.c +++ b/boards/te-stm32f103ret6kit/usb_descriptor.c @@ -192,9 +192,9 @@ u8 u8_usbStringAlt1[0x3A] = '0',0 }; -u8 u8_usbStringInterface = NULL; +u8 u8_usbStringInterface = 0; -ONE_DESCRIPTOR usbStringDescriptor[STR_DESC_LEN] = +ONE_DESCRIPTOR usbStringDescriptor[] = { { (u8*)u8_usbStringLangId, 0x04 }, { (u8*)u8_usbStringVendor, 0x12 }, diff --git a/src/common/Makefile b/src/common/Makefile index 7dfec83..158bb4f 100644 --- a/src/common/Makefile +++ b/src/common/Makefile @@ -3,5 +3,5 @@ objects-common += dfu.o \ hardware.o \ main.o \ usb.o \ - usb_callbacks.o \ - usb_descriptor.o + usb_callbacks.o + diff --git a/src/common/dfu.c b/src/common/dfu.c index 70a3b1a..f585b46 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -334,7 +334,7 @@ void dfuCopyBufferToExec() { flashErasePage((u32)(userSpace)); for (i=0;i Date: Fri, 1 Jun 2012 17:41:11 +0400 Subject: [PATCH 03/66] Minor fixes and cleanups Signed-off-by: Andrew 'Necromant' Andrianov --- boards/te-stm32f103ret6kit/board.c | 7 +++--- boards/te-stm32f103ret6kit/config.h | 9 ++++++-- boards/te-stm32f103ret6kit/usb_descriptor.c | 2 +- src/common/hardware.c | 2 ++ src/common/hardware.h | 7 ++++++ src/common/main.c | 17 ++++++++------ src/common/usb.c | 25 +-------------------- src/common/usb_descriptor.h | 2 +- 8 files changed, 32 insertions(+), 39 deletions(-) diff --git a/boards/te-stm32f103ret6kit/board.c b/boards/te-stm32f103ret6kit/board.c index b95ae78..cb9e159 100644 --- a/boards/te-stm32f103ret6kit/board.c +++ b/boards/te-stm32f103ret6kit/board.c @@ -73,14 +73,13 @@ static void inline setupUSB (void) { rwmVal = GET_REG(GPIO_CRL(GPIOB)); rwmVal &= 0xFF0FFFFF; rwmVal |= 0x00500000; - setPin (GPIOB,5); + resetPin (GPIOB,5); SET_REG(GPIO_CRL(GPIOB),rwmVal); pRCC->APB1ENR |= 0x00800000; - /* initialize the usb application */ - resetPin (GPIOB,5); /* present ourselves to the host */ - + setPin (GPIOB,5); /* present ourselves to the host */ + usbAppInit(); } diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index 26d369b..cdc0165 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -37,6 +37,8 @@ //#include "common.h" #define NUM_ALT_SETTINGS 2 +//No LED strobing whatsoever - saves space +//#define CONFIG_INHIBIT_STROBE #define LED_BANK GPIOA #define LED 5 @@ -50,7 +52,7 @@ #define BOOTLOADER_WAIT 6 #define USER_CODE_RAM ((u32)0x20000C00) -#define USER_CODE_FLASH ((u32)0x08005000) +#define USER_CODE_FLASH ((u32)0x08002800) #define VEND_ID0 0xAF #define VEND_ID1 0x1E @@ -58,10 +60,13 @@ #define PROD_ID1 0x00 -/* while this is '1' we're looping in the bootloader */ +//Any extra code for main, e.g. for timeouts +//#define CONFIG_EXTRA_MAIN_CODE +/* while this is '1' we're looping in the bootloader */ #define bootloaderCondition (1) +/* define to 0 to never exit, undefine to save space */ #define bootloaderExitCondition 0 #endif diff --git a/boards/te-stm32f103ret6kit/usb_descriptor.c b/boards/te-stm32f103ret6kit/usb_descriptor.c index af32975..a55bbf5 100644 --- a/boards/te-stm32f103ret6kit/usb_descriptor.c +++ b/boards/te-stm32f103ret6kit/usb_descriptor.c @@ -194,7 +194,7 @@ u8 u8_usbStringAlt1[0x3A] = u8 u8_usbStringInterface = 0; -ONE_DESCRIPTOR usbStringDescriptor[] = +ONE_DESCRIPTOR usbStringDescriptor[6] = { { (u8*)u8_usbStringLangId, 0x04 }, { (u8*)u8_usbStringVendor, 0x12 }, diff --git a/src/common/hardware.c b/src/common/hardware.c index 0dcb479..7cb5095 100644 --- a/src/common/hardware.c +++ b/src/common/hardware.c @@ -51,6 +51,7 @@ bool readPin(u32 bank, u8 pin) { } } +#ifndef CONFIG_INHIBIT_STROBE void strobePin(u32 bank, u8 pin, u8 count, u32 rate) { resetPin(bank,pin); @@ -66,6 +67,7 @@ void strobePin(u32 bank, u8 pin, u8 count, u32 rate) { resetPin(bank,pin); } } +#endif void systemReset(void) { SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00000001); diff --git a/src/common/hardware.h b/src/common/hardware.h index df588b3..35cbda6 100644 --- a/src/common/hardware.h +++ b/src/common/hardware.h @@ -178,7 +178,14 @@ void boardTeardown(); void setPin (u32 bank, u8 pin); void resetPin (u32 bank, u8 pin); bool readPin (u32 bank, u8 pin); + + +#ifndef CONFIG_INHIBIT_STROBE void strobePin (u32 bank, u8 pin, u8 count, u32 rate); +#else +#define strobePin(bank, pin, count, rate) ;; +#endif + void systemHardReset(void); void systemReset (void); diff --git a/src/common/main.c b/src/common/main.c index 63dcbcb..041656e 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -37,17 +37,20 @@ int main() { systemReset(); // peripherals but not PC boardInit(); - usbAppInit(); setupFLASH(); - + //usbAppInit(); strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); - - /* wait for host to upload program or halt bootloader */ - #ifdef LDR_DELAYED - bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); - int delay_count = 0; + #ifdef CONFIG_EXTRA_MAIN_CODE + CONFIG_EXTRA_MAIN_CODE #endif +/* This code now moved to CONFIG_EXTRA_MAIN_CODE */ +// bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); +// #ifdef CONFIG_BOOTLOADER_WAIT +// int delay_count = 0; +// #endif + +/* wait for host to upload program or halt bootloader */ while (bootloaderCondition) { strobePin(LED_BANK,LED,1,BLINK_SLOW); if (dfuUploadStarted()) { diff --git a/src/common/usb.c b/src/common/usb.c index 883b436..9b33479 100644 --- a/src/common/usb.c +++ b/src/common/usb.c @@ -33,30 +33,6 @@ #include "usb.h" #include "dfu.h" -void setupUSB (void) { - u32 rwmVal; /* read-write-modify place holder var */ - - /* Setup the USB DISC Pin */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; - SET_REG(RCC_APB2ENR,rwmVal); - - // todo, macroize usb_disc pin - /* Setup GPIOC Pin 12 as OD out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFF0FFFF; - rwmVal |= 0x00050000; - setPin (GPIOC,12); - SET_REG(GPIO_CRH(GPIOC),rwmVal); - - pRCC->APB1ENR |= 0x00800000; - - /* initialize the usb application */ - resetPin (GPIOC,12); /* present ourselves to the host */ - usbAppInit(); - -} - vu32 bDeviceState = UNCONNECTED; /* tracks sequential behavior of the ISTR */ @@ -408,6 +384,7 @@ void usbSetDeviceFeature(void) { /* nothing process */ } + void usbSetDeviceAddress(void) { bDeviceState = ADDRESSED; } diff --git a/src/common/usb_descriptor.h b/src/common/usb_descriptor.h index 4b9a1d3..d0b00b7 100644 --- a/src/common/usb_descriptor.h +++ b/src/common/usb_descriptor.h @@ -33,7 +33,7 @@ extern ONE_DESCRIPTOR usbDeviceDescriptorDFU; extern ONE_DESCRIPTOR usbConfigDescriptorDFU; -extern ONE_DESCRIPTOR usbStringDescriptor[]; +extern ONE_DESCRIPTOR usbStringDescriptor[6]; extern ONE_DESCRIPTOR usbFunctionalDescriptor; #endif From 49225a380ff42a02af52ad91763e82b6679c124a Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:41:54 +0400 Subject: [PATCH 04/66] Add config option to strip usb suspend/resume code Signed-off-by: Andrew 'Necromant' Andrianov --- boards/te-stm32f103ret6kit/board.c | 6 +++-- boards/te-stm32f103ret6kit/config.h | 14 +++++++---- src/common/dfu.c | 12 +++++----- src/common/usb.c | 36 ++++++++++++++++------------- src/common/usb.h | 8 +++++++ src/startup/c_only_md.ld | 2 +- 6 files changed, 48 insertions(+), 30 deletions(-) diff --git a/boards/te-stm32f103ret6kit/board.c b/boards/te-stm32f103ret6kit/board.c index cb9e159..e8f5d2c 100644 --- a/boards/te-stm32f103ret6kit/board.c +++ b/boards/te-stm32f103ret6kit/board.c @@ -73,10 +73,12 @@ static void inline setupUSB (void) { rwmVal = GET_REG(GPIO_CRL(GPIOB)); rwmVal &= 0xFF0FFFFF; rwmVal |= 0x00500000; - resetPin (GPIOB,5); SET_REG(GPIO_CRL(GPIOB),rwmVal); - pRCC->APB1ENR |= 0x00800000; + + resetPin (GPIOB,5); + rwmVal = 10000000; + while (rwmVal--);; /* initialize the usb application */ setPin (GPIOB,5); /* present ourselves to the host */ usbAppInit(); diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index cdc0165..a9775c6 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -40,6 +40,10 @@ //No LED strobing whatsoever - saves space //#define CONFIG_INHIBIT_STROBE +//Replace USB suspend/resume code with dummies. +//Saves a few bytes more +#define CONFIG_DISABLE_USB_SUSPEND + #define LED_BANK GPIOA #define LED 5 #define BLINK_FAST 0x50000 @@ -52,12 +56,12 @@ #define BOOTLOADER_WAIT 6 #define USER_CODE_RAM ((u32)0x20000C00) -#define USER_CODE_FLASH ((u32)0x08002800) +#define USER_CODE_FLASH ((u32)0x08005000) -#define VEND_ID0 0xAF -#define VEND_ID1 0x1E -#define PROD_ID0 0x03 -#define PROD_ID1 0x00 +#define VEND_ID0 0xAD +#define VEND_ID1 0xDE +#define PROD_ID0 0xDE +#define PROD_ID1 0xC0 //Any extra code for main, e.g. for timeouts diff --git a/src/common/dfu.c b/src/common/dfu.c index f585b46..971f0bd 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -35,14 +35,14 @@ #include "usb.h" /* DFU globals */ -static u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ -static DFUStatus dfuAppStatus; /* includes state */ -static bool userFlash = FALSE; +u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ + DFUStatus dfuAppStatus; /* includes state */ + bool userFlash = FALSE; bool dfuBusy = FALSE; -static u8 recvBuffer[wTransferSize]; -static u32 userFirmwareLen = 0; -static u16 thisBlockLen = 0; + u8 recvBuffer[wTransferSize]; + u32 userFirmwareLen = 0; + u16 thisBlockLen = 0; PLOT code_copy_lock; diff --git a/src/common/usb.c b/src/common/usb.c index 9b33479..12544ff 100644 --- a/src/common/usb.c +++ b/src/common/usb.c @@ -64,14 +64,14 @@ DEVICE_PROP Device_Property = USER_STANDARD_REQUESTS User_Standard_Requests = { - usbGetConfiguration, + nothingProc, usbSetConfiguration, - usbGetInterface, - usbSetInterface, - usbGetStatus, - usbClearFeature, - usbSetEndpointFeature, - usbSetDeviceFeature, + nothingProc, + nothingProc, + nothingProc, + nothingProc, + nothingProc, + nothingProc, usbSetDeviceAddress }; @@ -114,6 +114,7 @@ void usbAppInit(void) { USB_Init(); } +#ifndef CONFIG_DISABLE_USB_SUSPEND void usbSuspend(void) { u16 wCNTR; wCNTR = _GetCNTR(); @@ -187,6 +188,7 @@ void usbResume(RESUME_STATE eResumeSetVal) { break; } } +#endif RESULT usbPowerOn(void) { u16 wRegVal; @@ -349,10 +351,10 @@ u8* usbGetFunctionalDescriptor(u16 len) { * application level *******************************************/ +/* void usbGetConfiguration(void) { - /* nothing process */ } - +*/ void usbSetConfiguration(void) { if (pInformation->Current_Configuration != 0) { @@ -360,30 +362,32 @@ void usbSetConfiguration(void) { } } +/* + Lets save some space, shall we? void usbGetInterface(void) { - /* nothing process */ + } void usbSetInterface(void) { - /* nothing process */ + } void usbGetStatus(void) { - /* nothing process */ + } void usbClearFeature(void) { - /* nothing process */ + } void usbSetEndpointFeature(void) { - /* nothing process */ + } void usbSetDeviceFeature(void) { - /* nothing process */ -} +} +*/ void usbSetDeviceAddress(void) { bDeviceState = ADDRESSED; diff --git a/src/common/usb.h b/src/common/usb.h index e1563d6..1f1ad1d 100644 --- a/src/common/usb.h +++ b/src/common/usb.h @@ -45,6 +45,13 @@ #define F_SUSPEND_ENABLED 0 /* defines which interrupts are handled */ +#ifdef CONFIG_DISABLE_USB_SUSPEND +#define ISR_MSK (CNTR_CTRM | \ + CNTR_ERRM | \ + CNTR_SOFM | \ + CNTR_RESETM \ + ) +#else #define ISR_MSK (CNTR_CTRM | \ CNTR_WKUPM | \ CNTR_SUSPM | \ @@ -53,6 +60,7 @@ CNTR_ESOFM | \ CNTR_RESETM \ ) +#endif typedef enum _RESUME_STATE { diff --git a/src/startup/c_only_md.ld b/src/startup/c_only_md.ld index 09d57ce..1436995 100644 --- a/src/startup/c_only_md.ld +++ b/src/startup/c_only_md.ld @@ -25,7 +25,7 @@ _Minimum_Stack_Size = 0x100 ; MEMORY { RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 20K - FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 128K + FLASH (rx) : ORIGIN = 0x08000000, LENGTH = 512K } /* higher address of the user mode stack */ From 1534b6ebd01f09317640008a325c56f113cc16c8 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:42:44 +0400 Subject: [PATCH 05/66] Set default options to -Os and no debug. Result is ~5.9KiB. Signed-off-by: Andrew 'Necromant' Andrianov --- Makefile | 4 +++- boards/te-stm32f103ret6kit/config.h | 3 ++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/Makefile b/Makefile index bede7fc..1672d53 100644 --- a/Makefile +++ b/Makefile @@ -9,6 +9,8 @@ THUMB_IW = -mthumb-interwork TARGET=$(OBJDIR)/ucboot-$(BOARD) +OPT=s +DEBUG= CFLAGS += -O$(OPT) CFLAGS += -ffunction-sections -fdata-sections CFLAGS += -Wall -Wimplicit @@ -107,4 +109,4 @@ help: @echo "Have fun" .PRECIOUS: $(objects) $(TARGET).bin $(TARGET).elf -.PHONY: warmup \ No newline at end of file +.PHONY: warmup diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index a9775c6..cc326b9 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -38,10 +38,11 @@ #define NUM_ALT_SETTINGS 2 //No LED strobing whatsoever - saves space -//#define CONFIG_INHIBIT_STROBE +#define CONFIG_INHIBIT_STROBE //Replace USB suspend/resume code with dummies. //Saves a few bytes more + #define CONFIG_DISABLE_USB_SUSPEND #define LED_BANK GPIOA From 27053af79097258215c06b1afea99b5c8f6a4aef Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:43:55 +0400 Subject: [PATCH 06/66] Fix maple target to compile (Not tested!) Add coloring support to Makefile Signed-off-by: Andrew 'Necromant' Andrianov --- Makefile | 3 +- boards/maple/Makefile | 2 +- boards/maple/board.c | 108 ++++++++++++++++++++++++++---------------- boards/maple/config.h | 7 ++- 4 files changed, 76 insertions(+), 44 deletions(-) diff --git a/Makefile b/Makefile index 1672d53..e631387 100644 --- a/Makefile +++ b/Makefile @@ -48,6 +48,7 @@ ifneq ($(V),y) SILENT_LD = @echo ' $(tb_pur)[LD]$(col_rst) ' $(@F); SILENT_OBJCOPY = @echo ' $(tb_pur)[OBJCOPY]$(col_rst) ' $(@F); SILENT_OBJDUMP = @echo ' $(tb_pur)[OBJDUMP]$(col_rst) ' $(OBJDUMP); + SILENT_INFO = @echo ' $(tb_blu)[INFO]$(col_rst) Build for board $(tb_pur)$(BOARD)$(col_rst) complete'; #Shut up this crap MAKEFLAGS+=--no-print-directory endif @@ -97,7 +98,7 @@ warmup: @touch $(OBJDIR)/config.h build: warmup $(OBJDIR)/ucboot-$(BOARD).bin - @echo "Build for board $(BOARD) complete" + $(SILENT_INFO) clean: @rm -Rfv $(OBJDIR) diff --git a/boards/maple/Makefile b/boards/maple/Makefile index 88de6c6..0638a62 100644 --- a/boards/maple/Makefile +++ b/boards/maple/Makefile @@ -1 +1 @@ -objects-y+=board.o +objects-y+=board.o usb_descriptor.o diff --git a/boards/maple/board.c b/boards/maple/board.c index 4e77ad2..4415ca4 100644 --- a/boards/maple/board.c +++ b/boards/maple/board.c @@ -5,64 +5,87 @@ static void setupLED (void) { - // todo, swap out hardcoded pin/bank with macro - u32 rwmVal; /* read-write-modify place holder var */ - - /* Setup APB2 (GPIOA) */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000004; - SET_REG(RCC_APB2ENR,rwmVal); - - /* Setup GPIOA Pin 5 as PP Out */ - SET_REG(GPIO_CRL(GPIOA), 0x00100000); - - rwmVal = GET_REG(GPIO_CRL(GPIOA)); - rwmVal &= 0xFF0FFFFF; - rwmVal |= 0x00100000; - SET_REG(GPIO_CRL(GPIOA),rwmVal); - - setPin(GPIOA,5); + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup APB2 (GPIOA) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000004; + SET_REG(RCC_APB2ENR,rwmVal); + + /* Setup GPIOA Pin 5 as PP Out */ + SET_REG(GPIO_CRL(GPIOA), 0x00100000); + + rwmVal = GET_REG(GPIO_CRL(GPIOA)); + rwmVal &= 0xFF0FFFFF; + rwmVal |= 0x00100000; + SET_REG(GPIO_CRL(GPIOA),rwmVal); + setPin(GPIOA,5); } static void setupCLK (void) { - /* enable HSE */ - SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); - while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ + /* enable HSE */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); + while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ - /* enable flash prefetch buffer */ - SET_REG(FLASH_ACR, 0x00000012); + /* enable flash prefetch buffer */ + SET_REG(FLASH_ACR, 0x00000012); - /* Configure PLL */ - SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ - SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ - while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ + /* Configure PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ + while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ - /* Set SYSCLK as PLL */ - SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); - while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ + /* Set SYSCLK as PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); + while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ } static void setupBUTTON (void) { - // todo, swap out hardcoded pin/bank with macro - u32 rwmVal; /* read-write-modify place holder var */ + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ - /* Setup APB2 (GPIOC) */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; - SET_REG(RCC_APB2ENR,rwmVal); + /* Setup APB2 (GPIOC) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000010; + SET_REG(RCC_APB2ENR,rwmVal); - /* Setup GPIOC Pin 9 as PP Out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFFFFF0F; - rwmVal |= 0x00000040; - SET_REG(GPIO_CRH(GPIOC),rwmVal); + /* Setup GPIOC Pin 9 as PP Out */ + rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal &= 0xFFFFFF0F; + rwmVal |= 0x00000040; + SET_REG(GPIO_CRH(GPIOC),rwmVal); } +static void inline setupUSB (void) { + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup the USB DISC Pin */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x4; + SET_REG(RCC_APB2ENR,rwmVal); + + // todo, macroize usb_disc pin + /* Setup GPIOC Pin 12 as OD out */ + rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal &= 0xFFF0FFFF; + rwmVal |= 0x00050000; + SET_REG(GPIO_CRH(GPIOC),rwmVal); + pRCC->APB1ENR |= 0x00800000; + + resetPin (GPIOC,12); + rwmVal = 10000000; + while (rwmVal--);; + /* initialize the usb application */ + setPin (GPIOC,12); /* present ourselves to the host */ +} + + /* executed before actual jump to user code */ void boardTeardown() { - setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin + setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin } /* This is a common routine to setup the board */ @@ -71,4 +94,7 @@ void boardInit() setupCLK(); setupLED(); setupBUTTON(); + setupUSB(); } + + diff --git a/boards/maple/config.h b/boards/maple/config.h index 84fb47a..ac2c6a0 100644 --- a/boards/maple/config.h +++ b/boards/maple/config.h @@ -36,7 +36,9 @@ //WTF WAS THAT DOING HERE?? //#include "common.h" -#define INHIBIT_LEDS +#define NUM_ALT_SETTINGS 2 + +//#define INHIBIT_LEDS #define LED_BANK GPIOA #define LED 5 @@ -59,6 +61,9 @@ #define PROD_ID1 0x00 /* while this is '1' we're looping in the bootloader */ +#define CONFIG_EXTRA_MAIN_CODE \ + bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); \ + int delay_count =0; #define bootloaderCondition (delay_count++ < BOOTLOADER_WAIT) \ || no_user_jump From 182a459359efc0c4e4abdd05dd297f1c15966766 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:44:50 +0400 Subject: [PATCH 07/66] Shut up another warning Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/common/main.c b/src/common/main.c index 041656e..429a6d8 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -69,4 +69,5 @@ int main() { strobePin(LED_BANK,LED,5,BLINK_FAST); systemHardReset(); } + return 0; } From 4d3687b94faae8767c7a8017c780d1bf0d901eac Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:45:38 +0400 Subject: [PATCH 08/66] Add README and TODO Signed-off-by: Andrew 'Necromant' Andrianov --- README | 11 +++++++++++ TODO | 10 ++++++++++ 2 files changed, 21 insertions(+) create mode 100644 README create mode 100644 TODO diff --git a/README b/README new file mode 100644 index 0000000..be36dec --- /dev/null +++ b/README @@ -0,0 +1,11 @@ +This is a bootloader derived from leaflabs' maple dfu bootloader. +It has a better support for multiple boards, and some other +goodies. +To compile run 'make' +To specify toolchain - use CROSS_COMPILE, e.g. +'CROSS_COMPILE=arm-none-eabi- make' +Results are piled to build/ directory. +To see what's actually happening - run with V=y, e.g. +CROSS_COMPILE=arm-none-eabi- make V=y +This will dump a lot of info for you. +Have fun. diff --git a/TODO b/TODO new file mode 100644 index 0000000..7ff8277 --- /dev/null +++ b/TODO @@ -0,0 +1,10 @@ +This is a personal TODO list. + +Clean up board init, free up some space +Shut up a shitload of warnings from usb_lib +Merge here all the CREDITS, Licensing info and other shit +Wrap altsettings in macros, add API for adding custom endpoints +Merge Xilinx-SSCU for Mcortex +Add support for boards: mcortex mctx-dm sISP ucGUN, etc. +Talk with leaflabs, merge&check their boards +fix coding style!!! From 1b779d963c5fd0831c4b9e8dc058c07a32a11fe7 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:45:59 +0400 Subject: [PATCH 09/66] Add missing usb_descriptor for maple target Signed-off-by: Andrew 'Necromant' Andrianov --- boards/maple/usb_descriptor.c | 205 ++++++++++++++++++++++++++++++++++ 1 file changed, 205 insertions(+) create mode 100644 boards/maple/usb_descriptor.c diff --git a/boards/maple/usb_descriptor.c b/boards/maple/usb_descriptor.c new file mode 100644 index 0000000..a55bbf5 --- /dev/null +++ b/boards/maple/usb_descriptor.c @@ -0,0 +1,205 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ****************************************************************************/ + + +/** + * @file usb_descriptor.c + * + * @brief aka application descriptor; big static struct and callbacks for sending + * the descriptor. + * + */ + + +#include "usb_descriptor.h" + +u8 u8_usbDeviceDescriptorDFU[18] = + { + 0x12, /* bLength */ + 0x01, /* bDescriptorType */ + 0x00, /* bcdUSB, version 1.00 */ + 0x01, + 0x00, /* bDeviceClass : See interface */ + 0x00, /* bDeviceSubClass : See interface*/ + 0x00, /* bDeviceProtocol : See interface */ + bMaxPacketSize, /* bMaxPacketSize0 0x40 = 64 */ + VEND_ID0, /* idVendor (0110) */ + VEND_ID1, + + PROD_ID0, /* idProduct (0x1001 or 1002) */ + PROD_ID1, + + 0x01, /* bcdDevice*/ + 0x02, + 0x01, /* iManufacturer : index of string Manufacturer */ + 0x02, /* iProduct : index of string descriptor of product*/ + 0x03, /* iSerialNumber : index of string serial number*/ + 0x01 /*bNumConfigurations */ + }; + +ONE_DESCRIPTOR usbDeviceDescriptorDFU = + { + u8_usbDeviceDescriptorDFU, + 0x12 + }; + +u8 u8_usbFunctionalDescriptor[9] = + { + /******************** DFU Functional Descriptor********************/ + 0x09, /*blength = 7 Bytes*/ + 0x21, /* DFU Functional Descriptor*/ + 0x01, /*bmAttribute, can only download for now */ + 0xFF, /*DetachTimeOut= 255 ms*/ + 0x00, + (wTransferSize & 0x00FF), + (wTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ + 0x10, /* bcdDFUVersion = 1.1 */ + 0x01 + }; + +ONE_DESCRIPTOR usbFunctionalDescriptor = + { + u8_usbFunctionalDescriptor, + 0x09 + }; + + +u8 u8_usbConfigDescriptorDFU[36] = + { + 0x09, /* bLength: Configuation Descriptor size */ + 0x02, /* bDescriptorType: Configuration */ + 0x24, /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /* bNumInterfaces: 1 interface */ + 0x01, /* bConfigurationValue: */ + 0x00, /* iConfiguration: */ + 0x80, /* bmAttributes: */ + 0x32, /* MaxPower 100 mA */ + /* 09 */ + + /************ Descriptor of DFU interface 0 Alternate setting 0 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + + 0x04, /* iInterface: */ + + /************ Descriptor of DFU interface 0 Alternate setting 1 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x01, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + + 0x05, /* iInterface: */ + + /******************** DFU Functional Descriptor********************/ + 0x09, /*blength = 7 Bytes*/ + 0x21, /* DFU Functional Descriptor*/ + 0x01, /*bmAttribute, can only download for now */ + 0xFF, /*DetachTimeOut= 255 ms*/ + 0x00, + (wTransferSize & 0x00FF), + (wTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ + 0x10, /* bcdDFUVersion = 1.1 */ + 0x01 + /***********************************************************/ + /*36*/ + }; + +ONE_DESCRIPTOR usbConfigDescriptorDFU = + { + u8_usbConfigDescriptorDFU, + 0x24 + }; + +u8 u8_usbStringLangId[0x04] = + { + 0x04, + 0x03, + 0x09, + 0x04 /* LangID = 0x0409: U.S. English */ + }; + +u8 u8_usbStringVendor[0x12] = + { + 0x12, + 0x03, + 'L',0,'e',0,'a',0,'f',0,'L',0,'a',0,'b',0,'s',0 + }; + +u8 u8_usbStringProduct[0x14] = + { + 0x14, + 0x03, + 'M',0,'a',0,'p',0,'l',0,'e',0,' ',0,'0',0,'0',0,'3',0 + }; + +u8 u8_usbStringSerial[0x10] = + { + 0x10, + 0x03, + 'L',0,'L',0,'M',0,' ',0,'0',0,'0',0,'3',0 + }; + +u8 u8_usbStringAlt0[0x36] = + { + 0x36, + 0x03, + 'D',0,'F',0,'U',0,' ',0,'P',0,'r',0,'o',0,'g',0,'r',0, + 'a',0,'m',0,' ',0,'R',0,'A',0,'M',0,' ',0,'0',0,'x',0, + '2',0,'0',0,'0',0,'0',0,'0',0,'C',0,'0',0,'0',0 + }; + +u8 u8_usbStringAlt1[0x3A] = + { + 0x3A, + 0x03, + 'D',0,'F',0,'U',0,' ',0,'P',0,'r',0,'o',0,'g',0,'r',0, + 'a',0,'m',0,' ',0,'F',0,'L',0,'A',0,'S',0,'H',0,' ',0, + '0',0,'x',0,'0',0,'8',0,'0',0,'0',0,'5',0,'0',0,'0',0, + '0',0 + }; + +u8 u8_usbStringInterface = 0; + +ONE_DESCRIPTOR usbStringDescriptor[6] = + { + { (u8*)u8_usbStringLangId, 0x04 }, + { (u8*)u8_usbStringVendor, 0x12 }, + { (u8*)u8_usbStringProduct, 0x20 }, + { (u8*)u8_usbStringSerial, 0x10 }, + { (u8*)u8_usbStringAlt0, 0x36 }, + { (u8*)u8_usbStringAlt1, 0x3A } + }; From eb5ee79367c9cf08e5293a2623a27496de2d57a7 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:46:34 +0400 Subject: [PATCH 10/66] Add some stuff to TODO Signed-off-by: Andrew 'Necromant' Andrianov --- TODO | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO b/TODO index 7ff8277..0dc2815 100644 --- a/TODO +++ b/TODO @@ -1,6 +1,7 @@ This is a personal TODO list. Clean up board init, free up some space +Do we need different LD script and c_startup for different uCs?? Shut up a shitload of warnings from usb_lib Merge here all the CREDITS, Licensing info and other shit Wrap altsettings in macros, add API for adding custom endpoints From 78a1caa514eaee9d71d17d736aaf83395ffd4772 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:47:09 +0400 Subject: [PATCH 11/66] Shut up the last warnings from usb_regs.h Signed-off-by: Andrew 'Necromant' Andrianov --- src/usb_lib/usb_regs.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/usb_lib/usb_regs.h b/src/usb_lib/usb_regs.h index 6927f6e..78f4b47 100644 --- a/src/usb_lib/usb_regs.h +++ b/src/usb_lib/usb_regs.h @@ -353,9 +353,9 @@ enum EP_BUF_NUM * Return : None. *******************************************************************************/ #define _ToggleDTOG_RX(bEpNum) (_SetENDPOINT(bEpNum, \ - EP_DTOG_RX | _GetENDPOINT(bEpNum) & EPREG_MASK)) + (EP_DTOG_RX | _GetENDPOINT(bEpNum)) & EPREG_MASK)) #define _ToggleDTOG_TX(bEpNum) (_SetENDPOINT(bEpNum, \ - EP_DTOG_TX | _GetENDPOINT(bEpNum) & EPREG_MASK)) + (EP_DTOG_TX | _GetENDPOINT(bEpNum)) & EPREG_MASK)) /******************************************************************************* * Macro Name : ClearDTOG_RX / ClearDTOG_TX. @@ -377,7 +377,7 @@ enum EP_BUF_NUM * Return : None. *******************************************************************************/ #define _SetEPAddress(bEpNum,bAddr) _SetENDPOINT(bEpNum,\ - _GetENDPOINT(bEpNum) & EPREG_MASK | bAddr) + (_GetENDPOINT(bEpNum) & EPREG_MASK) | bAddr) /******************************************************************************* * Macro Name : GetEPAddress. From 3309716d42c39aebb2397e9427f6e6eb1007a53a Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:47:33 +0400 Subject: [PATCH 12/66] Change color scheme a little bit Signed-off-by: Andrew 'Necromant' Andrianov --- Makefile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Makefile b/Makefile index e631387..1922a86 100644 --- a/Makefile +++ b/Makefile @@ -41,6 +41,7 @@ tb_ylw=$(shell echo -e '\e[1;33m') tb_blu=$(shell echo -e '\e[1;34m') tb_pur=$(shell echo -e '\e[1;35m') col_rst=$(shell echo -e '\e[0m') +tb_grn=$(shell echo -e '\e[1;32m') ifneq ($(V),y) SILENT_CC = @echo ' $(tb_ylw)[CC]$(col_rst) ' $(@F); @@ -48,7 +49,7 @@ ifneq ($(V),y) SILENT_LD = @echo ' $(tb_pur)[LD]$(col_rst) ' $(@F); SILENT_OBJCOPY = @echo ' $(tb_pur)[OBJCOPY]$(col_rst) ' $(@F); SILENT_OBJDUMP = @echo ' $(tb_pur)[OBJDUMP]$(col_rst) ' $(OBJDUMP); - SILENT_INFO = @echo ' $(tb_blu)[INFO]$(col_rst) Build for board $(tb_pur)$(BOARD)$(col_rst) complete'; + SILENT_INFO = @echo ' $(tb_grn)[INFO]$(col_rst) Build for board $(tb_grn)$(BOARD)$(col_rst) complete'; #Shut up this crap MAKEFLAGS+=--no-print-directory endif From 91fc6183ba4f248ef408e926c493506265eefd12 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:47:58 +0400 Subject: [PATCH 13/66] Add README.porting Signed-off-by: Andrew 'Necromant' Andrianov --- README.porting | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 README.porting diff --git a/README.porting b/README.porting new file mode 100644 index 0000000..5ec158a --- /dev/null +++ b/README.porting @@ -0,0 +1,41 @@ +This file will give you a short overview of the porting process +Lacking of fast&easy porting of maple bootloader was the reason +of the major code rework I've done here. + +Have a good look at the boards/ directory. These are all the boards, +that are supported. Pick the one that looks like the closest to yours +and copy it under a different name. + +Each port consists of 4 files. + +config.h +usb_descriptors.c +board.c +Makefile + +config.h has several very important params, that are worth setting. +Most are self-explanatory, some need manual tuning. There are several +options that impact bootloader size. + +CONFIG_INHIBIT_STROBE + define this to completely remove any strobe code and save some bytes + +CONFIG_DISABLE_USB_SUSPEND + Disables USB suspend/resume support and saves quite a few bytes + +You can also adjust the USER_CODE_FLASH to some lower adress, depending +on the resulting loader size. Default maple settings waste a lot of +space. + +board.c has 2 functions that need to be implemented. +boardInit() - This should init the board, any leds, buttons, clocks and +present the usb device to the host + +boardTeardown() - should disconnect the device from host for good. + +usb_descriptors.c gives you fine-tuning of the device descriptors. + +Makefile - any additional CFLAGS, objects and other stuff goes here. +Normally you wouldn't wat to edit it. + +That's it, enjoy From 89bc6c8c7640c161e4b2a5c9b15999939b1581f8 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 17:49:47 +0400 Subject: [PATCH 14/66] Added myself to CREDITS Signed-off-by: Andrew 'Necromant' Andrianov --- CREDITS | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CREDITS b/CREDITS index 4318fff..a459e78 100644 --- a/CREDITS +++ b/CREDITS @@ -10,3 +10,7 @@ The fields are: name (N), email (E), web-address (W), description (D). N: Tormod Volden E: debian.tormod@gmail.com D: Fixes for DFU compliance + +N: Andrew 'Necromant' Andrianov +E: andrew@ncrmnt.org +D: Portability, cleanups, buildsystem tweeks From 1cfc68b16ccd9ba388187e5d59b65ea8dac4b653 Mon Sep 17 00:00:00 2001 From: nekromant Date: Fri, 1 Jun 2012 17:55:51 +0400 Subject: [PATCH 15/66] Update TODO Signed-off-by: Andrew 'Necromant' Andrianov --- TODO | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/TODO b/TODO index 0dc2815..710c405 100644 --- a/TODO +++ b/TODO @@ -1,11 +1,11 @@ This is a personal TODO list. -Clean up board init, free up some space +Clean up board init, free up some space *WiP Do we need different LD script and c_startup for different uCs?? -Shut up a shitload of warnings from usb_lib -Merge here all the CREDITS, Licensing info and other shit +Shut up a shitload of warnings from usb_lib *DONE +Merge here all the CREDITS, Licensing info and other shit *DONE (Somewhat) Wrap altsettings in macros, add API for adding custom endpoints Merge Xilinx-SSCU for Mcortex Add support for boards: mcortex mctx-dm sISP ucGUN, etc. -Talk with leaflabs, merge&check their boards +Talk with leaflabs, merge&check their boards *WiP fix coding style!!! From ece1037dc07425bd9209f6d56dc2d0a66f63ad5c Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 20:58:18 +0400 Subject: [PATCH 16/66] Make a bunch of vars volatile to work properly with -Os The dfu state machine locks and globals get called in ISR, hence must be volatile. If not, we are screwed. Signed-off-by: Andrew 'Necromant' Andrianov --- Makefile | 4 ++-- src/common/dfu.c | 17 +++++++++-------- src/common/dfu.h | 2 +- 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/Makefile b/Makefile index 1922a86..59f2397 100644 --- a/Makefile +++ b/Makefile @@ -21,9 +21,9 @@ CFLAGS += -I$(OBJDIR)/ GENDEPFLAGS = -MD -MP -MF $(@).d -CFLAGS += -mcpu=$(MCU) $(THUMB_IW) $(GENDEPFLAGS) +CFLAGS += -mcpu=$(MCU) $(THUMB) $(THUMB_IW) $(GENDEPFLAGS) ASFLAGS = -Wa,-adhlns=$(OBJDIR)/$(<:.s=.lst)#,--g$(DEBUG) -ASFLAGS += -mcpu=$(MCU) $(THUMB_IW) -x assembler-with-cpp +ASFLAGS += -mcpu=$(MCU) $(THUMB) $(THUMB_IW) -x assembler-with-cpp ASFLAGS += $(GENDEPFLAGS) LDFLAGS = -nostartfiles -Wl,-Map=$(TARGET).map,--cref,--gc-sections LDFLAGS += -lc -lgcc diff --git a/src/common/dfu.c b/src/common/dfu.c index 971f0bd..a896867 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -35,17 +35,17 @@ #include "usb.h" /* DFU globals */ -u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ - DFUStatus dfuAppStatus; /* includes state */ - bool userFlash = FALSE; -bool dfuBusy = FALSE; +volatile u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ +DFUStatus dfuAppStatus; /* includes state */ +volatile bool userFlash = FALSE; +volatile bool dfuBusy = FALSE; - u8 recvBuffer[wTransferSize]; - u32 userFirmwareLen = 0; - u16 thisBlockLen = 0; +volatile u8 recvBuffer[wTransferSize]; +volatile u32 userFirmwareLen = 0; +volatile u16 thisBlockLen = 0; -PLOT code_copy_lock; +volatile PLOT code_copy_lock; /* todo: force dfu globals to be singleton to avoid re-inits? */ void dfuInit(void) { @@ -151,6 +151,7 @@ bool dfuUpdateByRequest(void) { /* if were actually done writing, goto sync, else stay busy */ if (code_copy_lock == END) { dfuAppStatus.bwPollTimeout0 = 0x00; + dfuAppStatus.bwPollTimeout1 = 0x00; code_copy_lock=WAIT; dfuAppStatus.bState = dfuDNLOAD_IDLE; } else { diff --git a/src/common/dfu.h b/src/common/dfu.h index 252a6db..491998d 100644 --- a/src/common/dfu.h +++ b/src/common/dfu.h @@ -93,7 +93,7 @@ typedef enum _PLOT { -extern bool dfuBusy; +extern volatile bool dfuBusy; /* exposed functions */ void dfuInit(void); /* singleton dfu initializer */ From 689eda025a450dfef9a0812535aeb7f6acab17e7 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 22:26:40 +0400 Subject: [PATCH 17/66] Added RUNAPP_ALT hack, see README.porting for details Signed-off-by: Andrew 'Necromant' Andrianov --- README.porting | 8 ++++++++ boards/te-stm32f103ret6kit/config.h | 5 ++--- src/common/dfu.c | 6 ++++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/README.porting b/README.porting index 5ec158a..511c7aa 100644 --- a/README.porting +++ b/README.porting @@ -23,6 +23,14 @@ CONFIG_INHIBIT_STROBE CONFIG_DISABLE_USB_SUSPEND Disables USB suspend/resume support and saves quite a few bytes +CONFIG_RUNAPP_ALT + !!!This is a hack, that breaks the dfu standart. + Define this the to the altsetting number, e.g. 0, 1, etc. + Trying to download anything to that altsetting will cause immediate + jump to user code. dfu-util should exit due to device 'disconnect' + In this case you would normally want to tune the number of altsettings + to 3, to have a spare altsetting (or you can sacrifice RAM altsetting 0) + You can also adjust the USER_CODE_FLASH to some lower adress, depending on the resulting loader size. Default maple settings waste a lot of space. diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index cc326b9..1b274ed 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -33,10 +33,9 @@ #ifndef __CONFIG_H #define __CONFIG_H -//WTF WAS THAT DOING HERE?? -//#include "common.h" - #define NUM_ALT_SETTINGS 2 + +#define CONFIG_RUNAPP_ALT 0 //No LED strobing whatsoever - saves space #define CONFIG_INHIBIT_STROBE diff --git a/src/common/dfu.c b/src/common/dfu.c index a896867..b275bad 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -90,6 +90,12 @@ bool dfuUpdateByRequest(void) { flashUnlock(); } else { +#ifdef CONFIG_RUNAPP_ALT +if (pInformation->Current_AlternateSetting == CONFIG_RUNAPP_ALT) { + boardTeardown(); + jumpToUser(USER_CODE_FLASH); +} +#endif userAppAddr = USER_CODE_RAM; userFlash = FALSE; } From 465f9d9f75614780c0732c222d7a9c1866e48b9d Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 1 Jun 2012 22:33:10 +0400 Subject: [PATCH 18/66] Fix a typo Signed-off-by: Andrew 'Necromant' Andrianov --- README.porting | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.porting b/README.porting index 511c7aa..82ecf90 100644 --- a/README.porting +++ b/README.porting @@ -39,7 +39,8 @@ board.c has 2 functions that need to be implemented. boardInit() - This should init the board, any leds, buttons, clocks and present the usb device to the host -boardTeardown() - should disconnect the device from host for good. +boardTeardown() - should disconnect the device from host for good. Any + other code you want to run defore exiting loader goes here usb_descriptors.c gives you fine-tuning of the device descriptors. From b9ed91943bc8bf2dcd86a250e5019028e94735e9 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 2 Jun 2012 01:29:20 +0400 Subject: [PATCH 19/66] Wrap bootloaderExitCondition in #ifdef's Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/main.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/common/main.c b/src/common/main.c index 429a6d8..252dc41 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -38,7 +38,7 @@ int main() { systemReset(); // peripherals but not PC boardInit(); setupFLASH(); - //usbAppInit(); + usbAppInit(); strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); #ifdef CONFIG_EXTRA_MAIN_CODE CONFIG_EXTRA_MAIN_CODE @@ -56,8 +56,10 @@ int main() { if (dfuUploadStarted()) { dfuFinishUpload(); // systemHardReset from DFU once done } + #ifdef bootloaderExitCondition if (bootloaderExitCondition) break; + #endif } if (checkUserCode(USER_CODE_RAM)) { From 7e0defd87aa19d06c10c67468579f33532852236 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 2 Jun 2012 10:23:57 +0400 Subject: [PATCH 20/66] Uncomment some quite critical code, that got chewed up Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/main.c | 54 +++++++++++++++++++++++------------------------ 1 file changed, 27 insertions(+), 27 deletions(-) diff --git a/src/common/main.c b/src/common/main.c index 252dc41..51a241c 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -35,14 +35,14 @@ #include "dfu.h" int main() { - systemReset(); // peripherals but not PC - boardInit(); - setupFLASH(); - usbAppInit(); - strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); - #ifdef CONFIG_EXTRA_MAIN_CODE - CONFIG_EXTRA_MAIN_CODE - #endif + systemReset(); // peripherals but not PC + boardInit(); + setupFLASH(); + usbAppInit(); + strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); +#ifdef CONFIG_EXTRA_MAIN_CODE + CONFIG_EXTRA_MAIN_CODE +#endif /* This code now moved to CONFIG_EXTRA_MAIN_CODE */ // bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); @@ -51,25 +51,25 @@ int main() { // #endif /* wait for host to upload program or halt bootloader */ - while (bootloaderCondition) { - strobePin(LED_BANK,LED,1,BLINK_SLOW); - if (dfuUploadStarted()) { - dfuFinishUpload(); // systemHardReset from DFU once done - } - #ifdef bootloaderExitCondition - if (bootloaderExitCondition) - break; - #endif - } + while (bootloaderCondition) { + strobePin(LED_BANK,LED,1,BLINK_SLOW); + if (dfuUploadStarted()) { + dfuFinishUpload(); // systemHardReset from DFU once done + } +#ifdef bootloaderExitCondition + if (bootloaderExitCondition) + break; +#endif + } - if (checkUserCode(USER_CODE_RAM)) { - jumpToUser(USER_CODE_RAM); - } else if (checkUserCode(USER_CODE_FLASH)) { - jumpToUser(USER_CODE_FLASH); - } else { - // some sort of fault occurred, hard reset - strobePin(LED_BANK,LED,5,BLINK_FAST); - systemHardReset(); - } + if (checkUserCode(USER_CODE_RAM)) { + jumpToUser(USER_CODE_RAM); + } else if (checkUserCode(USER_CODE_FLASH)) { + jumpToUser(USER_CODE_FLASH); + } else { + // some sort of fault occurred, hard reset + strobePin(LED_BANK,LED,5,BLINK_FAST); + systemHardReset(); + } return 0; } From 73d06e4b52ebf90c5d0fd5bd0f094431dfba8038 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 2 Jun 2012 19:55:11 +0400 Subject: [PATCH 21/66] Implement optional INFO endpoint Looks useless, unless I can get upload code to work Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu.c | 563 ++++++++++++++++++++++++----------------------- 1 file changed, 284 insertions(+), 279 deletions(-) diff --git a/src/common/dfu.c b/src/common/dfu.c index b275bad..457d29c 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -49,330 +49,335 @@ volatile PLOT code_copy_lock; /* todo: force dfu globals to be singleton to avoid re-inits? */ void dfuInit(void) { - dfuAppStatus.bStatus = OK; - dfuAppStatus.bwPollTimeout0 = 0x00; - dfuAppStatus.bwPollTimeout1 = 0x00; - dfuAppStatus.bwPollTimeout2 = 0x00; - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */ - userFirmwareLen = 0; - thisBlockLen = 0;; - userAppAddr = USER_CODE_RAM; /* default RAM user code location */ - userFlash = FALSE; - code_copy_lock = WAIT; - dfuBusy=FALSE; + dfuAppStatus.bStatus = OK; + dfuAppStatus.bwPollTimeout0 = 0x00; + dfuAppStatus.bwPollTimeout1 = 0x00; + dfuAppStatus.bwPollTimeout2 = 0x00; + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */ + userFirmwareLen = 0; + thisBlockLen = 0;; + userAppAddr = USER_CODE_RAM; /* default RAM user code location */ + userFlash = FALSE; + code_copy_lock = WAIT; + dfuBusy=FALSE; } bool dfuUpdateByRequest(void) { - /* were using the global pInformation struct from usb_lib here, - see comment in maple_dfu.h around DFUEvent struct */ - dfuBusy = TRUE; + /* were using the global pInformation struct from usb_lib here, + see comment in maple_dfu.h around DFUEvent struct */ + dfuBusy = TRUE; + + u8 startState = dfuAppStatus.bState; + dfuAppStatus.bStatus = OK; + /* often leaner to nest if's then embed a switch/case */ + if (startState == dfuIDLE) { + /* device running inside DFU mode */ + dfuBusy = TRUE; // signals the main loop to defer to the dfu write-loop + if (pInformation->USBbRequest == DFU_DNLOAD) { + if (pInformation->USBwLengths.w > 0) { + userFirmwareLen = 0; + dfuAppStatus.bState = dfuDNLOAD_SYNC; + if ((pInformation->Current_AlternateSetting == 1) +#ifdef CONFIG_INFO_ALT + || (pInformation->Current_AlternateSetting == 2) +#endif + ) { +#ifndef CONFIG_INFO_ALT + userAppAddr = USER_CODE_FLASH; +#else + userAppAddr = (pInformation->Current_AlternateSetting == 2) ? USER_INFO_FLASH : USER_CODE_FLASH; +#endif + userFlash = TRUE; + /* make sure the flash is setup properly, unlock it */ + setupFLASH(); + flashUnlock(); + + } else { +#ifdef CONFIG_RUNAPP_ALT + if (pInformation->Current_AlternateSetting == CONFIG_RUNAPP_ALT) { + boardTeardown(); + jumpToUser(USER_CODE_FLASH); + } +#endif + userAppAddr = USER_CODE_RAM; + userFlash = FALSE; + } + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errNOTDONE; + } + } else if (pInformation->USBbRequest == DFU_UPLOAD) { + dfuAppStatus.bState = dfuUPLOAD_IDLE; + } else if (pInformation->USBbRequest == DFU_ABORT) { + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */ + } else if (pInformation->USBbRequest == DFU_GETSTATUS) { + dfuAppStatus.bState = dfuIDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuIDLE; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + } else if (startState == dfuDNLOAD_SYNC) { + /* device received block, waiting for DFU_GETSTATUS request */ + + if (pInformation->USBbRequest == DFU_GETSTATUS) { + /* todo, add routine to wait for last block write to finish */ + if (userFlash) { + if (code_copy_lock==WAIT) { + code_copy_lock=BEGINNING; + dfuAppStatus.bwPollTimeout0 = 0xFF; /* is this enough? */ + dfuAppStatus.bwPollTimeout1 = 0x01; /* is this enough? */ + dfuAppStatus.bState=dfuDNBUSY; + + } else if (code_copy_lock==BEGINNING) { + dfuAppStatus.bState=dfuDNLOAD_SYNC; + + } else if (code_copy_lock==MIDDLE) { + dfuAppStatus.bState=dfuDNLOAD_SYNC; + + } else if (code_copy_lock==END) { + dfuAppStatus.bwPollTimeout0 = 0x00; + code_copy_lock=WAIT; + dfuAppStatus.bState=dfuDNLOAD_IDLE; + } + + } else { + dfuAppStatus.bState = dfuDNLOAD_IDLE; + dfuCopyBufferToExec(); + } + + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuDNLOAD_SYNC; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + } else if (startState == dfuDNBUSY) { + /* if were actually done writing, goto sync, else stay busy */ + if (code_copy_lock == END) { + dfuAppStatus.bwPollTimeout0 = 0x00; + dfuAppStatus.bwPollTimeout1 = 0x00; + code_copy_lock=WAIT; + dfuAppStatus.bState = dfuDNLOAD_IDLE; + } else { + dfuAppStatus.bState= dfuDNBUSY; + } + + } else if (startState == dfuDNLOAD_IDLE) { + /* device is expecting dfu_dnload requests */ + if (pInformation->USBbRequest == DFU_DNLOAD) { + if (pInformation->USBwLengths.w > 0) { + dfuAppStatus.bState = dfuDNLOAD_SYNC; + } else { + /* todo, support "disagreement" if device expects more data than this */ + dfuAppStatus.bState = dfuMANIFEST_SYNC; + + /* relock the flash */ + flashLock(); + } + } else if (pInformation->USBbRequest == DFU_ABORT) { + dfuAppStatus.bState = dfuIDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATUS) { + dfuAppStatus.bState = dfuIDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuIDLE; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + } else if (startState == dfuMANIFEST_SYNC) { + /* device has received last block, waiting DFU_GETSTATUS request */ + + if (pInformation->USBbRequest == DFU_GETSTATUS) { + dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; + dfuAppStatus.bStatus = OK; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuMANIFEST_SYNC; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + } else if (startState == dfuMANIFEST) { + /* device is in manifestation phase */ + + /* should never receive request while in manifest! */ + dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; + dfuAppStatus.bStatus = OK; + + } else if (startState == dfuMANIFEST_WAIT_RESET) { + /* device has programmed new firmware but needs external + usb reset or power on reset to run the new code */ + + /* consider timing out and self-resetting */ + dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; + + } else if (startState == dfuUPLOAD_IDLE) { + /* device expecting further dfu_upload requests */ + + if (pInformation->USBbRequest == DFU_UPLOAD) { + /* todo, add routine to wait for last block write to finish */ + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } else if (pInformation->USBbRequest == DFU_ABORT) { + dfuAppStatus.bState = dfuIDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATUS) { + dfuAppStatus.bState = dfuUPLOAD_IDLE; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuUPLOAD_IDLE; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } + + + } else if (startState == dfuERROR) { + /* status is in error, awaiting DFU_CLRSTATUS request */ + + if (pInformation->USBbRequest == DFU_GETSTATUS) { + /* todo, add routine to wait for last block write to finish */ + dfuAppStatus.bState = dfuERROR; + } else if (pInformation->USBbRequest == DFU_GETSTATE) { + dfuAppStatus.bState = dfuERROR; + } else if (pInformation->USBbRequest == DFU_CLRSTATUS) { + /* todo handle any cleanup we need here */ + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.bStatus = OK; + } else { + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } - u8 startState = dfuAppStatus.bState; - dfuAppStatus.bStatus = OK; - /* often leaner to nest if's then embed a switch/case */ - if (startState == dfuIDLE) { - /* device running inside DFU mode */ - dfuBusy = TRUE; // signals the main loop to defer to the dfu write-loop + } else { + /* some kind of error... */ + dfuAppStatus.bState = dfuERROR; + dfuAppStatus.bStatus = errSTALLEDPKT; + } - if (pInformation->USBbRequest == DFU_DNLOAD) { + if (dfuAppStatus.bStatus == OK) { + return TRUE; + } else { + return FALSE; + } +} - if (pInformation->USBwLengths.w > 0) { +void dfuUpdateByReset(void) { + u8 startState = dfuAppStatus.bState; userFirmwareLen = 0; - dfuAppStatus.bState = dfuDNLOAD_SYNC; - if (pInformation->Current_AlternateSetting == 1) { - userAppAddr = USER_CODE_FLASH; - userFlash = TRUE; + if (startState == appDETACH) { + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.bStatus = OK; - /* make sure the flash is setup properly, unlock it */ - setupFLASH(); - flashUnlock(); + nvicDisableInterrupts(); + usbEnbISR(); + } else if (startState == appIDLE || startState == dfuIDLE) { + /* do nothing...might be normal usb bus activity */ } else { -#ifdef CONFIG_RUNAPP_ALT -if (pInformation->Current_AlternateSetting == CONFIG_RUNAPP_ALT) { - boardTeardown(); - jumpToUser(USER_CODE_FLASH); -} -#endif - userAppAddr = USER_CODE_RAM; - userFlash = FALSE; - } - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errNOTDONE; - } - } else if (pInformation->USBbRequest == DFU_UPLOAD) { - dfuAppStatus.bState = dfuUPLOAD_IDLE; - } else if (pInformation->USBbRequest == DFU_ABORT) { - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; /* are we really ok? we were just aborted */ - } else if (pInformation->USBbRequest == DFU_GETSTATUS) { - dfuAppStatus.bState = dfuIDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuIDLE; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - } else if (startState == dfuDNLOAD_SYNC) { - /* device received block, waiting for DFU_GETSTATUS request */ - - if (pInformation->USBbRequest == DFU_GETSTATUS) { - /* todo, add routine to wait for last block write to finish */ - if (userFlash) { - if (code_copy_lock==WAIT) { - code_copy_lock=BEGINNING; - dfuAppStatus.bwPollTimeout0 = 0xFF; /* is this enough? */ - dfuAppStatus.bwPollTimeout1 = 0x01; /* is this enough? */ - dfuAppStatus.bState=dfuDNBUSY; - - } else if (code_copy_lock==BEGINNING) { - dfuAppStatus.bState=dfuDNLOAD_SYNC; - - } else if (code_copy_lock==MIDDLE) { - dfuAppStatus.bState=dfuDNLOAD_SYNC; - - } else if (code_copy_lock==END) { - dfuAppStatus.bwPollTimeout0 = 0x00; - code_copy_lock=WAIT; - dfuAppStatus.bState=dfuDNLOAD_IDLE; - } - - } else { - dfuAppStatus.bState = dfuDNLOAD_IDLE; - dfuCopyBufferToExec(); - } - - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuDNLOAD_SYNC; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - } else if (startState == dfuDNBUSY) { - /* if were actually done writing, goto sync, else stay busy */ - if (code_copy_lock == END) { - dfuAppStatus.bwPollTimeout0 = 0x00; - dfuAppStatus.bwPollTimeout1 = 0x00; - code_copy_lock=WAIT; - dfuAppStatus.bState = dfuDNLOAD_IDLE; - } else { - dfuAppStatus.bState= dfuDNBUSY; - } - - } else if (startState == dfuDNLOAD_IDLE) { - /* device is expecting dfu_dnload requests */ - if (pInformation->USBbRequest == DFU_DNLOAD) { - if (pInformation->USBwLengths.w > 0) { - dfuAppStatus.bState = dfuDNLOAD_SYNC; - } else { - /* todo, support "disagreement" if device expects more data than this */ - dfuAppStatus.bState = dfuMANIFEST_SYNC; - - /* relock the flash */ - flashLock(); - } - } else if (pInformation->USBbRequest == DFU_ABORT) { - dfuAppStatus.bState = dfuIDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATUS) { - dfuAppStatus.bState = dfuIDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuIDLE; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - } else if (startState == dfuMANIFEST_SYNC) { - /* device has received last block, waiting DFU_GETSTATUS request */ - - if (pInformation->USBbRequest == DFU_GETSTATUS) { - dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; - dfuAppStatus.bStatus = OK; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuMANIFEST_SYNC; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - } else if (startState == dfuMANIFEST) { - /* device is in manifestation phase */ - - /* should never receive request while in manifest! */ - dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; - dfuAppStatus.bStatus = OK; - - } else if (startState == dfuMANIFEST_WAIT_RESET) { - /* device has programmed new firmware but needs external - usb reset or power on reset to run the new code */ - - /* consider timing out and self-resetting */ - dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; - - } else if (startState == dfuUPLOAD_IDLE) { - /* device expecting further dfu_upload requests */ - - if (pInformation->USBbRequest == DFU_UPLOAD) { - /* todo, add routine to wait for last block write to finish */ - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } else if (pInformation->USBbRequest == DFU_ABORT) { - dfuAppStatus.bState = dfuIDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATUS) { - dfuAppStatus.bState = dfuUPLOAD_IDLE; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuUPLOAD_IDLE; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - - } else if (startState == dfuERROR) { - /* status is in error, awaiting DFU_CLRSTATUS request */ - - if (pInformation->USBbRequest == DFU_GETSTATUS) { - /* todo, add routine to wait for last block write to finish */ - dfuAppStatus.bState = dfuERROR; - } else if (pInformation->USBbRequest == DFU_GETSTATE) { - dfuAppStatus.bState = dfuERROR; - } else if (pInformation->USBbRequest == DFU_CLRSTATUS) { - /* todo handle any cleanup we need here */ - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; - } else { - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - } else { - /* some kind of error... */ - dfuAppStatus.bState = dfuERROR; - dfuAppStatus.bStatus = errSTALLEDPKT; - } - - if (dfuAppStatus.bStatus == OK) { - return TRUE; - } else { - return FALSE; - } -} + /* we reset from the dfu, reset everything and startover, + which is the correct operation if this is an erroneous + event or properly following a MANIFEST */ + dfuAppStatus.bState = dfuIDLE; + dfuAppStatus.bStatus = OK; -void dfuUpdateByReset(void) { - u8 startState = dfuAppStatus.bState; - userFirmwareLen = 0; - - if (startState == appDETACH) { - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; - - nvicDisableInterrupts(); - usbEnbISR(); - - } else if (startState == appIDLE || startState == dfuIDLE) { - /* do nothing...might be normal usb bus activity */ - } else { - /* we reset from the dfu, reset everything and startover, - which is the correct operation if this is an erroneous - event or properly following a MANIFEST */ - dfuAppStatus.bState = dfuIDLE; - dfuAppStatus.bStatus = OK; - - systemHardReset(); - } + systemHardReset(); + } } void dfuUpdateByTimeout(void) { } u8* dfuCopyState(u16 length) { - if (length == 0) { - pInformation->Ctrl_Info.Usb_wLength=1; - return NULL; - } else { - return (&(dfuAppStatus.bState)); - } + if (length == 0) { + pInformation->Ctrl_Info.Usb_wLength=1; + return NULL; + } else { + return (&(dfuAppStatus.bState)); + } } u8* dfuCopyStatus(u16 length) { - if (length == 0) { - pInformation->Ctrl_Info.Usb_wLength = 6; - return NULL; - } else { - return( (u8*) &dfuAppStatus); - } + if (length == 0) { + pInformation->Ctrl_Info.Usb_wLength = 6; + return NULL; + } else { + return( (u8*) &dfuAppStatus); + } } u8* dfuCopyDNLOAD(u16 length) { - if (length==0) { - pInformation->Ctrl_Info.Usb_wLength = pInformation->USBwLengths.w - pInformation->Ctrl_Info.Usb_wOffset; - thisBlockLen = pInformation->USBwLengths.w; - return NULL; - } else { - return ((u8*)recvBuffer + pInformation->Ctrl_Info.Usb_wOffset); - } + if (length==0) { + pInformation->Ctrl_Info.Usb_wLength = pInformation->USBwLengths.w - pInformation->Ctrl_Info.Usb_wOffset; + thisBlockLen = pInformation->USBwLengths.w; + return NULL; + } else { + return ((u8*)recvBuffer + pInformation->Ctrl_Info.Usb_wOffset); + } } u8* dfuCopyUPLOAD(u16 length) { - /* not implemented here nor supported by dfu-util */ - return NULL; + /* not implemented here nor supported by dfu-util */ + return NULL; } void dfuCopyBufferToExec() { - int i; - u32* userSpace; - - if (!userFlash) { - userSpace = (u32*)(USER_CODE_RAM+userFirmwareLen); - /* we dont need to handle when thisBlock len is not divisible by 4, - since the linker will align everything to 4B anyway */ - for (i=0;i Date: Sat, 2 Jun 2012 19:55:35 +0400 Subject: [PATCH 22/66] Include config.h in usb.c Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/usb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/usb.c b/src/common/usb.c index 12544ff..7dfb5a5 100644 --- a/src/common/usb.c +++ b/src/common/usb.c @@ -29,7 +29,7 @@ * in the pre-attached state. includes some of the lower level callbacks * needed by the usb library, like suspend,resume,init,etc */ - +#include "config.h" #include "usb.h" #include "dfu.h" From ac7facf783caa3107dfcc6c91aa6ef888c3460c6 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 2 Jun 2012 19:58:00 +0400 Subject: [PATCH 23/66] Fixes to board files te-stm32f103ret6kit: Fix usb descriptors maple: fix config.h to reflect recent core changes Signed-off-by: Andrew 'Necromant' Andrianov --- boards/maple/config.h | 9 +- boards/te-stm32f103ret6kit/config.h | 8 +- boards/te-stm32f103ret6kit/usb_descriptor.c | 115 ++++++++++++-------- src/common/usb_descriptor.h | 4 +- 4 files changed, 82 insertions(+), 54 deletions(-) diff --git a/boards/maple/config.h b/boards/maple/config.h index ac2c6a0..798d752 100644 --- a/boards/maple/config.h +++ b/boards/maple/config.h @@ -1,4 +1,4 @@ -/* ***************************************************************************** +/****************************************************************************** * The MIT License * * Copyright (c) 2010 LeafLabs LLC. @@ -37,7 +37,7 @@ //#include "common.h" #define NUM_ALT_SETTINGS 2 - +#define STR_DESC_LEN 6 //#define INHIBIT_LEDS #define LED_BANK GPIOA @@ -52,8 +52,13 @@ #define STARTUP_BLINKS 5 #define BOOTLOADER_WAIT 6 + +//Enable informational altsetting +//#define CONFIG_INFO_ALT + #define USER_CODE_RAM ((u32)0x20000C00) #define USER_CODE_FLASH ((u32)0x08005000) +#define USER_INFO_FLASH ((u32)0x08004000) #define VEND_ID0 0xAF #define VEND_ID1 0x1E diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index 1b274ed..8eae72c 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -33,7 +33,8 @@ #ifndef __CONFIG_H #define __CONFIG_H -#define NUM_ALT_SETTINGS 2 +#define NUM_ALT_SETTINGS 3 +#define STR_DESC_LEN 7 #define CONFIG_RUNAPP_ALT 0 //No LED strobing whatsoever - saves space @@ -55,8 +56,9 @@ #define STARTUP_BLINKS 5 #define BOOTLOADER_WAIT 6 -#define USER_CODE_RAM ((u32)0x20000C00) -#define USER_CODE_FLASH ((u32)0x08005000) +#define USER_CODE_RAM ((u32) 0x20000C00) +#define USER_CODE_FLASH ((u32) 0x08005000) +#define USER_INFO_FLASH ((us32) 0x08004000) #define VEND_ID0 0xAD #define VEND_ID1 0xDE diff --git a/boards/te-stm32f103ret6kit/usb_descriptor.c b/boards/te-stm32f103ret6kit/usb_descriptor.c index a55bbf5..43041ae 100644 --- a/boards/te-stm32f103ret6kit/usb_descriptor.c +++ b/boards/te-stm32f103ret6kit/usb_descriptor.c @@ -85,7 +85,7 @@ ONE_DESCRIPTOR usbFunctionalDescriptor = }; -u8 u8_usbConfigDescriptorDFU[36] = +u8 u8_usbConfigDescriptorDFU[] = { 0x09, /* bLength: Configuation Descriptor size */ 0x02, /* bDescriptorType: Configuration */ @@ -106,9 +106,7 @@ u8 u8_usbConfigDescriptorDFU[36] = 0x00, /* bNumEndpoints*/ 0xFE, /* bInterfaceClass: DFU */ 0x01, /* bInterfaceSubClass */ - 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ - 0x04, /* iInterface: */ /************ Descriptor of DFU interface 0 Alternate setting 1 *********/ @@ -119,11 +117,20 @@ u8 u8_usbConfigDescriptorDFU[36] = 0x00, /* bNumEndpoints*/ 0xFE, /* bInterfaceClass: DFU */ 0x01, /* bInterfaceSubClass */ - 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ - 0x05, /* iInterface: */ + /************ Descriptor of DFU interface 0 Alternate setting 2 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x02, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + 0x06, /* iInterface: */ + /******************** DFU Functional Descriptor********************/ 0x09, /*blength = 7 Bytes*/ 0x21, /* DFU Functional Descriptor*/ @@ -152,54 +159,70 @@ u8 u8_usbStringLangId[0x04] = 0x04 /* LangID = 0x0409: U.S. English */ }; -u8 u8_usbStringVendor[0x12] = - { - 0x12, - 0x03, - 'L',0,'e',0,'a',0,'f',0,'L',0,'a',0,'b',0,'s',0 - }; -u8 u8_usbStringProduct[0x14] = - { - 0x14, - 0x03, - 'M',0,'a',0,'p',0,'l',0,'e',0,' ',0,'0',0,'0',0,'3',0 - }; -u8 u8_usbStringSerial[0x10] = - { - 0x10, - 0x03, - 'L',0,'L',0,'M',0,' ',0,'0',0,'0',0,'3',0 - }; -u8 u8_usbStringAlt0[0x36] = - { - 0x36, - 0x03, - 'D',0,'F',0,'U',0,' ',0,'P',0,'r',0,'o',0,'g',0,'r',0, - 'a',0,'m',0,' ',0,'R',0,'A',0,'M',0,' ',0,'0',0,'x',0, - '2',0,'0',0,'0',0,'0',0,'0',0,'C',0,'0',0,'0',0 - }; +u8 u8_usbStringVendor[] = +{ + 0x24, + 0x03, + 'T', 0x0, 'e', 0x0, 'r', 0x0, 'r', 0x0, 'a', 0x0, ' ', + 0x0, 'E', 0x0, 'l', 0x0, 'e', 0x0, 'c', 0x0, 't', 0x0, 'r', + 0x0, 'o', 0x0, 'n', 0x0, 'i', 0x0, 'c', 0x0, 'a', 0x0, +}; + +u8 u8_usbStringProduct[] = +{ + 0x26, + 0x03, + 'T', 0x0, 'e', 0x0, 'r', 0x0, 'r', 0x0, 'a', 0x0, 'D', 0x0, 'u', + 0x0, 'm', 0x0, 'b', 0x0, 'y', 0x0, 'B', 0x0, 'o', 0x0, 'a', 0x0, 'r', + 0x0, 'd', 0x0, 'K', 0x0, 'i', 0x0, 't', 0x0, +}; + +u8 u8_usbStringSerial[] = +{ + 0x2A, + 0x03, + 'T', 0x0, 'E', 0x0, '-', 0x0, 'S', 0x0, 'T', 0x0, 'M', 0x0, '3', + 0x0, '2', 0x0, 'F', 0x0, '1', 0x0, '0', 0x0, '3', 0x0, 'R', 0x0, + 'E', 0x0, 'T', 0x0, '6', 0x0, '-', 0x0, 'K', 0x0, 'I', 0x0, 'T', 0x0, +}; + +u8 u8_usbStringAlt0[] = +{ + 0x16, + 0x03, + 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, + 'R', 0x0, 'A', 0x0, 'M', 0x0, +}; + +u8 u8_usbStringAlt1[] = +{ + 0x1A, + 0x03, + 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, 'F', + 0x0, 'l', 0x0, 'a', 0x0, 's', 0x0, 'h', 0x0, +}; + +u8 u8_usbStringAlt2[] = +{ + 0x18, + 0x03, + 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, 'I', + 0x0, 'N', 0x0, 'F', 0x0, 'O', 0x0, +}; -u8 u8_usbStringAlt1[0x3A] = - { - 0x3A, - 0x03, - 'D',0,'F',0,'U',0,' ',0,'P',0,'r',0,'o',0,'g',0,'r',0, - 'a',0,'m',0,' ',0,'F',0,'L',0,'A',0,'S',0,'H',0,' ',0, - '0',0,'x',0,'0',0,'8',0,'0',0,'0',0,'5',0,'0',0,'0',0, - '0',0 - }; u8 u8_usbStringInterface = 0; -ONE_DESCRIPTOR usbStringDescriptor[6] = +ONE_DESCRIPTOR usbStringDescriptor[] = { { (u8*)u8_usbStringLangId, 0x04 }, - { (u8*)u8_usbStringVendor, 0x12 }, - { (u8*)u8_usbStringProduct, 0x20 }, - { (u8*)u8_usbStringSerial, 0x10 }, - { (u8*)u8_usbStringAlt0, 0x36 }, - { (u8*)u8_usbStringAlt1, 0x3A } + { (u8*)u8_usbStringVendor, 0x24 }, + { (u8*)u8_usbStringProduct, 0x26 }, + { (u8*)u8_usbStringSerial, 0x2A }, + { (u8*)u8_usbStringAlt0, 0x16 }, + { (u8*)u8_usbStringAlt1, 0x1A }, + { (u8*)u8_usbStringAlt2, 0x18 }, }; diff --git a/src/common/usb_descriptor.h b/src/common/usb_descriptor.h index d0b00b7..a969435 100644 --- a/src/common/usb_descriptor.h +++ b/src/common/usb_descriptor.h @@ -29,11 +29,9 @@ #include "usb_lib.h" #include "usb.h" -#define STR_DESC_LEN 6 - extern ONE_DESCRIPTOR usbDeviceDescriptorDFU; extern ONE_DESCRIPTOR usbConfigDescriptorDFU; -extern ONE_DESCRIPTOR usbStringDescriptor[6]; +extern ONE_DESCRIPTOR usbStringDescriptor[]; extern ONE_DESCRIPTOR usbFunctionalDescriptor; #endif From cb4399f3fd791d4897253a619ea5376062fca348 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 2 Jun 2012 20:46:39 +0400 Subject: [PATCH 24/66] Added initial stuff for Motor Cortex Board Signed-off-by: Andrew 'Necromant' Andrianov --- boards/mcortex/Makefile | 2 + boards/mcortex/board.c | 82 ++++++++++++ boards/mcortex/config.h | 78 +++++++++++ boards/mcortex/usb_descriptor.c | 223 ++++++++++++++++++++++++++++++++ 4 files changed, 385 insertions(+) create mode 100644 boards/mcortex/Makefile create mode 100644 boards/mcortex/board.c create mode 100644 boards/mcortex/config.h create mode 100644 boards/mcortex/usb_descriptor.c diff --git a/boards/mcortex/Makefile b/boards/mcortex/Makefile new file mode 100644 index 0000000..81a35d9 --- /dev/null +++ b/boards/mcortex/Makefile @@ -0,0 +1,2 @@ +CFLAGS+=-DSTM32F10X_MD +objects-y+=board.o usb_descriptor.o diff --git a/boards/mcortex/board.c b/boards/mcortex/board.c new file mode 100644 index 0000000..05c4f2f --- /dev/null +++ b/boards/mcortex/board.c @@ -0,0 +1,82 @@ +#include "stm32f10x_type.h" +#include "cortexm3_macro.h" +#include "common.h" +#include "hardware.h" + + +void setupLED (void) { + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup APB2 (GPIOG) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000100; + SET_REG(RCC_APB2ENR,rwmVal); + + /* Setup GPIOA Pin 5 as PP Out */ +// SET_REG(GPIO_CRL(GPIOG), 0x00100000); + +// rwmVal = GET_REG(GPIO_CRL(GPIOG)); +// rwmVal &= 0xFF0FFFFF; +// rwmVal |= 0x00100000; +// SET_REG(GPIO_CRL(GPIOG),rwmVal); + +// setPin(GPIOA,5); +} + + +/* Motor Cortex uses 12Mhz quartz */ +void setupCLK (void) { + /* enable HSE */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); + while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ + + /* enable flash prefetch buffer */ + SET_REG(FLASH_ACR, 0x00000012); + + /* Configure PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00110400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ + while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ + + /* Set SYSCLK as PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); + while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ +} + +void setupUSB (void) { + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup the USB DISC Pin */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000010; + SET_REG(RCC_APB2ENR,rwmVal); + + // todo, macroize usb_disc pin + /* Setup GPIOC Pin 12 as OD out */ + rwmVal = GET_REG(GPIO_CRH(GPIOA)); + rwmVal &= 0xFFF0FFFF; + rwmVal |= 0x00050000; + setPin (GPIOA,12); + SET_REG(GPIO_CRH(GPIOA),rwmVal); + pRCC->APB1ENR |= 0x00800000; + /* initialize the usb application */ + resetPin (GPIOA,12); /* present ourselves to the host */ + u32 c = 120000000; + while (c--); +} + + +/* executed before actual jump to user code */ +void boardTeardown() +{ + //resetPin(GPIOB,5); // disconnect usb from host. todo, macroize pin +} + +/* This is a common routine to setup the board */ +void boardInit() +{ + setupCLK(); + setupLED(); + setupUSB(); +} diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h new file mode 100644 index 0000000..8eae72c --- /dev/null +++ b/boards/mcortex/config.h @@ -0,0 +1,78 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ****************************************************************************/ + +/** + * @file config.h + * + * @brief bootloader settings and macro defines + * + * + */ + +#ifndef __CONFIG_H +#define __CONFIG_H + +#define NUM_ALT_SETTINGS 3 +#define STR_DESC_LEN 7 + +#define CONFIG_RUNAPP_ALT 0 +//No LED strobing whatsoever - saves space +#define CONFIG_INHIBIT_STROBE + +//Replace USB suspend/resume code with dummies. +//Saves a few bytes more + +#define CONFIG_DISABLE_USB_SUSPEND + +#define LED_BANK GPIOA +#define LED 5 +#define BLINK_FAST 0x50000 +#define BLINK_SLOW 0x100000 + +#define BUTTON_BANK GPIOC +#define BUTTON 9 + +#define STARTUP_BLINKS 5 +#define BOOTLOADER_WAIT 6 + +#define USER_CODE_RAM ((u32) 0x20000C00) +#define USER_CODE_FLASH ((u32) 0x08005000) +#define USER_INFO_FLASH ((us32) 0x08004000) + +#define VEND_ID0 0xAD +#define VEND_ID1 0xDE +#define PROD_ID0 0xDE +#define PROD_ID1 0xC0 + + +//Any extra code for main, e.g. for timeouts +//#define CONFIG_EXTRA_MAIN_CODE + +/* while this is '1' we're looping in the bootloader */ +#define bootloaderCondition (1) + +/* define to 0 to never exit, undefine to save space */ +#define bootloaderExitCondition 0 + +#endif diff --git a/boards/mcortex/usb_descriptor.c b/boards/mcortex/usb_descriptor.c new file mode 100644 index 0000000..5061d79 --- /dev/null +++ b/boards/mcortex/usb_descriptor.c @@ -0,0 +1,223 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ****************************************************************************/ + + +/** + * @file usb_descriptor.c + * + * @brief aka application descriptor; big static struct and callbacks for sending + * the descriptor. + * + */ + + +#include "usb_descriptor.h" + +u8 u8_usbDeviceDescriptorDFU[18] = + { + 0x12, /* bLength */ + 0x01, /* bDescriptorType */ + 0x00, /* bcdUSB, version 1.00 */ + 0x01, + 0x00, /* bDeviceClass : See interface */ + 0x00, /* bDeviceSubClass : See interface*/ + 0x00, /* bDeviceProtocol : See interface */ + bMaxPacketSize, /* bMaxPacketSize0 0x40 = 64 */ + VEND_ID0, /* idVendor (0110) */ + VEND_ID1, + + PROD_ID0, /* idProduct (0x1001 or 1002) */ + PROD_ID1, + + 0x01, /* bcdDevice*/ + 0x02, + 0x01, /* iManufacturer : index of string Manufacturer */ + 0x02, /* iProduct : index of string descriptor of product*/ + 0x03, /* iSerialNumber : index of string serial number*/ + 0x01 /*bNumConfigurations */ + }; + +ONE_DESCRIPTOR usbDeviceDescriptorDFU = + { + u8_usbDeviceDescriptorDFU, + 0x12 + }; + +u8 u8_usbFunctionalDescriptor[9] = + { + /******************** DFU Functional Descriptor********************/ + 0x09, /*blength = 7 Bytes*/ + 0x21, /* DFU Functional Descriptor*/ + 0x01, /*bmAttribute, can only download for now */ + 0xFF, /*DetachTimeOut= 255 ms*/ + 0x00, + (wTransferSize & 0x00FF), + (wTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ + 0x10, /* bcdDFUVersion = 1.1 */ + 0x01 + }; + +ONE_DESCRIPTOR usbFunctionalDescriptor = + { + u8_usbFunctionalDescriptor, + 0x09 + }; + + +u8 u8_usbConfigDescriptorDFU[] = + { + 0x09, /* bLength: Configuation Descriptor size */ + 0x02, /* bDescriptorType: Configuration */ + 0x24, /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /* bNumInterfaces: 1 interface */ + 0x01, /* bConfigurationValue: */ + 0x00, /* iConfiguration: */ + 0x80, /* bmAttributes: */ + 0x32, /* MaxPower 100 mA */ + /* 09 */ + + /************ Descriptor of DFU interface 0 Alternate setting 0 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + 0x04, /* iInterface: */ + + /************ Descriptor of DFU interface 0 Alternate setting 1 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x01, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + 0x05, /* iInterface: */ + + /************ Descriptor of DFU interface 0 Alternate setting 2 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x02, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + 0x06, /* iInterface: */ + + /******************** DFU Functional Descriptor********************/ + 0x09, /*blength = 7 Bytes*/ + 0x21, /* DFU Functional Descriptor*/ + 0x01, /*bmAttribute, can only download for now */ + 0xFF, /*DetachTimeOut= 255 ms*/ + 0x00, + (wTransferSize & 0x00FF), + (wTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ + 0x10, /* bcdDFUVersion = 1.1 */ + 0x01 + /***********************************************************/ + /*36*/ + }; + +ONE_DESCRIPTOR usbConfigDescriptorDFU = + { + u8_usbConfigDescriptorDFU, + 0x24 + }; + +u8 u8_usbStringLangId[0x04] = + { + 0x04, + 0x03, + 0x09, + 0x04 /* LangID = 0x0409: U.S. English */ + }; + + +u8 u8_usbStringVendor[] = +{ + 0x16, + 0x03, + 'n', 0x0, 'c', 0x0, 'r', 0x0, 'm', 0x0, 'n', 0x0, 't', 0x0, + '.', 0x0, 'o', 0x0, 'r', 0x0, 'g', 0x0, +}; + +u8 u8_usbStringProduct[] = +{ + 0x1A, + 0x03, + 'M', 0x0, 'o', 0x0, 't', 0x0, 'o', 0x0, 'r', 0x0, ' ', 0x0, + 'C', 0x0, 'o', 0x0, 'r', 0x0, 't', 0x0, 'e', 0x0, 'x', 0x0, +}; + +u8 u8_usbStringSerial[] = +{ + 0x1A, + 0x03, + '0', 0x0, '.', 0x0, '9', 0x0, ' ', 0x0, 'A', 0x0, 'z', 0x0, 'r', 0x0, + 'a', 0x0, ' ', 0x0, 'O', 0x0, 'n', 0x0, 'e', 0x0, +}; + +u8 u8_usbStringAlt0[] = +{ + 0x16, + 0x03, + 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, + 'R', 0x0, 'A', 0x0, 'M', 0x0, +}; + +u8 u8_usbStringAlt1[] = +{ + 0x1A, + 0x03, + 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, 'F', + 0x0, 'l', 0x0, 'a', 0x0, 's', 0x0, 'h', 0x0, +}; + +u8 u8_usbStringAlt2[] = +{ + 0x18, + 0x03, + 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, 'I', + 0x0, 'N', 0x0, 'F', 0x0, 'O', 0x0, +}; + + +u8 u8_usbStringInterface = 0; + +ONE_DESCRIPTOR usbStringDescriptor[] = + { + { (u8*)u8_usbStringLangId, 0x04 }, + { (u8*)u8_usbStringVendor, 0x16 }, + { (u8*)u8_usbStringProduct, 0x1A }, + { (u8*)u8_usbStringSerial, 0x1A }, + { (u8*)u8_usbStringAlt0, 0x16 }, + { (u8*)u8_usbStringAlt1, 0x1A }, + { (u8*)u8_usbStringAlt2, 0x18 }, + }; From 863fb00844e93567113a91388080ec353c266395 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 2 Jun 2012 23:45:48 +0400 Subject: [PATCH 25/66] Add GPIOG definition Signed-off-by: Andrew 'Necromant' Andrianov --- boards/mcortex/board.c | 11 ++++------- boards/mcortex/config.h | 12 +++++++----- src/common/hardware.h | 1 + 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/boards/mcortex/board.c b/boards/mcortex/board.c index 05c4f2f..f0a4f73 100644 --- a/boards/mcortex/board.c +++ b/boards/mcortex/board.c @@ -13,15 +13,12 @@ void setupLED (void) { rwmVal |= 0x00000100; SET_REG(RCC_APB2ENR,rwmVal); - /* Setup GPIOA Pin 5 as PP Out */ -// SET_REG(GPIO_CRL(GPIOG), 0x00100000); + rwmVal = GET_REG(GPIO_CRH(GPIOG)); + rwmVal &= 0xFFFFFFF0; + rwmVal |= 0x00000001; -// rwmVal = GET_REG(GPIO_CRL(GPIOG)); -// rwmVal &= 0xFF0FFFFF; -// rwmVal |= 0x00100000; -// SET_REG(GPIO_CRL(GPIOG),rwmVal); + SET_REG(GPIO_CRH(GPIOG),rwmVal); -// setPin(GPIOA,5); } diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index 8eae72c..917eb3f 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -37,16 +37,18 @@ #define STR_DESC_LEN 7 #define CONFIG_RUNAPP_ALT 0 +#define CONFIG_INFO_ALT + //No LED strobing whatsoever - saves space -#define CONFIG_INHIBIT_STROBE +//#define CONFIG_INHIBIT_STROBE //Replace USB suspend/resume code with dummies. //Saves a few bytes more -#define CONFIG_DISABLE_USB_SUSPEND +//#define CONFIG_DISABLE_USB_SUSPEND -#define LED_BANK GPIOA -#define LED 5 +#define LED_BANK GPIOG +#define LED 8 #define BLINK_FAST 0x50000 #define BLINK_SLOW 0x100000 @@ -58,7 +60,7 @@ #define USER_CODE_RAM ((u32) 0x20000C00) #define USER_CODE_FLASH ((u32) 0x08005000) -#define USER_INFO_FLASH ((us32) 0x08004000) +#define USER_INFO_FLASH ((u32) 0x08004000) #define VEND_ID0 0xAD #define VEND_ID1 0xDE diff --git a/src/common/hardware.h b/src/common/hardware.h index 35cbda6..cbd0ddf 100644 --- a/src/common/hardware.h +++ b/src/common/hardware.h @@ -41,6 +41,7 @@ #define GPIOA ((u32) 0x40010800) #define GPIOC ((u32) 0x40011000) #define GPIOB ((u32) 0x40010C00) +#define GPIOG ((u32) 0x40012000) #define RCC_CR RCC #define RCC_CFGR (RCC + 0x04) From fdbfdb3b833987af4a25159bcba377fad046838f Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sun, 3 Jun 2012 01:02:38 +0400 Subject: [PATCH 26/66] Nove FLASH_PAGE_SIZE to boardconfig Signed-off-by: Andrew 'Necromant' Andrianov --- boards/maple/config.h | 2 ++ boards/mcortex/config.h | 17 ++++++++++++----- boards/te-stm32f103ret6kit/config.h | 1 + src/common/dfu.c | 2 +- src/common/hardware.c | 2 +- src/common/usb.h | 2 +- 6 files changed, 18 insertions(+), 8 deletions(-) diff --git a/boards/maple/config.h b/boards/maple/config.h index 798d752..cbab164 100644 --- a/boards/maple/config.h +++ b/boards/maple/config.h @@ -36,6 +36,8 @@ //WTF WAS THAT DOING HERE?? //#include "common.h" +#define FLASH_PAGE_SIZE 0x400 + #define NUM_ALT_SETTINGS 2 #define STR_DESC_LEN 6 //#define INHIBIT_LEDS diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index 917eb3f..06c9a74 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -36,7 +36,7 @@ #define NUM_ALT_SETTINGS 3 #define STR_DESC_LEN 7 -#define CONFIG_RUNAPP_ALT 0 +//#define CONFIG_RUNAPP_ALT 0 #define CONFIG_INFO_ALT //No LED strobing whatsoever - saves space @@ -45,7 +45,10 @@ //Replace USB suspend/resume code with dummies. //Saves a few bytes more -//#define CONFIG_DISABLE_USB_SUSPEND +#define CONFIG_DISABLE_USB_SUSPEND + +/* F*ck, that costed me an hour of lifetime to figure out! */ +#define FLASH_PAGE_SIZE 0x800 #define LED_BANK GPIOG #define LED 8 @@ -56,7 +59,7 @@ #define BUTTON 9 #define STARTUP_BLINKS 5 -#define BOOTLOADER_WAIT 6 +#define BOOTLOADER_WAIT 16 #define USER_CODE_RAM ((u32) 0x20000C00) #define USER_CODE_FLASH ((u32) 0x08005000) @@ -72,9 +75,13 @@ //#define CONFIG_EXTRA_MAIN_CODE /* while this is '1' we're looping in the bootloader */ -#define bootloaderCondition (1) +#define CONFIG_EXTRA_MAIN_CODE \ + bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); \ + int delay_count =0; + +#define bootloaderCondition (delay_count++ < BOOTLOADER_WAIT) /* define to 0 to never exit, undefine to save space */ -#define bootloaderExitCondition 0 + //#define bootloaderExitCondition 0 #endif diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index 8eae72c..c289c71 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -35,6 +35,7 @@ #define NUM_ALT_SETTINGS 3 #define STR_DESC_LEN 7 +#define FLASH_PAGE_SIZE 0x800 #define CONFIG_RUNAPP_ALT 0 //No LED strobing whatsoever - saves space diff --git a/src/common/dfu.c b/src/common/dfu.c index 457d29c..f1141cd 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -30,7 +30,7 @@ * * */ - +#include "config.h" #include "dfu.h" #include "usb.h" diff --git a/src/common/hardware.c b/src/common/hardware.c index 7cb5095..ce63d82 100644 --- a/src/common/hardware.c +++ b/src/common/hardware.c @@ -199,7 +199,7 @@ bool flashErasePage(u32 pageAddr) { } bool flashErasePages(u32 pageAddr, u16 n) { while (n-->0) { - if (!flashErasePage(pageAddr+0x400*n)) { + if (!flashErasePage(pageAddr*FLASH_PAGE_SIZE*n)) { return FALSE; } } diff --git a/src/common/usb.h b/src/common/usb.h index 1f1ad1d..bd2a49e 100644 --- a/src/common/usb.h +++ b/src/common/usb.h @@ -37,7 +37,7 @@ #define ENDP3_RXADDR 0x110 #define bMaxPacketSize 0x40 /* 64B, maximum for usb FS devices */ -#define wTransferSize 0x0400 /* 1024B, want: maxpacket < wtransfer < 10KB (to ensure everything can live in ram */ +#define wTransferSize FLASH_PAGE_SIZE /* 1024B, want: maxpacket < wtransfer < 10KB (to ensure everything can live in ram */ #define NUM_ENDPTS 0x01 From 99acd2e8274bbc91554f1623496f9dd711c86301 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 00:01:08 +0400 Subject: [PATCH 27/66] Fix TODO/README and add stubs for further DFU refactoring Signed-off-by: Andrew 'Necromant' Andrianov --- TODO | 7 +++---- src/common/Makefile | 3 ++- src/common/dfu.c | 4 ---- src/common/dfu_flash.c | 0 src/common/dfu_fpga.c | 0 src/common/dfu_ram.c | 38 ++++++++++++++++++++++++++++++++++++++ src/common/dfu_spi.c | 0 7 files changed, 43 insertions(+), 9 deletions(-) create mode 100644 src/common/dfu_flash.c create mode 100644 src/common/dfu_fpga.c create mode 100644 src/common/dfu_ram.c create mode 100644 src/common/dfu_spi.c diff --git a/TODO b/TODO index 710c405..b2a592a 100644 --- a/TODO +++ b/TODO @@ -1,11 +1,10 @@ This is a personal TODO list. Clean up board init, free up some space *WiP -Do we need different LD script and c_startup for different uCs?? +Do we need different LD script and c_startup for different uCs?? LD for sure! +Generate LD with sizes stuff on-the-fly *WiP Shut up a shitload of warnings from usb_lib *DONE Merge here all the CREDITS, Licensing info and other shit *DONE (Somewhat) -Wrap altsettings in macros, add API for adding custom endpoints -Merge Xilinx-SSCU for Mcortex Add support for boards: mcortex mctx-dm sISP ucGUN, etc. Talk with leaflabs, merge&check their boards *WiP -fix coding style!!! +fix coding style!!! *WiP diff --git a/src/common/Makefile b/src/common/Makefile index 158bb4f..690598c 100644 --- a/src/common/Makefile +++ b/src/common/Makefile @@ -3,5 +3,6 @@ objects-common += dfu.o \ hardware.o \ main.o \ usb.o \ - usb_callbacks.o + usb_callbacks.o \ + dfu_ram.o diff --git a/src/common/dfu.c b/src/common/dfu.c index f1141cd..51ab4a8 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -44,7 +44,6 @@ volatile u8 recvBuffer[wTransferSize]; volatile u32 userFirmwareLen = 0; volatile u16 thisBlockLen = 0; - volatile PLOT code_copy_lock; /* todo: force dfu globals to be singleton to avoid re-inits? */ @@ -341,16 +340,13 @@ void dfuCopyBufferToExec() { } } else { userSpace = (u32*)(USER_CODE_FLASH+userFirmwareLen); - flashErasePage((u32)(userSpace)); - for (i=0;i Date: Tue, 5 Jun 2012 01:00:01 +0400 Subject: [PATCH 28/66] Move IO routines to dfu_io.c Also break configs for all boards Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/Makefile | 3 +- src/common/dfu.c | 126 +++++++++++++++-------------------------- src/common/dfu_flash.c | 0 src/common/dfu_fpga.c | 0 src/common/dfu_io.c | 78 +++++++++++++++++++++++++ src/common/dfu_ram.c | 38 ------------- src/common/dfu_spi.c | 0 7 files changed, 126 insertions(+), 119 deletions(-) delete mode 100644 src/common/dfu_flash.c delete mode 100644 src/common/dfu_fpga.c create mode 100644 src/common/dfu_io.c delete mode 100644 src/common/dfu_ram.c delete mode 100644 src/common/dfu_spi.c diff --git a/src/common/Makefile b/src/common/Makefile index 690598c..158bb4f 100644 --- a/src/common/Makefile +++ b/src/common/Makefile @@ -3,6 +3,5 @@ objects-common += dfu.o \ hardware.o \ main.o \ usb.o \ - usb_callbacks.o \ - dfu_ram.o + usb_callbacks.o diff --git a/src/common/dfu.c b/src/common/dfu.c index 51ab4a8..6fa59b9 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -37,7 +37,6 @@ /* DFU globals */ volatile u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ DFUStatus dfuAppStatus; /* includes state */ -volatile bool userFlash = FALSE; volatile bool dfuBusy = FALSE; volatile u8 recvBuffer[wTransferSize]; @@ -56,12 +55,18 @@ void dfuInit(void) { dfuAppStatus.iString = 0x00; /* all strings must be 0x00 until we make them! */ userFirmwareLen = 0; thisBlockLen = 0;; - userAppAddr = USER_CODE_RAM; /* default RAM user code location */ - userFlash = FALSE; code_copy_lock = WAIT; dfuBusy=FALSE; } +/* Hell yeah */ +#include "dfu_io.c" + +#define MAP_ALTSETTING_INIT(alt,func) \ + if ((pInformation->Current_AlternateSetting == alt) func(); + +void (*dfuCopyFunc)(void); + bool dfuUpdateByRequest(void) { /* were using the global pInformation struct from usb_lib here, see comment in maple_dfu.h around DFUEvent struct */ @@ -77,31 +82,26 @@ bool dfuUpdateByRequest(void) { if (pInformation->USBwLengths.w > 0) { userFirmwareLen = 0; dfuAppStatus.bState = dfuDNLOAD_SYNC; - if ((pInformation->Current_AlternateSetting == 1) -#ifdef CONFIG_INFO_ALT - || (pInformation->Current_AlternateSetting == 2) +#ifdef CONFIG_ALTSETTING_RAM + MAP_ALTSETTING_INIT(CONFIG_ALTSETTING_RAM,dfuToRamInit); #endif - ) { -#ifndef CONFIG_INFO_ALT - userAppAddr = USER_CODE_FLASH; -#else - userAppAddr = (pInformation->Current_AlternateSetting == 2) ? USER_INFO_FLASH : USER_CODE_FLASH; + +#ifdef CONFIG_ALTSETTING_FLASH + MAP_ALTSETTING_INIT(CONFIG_ALTSETTING_FLASH,dfuToFlashInit); #endif - userFlash = TRUE; - /* make sure the flash is setup properly, unlock it */ - setupFLASH(); - flashUnlock(); - - } else { -#ifdef CONFIG_RUNAPP_ALT - if (pInformation->Current_AlternateSetting == CONFIG_RUNAPP_ALT) { - boardTeardown(); - jumpToUser(USER_CODE_FLASH); - } + +#ifdef CONFIG_ALTSETTING_RUN + MAP_ALTSETTING_INIT(CONFIG_ALTSETTING_RUN,dfuToRunInit); +#endif + +#ifdef CONFIG_ALTSETTING_SPI + MAP_ALTSETTING_INIT(CONFIG_ALTSETTING_RUN,dfuToSPIInit); #endif - userAppAddr = USER_CODE_RAM; - userFlash = FALSE; - } + +#ifdef CONFIG_ALTSETTING_FPGA + MAP_ALTSETTING_INIT(CONFIG_ALTSETTING_FPGA,dfuToFPGAInit); +#endif + } else { dfuAppStatus.bState = dfuERROR; dfuAppStatus.bStatus = errNOTDONE; @@ -125,30 +125,25 @@ bool dfuUpdateByRequest(void) { if (pInformation->USBbRequest == DFU_GETSTATUS) { /* todo, add routine to wait for last block write to finish */ - if (userFlash) { - if (code_copy_lock==WAIT) { - code_copy_lock=BEGINNING; - dfuAppStatus.bwPollTimeout0 = 0xFF; /* is this enough? */ - dfuAppStatus.bwPollTimeout1 = 0x01; /* is this enough? */ - dfuAppStatus.bState=dfuDNBUSY; - - } else if (code_copy_lock==BEGINNING) { - dfuAppStatus.bState=dfuDNLOAD_SYNC; - - } else if (code_copy_lock==MIDDLE) { - dfuAppStatus.bState=dfuDNLOAD_SYNC; - - } else if (code_copy_lock==END) { - dfuAppStatus.bwPollTimeout0 = 0x00; - code_copy_lock=WAIT; - dfuAppStatus.bState=dfuDNLOAD_IDLE; - } - - } else { - dfuAppStatus.bState = dfuDNLOAD_IDLE; - dfuCopyBufferToExec(); + if (code_copy_lock==WAIT) { + code_copy_lock=BEGINNING; + dfuAppStatus.bwPollTimeout0 = 0xFF; /* is this enough? */ + dfuAppStatus.bwPollTimeout1 = 0x01; /* is this enough? */ + dfuAppStatus.bState=dfuDNBUSY; + + } else if (code_copy_lock==BEGINNING) { + dfuAppStatus.bState=dfuDNLOAD_SYNC; + + } else if (code_copy_lock==MIDDLE) { + dfuAppStatus.bState=dfuDNLOAD_SYNC; + + } else if (code_copy_lock==END) { + dfuAppStatus.bwPollTimeout0 = 0x00; + code_copy_lock=WAIT; + dfuAppStatus.bState=dfuDNLOAD_IDLE; } + } else if (pInformation->USBbRequest == DFU_GETSTATE) { dfuAppStatus.bState = dfuDNLOAD_SYNC; } else { @@ -327,29 +322,6 @@ u8* dfuCopyUPLOAD(u16 length) { return NULL; } -void dfuCopyBufferToExec() { - int i; - u32* userSpace; - - if (!userFlash) { - userSpace = (u32*)(USER_CODE_RAM+userFirmwareLen); - /* we dont need to handle when thisBlock len is not divisible by 4, - since the linker will align everything to 4B anyway */ - for (i=0;i RAM */ +void dfuToRamCopy() +{ + int i; + u32* userSpace = (u32*)(USER_CODE_RAM+userFirmwareLen); + /* we dont need to handle when thisBlock len is not divisible by 4, + since the linker will align everything to 4B anyway */ + for (i=0;i FLASH */ + +#ifdef CONFIG_ALTSETTING_FLASH +void dfuToFlashCopy() +{ + int i; + u32* userSpace = (u32*)(USER_CODE_FLASH+userFirmwareLen); + flashErasePage((u32)(userSpace)); + for (i=0;i FPGA */ diff --git a/src/common/dfu_ram.c b/src/common/dfu_ram.c deleted file mode 100644 index b888bd2..0000000 --- a/src/common/dfu_ram.c +++ /dev/null @@ -1,38 +0,0 @@ -#include "dfu.h" -#include "usb.h" - -extern volatile u8 recvBuffer[wTransferSize]; -extern volatile u32 userFirmwareLen; -extern volatile u16 thisBlockLen; - -void dfuCopyToRam() -{ - int i; - u32* userSpace = (u32*)(USER_CODE_RAM+userFirmwareLen); - /* we dont need to handle when thisBlock len is not divisible by 4, - since the linker will align everything to 4B anyway */ - for (i=0;i Date: Tue, 5 Jun 2012 01:18:36 +0400 Subject: [PATCH 29/66] fixe board configs to reflect recent dfu.c changes Signed-off-by: Andrew 'Necromant' Andrianov --- boards/maple/config.h | 14 +++++++++++--- boards/mcortex/config.h | 26 +++++++++++++++++--------- boards/te-stm32f103ret6kit/config.h | 19 ++++++++++++++++--- src/common/dfu_io.c | 4 ++-- 4 files changed, 46 insertions(+), 17 deletions(-) diff --git a/boards/maple/config.h b/boards/maple/config.h index cbab164..0e8ef6b 100644 --- a/boards/maple/config.h +++ b/boards/maple/config.h @@ -36,11 +36,14 @@ //WTF WAS THAT DOING HERE?? //#include "common.h" -#define FLASH_PAGE_SIZE 0x400 +/* THIS IS MCU-DEPENDANT! RTFM: Reference Manual */ +#define FLASH_PAGE_SIZE 0x800 #define NUM_ALT_SETTINGS 2 #define STR_DESC_LEN 6 -//#define INHIBIT_LEDS + +//No LED strobing whatsoever - saves space +#define CONFIG_INHIBIT_STROBE #define LED_BANK GPIOA #define LED 5 @@ -60,7 +63,12 @@ #define USER_CODE_RAM ((u32)0x20000C00) #define USER_CODE_FLASH ((u32)0x08005000) -#define USER_INFO_FLASH ((u32)0x08004000) + +#define CONFIG_ALSETTING_RAM 0 +#define CONFIG_ALSETTING_FLASH 1 +#define CONFIG_ALSETTING_RUN 2 +/* Any transfer to run altsetting will cause a jump to this addr */ +#define CONFIG_RUN_ADDR USER_CODE_FLASH #define VEND_ID0 0xAF #define VEND_ID1 0x1E diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index 06c9a74..e2b9788 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -36,18 +36,15 @@ #define NUM_ALT_SETTINGS 3 #define STR_DESC_LEN 7 -//#define CONFIG_RUNAPP_ALT 0 -#define CONFIG_INFO_ALT - //No LED strobing whatsoever - saves space -//#define CONFIG_INHIBIT_STROBE +#define CONFIG_INHIBIT_STROBE -//Replace USB suspend/resume code with dummies. -//Saves a few bytes more +/* Replace USB suspend/resume code with dummies. */ +/* Saves a few bytes */ #define CONFIG_DISABLE_USB_SUSPEND -/* F*ck, that costed me an hour of lifetime to figure out! */ +/* THIS IS MCU-DEPENDANT! RTFM: Reference Manual */ #define FLASH_PAGE_SIZE 0x800 #define LED_BANK GPIOG @@ -63,8 +60,19 @@ #define USER_CODE_RAM ((u32) 0x20000C00) #define USER_CODE_FLASH ((u32) 0x08005000) -#define USER_INFO_FLASH ((u32) 0x08004000) +/* AltSetting Configuration + * Undefine those you do not need to strip + * down the size of the loader + */ + +#define CONFIG_ALSETTING_RAM 0 +#define CONFIG_ALSETTING_FLASH 1 +#define CONFIG_ALSETTING_RUN 2 +/* Any transfer to run altsetting will cause a jump to this addr */ +#define CONFIG_RUN_ADDR USER_CODE_FLASH + +/* VID&PID Settings */ #define VEND_ID0 0xAD #define VEND_ID1 0xDE #define PROD_ID0 0xDE @@ -79,7 +87,7 @@ bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); \ int delay_count =0; -#define bootloaderCondition (delay_count++ < BOOTLOADER_WAIT) +#define bootloaderCondition ((no_user_jump) || (delay_count++ < BOOTLOADER_WAIT)) /* define to 0 to never exit, undefine to save space */ //#define bootloaderExitCondition 0 diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index c289c71..b31d0de 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -35,9 +35,7 @@ #define NUM_ALT_SETTINGS 3 #define STR_DESC_LEN 7 -#define FLASH_PAGE_SIZE 0x800 -#define CONFIG_RUNAPP_ALT 0 //No LED strobing whatsoever - saves space #define CONFIG_INHIBIT_STROBE @@ -46,6 +44,9 @@ #define CONFIG_DISABLE_USB_SUSPEND +/* THIS IS MCU-DEPENDANT! RTFM: Reference Manual */ +#define FLASH_PAGE_SIZE 0x800 + #define LED_BANK GPIOA #define LED 5 #define BLINK_FAST 0x50000 @@ -59,8 +60,20 @@ #define USER_CODE_RAM ((u32) 0x20000C00) #define USER_CODE_FLASH ((u32) 0x08005000) -#define USER_INFO_FLASH ((us32) 0x08004000) + +/* AltSetting Configuration + * Undefine those you do not need to strip + * down the size of the loader + */ + +#define CONFIG_ALSETTING_RAM 0 +#define CONFIG_ALSETTING_FLASH 1 +#define CONFIG_ALSETTING_RUN 2 +/* Any transfer to run altsetting will cause a jump to this addr */ +#define CONFIG_RUN_ADDR USER_CODE_FLASH + +/* VID&PID Settings */ #define VEND_ID0 0xAD #define VEND_ID1 0xDE #define PROD_ID0 0xDE diff --git a/src/common/dfu_io.c b/src/common/dfu_io.c index 45f4a44..40a45c7 100644 --- a/src/common/dfu_io.c +++ b/src/common/dfu_io.c @@ -48,9 +48,8 @@ void dfuToFlashInit() #ifdef CONFIG_ALTSETTING_RUN void dfuToRunInit() { - boardTeardown(); - jumpToUser(USER_CODE_FLASH); + jumpToUser(CONFIG_RUN_ADDR); } #endif @@ -73,6 +72,7 @@ void dfuToFPGAInit() { /* TODO */ } + #endif /* TODO: FPGA,SPI,etc */ /* DFU ====> FPGA */ From 930ba94042c68525157c2393f8f181666c2976a0 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 01:25:13 +0400 Subject: [PATCH 30/66] Fix FLASH_PAGE_SIZE for maple, added MAPLE RET6 dummy. UNTESTED!!! Signed-off-by: Andrew 'Necromant' Andrianov --- boards/maple-ret6/Makefile | 1 + boards/maple-ret6/board.c | 98 ++++++++++++++ boards/maple-ret6/config.h | 88 +++++++++++++ boards/maple-ret6/usb_descriptor.c | 205 +++++++++++++++++++++++++++++ boards/maple/config.h | 2 +- 5 files changed, 393 insertions(+), 1 deletion(-) create mode 100644 boards/maple-ret6/Makefile create mode 100644 boards/maple-ret6/board.c create mode 100644 boards/maple-ret6/config.h create mode 100644 boards/maple-ret6/usb_descriptor.c diff --git a/boards/maple-ret6/Makefile b/boards/maple-ret6/Makefile new file mode 100644 index 0000000..0638a62 --- /dev/null +++ b/boards/maple-ret6/Makefile @@ -0,0 +1 @@ +objects-y+=board.o usb_descriptor.o diff --git a/boards/maple-ret6/board.c b/boards/maple-ret6/board.c new file mode 100644 index 0000000..6875551 --- /dev/null +++ b/boards/maple-ret6/board.c @@ -0,0 +1,98 @@ +#include "stm32f10x_type.h" +#include "cortexm3_macro.h" +#include "common.h" +#include "hardware.h" + + +static void setupLED (void) { + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup APB2 (GPIOA) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000004; + SET_REG(RCC_APB2ENR,rwmVal); + + /* Setup GPIOA Pin 5 as PP Out */ + SET_REG(GPIO_CRL(GPIOA), 0x00100000); + + rwmVal = GET_REG(GPIO_CRL(GPIOA)); + rwmVal &= 0xFF0FFFFF; + rwmVal |= 0x00100000; + SET_REG(GPIO_CRL(GPIOA),rwmVal); + setPin(GPIOA,5); +} + +static void setupCLK (void) { + /* enable HSE */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); + while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ + + /* enable flash prefetch buffer */ + SET_REG(FLASH_ACR, 0x00000012); + + /* Configure PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ + while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ + + /* Set SYSCLK as PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); + while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ +} + +static void setupBUTTON (void) { + // todo, swap out hardcoded pin/bank with macro + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup APB2 (GPIOC) */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000010; + SET_REG(RCC_APB2ENR,rwmVal); + + /* Setup GPIOC Pin 9 as PP Out */ + rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal &= 0xFFFFFF0F; + rwmVal |= 0x00000040; + SET_REG(GPIO_CRH(GPIOC),rwmVal); + +} + +static void inline setupUSB (void) { + u32 rwmVal; /* read-write-modify place holder var */ + + /* Setup the USB DISC Pin */ + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x4; + SET_REG(RCC_APB2ENR,rwmVal); + + // todo, macroize usb_disc pin + /* Setup GPIOC Pin 12 as OD out */ + rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal &= 0xFFF0FFFF; + rwmVal |= 0x00050000; + SET_REG(GPIO_CRH(GPIOC),rwmVal); + pRCC->APB1ENR |= 0x00800000; + + resetPin (GPIOC,12); + rwmVal = 10000000; + while (rwmVal--);; + /* initialize the usb application */ + setPin (GPIOC,12); /* present ourselves to the host */ +} + + +/* executed before actual jump to user code */ +void boardTeardown() +{ + setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin +} + +/* This is a common routine to setup the board */ +void boardInit() +{ + setupCLK(); + setupLED(); + setupBUTTON(); + setupUSB(); +} diff --git a/boards/maple-ret6/config.h b/boards/maple-ret6/config.h new file mode 100644 index 0000000..0e8ef6b --- /dev/null +++ b/boards/maple-ret6/config.h @@ -0,0 +1,88 @@ +/****************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ****************************************************************************/ + +/** + * @file config.h + * + * @brief bootloader settings and macro defines + * + * + */ + +#ifndef __CONFIG_H +#define __CONFIG_H + +//WTF WAS THAT DOING HERE?? +//#include "common.h" + +/* THIS IS MCU-DEPENDANT! RTFM: Reference Manual */ +#define FLASH_PAGE_SIZE 0x800 + +#define NUM_ALT_SETTINGS 2 +#define STR_DESC_LEN 6 + +//No LED strobing whatsoever - saves space +#define CONFIG_INHIBIT_STROBE + +#define LED_BANK GPIOA +#define LED 5 +#define BLINK_FAST 0x50000 +#define BLINK_SLOW 0x100000 + +#define BUTTON_BANK GPIOC + +#define BUTTON 9 + +#define STARTUP_BLINKS 5 +#define BOOTLOADER_WAIT 6 + + +//Enable informational altsetting +//#define CONFIG_INFO_ALT + +#define USER_CODE_RAM ((u32)0x20000C00) +#define USER_CODE_FLASH ((u32)0x08005000) + +#define CONFIG_ALSETTING_RAM 0 +#define CONFIG_ALSETTING_FLASH 1 +#define CONFIG_ALSETTING_RUN 2 +/* Any transfer to run altsetting will cause a jump to this addr */ +#define CONFIG_RUN_ADDR USER_CODE_FLASH + +#define VEND_ID0 0xAF +#define VEND_ID1 0x1E +#define PROD_ID0 0x03 +#define PROD_ID1 0x00 + +/* while this is '1' we're looping in the bootloader */ +#define CONFIG_EXTRA_MAIN_CODE \ + bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); \ + int delay_count =0; + +#define bootloaderCondition (delay_count++ < BOOTLOADER_WAIT) \ + || no_user_jump + +#define bootloaderExitCondition 0 + +#endif diff --git a/boards/maple-ret6/usb_descriptor.c b/boards/maple-ret6/usb_descriptor.c new file mode 100644 index 0000000..a55bbf5 --- /dev/null +++ b/boards/maple-ret6/usb_descriptor.c @@ -0,0 +1,205 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2010 LeafLabs LLC. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ****************************************************************************/ + + +/** + * @file usb_descriptor.c + * + * @brief aka application descriptor; big static struct and callbacks for sending + * the descriptor. + * + */ + + +#include "usb_descriptor.h" + +u8 u8_usbDeviceDescriptorDFU[18] = + { + 0x12, /* bLength */ + 0x01, /* bDescriptorType */ + 0x00, /* bcdUSB, version 1.00 */ + 0x01, + 0x00, /* bDeviceClass : See interface */ + 0x00, /* bDeviceSubClass : See interface*/ + 0x00, /* bDeviceProtocol : See interface */ + bMaxPacketSize, /* bMaxPacketSize0 0x40 = 64 */ + VEND_ID0, /* idVendor (0110) */ + VEND_ID1, + + PROD_ID0, /* idProduct (0x1001 or 1002) */ + PROD_ID1, + + 0x01, /* bcdDevice*/ + 0x02, + 0x01, /* iManufacturer : index of string Manufacturer */ + 0x02, /* iProduct : index of string descriptor of product*/ + 0x03, /* iSerialNumber : index of string serial number*/ + 0x01 /*bNumConfigurations */ + }; + +ONE_DESCRIPTOR usbDeviceDescriptorDFU = + { + u8_usbDeviceDescriptorDFU, + 0x12 + }; + +u8 u8_usbFunctionalDescriptor[9] = + { + /******************** DFU Functional Descriptor********************/ + 0x09, /*blength = 7 Bytes*/ + 0x21, /* DFU Functional Descriptor*/ + 0x01, /*bmAttribute, can only download for now */ + 0xFF, /*DetachTimeOut= 255 ms*/ + 0x00, + (wTransferSize & 0x00FF), + (wTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ + 0x10, /* bcdDFUVersion = 1.1 */ + 0x01 + }; + +ONE_DESCRIPTOR usbFunctionalDescriptor = + { + u8_usbFunctionalDescriptor, + 0x09 + }; + + +u8 u8_usbConfigDescriptorDFU[36] = + { + 0x09, /* bLength: Configuation Descriptor size */ + 0x02, /* bDescriptorType: Configuration */ + 0x24, /* wTotalLength: Bytes returned */ + 0x00, + 0x01, /* bNumInterfaces: 1 interface */ + 0x01, /* bConfigurationValue: */ + 0x00, /* iConfiguration: */ + 0x80, /* bmAttributes: */ + 0x32, /* MaxPower 100 mA */ + /* 09 */ + + /************ Descriptor of DFU interface 0 Alternate setting 0 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x00, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + + 0x04, /* iInterface: */ + + /************ Descriptor of DFU interface 0 Alternate setting 1 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x01, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + + 0x05, /* iInterface: */ + + /******************** DFU Functional Descriptor********************/ + 0x09, /*blength = 7 Bytes*/ + 0x21, /* DFU Functional Descriptor*/ + 0x01, /*bmAttribute, can only download for now */ + 0xFF, /*DetachTimeOut= 255 ms*/ + 0x00, + (wTransferSize & 0x00FF), + (wTransferSize & 0xFF00) >> 8, /* TransferSize = 1024 Byte*/ + 0x10, /* bcdDFUVersion = 1.1 */ + 0x01 + /***********************************************************/ + /*36*/ + }; + +ONE_DESCRIPTOR usbConfigDescriptorDFU = + { + u8_usbConfigDescriptorDFU, + 0x24 + }; + +u8 u8_usbStringLangId[0x04] = + { + 0x04, + 0x03, + 0x09, + 0x04 /* LangID = 0x0409: U.S. English */ + }; + +u8 u8_usbStringVendor[0x12] = + { + 0x12, + 0x03, + 'L',0,'e',0,'a',0,'f',0,'L',0,'a',0,'b',0,'s',0 + }; + +u8 u8_usbStringProduct[0x14] = + { + 0x14, + 0x03, + 'M',0,'a',0,'p',0,'l',0,'e',0,' ',0,'0',0,'0',0,'3',0 + }; + +u8 u8_usbStringSerial[0x10] = + { + 0x10, + 0x03, + 'L',0,'L',0,'M',0,' ',0,'0',0,'0',0,'3',0 + }; + +u8 u8_usbStringAlt0[0x36] = + { + 0x36, + 0x03, + 'D',0,'F',0,'U',0,' ',0,'P',0,'r',0,'o',0,'g',0,'r',0, + 'a',0,'m',0,' ',0,'R',0,'A',0,'M',0,' ',0,'0',0,'x',0, + '2',0,'0',0,'0',0,'0',0,'0',0,'C',0,'0',0,'0',0 + }; + +u8 u8_usbStringAlt1[0x3A] = + { + 0x3A, + 0x03, + 'D',0,'F',0,'U',0,' ',0,'P',0,'r',0,'o',0,'g',0,'r',0, + 'a',0,'m',0,' ',0,'F',0,'L',0,'A',0,'S',0,'H',0,' ',0, + '0',0,'x',0,'0',0,'8',0,'0',0,'0',0,'5',0,'0',0,'0',0, + '0',0 + }; + +u8 u8_usbStringInterface = 0; + +ONE_DESCRIPTOR usbStringDescriptor[6] = + { + { (u8*)u8_usbStringLangId, 0x04 }, + { (u8*)u8_usbStringVendor, 0x12 }, + { (u8*)u8_usbStringProduct, 0x20 }, + { (u8*)u8_usbStringSerial, 0x10 }, + { (u8*)u8_usbStringAlt0, 0x36 }, + { (u8*)u8_usbStringAlt1, 0x3A } + }; diff --git a/boards/maple/config.h b/boards/maple/config.h index 0e8ef6b..f4898a0 100644 --- a/boards/maple/config.h +++ b/boards/maple/config.h @@ -37,7 +37,7 @@ //#include "common.h" /* THIS IS MCU-DEPENDANT! RTFM: Reference Manual */ -#define FLASH_PAGE_SIZE 0x800 +#define FLASH_PAGE_SIZE 0x400 #define NUM_ALT_SETTINGS 2 #define STR_DESC_LEN 6 From c8a53e0ee223e81dd63222ef752ee9af8cee3b5c Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 14:04:59 +0400 Subject: [PATCH 31/66] Fix typo in configs Signed-off-by: Andrew 'Necromant' Andrianov --- boards/maple-ret6/config.h | 6 +++--- boards/maple/config.h | 6 +++--- boards/mcortex/config.h | 6 +++--- boards/te-stm32f103ret6kit/config.h | 8 ++++---- 4 files changed, 13 insertions(+), 13 deletions(-) diff --git a/boards/maple-ret6/config.h b/boards/maple-ret6/config.h index 0e8ef6b..003309f 100644 --- a/boards/maple-ret6/config.h +++ b/boards/maple-ret6/config.h @@ -64,9 +64,9 @@ #define USER_CODE_RAM ((u32)0x20000C00) #define USER_CODE_FLASH ((u32)0x08005000) -#define CONFIG_ALSETTING_RAM 0 -#define CONFIG_ALSETTING_FLASH 1 -#define CONFIG_ALSETTING_RUN 2 +#define CONFIG_ALTSETTING_RAM 0 +#define CONFIG_ALTSETTING_FLASH 1 +#define CONFIG_ALTSETTING_RUN 2 /* Any transfer to run altsetting will cause a jump to this addr */ #define CONFIG_RUN_ADDR USER_CODE_FLASH diff --git a/boards/maple/config.h b/boards/maple/config.h index f4898a0..303d692 100644 --- a/boards/maple/config.h +++ b/boards/maple/config.h @@ -64,9 +64,9 @@ #define USER_CODE_RAM ((u32)0x20000C00) #define USER_CODE_FLASH ((u32)0x08005000) -#define CONFIG_ALSETTING_RAM 0 -#define CONFIG_ALSETTING_FLASH 1 -#define CONFIG_ALSETTING_RUN 2 +#define CONFIG_ALTSETTING_RAM 0 +#define CONFIG_ALTSETTING_FLASH 1 +#define CONFIG_ALTSETTING_RUN 2 /* Any transfer to run altsetting will cause a jump to this addr */ #define CONFIG_RUN_ADDR USER_CODE_FLASH diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index e2b9788..20d0cb4 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -66,9 +66,9 @@ * down the size of the loader */ -#define CONFIG_ALSETTING_RAM 0 -#define CONFIG_ALSETTING_FLASH 1 -#define CONFIG_ALSETTING_RUN 2 +#define CONFIG_ALTSETTING_RAM 0 +#define CONFIG_ALTSETTING_FLASH 1 +#define CONFIG_ALTSETTING_RUN 2 /* Any transfer to run altsetting will cause a jump to this addr */ #define CONFIG_RUN_ADDR USER_CODE_FLASH diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index b31d0de..63c5020 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -37,7 +37,7 @@ #define STR_DESC_LEN 7 //No LED strobing whatsoever - saves space -#define CONFIG_INHIBIT_STROBE +//#define CONFIG_INHIBIT_STROBE //Replace USB suspend/resume code with dummies. //Saves a few bytes more @@ -67,9 +67,9 @@ * down the size of the loader */ -#define CONFIG_ALSETTING_RAM 0 -#define CONFIG_ALSETTING_FLASH 1 -#define CONFIG_ALSETTING_RUN 2 +#define CONFIG_ALTSETTING_RAM 0 +#define CONFIG_ALTSETTING_FLASH 1 +#define CONFIG_ALTSETTING_RUN 2 /* Any transfer to run altsetting will cause a jump to this addr */ #define CONFIG_RUN_ADDR USER_CODE_FLASH From ab411c083c5dc36cc5568b6273d3edbe3933427e Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 14:05:11 +0400 Subject: [PATCH 32/66] Some changes to dfu machine dfu: fixed errors in dfu_io dfu: Allow multiple downloads Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu.c | 12 +++++++----- src/common/dfu_io.c | 6 ++---- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/common/dfu.c b/src/common/dfu.c index 6fa59b9..36a59de 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -59,14 +59,16 @@ void dfuInit(void) { dfuBusy=FALSE; } -/* Hell yeah */ -#include "dfu_io.c" #define MAP_ALTSETTING_INIT(alt,func) \ - if ((pInformation->Current_AlternateSetting == alt) func(); + if (pInformation->Current_AlternateSetting == alt) func(); + void (*dfuCopyFunc)(void); +/* Hell yeah */ +#include "dfu_io.c" + bool dfuUpdateByRequest(void) { /* were using the global pInformation struct from usb_lib here, see comment in maple_dfu.h around DFUEvent struct */ @@ -210,8 +212,8 @@ bool dfuUpdateByRequest(void) { usb reset or power on reset to run the new code */ /* consider timing out and self-resetting */ - dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; - + //dfuAppStatus.bState = dfuMANIFEST_WAIT_RESET; + dfuAppStatus.bState = dfuIDLE; } else if (startState == dfuUPLOAD_IDLE) { /* device expecting further dfu_upload requests */ diff --git a/src/common/dfu_io.c b/src/common/dfu_io.c index 40a45c7..c21719a 100644 --- a/src/common/dfu_io.c +++ b/src/common/dfu_io.c @@ -18,7 +18,7 @@ void dfuToRamInit() { dfuCopyFunc = dfuToRamCopy; userAppAddr = USER_CODE_RAM; - userFlash = FALSE; + strobePin(LED_BANK,LED,5,BLINK_FAST); } #endif @@ -38,8 +38,7 @@ void dfuToFlashCopy() void dfuToFlashInit() { userAppAddr = USER_CODE_FLASH; - userFlash = TRUE; - dfuCopyFunc = dfuToFlashCopy(); + dfuCopyFunc = dfuToFlashCopy; setupFLASH(); flashUnlock(); } @@ -48,7 +47,6 @@ void dfuToFlashInit() #ifdef CONFIG_ALTSETTING_RUN void dfuToRunInit() { - boardTeardown(); jumpToUser(CONFIG_RUN_ADDR); } #endif From 24d90c1aff7c5645a463207207b9f05d7069e2e8 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 14:14:06 +0400 Subject: [PATCH 33/66] Fix README Signed-off-by: Andrew 'Necromant' Andrianov --- README | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/README b/README index be36dec..af35d05 100644 --- a/README +++ b/README @@ -9,3 +9,22 @@ To see what's actually happening - run with V=y, e.g. CROSS_COMPILE=arm-none-eabi- make V=y This will dump a lot of info for you. Have fun. + +WHY? +---- +I needed a bootloader for my custom boards. Upstream maple was +shitty in places. So I cleaned up the mess + +WHAT'S THE DIFFERENCE, COMPARED TO MAPLE's UPSTREAM ONE? +-------------------------------------------------------- +Easy to add new boards +Configurable FLASH_PAGE_SIZE per-board +Configurable bootloader enter and leave conditions. per-board. +Configurable descriptors. per-board. +Extra options to save space +Batch-compile for all boards. +Pretty colored compilation output. +Configurable altsettings, easy to add new download targets +Multiple downloads in one run support.4 +New altsetting: download-to-run +Compiles with -Os, and works when compiled like that From 7e1e72f5da5972c6e572a64e8d819dd128d435d6 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 14:21:26 +0400 Subject: [PATCH 34/66] Add a small developer README Signed-off-by: Andrew 'Necromant' Andrianov --- README.dev | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 README.dev diff --git a/README.dev b/README.dev new file mode 100644 index 0000000..16fd91d --- /dev/null +++ b/README.dev @@ -0,0 +1,22 @@ +These are misc developer hints. + +Adding new io targets. +---------------------- +The stock maple booloader supported only 2 targets. RAM and FLASH. +However, in my case I needed to add more targets, and made it easy to add new +ones. + + +You have to do the following. + +* In dfu_io.c (located under src/common) + implement your dfuToSomewhereInit function + It should set dfuCopyFunc to your dfuToSomewhereCopy implementation, located just here. + DO wrap it in CONFIG_ALSETTING_SOMEWHERE #ifdef to save space for the others. + +* In dfu.c, in dfuUpdateByRequest add the MAP_ALSETTING_INIT macro, wrapped it #ifdefs + that will call your init function, when a query arrives to the specified altsetting. + +* Test it! + +* Send the patches From 4eb9a60e1e69af10e113ec5e797c7cc69213a74a Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 14:42:39 +0400 Subject: [PATCH 35/66] Initial stubs for dfu upload functionality Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu.c | 10 ++++++++++ src/common/dfu_io.c | 18 ++++++++++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/common/dfu.c b/src/common/dfu.c index 36a59de..d0e2f40 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -65,6 +65,9 @@ void dfuInit(void) { void (*dfuCopyFunc)(void); +#ifdef CONFIG_UPLOAD +void (*dfuUploadFunc)(void); +#endif /* Hell yeah */ #include "dfu_io.c" @@ -321,6 +324,13 @@ u8* dfuCopyDNLOAD(u16 length) { u8* dfuCopyUPLOAD(u16 length) { /* not implemented here nor supported by dfu-util */ + if (length==0) { + pInformation->Ctrl_Info.Usb_wLength = pInformation->USBwLengths.w - pInformation->Ctrl_Info.Usb_wOffset; + thisBlockLen = pInformation->USBwLengths.w; + return NULL; + } else { + return ((u8*)recvBuffer + pInformation->Ctrl_Info.Usb_wOffset); + } return NULL; } diff --git a/src/common/dfu_io.c b/src/common/dfu_io.c index c21719a..8ce47d9 100644 --- a/src/common/dfu_io.c +++ b/src/common/dfu_io.c @@ -14,11 +14,29 @@ void dfuToRamCopy() } } +#ifdef CONFIG_UPLOAD + +void dfuFromRamCopy() +{ + int i; + u32* userSpace = (u32*)(USER_CODE_RAM+userFirmwareLen); + /* we dont need to handle when thisBlock len is not divisible by 4, + since the linker will align everything to 4B anyway */ + for (i=0;i Date: Tue, 5 Jun 2012 20:51:02 +0400 Subject: [PATCH 36/66] Do an NVIC_SetVectorTable before jump Signed-off-by: Andrew 'Necromant' Andrianov --- boards/te-stm32f103ret6kit/config.h | 2 +- src/common/hardware.c | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index 63c5020..9ea3817 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -59,7 +59,7 @@ #define BOOTLOADER_WAIT 6 #define USER_CODE_RAM ((u32) 0x20000C00) -#define USER_CODE_FLASH ((u32) 0x08005000) +#define USER_CODE_FLASH ((u32) 0x08003000) /* AltSetting Configuration diff --git a/src/common/hardware.c b/src/common/hardware.c index ce63d82..3517f37 100644 --- a/src/common/hardware.c +++ b/src/common/hardware.c @@ -115,6 +115,7 @@ void jumpToUser (u32 usrAddr) { flashLock(); usbDsbISR(); nvicDisableInterrupts(); + SET_REG(SCB_VTOR,usrAddr); boardTeardown(); systemReset(); // resets clocks and periphs, not core regs From d244d912275bf4711c3f18adbb8b6518c40bc7fa Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 21:38:37 +0400 Subject: [PATCH 37/66] Add xilinx sscu driver No example usages, yet Signed-off-by: Andrew 'Necromant' Andrianov --- boards/mcortex/config.h | 2 +- src/common/Makefile | 3 ++- src/common/xsscu.c | 47 +++++++++++++++++++++++++++++++++++++++++ src/common/xsscu.h | 21 ++++++++++++++++++ 4 files changed, 71 insertions(+), 2 deletions(-) create mode 100644 src/common/xsscu.c create mode 100644 src/common/xsscu.h diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index 20d0cb4..dd3a78b 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -37,7 +37,7 @@ #define STR_DESC_LEN 7 //No LED strobing whatsoever - saves space -#define CONFIG_INHIBIT_STROBE +//#define CONFIG_INHIBIT_STROBE /* Replace USB suspend/resume code with dummies. */ /* Saves a few bytes */ diff --git a/src/common/Makefile b/src/common/Makefile index 158bb4f..f8eeffa 100644 --- a/src/common/Makefile +++ b/src/common/Makefile @@ -3,5 +3,6 @@ objects-common += dfu.o \ hardware.o \ main.o \ usb.o \ - usb_callbacks.o + usb_callbacks.o \ + xsscu.o diff --git a/src/common/xsscu.c b/src/common/xsscu.c new file mode 100644 index 0000000..f49d23c --- /dev/null +++ b/src/common/xsscu.c @@ -0,0 +1,47 @@ +#include "xsscu.h" + +int xsscu_reset(const struct xsscu_unit* x) +{ + int i = 500; + x->clk_ctl(0); + x->sout_ctl(0); + x->progb_ctl(0); + x->delay(x->delay_pb); + x->progb_ctl(1); //progb + x->delay(x->delay_pb); + while (i--) { + if (x->get_initb()) //initb + return 0; + x->delay(x->delay_clk); + } + return 1; +} + +int xsscu_finalize(const struct xsscu_unit* x, int c) +{ + while (c--) { + x->clk_ctl(0); + x->delay(x->delay_clk); + x->clk_ctl(1); + x->delay(x->delay_clk); + if (x->get_done()) + return 0; + } + return 1; +} + +void xsscu_write(const struct xsscu_unit* x, const unsigned char* fw, int fw_size) +{ + int i=0; + int k; + while (i < fw_size) { + for (k = 7; k >= 0; k--) { + x->sout_ctl(fw[i] & (1 << k)); + x->clk_ctl(1); + x->delay(x->delay_clk); + x->clk_ctl(0); + x->delay(x->delay_clk); + } + i++; + } +} diff --git a/src/common/xsscu.h b/src/common/xsscu.h new file mode 100644 index 0000000..6d79d81 --- /dev/null +++ b/src/common/xsscu.h @@ -0,0 +1,21 @@ +#ifndef XSSCU_H +#define XSSCU_H + +struct xsscu_unit { + char* name; + void (*clk_ctl)(char n); + void (*sout_ctl)(char n); + void (*progb_ctl)(char n); + char (*get_initb)(void); + char (*get_done)(void); + void (*delay)(int n); + int delay_pb; + int delay_clk; +} ; + +int xsscu_reset(const struct xsscu_unit* x); +int xsscu_finalize(const struct xsscu_unit* x, int c); +void xsscu_write(const struct xsscu_unit* x, const unsigned char* fw, int fw_size); + + +#endif \ No newline at end of file From 0d71686bdcb1e935cf5cd4139f41a28ed7f1118c Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 21:43:48 +0400 Subject: [PATCH 38/66] Added licencse stuff to xsscu headers. Let it be MIT Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/xsscu.c | 24 ++++++++++++++++++++++++ src/common/xsscu.h | 27 ++++++++++++++++++++++++++- 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/common/xsscu.c b/src/common/xsscu.c index f49d23c..5cac194 100644 --- a/src/common/xsscu.c +++ b/src/common/xsscu.c @@ -1,3 +1,27 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2012 Andrew 'Necromant' Andrianov + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ****************************************************************************/ + #include "xsscu.h" int xsscu_reset(const struct xsscu_unit* x) diff --git a/src/common/xsscu.h b/src/common/xsscu.h index 6d79d81..d28de23 100644 --- a/src/common/xsscu.h +++ b/src/common/xsscu.h @@ -1,3 +1,28 @@ +/* ***************************************************************************** + * The MIT License + * + * Copyright (c) 2012 Andrew 'Necromant' Andrianov + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + * ****************************************************************************/ + + #ifndef XSSCU_H #define XSSCU_H @@ -18,4 +43,4 @@ int xsscu_finalize(const struct xsscu_unit* x, int c); void xsscu_write(const struct xsscu_unit* x, const unsigned char* fw, int fw_size); -#endif \ No newline at end of file +#endif From 5c900d59647e86af92d751ae2af119f064b29061 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 22:50:02 +0400 Subject: [PATCH 39/66] Some quite critical fixes Fix flash corruption on copy Add blinks before jump Fix formatting of hardware.c Signed-off-by: Andrew 'Necromant' Andrianov --- boards/mcortex/config.h | 2 +- src/common/dfu_io.c | 7 ++ src/common/hardware.c | 273 ++++++++++++++++++++-------------------- 3 files changed, 145 insertions(+), 137 deletions(-) diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index dd3a78b..a75b301 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -56,7 +56,7 @@ #define BUTTON 9 #define STARTUP_BLINKS 5 -#define BOOTLOADER_WAIT 16 +#define BOOTLOADER_WAIT 5 #define USER_CODE_RAM ((u32) 0x20000C00) #define USER_CODE_FLASH ((u32) 0x08005000) diff --git a/src/common/dfu_io.c b/src/common/dfu_io.c index 8ce47d9..3f71fe2 100644 --- a/src/common/dfu_io.c +++ b/src/common/dfu_io.c @@ -1,4 +1,6 @@ + + /* This file will be just sucked in dfu.c for the sake of simplicity */ #ifdef CONFIG_ALTSETTING_RAM @@ -12,6 +14,8 @@ void dfuToRamCopy() for (i=0;i0) { - for (c=rate;c>0;c--) { - asm volatile ("nop"); - } - setPin(bank,pin); - for (c=rate;c>0;c--) { - asm volatile ("nop"); - } - resetPin(bank,pin); - } + resetPin(bank,pin); + + u32 c; + while (count-- >0) { + for (c=rate;c>0;c--) { + asm volatile ("nop"); + } + setPin(bank,pin); + for (c=rate;c>0;c--) { + asm volatile ("nop"); + } + resetPin(bank,pin); + } } #endif void systemReset(void) { - SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00000001); - SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xF8FF0000); - SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFEF6FFFF); - SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFFFBFFFF); - SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xFF80FFFF); + SET_REG(RCC_CR, GET_REG(RCC_CR) | 0x00000001); + SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xF8FF0000); + SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFEF6FFFF); + SET_REG(RCC_CR, GET_REG(RCC_CR) & 0xFFFBFFFF); + SET_REG(RCC_CFGR, GET_REG(RCC_CFGR) & 0xFF80FFFF); - SET_REG(RCC_CIR, 0x00000000); /* disable all RCC interrupts */ + SET_REG(RCC_CIR, 0x00000000); /* disable all RCC interrupts */ } void setupFLASH() { - /* configure the HSI oscillator */ - if ((pRCC->CR & 0x01) == 0x00) { - u32 rwmVal = pRCC->CR; - rwmVal |= 0x01; - pRCC->CR = rwmVal; - } + /* configure the HSI oscillator */ + if ((pRCC->CR & 0x01) == 0x00) { + u32 rwmVal = pRCC->CR; + rwmVal |= 0x01; + pRCC->CR = rwmVal; + } - /* wait for it to come on */ - while ((pRCC->CR & 0x02) == 0x00) {} + /* wait for it to come on */ + while ((pRCC->CR & 0x02) == 0x00) {} } bool checkUserCode (u32 usrAddr) { - u32 sp = *(vu32*) usrAddr; + u32 sp = *(vu32*) usrAddr; - if ((sp & 0x2FFE0000) == 0x20000000) { - return (TRUE); - } else { - return (FALSE); - } + if ((sp & 0x2FFE0000) == 0x20000000) { + return (TRUE); + } else { + return (FALSE); + } } void jumpToUser (u32 usrAddr) { - typedef void (*funcPtr)(void); + strobePin(LED_BANK,LED,5,BLINK_FAST); + typedef void (*funcPtr)(void); - u32 jumpAddr = *(vu32*) (usrAddr + 0x04); /* reset ptr in vector table */ - funcPtr usrMain = (funcPtr) jumpAddr; + u32 jumpAddr = *(vu32*) (usrAddr + 0x04); /* reset ptr in vector table */ + funcPtr usrMain = (funcPtr) jumpAddr; - /* tear down all the dfu related setup */ - // disable usb interrupts, clear them, turn off usb, set the disc pin - // todo pick exactly what we want to do here, now its just a conservative - flashLock(); - usbDsbISR(); - nvicDisableInterrupts(); - SET_REG(SCB_VTOR,usrAddr); - boardTeardown(); - systemReset(); // resets clocks and periphs, not core regs + /* tear down all the dfu related setup */ + // disable usb interrupts, clear them, turn off usb, set the disc pin + // todo pick exactly what we want to do here, now its just a conservative + flashLock(); + usbDsbISR(); + nvicDisableInterrupts(); + SET_REG(SCB_VTOR,usrAddr); + boardTeardown(); + systemReset(); // resets clocks and periphs, not core regs - __MSR_MSP(*(vu32*) usrAddr); /* set the users stack ptr */ + __MSR_MSP(*(vu32*) usrAddr); /* set the users stack ptr */ - usrMain(); /* go! */ + usrMain(); /* go! */ } void nvicInit(NVIC_InitTypeDef* NVIC_InitStruct) { - u32 tmppriority = 0x00; - u32 tmpreg = 0x00; - u32 tmpmask = 0x00; - u32 tmppre = 0; - u32 tmpsub = 0x0F; + u32 tmppriority = 0x00; + u32 tmpreg = 0x00; + u32 tmpmask = 0x00; + u32 tmppre = 0; + u32 tmpsub = 0x0F; - SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE; - NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE; + SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE; + NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE; - /* Compute the Corresponding IRQ Priority --------------------------------*/ - tmppriority = (0x700 - (rSCB->AIRCR & (u32)0x700))>> 0x08; - tmppre = (0x4 - tmppriority); - tmpsub = tmpsub >> tmppriority; + /* Compute the Corresponding IRQ Priority --------------------------------*/ + tmppriority = (0x700 - (rSCB->AIRCR & (u32)0x700))>> 0x08; + tmppre = (0x4 - tmppriority); + tmpsub = tmpsub >> tmppriority; - tmppriority = (u32)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; - tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; + tmppriority = (u32)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; + tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; - tmppriority = tmppriority << 0x04; - tmppriority = ((u32)tmppriority) << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); + tmppriority = tmppriority << 0x04; + tmppriority = ((u32)tmppriority) << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); - tmpreg = rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)]; - tmpmask = (u32)0xFF << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); - tmpreg &= ~tmpmask; - tmppriority &= tmpmask; - tmpreg |= tmppriority; + tmpreg = rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)]; + tmpmask = (u32)0xFF << ((NVIC_InitStruct->NVIC_IRQChannel & (u8)0x03) * 0x08); + tmpreg &= ~tmpmask; + tmppriority &= tmpmask; + tmpreg |= tmppriority; - rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)] = tmpreg; + rNVIC->IPR[(NVIC_InitStruct->NVIC_IRQChannel >> 0x02)] = tmpreg; - /* Enable the Selected IRQ Channels --------------------------------------*/ - rNVIC->ISER[(NVIC_InitStruct->NVIC_IRQChannel >> 0x05)] = - (u32)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (u8)0x1F); + /* Enable the Selected IRQ Channels --------------------------------------*/ + rNVIC->ISER[(NVIC_InitStruct->NVIC_IRQChannel >> 0x05)] = + (u32)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (u8)0x1F); } void nvicDisableInterrupts() { - NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE; - rNVIC->ICER[0] = 0xFFFFFFFF; - rNVIC->ICER[1] = 0xFFFFFFFF; - rNVIC->ICPR[0] = 0xFFFFFFFF; - rNVIC->ICPR[1] = 0xFFFFFFFF; + NVIC_TypeDef* rNVIC = (NVIC_TypeDef *) NVIC_BASE; + rNVIC->ICER[0] = 0xFFFFFFFF; + rNVIC->ICER[1] = 0xFFFFFFFF; + rNVIC->ICPR[0] = 0xFFFFFFFF; + rNVIC->ICPR[1] = 0xFFFFFFFF; - SET_REG(STK_CTRL,0x04); /* disable the systick, which operates separately from nvic */ + SET_REG(STK_CTRL,0x04); /* disable the systick, which operates separately from nvic */ } void systemHardReset(void) { - SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE; + SCB_TypeDef* rSCB = (SCB_TypeDef *) SCB_BASE; - /* Reset */ - rSCB->AIRCR = (u32)AIRCR_RESET_REQ; + /* Reset */ + rSCB->AIRCR = (u32)AIRCR_RESET_REQ; - /* should never get here */ - while (1) { - asm volatile("nop"); - } + /* should never get here */ + while (1) { + asm volatile("nop"); + } } bool flashErasePage(u32 pageAddr) { - u32 rwmVal = GET_REG(FLASH_CR); - rwmVal = FLASH_CR_PER; - SET_REG(FLASH_CR,rwmVal); + u32 rwmVal = GET_REG(FLASH_CR); + rwmVal = FLASH_CR_PER; + SET_REG(FLASH_CR,rwmVal); - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - SET_REG(FLASH_AR,pageAddr); - SET_REG(FLASH_CR,FLASH_CR_START | FLASH_CR_PER); - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + SET_REG(FLASH_AR,pageAddr); + SET_REG(FLASH_CR,FLASH_CR_START | FLASH_CR_PER); + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - /* todo: verify the page was erased */ + /* todo: verify the page was erased */ - rwmVal = 0x00; - SET_REG(FLASH_CR,rwmVal); + rwmVal = 0x00; + SET_REG(FLASH_CR,rwmVal); - return TRUE; + return TRUE; } bool flashErasePages(u32 pageAddr, u16 n) { - while (n-->0) { - if (!flashErasePage(pageAddr*FLASH_PAGE_SIZE*n)) { - return FALSE; - } - } + while (n-->0) { + if (!flashErasePage(pageAddr*FLASH_PAGE_SIZE*n)) { + return FALSE; + } + } - return TRUE; + return TRUE; } bool flashWriteWord(u32 addr, u32 word) { - vu16 *flashAddr = (vu16*)addr; - vu32 lhWord = (vu32)word & 0x0000FFFF; - vu32 hhWord = ((vu32)word & 0xFFFF0000)>>16; + vu16 *flashAddr = (vu16*)addr; + vu32 lhWord = (vu32)word & 0x0000FFFF; + vu32 hhWord = ((vu32)word & 0xFFFF0000)>>16; - u32 rwmVal = GET_REG(FLASH_CR); - SET_REG(FLASH_CR,FLASH_CR_PG); + u32 rwmVal = GET_REG(FLASH_CR); + SET_REG(FLASH_CR,FLASH_CR_PG); - /* apparently we need not write to FLASH_AR and can - simply do a native write of a half word */ - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - *(flashAddr+0x01) = (vu16)hhWord; - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - *(flashAddr) = (vu16)lhWord; - while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + /* apparently we need not write to FLASH_AR and can + simply do a native write of a half word */ + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + *(flashAddr+0x01) = (vu16)hhWord; + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} + *(flashAddr) = (vu16)lhWord; + while (GET_REG(FLASH_SR) & FLASH_SR_BSY) {} - rwmVal &= 0xFFFFFFFE; - SET_REG(FLASH_CR,rwmVal); + rwmVal &= 0xFFFFFFFE; + SET_REG(FLASH_CR,rwmVal); - /* verify the write */ - if (*(vu32*)addr != word) { - return FALSE; - } + /* verify the write */ + if (*(vu32*)addr != word) { + return FALSE; + } - return TRUE; + return TRUE; } void flashLock() { - /* take down the HSI oscillator? it may be in use elsewhere */ + /* take down the HSI oscillator? it may be in use elsewhere */ - /* ensure all FPEC functions disabled and lock the FPEC */ - SET_REG(FLASH_CR,0x00000080); + /* ensure all FPEC functions disabled and lock the FPEC */ + SET_REG(FLASH_CR,0x00000080); } void flashUnlock() { - /* unlock the flash */ - SET_REG(FLASH_KEYR,FLASH_KEY1); - SET_REG(FLASH_KEYR,FLASH_KEY2); + /* unlock the flash */ + SET_REG(FLASH_KEYR,FLASH_KEY1); + SET_REG(FLASH_KEYR,FLASH_KEY2); } From 4f68f61480f49f8c1151e81eddd2f61c372164a4 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Tue, 5 Jun 2012 23:09:46 +0400 Subject: [PATCH 40/66] Extra special commit with steroids and coffeine dfu now works 10 times faster! Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/dfu.c b/src/common/dfu.c index d0e2f40..dc5fec5 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -132,8 +132,8 @@ bool dfuUpdateByRequest(void) { /* todo, add routine to wait for last block write to finish */ if (code_copy_lock==WAIT) { code_copy_lock=BEGINNING; - dfuAppStatus.bwPollTimeout0 = 0xFF; /* is this enough? */ - dfuAppStatus.bwPollTimeout1 = 0x01; /* is this enough? */ + dfuAppStatus.bwPollTimeout0 = 0x50; /* is this enough? */ + dfuAppStatus.bwPollTimeout1 = 0x00; /* is this enough? */ dfuAppStatus.bState=dfuDNBUSY; } else if (code_copy_lock==BEGINNING) { From ff8ea6b89485e394672d9c2f08deae9769f2b835 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Wed, 6 Jun 2012 00:41:51 +0400 Subject: [PATCH 41/66] mcortex: Fix USB disconnection handling Signed-off-by: Andrew 'Necromant' Andrianov --- boards/mcortex/board.c | 28 +++++++++++----------------- boards/mcortex/config.h | 4 ++-- 2 files changed, 13 insertions(+), 19 deletions(-) diff --git a/boards/mcortex/board.c b/boards/mcortex/board.c index f0a4f73..7460aba 100644 --- a/boards/mcortex/board.c +++ b/boards/mcortex/board.c @@ -4,7 +4,7 @@ #include "hardware.h" -void setupLED (void) { +inline void setupLED (void) { // todo, swap out hardcoded pin/bank with macro u32 rwmVal; /* read-write-modify place holder var */ @@ -23,7 +23,7 @@ void setupLED (void) { /* Motor Cortex uses 12Mhz quartz */ -void setupCLK (void) { +inline void setupCLK (void) { /* enable HSE */ SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ @@ -41,33 +41,27 @@ void setupCLK (void) { while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ } -void setupUSB (void) { - u32 rwmVal; /* read-write-modify place holder var */ - /* Setup the USB DISC Pin */ +/* We simulate disconnection by pulling PA12(USB) to the ground */ +/* This saves us a pin */ +inline void setupUSB (void) { + u32 rwmVal; /* read-write-modify place holder var */ rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; + rwmVal |= 0x0000004; SET_REG(RCC_APB2ENR,rwmVal); - - // todo, macroize usb_disc pin - /* Setup GPIOC Pin 12 as OD out */ rwmVal = GET_REG(GPIO_CRH(GPIOA)); - rwmVal &= 0xFFF0FFFF; - rwmVal |= 0x00050000; - setPin (GPIOA,12); + rwmVal &= 0xFFF00FFF; + rwmVal |= 0x00011000; SET_REG(GPIO_CRH(GPIOA),rwmVal); + resetPin (GPIOA,12); pRCC->APB1ENR |= 0x00800000; - /* initialize the usb application */ - resetPin (GPIOA,12); /* present ourselves to the host */ - u32 c = 120000000; - while (c--); } /* executed before actual jump to user code */ void boardTeardown() { - //resetPin(GPIOB,5); // disconnect usb from host. todo, macroize pin + } /* This is a common routine to setup the board */ diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index a75b301..ae64659 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -84,10 +84,10 @@ /* while this is '1' we're looping in the bootloader */ #define CONFIG_EXTRA_MAIN_CODE \ - bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); \ + bool have_code = (((checkUserCode(USER_CODE_FLASH)) || (checkUserCode(USER_CODE_RAM)))); \ int delay_count =0; -#define bootloaderCondition ((no_user_jump) || (delay_count++ < BOOTLOADER_WAIT)) +#define bootloaderCondition ((!have_code) || (delay_count++ < BOOTLOADER_WAIT)) /* define to 0 to never exit, undefine to save space */ //#define bootloaderExitCondition 0 From c9147d39126644f26a106872c7b876aec1a9a19a Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Wed, 6 Jun 2012 19:39:48 +0400 Subject: [PATCH 42/66] Added more porting info Signed-off-by: Andrew 'Necromant' Andrianov --- README.porting | 72 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 66 insertions(+), 6 deletions(-) diff --git a/README.porting b/README.porting index 82ecf90..7db5c43 100644 --- a/README.porting +++ b/README.porting @@ -23,21 +23,81 @@ CONFIG_INHIBIT_STROBE CONFIG_DISABLE_USB_SUSPEND Disables USB suspend/resume support and saves quite a few bytes -CONFIG_RUNAPP_ALT +FLASH_PAGE_SIZE + Set this according to the datasheet. You can also use stm32flash output + if lazy. Incorrect setting of this one will cause the data corruption. + +Altsettings. +The altsettings are configuread by defining the altsetting function +to the altsetting number. +e.g. CONFIG_ALTSETTING_RAM 0 +Currently 3 altsetting functions are supported: +CONFIG_ALTSETTING_RAM CONFIG_ALTSETTING_FLASH and CONFIG_ALTSETTING_RUN + +CONFIG_ALTSETTING_RAM - provides the ability to download code to RAM +CONFIG_ALTSETTING_FLASH - to flash +CONFIG_ALTSETTING_RUN !!!This is a hack, that breaks the dfu standart. Define this the to the altsetting number, e.g. 0, 1, etc. Trying to download anything to that altsetting will cause immediate jump to user code. dfu-util should exit due to device 'disconnect' - In this case you would normally want to tune the number of altsettings - to 3, to have a spare altsetting (or you can sacrifice RAM altsetting 0) + The jump will be performed to the addr defined in CONFIG_RUN_ADDR + +Undefining any of the above will remove the involved code, thus reducing +the loader size. + +N.B. Make sure you adjust usb string descriptors for altsettings to +reflect your config. + +BONUS! +For easy editing of string descriptors in emacs, you can use this lisp +sniplet: + +--------8<---------- +(defun th-usb-string-descriptor(text) + (interactive "sString, plz: ") + (insert "{\n") + (setq pos '2) + (loop for x in (string-to-list text) + do (insert (concat "'" (make-string 1 x) "', 0x0, ")) + (setq pos (+ 2 pos)) + ) + (insert " };") + (beginning-of-line) + (insert (format "0x%X,\n0x03,\n" pos)) +) +--------8<---------- + +just place it somewhere in your ~/.emacs, and use via +M-x th-usb-string-descriptor You can also adjust the USER_CODE_FLASH to some lower adress, depending -on the resulting loader size. Default maple settings waste a lot of -space. +on the resulting loader size. Default maple settings waste a lot of precious +code space. + +You can add any extra code to before the main loop via +CONFIG_EXTRA_MAIN_CODE +This is useful to implement custom rules whether to enter the +bootloader or not. + +Example: + +#define CONFIG_EXTRA_MAIN_CODE \ + bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); \ + int delay_count =0; + +There are also + +bootloaderCondition and bootloaderExitCondition defines. +The first one is used for the main loop, while it evaluates to 'TRUE' +we're in the bootloader mode. +The second one is the extra condition to break the loop. Undefine +to save space. + board.c has 2 functions that need to be implemented. boardInit() - This should init the board, any leds, buttons, clocks and -present the usb device to the host +present the device to host via enabling the pullup (If necessary) boardTeardown() - should disconnect the device from host for good. Any other code you want to run defore exiting loader goes here From f6411b986a8979f78aa293544627daa598617edf Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Wed, 6 Jun 2012 20:49:22 +0400 Subject: [PATCH 43/66] mcortex: Minor adjustments to board files Signed-off-by: Andrew 'Necromant' Andrianov --- boards/mcortex/config.h | 4 +-- boards/mcortex/usb_descriptor.c | 50 +++++++++++++++++++++++++++++---- 2 files changed, 47 insertions(+), 7 deletions(-) diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index ae64659..a7c0df7 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -33,8 +33,8 @@ #ifndef __CONFIG_H #define __CONFIG_H -#define NUM_ALT_SETTINGS 3 -#define STR_DESC_LEN 7 +#define NUM_ALT_SETTINGS 5 +#define STR_DESC_LEN 9 //No LED strobing whatsoever - saves space //#define CONFIG_INHIBIT_STROBE diff --git a/boards/mcortex/usb_descriptor.c b/boards/mcortex/usb_descriptor.c index 5061d79..54eee49 100644 --- a/boards/mcortex/usb_descriptor.c +++ b/boards/mcortex/usb_descriptor.c @@ -131,6 +131,29 @@ u8 u8_usbConfigDescriptorDFU[] = 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ 0x06, /* iInterface: */ + /************ Descriptor of DFU interface 0 Alternate setting 3 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x03, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + 0x07, /* iInterface: */ + + /************ Descriptor of DFU interface 0 Alternate setting 4 *********/ + 0x09, /* bLength: Interface Descriptor size */ + 0x04, /* bDescriptorType: */ + 0x00, /* bInterfaceNumber: Number of Interface */ + 0x04, /* bAlternateSetting: Alternate setting */ + 0x00, /* bNumEndpoints*/ + 0xFE, /* bInterfaceClass: DFU */ + 0x01, /* bInterfaceSubClass */ + 0x02, /* nInterfaceProtocol, switched to 0x02 while in dfu_mode */ + 0x08, /* iInterface: */ + + /******************** DFU Functional Descriptor********************/ 0x09, /*blength = 7 Bytes*/ 0x21, /* DFU Functional Descriptor*/ @@ -148,7 +171,7 @@ u8 u8_usbConfigDescriptorDFU[] = ONE_DESCRIPTOR usbConfigDescriptorDFU = { u8_usbConfigDescriptorDFU, - 0x24 + 0x48 }; u8 u8_usbStringLangId[0x04] = @@ -200,12 +223,26 @@ u8 u8_usbStringAlt1[] = 0x0, 'l', 0x0, 'a', 0x0, 's', 0x0, 'h', 0x0, }; -u8 u8_usbStringAlt2[] = -{ +u8 u8_usbStringAlt2[] = { + 0x16, + 0x03, + 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, + 'R', 0x0, 'U', 0x0, 'N', 0x0, +}; + +u8 u8_usbStringAlt3[] = { 0x18, 0x03, - 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, 'I', - 0x0, 'N', 0x0, 'F', 0x0, 'O', 0x0, + 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, + 'F', 0x0, 'P', 0x0, 'G', 0x0, 'A', 0x0, +}; + +u8 u8_usbStringAlt4[] = { + 0x22, + 0x03, + 'D', 0x0, 'F', 0x0, 'U', 0x0, ' ', 0x0, 't', 0x0, 'o', 0x0, ' ', 0x0, + 'S', 0x0, 'P', 0x0, 'I', 0x0, ' ', 0x0, 'F', 0x0, 'l', 0x0, 'a', 0x0, + 's', 0x0, 'h', 0x0, }; @@ -220,4 +257,7 @@ ONE_DESCRIPTOR usbStringDescriptor[] = { (u8*)u8_usbStringAlt0, 0x16 }, { (u8*)u8_usbStringAlt1, 0x1A }, { (u8*)u8_usbStringAlt2, 0x18 }, + { (u8*)u8_usbStringAlt3, 0x18 }, + { (u8*)u8_usbStringAlt4, 0x22 }, + }; From a737843b89a6b8a29ad9f84789c11ac67f2776f9 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Wed, 6 Jun 2012 20:57:38 +0400 Subject: [PATCH 44/66] mcortex: Fix USB descriptors to expose all 5 altsettings Signed-off-by: Andrew 'Necromant' Andrianov --- boards/mcortex/usb_descriptor.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/boards/mcortex/usb_descriptor.c b/boards/mcortex/usb_descriptor.c index 54eee49..ae13091 100644 --- a/boards/mcortex/usb_descriptor.c +++ b/boards/mcortex/usb_descriptor.c @@ -89,7 +89,7 @@ u8 u8_usbConfigDescriptorDFU[] = { 0x09, /* bLength: Configuation Descriptor size */ 0x02, /* bDescriptorType: Configuration */ - 0x24, /* wTotalLength: Bytes returned */ + 0x3f, /* wTotalLength: Bytes returned */ 0x00, 0x01, /* bNumInterfaces: 1 interface */ 0x01, /* bConfigurationValue: */ @@ -171,7 +171,7 @@ u8 u8_usbConfigDescriptorDFU[] = ONE_DESCRIPTOR usbConfigDescriptorDFU = { u8_usbConfigDescriptorDFU, - 0x48 + 0x3f }; u8 u8_usbStringLangId[0x04] = From 2b1cd689362695956b2fa18a1aebe834faeebfbd Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Wed, 6 Jun 2012 21:22:20 +0400 Subject: [PATCH 45/66] Remove loop from dfuFinishUpload, go on blinking after one download Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/common/dfu.c b/src/common/dfu.c index dc5fec5..48a725a 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -347,13 +347,11 @@ bool dfuUploadStarted() { } void dfuFinishUpload() { - while (1) { if (code_copy_lock == BEGINNING) { code_copy_lock=MIDDLE; dfuCopyFunc(); code_copy_lock = END; } - } /* otherwise do nothing, dfu state machine resets itself */ } From d6dea895ab704df25263b3bae641144f8b18a4f8 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Wed, 6 Jun 2012 21:23:06 +0400 Subject: [PATCH 46/66] Tuned timeouts a little more, should be a little bit faster Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/dfu.c b/src/common/dfu.c index 48a725a..0e9a488 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -132,7 +132,7 @@ bool dfuUpdateByRequest(void) { /* todo, add routine to wait for last block write to finish */ if (code_copy_lock==WAIT) { code_copy_lock=BEGINNING; - dfuAppStatus.bwPollTimeout0 = 0x50; /* is this enough? */ + dfuAppStatus.bwPollTimeout0 = 0x40; /* is this enough? */ dfuAppStatus.bwPollTimeout1 = 0x00; /* is this enough? */ dfuAppStatus.bState=dfuDNBUSY; From ba956a3412279a0a4e65f12eb107261bb6b36808 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Wed, 6 Jun 2012 21:34:01 +0400 Subject: [PATCH 47/66] Performance tuning. No strobe while download, tuned timeouts. Results: ~20KiloBytes/s when downloading to flash Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/common/dfu.c b/src/common/dfu.c index 0e9a488..918feaa 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -132,7 +132,7 @@ bool dfuUpdateByRequest(void) { /* todo, add routine to wait for last block write to finish */ if (code_copy_lock==WAIT) { code_copy_lock=BEGINNING; - dfuAppStatus.bwPollTimeout0 = 0x40; /* is this enough? */ + dfuAppStatus.bwPollTimeout0 = 0x30; /* is this enough? */ dfuAppStatus.bwPollTimeout1 = 0x00; /* is this enough? */ dfuAppStatus.bState=dfuDNBUSY; @@ -347,11 +347,14 @@ bool dfuUploadStarted() { } void dfuFinishUpload() { + while (dfuAppStatus.bState != dfuDNLOAD_IDLE) + { if (code_copy_lock == BEGINNING) { code_copy_lock=MIDDLE; dfuCopyFunc(); code_copy_lock = END; } + } /* otherwise do nothing, dfu state machine resets itself */ } From d725c1fcedd7589d5107eaf86cb3fdd05349cd90 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Wed, 6 Jun 2012 21:49:41 +0400 Subject: [PATCH 48/66] More performance tuning. Flash writing speed is now the bottleneck Implemented DUMMY altsetting for USB performance testing. Results: 490k in 8 secs => ~61Kilobyte/sec. Looks like the max we can get here. Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu.c | 6 +++++- src/common/dfu_io.c | 20 ++++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/src/common/dfu.c b/src/common/dfu.c index 918feaa..fafdb12 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -87,6 +87,10 @@ bool dfuUpdateByRequest(void) { if (pInformation->USBwLengths.w > 0) { userFirmwareLen = 0; dfuAppStatus.bState = dfuDNLOAD_SYNC; +#ifdef CONFIG_ALTSETTING_DUMMY + MAP_ALTSETTING_INIT(CONFIG_ALTSETTING_DUMMY,dfuToNowhereInit); +#endif + #ifdef CONFIG_ALTSETTING_RAM MAP_ALTSETTING_INIT(CONFIG_ALTSETTING_RAM,dfuToRamInit); #endif @@ -132,7 +136,7 @@ bool dfuUpdateByRequest(void) { /* todo, add routine to wait for last block write to finish */ if (code_copy_lock==WAIT) { code_copy_lock=BEGINNING; - dfuAppStatus.bwPollTimeout0 = 0x30; /* is this enough? */ + dfuAppStatus.bwPollTimeout0 = 0x20; /* is this enough? */ dfuAppStatus.bwPollTimeout1 = 0x00; /* is this enough? */ dfuAppStatus.bState=dfuDNBUSY; diff --git a/src/common/dfu_io.c b/src/common/dfu_io.c index 3f71fe2..6c817b2 100644 --- a/src/common/dfu_io.c +++ b/src/common/dfu_io.c @@ -76,6 +76,26 @@ void dfuToRunInit() } #endif + +/* This one copies the fw to nowhere, just for performance testing */ +#ifdef CONFIG_ALTSETTING_DUMMY + +void dfuToNowhereCopy() +{ + userFirmwareLen += thisBlockLen; + thisBlockLen = 0; + + /* just copied all the fw to astral */ +} + +void dfuToNowhereInit() +{ + userAppAddr = USER_CODE_FLASH; + dfuCopyFunc = dfuToNowhereCopy; +} + +#endif + #ifdef CONFIG_ALTSETTING_SPI void dfuToSPICopy() From d9655db9e5806b19eb11b30b86aa7b294fa11e9c Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Wed, 6 Jun 2012 21:55:23 +0400 Subject: [PATCH 49/66] Add benchmark results to README Signed-off-by: Andrew 'Necromant' Andrianov --- README | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README b/README index af35d05..6796027 100644 --- a/README +++ b/README @@ -28,3 +28,7 @@ Configurable altsettings, easy to add new download targets Multiple downloads in one run support.4 New altsetting: download-to-run Compiles with -Os, and works when compiled like that +a LOT faster: + Max USB download speed - 61 Kilobytes/s. + Real Life flash programming speed - 20 Kilobytes/s + From af8955b1120a978df3ac65cdce1ef8c1ee6ba47f Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 8 Jun 2012 00:40:07 +0400 Subject: [PATCH 50/66] Do not disable SysTick before jump to app. It screws up ST's SysTickConfig() routine if using stock libs Needs more testing Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/hardware.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/common/hardware.c b/src/common/hardware.c index f610a87..21925e6 100644 --- a/src/common/hardware.c +++ b/src/common/hardware.c @@ -166,8 +166,6 @@ void nvicDisableInterrupts() { rNVIC->ICER[1] = 0xFFFFFFFF; rNVIC->ICPR[0] = 0xFFFFFFFF; rNVIC->ICPR[1] = 0xFFFFFFFF; - - SET_REG(STK_CTRL,0x04); /* disable the systick, which operates separately from nvic */ } void systemHardReset(void) { From cb1bf0b6ddb52dbec0ffbfa0a87438757cc280e5 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 8 Jun 2012 00:45:07 +0400 Subject: [PATCH 51/66] mcortex: changed bootloader condition to fire only by dfu-util command Signed-off-by: Andrew 'Necromant' Andrianov --- boards/mcortex/config.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index a7c0df7..45ed8db 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -83,11 +83,17 @@ //#define CONFIG_EXTRA_MAIN_CODE /* while this is '1' we're looping in the bootloader */ + +/* #define CONFIG_EXTRA_MAIN_CODE \ bool have_code = (((checkUserCode(USER_CODE_FLASH)) || (checkUserCode(USER_CODE_RAM)))); \ int delay_count =0; + #define bootloaderCondition ((!have_code) || (delay_count++ < BOOTLOADER_WAIT)) +*/ + +#define bootloaderCondition 1 /* define to 0 to never exit, undefine to save space */ //#define bootloaderExitCondition 0 From 823e6b2af49924c2de5f9a6207d9718a9f140e69 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 8 Jun 2012 02:26:23 +0400 Subject: [PATCH 52/66] Revert "Do not disable SysTick before jump to app." This reverts commit bcf9376053dcbccc2b99605a3bd089f738c21dd5. That was a bad idea. Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/hardware.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/common/hardware.c b/src/common/hardware.c index 21925e6..f610a87 100644 --- a/src/common/hardware.c +++ b/src/common/hardware.c @@ -166,6 +166,8 @@ void nvicDisableInterrupts() { rNVIC->ICER[1] = 0xFFFFFFFF; rNVIC->ICPR[0] = 0xFFFFFFFF; rNVIC->ICPR[1] = 0xFFFFFFFF; + + SET_REG(STK_CTRL,0x04); /* disable the systick, which operates separately from nvic */ } void systemHardReset(void) { From d63ec7df4ecd8f4370ba1f7a3932a88e834d9778 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 8 Jun 2012 10:42:04 +0400 Subject: [PATCH 53/66] dfuToRun: Do not jump to user app from interrupt. Doing so screws all interrupts in the user app. Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu_io.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/common/dfu_io.c b/src/common/dfu_io.c index 6c817b2..3e6db6c 100644 --- a/src/common/dfu_io.c +++ b/src/common/dfu_io.c @@ -70,10 +70,18 @@ void dfuToFlashInit() #endif #ifdef CONFIG_ALTSETTING_RUN -void dfuToRunInit() + +void dfuToRunCopy() { jumpToUser(CONFIG_RUN_ADDR); } + +void dfuToRunInit() +{ + /* We cannot jump from here, sice we're in interrupt */ + /* Without extra init it screws up interrupts in the app */ + dfuCopyFunc = dfuToRunCopy; +} #endif From b6a01b34ef0bf8dc5c7cf850132ca35859652e27 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Fri, 8 Jun 2012 12:05:00 +0400 Subject: [PATCH 54/66] Added optimizing README Signed-off-by: Andrew 'Necromant' Andrianov --- README.optimizing | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 README.optimizing diff --git a/README.optimizing b/README.optimizing new file mode 100644 index 0000000..3c9ab8f --- /dev/null +++ b/README.optimizing @@ -0,0 +1,25 @@ +Misc notes about code size. + +The default compiler settings provide quite good size optimizations. +If you still aren't happy - read further. + +board.c +It is a good idea to replace the beautiful get/set code with a bunch +of SET_REGS with magic numbers. It will be less readable, but will +occupy less space. + +config.h +Disable any strobing at all, disable usb suspend code. +Enable only one altsetting that you really need. + +The most minimal working config I could get was about 5.9KiB. + +VTOR. For the interrupts in your app to work, VTOR register, aka +SCB->VTOR needs to be set to point to the new vector table. +ST's app notes recommend setting this in the application itself, +during init. +ST's periph libs do that in SystemInit(); See their code for details. +It is not necessary here, since the VTOR is set before the jump +by the bootloader automatically and it proves to be working. +It won't hurt setting it again, but if you are running in a memory +constrained system, you can safely omit that. From f99ac993e0e64afda43af590c3451116cdb36b27 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 8 Jun 2012 12:07:02 +0400 Subject: [PATCH 55/66] Updates to optimizing README Signed-off-by: Andrew 'Necromant' Andrianov --- README.optimizing | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.optimizing b/README.optimizing index 3c9ab8f..f178f27 100644 --- a/README.optimizing +++ b/README.optimizing @@ -7,6 +7,8 @@ board.c It is a good idea to replace the beautiful get/set code with a bunch of SET_REGS with magic numbers. It will be less readable, but will occupy less space. +Only leave boardInit and boardTeardown, or declare other functions +as inline. config.h Disable any strobing at all, disable usb suspend code. From fbcd20296ae64be6b4c9cf9aa320698b9e548d37 Mon Sep 17 00:00:00 2001 From: Andrew Date: Fri, 8 Jun 2012 12:12:23 +0400 Subject: [PATCH 56/66] Fix TODO Signed-off-by: Andrew 'Necromant' Andrianov --- TODO | 1 + 1 file changed, 1 insertion(+) diff --git a/TODO b/TODO index b2a592a..9d09835 100644 --- a/TODO +++ b/TODO @@ -8,3 +8,4 @@ Merge here all the CREDITS, Licensing info and other shit *DONE (Somewhat) Add support for boards: mcortex mctx-dm sISP ucGUN, etc. Talk with leaflabs, merge&check their boards *WiP fix coding style!!! *WiP +See if some common usb_descriptors.c parts can be macrofied and moved away from boards. Is it good idea? \ No newline at end of file From 0aaddee498ede334e60276f45860950d92f8f593 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 9 Jun 2012 00:30:59 +0400 Subject: [PATCH 57/66] maple-ret6: Fix board initialisation code and config Signed-off-by: Andrew 'Necromant' Andrianov --- boards/maple-ret6/board.c | 74 +++++++++++++------------------------- boards/maple-ret6/config.h | 45 +++++++++++++++-------- 2 files changed, 55 insertions(+), 64 deletions(-) diff --git a/boards/maple-ret6/board.c b/boards/maple-ret6/board.c index 6875551..706f4fb 100644 --- a/boards/maple-ret6/board.c +++ b/boards/maple-ret6/board.c @@ -4,23 +4,13 @@ #include "hardware.h" -static void setupLED (void) { - // todo, swap out hardcoded pin/bank with macro - u32 rwmVal; /* read-write-modify place holder var */ - - /* Setup APB2 (GPIOA) */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000004; - SET_REG(RCC_APB2ENR,rwmVal); - - /* Setup GPIOA Pin 5 as PP Out */ - SET_REG(GPIO_CRL(GPIOA), 0x00100000); - - rwmVal = GET_REG(GPIO_CRL(GPIOA)); - rwmVal &= 0xFF0FFFFF; - rwmVal |= 0x00100000; - SET_REG(GPIO_CRL(GPIOA),rwmVal); - setPin(GPIOA,5); +void setupLED (void) { + /* enable LED pin */ + pRCC->APB2ENR |= RCC_APB2ENR_LED; + + /* Setup LED pin as output open drain */ + SET_REG(LED_BANK_CR,(GET_REG(LED_BANK_CR) & LED_CR_MASK) | LED_CR_OUTPUT); + setPin(LED_BANK, LED); } static void setupCLK (void) { @@ -41,51 +31,37 @@ static void setupCLK (void) { while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ } -static void setupBUTTON (void) { - // todo, swap out hardcoded pin/bank with macro - u32 rwmVal; /* read-write-modify place holder var */ - - /* Setup APB2 (GPIOC) */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x00000010; - SET_REG(RCC_APB2ENR,rwmVal); - - /* Setup GPIOC Pin 9 as PP Out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFFFFF0F; - rwmVal |= 0x00000040; - SET_REG(GPIO_CRH(GPIOC),rwmVal); +void setupBUTTON (void) { + /* enable button pin */ + pRCC->APB2ENR |= RCC_APB2ENR_BUT; + /* Setup button pin as floating input */ + SET_REG(BUT_BANK_CR,(GET_REG(BUT_BANK_CR) & BUT_CR_MASK) | BUT_CR_OUTPUT); + setPin(BUTTON_BANK, BUTTON); } -static void inline setupUSB (void) { - u32 rwmVal; /* read-write-modify place holder var */ +void setupUSB (void) { + /* enable USB DISC Pin */ + pRCC->APB2ENR |= RCC_APB2ENR_USB; - /* Setup the USB DISC Pin */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x4; - SET_REG(RCC_APB2ENR,rwmVal); + /* Setup USB DISC pin as output open drain */ + SET_REG(USB_DISC_CR, + (GET_REG(USB_DISC_CR) & USB_DISC_CR_MASK) | USB_DISC_CR_OUTPUT_OD); + setPin(USB_DISC_BANK, USB_DISC); - // todo, macroize usb_disc pin - /* Setup GPIOC Pin 12 as OD out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); - rwmVal &= 0xFFF0FFFF; - rwmVal |= 0x00050000; - SET_REG(GPIO_CRH(GPIOC),rwmVal); + /* turn on the USB clock */ pRCC->APB1ENR |= 0x00800000; - resetPin (GPIOC,12); - rwmVal = 10000000; - while (rwmVal--);; - /* initialize the usb application */ - setPin (GPIOC,12); /* present ourselves to the host */ + /* initialize the USB application */ + resetPin(USB_DISC_BANK, USB_DISC); /* present ourselves to the host */ + } /* executed before actual jump to user code */ void boardTeardown() { - setPin(GPIOC,12); // disconnect usb from host. todo, macroize pin + setPin(USB_DISC_BANK, USB_DISC); /* unpresent ourselves to the host */ } /* This is a common routine to setup the board */ diff --git a/boards/maple-ret6/config.h b/boards/maple-ret6/config.h index 003309f..55bf342 100644 --- a/boards/maple-ret6/config.h +++ b/boards/maple-ret6/config.h @@ -33,8 +33,29 @@ #ifndef __CONFIG_H #define __CONFIG_H -//WTF WAS THAT DOING HERE?? -//#include "common.h" +/* maple ret6 config */ +#define LED_BANK GPIOA +#define LED 5 +#define LED_BANK_CR GPIO_CRL(LED_BANK) +#define LED_CR_MASK 0xFF0FFFFF +#define LED_CR_OUTPUT 0x00100000 +#define RCC_APB2ENR_LED 0x00000004 /* enable PA */ + +/* On the Maple RET6, BUT is PC9 */ +#define BUTTON_BANK GPIOC +#define BUTTON 9 +#define BUT_BANK_CR GPIO_CRH(BUTTON_BANK) +#define BUT_CR_MASK 0xFFFFFF0F +#define BUT_CR_OUTPUT 0x00000040 +#define RCC_APB2ENR_BUT 0x00000010 /* enable PC */ + +/* USB Disc Pin Setup. On the Maple RET6, USB_DISC is PC12 */ +#define USB_DISC_BANK GPIOC +#define USB_DISC 12 +#define USB_DISC_CR GPIO_CRH(USB_DISC_BANK) +#define USB_DISC_CR_MASK 0xFFF0FFFF +#define USB_DISC_CR_OUTPUT_OD 0x00050000 +#define RCC_APB2ENR_USB 0x00000010 /* THIS IS MCU-DEPENDANT! RTFM: Reference Manual */ #define FLASH_PAGE_SIZE 0x800 @@ -43,17 +64,11 @@ #define STR_DESC_LEN 6 //No LED strobing whatsoever - saves space -#define CONFIG_INHIBIT_STROBE +//#define CONFIG_INHIBIT_STROBE -#define LED_BANK GPIOA -#define LED 5 #define BLINK_FAST 0x50000 #define BLINK_SLOW 0x100000 -#define BUTTON_BANK GPIOC - -#define BUTTON 9 - #define STARTUP_BLINKS 5 #define BOOTLOADER_WAIT 6 @@ -66,7 +81,7 @@ #define CONFIG_ALTSETTING_RAM 0 #define CONFIG_ALTSETTING_FLASH 1 -#define CONFIG_ALTSETTING_RUN 2 +//#define CONFIG_ALTSETTING_RUN 2 /* Any transfer to run altsetting will cause a jump to this addr */ #define CONFIG_RUN_ADDR USER_CODE_FLASH @@ -76,12 +91,12 @@ #define PROD_ID1 0x00 /* while this is '1' we're looping in the bootloader */ -#define CONFIG_EXTRA_MAIN_CODE \ - bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); \ - int delay_count =0; +#define CONFIG_EXTRA_MAIN_CODE \ + bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); \ + int delay_count =0; -#define bootloaderCondition (delay_count++ < BOOTLOADER_WAIT) \ - || no_user_jump +#define bootloaderCondition ((delay_count++ < BOOTLOADER_WAIT) \ + || no_user_jump) #define bootloaderExitCondition 0 From c87962b02fc080e87064d082d40ef2af86e23489 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 9 Jun 2012 00:41:05 +0400 Subject: [PATCH 58/66] maple: fix board configuration, according to the upstream Signed-off-by: Andrew 'Necromant' Andrianov --- boards/maple/board.c | 63 +++++++++++++++++++++---------------------- boards/maple/config.h | 8 +++--- 2 files changed, 34 insertions(+), 37 deletions(-) diff --git a/boards/maple/board.c b/boards/maple/board.c index 4415ca4..312f69b 100644 --- a/boards/maple/board.c +++ b/boards/maple/board.c @@ -4,41 +4,42 @@ #include "hardware.h" +static void setupCLK (void) { + /* enable HSE */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); + while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ + + /* enable flash prefetch buffer */ + SET_REG(FLASH_ACR, 0x00000012); + + /* Configure PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ + SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ + while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ + + /* Set SYSCLK as PLL */ + SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); + while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ +} + static void setupLED (void) { // todo, swap out hardcoded pin/bank with macro u32 rwmVal; /* read-write-modify place holder var */ /* Setup APB2 (GPIOA) */ - rwmVal = GET_REG(RCC_APB2ENR); + rwmVal = GET_REG(RCC_APB2ENR); rwmVal |= 0x00000004; SET_REG(RCC_APB2ENR,rwmVal); /* Setup GPIOA Pin 5 as PP Out */ SET_REG(GPIO_CRL(GPIOA), 0x00100000); - rwmVal = GET_REG(GPIO_CRL(GPIOA)); + rwmVal = GET_REG(GPIO_CRL(GPIOA)); rwmVal &= 0xFF0FFFFF; rwmVal |= 0x00100000; SET_REG(GPIO_CRL(GPIOA),rwmVal); - setPin(GPIOA,5); -} - -static void setupCLK (void) { - /* enable HSE */ - SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x00010001); - while ((GET_REG(RCC_CR) & 0x00020000) == 0); /* for it to come on */ - - /* enable flash prefetch buffer */ - SET_REG(FLASH_ACR, 0x00000012); - - /* Configure PLL */ - SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x001D0400); /* pll=72Mhz,APB1=36Mhz,AHB=72Mhz */ - SET_REG(RCC_CR,GET_REG(RCC_CR) | 0x01000000); /* enable the pll */ - while ((GET_REG(RCC_CR) & 0x03000000) == 0); /* wait for it to come on */ - /* Set SYSCLK as PLL */ - SET_REG(RCC_CFGR,GET_REG(RCC_CFGR) | 0x00000002); - while ((GET_REG(RCC_CFGR) & 0x00000008) == 0); /* wait for it to come on */ + setPin(GPIOA,5); } static void setupBUTTON (void) { @@ -46,42 +47,40 @@ static void setupBUTTON (void) { u32 rwmVal; /* read-write-modify place holder var */ /* Setup APB2 (GPIOC) */ - rwmVal = GET_REG(RCC_APB2ENR); + rwmVal = GET_REG(RCC_APB2ENR); rwmVal |= 0x00000010; SET_REG(RCC_APB2ENR,rwmVal); /* Setup GPIOC Pin 9 as PP Out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal = GET_REG(GPIO_CRH(GPIOC)); rwmVal &= 0xFFFFFF0F; rwmVal |= 0x00000040; SET_REG(GPIO_CRH(GPIOC),rwmVal); } -static void inline setupUSB (void) { +static void setupUSB (void) { u32 rwmVal; /* read-write-modify place holder var */ /* Setup the USB DISC Pin */ - rwmVal = GET_REG(RCC_APB2ENR); - rwmVal |= 0x4; + rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x00000010; SET_REG(RCC_APB2ENR,rwmVal); // todo, macroize usb_disc pin /* Setup GPIOC Pin 12 as OD out */ - rwmVal = GET_REG(GPIO_CRH(GPIOC)); + rwmVal = GET_REG(GPIO_CRH(GPIOC)); rwmVal &= 0xFFF0FFFF; rwmVal |= 0x00050000; + setPin (GPIOC,12); SET_REG(GPIO_CRH(GPIOC),rwmVal); + pRCC->APB1ENR |= 0x00800000; - resetPin (GPIOC,12); - rwmVal = 10000000; - while (rwmVal--);; /* initialize the usb application */ - setPin (GPIOC,12); /* present ourselves to the host */ + resetPin (GPIOC,12); /* present ourselves to the host */ } - /* executed before actual jump to user code */ void boardTeardown() { @@ -96,5 +95,3 @@ void boardInit() setupBUTTON(); setupUSB(); } - - diff --git a/boards/maple/config.h b/boards/maple/config.h index 303d692..22cd64e 100644 --- a/boards/maple/config.h +++ b/boards/maple/config.h @@ -43,7 +43,7 @@ #define STR_DESC_LEN 6 //No LED strobing whatsoever - saves space -#define CONFIG_INHIBIT_STROBE +//#define CONFIG_INHIBIT_STROBE #define LED_BANK GPIOA #define LED 5 @@ -66,7 +66,7 @@ #define CONFIG_ALTSETTING_RAM 0 #define CONFIG_ALTSETTING_FLASH 1 -#define CONFIG_ALTSETTING_RUN 2 +//#define CONFIG_ALTSETTING_RUN 2 /* Any transfer to run altsetting will cause a jump to this addr */ #define CONFIG_RUN_ADDR USER_CODE_FLASH @@ -80,8 +80,8 @@ bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); \ int delay_count =0; -#define bootloaderCondition (delay_count++ < BOOTLOADER_WAIT) \ - || no_user_jump +#define bootloaderCondition ((delay_count++ < BOOTLOADER_WAIT) \ + || no_user_jump) #define bootloaderExitCondition 0 From 35665add66eacfa5a34b69ffbc56f4eb93026633 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 9 Jun 2012 01:07:00 +0400 Subject: [PATCH 59/66] Add a status matrix Signed-off-by: Andrew 'Necromant' Andrianov --- README.status | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 README.status diff --git a/README.status b/README.status new file mode 100644 index 0000000..59dc4fe --- /dev/null +++ b/README.status @@ -0,0 +1,14 @@ + +This is the status matrix of exisiting boards and +their current status. + +| Boardname | Board Fullname | Vendor | Status | Notes | +|---------------------+------------------------+----------------------+--------+-------| +| mcortex | Motor Cortex | Necromant & DmitrySu | good | 1 | +| te-stm32f103ret6kit | TE eval board | TerraElectronica.ru | good | 2 | +| maple | Leaflabs maple(master) | Leaflabs LLC | should | 3 | +| maple-ret6 | Leaflabs maple RET6 | Leaflabs LLC | should | 3 | + +1. SPI and FPGA targets need extra love +2. USB disconnection doesn't work. Screwed USB disc circuit? +3. Being tested upstream right now, initial testing went bad. From 0ff05c66694f7c27d28ab7440af54a894508c437 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 9 Jun 2012 13:24:29 +0400 Subject: [PATCH 60/66] te-stm32f103ret6kit: fix usb disconnection handling Signed-off-by: Andrew 'Necromant' Andrianov --- boards/te-stm32f103ret6kit/board.c | 17 ++++++----------- boards/te-stm32f103ret6kit/config.h | 4 ++-- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/boards/te-stm32f103ret6kit/board.c b/boards/te-stm32f103ret6kit/board.c index e8f5d2c..017a6f5 100644 --- a/boards/te-stm32f103ret6kit/board.c +++ b/boards/te-stm32f103ret6kit/board.c @@ -69,26 +69,20 @@ static void inline setupUSB (void) { SET_REG(RCC_APB2ENR,rwmVal); // todo, macroize usb_disc pin - /* Setup GPIOC Pin 12 as OD out */ + /* Setup GPIOB Pin 5 as OD out */ rwmVal = GET_REG(GPIO_CRL(GPIOB)); rwmVal &= 0xFF0FFFFF; - rwmVal |= 0x00500000; + rwmVal |= 0x00100000; SET_REG(GPIO_CRL(GPIOB),rwmVal); - pRCC->APB1ENR |= 0x00800000; - - resetPin (GPIOB,5); - rwmVal = 10000000; - while (rwmVal--);; - /* initialize the usb application */ - setPin (GPIOB,5); /* present ourselves to the host */ - usbAppInit(); + setPin (GPIOB,5); + resetPin (GPIOB,5); /* present ourselves to the host */ } /* executed before actual jump to user code */ void boardTeardown() { - resetPin(GPIOB,5); // disconnect usb from host. todo, macroize pin + setPin(GPIOB,5); // disconnect usb from host. todo, macroize pin } /* This is a common routine to setup the board */ @@ -97,5 +91,6 @@ void boardInit() setupCLK(); setupLED(); setupBUTTON(); + strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); setupUSB(); } diff --git a/boards/te-stm32f103ret6kit/config.h b/boards/te-stm32f103ret6kit/config.h index 9ea3817..5de1615 100644 --- a/boards/te-stm32f103ret6kit/config.h +++ b/boards/te-stm32f103ret6kit/config.h @@ -42,7 +42,7 @@ //Replace USB suspend/resume code with dummies. //Saves a few bytes more -#define CONFIG_DISABLE_USB_SUSPEND +//#define CONFIG_DISABLE_USB_SUSPEND /* THIS IS MCU-DEPENDANT! RTFM: Reference Manual */ #define FLASH_PAGE_SIZE 0x800 @@ -59,7 +59,7 @@ #define BOOTLOADER_WAIT 6 #define USER_CODE_RAM ((u32) 0x20000C00) -#define USER_CODE_FLASH ((u32) 0x08003000) +#define USER_CODE_FLASH ((u32) 0x08005000) /* AltSetting Configuration From 23b1b63ed67a12bf11e78188597805fe40ca299d Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 9 Jun 2012 13:25:15 +0400 Subject: [PATCH 61/66] Move usb clock enabling to main, since that's common for everything Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/main.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/common/main.c b/src/common/main.c index 51a241c..74f6582 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -37,19 +37,17 @@ int main() { systemReset(); // peripherals but not PC boardInit(); - setupFLASH(); + pRCC->APB1ENR |= 0x00800000; usbAppInit(); + setupFLASH(); strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); + + + #ifdef CONFIG_EXTRA_MAIN_CODE CONFIG_EXTRA_MAIN_CODE #endif -/* This code now moved to CONFIG_EXTRA_MAIN_CODE */ -// bool no_user_jump = (((!checkUserCode(USER_CODE_FLASH)) && (!checkUserCode(USER_CODE_RAM))) || readPin(BUTTON_BANK,BUTTON)); -// #ifdef CONFIG_BOOTLOADER_WAIT -// int delay_count = 0; -// #endif - /* wait for host to upload program or halt bootloader */ while (bootloaderCondition) { strobePin(LED_BANK,LED,1,BLINK_SLOW); From 7b126f6cb64a4a8ae1546b33f597c00617b0d1fb Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Mon, 11 Jun 2012 01:30:20 +0400 Subject: [PATCH 62/66] dfu: Only call copyFunc if thiBlocklen is nonzero Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/common/dfu.c b/src/common/dfu.c index fafdb12..42e5594 100644 --- a/src/common/dfu.c +++ b/src/common/dfu.c @@ -39,10 +39,9 @@ volatile u32 userAppAddr = USER_CODE_RAM; /* default RAM user code location */ DFUStatus dfuAppStatus; /* includes state */ volatile bool dfuBusy = FALSE; -volatile u8 recvBuffer[wTransferSize]; -volatile u32 userFirmwareLen = 0; -volatile u16 thisBlockLen = 0; - +u8 recvBuffer[wTransferSize]; +u32 userFirmwareLen = 0; +int thisBlockLen; volatile PLOT code_copy_lock; /* todo: force dfu globals to be singleton to avoid re-inits? */ @@ -355,7 +354,8 @@ void dfuFinishUpload() { { if (code_copy_lock == BEGINNING) { code_copy_lock=MIDDLE; - dfuCopyFunc(); + if (thisBlockLen!=0) + dfuCopyFunc(); code_copy_lock = END; } } From 9e1ddfa8f8b1f56302c045ac8c506f8058a85674 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Mon, 11 Jun 2012 11:48:04 +0400 Subject: [PATCH 63/66] dfu_io: Add proper routines for FPGA download Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/dfu_io.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/src/common/dfu_io.c b/src/common/dfu_io.c index 3e6db6c..739cb0f 100644 --- a/src/common/dfu_io.c +++ b/src/common/dfu_io.c @@ -1,6 +1,3 @@ - - - /* This file will be just sucked in dfu.c for the sake of simplicity */ #ifdef CONFIG_ALTSETTING_RAM @@ -92,7 +89,6 @@ void dfuToNowhereCopy() { userFirmwareLen += thisBlockLen; thisBlockLen = 0; - /* just copied all the fw to astral */ } @@ -118,10 +114,20 @@ void dfuToSPIInit() #endif #ifdef CONFIG_ALTSETTING_FPGA +#include "xsscu.h" + +extern const struct xsscu_unit fpga0; +void dfuToXsscuCopy() +{ + xsscu_write(&fpga0, (unsigned char*) recvBuffer, thisBlockLen); + userFirmwareLen += thisBlockLen; + thisBlockLen = 0; +} void dfuToFPGAInit() { - /* TODO */ + xsscu_reset(&fpga0); + dfuCopyFunc = dfuToXsscuCopy; } #endif From 7bd31ae318d594e91ba6f938efa861a17a49cf52 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Mon, 11 Jun 2012 11:48:50 +0400 Subject: [PATCH 64/66] mcortex: Properly add FPGA target Signed-off-by: Andrew 'Necromant' Andrianov --- boards/mcortex/board.c | 86 ++++++++++++++++++++++++++++++++++++++++- boards/mcortex/config.h | 1 + 2 files changed, 85 insertions(+), 2 deletions(-) diff --git a/boards/mcortex/board.c b/boards/mcortex/board.c index 7460aba..fb8362b 100644 --- a/boards/mcortex/board.c +++ b/boards/mcortex/board.c @@ -2,7 +2,7 @@ #include "cortexm3_macro.h" #include "common.h" #include "hardware.h" - +#include "xsscu.h" inline void setupLED (void) { // todo, swap out hardcoded pin/bank with macro @@ -42,6 +42,15 @@ inline void setupCLK (void) { } +static void mdelay(int n) +{ + int i = n*100; + while (i-- > 0) { + asm("nop"); + } +} + + /* We simulate disconnection by pulling PA12(USB) to the ground */ /* This saves us a pin */ inline void setupUSB (void) { @@ -54,9 +63,81 @@ inline void setupUSB (void) { rwmVal |= 0x00011000; SET_REG(GPIO_CRH(GPIOA),rwmVal); resetPin (GPIOA,12); - pRCC->APB1ENR |= 0x00800000; } +#define AFIO_MAPR (u32) (0x40000000 + 0x10000 + 0x4) + + + +/* +JRST CCLK PB4 +JTDO DIN PB3 +JTCK PROGB PA14 + +JTDI INITB PA15 +JTMS DONE PA13 +*/ + +#define sr_func(func,g,p) \ + static void func(char m) { \ + m ? setPin(g,p) : resetPin(g,p); \ + } + +sr_func(clk_ctl,GPIOB,4); +sr_func(progb_ctl,GPIOA,14); +sr_func(sout_ctl,GPIOB,3); + +static char get_done() +{ + //return (char) GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) + return readPin(GPIOA,1); +} + +static char get_initb() +{ + //return (char) GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_1) + return readPin(GPIOA,1); +} + + +const struct xsscu_unit fpga0 = { +.name = "Xilinx XS3S100E", +.clk_ctl = clk_ctl, +.sout_ctl = sout_ctl, +.progb_ctl = progb_ctl, +.get_initb = get_initb, +.get_done = get_done, +.delay = mdelay, +.delay_pb = 1200, +.delay_clk = 0 +}; + + +inline void setupXSSCU() +{ + /* We need to remap some stuff */ + /* Enable AFIO clock */ + u32 rwmVal = GET_REG(RCC_APB2ENR); + rwmVal |= 0x000000D; + /* Remap stuff */ + SET_REG(RCC_APB2ENR,rwmVal); + rwmVal = GET_REG(AFIO_MAPR); + rwmVal&=0xF8FFFFFF; + rwmVal|=0x4000000; + SET_REG(AFIO_MAPR,rwmVal); + /* Now, let's setup GPIOS */ + /* GPIOB */ + rwmVal = GET_REG(GPIO_CRL(GPIOB)); + rwmVal&=0xFFF00FFF; + rwmVal|=0x00011000; + SET_REG(GPIO_CRL(GPIOB),rwmVal); + /* Now, let's setup GPIOS */ + /* GPIOA */ + rwmVal = GET_REG(GPIO_CRH(GPIOA)); + rwmVal&=0xF0FFFFFF; + rwmVal|=0x01000000; + SET_REG(GPIO_CRH(GPIOA),rwmVal); +} /* executed before actual jump to user code */ void boardTeardown() @@ -68,6 +149,7 @@ void boardTeardown() void boardInit() { setupCLK(); + setupXSSCU(); setupLED(); setupUSB(); } diff --git a/boards/mcortex/config.h b/boards/mcortex/config.h index 45ed8db..90f7d5e 100644 --- a/boards/mcortex/config.h +++ b/boards/mcortex/config.h @@ -69,6 +69,7 @@ #define CONFIG_ALTSETTING_RAM 0 #define CONFIG_ALTSETTING_FLASH 1 #define CONFIG_ALTSETTING_RUN 2 +#define CONFIG_ALTSETTING_FPGA 3 /* Any transfer to run altsetting will cause a jump to this addr */ #define CONFIG_RUN_ADDR USER_CODE_FLASH From 926b9301c0a5d9ca082b077da6c8f365a7eca007 Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Mon, 11 Jun 2012 11:49:43 +0400 Subject: [PATCH 65/66] Update README.status with a better status matrix Signed-off-by: Andrew 'Necromant' Andrianov --- README.status | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/README.status b/README.status index 59dc4fe..854ec4c 100644 --- a/README.status +++ b/README.status @@ -12,3 +12,20 @@ their current status. 1. SPI and FPGA targets need extra love 2. USB disconnection doesn't work. Screwed USB disc circuit? 3. Being tested upstream right now, initial testing went bad. + + +Feature status. + +| Feature | boards | status | upstream status | Notes | +|----------------------------+---------+--------+-----------------+-------| +| Flash Download | all | good | good | | +| RAM Download | all | good | good | | +| USB Software Disconnection | -all | WiP | ??? | | +| SPI Flash Download | mcortex | WiP | n/a | 1 | +| FPGA XSSCU Download | mcortex | mostly | n/a | 2 | + +1. Driver needs to be ported. Only board with flash - mcortex +2. Should work, requires some extra dummy bytes in the fw, needs more testing + + + From a5b4b239f9211ac0760f64f21759bc9b8787703e Mon Sep 17 00:00:00 2001 From: Andrew 'Necromant' Andrianov Date: Sat, 23 Jun 2012 14:09:20 +0400 Subject: [PATCH 66/66] main: reordered events to fix enumeration issues. More testing needed Signed-off-by: Andrew 'Necromant' Andrianov --- src/common/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/main.c b/src/common/main.c index 74f6582..4f6ea8e 100644 --- a/src/common/main.c +++ b/src/common/main.c @@ -37,11 +37,11 @@ int main() { systemReset(); // peripherals but not PC boardInit(); + strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); pRCC->APB1ENR |= 0x00800000; usbAppInit(); setupFLASH(); - strobePin(LED_BANK,LED,STARTUP_BLINKS,BLINK_FAST); - + #ifdef CONFIG_EXTRA_MAIN_CODE