From 2e79aafb7081a5305ee875277d26734779ca6d2f Mon Sep 17 00:00:00 2001 From: Perry Hung Date: Fri, 17 Sep 2010 03:49:14 -0400 Subject: Enable USB auto-reset in a hard fault. Redirect thread-mode execution to a fail routine which throbs the LED to indicate a hard fault. Because the fail routine runs in thread mode with interrupts on, USB auto-reset should now work. Test by executing some bogus instruction (e.g. *(volatile int*)0xf34fdaa = 0;) and check that the auto-reset continues to work. --- Makefile | 9 ++++--- libmaple/exc.c | 63 --------------------------------------------- libmaple/rules.mk | 11 +++++--- libmaple/util.c | 51 ++++++++++++++++++++++-------------- libmaple/util.h | 1 + support/make/build-rules.mk | 7 +++-- 6 files changed, 49 insertions(+), 93 deletions(-) delete mode 100644 libmaple/exc.c diff --git a/Makefile b/Makefile index bc34f9e..4fd5ade 100644 --- a/Makefile +++ b/Makefile @@ -29,11 +29,12 @@ LIBMAPLE_PATH := $(SRCROOT)/libmaple SUPPORT_PATH := $(SRCROOT)/support # Useful variables -GLOBAL_CFLAGS := -Os -g -mcpu=cortex-m3 -mthumb -march=armv7-m -nostdlib \ - -ffunction-sections -fdata-sections -Wl,--gc-sections \ - -DBOARD_$(BOARD) -DMCU_$(MCU) +GLOBAL_CFLAGS := -Os -g -mcpu=cortex-m3 -mthumb -march=armv7-m -nostdlib \ + -ffunction-sections -fdata-sections -Wl,--gc-sections \ + -DBOARD_$(BOARD) -DMCU_$(MCU) GLOBAL_CXXFLAGS := -fno-rtti -fno-exceptions -Wall -DBOARD_$(BOARD) -DMCU_$(MCU) - +GLOBAL_ASFLAGS := -mcpu=cortex-m3 -march=armv7-m -mthumb -DBOARD_$(BOARD) \ + -DMCU_$(MCU) -x assembler-with-cpp LDDIR := $(SUPPORT_PATH)/ld LDFLAGS = -T$(LDDIR)/$(LDSCRIPT) -L$(LDDIR) \ diff --git a/libmaple/exc.c b/libmaple/exc.c deleted file mode 100644 index a5c67fa..0000000 --- a/libmaple/exc.c +++ /dev/null @@ -1,63 +0,0 @@ - -/* ***************************************************************************** - * The MIT License - * - * Copyright (c) 2010 Perry Hung. - * - * 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. - * ****************************************************************************/ - -/** - * @brief libmaple exception handlers. If DEBUG_LEVEL > DEBUG_FAULT, then these - * exceptions will ASSERT fail and call into the default _fail() light - * blinking. - */ - -#include "util.h" - -void NMIException(void) { - ASSERT_FAULT(0); - while(1) - ; -} - -void HardFaultException(void) { - ASSERT_FAULT(0); - while(1) - ; -} - -void MemManageException(void) { - ASSERT_FAULT(0); - while(1) - ; -} - -void BusFaultException(void) { - ASSERT_FAULT(0); - while(1) - ; -} - -void UsageFaultException(void) { - ASSERT_FAULT(0); - while(1) - ; -} - diff --git a/libmaple/rules.mk b/libmaple/rules.mk index 8428277..cd50495 100644 --- a/libmaple/rules.mk +++ b/libmaple/rules.mk @@ -16,7 +16,6 @@ cSRCS_$(d) := systick.c \ timers.c \ adc.c \ syscalls.c \ - exc.c \ exti.c \ gpio.c \ nvic.c \ @@ -25,8 +24,8 @@ cSRCS_$(d) := systick.c \ rcc.c \ flash.c \ spi.c \ - fsmc.c \ - dac.c \ + fsmc.c \ + dac.c \ usb/usb.c \ usb/usb_callbacks.c \ usb/usb_hardware.c \ @@ -37,12 +36,16 @@ cSRCS_$(d) := systick.c \ usb/usb_lib/usb_mem.c \ usb/usb_lib/usb_regs.c +sSRCS_$(d) := exc.S + cFILES_$(d) := $(cSRCS_$(d):%=$(d)/%) +sFILES_$(d) := $(sSRCS_$(d):%=$(d)/%) -OBJS_$(d) := $(cFILES_$(d):%.c=$(BUILD_PATH)/%.o) +OBJS_$(d) := $(cFILES_$(d):%.c=$(BUILD_PATH)/%.o) $(sFILES_$(d):%.S=$(BUILD_PATH)/%.o) DEPS_$(d) := $(OBJS_$(d):%.o=%.d) $(OBJS_$(d)): TGT_CFLAGS := $(CFLAGS_$(d)) +$(OBJS_$(d)): TGT_ASFLAGS := TGT_BIN += $(OBJS_$(d)) diff --git a/libmaple/util.c b/libmaple/util.c index a747948..3325d2b 100644 --- a/libmaple/util.c +++ b/libmaple/util.c @@ -68,7 +68,7 @@ void _fail(const char* file, int line, const char* exp) { usart_putudec(ERROR_USART_NUM, line); usart_putc(ERROR_USART_NUM, '\n'); usart_putc(ERROR_USART_NUM, '\r'); - + /* Turn on the error LED */ gpio_set_mode(ERROR_LED_PORT, ERROR_LED_PIN, GPIO_MODE_OUTPUT_PP); @@ -77,24 +77,35 @@ void _fail(const char* file, int line, const char* exp) { nvic_irq_enable(NVIC_INT_USBLP); /* Error fade */ - while (1) { - if (CC == TOP_CNT) { - slope = -1; - } else if (CC == 0) { - slope = 1; - } - - if (i == TOP_CNT) { - CC += slope; - i = 0; - } - - if (i < CC) { - gpio_write_bit(ERROR_LED_PORT, ERROR_LED_PIN, 1); - } else { - gpio_write_bit(ERROR_LED_PORT, ERROR_LED_PIN, 0); - } - i++; - } + throb(); +} + +void throb(void) { + int32 slope = 1; + uint32 CC = 0x0000; + uint32 TOP_CNT = 0x0200; + uint32 i = 0; + + gpio_set_mode(ERROR_LED_PORT, ERROR_LED_PIN, GPIO_MODE_OUTPUT_PP); + /* Error fade */ + while (1) { + if (CC == TOP_CNT) { + slope = -1; + } else if (CC == 0) { + slope = 1; + } + + if (i == TOP_CNT) { + CC += slope; + i = 0; + } + + if (i < CC) { + gpio_write_bit(ERROR_LED_PORT, ERROR_LED_PIN, 1); + } else { + gpio_write_bit(ERROR_LED_PORT, ERROR_LED_PIN, 0); + } + i++; + } } diff --git a/libmaple/util.h b/libmaple/util.h index 63e305d..be5e430 100644 --- a/libmaple/util.h +++ b/libmaple/util.h @@ -66,6 +66,7 @@ extern "C"{ #endif void _fail(const char*, int, const char*); +void throb(void); #ifdef __cplusplus } // extern "C" diff --git a/support/make/build-rules.mk b/support/make/build-rules.mk index 3892af9..4dcf00f 100644 --- a/support/make/build-rules.mk +++ b/support/make/build-rules.mk @@ -3,7 +3,7 @@ CC := arm-none-eabi-gcc CXX := arm-none-eabi-g++ LD := arm-none-eabi-ld -v AR := arm-none-eabi-ar -AS := arm-none-eabi-as +AS := arm-none-eabi-gcc OBJCOPY := arm-none-eabi-objcopy DISAS := arm-none-eabi-objdump OBJDUMP := arm-none-eabi-objdump @@ -14,10 +14,10 @@ OPENOCD := openocd # Suppress annoying output unless V is set ifndef V SILENT_CC = @echo ' [CC] ' $(@:$(BUILD_PATH)/%.o=%.c); + SILENT_AS = @echo ' [AS] ' $(@:$(BUILD_PATH)/%.o=%.S); SILENT_CXX = @echo ' [CXX] ' $(@:$(BUILD_PATH)/%.o=%.cpp); SILENT_LD = @echo ' [LD] ' $(@F); SILENT_AR = @echo ' [AR] ' - SILENT_AS = @echo ' [AS] ' SILENT_OBJCOPY = @echo ' [OBJCOPY] ' $(@F); SILENT_DISAS = @echo ' [DISAS] ' $(@:$(BUILD_PATH)/%.bin=%).disas; SILENT_OBJDUMP = @echo ' [OBJDUMP] ' $(OBJDUMP); @@ -28,6 +28,7 @@ TGT_BIN := CFLAGS = $(GLOBAL_CFLAGS) $(TGT_CFLAGS) CXXFLAGS = $(GLOBAL_CXXFLAGS) $(TGT_CXXFLAGS) +ASFLAGS = $(GLOBAL_ASFLAGS) $(TGT_ASFLAGS) # General directory independent build rules, generate dependency information $(BUILD_PATH)/%.o: %.c @@ -36,3 +37,5 @@ $(BUILD_PATH)/%.o: %.c $(BUILD_PATH)/%.o: %.cpp $(SILENT_CXX) $(CXX) $(CFLAGS) $(CXXFLAGS) -MMD -MP -MF $(@:%.o=%.d) -MT $@ -o $@ -c $< +$(BUILD_PATH)/%.o: %.S + $(SILENT_AS) $(AS) $(ASFLAGS) -MMD -MP -MF $(@:%.o=%.d) -MT $@ -o $@ -c $< -- cgit v1.2.3