USART tx length fix, some experiments with cpp string manip

This commit is contained in:
2024-12-16 12:29:13 +01:00
parent 7b7e783d68
commit 17099b0047
4 changed files with 39 additions and 96 deletions

View File

@@ -27,7 +27,7 @@
/**
* Pointer to the current high watermark of the heap usage
*/
static uint8_t *__sbrk_heap_end = NULL;
static uint8_t* __sbrk_heap_end = NULL;
/**
* @brief _sbrk() allocates memory to the newlib heap and is used by malloc
@@ -50,30 +50,27 @@ static uint8_t *__sbrk_heap_end = NULL;
* @param incr Memory size
* @return Pointer to allocated memory
*/
void *_sbrk(ptrdiff_t incr)
{
extern uint8_t _end; /* Symbol defined in the linker script */
extern uint8_t _estack; /* Symbol defined in the linker script */
extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
const uint8_t *max_heap = (uint8_t *)stack_limit;
uint8_t *prev_heap_end;
void* _sbrk(ptrdiff_t incr) {
extern uint8_t _end; /* Symbol defined in the linker script */
extern uint8_t _estack; /* Symbol defined in the linker script */
extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */
const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size;
const uint8_t* max_heap = (uint8_t*)stack_limit;
uint8_t* prev_heap_end;
/* Initialize heap end at first call */
if (NULL == __sbrk_heap_end)
{
__sbrk_heap_end = &_end;
}
/* Initialize heap end at first call */
if (NULL == __sbrk_heap_end) {
__sbrk_heap_end = &_end;
}
/* Protect heap from growing into the reserved MSP stack */
if (__sbrk_heap_end + incr > max_heap)
{
errno = ENOMEM;
return (void *)-1;
}
/* Protect heap from growing into the reserved MSP stack */
if (__sbrk_heap_end + incr > max_heap) {
errno = ENOMEM;
return (void*)-1;
}
prev_heap_end = __sbrk_heap_end;
__sbrk_heap_end += incr;
prev_heap_end = __sbrk_heap_end;
__sbrk_heap_end += incr;
return (void *)prev_heap_end;
return (void*)prev_heap_end;
}