From 573ee2dd6790c00c9e0f709f222bc2a68f015d6d Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 18 Jun 2024 14:51:43 +0200 Subject: WIP losing my mind --- lib/mpack/CMakeLists.txt | 4 +++- lib/pbdrv/CMakeLists.txt | 9 ++++++--- lib/pbdrv/drv/arduino/mod.cpp | 4 ++++ lib/pbdrv/mpack-config.h | 19 ++++++++++++++++--- lib/pbdrv/pb-mem.c | 31 +++++++++++++++++++++++++++++++ lib/pbdrv/pb-mem.h | 20 ++++++++++---------- lib/pbdrv/pb-route.c | 8 ++++---- lib/pbdrv/pb-send.c | 2 +- lib/pbdrv/pb-types.h | 3 +++ 9 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 lib/pbdrv/pb-mem.c (limited to 'lib') diff --git a/lib/mpack/CMakeLists.txt b/lib/mpack/CMakeLists.txt index 0e4359d..4badc7b 100644 --- a/lib/mpack/CMakeLists.txt +++ b/lib/mpack/CMakeLists.txt @@ -7,6 +7,8 @@ cmake_minimum_required(VERSION 3.29) set(CMAKE_C_STANDARD 11) set(CMAKE_EXPORT_COMPILE_COMMANDS 1) +# set(CMAKE_BUILD_TYPE Debug) + project(mpack C) add_library(mpack STATIC @@ -24,5 +26,5 @@ target_include_directories(mpack SYSTEM INTERFACE ) # causes some wild crashes, please leave off -add_compile_definitions(MPACK_READ_TRACKING=0) +target_compile_definitions(mpack PRIVATE MPACK_READ_TRACKING=0) diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt index 0c09e67..d3ccebc 100644 --- a/lib/pbdrv/CMakeLists.txt +++ b/lib/pbdrv/CMakeLists.txt @@ -11,8 +11,10 @@ add_compile_definitions(DEBUG) project(pbdrv C CXX) add_subdirectory(lib/mpack) -add_compile_definitions(MPACK_HAS_CONFIG=1) -target_include_directories(mpack INTERFACE .) # mpack-config.h + +# mpack-config.h +target_compile_definitions(mpack PRIVATE MPACK_HAS_CONFIG=1) +target_include_directories(mpack PRIVATE .) # generic puzzle bus message handling library functions add_library(pbdrv STATIC @@ -25,13 +27,14 @@ target_compile_definitions(pbdrv PRIVATE PB_TARGET_STDLIB) target_link_libraries(pbdrv mpack) # puzzle bus *module* specific code -add_library(pbdrv-mod STATIC +add_library(pbdrv-mod OBJECT pb-msg.c pb-serial.c pb-buf.c pb-mod.c pb-send.c pb-route.c + pb-mem.c ) target_include_directories(pbdrv-mod SYSTEM INTERFACE .) target_compile_definitions(pbdrv-mod PRIVATE PB_TARGET_FREERTOS) diff --git a/lib/pbdrv/drv/arduino/mod.cpp b/lib/pbdrv/drv/arduino/mod.cpp index 9130334..328c3c7 100644 --- a/lib/pbdrv/drv/arduino/mod.cpp +++ b/lib/pbdrv/drv/arduino/mod.cpp @@ -82,3 +82,7 @@ int main(void) { * puzzle modules are likely not using USB. */ +void pb_null_err(const char * from) { + Serial.println(from); +} + diff --git a/lib/pbdrv/mpack-config.h b/lib/pbdrv/mpack-config.h index b8e806c..7e7d0b2 100644 --- a/lib/pbdrv/mpack-config.h +++ b/lib/pbdrv/mpack-config.h @@ -2,7 +2,20 @@ #include "pb-mem.h" -#define MPACK_FREE(ptr) pb_free(ptr) -#define MPACK_MALLOC(sz) pb_malloc(sz) -#define MPACK_REALLOC(ptr, sz) pb_realloc(ptr, sz) +// use pb_* functions +#define MPACK_STDLIB 0 + +// pb_* memory management functions +#define MPACK_FREE pb_free +#define MPACK_MALLOC pb_malloc +#define MPACK_REALLOC pb_realloc + +// more reasonable buffer size (all messages are small) +#define MPACK_BUFFER_SIZE 80 +#define MPACK_STACK_SIZE 80 +#define MPACK_PAGE_SIZE 80 + +// // disable unused features (causes errors?) +// #define MPACK_NODE 0 +// #define MPACK_BUILDER 0 diff --git a/lib/pbdrv/pb-mem.c b/lib/pbdrv/pb-mem.c new file mode 100644 index 0000000..2ef4419 --- /dev/null +++ b/lib/pbdrv/pb-mem.c @@ -0,0 +1,31 @@ +#include "pb-mem.h" + +#ifdef PB_TARGET_FREERTOS +#include +#define _pb_free vPortFree +#define _pb_malloc pvPortMalloc +#endif + +#ifdef PB_TARGET_STDLIB +#include +#define _pb_free free +#define _pb_malloc malloc +#define _pb_realloc realloc +#endif + +inline void * pb_malloc(size_t sz) { + return _pb_malloc(sz); +} + +inline void pb_free(void * ptr) { + _pb_free(ptr); +} + +inline void * pb_realloc(void * ptr, size_t sz) { +#ifdef _pb_realloc + return _pb_realloc(ptr, sz); +#else + return NULL; // shit out of luck +#endif +} + diff --git a/lib/pbdrv/pb-mem.h b/lib/pbdrv/pb-mem.h index 9bf98cb..0da040c 100644 --- a/lib/pbdrv/pb-mem.h +++ b/lib/pbdrv/pb-mem.h @@ -1,16 +1,16 @@ #pragma once -#ifdef PB_TARGET_FREERTOS -#include -#define pb_free(ptr) vPortFree(ptr) -#define pb_malloc(sz) pvPortMalloc(sz) -#define pb_realloc(ptr, sz) pvPortRealloc(ptr, sz) +#include "pb-types.h" + +#ifdef __cplusplus +extern "C" { #endif -#ifdef PB_TARGET_STDLIB -#include -#define pb_free(ptr) free(ptr) -#define pb_malloc(sz) malloc(sz) -#define pb_realloc(ptr, sz) realloc(ptr, sz) +void * pb_malloc(size_t sz); +void pb_free(void * ptr); +void * pb_realloc(void * ptr, size_t sz); + +#ifdef __cplusplus +} #endif diff --git a/lib/pbdrv/pb-route.c b/lib/pbdrv/pb-route.c index 84d0fa0..e991ef4 100644 --- a/lib/pbdrv/pb-route.c +++ b/lib/pbdrv/pb-route.c @@ -6,8 +6,6 @@ #include "pb-send.h" #include "pb-types.h" -#include - __weak bool pb_hook_route_msg(pb_msg_t * msg) { return false; } __weak void pb_route_msg(pb_msg_t * msg) { if (pb_hook_route_msg(msg)) return; @@ -84,9 +82,11 @@ __weak void pb_route_cmd_state_set(pb_msg_t * msg) { __weak void pb_route_cmd_magic_req(pb_msg_t * msg) { pb_cmd_magic_t * cmd = msg->cmd; - // return early if magic is invalid + // return early if magic has wrong size if (cmd->_magic_size != sizeof(pb_cmd_magic_req)) return; - if (memcmp(cmd->magic, pb_cmd_magic_req, cmd->_magic_size) != 0) return; + // // return early if magic doesn't match + // for (size_t i = 0; i < sizeof(pb_cmd_magic_req); i++) + // if (cmd->magic[i] != pb_cmd_magic_req[i]) return; // FIXME: this should be removed (see handover: RP2040 I2C limitations) vTaskDelay(2000 / portTICK_PERIOD_MS); diff --git a/lib/pbdrv/pb-send.c b/lib/pbdrv/pb-send.c index b70af3a..66c43c1 100644 --- a/lib/pbdrv/pb-send.c +++ b/lib/pbdrv/pb-send.c @@ -2,7 +2,7 @@ #include "pb-mod.h" #include "pb-msg.h" -void pb_send_reply(pb_msg_t * msg, pb_buf_t * reply) { +__weak void pb_send_reply(pb_msg_t * msg, pb_buf_t * reply) { return pb_i2c_send(msg->sender, (uint8_t *) reply->data, reply->size); } diff --git a/lib/pbdrv/pb-types.h b/lib/pbdrv/pb-types.h index 4d085f9..fab9028 100644 --- a/lib/pbdrv/pb-types.h +++ b/lib/pbdrv/pb-types.h @@ -75,6 +75,9 @@ typedef struct { size_t _magic_size; //!< [META] size of \p magic } pb_cmd_magic_t; +// DEBUG: remove +void pb_null_err(const char * from); + #ifdef __cplusplus } #endif -- cgit v1.2.3