Skip to content

Commit 4aa51f3

Browse files
Merge branch 'main' into stopwatch-persistence
2 parents 5c2dba1 + d69cfcf commit 4aa51f3

File tree

8 files changed

+105
-23
lines changed

8 files changed

+105
-23
lines changed

src/CMakeLists.txt

+4
Original file line numberDiff line numberDiff line change
@@ -791,6 +791,10 @@ add_definitions(-DFREERTOS)
791791
add_definitions(-D__STACK_SIZE=1024)
792792
add_definitions(-D__HEAP_SIZE=0)
793793
add_definitions(-DMYNEWT_VAL_BLE_LL_RFMGMT_ENABLE_TIME=1500)
794+
add_definitions(-DLFS_CONFIG=libs/lfs_config.h)
795+
796+
# _sbrk is purposefully not implemented so that builds fail when it is used
797+
add_link_options(-Wl,-wrap=malloc -Wl,-wrap=free -Wl,-wrap=calloc -Wl,-wrap=realloc -Wl,-wrap=_malloc_r -Wl,-wrap=_sbrk)
794798

795799
# Note: Only use this for debugging
796800
# Derive the low frequency clock from the main clock (SYNT)

src/FreeRTOS/heap_4_infinitime.c

+16-12
Original file line numberDiff line numberDiff line change
@@ -60,15 +60,6 @@ task.h is included from an application file. */
6060
/* Assumes 8bit bytes! */
6161
#define heapBITS_PER_BYTE ( ( size_t ) 8 )
6262

63-
/* Allocate the memory for the heap. */
64-
#if( configAPPLICATION_ALLOCATED_HEAP == 1 )
65-
/* The application writer has already defined the array used for the RTOS
66-
heap - probably so it can be placed in a special segment or address. */
67-
extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
68-
#else
69-
static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ];
70-
#endif /* configAPPLICATION_ALLOCATED_HEAP */
71-
7263
/* Define the linked list structure. This is used to link free blocks in order
7364
of their memory address. */
7465
typedef struct A_BLOCK_LINK
@@ -113,6 +104,8 @@ application. When the bit is free the block is still part of the free heap
113104
space. */
114105
static size_t xBlockAllocatedBit = 0;
115106

107+
static size_t xHeapSize = 0;
108+
116109
/*-----------------------------------------------------------*/
117110

118111
void *pvPortMalloc( size_t xWantedSize )
@@ -332,27 +325,38 @@ size_t xPortGetMinimumEverFreeHeapSize( void )
332325
}
333326
/*-----------------------------------------------------------*/
334327

328+
size_t xPortGetHeapSize( void )
329+
{
330+
return xHeapSize;
331+
}
332+
/*-----------------------------------------------------------*/
333+
335334
void vPortInitialiseBlocks( void )
336335
{
337336
/* This just exists to keep the linker quiet. */
338337
}
339338
/*-----------------------------------------------------------*/
340339

340+
extern uint8_t *__HeapLimit; // Defined by nrf_common.ld
341+
341342
static void prvHeapInit( void )
342343
{
343344
BlockLink_t *pxFirstFreeBlock;
344345
uint8_t *pucAlignedHeap;
345346
size_t uxAddress;
346-
size_t xTotalHeapSize = configTOTAL_HEAP_SIZE;
347+
size_t xTotalHeapSize = ( size_t ) &__StackLimit - ( size_t ) &__HeapLimit;
348+
uint8_t *pucHeap = ( uint8_t * ) &__HeapLimit;
349+
350+
xHeapSize = xTotalHeapSize;
347351

348352
/* Ensure the heap starts on a correctly aligned boundary. */
349-
uxAddress = ( size_t ) ucHeap;
353+
uxAddress = ( size_t ) pucHeap;
350354

351355
if( ( uxAddress & portBYTE_ALIGNMENT_MASK ) != 0 )
352356
{
353357
uxAddress += ( portBYTE_ALIGNMENT - 1 );
354358
uxAddress &= ~( ( size_t ) portBYTE_ALIGNMENT_MASK );
355-
xTotalHeapSize -= uxAddress - ( size_t ) ucHeap;
359+
xTotalHeapSize -= uxAddress - ( size_t ) pucHeap;
356360
}
357361

358362
pucAlignedHeap = ( uint8_t * ) uxAddress;

src/FreeRTOS/portmacro_cmsis.h

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ __STATIC_INLINE uint32_t ulPortRaiseBASEPRI( void )
180180

181181
/*-----------------------------------------------------------*/
182182

183+
size_t xPortGetHeapSize(void);
183184

184185
#ifdef __cplusplus
185186
}

src/FreeRTOSConfig.h

-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@
6262
#define configTICK_RATE_HZ 1024
6363
#define configMAX_PRIORITIES (3)
6464
#define configMINIMAL_STACK_SIZE (120)
65-
#define configTOTAL_HEAP_SIZE (1024 * 39)
6665
#define configMAX_TASK_NAME_LEN (4)
6766
#define configUSE_16_BIT_TICKS 0
6867
#define configIDLE_SHOULD_YIELD 1

src/displayapp/screens/SystemInfo.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ std::unique_ptr<Screen> SystemInfo::CreateScreen3() {
195195
"#808080 SPI Flash# %02x-%02x-%02x\n"
196196
"\n"
197197
"#808080 Memory heap#\n"
198-
" #808080 Free# %d\n"
198+
" #808080 Free# %d/%d\n"
199199
" #808080 Min free# %d\n"
200200
" #808080 Alloc err# %d\n"
201201
" #808080 Ovrfl err# %d\n",
@@ -209,6 +209,7 @@ std::unique_ptr<Screen> SystemInfo::CreateScreen3() {
209209
spiFlashId.type,
210210
spiFlashId.density,
211211
xPortGetFreeHeapSize(),
212+
xPortGetHeapSize(),
212213
xPortGetMinimumEverFreeHeapSize(),
213214
mallocFailedCount,
214215
stackOverflowCount);

src/displayapp/screens/Weather.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -154,11 +154,11 @@ void Weather::Refresh() {
154154
std::tm localTime = *std::localtime(reinterpret_cast<const time_t*>(&optCurrentForecast->timestamp));
155155

156156
for (int i = 0; i < optCurrentForecast->nbDays; i++) {
157-
int16_t minTemp = optCurrentForecast->days[i]->minTemperature.Celsius();
158157
int16_t maxTemp = optCurrentForecast->days[i]->maxTemperature.Celsius();
158+
int16_t minTemp = optCurrentForecast->days[i]->minTemperature.Celsius();
159159
if (settingsController.GetWeatherFormat() == Controllers::Settings::WeatherFormat::Imperial) {
160-
minTemp = optCurrentForecast->days[i]->maxTemperature.Fahrenheit();
161-
maxTemp = optCurrentForecast->days[i]->minTemperature.Fahrenheit();
160+
maxTemp = optCurrentForecast->days[i]->maxTemperature.Fahrenheit();
161+
minTemp = optCurrentForecast->days[i]->minTemperature.Fahrenheit();
162162
}
163163
lv_table_set_cell_type(forecast, 2, i, TemperatureStyle(optCurrentForecast->days[i]->maxTemperature));
164164
lv_table_set_cell_type(forecast, 3, i, TemperatureStyle(optCurrentForecast->days[i]->minTemperature));

src/libs/lfs_config.h

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
#pragma once
2+
3+
#include <libraries/log/nrf_log.h>
4+
5+
#ifndef LFS_TRACE
6+
#ifdef LFS_YES_TRACE
7+
#define LFS_TRACE_(fmt, ...) \
8+
NRF_LOG_DEBUG("[LFS] %s:%d:trace: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
9+
#define LFS_TRACE(...) LFS_TRACE_(__VA_ARGS__, "")
10+
#else
11+
#define LFS_TRACE(...)
12+
#endif
13+
#endif
14+
15+
#ifndef LFS_DEBUG
16+
#ifndef LFS_NO_DEBUG
17+
#define LFS_DEBUG_(fmt, ...) \
18+
NRF_LOG_DEBUG("[LFS] %s:%d:debug: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
19+
#define LFS_DEBUG(...) LFS_DEBUG_(__VA_ARGS__, "")
20+
#else
21+
#define LFS_DEBUG(...)
22+
#endif
23+
#endif
24+
25+
#ifndef LFS_WARN
26+
#ifndef LFS_NO_WARN
27+
#define LFS_WARN_(fmt, ...) \
28+
NRF_LOG_WARNING("[LFS] %s:%d:warn: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
29+
#define LFS_WARN(...) LFS_WARN_(__VA_ARGS__, "")
30+
#else
31+
#define LFS_WARN(...)
32+
#endif
33+
#endif
34+
35+
#ifndef LFS_ERROR
36+
#ifndef LFS_NO_ERROR
37+
#define LFS_ERROR_(fmt, ...) \
38+
NRF_LOG_ERROR("[LFS] %s:%d:error: " fmt "%s\n", __FILE__, __LINE__, __VA_ARGS__)
39+
#define LFS_ERROR(...) LFS_ERROR_(__VA_ARGS__, "")
40+
#else
41+
#define LFS_ERROR(...)
42+
#endif
43+
#endif
44+
45+
// This is required in order for the CRC implementation in littlefs/lfs_util.c to be compiled
46+
#undef LFS_CONFIG
47+
48+
#undef LFS_UTIL_H
49+
#include <littlefs/lfs_util.h>

src/stdlib.c

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <stdlib.h>
2+
#include <string.h>
23
#include <FreeRTOS.h>
34

45
// Override malloc() and free() to use the memory manager from FreeRTOS.
@@ -10,18 +11,41 @@ void* malloc(size_t size) {
1011
return pvPortMalloc(size);
1112
}
1213

14+
void* __wrap_malloc(size_t size) {
15+
return malloc(size);
16+
}
17+
18+
void* __wrap__malloc_r(struct _reent* reent, size_t size) {
19+
(void) reent;
20+
return malloc(size);
21+
}
22+
1323
void free(void* ptr) {
1424
vPortFree(ptr);
1525
}
1626

27+
void __wrap_free(void* ptr) {
28+
free(ptr);
29+
}
30+
1731
void* calloc(size_t num, size_t size) {
18-
(void)(num);
19-
(void)(size);
20-
// Not supported
21-
return NULL;
32+
void *ptr = malloc(num * size);
33+
if (ptr) {
34+
memset(ptr, 0, num * size);
35+
}
36+
return ptr;
2237
}
2338

24-
void *pvPortRealloc(void *ptr, size_t xWantedSize);
25-
void* realloc( void *ptr, size_t newSize) {
39+
void* __wrap_calloc(size_t num, size_t size) {
40+
return calloc(num, size);
41+
}
42+
43+
void* pvPortRealloc(void* ptr, size_t xWantedSize);
44+
45+
void* realloc(void* ptr, size_t newSize) {
2646
return pvPortRealloc(ptr, newSize);
2747
}
48+
49+
void* __wrap_realloc(void* ptr, size_t newSize) {
50+
return realloc(ptr, newSize);
51+
}

0 commit comments

Comments
 (0)