From 9c9f77a2a9cd02db6fbaf92c7052907a3e72e5ee Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Sun, 20 Mar 2022 07:35:15 +0100 Subject: [PATCH] Do not allocate memory for LUA Rx fifo from the USB ISR Rather use explicit allocation when the serial is set as the on-demand method that was used previously. --- radio/src/lua/api_general.cpp | 18 +++++++++++++++--- radio/src/lua/lua_api.h | 3 +++ radio/src/serial.cpp | 3 +++ 3 files changed, 21 insertions(+), 3 deletions(-) diff --git a/radio/src/lua/api_general.cpp b/radio/src/lua/api_general.cpp index 5d92ed46e..7476550f8 100644 --- a/radio/src/lua/api_general.cpp +++ b/radio/src/lua/api_general.cpp @@ -103,13 +103,25 @@ static int luaRxFifoGetByte(void*, uint8_t* data) return luaRxFifo->pop(*data); } -void luaReceiveData(uint8_t* buf, uint32_t len) +void luaAllocRxFifo() { if (!luaRxFifo) { - luaRxFifo = new Fifo(); + auto fifo = new Fifo(); + luaRxFifo = fifo; luaSetGetSerialByte(nullptr, luaRxFifoGetByte); } - +} + +void luaFreeRxFifo() +{ + auto fifo = luaRxFifo; + luaSetGetSerialByte(nullptr, nullptr); + luaRxFifo = nullptr; + delete(fifo); +} + +void luaReceiveData(uint8_t* buf, uint32_t len) +{ if (luaRxFifo) { while(len--) luaRxFifo->push(*buf++); } diff --git a/radio/src/lua/lua_api.h b/radio/src/lua/lua_api.h index 4506c4b2f..38ffe4e74 100644 --- a/radio/src/lua/lua_api.h +++ b/radio/src/lua/lua_api.h @@ -50,7 +50,10 @@ extern "C" { // LUA serial connection #define LUA_FIFO_SIZE 256 +void luaAllocRxFifo(); +void luaFreeRxFifo(); void luaReceiveData(uint8_t* buf, uint32_t len); + void luaSetSendCb(void* ctx, void (*cb)(void*, uint8_t)); void luaSetGetSerialByte(void* ctx, int (*fct)(void*, uint8_t*)); diff --git a/radio/src/serial.cpp b/radio/src/serial.cpp index acfebc253..45ba48871 100644 --- a/radio/src/serial.cpp +++ b/radio/src/serial.cpp @@ -148,7 +148,10 @@ static void serialSetCallBacks(int mode, void* ctx, const etx_serial_port_t* por if (getByte) { luaSetGetSerialByte(ctx, getByte); } else if (setRxCb) { + luaAllocRxFifo(); setRxCb(ctx, luaReceiveData); + } else { + luaFreeRxFifo(); } break; #endif