From f919eb6a6466669aef0dd719436f0040d82fe984 Mon Sep 17 00:00:00 2001 From: geeksville Date: Wed, 15 Jul 2020 13:24:57 -0700 Subject: [PATCH] Install a hardfault handler on NRF52 --- src/configuration.h | 9 ++++-- src/nrf52/NRF52Bluetooth.cpp | 56 +----------------------------------- src/nrf52/hardfault.cpp | 52 +++++++++++++++++---------------- 3 files changed, 36 insertions(+), 81 deletions(-) diff --git a/src/configuration.h b/src/configuration.h index 93490d84..d254b980 100644 --- a/src/configuration.h +++ b/src/configuration.h @@ -305,9 +305,15 @@ along with this program. If not, see . // What platforms should use SEGGER? #ifdef NRF52_SERIES +// Always include the SEGGER code on NRF52 - because useful for debugging +#include "SEGGER_RTT.h" + +// Debug printing to segger console +#define SEGGER_MSG(...) SEGGER_RTT_printf(0, __VA_ARGS__) + // nrf52 gets its settings via variant files #ifndef PIN_SERIAL_RX -// No serial ports on this board - use segger in memory console +// No serial ports on this board - ONLY use segger in memory console #define USE_SEGGER #endif @@ -316,7 +322,6 @@ along with this program. If not, see . #endif #ifdef USE_SEGGER -#include "SEGGER_RTT.h" #define DEBUG_MSG(...) SEGGER_RTT_printf(0, __VA_ARGS__) #else #ifdef DEBUG_PORT diff --git a/src/nrf52/NRF52Bluetooth.cpp b/src/nrf52/NRF52Bluetooth.cpp index ed1f39d2..8e1790d7 100644 --- a/src/nrf52/NRF52Bluetooth.cpp +++ b/src/nrf52/NRF52Bluetooth.cpp @@ -216,58 +216,4 @@ void NRF52Bluetooth::setup() void updateBatteryLevel(uint8_t level) { blebas.write(level); -} - -/* -void loop() -{ - digitalToggle(LED_RED); - - if ( Bluefruit.connected() ) { - uint8_t hrmdata[2] = { 0b00000110, bps++ }; // Sensor connected, increment BPS value - - // Note: We use .notify instead of .write! - // If it is connected but CCCD is not enabled - // The characteristic's value is still updated although notification is not sent - if ( hrmc.notify(hrmdata, sizeof(hrmdata)) ){ - Serial.print("Heart Rate Measurement updated to: "); Serial.println(bps); - }else{ - Serial.println("ERROR: Notify not set in the CCCD or not connected!"); - } - } - - // Only send update once per second - delay(1000); -} -*/ - -/* -examples of advanced characteristics. use setReadAuthorizeCallback to prepare data for reads by others - -err_t BLEDfu::begin(void) -{ - // Invoke base class begin() - VERIFY_STATUS( BLEService::begin() ); - - // No need to keep packet & revision characteristics - BLECharacteristic chr_packet(UUID128_CHR_DFU_PACKET); - chr_packet.setTempMemory(); - chr_packet.setProperties(CHR_PROPS_WRITE_WO_RESP); - chr_packet.setMaxLen(20); - VERIFY_STATUS( chr_packet.begin() ); - - _chr_control.setProperties(CHR_PROPS_WRITE | CHR_PROPS_NOTIFY); - _chr_control.setMaxLen(23); - _chr_control.setWriteAuthorizeCallback(bledfu_control_wr_authorize_cb); - VERIFY_STATUS( _chr_control.begin() ); - - BLECharacteristic chr_revision(UUID128_CHR_DFU_REVISON); - chr_revision.setTempMemory(); - chr_revision.setProperties(CHR_PROPS_READ); - chr_revision.setFixedLen(2); - VERIFY_STATUS( chr_revision.begin()); - chr_revision.write16(DFU_REV_APPMODE); - - return ERROR_NONE; -} -*/ \ No newline at end of file +} \ No newline at end of file diff --git a/src/nrf52/hardfault.cpp b/src/nrf52/hardfault.cpp index 4cf4dff7..f49bba68 100644 --- a/src/nrf52/hardfault.cpp +++ b/src/nrf52/hardfault.cpp @@ -5,47 +5,51 @@ enum { r0, r1, r2, r3, r12, lr, pc, psr }; +// we can't use the regular DEBUG_MSG for these crash dumps because it depends on threading still being running. Instead use the +// segger in memory tool +#define FAULT_MSG(...) SEGGER_MSG(__VA_ARGS__) + // Per http://infocenter.arm.com/help/index.jsp?topic=/com.arm.doc.dui0552a/Cihcfefj.html static void printUsageErrorMsg(uint32_t cfsr) { - DEBUG_MSG("Usage fault: "); + FAULT_MSG("Usage fault: "); cfsr >>= SCB_CFSR_USGFAULTSR_Pos; // right shift to lsb if ((cfsr & (1 << 9)) != 0) - DEBUG_MSG("Divide by zero\n"); + FAULT_MSG("Divide by zero\n"); if ((cfsr & (1 << 8)) != 0) - DEBUG_MSG("Unaligned\n"); + FAULT_MSG("Unaligned\n"); } static void printBusErrorMsg(uint32_t cfsr) { - DEBUG_MSG("Usage fault: "); + FAULT_MSG("Bus fault: "); cfsr >>= SCB_CFSR_BUSFAULTSR_Pos; // right shift to lsb if ((cfsr & (1 << 0)) != 0) - DEBUG_MSG("Instruction bus error\n"); + FAULT_MSG("Instruction bus error\n"); if ((cfsr & (1 << 1)) != 0) - DEBUG_MSG("Precise data bus error\n"); + FAULT_MSG("Precise data bus error\n"); if ((cfsr & (1 << 2)) != 0) - DEBUG_MSG("Imprecise data bus error\n"); + FAULT_MSG("Imprecise data bus error\n"); } static void printMemErrorMsg(uint32_t cfsr) { - DEBUG_MSG("Usage fault: "); + FAULT_MSG("Memory fault: "); cfsr >>= SCB_CFSR_MEMFAULTSR_Pos; // right shift to lsb if ((cfsr & (1 << 0)) != 0) - DEBUG_MSG("Instruction access violation\n"); + FAULT_MSG("Instruction access violation\n"); if ((cfsr & (1 << 1)) != 0) - DEBUG_MSG("Data access violation\n"); + FAULT_MSG("Data access violation\n"); } -static void HardFault_Impl(uint32_t stack[]) +extern "C" void HardFault_Impl(uint32_t stack[]) { - DEBUG_MSG("In Hard Fault Handler\n"); - DEBUG_MSG("SCB->HFSR = 0x%08lx\n", SCB->HFSR); + FAULT_MSG("In Hard Fault Handler\n"); + FAULT_MSG("SCB->HFSR = 0x%08lx\n", SCB->HFSR); if ((SCB->HFSR & SCB_HFSR_FORCED_Msk) != 0) { - DEBUG_MSG("Forced Hard Fault\n"); - DEBUG_MSG("SCB->CFSR = 0x%08lx\n", SCB->CFSR); + FAULT_MSG("Forced Hard Fault\n"); + FAULT_MSG("SCB->CFSR = 0x%08lx\n", SCB->CFSR); if ((SCB->CFSR & SCB_CFSR_USGFAULTSR_Msk) != 0) { printUsageErrorMsg(SCB->CFSR); @@ -57,21 +61,21 @@ static void HardFault_Impl(uint32_t stack[]) printMemErrorMsg(SCB->CFSR); } - DEBUG_MSG("r0 = 0x%08lx\n", stack[r0]); - DEBUG_MSG("r1 = 0x%08lx\n", stack[r1]); - DEBUG_MSG("r2 = 0x%08lx\n", stack[r2]); - DEBUG_MSG("r3 = 0x%08lx\n", stack[r3]); - DEBUG_MSG("r12 = 0x%08lx\n", stack[r12]); - DEBUG_MSG("lr = 0x%08lx\n", stack[lr]); - DEBUG_MSG("pc = 0x%08lx\n", stack[pc]); - DEBUG_MSG("psr = 0x%08lx\n", stack[psr]); + FAULT_MSG("r0 = 0x%08lx\n", stack[r0]); + FAULT_MSG("r1 = 0x%08lx\n", stack[r1]); + FAULT_MSG("r2 = 0x%08lx\n", stack[r2]); + FAULT_MSG("r3 = 0x%08lx\n", stack[r3]); + FAULT_MSG("r12 = 0x%08lx\n", stack[r12]); + FAULT_MSG("lr = 0x%08lx\n", stack[lr]); + FAULT_MSG("pc = 0x%08lx\n", stack[pc]); + FAULT_MSG("psr = 0x%08lx\n", stack[psr]); asm volatile("bkpt #01"); while (1) ; } } -void HardFault_Handler(void) +extern "C" void HardFault_Handler(void) { asm volatile(" mrs r0,msp\n" " b HardFault_Impl \n");