diff options
-rw-r--r-- | lib/mpack/CMakeLists.txt | 4 | ||||
-rw-r--r-- | lib/pbdrv/CMakeLists.txt | 9 | ||||
-rw-r--r-- | lib/pbdrv/drv/arduino/mod.cpp | 4 | ||||
-rw-r--r-- | lib/pbdrv/mpack-config.h | 19 | ||||
-rw-r--r-- | lib/pbdrv/pb-mem.c | 31 | ||||
-rw-r--r-- | lib/pbdrv/pb-mem.h | 20 | ||||
-rw-r--r-- | lib/pbdrv/pb-route.c | 8 | ||||
-rw-r--r-- | lib/pbdrv/pb-send.c | 2 | ||||
-rw-r--r-- | lib/pbdrv/pb-types.h | 3 | ||||
-rw-r--r-- | puzzle/dummy/CMakeLists.txt | 1 | ||||
-rw-r--r-- | puzzle/dummy/FreeRTOSConfig.h | 2 | ||||
-rw-r--r-- | puzzle/dummy/main.cpp | 16 |
12 files changed, 95 insertions, 24 deletions
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 <FreeRTOS.h> +#define _pb_free vPortFree +#define _pb_malloc pvPortMalloc +#endif + +#ifdef PB_TARGET_STDLIB +#include <stdlib.h> +#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 <FreeRTOS.h> -#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 <stdlib.h> -#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 <string.h> - __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 diff --git a/puzzle/dummy/CMakeLists.txt b/puzzle/dummy/CMakeLists.txt index bcbab88..0d5e1bd 100644 --- a/puzzle/dummy/CMakeLists.txt +++ b/puzzle/dummy/CMakeLists.txt @@ -7,6 +7,7 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS 1) # enable debug features set(CMAKE_BUILD_TYPE Debug) add_compile_definitions(DEBUG) +# add_compile_options(-O0) # no optimizations # arduino set(CMAKE_TOOLCHAIN_FILE ${CMAKE_SOURCE_DIR}/lib/Arduino-CMake-Toolchain/Arduino-toolchain.cmake) diff --git a/puzzle/dummy/FreeRTOSConfig.h b/puzzle/dummy/FreeRTOSConfig.h index 1cfdd71..f398db9 100644 --- a/puzzle/dummy/FreeRTOSConfig.h +++ b/puzzle/dummy/FreeRTOSConfig.h @@ -20,7 +20,7 @@ // #define configTOTAL_HEAP_SIZE (1024) #define configTOTAL_HEAP_SIZE (7 * 1024) #define configCHECK_FOR_STACK_OVERFLOW 0 -#define configUSE_MALLOC_FAILED_HOOK 0 +#define configUSE_MALLOC_FAILED_HOOK 1 #define configUSE_DAEMON_TASK_STARTUP_HOOK 0 #define configGENERATE_RUN_TIME_STATS 0 #define configUSE_TRACE_FACILITY 0 diff --git a/puzzle/dummy/main.cpp b/puzzle/dummy/main.cpp index 06f7569..217ce94 100644 --- a/puzzle/dummy/main.cpp +++ b/puzzle/dummy/main.cpp @@ -5,16 +5,30 @@ #include "pb-mod.h" #include "pb-route.h" +#include "pb-send.h" const char * PB_MOD_NAME = "dummy"; const i2c_addr_t PB_MOD_ADDR = 0x69; void setup() { Serial.begin(115200); + + pb_buf_t buf = pb_send_magic_res(); + Serial.print("response bytes:"); + for (size_t i = 0; i < buf.size; i++) { + Serial.print(" "); + Serial.print(buf.data[i]); + } + Serial.print("\r\n"); + pb_buf_free(&buf); } void loop() { - Serial.write("Hello world!\r\n"); + Serial.write("."); vTaskDelay(1000 / portTICK_PERIOD_MS); } +void vApplicationMallocFailedHook(void) { + Serial.println("malloc failed!"); +} + |