From 301d9cddfce16e73c520575279b224c179412140 Mon Sep 17 00:00:00 2001 From: zedamota Date: Thu, 13 Jan 2011 15:57:30 +0000 Subject: [PATCH] OP-268/PipX Bootloader - Trunk integration of Edouard's work. git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2399 ebee16cc-31ac-478f-84a7-5cbb03baadba --- flight/Bootloaders/PipXtreme/Makefile | 688 ++++++++++++++++++ flight/Bootloaders/PipXtreme/inc/README | 3 + flight/Bootloaders/PipXtreme/inc/common.h | 115 +++ flight/Bootloaders/PipXtreme/inc/op_dfu.h | 61 ++ flight/Bootloaders/PipXtreme/inc/pios_board.h | 384 ++++++++++ .../Bootloaders/PipXtreme/inc/pios_config.h | 67 ++ flight/Bootloaders/PipXtreme/inc/stopwatch.h | 55 ++ flight/Bootloaders/PipXtreme/main.c | 209 ++++++ flight/Bootloaders/PipXtreme/op_dfu.c | 492 +++++++++++++ flight/Bootloaders/PipXtreme/pios_board.c | 345 +++++++++ flight/Bootloaders/PipXtreme/stopwatch.c | 103 +++ 11 files changed, 2522 insertions(+) create mode 100644 flight/Bootloaders/PipXtreme/Makefile create mode 100644 flight/Bootloaders/PipXtreme/inc/README create mode 100644 flight/Bootloaders/PipXtreme/inc/common.h create mode 100644 flight/Bootloaders/PipXtreme/inc/op_dfu.h create mode 100644 flight/Bootloaders/PipXtreme/inc/pios_board.h create mode 100644 flight/Bootloaders/PipXtreme/inc/pios_config.h create mode 100644 flight/Bootloaders/PipXtreme/inc/stopwatch.h create mode 100644 flight/Bootloaders/PipXtreme/main.c create mode 100644 flight/Bootloaders/PipXtreme/op_dfu.c create mode 100644 flight/Bootloaders/PipXtreme/pios_board.c create mode 100644 flight/Bootloaders/PipXtreme/stopwatch.c diff --git a/flight/Bootloaders/PipXtreme/Makefile b/flight/Bootloaders/PipXtreme/Makefile new file mode 100644 index 000000000..e30b808f5 --- /dev/null +++ b/flight/Bootloaders/PipXtreme/Makefile @@ -0,0 +1,688 @@ + ##### + # Project: OpenPilot + # + # + # Makefile for OpenPilot project build PiOS and the AP. + # + # The OpenPilot Team, http://www.openpilot.org, Copyright (C) 2009. + # + # + # This program is free software; you can redistribute it and/or modify + # it under the terms of the GNU General Public License as published by + # the Free Software Foundation; either version 3 of the License, or + # (at your option) any later version. + # + # This program is distributed in the hope that it will be useful, but + # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + # for more details. + # + # You should have received a copy of the GNU General Public License along + # with this program; if not, write to the Free Software Foundation, Inc., + # 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + ##### + + +# Set developer code and compile options +# Set to YES to compile for debugging +DEBUG ?= NO + +# Set to YES to use the Servo output pins for debugging via scope or logic analyser +ENABLE_DEBUG_PINS ?= NO + +# Set to Yes to enable the AUX UART which is mapped on the S1 (Tx) and S2 (Rx) servo outputs +ENABLE_AUX_UART ?= NO + +# +USE_BOOTLOADER ?= NO + + +# Set to YES when using Code Sourcery toolchain +CODE_SOURCERY ?= NO + +# Toolchain prefix (i.e arm-elf- -> arm-elf-gcc.exe) +TCHAIN_PREFIX ?= arm-none-eabi- + +# Remove command is different for Code Sourcery on Windows +ifeq ($(CODE_SOURCERY), YES) +REMOVE_CMD = cs-rm +else +REMOVE_CMD = rm +endif + +FLASH_TOOL = OPENOCD + +# YES enables -mthumb option to flags for source-files listed +# in SRC and CPPSRC +USE_THUMB_MODE = YES + + +# MCU name, submodel and board +# - MCU used for compiler-option (-mcpu) +# - MODEL used for linker-script name (-T) and passed as define +# - BOARD just passed as define (optional) +MCU = cortex-m3 +CHIP = STM32F103CBT +BOARD = STM32103CB_PIPXTREME +MODEL = MD +ifeq ($(USE_BOOTLOADER), YES) +BOOT_MODEL = $(MODEL)_BL + +else +BOOT_MODEL = $(MODEL)_NB +endif + +# Directory for output files (lst, obj, dep, elf, sym, map, hex, bin etc.) +OUTDIR = Build + +# Target file name (without extension). +TARGET = PipXtreme_BL + +# Paths +OPSYSTEM = . +OPSYSTEMINC = $(OPSYSTEM)/inc +OPUAVTALK = ./UAVTalk +OPUAVTALKINC = $(OPUAVTALK)/inc +OPUAVOBJ = ./UAVObjects +OPUAVOBJINC = $(OPUAVOBJ)/inc +OPTESTS = ./Tests +OPMODULEDIR = ./Modules +FLIGHTLIB = ../../Libraries +FLIGHTLIBINC = ../../Libraries/inc +PIOS = ../../PiOS +PIOSINC = $(PIOS)/inc +PIOSSTM32F10X = $(PIOS)/STM32F10x +PIOSCOMMON = $(PIOS)/Common +PIOSBOARDS = $(PIOS)/Boards +APPLIBDIR = $(PIOSSTM32F10X)/Libraries +STMLIBDIR = $(APPLIBDIR) +STMSPDDIR = $(STMLIBDIR)/STM32F10x_StdPeriph_Driver +STMUSBDIR = $(STMLIBDIR)/STM32_USB-FS-Device_Driver +STMSPDSRCDIR = $(STMSPDDIR)/src +STMSPDINCDIR = $(STMSPDDIR)/inc +STMUSBSRCDIR = $(STMUSBDIR)/src +STMUSBINCDIR = $(STMUSBDIR)/inc +CMSISDIR = $(STMLIBDIR)/CMSIS/Core/CM3 +DOSFSDIR = $(APPLIBDIR)/dosfs +MSDDIR = $(APPLIBDIR)/msd +RTOSDIR = $(APPLIBDIR)/FreeRTOS +RTOSSRCDIR = $(RTOSDIR)/Source +RTOSINCDIR = $(RTOSSRCDIR)/include +DOXYGENDIR = ../Doc/Doxygen + +# List C source files here. (C dependencies are automatically generated.) +# use file-extension c for "c-only"-files + +## OPENPILOT_BL CORE: +SRC += $(OPSYSTEM)/main.c +SRC += $(OPSYSTEM)/pios_board.c +SRC += $(OPSYSTEM)/op_dfu.c +SRC += $(OPSYSTEM)/stopwatch.c + + +## PIOS Hardware (STM32F10x) +SRC += $(PIOSSTM32F10X)/pios_sys.c +SRC += $(PIOSSTM32F10X)/pios_led.c +SRC += $(PIOSSTM32F10X)/pios_delay.c +SRC += $(PIOSSTM32F10X)/pios_usart.c +SRC += $(PIOSSTM32F10X)/pios_irq.c +#SRC += $(PIOSSTM32F10X)/pios_adc.c +#SRC += $(PIOSSTM32F10X)/pios_servo.c +#SRC += $(PIOSSTM32F10X)/pios_i2c.c +#SRC += $(PIOSSTM32F10X)/pios_spi.c +#SRC += $(PIOSSTM32F10X)/pios_ppm.c +#SRC += $(PIOSSTM32F10X)/pios_pwm.c +#SRC += $(PIOSSTM32F10X)/pios_spektrum.c +SRC += $(PIOSSTM32F10X)/pios_debug.c +SRC += $(PIOSSTM32F10X)/pios_gpio.c +#SRC += $(PIOSSTM32F10X)/pios_exti.c +#SRC += $(PIOSSTM32F10X)/pios_wdg.c + + +# PIOS USB related files (seperated to make code maintenance more easy) +SRC += $(PIOSSTM32F10X)/pios_usb_hid.c +SRC += $(PIOSSTM32F10X)/pios_usb_hid_desc.c +#SRC += $(PIOSSTM32F10X)/pios_usb_hid_endp.c +SRC += $(PIOSSTM32F10X)/pios_usb_hid_istr.c +SRC += $(PIOSSTM32F10X)/pios_usb_hid_prop.c +SRC += $(PIOSSTM32F10X)/pios_usb_hid_pwr.c + +## PIOS Hardware (Common) +#SRC += $(PIOSCOMMON)/pios_sdcard.c +SRC += $(PIOSCOMMON)/pios_com.c +#SRC += $(PIOSCOMMON)/pios_bmp085.c +#SRC += $(PIOSCOMMON)/pios_opahrs_v0.c +SRC += $(PIOSCOMMON)/pios_bl_helper.c +SRC += $(PIOSCOMMON)/pios_iap.c +#SRC += $(PIOSCOMMON)/pios_opahrs_proto.c +SRC += $(PIOSCOMMON)/printf-stdarg.c + +## Libraries for flight calculations +SRC += $(FLIGHTLIB)/fifo_buffer.c + +## CMSIS for STM32 +SRC += $(CMSISDIR)/core_cm3.c +SRC += $(CMSISDIR)/system_stm32f10x.c + +## Used parts of the STM-Library +#SRC += $(STMSPDSRCDIR)/stm32f10x_adc.c +SRC += $(STMSPDSRCDIR)/stm32f10x_bkp.c +SRC += $(STMSPDSRCDIR)/stm32f10x_crc.c +#SRC += $(STMSPDSRCDIR)/stm32f10x_dac.c +SRC += $(STMSPDSRCDIR)/stm32f10x_dma.c +SRC += $(STMSPDSRCDIR)/stm32f10x_exti.c +SRC += $(STMSPDSRCDIR)/stm32f10x_flash.c +SRC += $(STMSPDSRCDIR)/stm32f10x_gpio.c +#SRC += $(STMSPDSRCDIR)/stm32f10x_i2c.c +SRC += $(STMSPDSRCDIR)/stm32f10x_pwr.c +SRC += $(STMSPDSRCDIR)/stm32f10x_rcc.c +SRC += $(STMSPDSRCDIR)/stm32f10x_rtc.c +SRC += $(STMSPDSRCDIR)/stm32f10x_spi.c +SRC += $(STMSPDSRCDIR)/stm32f10x_tim.c +SRC += $(STMSPDSRCDIR)/stm32f10x_usart.c +#SRC += $(STMSPDSRCDIR)/stm32f10x_iwdg.c +SRC += $(STMSPDSRCDIR)/stm32f10x_dbgmcu.c +SRC += $(STMSPDSRCDIR)/misc.c + +## STM32 USB Library +SRC += $(STMUSBSRCDIR)/usb_core.c +SRC += $(STMUSBSRCDIR)/usb_init.c +SRC += $(STMUSBSRCDIR)/usb_int.c +SRC += $(STMUSBSRCDIR)/usb_mem.c +SRC += $(STMUSBSRCDIR)/usb_regs.c +SRC += $(STMUSBSRCDIR)/usb_sil.c + +# List C source files here which must be compiled in ARM-Mode (no -mthumb). +# use file-extension c for "c-only"-files +## just for testing, timer.c could be compiled in thumb-mode too +SRCARM = + +# List C++ source files here. +# use file-extension .cpp for C++-files (not .C) +CPPSRC = + +# List C++ source files here which must be compiled in ARM-Mode. +# use file-extension .cpp for C++-files (not .C) +#CPPSRCARM = $(TARGET).cpp +CPPSRCARM = + +# List Assembler source files here. +# Make them always end in a capital .S. Files ending in a lowercase .s +# will not be considered source files but generated files (assembler +# output from the compiler), and will be deleted upon "make clean"! +# Even though the DOS/Win* filesystem matches both .s and .S the same, +# it will preserve the spelling of the filenames, and gcc itself does +# care about how the name is spelled on its command-line. +ASRC = $(PIOSSTM32F10X)/startup_stm32f10x_$(MODEL).S + +# List Assembler source files here which must be assembled in ARM-Mode.. +ASRCARM = + +# List any extra directories to look for include files here. +# Each directory must be seperated by a space. +EXTRAINCDIRS = $(OPSYSTEM) +EXTRAINCDIRS += $(OPSYSTEMINC) +EXTRAINCDIRS += $(OPUAVTALK) +EXTRAINCDIRS += $(OPUAVTALKINC) +EXTRAINCDIRS += $(OPUAVOBJ) +EXTRAINCDIRS += $(OPUAVOBJINC) +EXTRAINCDIRS += $(PIOS) +EXTRAINCDIRS += $(PIOSINC) +EXTRAINCDIRS += $(FLIGHTLIBINC) +EXTRAINCDIRS += $(PIOSSTM32F10X) +EXTRAINCDIRS += $(PIOSCOMMON) +EXTRAINCDIRS += $(PIOSBOARDS) +EXTRAINCDIRS += $(STMSPDINCDIR) +EXTRAINCDIRS += $(STMUSBINCDIR) +EXTRAINCDIRS += $(CMSISDIR) +EXTRAINCDIRS += $(DOSFSDIR) +EXTRAINCDIRS += $(MSDDIR) +EXTRAINCDIRS += $(RTOSINCDIR) +EXTRAINCDIRS += $(APPLIBDIR) +EXTRAINCDIRS += $(RTOSSRCDIR)/portable/GCC/ARM_CM3 + + + +# List any extra directories to look for library files here. +# Also add directories where the linker should search for +# includes from linker-script to the list +# Each directory must be seperated by a space. +EXTRA_LIBDIRS = + +# Extra Libraries +# Each library-name must be seperated by a space. +# i.e. to link with libxyz.a, libabc.a and libefsl.a: +# EXTRA_LIBS = xyz abc efsl +# for newlib-lpc (file: libnewlibc-lpc.a): +# EXTRA_LIBS = newlib-lpc +EXTRA_LIBS = + +# Path to Linker-Scripts +LINKERSCRIPTPATH = $(PIOSSTM32F10X) + +# Optimization level, can be [0, 1, 2, 3, s]. +# 0 = turn off optimization. s = optimize for size. +# (Note: 3 is not always the best optimization level. See avr-libc FAQ.) + +ifeq ($(DEBUG),YES) +OPT = 0 +else +OPT = s +endif + +# Output format. (can be ihex or binary or both) +# binary to create a load-image in raw-binary format i.e. for SAM-BA, +# ihex to create a load-image in Intel hex format +#LOADFORMAT = ihex +#LOADFORMAT = binary +LOADFORMAT = both + +# Debugging format. +DEBUGF = dwarf-2 + +# Place project-specific -D (define) and/or +# -U options for C here. +CDEFS = -DSTM32F10X_$(MODEL) +CDEFS += -DUSE_STDPERIPH_DRIVER +CDEFS += -DUSE_$(BOARD) +ifeq ($(ENABLE_DEBUG_PINS), YES) +CDEFS += -DPIOS_ENABLE_DEBUG_PINS +endif +ifeq ($(ENABLE_AUX_UART), YES) +CDEFS += -DPIOS_ENABLE_AUX_UART +endif +ifeq ($(USE_BOOTLOADER), YES) +CDEFS += -DUSE_BOOTLOADER +endif + + +# Place project-specific -D and/or -U options for +# Assembler with preprocessor here. +#ADEFS = -DUSE_IRQ_ASM_WRAPPER +ADEFS = -D__ASSEMBLY__ + +# Compiler flag to set the C Standard level. +# c89 - "ANSI" C +# gnu89 - c89 plus GCC extensions +# c99 - ISO C99 standard (not yet fully implemented) +# gnu99 - c99 plus GCC extensions +CSTANDARD = -std=gnu99 + +#----- + +# Compiler flags. + +# -g*: generate debugging information +# -O*: optimization level +# -f...: tuning, see GCC manual and avr-libc documentation +# -Wall...: warning level +# -Wa,...: tell GCC to pass this to the assembler. +# -adhlns...: create assembler listing +# +# Flags for C and C++ (arm-elf-gcc/arm-elf-g++) + +ifeq ($(DEBUG),YES) +CFLAGS = -g$(DEBUGF) -DDEBUG +endif + +CFLAGS += -O$(OPT) +ifeq ($(DEBUG),NO) +CFLAGS += -ffunction-sections +endif + +CFLAGS += -mcpu=$(MCU) -mthumb +CFLAGS += $(CDEFS) +CFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) -I. + +CFLAGS += -mapcs-frame +CFLAGS += -fomit-frame-pointer +CFLAGS += -fpromote-loop-indices + +CFLAGS += -Wall +CFLAGS += -Werror +CFLAGS += -Wa,-adhlns=$(addprefix $(OUTDIR)/, $(notdir $(addsuffix .lst, $(basename $<)))) +# Compiler flags to generate dependency files: +CFLAGS += -MD -MP -MF $(OUTDIR)/dep/$(@F).d + +# flags only for C +#CONLYFLAGS += -Wnested-externs +CONLYFLAGS += $(CSTANDARD) + +# Assembler flags. +# -Wa,...: tell GCC to pass this to the assembler. +# -ahlns: create listing +ASFLAGS = -mcpu=$(MCU) -mthumb -I. -x assembler-with-cpp +ASFLAGS += $(ADEFS) +ASFLAGS += -Wa,-adhlns=$(addprefix $(OUTDIR)/, $(notdir $(addsuffix .lst, $(basename $<)))) +ASFLAGS += $(patsubst %,-I%,$(EXTRAINCDIRS)) + +MATH_LIB = -lm + +# Linker flags. +# -Wl,...: tell GCC to pass this to linker. +# -Map: create map file +# --cref: add cross reference to map file +LDFLAGS = -nostartfiles -Wl,-Map=$(OUTDIR)/$(TARGET).map,--cref,--gc-sections +ifeq ($(DEBUG),NO) +LDFLAGS += -Wl,-static -Wl,-s +endif +LDFLAGS += $(patsubst %,-L%,$(EXTRA_LIBDIRS)) +LDFLAGS += -lc +LDFLAGS += $(patsubst %,-l%,$(EXTRA_LIBS)) +LDFLAGS += $(MATH_LIB) +LDFLAGS += -lc -lgcc + +# Set linker-script name depending on selected submodel name +LDFLAGS +=-T$(LINKERSCRIPTPATH)/link_stm32f10x_$(MODEL).ld + +# --------------------------------------------------------------------------- +# Options for OpenOCD flash-programming +# see openocd.pdf/openocd.texi for further information +# +OOCD_LOADFILE+=$(OUTDIR)/$(TARGET).elf +# if OpenOCD is in the $PATH just set OOCD_EXE=openocd +OOCD_EXE=openocd +# debug level +OOCD_CL=-d0 +# interface and board/target settings (using the OOCD target-library here) +UNAME := $(shell uname) +ifeq ($(UNAME), Darwin) + OOCD_CL+=-f ../../Project/OpenOCD/floss-jtag.openpilot.osx.cfg -f ../../Project/OpenOCD/stm32.cfg +else + OOCD_CL+=-f ../../Project/OpenOCD/floss-jtag.openpilot.cfg -f ../../Project/OpenOCD/stm32.cfg +endif +# initialize +OOCD_CL+=-c init +# show the targets +OOCD_CL+=-c targets +# commands to prepare flash-write +OOCD_CL+= -c "reset halt" +# flash erase +OOCD_CL+=-c "stm32x mass_erase 0" +# flash-write +OOCD_CL+=-c "flash write_image $(OOCD_LOADFILE)" +# Verify +OOCD_CL+=-c "verify_image $(OOCD_LOADFILE)" +# reset target +OOCD_CL+=-c "reset run" +# terminate OOCD after programming +OOCD_CL+=-c shutdown +# --------------------------------------------------------------------------- + + +# Define programs and commands. +CC = $(TCHAIN_PREFIX)gcc +CPP = $(TCHAIN_PREFIX)g++ +AR = $(TCHAIN_PREFIX)ar +OBJCOPY = $(TCHAIN_PREFIX)objcopy +OBJDUMP = $(TCHAIN_PREFIX)objdump +SIZE = $(TCHAIN_PREFIX)size +NM = $(TCHAIN_PREFIX)nm +REMOVE = $(REMOVE_CMD) -f +###SHELL = sh +###COPY = cp + + + +# Define Messages +# English +MSG_ERRORS_NONE = Errors: none +MSG_BEGIN = ${quote}-------- begin (mode: $(RUN_MODE)) --------${quote} +MSG_END = ${quote}-------- end --------${quote} +MSG_MODINIT = ${quote}**** Generating ModInit.c${quote} +MSG_SIZE_BEFORE = ${quote}Size before:${quote} +MSG_SIZE_AFTER = ${quote}Size after build:${quote} +MSG_LOAD_FILE = ${quote}Creating load file:${quote} +MSG_EXTENDED_LISTING = ${quote}Creating Extended Listing/Disassembly:${quote} +MSG_SYMBOL_TABLE = ${quote}Creating Symbol Table:${quote} +MSG_LINKING = ${quote}**** Linking :${quote} +MSG_COMPILING = ${quote}**** Compiling C :${quote} +MSG_COMPILING_ARM = ${quote}**** Compiling C (ARM-only):${quote} +MSG_COMPILINGCPP = ${quote}Compiling C++ :${quote} +MSG_COMPILINGCPP_ARM = ${quote}Compiling C++ (ARM-only):${quote} +MSG_ASSEMBLING = ${quote}**** Assembling:${quote} +MSG_ASSEMBLING_ARM = ${quote}****Assembling (ARM-only):${quote} +MSG_CLEANING = ${quote}Cleaning project:${quote} +MSG_FORMATERROR = ${quote}Can not handle output-format${quote} +MSG_ASMFROMC = ${quote}Creating asm-File from C-Source:${quote} +MSG_ASMFROMC_ARM = ${quote}Creating asm-File from C-Source (ARM-only):${quote} + +# List of all source files. +ALLSRC = $(ASRCARM) $(ASRC) $(SRCARM) $(SRC) $(CPPSRCARM) $(CPPSRC) +# List of all source files without directory and file-extension. +ALLSRCBASE = $(notdir $(basename $(ALLSRC))) + +# Define all object files. +ALLOBJ = $(addprefix $(OUTDIR)/, $(addsuffix .o, $(ALLSRCBASE))) + +# Define all listing files (used for make clean). +LSTFILES = $(addprefix $(OUTDIR)/, $(addsuffix .lst, $(ALLSRCBASE))) +# Define all depedency-files (used for make clean). +DEPFILES = $(addprefix $(OUTDIR)/dep/, $(addsuffix .o.d, $(ALLSRCBASE))) + +elf: $(OUTDIR)/$(TARGET).elf +lss: $(OUTDIR)/$(TARGET).lss +sym: $(OUTDIR)/$(TARGET).sym +hex: $(OUTDIR)/$(TARGET).hex +bin: $(OUTDIR)/$(TARGET).bin + +# Default target. +#all: begin gccversion sizebefore build sizeafter finished end +all: begin gccversion build sizeafter finished end + +ifeq ($(LOADFORMAT),ihex) +build: elf hex sym +else +ifeq ($(LOADFORMAT),binary) +build: elf bin sym +else +ifeq ($(LOADFORMAT),both) +build: elf hex bin sym +else +$(error "$(MSG_FORMATERROR) $(FORMAT)") +endif +endif +endif + +# Test if quotes are needed for the echo-command +result = ${shell echo "test"} +ifeq (${result}, test) + quote = ' +else + quote = +endif + +${OUTDIR}/InitMods.c: Makefile + @echo ${MSG_MODINIT} + @echo ${quote}// Autogenerated file${quote} > ${OUTDIR}/InitMods.c + @echo ${quote}${foreach MOD, ${MODNAMES}, extern unsigned int ${MOD}Initialize(void);}${quote} >> ${OUTDIR}/InitMods.c + @echo ${quote}void InitModules() {${quote} >> ${OUTDIR}/InitMods.c + @echo ${quote}${foreach MOD, ${MODNAMES}, ${MOD}Initialize();}${quote} >> ${OUTDIR}/InitMods.c + @echo ${quote}}${quote} >> ${OUTDIR}/InitMods.c + +# Eye candy. +begin: +## @echo + @echo $(MSG_BEGIN) + +finished: +## @echo $(MSG_ERRORS_NONE) + +end: + @echo $(MSG_END) +## @echo + +# Display sizes of sections. +ELFSIZE = $(SIZE) -A $(OUTDIR)/$(TARGET).elf +#ELFSIZE = $(SIZE) -B -t $(ALLOBJ) $(OUTDIR)/$(TARGET).elf +##ELFSIZE = $(SIZE) --format=Berkeley --common $(OUTDIR)/$(TARGET).elf +sizebefore: +# @if [ -f $(OUTDIR)/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_BEFORE); $(ELFSIZE); echo; fi + +sizeafter: +# @if [ -f $(OUTDIR)/$(TARGET).elf ]; then echo; echo $(MSG_SIZE_AFTER); $(ELFSIZE); echo; fi + @echo $(MSG_SIZE_AFTER) + $(ELFSIZE) + +# Display compiler version information. +gccversion : + @$(CC) --version +# @echo $(ALLOBJ) + +# Program the device. +ifeq ($(FLASH_TOOL),OPENOCD) +# Program the device with Dominic Rath's OPENOCD in "batch-mode", needs cfg and "reset-script". +program: $(OUTDIR)/$(TARGET).elf + @echo ${quote}Programming with OPENOCD${quote} + $(OOCD_EXE) $(OOCD_CL) +endif + +# Create final output file (.hex) from ELF output file. +%.hex: %.elf +## @echo + @echo $(MSG_LOAD_FILE) $@ + $(OBJCOPY) -O ihex $< $@ + +# Create final output file (.bin) from ELF output file. +%.bin: %.elf +## @echo + @echo $(MSG_LOAD_FILE) $@ + $(OBJCOPY) -O binary $< $@ + +# Create extended listing file/disassambly from ELF output file. +# using objdump testing: option -C +%.lss: %.elf +## @echo + @echo $(MSG_EXTENDED_LISTING) $@ + $(OBJDUMP) -h -S -C -r $< > $@ +# $(OBJDUMP) -x -S $< > $@ + +# 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 : $(ALLOBJ) +%.elf: $(ALLOBJ) + @echo $(MSG_LINKING) $@ +# use $(CC) for C-only projects or $(CPP) for C++-projects: + $(CC) $(THUMB) $(CFLAGS) $(ALLOBJ) --output $@ $(LDFLAGS) +# $(CPP) $(THUMB) $(CFLAGS) $(ALLOBJ) --output $@ $(LDFLAGS) + + +# Assemble: create object files from assembler source files. +define ASSEMBLE_TEMPLATE +$(OUTDIR)/$(notdir $(basename $(1))).o : $(1) +## @echo + @echo $(MSG_ASSEMBLING) $$< to $$@ + $(CC) -c $(THUMB) $$(ASFLAGS) $$< -o $$@ +endef +$(foreach src, $(ASRC), $(eval $(call ASSEMBLE_TEMPLATE, $(src)))) + +# Assemble: create object files from assembler source files. ARM-only +define ASSEMBLE_ARM_TEMPLATE +$(OUTDIR)/$(notdir $(basename $(1))).o : $(1) +## @echo + @echo $(MSG_ASSEMBLING_ARM) $$< to $$@ + $(CC) -c $$(ASFLAGS) $$< -o $$@ +endef +$(foreach src, $(ASRCARM), $(eval $(call ASSEMBLE_ARM_TEMPLATE, $(src)))) + + +# Compile: create object files from C source files. +define COMPILE_C_TEMPLATE +$(OUTDIR)/$(notdir $(basename $(1))).o : $(1) +## @echo + @echo $(MSG_COMPILING) $$< to $$@ + $(CC) -c $(THUMB) $$(CFLAGS) $$(CONLYFLAGS) $$< -o $$@ +endef +$(foreach src, $(SRC), $(eval $(call COMPILE_C_TEMPLATE, $(src)))) + +# Compile: create object files from C source files. ARM-only +define COMPILE_C_ARM_TEMPLATE +$(OUTDIR)/$(notdir $(basename $(1))).o : $(1) +## @echo + @echo $(MSG_COMPILING_ARM) $$< to $$@ + $(CC) -c $$(CFLAGS) $$(CONLYFLAGS) $$< -o $$@ +endef +$(foreach src, $(SRCARM), $(eval $(call COMPILE_C_ARM_TEMPLATE, $(src)))) + + +# Compile: create object files from C++ source files. +define COMPILE_CPP_TEMPLATE +$(OUTDIR)/$(notdir $(basename $(1))).o : $(1) +## @echo + @echo $(MSG_COMPILINGCPP) $$< to $$@ + $(CC) -c $(THUMB) $$(CFLAGS) $$(CPPFLAGS) $$< -o $$@ +endef +$(foreach src, $(CPPSRC), $(eval $(call COMPILE_CPP_TEMPLATE, $(src)))) + +# Compile: create object files from C++ source files. ARM-only +define COMPILE_CPP_ARM_TEMPLATE +$(OUTDIR)/$(notdir $(basename $(1))).o : $(1) +## @echo + @echo $(MSG_COMPILINGCPP_ARM) $$< to $$@ + $(CC) -c $$(CFLAGS) $$(CPPFLAGS) $$< -o $$@ +endef +$(foreach src, $(CPPSRCARM), $(eval $(call COMPILE_CPP_ARM_TEMPLATE, $(src)))) + + +# Compile: create assembler files from C source files. ARM/Thumb +$(SRC:.c=.s) : %.s : %.c + @echo $(MSG_ASMFROMC) $< to $@ + $(CC) $(THUMB) -S $(CFLAGS) $(CONLYFLAGS) $< -o $@ + +# Compile: create assembler files from C source files. ARM only +$(SRCARM:.c=.s) : %.s : %.c + @echo $(MSG_ASMFROMC_ARM) $< to $@ + $(CC) -S $(CFLAGS) $(CONLYFLAGS) $< -o $@ + +# Generate Doxygen documents +docs: + doxygen $(DOXYGENDIR)/doxygen.cfg + +# Target: clean project. +clean: begin clean_list finished end + +clean_list : +## @echo + @echo $(MSG_CLEANING) + $(REMOVE) $(OUTDIR)/$(TARGET).map + $(REMOVE) $(OUTDIR)/$(TARGET).elf + $(REMOVE) $(OUTDIR)/$(TARGET).hex + $(REMOVE) $(OUTDIR)/$(TARGET).bin + $(REMOVE) $(OUTDIR)/$(TARGET).sym + $(REMOVE) $(OUTDIR)/$(TARGET).lss + $(REMOVE) $(ALLOBJ) + $(REMOVE) $(LSTFILES) + $(REMOVE) $(DEPFILES) + $(REMOVE) $(SRC:.c=.s) + $(REMOVE) $(SRCARM:.c=.s) + $(REMOVE) $(CPPSRC:.cpp=.s) + $(REMOVE) $(CPPSRCARM:.cpp=.s) + + +# Create output files directory +# all known MS Windows OS define the ComSpec environment variable +ifdef ComSpec +$(shell md $(OUTDIR) 2>NUL) +else +$(shell mkdir $(OUTDIR) 2>/dev/null) +endif + +# Include the dependency files. +ifdef ComSpec +-include $(shell md $(OUTDIR)\dep 2>NUL) $(wildcard $(OUTDIR)/dep/*) +else +-include $(shell mkdir $(OUTDIR) 2>/dev/null) $(shell mkdir $(OUTDIR)/dep 2>/dev/null) $(wildcard $(OUTDIR)/dep/*) +endif + + + +# Listing of phony targets. +.PHONY : all begin finish end sizebefore sizeafter gccversion \ +build elf hex bin lss sym clean clean_list program + diff --git a/flight/Bootloaders/PipXtreme/inc/README b/flight/Bootloaders/PipXtreme/inc/README new file mode 100644 index 000000000..5250576d5 --- /dev/null +++ b/flight/Bootloaders/PipXtreme/inc/README @@ -0,0 +1,3 @@ +pios_board.h should be the same as the pios_board.h of the PipX main FW, +and should be migrated to a PipX include in the main PiOS directory once +we move this to the main SVN. diff --git a/flight/Bootloaders/PipXtreme/inc/common.h b/flight/Bootloaders/PipXtreme/inc/common.h new file mode 100644 index 000000000..e1194a01e --- /dev/null +++ b/flight/Bootloaders/PipXtreme/inc/common.h @@ -0,0 +1,115 @@ +/** + ****************************************************************************** + * @addtogroup OpenPilotBL OpenPilot BootLoader + * @brief These files contain the code to the OpenPilot MB Bootloader. + * + * @{ + * @file common.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief This file contains various common defines for the BootLoader + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#ifndef COMMON_H_ +#define COMMON_H_ + +//#include "board.h" + +typedef enum { + start, keepgoing, +} DownloadAction; + +/**************************************************/ +/* OP_DFU states */ +/**************************************************/ + +typedef enum { + DFUidle, //0 + uploading, //1 + wrong_packet_received, //2 + too_many_packets, //3 + too_few_packets, //4 + Last_operation_Success, //5 + downloading, //6 + BLidle, //7 + Last_operation_failed, //8 + uploadingStarting, //9 + outsideDevCapabilities, //10 + CRC_Fail,//11 + failed_jump,//12 +} DFUStates; +/**************************************************/ +/* OP_DFU commands */ +/**************************************************/ +typedef enum { + Reserved, //0 + Req_Capabilities, //1 + Rep_Capabilities, //2 + EnterDFU, //3 + JumpFW, //4 + Reset, //5 + Abort_Operation, //6 + Upload, //7 + Op_END, //8 + Download_Req, //9 + Download, //10 + Status_Request, //11 + Status_Rep +//12 +} DFUCommands; + +typedef enum { + High_Density, + Medium_Density +}DeviceType; +/**************************************************/ +/* OP_DFU transfer types */ +/**************************************************/ +typedef enum { + FW, //0 + Descript +//2 +} DFUTransfer; +/**************************************************/ +/* OP_DFU transfer port */ +/**************************************************/ +typedef enum { + Usb, //0 + Serial +//2 +} DFUPort; +/**************************************************/ +/* OP_DFU programable programable HW types */ +/**************************************************/ +typedef enum { + Self_flash, //0 + Remote_flash_via_spi +//1 +} DFUProgType; +/**************************************************/ +/* OP_DFU programable sources */ +/**************************************************/ +#define USB 0 +#define SPI 1 + +#define DownloadDelay 100000 + +#define MAX_DEL_RETRYS 3 +#define MAX_WRI_RETRYS 3 + +#endif /* COMMON_H_ */ diff --git a/flight/Bootloaders/PipXtreme/inc/op_dfu.h b/flight/Bootloaders/PipXtreme/inc/op_dfu.h new file mode 100644 index 000000000..fd77afa03 --- /dev/null +++ b/flight/Bootloaders/PipXtreme/inc/op_dfu.h @@ -0,0 +1,61 @@ + /** + ****************************************************************************** + * + * @file op_dfu.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __OP_DFU_H +#define __OP_DFU_H +#include "common.h" +/* Includes ------------------------------------------------------------------*/ +/* Exported types ------------------------------------------------------------*/ +typedef struct +{ + uint8_t programmingType; + uint8_t readWriteFlags; + uint32_t startOfUserCode; + uint32_t sizeOfCode; + uint8_t sizeOfDescription; + uint8_t BL_Version; + uint8_t devID; + DeviceType devType; + uint32_t FW_Crc; +}Device; + +/* Exported constants --------------------------------------------------------*/ +/* Exported macro ------------------------------------------------------------*/ +/* Exported define -----------------------------------------------------------*/ +#define COMMAND 0 +#define COUNT 1 +#define DATA 5 + +/* Exported functions ------------------------------------------------------- */ +void processComand(uint8_t *Receive_Buffer); +uint32_t baseOfAdressType(uint8_t type); +uint8_t isBiggerThanAvailable(uint8_t type, uint32_t size); +void OPDfuIni(uint8_t discover); +void DataDownload(DownloadAction); +bool flash_read(uint8_t * buffer, uint32_t adr, DFUProgType type); +#endif /* __OP_DFU_H */ + +/******************* (C) COPYRIGHT 2010 STMicroelectronics *****END OF FILE****/ diff --git a/flight/Bootloaders/PipXtreme/inc/pios_board.h b/flight/Bootloaders/PipXtreme/inc/pios_board.h new file mode 100644 index 000000000..8c2d05bb9 --- /dev/null +++ b/flight/Bootloaders/PipXtreme/inc/pios_board.h @@ -0,0 +1,384 @@ + /** + ****************************************************************************** + * + * @file pios_board.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Defines board hardware for the OpenPilot Version 1.1 hardware. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef PIOS_BOARD_H +#define PIOS_BOARD_H + +// ***************************************************************** +// Timers and Channels Used + +/* +Timer | Channel 1 | Channel 2 | Channel 3 | Channel 4 +------+-----------+-----------+-----------+---------- +TIM1 | | | | +TIM2 | --------------- DELAY ---------------------- +TIM3 | --------------- Timer Interrupt ------------ +TIM4 | --------------- STOPWATCH ------------------ +TIM5 | | | | +TIM6 | | | | +TIM7 | | | | +TIM8 | | | | +------+-----------+-----------+-----------+---------- +*/ + +//------------------------ +// DMA Channels Used +//------------------------ +/* Channel 1 - */ +/* Channel 2 - */ +/* Channel 3 - */ +/* Channel 4 - */ +/* Channel 5 - */ +/* Channel 6 - */ +/* Channel 7 - */ +/* Channel 8 - */ +/* Channel 9 - */ +/* Channel 10 - */ +/* Channel 11 - */ +/* Channel 12 - */ + +//------------------------ +// BOOTLOADER_SETTINGS +//------------------------ +#define FUNC_ID 3 +#define HW_VERSION 21 +#define BOOTLOADER_VERSION 0 +#define MEM_SIZE 0x20000 //128K +#define SIZE_OF_DESCRIPTION 100 +#define START_OF_USER_CODE (uint32_t)0x08002000 +#define SIZE_OF_CODE (uint32_t)(MEM_SIZE-(START_OF_USER_CODE-0x08000000)-SIZE_OF_DESCRIPTION) +#ifdef STM32F10X_HD + #define HW_TYPE 0 //0=high_density 1=medium_density; +#elif STM32F10X_MD + #define HW_TYPE 1 //0=high_density 1=medium_density; +#endif +#define BOARD_READABLE TRUE +#define BOARD_WRITABLA TRUE +#define MAX_DEL_RETRYS 3 + + +// ***************************************************************** +// System Settings + +#define PIOS_MASTER_CLOCK 72000000ul +#define PIOS_PERIPHERAL_CLOCK (PIOS_MASTER_CLOCK / 2) + +#if defined(USE_BOOTLOADER) + #define PIOS_NVIC_VECTTAB_FLASH (START_OF_USER_CODE) +#else + #define PIOS_NVIC_VECTTAB_FLASH ((uint32_t)0x08000000) +#endif + +// ***************************************************************** +// Interrupt Priorities + +#define PIOS_IRQ_PRIO_LOW 12 // lower than RTOS +#define PIOS_IRQ_PRIO_MID 8 // higher than RTOS +#define PIOS_IRQ_PRIO_HIGH 5 // for SPI, ADC, I2C etc... +#define PIOS_IRQ_PRIO_HIGHEST 4 // for USART etc... + +// ***************************************************************** +// PIOS_LED + +#define PIOS_LED_LED1_GPIO_PORT GPIOA // USB Activity LED +#define PIOS_LED_LED1_GPIO_PIN GPIO_Pin_3 +#define PIOS_LED_LED1_GPIO_CLK RCC_APB2Periph_GPIOA + +#define PIOS_LED_LED2_GPIO_PORT GPIOB // LINK LED +#define PIOS_LED_LED2_GPIO_PIN GPIO_Pin_5 +#define PIOS_LED_LED2_GPIO_CLK RCC_APB2Periph_GPIOB + +#define PIOS_LED_LED3_GPIO_PORT GPIOB // RX LED +#define PIOS_LED_LED3_GPIO_PIN GPIO_Pin_6 +#define PIOS_LED_LED3_GPIO_CLK RCC_APB2Periph_GPIOB + +#define PIOS_LED_LED4_GPIO_PORT GPIOB // TX LED +#define PIOS_LED_LED4_GPIO_PIN GPIO_Pin_7 +#define PIOS_LED_LED4_GPIO_CLK RCC_APB2Periph_GPIOB + +#define PIOS_LED_NUM 4 +#define PIOS_LED_PORTS { PIOS_LED_LED1_GPIO_PORT, PIOS_LED_LED2_GPIO_PORT, PIOS_LED_LED3_GPIO_PORT, PIOS_LED_LED4_GPIO_PORT } +#define PIOS_LED_PINS { PIOS_LED_LED1_GPIO_PIN, PIOS_LED_LED2_GPIO_PIN, PIOS_LED_LED3_GPIO_PIN, PIOS_LED_LED4_GPIO_PIN } +#define PIOS_LED_CLKS { PIOS_LED_LED1_GPIO_CLK, PIOS_LED_LED2_GPIO_CLK, PIOS_LED_LED3_GPIO_CLK, PIOS_LED_LED4_GPIO_CLK } + +#define USB_LED_ON PIOS_LED_On(LED1) +#define USB_LED_OFF PIOS_LED_Off(LED1) +#define USB_LED_TOGGLE PIOS_LED_Toggle(LED1) + +#define LINK_LED_ON PIOS_LED_On(LED2) +#define LINK_LED_OFF PIOS_LED_Off(LED2) +#define LINK_LED_TOGGLE PIOS_LED_Toggle(LED2) + +#define RX_LED_ON PIOS_LED_On(LED3) +#define RX_LED_OFF PIOS_LED_Off(LED3) +#define RX_LED_TOGGLE PIOS_LED_Toggle(LED3) + +#define TX_LED_ON PIOS_LED_On(LED4) +#define TX_LED_OFF PIOS_LED_Off(LED4) +#define TX_LED_TOGGLE PIOS_LED_Toggle(LED4) + +// ***************************************************************** +// Delay Timer + +#define PIOS_DELAY_TIMER TIM2 +#define PIOS_DELAY_TIMER_RCC_FUNC RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE) + +// ***************************************************************** +// Timer interrupt + +#define TIMER_INT_TIMER TIM3 +#define TIMER_INT_FUNC TIM3_IRQHandler +#define TIMER_INT_PRIORITY 2 + +// ***************************************************************** +// Stop watch timer + +#define STOPWATCH_TIMER TIM4 + +// ***************************************************************** +// SPI +// +// See also pios_board.c + +#define PIOS_SPI_PORT 0 + +// ***************************************************************** +// PIOS_USART + +#define PIOS_USART_RX_BUFFER_SIZE 512 +#define PIOS_USART_TX_BUFFER_SIZE 512 + +#define PIOS_COM_SERIAL 0 +//#define PIOS_COM_DEBUG PIOS_COM_SERIAL // comment this out if you don't want debug text sent out on the serial port + +#define PIOS_USART_BAUDRATE 57600 +//#define PIOS_USART_BAUDRATE 115200 + +#if defined(PIOS_INCLUDE_USB_HID) + #define PIOS_COM_TELEM_USB 1 +#endif + +#if defined(PIOS_COM_DEBUG) + #define DEBUG_PRINTF(...) PIOS_COM_SendFormattedString(PIOS_COM_DEBUG, __VA_ARGS__) +// #define DEBUG_PRINTF(...) PIOS_COM_SendFormattedStringNonBlocking(PIOS_COM_DEBUG, __VA_ARGS__) +#else + #define DEBUG_PRINTF(...) +#endif + +// ***************************************************************** +// ADC + +// PIOS_ADC_PinGet(0) = Temperature Sensor (On-board) +// PIOS_ADC_PinGet(1) = PSU Voltage + +#define PIOS_ADC_OVERSAMPLING_RATE 2 + +#define PIOS_ADC_USE_TEMP_SENSOR 1 +#define PIOS_ADC_TEMP_SENSOR_ADC ADC1 +#define PIOS_ADC_TEMP_SENSOR_ADC_CHANNEL 16 // Temperature sensor channel +//#define PIOS_ADC_TEMP_SENSOR_ADC_CHANNEL 17 // VREF channel + +#define PIOS_ADC_PIN1_GPIO_PORT GPIOB // Port B (PSU Voltage) +#define PIOS_ADC_PIN1_GPIO_PIN GPIO_Pin_1 // PB1 .. ADC12_IN9 +#define PIOS_ADC_PIN1_GPIO_CHANNEL ADC_Channel_9 +#define PIOS_ADC_PIN1_ADC ADC2 +#define PIOS_ADC_PIN1_ADC_NUMBER 1 + +#define PIOS_ADC_NUM_PINS 1 + +#define PIOS_ADC_PORTS { PIOS_ADC_PIN1_GPIO_PORT } +#define PIOS_ADC_PINS { PIOS_ADC_PIN1_GPIO_PIN } +#define PIOS_ADC_CHANNELS { PIOS_ADC_PIN1_GPIO_CHANNEL } +#define PIOS_ADC_MAPPING { PIOS_ADC_PIN1_ADC } +#define PIOS_ADC_CHANNEL_MAPPING { PIOS_ADC_PIN1_ADC_NUMBER } + +#define PIOS_ADC_NUM_CHANNELS (PIOS_ADC_NUM_PINS + PIOS_ADC_USE_TEMP_SENSOR) +#define PIOS_ADC_NUM_ADC_CHANNELS 2 +#define PIOS_ADC_USE_ADC2 1 +#define PIOS_ADC_CLOCK_FUNCTION RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_ADC2, ENABLE) +//#define PIOS_ADC_ADCCLK RCC_PCLK2_Div2 // ADC clock = PCLK2/2 +//#define PIOS_ADC_ADCCLK RCC_PCLK2_Div4 // ADC clock = PCLK2/4 +//#define PIOS_ADC_ADCCLK RCC_PCLK2_Div6 // ADC clock = PCLK2/6 +#define PIOS_ADC_ADCCLK RCC_PCLK2_Div8 // ADC clock = PCLK2/8 +//#define PIOS_ADC_SAMPLE_TIME ADC_SampleTime_1Cycles5 +//#define PIOS_ADC_SAMPLE_TIME ADC_SampleTime_7Cycles5 +//#define PIOS_ADC_SAMPLE_TIME ADC_SampleTime_13Cycles5 +//#define PIOS_ADC_SAMPLE_TIME ADC_SampleTime_28Cycles5 +//#define PIOS_ADC_SAMPLE_TIME ADC_SampleTime_41Cycles5 +//#define PIOS_ADC_SAMPLE_TIME ADC_SampleTime_55Cycles5 +//#define PIOS_ADC_SAMPLE_TIME ADC_SampleTime_71Cycles5 +#define PIOS_ADC_SAMPLE_TIME ADC_SampleTime_239Cycles5 + /* Sample time: */ + /* With an ADCCLK = 14 MHz and a sampling time of 293.5 cycles: */ + /* Tconv = 239.5 + 12.5 = 252 cycles = 18�s */ + /* (1 / (ADCCLK / CYCLES)) = Sample Time (�S) */ +#define PIOS_ADC_IRQ_PRIO 3 + +// ***************************************************************** +// GPIO output pins + +// GPIO_Mode_Out_OD Open Drain Output +// GPIO_Mode_Out_PP Push-Pull Output +// GPIO_Mode_AF_OD Open Drain Output Alternate-Function +// GPIO_Mode_AF_PP Push-Pull Output Alternate-Function + +// Serial port RTS line +#define PIOS_GPIO_OUT_0_PORT GPIOB +#define PIOS_GPIO_OUT_0_PIN GPIO_Pin_15 +#define PIOS_GPIO_OUT_0_GPIO_CLK RCC_APB2Periph_GPIOB + +// RF module chip-select line +#define PIOS_GPIO_OUT_1_PORT GPIOA +#define PIOS_GPIO_OUT_1_PIN GPIO_Pin_4 +#define PIOS_GPIO_OUT_1_GPIO_CLK RCC_APB2Periph_GPIOA + +// EEPROM chip-select line +#define PIOS_GPIO_OUT_2_PORT GPIOA +#define PIOS_GPIO_OUT_2_PIN GPIO_Pin_1 +#define PIOS_GPIO_OUT_2_GPIO_CLK RCC_APB2Periph_GPIOA +/* +// SPI-1 SCK line +#define PIOS_GPIO_OUT_3_PORT GPIOA +#define PIOS_GPIO_OUT_3_PIN GPIO_Pin_5 +#define PIOS_GPIO_OUT_3_GPIO_CLK RCC_APB2Periph_GPIOA + +// SPI-1 MOSI line +#define PIOS_GPIO_OUT_4_PORT GPIOA +#define PIOS_GPIO_OUT_4_PIN GPIO_Pin_7 +#define PIOS_GPIO_OUT_4_GPIO_CLK RCC_APB2Periph_GPIOA +*/ +#define PIOS_GPIO_NUM 3 +#define PIOS_GPIO_PORTS { PIOS_GPIO_OUT_0_PORT, PIOS_GPIO_OUT_1_PORT, PIOS_GPIO_OUT_2_PORT, } +#define PIOS_GPIO_PINS { PIOS_GPIO_OUT_0_PIN, PIOS_GPIO_OUT_1_PIN, PIOS_GPIO_OUT_2_PIN, } +#define PIOS_GPIO_CLKS { PIOS_GPIO_OUT_0_GPIO_CLK, PIOS_GPIO_OUT_1_GPIO_CLK, PIOS_GPIO_OUT_2_GPIO_CLK } + +#define SERIAL_RTS_ENABLE PIOS_GPIO_Enable(0) +#define SERIAL_RTS_SET PIOS_GPIO_Off(0) +#define SERIAL_RTS_CLEAR PIOS_GPIO_On(0) + +#define RF_CS_ENABLE PIOS_GPIO_Enable(1) +#define RF_CS_HIGH PIOS_GPIO_Off(1) +#define RF_CS_LOW PIOS_GPIO_On(1) + +#define EE_CS_ENABLE PIOS_GPIO_Enable(2) +#define EE_CS_HIGH PIOS_GPIO_Off(2) +#define EE_CS_LOW PIOS_GPIO_On(2) +/* +#define SPI1_SCK_ENABLE PIOS_GPIO_Enable(3) +#define SPI1_SCK_HIGH PIOS_GPIO_Off(3) +#define SPI1_SCK_LOW PIOS_GPIO_On(3) + +#define SPI1_MOSI_ENABLE PIOS_GPIO_Enable(4) +#define SPI1_MOSI_HIGH PIOS_GPIO_Off(4) +#define SPI1_MOSI_LOW PIOS_GPIO_On(4) +*/ +// ***************************************************************** +// GPIO input pins + +// GPIO_Mode_AIN Analog Input +// GPIO_Mode_IN_FLOATING Input Floating +// GPIO_Mode_IPD Input Pull-Down +// GPIO_Mode_IPU Input Pull-up + +// API mode line +#define GPIO_IN_0_PORT GPIOB +#define GPIO_IN_0_PIN GPIO_Pin_13 +#define GPIO_IN_0_MODE GPIO_Mode_IPU + +// Serial port CTS line +#define GPIO_IN_1_PORT GPIOB +#define GPIO_IN_1_PIN GPIO_Pin_14 +#define GPIO_IN_1_MODE GPIO_Mode_IPU + +// VBUS sense line +#define GPIO_IN_2_PORT GPIOA +#define GPIO_IN_2_PIN GPIO_Pin_8 +#define GPIO_IN_2_MODE GPIO_Mode_IN_FLOATING + +// 868MHz jumper option +#define GPIO_IN_3_PORT GPIOB +#define GPIO_IN_3_PIN GPIO_Pin_8 +#define GPIO_IN_3_MODE GPIO_Mode_IPU + +// 915MHz jumper option +#define GPIO_IN_4_PORT GPIOB +#define GPIO_IN_4_PIN GPIO_Pin_9 +#define GPIO_IN_4_MODE GPIO_Mode_IPU + +// RF INT line +#define GPIO_IN_5_PORT GPIOA +#define GPIO_IN_5_PIN GPIO_Pin_2 +#define GPIO_IN_5_MODE GPIO_Mode_IN_FLOATING + +// RF misc line +#define GPIO_IN_6_PORT GPIOB +#define GPIO_IN_6_PIN GPIO_Pin_0 +#define GPIO_IN_6_MODE GPIO_Mode_IN_FLOATING + +#define GPIO_IN_NUM 7 +#define GPIO_IN_PORTS { GPIO_IN_0_PORT, GPIO_IN_1_PORT, GPIO_IN_2_PORT, GPIO_IN_3_PORT, GPIO_IN_4_PORT, GPIO_IN_5_PORT, GPIO_IN_6_PORT } +#define GPIO_IN_PINS { GPIO_IN_0_PIN, GPIO_IN_1_PIN, GPIO_IN_2_PIN, GPIO_IN_3_PIN, GPIO_IN_4_PIN, GPIO_IN_5_PIN, GPIO_IN_6_PIN } +#define GPIO_IN_MODES { GPIO_IN_0_MODE, GPIO_IN_1_MODE, GPIO_IN_2_MODE, GPIO_IN_3_MODE, GPIO_IN_4_MODE, GPIO_IN_5_MODE, GPIO_IN_6_MODE } + +#define API_MODE_PIN 0 +#define SERIAL_CTS_PIN 1 +#define VBUS_SENSE_PIN 2 +#define _868MHz_PIN 3 +#define _915MHz_PIN 4 +#define RF_INT_PIN 5 +#define RF_MISC_PIN 6 + +// ***************************************************************** +// USB + +#if defined(PIOS_INCLUDE_USB_HID) + #define PIOS_USB_ENABLED 1 + #define PIOS_USB_DETECT_GPIO_PORT GPIO_IN_2_PORT + #define PIOS_USB_DETECT_GPIO_PIN GPIO_IN_2_PIN + #define PIOS_USB_DETECT_EXTI_LINE EXTI_Line4 + #define PIOS_IRQ_USB_PRIORITY 8 +#endif + +// ***************************************************************** +// RFM22 + +//#define RFM22_EXT_INT_USE + +#define RFM22_PIOS_SPI PIOS_SPI_PORT // SPIx + +#if defined(RFM22_EXT_INT_USE) + #define RFM22_EXT_INT_PORT_SOURCE GPIO_PortSourceGPIOA + #define RFM22_EXT_INT_PIN_SOURCE GPIO_PinSource2 + + #define RFM22_EXT_INT_LINE EXTI_Line2 + #define RFM22_EXT_INT_IRQn EXTI2_IRQn + #define RFM22_EXT_INT_FUNC EXTI2_IRQHandler + + #define RFM22_EXT_INT_PRIORITY 1 +#endif + +// ***************************************************************** + +#endif /* PIOS_BOARD_H */ diff --git a/flight/Bootloaders/PipXtreme/inc/pios_config.h b/flight/Bootloaders/PipXtreme/inc/pios_config.h new file mode 100644 index 000000000..8202c3163 --- /dev/null +++ b/flight/Bootloaders/PipXtreme/inc/pios_config.h @@ -0,0 +1,67 @@ +/** + ****************************************************************************** + * @addtogroup OpenPilotBL OpenPilot BootLoader + * @{ + * @file pios_config.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief PiOS configuration header. + * Central compile time config for the project. + * In particular, pios_config.h is where you define which PiOS libraries + * and features are included in the firmware. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + + +#ifndef PIOS_CONFIG_H +#define PIOS_CONFIG_H +#define PIOS_INCLUDE_BL_HELPER +#define USB_HID +/* Enable/Disable PiOS Modules */ +#define PIOS_INCLUDE_DELAY +#define PIOS_INCLUDE_IRQ +#define PIOS_INCLUDE_LED +//#define PIOS_INCLUDE_SPI +#define PIOS_INCLUDE_SYS +#define PIOS_INCLUDE_USART +#define PIOS_INCLUDE_USB_HID +#define PIOS_INCLUDE_OPAHRS +#define PIOS_INCLUDE_COM +#define PIOS_INCLUDE_GPIO +#define PIOS_NO_GPS +//#define DEBUG_SSP + +/* Defaults for Logging */ +#define LOG_FILENAME "PIOS.LOG" +#define STARTUP_LOG_ENABLED 1 + +/* COM Module */ +#define GPS_BAUDRATE 19200 +#define TELEM_BAUDRATE 19200 +#define AUXUART_ENABLED 0 +#define AUXUART_BAUDRATE 19200 + +/* Servos */ +#define SERVOS_POSITION_MIN 800 +#define SERVOS_POSITION_MAX 2200 + +#endif /* PIOS_CONFIG_H */ +/** + * @} + * @} + */ diff --git a/flight/Bootloaders/PipXtreme/inc/stopwatch.h b/flight/Bootloaders/PipXtreme/inc/stopwatch.h new file mode 100644 index 000000000..5c60c186a --- /dev/null +++ b/flight/Bootloaders/PipXtreme/inc/stopwatch.h @@ -0,0 +1,55 @@ +/** + ****************************************************************************** + * @addtogroup OpenPilotBL OpenPilot BootLoader + * @{ + * + * @file stopwatch.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Timer functions for the LED PWM. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef _STOPWATCH_H +#define _STOPWATCH_H + +///////////////////////////////////////////////////////////////////////////// +// Global definitions +///////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////// +// Global Types +///////////////////////////////////////////////////////////////////////////// + + +///////////////////////////////////////////////////////////////////////////// +// Prototypes +///////////////////////////////////////////////////////////////////////////// + +extern s32 STOPWATCH_Init(u32 resolution, TIM_TypeDef* TIM); +extern s32 STOPWATCH_Reset(TIM_TypeDef* TIM); +extern u32 STOPWATCH_ValueGet(TIM_TypeDef* TIM); + + +///////////////////////////////////////////////////////////////////////////// +// Export global variables +///////////////////////////////////////////////////////////////////////////// + + +#endif /* _STOPWATCH_H */ diff --git a/flight/Bootloaders/PipXtreme/main.c b/flight/Bootloaders/PipXtreme/main.c new file mode 100644 index 000000000..624825684 --- /dev/null +++ b/flight/Bootloaders/PipXtreme/main.c @@ -0,0 +1,209 @@ +/** + ****************************************************************************** + * @addtogroup OpenPilotBL OpenPilot BootLoader + * @brief These files contain the code to the OpenPilot MB Bootloader. + * + * @{ + * @file main.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief This is the file with the main function of the OpenPilot BootLoader + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +/* Bootloader Includes */ +#include +#include "stopwatch.h" +#include "op_dfu.h" +#include "usb_lib.h" +#include "pios_iap.h" +#include "fifo_buffer.h" +/* Prototype of PIOS_Board_Init() function */ +extern void PIOS_Board_Init(void); +extern void FLASH_Download(); +#define BSL_HOLD_STATE ((PIOS_USB_DETECT_GPIO_PORT->IDR & PIOS_USB_DETECT_GPIO_PIN) ? 0 : 1) + +/* Private typedef -----------------------------------------------------------*/ +typedef void (*pFunction)(void); +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +pFunction Jump_To_Application; +uint32_t JumpAddress; + +/// LEDs PWM +uint32_t period1 = 50; // *100 uS -> 5 mS +uint32_t sweep_steps1 = 100; // * 5 mS -> 500 mS +uint32_t period2 = 50; // *100 uS -> 5 mS +uint32_t sweep_steps2 = 100; // * 5 mS -> 500 mS + + +//////////////////////////////////////// +uint8_t tempcount=0; + + +/* Extern variables ----------------------------------------------------------*/ +DFUStates DeviceState; +int16_t status=0; +uint8_t JumpToApp = FALSE; +uint8_t GO_dfu = FALSE; +uint8_t USB_connected = FALSE; +uint8_t User_DFU_request = FALSE; +static uint8_t mReceive_Buffer[64]; +/* Private function prototypes -----------------------------------------------*/ +uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count); +uint8_t processRX(); +void jump_to_app(); + +#define BLUE LED1 +#define RED LED2 +#define LED_PWM_TIMER TIM6 +int main() { + /* NOTE: Do NOT modify the following start-up sequence */ + /* Any new initialization functions should be added in OpenPilotInit() */ + + /* Brings up System using CMSIS functions, enables the LEDs. */ + PIOS_SYS_Init(); + if (BSL_HOLD_STATE == 0) + USB_connected = TRUE; + + PIOS_IAP_Init(); + + if (PIOS_IAP_CheckRequest() == TRUE) { + PIOS_Board_Init(); + PIOS_DELAY_WaitmS(1000); + User_DFU_request = TRUE; + PIOS_IAP_ClearRequest(); + } + + GO_dfu = (USB_connected == TRUE) || (User_DFU_request == TRUE); + + if (GO_dfu == TRUE) { + PIOS_Board_Init(); + if(User_DFU_request == TRUE) + DeviceState = DFUidle; + else + DeviceState = BLidle; + STOPWATCH_Init(100,LED_PWM_TIMER); + } else + JumpToApp = TRUE; + + STOPWATCH_Reset(LED_PWM_TIMER); + while (TRUE) { + if (JumpToApp == TRUE) + jump_to_app(); + //pwm_period = 50; // *100 uS -> 5 mS + //pwm_sweep_steps =100; // * 5 mS -> 500 mS + + switch (DeviceState) { + case Last_operation_Success: + case uploadingStarting: + case DFUidle: + period1 = 50; + sweep_steps1 = 100; + PIOS_LED_Off(RED); + period2 = 0; + break; + case uploading: + period1 = 50; + sweep_steps1 = 100; + period2 = 25; + sweep_steps2 = 50; + break; + case downloading: + period1 = 25; + sweep_steps1 = 50; + PIOS_LED_Off(RED); + period2 = 0; + break; + case BLidle: + period1 = 0; + PIOS_LED_On(BLUE); + period2 = 0; + break; + default://error + period1 = 50; + sweep_steps1 = 100; + period2 = 50; + sweep_steps2 = 100; + } + + if (period1 != 0) { + if (LedPWM(period1, sweep_steps1, STOPWATCH_ValueGet(LED_PWM_TIMER))) + PIOS_LED_On(BLUE); + else + PIOS_LED_Off(BLUE); + } else + PIOS_LED_On(BLUE); + + if (period2 != 0) { + if (LedPWM(period2, sweep_steps2, STOPWATCH_ValueGet(LED_PWM_TIMER))) + PIOS_LED_On(RED); + else + PIOS_LED_Off(RED); + } else + PIOS_LED_Off(RED); + + if (STOPWATCH_ValueGet(LED_PWM_TIMER) > 100 * 50 * 100) + STOPWATCH_Reset(LED_PWM_TIMER); + if ((STOPWATCH_ValueGet(LED_PWM_TIMER) > 60000) && (DeviceState == BLidle)) + JumpToApp = TRUE; + + processRX(); + DataDownload(start); + } +} + +void jump_to_app() { + if (((*(__IO uint32_t*) START_OF_USER_CODE) & 0x2FFE0000) == 0x20000000) { /* Jump to user application */ + FLASH_Lock(); + RCC_APB2PeriphResetCmd(0xffffffff, ENABLE); + RCC_APB1PeriphResetCmd(0xffffffff, ENABLE); + RCC_APB2PeriphResetCmd(0xffffffff, DISABLE); + RCC_APB1PeriphResetCmd(0xffffffff, DISABLE); + _SetCNTR(0); // clear interrupt mask + _SetISTR(0); // clear all requests + + JumpAddress = *(__IO uint32_t*) (START_OF_USER_CODE + 4); + Jump_To_Application = (pFunction) JumpAddress; + /* Initialize user application's Stack Pointer */ + __set_MSP(*(__IO uint32_t*) START_OF_USER_CODE); + Jump_To_Application(); + } else { + DeviceState = failed_jump; + return; + } +} +uint32_t LedPWM(uint32_t pwm_period, uint32_t pwm_sweep_steps, uint32_t count) { + uint32_t pwm_duty = ((count / pwm_period) % pwm_sweep_steps) + / (pwm_sweep_steps / pwm_period); + if ((count % (2 * pwm_period * pwm_sweep_steps)) > pwm_period + * pwm_sweep_steps) + pwm_duty = pwm_period - pwm_duty; // negative direction each 50*100 ticks + return ((count % pwm_period) > pwm_duty) ? 1 : 0; +} + +uint8_t processRX() { + while (PIOS_COM_ReceiveBufferUsed(PIOS_COM_TELEM_USB) >= 63) { + for (int32_t x = 0; x < 63; ++x) { + mReceive_Buffer[x] = PIOS_COM_ReceiveBuffer(PIOS_COM_TELEM_USB); + } + processComand(mReceive_Buffer); + } + return TRUE; +} + diff --git a/flight/Bootloaders/PipXtreme/op_dfu.c b/flight/Bootloaders/PipXtreme/op_dfu.c new file mode 100644 index 000000000..4b2bd37fb --- /dev/null +++ b/flight/Bootloaders/PipXtreme/op_dfu.c @@ -0,0 +1,492 @@ +/** + ****************************************************************************** + * @addtogroup OpenPilotBL OpenPilot BootLoader + * @brief These files contain the code to the OpenPilot MB Bootloader. + * + * @{ + * @file op_dfu.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief This file contains the DFU commands handling code + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +/* Includes ------------------------------------------------------------------*/ +#include "pios.h" +#include "op_dfu.h" +#include "pios_bl_helper.h" +/* Private typedef -----------------------------------------------------------*/ +/* Private define ------------------------------------------------------------*/ +/* Private macro -------------------------------------------------------------*/ +/* Private variables ---------------------------------------------------------*/ +//programmable devices +Device devicesTable[10]; +uint8_t numberOfDevices = 0; + +DFUProgType currentProgrammingDestination; //flash, flash_trough spi +uint8_t currentDeviceCanRead; +uint8_t currentDeviceCanWrite; +Device currentDevice; + +uint8_t Buffer[64]; +uint8_t echoBuffer[64]; +uint8_t SendBuffer[64]; +uint8_t Command = 0; +uint8_t EchoReqFlag = 0; +uint8_t EchoAnsFlag = 0; +uint8_t StartFlag = 0; +uint32_t Aditionals = 0; +uint32_t SizeOfTransfer = 0; +uint32_t Expected_CRC = 0; +uint8_t SizeOfLastPacket = 0; +uint32_t Next_Packet = 0; +uint8_t TransferType; +uint32_t Count = 0; +uint32_t Data; +uint8_t Data0; +uint8_t Data1; +uint8_t Data2; +uint8_t Data3; +uint8_t offset = 0; +uint32_t aux; +//Download vars +uint32_t downSizeOfLastPacket = 0; +uint32_t downPacketTotal = 0; +uint32_t downPacketCurrent = 0; +DFUTransfer downType = 0; +/* Extern variables ----------------------------------------------------------*/ +extern DFUStates DeviceState; +extern uint8_t JumpToApp; +/* Private function prototypes -----------------------------------------------*/ +/* Private functions ---------------------------------------------------------*/ +void sendData(uint8_t * buf,uint16_t size); +uint32_t CalcFirmCRC(void); + +void DataDownload(DownloadAction action) { + if ((DeviceState == downloading)) { + + uint8_t packetSize; + uint32_t offset; + uint32_t partoffset; + SendBuffer[0] = 0x01; + SendBuffer[1] = Download; + SendBuffer[2] = downPacketCurrent >> 24; + SendBuffer[3] = downPacketCurrent >> 16; + SendBuffer[4] = downPacketCurrent >> 8; + SendBuffer[5] = downPacketCurrent; + if (downPacketCurrent == downPacketTotal - 1) { + packetSize = downSizeOfLastPacket; + } else { + packetSize = 14; + } + for (uint8_t x = 0; x < packetSize; ++x) { + partoffset = (downPacketCurrent * 14 * 4) + (x * 4); + offset = baseOfAdressType(downType) + partoffset; + if(!flash_read(SendBuffer+(6+x*4),offset,currentProgrammingDestination)) + { + DeviceState = Last_operation_failed; + } + /* + switch (currentProgrammingDestination) { + case Remote_flash_via_spi: + if (downType == Descript) { + SendBuffer[6 + (x * 4)] + = spi_dev_desc[(uint8_t) partoffset]; + SendBuffer[7 + (x * 4)] = spi_dev_desc[(uint8_t) partoffset + + 1]; + SendBuffer[8 + (x * 4)] = spi_dev_desc[(uint8_t) partoffset + + 2]; + SendBuffer[9 + (x * 4)] = spi_dev_desc[(uint8_t) partoffset + + 3]; + } + break; + case Self_flash: + SendBuffer[6 + (x * 4)] = *FLASH_If_Read(offset); + SendBuffer[7 + (x * 4)] = *FLASH_If_Read(offset + 1); + SendBuffer[8 + (x * 4)] = *FLASH_If_Read(offset + 2); + SendBuffer[9 + (x * 4)] = *FLASH_If_Read(offset + 3); + break; + } +*/ + } + //PIOS USB_SIL_Write(EP1_IN, (uint8_t*) SendBuffer, 64); + downPacketCurrent = downPacketCurrent + 1; + if (downPacketCurrent > downPacketTotal - 1) { + // STM_EVAL_LEDOn(LED2); + DeviceState = Last_operation_Success; + Aditionals = (uint32_t) Download; + } + sendData(SendBuffer+1,63); + } +} +void processComand(uint8_t *xReceive_Buffer) { + + Command = xReceive_Buffer[COMMAND]; +#ifdef DEBUG_SSP + char str[63]={0}; + sprintf(str,"Received COMMAND:%d|",Command); + PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); +#endif + EchoReqFlag = (Command >> 7); + EchoAnsFlag = (Command >> 6) & 0x01; + StartFlag = (Command >> 5) & 0x01; + Count = xReceive_Buffer[COUNT] << 24; + Count += xReceive_Buffer[COUNT + 1] << 16; + Count += xReceive_Buffer[COUNT + 2] << 8; + Count += xReceive_Buffer[COUNT + 3]; + + Data = xReceive_Buffer[DATA] << 24; + Data += xReceive_Buffer[DATA + 1] << 16; + Data += xReceive_Buffer[DATA + 2] << 8; + Data += xReceive_Buffer[DATA + 3]; + Data0 = xReceive_Buffer[DATA]; + Data1 = xReceive_Buffer[DATA + 1]; + Data2 = xReceive_Buffer[DATA + 2]; + Data3 = xReceive_Buffer[DATA + 3]; + Command = Command & 0b00011111; + + if (EchoReqFlag == 1) { + memcpy(echoBuffer, Buffer, 64); + } + switch (Command) { + case EnterDFU: + if (((DeviceState == BLidle) && (Data0 < numberOfDevices)) + || (DeviceState == DFUidle)) { + if (Data0 > 0)//PORQUE??? + OPDfuIni(TRUE); + DeviceState = DFUidle; + currentProgrammingDestination = devicesTable[Data0].programmingType; + currentDeviceCanRead = devicesTable[Data0].readWriteFlags & 0x01; + currentDeviceCanWrite = devicesTable[Data0].readWriteFlags >> 1 + & 0x01; + currentDevice = devicesTable[Data0]; + uint8_t result = 0; + switch (currentProgrammingDestination) { + case Self_flash: + result = FLASH_Ini(); + break; + case Remote_flash_via_spi: + result = TRUE; + break; + default: + DeviceState = Last_operation_failed; + Aditionals = (uint16_t) Command; + } + if (result != 1) { + DeviceState = Last_operation_failed; + Aditionals = (uint32_t) Command; + } + } + break; + case Upload: + if ((DeviceState == DFUidle) || (DeviceState == uploading)) { + if ((StartFlag == 1) && (Next_Packet == 0)) { + TransferType = Data0; + SizeOfTransfer = Count; + Next_Packet = 1; + Expected_CRC = Data2 << 24; + Expected_CRC += Data3 << 16; + Expected_CRC += xReceive_Buffer[DATA + 4] << 8; + Expected_CRC += xReceive_Buffer[DATA + 5]; + SizeOfLastPacket = Data1; + + if (isBiggerThanAvailable(TransferType, (SizeOfTransfer - 1) + * 14 * 4 + SizeOfLastPacket * 4) == TRUE) { + DeviceState = outsideDevCapabilities; + Aditionals = (uint32_t) Command; + } else { + uint8_t result = 1; + if (TransferType == FW) { + switch (currentProgrammingDestination) { + case Self_flash: + result = FLASH_Start(); + break; + case Remote_flash_via_spi: + result = FALSE; + break; + default: + break; + } + } + if (result != 1) { + DeviceState = Last_operation_failed;//ok + Aditionals = (uint32_t) Command; + } else { + + DeviceState = uploading; + } + } + } else if ((StartFlag != 1) && (Next_Packet != 0)) { + if (Count > SizeOfTransfer) { + DeviceState = too_many_packets; + Aditionals = Count; + } else if (Count == Next_Packet - 1) { + uint8_t numberOfWords = 14; + if (Count == SizeOfTransfer - 1)//is this the last packet? + { + numberOfWords = SizeOfLastPacket; + } + uint8_t result = 0; + switch (currentProgrammingDestination) { + case Self_flash: + for (uint8_t x = 0; x < numberOfWords; ++x) { + offset = 4 * x; + Data = xReceive_Buffer[DATA + offset] << 24; + Data += xReceive_Buffer[DATA + 1 + offset] << 16; + Data += xReceive_Buffer[DATA + 2 + offset] << 8; + Data += xReceive_Buffer[DATA + 3 + offset]; + aux = baseOfAdressType(TransferType) + (uint32_t)( + Count * 14 * 4 + x * 4); + result = 0; + for (int retry = 0; retry < MAX_WRI_RETRYS; ++retry) { + if (result == 0) { + result = (FLASH_ProgramWord(aux, Data) + == FLASH_COMPLETE) ? 1 : 0; + } + } + } + break; + case Remote_flash_via_spi: + result = FALSE; // No support for this for the PipX + break; + default: + result = 0; + break; + } + if (result != 1) { + DeviceState = Last_operation_failed; + Aditionals = (uint32_t) Command; + } + + ++Next_Packet; + } else { + + DeviceState = wrong_packet_received; + Aditionals = Count; + } + // FLASH_ProgramWord(MemLocations[TransferType]+4,++Next_Packet);//+Count,Data); + } else { + DeviceState = Last_operation_failed; + Aditionals = (uint32_t) Command; + } + } + break; + case Req_Capabilities: + OPDfuIni(TRUE); + Buffer[0] = 0x01; + Buffer[1] = Rep_Capabilities; + if (Data0 == 0) { + Buffer[2] = 0; + Buffer[3] = 0; + Buffer[4] = 0; + Buffer[5] = 0; + Buffer[6] = 0; + Buffer[7] = numberOfDevices; + uint16_t WRFlags = 0; + for (int x = 0; x < numberOfDevices; ++x) { + WRFlags = ((devicesTable[x].readWriteFlags << (x * 2)) + | WRFlags); + } + Buffer[8] = WRFlags >> 8; + Buffer[9] = WRFlags; + } else { + Buffer[2] = devicesTable[Data0 - 1].sizeOfCode >> 24; + Buffer[3] = devicesTable[Data0 - 1].sizeOfCode >> 16; + Buffer[4] = devicesTable[Data0 - 1].sizeOfCode >> 8; + Buffer[5] = devicesTable[Data0 - 1].sizeOfCode; + Buffer[6] = Data0; + Buffer[7] = devicesTable[Data0 - 1].BL_Version; + Buffer[8] = devicesTable[Data0 - 1].sizeOfDescription; + Buffer[9] = devicesTable[Data0 - 1].devID; + Buffer[10] = devicesTable[Data0 - 1].FW_Crc >> 24; + Buffer[11] = devicesTable[Data0 - 1].FW_Crc >> 16; + Buffer[12] = devicesTable[Data0 - 1].FW_Crc >> 8; + Buffer[13] = devicesTable[Data0 - 1].FW_Crc; + } + sendData(Buffer + 1, 63); + //PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, Buffer + 1, 63);//FIX+1 + break; + case JumpFW: + FLASH_Lock(); + JumpToApp = 1; + break; + case Reset: + //PIOS Reset_Device(); + break; + case Abort_Operation: + Next_Packet = 0; + DeviceState = DFUidle; + break; + + case Op_END: + if (DeviceState == uploading) { + if (Next_Packet - 1 == SizeOfTransfer) { + Next_Packet = 0; + if ((TransferType != FW) || (Expected_CRC == CalcFirmCRC())) { + DeviceState = Last_operation_Success; + } else { + DeviceState = CRC_Fail; + } + } + if (Next_Packet - 1 < SizeOfTransfer) { + Next_Packet = 0; + DeviceState = too_few_packets; + } + } + + break; + case Download_Req: +#ifdef DEBUG_SSP + sprintf(str,"COMMAND:DOWNLOAD_REQ 1 Status=%d|",DeviceState); + PIOS_COM_SendString(PIOS_COM_TELEM_USB,str); +#endif + if (DeviceState == DFUidle) { +#ifdef DEBUG_SSP + PIOS_COM_SendString(PIOS_COM_TELEM_USB,"COMMAND:DOWNLOAD_REQ 1|"); +#endif + downType = Data0; + downPacketTotal = Count; + downSizeOfLastPacket = Data1; + if (isBiggerThanAvailable(downType, (downPacketTotal - 1) * 14 + + downSizeOfLastPacket) == 1) { + DeviceState = outsideDevCapabilities; + Aditionals = (uint32_t) Command; + + } else { + downPacketCurrent = 0; + DeviceState = downloading; + } + } else { + DeviceState = Last_operation_failed; + Aditionals = (uint32_t) Command; + } + break; + + case Status_Request: + Buffer[0] = 0x01; + Buffer[1] = Status_Rep; + if (DeviceState == wrong_packet_received) { + Buffer[2] = Aditionals >> 24; + Buffer[3] = Aditionals >> 16; + Buffer[4] = Aditionals >> 8; + Buffer[5] = Aditionals; + } else { + Buffer[2] = 0; + Buffer[3] = ((uint16_t) Aditionals) >> 8; + Buffer[4] = ((uint16_t) Aditionals); + Buffer[5] = 0; + } + Buffer[6] = DeviceState; + Buffer[7] = 0; + Buffer[8] = 0; + Buffer[9] = 0; + sendData(Buffer + 1, 63); + if (DeviceState == Last_operation_Success) { + DeviceState = DFUidle; + } + break; + case Status_Rep: + + break; + + } + if (EchoReqFlag == 1) { + echoBuffer[1] = echoBuffer[1] | EchoAnsFlag; + sendData(echoBuffer + 1, 63); + } + return; +} +void OPDfuIni(uint8_t discover) { + Device dev; + dev.programmingType = Self_flash; + dev.readWriteFlags = (BOARD_READABLE | (BOARD_WRITABLA << 1)); + dev.startOfUserCode = START_OF_USER_CODE; + dev.sizeOfCode = SIZE_OF_CODE; + dev.sizeOfDescription = SIZE_OF_DESCRIPTION; + dev.BL_Version = BOOTLOADER_VERSION; + dev.FW_Crc = CalcFirmCRC(); + dev.devID = HW_VERSION; + dev.devType = HW_TYPE; + numberOfDevices = 1; + devicesTable[0] = dev; + if (discover) { + //TODO check other devices trough spi or whatever + } +} +uint32_t baseOfAdressType(DFUTransfer type) { + switch (type) { + case FW: + return currentDevice.startOfUserCode; + break; + case Descript: + return currentDevice.startOfUserCode + currentDevice.sizeOfCode; + break; + default: + + return 0; + } +} +uint8_t isBiggerThanAvailable(DFUTransfer type, uint32_t size) { + switch (type) { + case FW: + return (size > currentDevice.sizeOfCode) ? 1 : 0; + break; + case Descript: + return (size > currentDevice.sizeOfDescription) ? 1 : 0; + break; + default: + return TRUE; + } +} + +uint32_t CalcFirmCRC() { + switch (currentProgrammingDestination) { + case Self_flash: + return crc_memory_calc(); + break; + case Remote_flash_via_spi: + return 0; + break; + default: + return 0; + break; + } + +} +void sendData(uint8_t * buf,uint16_t size) +{ + PIOS_COM_SendBuffer(PIOS_COM_TELEM_USB, buf, size); + if(DeviceState == downloading) + PIOS_DELAY_WaitmS(10); +} + +bool flash_read(uint8_t * buffer, uint32_t adr, DFUProgType type) { + switch (type) { + case Remote_flash_via_spi: + return FALSE; // We should not get this for the PipX + break; + case Self_flash: + for (uint8_t x = 0; x < 4; ++x) { + buffer[x] = *FLASH_If_Read(adr + x); + } + return TRUE; + break; + default: + return FALSE; + } +} diff --git a/flight/Bootloaders/PipXtreme/pios_board.c b/flight/Bootloaders/PipXtreme/pios_board.c new file mode 100644 index 000000000..c28e442a9 --- /dev/null +++ b/flight/Bootloaders/PipXtreme/pios_board.c @@ -0,0 +1,345 @@ +/** + ****************************************************************************** + * + * @file pios_board.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Defines board specific static initializers for hardware for the PipBee board. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include + +/** + * PIOS_Board_Init() + * initializes all the core subsystems on this specific hardware + * called from System/openpilot.c + */ +void PIOS_Board_Init(void) { + + + /* Enable Prefetch Buffer */ + FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); + + /* Flash 2 wait state */ + FLASH_SetLatency(FLASH_Latency_2); + + /* Delay system */ + PIOS_DELAY_Init(); + + /* SPI Init */ + // PIOS_SPI_Init(); + + + /* Initialize the PiOS library */ + PIOS_COM_Init(); + PIOS_GPIO_Init(); + +#if defined(PIOS_INCLUDE_USB_HID) + PIOS_USB_HID_Init(0); +#endif + //PIOS_I2C_Init(); + RCC_AHBPeriphClockCmd(RCC_AHBPeriph_CRC, ENABLE);//TODO Tirar +} + + +// *********************************************************************************** +// SPI + +#if defined(PIOS_INCLUDE_SPI) + +#include + +/* OP Interface + * + * NOTE: Leave this declared as const data so that it ends up in the + * .rodata section (ie. Flash) rather than in the .bss section (RAM). + */ +void PIOS_SPI_port_irq_handler(void); +void DMA1_Channel5_IRQHandler() __attribute__ ((alias ("PIOS_SPI_port_irq_handler"))); +void DMA1_Channel4_IRQHandler() __attribute__ ((alias ("PIOS_SPI_port_irq_handler"))); + +static const struct pios_spi_cfg pios_spi_port_cfg = +{ + .regs = SPI1, +// .regs = SPI2, +// .regs = SPI3, + + .init = + { + .SPI_Mode = SPI_Mode_Master, +// .SPI_Mode = SPI_Mode_Slave, + + .SPI_Direction = SPI_Direction_2Lines_FullDuplex, +// .SPI_Direction = SPI_Direction_2Lines_RxOnly, +// .SPI_Direction = SPI_Direction_1Line_Rx, +// .SPI_Direction = SPI_Direction_1Line_Tx, + +// .SPI_DataSize = SPI_DataSize_16b, + .SPI_DataSize = SPI_DataSize_8b, + + .SPI_NSS = SPI_NSS_Soft, +// .SPI_NSS = SPI_NSS_Hard, + + .SPI_FirstBit = SPI_FirstBit_MSB, +// .SPI_FirstBit = SPI_FirstBit_LSB, + + .SPI_CRCPolynomial = 0, + + .SPI_CPOL = SPI_CPOL_Low, +// .SPI_CPOL = SPI_CPOL_High, + + .SPI_CPHA = SPI_CPHA_1Edge, +// .SPI_CPHA = SPI_CPHA_2Edge, + +// .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2, // fastest SCLK +// .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_4, +// .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8, +// .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16, +// .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_32, +// .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64, +// .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128, + .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256, // slowest SCLK + }, + .use_crc = FALSE, + + .dma = + { + .ahb_clk = RCC_AHBPeriph_DMA1, + .irq = + { + .handler = PIOS_SPI_port_irq_handler, + .flags = (DMA1_FLAG_TC2 | DMA1_FLAG_TE2 | DMA1_FLAG_HT2 | DMA1_FLAG_GL2), + .init = { + .NVIC_IRQChannel = DMA1_Channel2_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGH, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, + + .rx = { + .channel = DMA1_Channel2, + .init = { + .DMA_PeripheralBaseAddr = (uint32_t)&(SPI1->DR), + .DMA_DIR = DMA_DIR_PeripheralSRC, + .DMA_PeripheralInc = DMA_PeripheralInc_Disable, + .DMA_MemoryInc = DMA_MemoryInc_Enable, + .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte, + .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte, + .DMA_Mode = DMA_Mode_Normal, + .DMA_Priority = DMA_Priority_Medium, + .DMA_M2M = DMA_M2M_Disable, + }, + }, + .tx = { + .channel = DMA1_Channel3, + .init = { + .DMA_PeripheralBaseAddr = (uint32_t)&(SPI1->DR), + .DMA_DIR = DMA_DIR_PeripheralDST, + .DMA_PeripheralInc = DMA_PeripheralInc_Disable, + .DMA_MemoryInc = DMA_MemoryInc_Enable, + .DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte, + .DMA_MemoryDataSize = DMA_MemoryDataSize_Byte, + .DMA_Mode = DMA_Mode_Normal, + .DMA_Priority = DMA_Priority_Medium, + .DMA_M2M = DMA_M2M_Disable, + }, + }, + }, + + .ssel = + { + .gpio = GPIOA, + .init = + { + .GPIO_Pin = GPIO_Pin_4, + .GPIO_Speed = GPIO_Speed_10MHz, + .GPIO_Mode = GPIO_Mode_Out_PP, + }, + }, + .sclk = + { + .gpio = GPIOA, + .init = + { + .GPIO_Pin = GPIO_Pin_5, + .GPIO_Speed = GPIO_Speed_10MHz, + .GPIO_Mode = GPIO_Mode_AF_PP, + }, + }, + .miso = + { + .gpio = GPIOA, + .init = + { + .GPIO_Pin = GPIO_Pin_6, + .GPIO_Speed = GPIO_Speed_10MHz, + .GPIO_Mode = GPIO_Mode_IN_FLOATING, +// .GPIO_Mode = GPIO_Mode_IPU, + }, + }, + .mosi = + { + .gpio = GPIOA, + .init = + { + .GPIO_Pin = GPIO_Pin_7, + .GPIO_Speed = GPIO_Speed_10MHz, + .GPIO_Mode = GPIO_Mode_AF_PP, + }, + }, +}; + +/* + * Board specific number of devices. + */ +struct pios_spi_dev pios_spi_devs[] = +{ + { + .cfg = &pios_spi_port_cfg, + }, +}; + +uint8_t pios_spi_num_devices = NELEMENTS(pios_spi_devs); + +void PIOS_SPI_port_irq_handler(void) +{ + /* Call into the generic code to handle the IRQ for this specific device */ + PIOS_SPI_IRQ_Handler(PIOS_SPI_PORT); +} + +#endif /* PIOS_INCLUDE_SPI */ + +// *********************************************************************************** +// USART + +#if defined(PIOS_INCLUDE_USART) +#include + +/* + * SERIAL USART + */ +void PIOS_USART_serial_irq_handler(void); +void USART1_IRQHandler() __attribute__ ((alias ("PIOS_USART_serial_irq_handler"))); + +const struct pios_usart_cfg pios_usart_serial_cfg = +{ + .regs = USART1, + .init = + { + #if defined(PIOS_USART_BAUDRATE) + .USART_BaudRate = PIOS_USART_BAUDRATE, + #else + .USART_BaudRate = 57600, + #endif + .USART_WordLength = USART_WordLength_8b, + .USART_Parity = USART_Parity_No, + .USART_StopBits = USART_StopBits_1, + .USART_HardwareFlowControl = USART_HardwareFlowControl_None, + .USART_Mode = USART_Mode_Rx | USART_Mode_Tx, + }, + .irq = + { + .handler = PIOS_USART_serial_irq_handler, + .init = + { + .NVIC_IRQChannel = USART1_IRQn, + .NVIC_IRQChannelPreemptionPriority = PIOS_IRQ_PRIO_HIGH, + .NVIC_IRQChannelSubPriority = 0, + .NVIC_IRQChannelCmd = ENABLE, + }, + }, + .rx = + { + .gpio = GPIOA, + .init = + { + .GPIO_Pin = GPIO_Pin_10, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_IPU, + }, + }, + .tx = + { + .gpio = GPIOA, + .init = + { + .GPIO_Pin = GPIO_Pin_9, + .GPIO_Speed = GPIO_Speed_2MHz, + .GPIO_Mode = GPIO_Mode_AF_PP, + }, + }, +}; + +/* + * Board specific number of devices. + */ +struct pios_usart_dev pios_usart_devs[] = +{ + #define PIOS_USART_SERIAL 0 + { + .cfg = &pios_usart_serial_cfg, + }, +}; + +uint8_t pios_usart_num_devices = NELEMENTS(pios_usart_devs); + +void PIOS_USART_serial_irq_handler(void) +{ + PIOS_USART_IRQ_Handler(PIOS_USART_SERIAL); +} + +#endif /* PIOS_INCLUDE_USART */ + +// *********************************************************************************** + +#if defined(PIOS_INCLUDE_COM) +#include + +/* + * COM devices + */ + +/* + * Board specific number of devices. + */ +extern const struct pios_com_driver pios_usart_com_driver; +#if defined(PIOS_INCLUDE_USB_HID) + extern const struct pios_com_driver pios_usb_com_driver; +#endif + +struct pios_com_dev pios_com_devs[] = +{ + { + .id = PIOS_USART_SERIAL, + .driver = &pios_usart_com_driver, + }, +#if defined(PIOS_INCLUDE_USB_HID) + { + .id = 0, + .driver = &pios_usb_com_driver, + }, +#endif +}; + +const uint8_t pios_com_num_devices = NELEMENTS(pios_com_devs); + +#endif /* PIOS_INCLUDE_COM */ + +// *********************************************************************************** diff --git a/flight/Bootloaders/PipXtreme/stopwatch.c b/flight/Bootloaders/PipXtreme/stopwatch.c new file mode 100644 index 000000000..af4155273 --- /dev/null +++ b/flight/Bootloaders/PipXtreme/stopwatch.c @@ -0,0 +1,103 @@ +/** + ****************************************************************************** + * @addtogroup OpenPilotBL OpenPilot BootLoader + * @{ + * + * @file stopwatch.c + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010. + * @brief Timer functions for the LED PWM. + * @see The GNU Public License (GPL) Version 3 + * + *****************************************************************************/ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +///////////////////////////////////////////////////////////////////////////// +// Include files +///////////////////////////////////////////////////////////////////////////// + +#include "stm32f10x_tim.h" + +///////////////////////////////////////////////////////////////////////////// +// Local definitions +///////////////////////////////////////////////////////////////////////////// + +//#define STOPWATCH_TIMER_BASE TIM6 +//#define STOPWATCH_TIMER_RCC RCC_APB1Periph_TIM6 + +uint32_t STOPWATCH_Init(u32 resolution, TIM_TypeDef* TIM) { + uint32_t STOPWATCH_TIMER_RCC; + switch ((uint32_t)TIM) { + case (uint32_t)TIM6: + STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM6; + break; + case (uint32_t)TIM7: + STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM7; + break; + default: + STOPWATCH_TIMER_RCC = RCC_APB1Periph_TIM6; + } + + // enable timer clock + if (STOPWATCH_TIMER_RCC == RCC_APB2Periph_TIM1 || STOPWATCH_TIMER_RCC + == RCC_APB2Periph_TIM8) + RCC_APB2PeriphClockCmd(STOPWATCH_TIMER_RCC, ENABLE); + else + RCC_APB1PeriphClockCmd(STOPWATCH_TIMER_RCC, ENABLE); + + // time base configuration + TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; + TIM_TimeBaseStructure.TIM_Period = 0xffff; // max period + TIM_TimeBaseStructure.TIM_Prescaler = (72 * resolution) - 1; // uS accuracy @ 72 MHz + TIM_TimeBaseStructure.TIM_ClockDivision = 0; + TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; + TIM_TimeBaseInit(TIM, &TIM_TimeBaseStructure); + + // enable interrupt request + TIM_ITConfig(TIM, TIM_IT_Update, ENABLE); + + // start counter + TIM_Cmd(TIM, ENABLE); + + return 0; // no error +} + +///////////////////////////////////////////////////////////////////////////// +//! Resets the stopwatch +//! \return < 0 on errors +///////////////////////////////////////////////////////////////////////////// +uint32_t STOPWATCH_Reset(TIM_TypeDef* TIM) { + // reset counter + TIM->CNT = 1; // set to 1 instead of 0 to avoid new IRQ request + TIM_ClearITPendingBit(TIM, TIM_IT_Update); + + return 0; // no error +} + +///////////////////////////////////////////////////////////////////////////// +//! Returns current value of stopwatch +//! \return 1..65535: valid stopwatch value +//! \return 0xffffffff: counter overrun +///////////////////////////////////////////////////////////////////////////// +uint32_t STOPWATCH_ValueGet(TIM_TypeDef* TIM) { + uint32_t value = TIM->CNT; + + if (TIM_GetITStatus(TIM, TIM_IT_Update) != RESET) + value = 0xffffffff; + + return value; +} +