Skip to content

Commit e400dc8

Browse files
committed
fix newlib nano _sbrk() is not linked for checking heap
1 parent 2a41855 commit e400dc8

File tree

6 files changed

+53
-98
lines changed

6 files changed

+53
-98
lines changed

cores/nRF5/common_func.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,12 +114,6 @@
114114
//--------------------------------------------------------------------+
115115
// DEBUG HELPER
116116
//--------------------------------------------------------------------+
117-
#if CFG_DEBUG == 2
118-
#define malloc_named( name, size ) ({ PRINTF("[malloc] %s : %d\r\n", name, size); malloc(size); })
119-
#else
120-
#define malloc_named( name, size ) malloc ( size )
121-
#endif
122-
123117
const char* dbg_err_str(int32_t err_id); // TODO move to other place
124118

125119
#if CFG_DEBUG

cores/nRF5/linker/nrf52832_s132_v6.ld

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ GROUP(-lgcc -lc -lnosys)
55

66
MEMORY
77
{
8-
FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0x6D000-0x26000
8+
FLASH (rx) : ORIGIN = 0x26000, LENGTH = 0x6D000 - 0x26000
99

1010
/* SRAM required by S132 depend on
1111
* - Attribute Table Size

cores/nRF5/new.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@
1919
#include <stdlib.h>
2020
#include "rtos.h"
2121

22+
#include <errno.h>
23+
#include <sys/stat.h>
24+
#include <malloc.h>
25+
2226
void *operator new(size_t size) {
2327
return rtos_malloc(size);
2428
}
@@ -42,3 +46,46 @@ void operator delete(void * ptr, unsigned int) {
4246
void operator delete[](void * ptr, unsigned int) {
4347
rtos_free(ptr);
4448
}
49+
50+
51+
extern "C"
52+
{
53+
54+
// defined in linker script
55+
extern unsigned char __HeapBase[];
56+
extern unsigned char __HeapLimit[];
57+
58+
static unsigned char *sbrk_heap_top = __HeapBase;
59+
60+
__attribute__((used))
61+
caddr_t _sbrk( int incr )
62+
{
63+
unsigned char *prev_heap;
64+
65+
if ( sbrk_heap_top + incr > __HeapLimit )
66+
{
67+
/* Out of dynamic memory heap space */
68+
errno = ENOMEM;
69+
return (caddr_t) -1;
70+
}
71+
72+
prev_heap = sbrk_heap_top;
73+
74+
sbrk_heap_top += incr;
75+
76+
return (caddr_t) prev_heap;
77+
}
78+
79+
void __malloc_lock(struct _reent *ptr)
80+
{
81+
(void) ptr;
82+
vTaskSuspendAll();
83+
}
84+
85+
void __malloc_unlock(struct _reent *ptr)
86+
{
87+
(void) ptr;
88+
xTaskResumeAll();
89+
}
90+
91+
}

cores/nRF5/rtos.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,14 @@ void yield(void)
9393

9494
void vApplicationStackOverflowHook( TaskHandle_t xTask, char *pcTaskName )
9595
{
96-
LOG_LV1("RTOS", "%s Stack Overflow !!!", pcTaskName);
97-
while(1) yield();
96+
LOG_LV1("RTOS", "Task %s: stack Overflow !!!", pcTaskName);
97+
while(CFG_DEBUG) yield();
9898
}
9999

100100
void vApplicationMallocFailedHook(void)
101101
{
102-
LOG_LV1("RTOS", "Failed to Malloc");
102+
LOG_LV1("RTOS", "Task %s: failed to Malloc", pcTaskGetName(xTaskGetCurrentTaskHandle()));
103+
while(CFG_DEBUG) yield();
103104
}
104105

105106
/* configSUPPORT_STATIC_ALLOCATION is set to 1, so the application must provide an

cores/nRF5/syscall_newlib.c

Lines changed: 0 additions & 87 deletions
This file was deleted.

cores/nRF5/utility/adafruit_fifo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Adafruit_FIFO::Adafruit_FIFO(uint8_t item_size, uint16_t depth)
6060

6161
void Adafruit_FIFO::begin(void)
6262
{
63-
_buffer = (uint8_t*) malloc(_item_size*_depth);
63+
_buffer = (uint8_t*) rtos_malloc(_item_size*_depth);
6464
_mutex = xSemaphoreCreateMutex();
6565
}
6666

0 commit comments

Comments
 (0)