diff --git a/package/STM32G030Booter/main.c b/package/STM32G030Booter/main.c index 8a46aea0a..53dc0d050 100644 --- a/package/STM32G030Booter/main.c +++ b/package/STM32G030Booter/main.c @@ -68,18 +68,8 @@ uint8_t Shell_Ready = 0; * @retval int */ -#define use_mem_pool - -#ifdef use_mem_pool -/* use mem pool */ -Pool pikaPool; -void* __impl_pikaMalloc(size_t size) { - void* mem = pool_malloc(&pikaPool, size); - return mem; -} -void __impl_pikaFree(void* ptrm, size_t size) { - pool_free(&pikaPool, ptrm, size); -} +#if use_mem_pool +extern Pool pikaPool; #endif int main(void) { @@ -93,13 +83,14 @@ int main(void) { SystemClock_Config(); MX_GPIO_Init(); HARDWARE_PRINTF_Init(); - - /* init mem pool */ - #ifdef use_mem_pool - pikaPool = pool_init(0x1B00, 4); - #endif - printf("stm32 hardware init ok\r\n"); + printf("[info]: stm32 hardware init ok\r\n"); + /* init mem pool */ + #if use_mem_pool + pikaPool = pool_init(0x1B00, 4); + printf("[info]: pika memory poool init ok \r\n"); + #endif + /* boot pikaScript */ char* code = (char*)FLASH_SCRIPT_START_ADDR; if (code[0] != 0xFF) { diff --git a/package/STM32G030Booter/stm32g030_pika_msp.c b/package/STM32G030Booter/stm32g030_pika_msp.c index a3b42d563..c74c241a3 100644 --- a/package/STM32G030Booter/stm32g030_pika_msp.c +++ b/package/STM32G030Booter/stm32g030_pika_msp.c @@ -163,7 +163,25 @@ int32_t __platformSavePikaAsmEOF() { return 0; } +/* support mem pool */ +#if use_mem_pool +/* use mem pool */ +Pool pikaPool; +void* __impl_pikaMalloc(size_t size) { + void* mem = pool_malloc(&pikaPool, size); + return mem; +} +void __impl_pikaFree(void* ptrm, size_t size) { + pool_free(&pikaPool, ptrm, size); +} +#endif + /* support download python script by uart1 */ +uint8_t pika_memory_lock = 0; +uint8_t __isLocked_pikaMemory(void){ + return pika_memory_lock; +} + CodeHeap codeHeap; void STM32_Code_Init(void) { codeHeap.size = 0; @@ -182,16 +200,16 @@ uint8_t STM32_Code_reciveHandler(char* data, uint32_t rxSize) { } } if (1 == codeHeap.ena) { + if(!pika_memory_lock){ + pika_memory_lock = 1; + #if use_mem_pool + pool_deinit(&pikaPool); + #endif + } codeHeap.reciveTime = uwTick; codeHeap.oldSize = codeHeap.size; codeHeap.size += rxSize; - /* copy old to new content */ - char* new_content = pikaMalloc(codeHeap.size + 1); - memcpy(new_content, codeHeap.content, codeHeap.oldSize + 1); - pikaFree(codeHeap.content, codeHeap.oldSize + 1); - /* update new content */ - codeHeap.content = new_content; - /* copy append content to new content */ + codeHeap.content = realloc(codeHeap.content, codeHeap.size + 1); memcpy(codeHeap.content + codeHeap.oldSize, data, rxSize); codeHeap.content[codeHeap.size] = 0; /* reciving code */ diff --git a/package/STM32G030Booter/stm32g030_pika_msp.h b/package/STM32G030Booter/stm32g030_pika_msp.h index 74484b222..0077b42cb 100644 --- a/package/STM32G030Booter/stm32g030_pika_msp.h +++ b/package/STM32G030Booter/stm32g030_pika_msp.h @@ -1,6 +1,8 @@ #ifndef __STM32G030_PIKA_PORT__H #define __STM32G030_PIKA_PORT__H +#define use_mem_pool 1 + #include #include "pikaObj.h" typedef struct _CodeHeap { diff --git a/src/PikaPlatform.c b/src/PikaPlatform.c index ceca6e1d9..c853edd2d 100644 --- a/src/PikaPlatform.c +++ b/src/PikaPlatform.c @@ -54,6 +54,10 @@ PIKA_WEAK void __impl_pikaFree(void* ptrm, size_t size) { __platformFree(ptrm); } +PIKA_WEAK uint8_t __isLocked_pikaMemory(void){ + return 0; +} + PIKA_WEAK void __platformPrintf(char* fmt, ...) { va_list args; va_start(args, fmt); diff --git a/src/PikaPlatform.h b/src/PikaPlatform.h index 293be5ff1..e40a4979c 100644 --- a/src/PikaPlatform.h +++ b/src/PikaPlatform.h @@ -46,10 +46,10 @@ void __platformDisableIrqHandle(void); void* __impl_pikaMalloc(size_t size); void __impl_pikaFree(void* ptrm, size_t size); +uint8_t __isLocked_pikaMemory(void); char* __platformLoadPikaAsm(void); int32_t __platformSavePikaAsm(char* PikaAsm); uint8_t __platformAsmIsToFlash(char* pyMultiLine); int32_t __platformSavePikaAsmEOF(void); - #endif diff --git a/src/dataMemory.c b/src/dataMemory.c index 6e23a604a..54475cb25 100644 --- a/src/dataMemory.c +++ b/src/dataMemory.c @@ -32,6 +32,9 @@ PikaMemInfo pikaMemInfo = {0}; void* pikaMalloc(uint32_t size) { + /* pika memory lock */ + while(__isLocked_pikaMemory()){ + } pikaMemInfo.heapUsed += size; if (pikaMemInfo.heapUsedMax < pikaMemInfo.heapUsed) { pikaMemInfo.heapUsedMax = pikaMemInfo.heapUsed; @@ -49,6 +52,9 @@ void* pikaMalloc(uint32_t size) { } void pikaFree(void* mem, uint32_t size) { + /* pika memory lock */ + while(__isLocked_pikaMemory()){ + } __platformDisableIrqHandle(); __impl_pikaFree(mem, size); __platformEnableIrqHandle();