From d6869d1ec4bd24cd2c3eafa534f0849b25ec5607 Mon Sep 17 00:00:00 2001 From: Clyne Sullivan Date: Thu, 28 Feb 2019 17:04:22 -0500 Subject: added basic code --- arduino/cores/nRF5/utility/debug.cpp | 444 +++++++++++++++++++++++++++++++++++ 1 file changed, 444 insertions(+) create mode 100755 arduino/cores/nRF5/utility/debug.cpp (limited to 'arduino/cores/nRF5/utility/debug.cpp') diff --git a/arduino/cores/nRF5/utility/debug.cpp b/arduino/cores/nRF5/utility/debug.cpp new file mode 100755 index 0000000..d786202 --- /dev/null +++ b/arduino/cores/nRF5/utility/debug.cpp @@ -0,0 +1,444 @@ +/**************************************************************************/ +/*! + @file debug.cpp + @author hathach (tinyusb.org) + + @section LICENSE + + Software License Agreement (BSD License) + + Copyright (c) 2018, Adafruit Industries (adafruit.com) + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + 3. Neither the name of the copyright holders nor the + names of its contributors may be used to endorse or promote products + derived from this software without specific prior written permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY + EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY + DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ +/**************************************************************************/ + +#include +#include +#include +#include +#include + +// defined in linker script +extern uint32_t __data_start__[]; +extern uint32_t __data_end__[]; + +extern uint32_t __bss_start__[]; +extern uint32_t __bss_end__[]; + +extern unsigned char __HeapBase[]; +extern unsigned char __HeapLimit[]; + +extern uint32_t __StackTop[]; +extern uint32_t __StackLimit[]; + +extern "C" +{ + +void HardFault_Handler(void) +{ + // reset on hardfault + NVIC_SystemReset(); +} + +int dbgHeapTotal(void) +{ + return ((uint32_t) __HeapLimit) - ((uint32_t) __HeapBase); +} + +int dbgHeapUsed(void) +{ + return (mallinfo()).uordblks; +} + +int dbgStackTotal(void) +{ + return ((uint32_t) __StackTop) - ((uint32_t) __StackLimit); +} + +int dbgStackUsed(void) +{ + enum { STACK_PATTERN = 0xADADADAD }; + + uint32_t * p_start = (uint32_t*) &__StackLimit; + uint32_t * p_end = (uint32_t*) &__StackTop; + + uint32_t * p_buf = p_start; + while( *p_buf == STACK_PATTERN && p_buf != p_end) + { + p_buf++; + } + + if (p_buf == p_end) return (-1); + + return ((uint32_t) p_end) - ((uint32_t) p_buf); +} + +static void printMemRegion(const char* name, uint32_t top, uint32_t bottom, uint32_t used) +{ + char buffer[30]; + if ( used ) + { + sprintf(buffer, "%5lu / %5lu (%02lu%%)", used, top-bottom, (used*100)/ (top-bottom)); + }else + { + sprintf(buffer, "%lu", top-bottom); + } + + printf("| %-5s| 0x%04X - 0x%04X | %-19s |\n", name, (uint16_t) bottom, (uint16_t) (top-1), buffer); +} + +void dbgMemInfo(void) +{ + printf(" ______________________________________________\n"); + printf("| Name | Addr 0x2000xxxx | Usage |\n"); + printf("| ---------------------------------------------|\n"); + + // Pritn SRAM used for Stack executed by S132 and ISR + printMemRegion("Stack", ((uint32_t) __StackTop), ((uint32_t) __StackLimit), dbgStackUsed() ); + + // Print Heap usage overall (including memory malloced to tasks) + printMemRegion("Heap", ((uint32_t) __HeapLimit), ((uint32_t) __HeapBase), dbgHeapUsed() ); + + // DATA + BSS + printMemRegion("Bss", ((uint32_t) __bss_end__), ((uint32_t) __data_start__), 0); + + // Print SRAM Used by SoftDevice + printMemRegion("S132", (uint32_t) __data_start__, 0x20000000, 0); + + printf("|______________________________________________|\n"); + printf("\n"); + + // Print Task list + uint32_t tasknum = uxTaskGetNumberOfTasks(); + char* buf = (char*) rtos_malloc(tasknum*40); // 40 bytes per task + + vTaskList(buf); + + printf("Task State Prio StackLeft Num\n"); + printf("-----------------------------------\n"); + printf(buf); + printf("\n"); + rtos_free(buf); +} + +void dbgPrintVersion(void) +{ + printf("\n"); + printf("BSP Library : " ARDUINO_BSP_VERSION "\n"); + printf("Bootloader : %s\n", getBootloaderVersion()); + printf("Serial No : %s\n", getMcuUniqueID()); + printf("\n"); +} + +/******************************************************************************/ +/*! + @brief Helper function to display memory contents in a friendly format +*/ +/******************************************************************************/ +static void dump_str_line(uint8_t const* buf, uint16_t count) +{ + // each line is 16 bytes + for(int i=0; i