From ecedd3d0e2d033d7c641cf1be117c20ae9402d86 Mon Sep 17 00:00:00 2001 From: hathach Date: Fri, 17 Feb 2017 23:07:30 +0700 Subject: [PATCH] refactor linker & startup for dynamic checking stack & heap usage. - add syscall __sbrk() for malloc function - add dbgMemInfo() is visual format - FreeRTOS enable : configCHECK_FOR_STACK_OVERFLOW, configUSE_MALLOC_FAILED_HOOK, configUSE_TRACE_FACILITY, configUSE_STATS_FORMATTING_FUNCTIONS --- boards.txt | 2 +- .../armgcc/armgcc_s132_nrf52832_xxaa.ld | 25 ----- cores/nRF5/debug.cpp | 92 +++++++++++++++++-- cores/nRF5/debug.h | 2 + cores/nRF5/freertos/config/FreeRTOSConfig.h | 8 +- .../toolchain/gcc => }/gcc_startup_nrf52.S | 8 +- .../toolchain/gcc => }/nrf5x_common.ld | 27 +++--- .../library.properties | 4 +- platform.txt | 2 +- variants/feather52/feather52_s132.ld | 33 +++++++ 10 files changed, 141 insertions(+), 62 deletions(-) delete mode 100755 cores/nRF5/SDK/components/softdevice/s132/toolchain/armgcc/armgcc_s132_nrf52832_xxaa.ld rename cores/nRF5/{SDK/components/toolchain/gcc => }/gcc_startup_nrf52.S (96%) rename cores/nRF5/{SDK/components/toolchain/gcc => }/nrf5x_common.ld (84%) create mode 100755 variants/feather52/feather52_s132.ld diff --git a/boards.txt b/boards.txt index 13420cb6..e8fe66ac 100644 --- a/boards.txt +++ b/boards.txt @@ -47,7 +47,7 @@ feather52.build.variant=feather52 feather52.build.variant_system_lib= feather52.build.extra_flags=-DNRF52 -DS132 -DNRF51_S132 feather52.build.float_flags=-mfloat-abi=hard -mfpu=fpv4-sp-d16 -feather52.build.ldscript=armgcc_s132_nrf52832_xxaa.ld +feather52.build.ldscript=feather52_s132.ld feather52.build.nrfutil=nrfutil feather52.build.nrfutil.windows={runtime.platform.path}/tools/nrfutil-0.5.2/binaries/win32/nrfutil.exe diff --git a/cores/nRF5/SDK/components/softdevice/s132/toolchain/armgcc/armgcc_s132_nrf52832_xxaa.ld b/cores/nRF5/SDK/components/softdevice/s132/toolchain/armgcc/armgcc_s132_nrf52832_xxaa.ld deleted file mode 100755 index 1fc03c69..00000000 --- a/cores/nRF5/SDK/components/softdevice/s132/toolchain/armgcc/armgcc_s132_nrf52832_xxaa.ld +++ /dev/null @@ -1,25 +0,0 @@ -/* Linker script to configure memory regions. */ - -SEARCH_DIR(.) -GROUP(-lgcc -lc -lnosys) - -MEMORY -{ - FLASH (rx) : ORIGIN = 0x1c000, LENGTH = 0x64000 - - /* RAM (rwx) : ORIGIN = 0x20002080, LENGTH = 0xdf80 default ATTR SIZE of 0x580 */ - /* Increase Attr Table Size to 0x1000 */ - RAM (rwx) : ORIGIN = 0x20002B00, LENGTH = 0xD500 -} - -SECTIONS -{ - .fs_data : - { - PROVIDE(__start_fs_data = .); - KEEP(*(.fs_data)) - PROVIDE(__stop_fs_data = .); - } > RAM -} INSERT AFTER .data; - -INCLUDE "nrf5x_common.ld" \ No newline at end of file diff --git a/cores/nRF5/debug.cpp b/cores/nRF5/debug.cpp index 823468f6..1f5af572 100644 --- a/cores/nRF5/debug.cpp +++ b/cores/nRF5/debug.cpp @@ -40,18 +40,17 @@ #include "debug.h" // defined in linker script -extern unsigned char __end__[]; +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[]; -int dbgHeapTotal(void) -{ - return ((uint32_t) __HeapLimit) - ((uint32_t) __end__); -} - -int dbgHeapUsed(void) -{ - return (mallinfo()).uordblks; -} +extern uint32_t __StackTop[]; +extern uint32_t __StackLimit[]; extern "C" { @@ -69,4 +68,77 @@ extern "C" va_end(ap); return len; } + + void vApplicationMallocFailedHook(void) + { + Serial.println("Failed to Malloc"); + } + + void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName ) + { + Serial.printf("%s Stack Overflow !!!", pcTaskName); + } +} + +int dbgHeapTotal(void) +{ + return ((uint32_t) __HeapLimit) - ((uint32_t) __HeapBase); +} + +int dbgHeapUsed(void) +{ + return (mallinfo()).uordblks; +} + +static void printMemRegion(const char* name, uint32_t top, uint32_t bottom, uint32_t used) +{ + const int width = 21; + + Serial.printf(" _____________________ 0x%08lX\n", top); + Serial.printf("| |\n"); + + int lpad = (width-strlen(name))/2; + int rpad = width - strlen(name) - lpad; + Serial.printf("|%*c%s%*c|\n", lpad, ' ', name, rpad, ' '); + + if ( used ) + { + Serial.printf("| %d / %d (%02lu%%) |\n", used, top-bottom, (used*100)/ (top-bottom)); + }else + { + Serial.printf("| %d |\n",top-bottom); + } + + Serial.printf("|_____________________| 0x%08lX\n", bottom); +} + +void dbgMemInfo(void) +{ + Serial.println(" Memory Map"); + + // Pritn SRAM used for Stack executed by S132 and ISR + uint32_t stack_used = ((uint32_t) __StackTop) -((uint32_t) __StackLimit); + printMemRegion("Stack", ((uint32_t) __StackTop), ((uint32_t) __StackLimit), 0); + + // Print Heap usage overall (including memory malloced to tasks) + printMemRegion("Heap", ((uint32_t) __HeapLimit), ((uint32_t) __HeapBase), dbgHeapUsed()); + + // DATA + BSS + printMemRegion("Data & Bss", ((uint32_t) __bss_end__), ((uint32_t) __data_start__), 0); + + // Print SRAM Used by SoftDevice + printMemRegion("S132", (uint32_t) __data_start__, 0x20000000, 0); + + Serial.println(); + + // Print Task list + uint32_t tasknum = uxTaskGetNumberOfTasks(); + char* buf = (char*) rtos_malloc(tasknum*40); // 40 bytes per task + + vTaskList(buf); + + Serial.println("Task State Prio Stack Num"); + Serial.println("-----------------------------------"); + Serial.println(buf); + rtos_free(buf); } diff --git a/cores/nRF5/debug.h b/cores/nRF5/debug.h index 8e38d792..9d1cc199 100644 --- a/cores/nRF5/debug.h +++ b/cores/nRF5/debug.h @@ -47,4 +47,6 @@ static inline int dbgHeapFree(void) return dbgHeapTotal() - dbgHeapUsed(); } +void dbgMemInfo(void); + #endif diff --git a/cores/nRF5/freertos/config/FreeRTOSConfig.h b/cores/nRF5/freertos/config/FreeRTOSConfig.h index a2c71a78..174193be 100644 --- a/cores/nRF5/freertos/config/FreeRTOSConfig.h +++ b/cores/nRF5/freertos/config/FreeRTOSConfig.h @@ -115,13 +115,13 @@ /* Hook function related definitions. */ #define configUSE_IDLE_HOOK 0 #define configUSE_TICK_HOOK 0 -#define configCHECK_FOR_STACK_OVERFLOW 0 -#define configUSE_MALLOC_FAILED_HOOK 0 +#define configCHECK_FOR_STACK_OVERFLOW 1 +#define configUSE_MALLOC_FAILED_HOOK 1 /* Run time and task stats gathering related definitions. */ #define configGENERATE_RUN_TIME_STATS 0 -#define configUSE_TRACE_FACILITY 0 -#define configUSE_STATS_FORMATTING_FUNCTIONS 0 +#define configUSE_TRACE_FACILITY 1 +#define configUSE_STATS_FORMATTING_FUNCTIONS 1 /* Co-routine definitions. */ #define configUSE_CO_ROUTINES 0 diff --git a/cores/nRF5/SDK/components/toolchain/gcc/gcc_startup_nrf52.S b/cores/nRF5/gcc_startup_nrf52.S similarity index 96% rename from cores/nRF5/SDK/components/toolchain/gcc/gcc_startup_nrf52.S rename to cores/nRF5/gcc_startup_nrf52.S index 976d1fe2..ec58719e 100755 --- a/cores/nRF5/SDK/components/toolchain/gcc/gcc_startup_nrf52.S +++ b/cores/nRF5/gcc_startup_nrf52.S @@ -29,12 +29,12 @@ .syntax unified .arch armv7e-m - .section .stack +/* .section .stack .align 3 #ifdef __STACK_SIZE .equ Stack_Size, __STACK_SIZE #else - .equ Stack_Size, 8192 /* Reduce could affect SD function */ + .equ Stack_Size, 8192 #endif .globl __StackTop .globl __StackLimit @@ -49,7 +49,7 @@ __StackTop: #ifdef __HEAP_SIZE .equ Heap_Size, __HEAP_SIZE #else - .equ Heap_Size, 25600 /* TODO Should be all space left */ + .equ Heap_Size, 25600 #endif .globl __HeapBase .globl __HeapLimit @@ -60,7 +60,7 @@ __HeapBase: .size __HeapBase, . - __HeapBase __HeapLimit: .size __HeapLimit, . - __HeapLimit - +*/ .section .isr_vector .align 2 .globl __isr_vector diff --git a/cores/nRF5/SDK/components/toolchain/gcc/nrf5x_common.ld b/cores/nRF5/nrf5x_common.ld similarity index 84% rename from cores/nRF5/SDK/components/toolchain/gcc/nrf5x_common.ld rename to cores/nRF5/nrf5x_common.ld index 69c98f89..068dfaee 100755 --- a/cores/nRF5/SDK/components/toolchain/gcc/nrf5x_common.ld +++ b/cores/nRF5/nrf5x_common.ld @@ -45,6 +45,14 @@ OUTPUT_FORMAT ("elf32-littlearm", "elf32-bigarm", "elf32-littlearm") */ ENTRY(Reset_Handler) +__StackSize = 1024*3; + +/* Set stack top to end of RAM, and stack limit move down by + * size of stack_dummy section */ +PROVIDE(__StackTop = ORIGIN(RAM) + LENGTH(RAM)); +PROVIDE(__StackLimit = __StackTop - __StackSize); +PROVIDE(__stack = __StackTop); + SECTIONS { .text : @@ -132,27 +140,16 @@ SECTIONS __bss_end__ = .; } > RAM - .heap (COPY): + /*.heap (COPY):*/ + .heap : { __end__ = .; PROVIDE(end = .); + __HeapBase = .; *(.heap*) + . = ALIGN(__StackLimit); __HeapLimit = .; } > RAM - - /* .stack_dummy section doesn't contains any symbols. It is only - * used for linker to calculate size of stack sections, and assign - * values to stack symbols later */ - .stack_dummy (COPY): - { - *(.stack*) - } > RAM - - /* Set stack top to end of RAM, and stack limit move down by - * size of stack_dummy section */ - __StackTop = ORIGIN(RAM) + LENGTH(RAM); - __StackLimit = __StackTop - SIZEOF(.stack_dummy); - PROVIDE(__stack = __StackTop); /* Check if data + heap + stack exceeds RAM limit */ ASSERT(__StackLimit >= __HeapLimit, "region RAM overflowed with stack") diff --git a/libraries/AdafruitFeather52Examples/library.properties b/libraries/AdafruitFeather52Examples/library.properties index 9b070238..25133437 100644 --- a/libraries/AdafruitFeather52Examples/library.properties +++ b/libraries/AdafruitFeather52Examples/library.properties @@ -2,8 +2,8 @@ name=Adafruit Feather52 Examples version=0.1.0 author=Adafruit maintainer=Adafruit -sentence=Adafruit Bluefruit Feather52 Examples -paragraph=Collection of examples for Bluefruit Feather52 Board +sentence=Adafruit Bluefruit52 HW Examples +paragraph=Collection of hardware examples for Bluefruit Feather52 Board category=Communication url=http://www.adafruit.com/ architectures=nRF52 diff --git a/platform.txt b/platform.txt index c7944013..15b33bd7 100644 --- a/platform.txt +++ b/platform.txt @@ -82,7 +82,7 @@ recipe.S.o.pattern="{compiler.path}{compiler.S.cmd}" {compiler.S.flags} -DF_CPU= recipe.ar.pattern="{compiler.path}{compiler.ar.cmd}" {compiler.ar.flags} {compiler.ar.extra_flags} "{archive_file_path}" "{object_file}" ## Combine gc-sections, archives, and objects -recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} "-L{nrf.sdk.path}/components/toolchain/gcc/" "-L{nrf.sdk.path}/components/softdevice/s132/toolchain/armgcc/" "-T{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs --specs=nosys.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" {object_files} -Wl,--start-group -lm "{build.path}/{archive_file}" -Wl,--end-group +recipe.c.combine.pattern="{compiler.path}{compiler.c.elf.cmd}" "-L{build.path}" {compiler.c.elf.flags} {compiler.c.elf.extra_flags} "-L{build.variant.path}" "-L{build.core.path}" "-T{build.ldscript}" "-Wl,-Map,{build.path}/{build.project_name}.map" --specs=nano.specs --specs=nosys.specs {compiler.ldflags} -o "{build.path}/{build.project_name}.elf" {object_files} -Wl,--start-group -lm "{build.path}/{archive_file}" -Wl,--end-group ## Create output (bin file) #recipe.objcopy.bin.pattern="{compiler.path}{compiler.elf2bin.cmd}" {compiler.elf2bin.flags} {compiler.elf2bin.extra_flags} "{build.path}/{build.project_name}.elf" "{build.path}/{build.project_name}.bin" diff --git a/variants/feather52/feather52_s132.ld b/variants/feather52/feather52_s132.ld new file mode 100755 index 00000000..4bf25d55 --- /dev/null +++ b/variants/feather52/feather52_s132.ld @@ -0,0 +1,33 @@ +/* Linker script to configure memory regions. */ + +SEARCH_DIR(.) +GROUP(-lgcc -lc -lnosys) + +MEMORY +{ + FLASH (rx) : ORIGIN = 0x1c000, LENGTH = 0x64000 + + /* ATTR SIZE = 0x0580, mid bandwidth (central, prph, sec) = (0, 1, 0) */ + /* RAM (rwx) : ORIGIN = 0x20002080, LENGTH = 0xdf80 */ + + /* ATTR SIZE = 0x1000, mid bandwidth (central, prph, sec) = (0, 1, 0) */ + /* RAM (rwx) : ORIGIN = 0x20002B00, LENGTH = 0xD500 */ + + /* ATTR SIZE = 0x0B00, mid bandwidth (central, prph, sec) = (0, 1, 0) */ + RAM (rwx) : ORIGIN = 0x20002600, LENGTH = 0xDA00 + + /* ATTR SIZE = 0x0B00, mid bandwidth (central, prph, sec) = (4, 1, 4) */ + /*RAM (rwx) : ORIGIN = 0x20002600, LENGTH = 0xDA00*/ +} + +SECTIONS +{ + .fs_data : + { + PROVIDE(__start_fs_data = .); + KEEP(*(.fs_data)) + PROVIDE(__stop_fs_data = .); + } > RAM +} INSERT AFTER .data; + +INCLUDE "nrf5x_common.ld" \ No newline at end of file