From 3f3bef9c6bf0b9143da80e4b4363eb0362fb91c9 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 18 Jun 2024 17:29:25 +0200 Subject: clean up code for getting main controller compiling again --- lib/mpack/read-remaining.c | 4 ++-- lib/pbdrv/CMakeLists.txt | 20 +++++--------------- lib/pbdrv/drv/arduino/cfg.cmake | 3 +++ lib/pbdrv/drv/arduino/include.cmake | 12 ++++++++++++ lib/pbdrv/ext/freertos/include.cmake | 10 ++++++++++ lib/pbdrv/ext/freertos/pb-mem.c | 31 +++++++++++++++++++++++++++++++ lib/pbdrv/ext/freertos/pb-mod.c | 9 +++++++++ lib/pbdrv/ext/stdlib/include.cmake | 4 ++++ lib/pbdrv/ext/stdlib/pb-mem.c | 26 ++++++++++++++++++++++++++ lib/pbdrv/mpack-config.h | 2 ++ lib/pbdrv/pb-mem.c | 31 ------------------------------- lib/pbdrv/pb-mem.h | 3 +++ lib/pbdrv/pb-msg.c | 3 ++- lib/pbdrv/pb-route.c | 9 +++------ lib/pbdrv/pb-serial.c | 1 + 15 files changed, 113 insertions(+), 55 deletions(-) create mode 100644 lib/pbdrv/drv/arduino/include.cmake create mode 100644 lib/pbdrv/ext/freertos/include.cmake create mode 100644 lib/pbdrv/ext/freertos/pb-mem.c create mode 100644 lib/pbdrv/ext/freertos/pb-mod.c create mode 100644 lib/pbdrv/ext/stdlib/include.cmake create mode 100644 lib/pbdrv/ext/stdlib/pb-mem.c delete mode 100644 lib/pbdrv/pb-mem.c (limited to 'lib') diff --git a/lib/mpack/read-remaining.c b/lib/mpack/read-remaining.c index ebc9b56..46b5815 100644 --- a/lib/mpack/read-remaining.c +++ b/lib/mpack/read-remaining.c @@ -1,10 +1,10 @@ #include "mpack.h" size_t mpack_read_remaining_bytes(mpack_reader_t * reader, char * p, size_t count) { - size_t limit =mpack_reader_remaining(reader, NULL); + size_t limit = mpack_reader_remaining(reader, NULL); if (0 < count && count < limit) limit = count; - memcpy(p, reader->data, limit); + MPACK_MEMCPY(p, reader->data, limit); return limit; } diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt index d3ccebc..a973406 100644 --- a/lib/pbdrv/CMakeLists.txt +++ b/lib/pbdrv/CMakeLists.txt @@ -17,13 +17,12 @@ 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 +add_library(pbdrv OBJECT pb-msg.c pb-serial.c pb-buf.c ) target_include_directories(pbdrv SYSTEM INTERFACE .) -target_compile_definitions(pbdrv PRIVATE PB_TARGET_STDLIB) target_link_libraries(pbdrv mpack) # puzzle bus *module* specific code @@ -34,20 +33,11 @@ add_library(pbdrv-mod OBJECT 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) -target_link_libraries(pbdrv-mod - mpack - # freertos is used to defer the handling of i2c messages outside the receive - # interrupt service routine - freertos_kernel - freertos_kernel_include - freertos_config - ) +target_link_libraries(pbdrv-mod mpack) -# supported puzzle bus drivers -include(drv/arduino/cfg.cmake) -# include(drv/rp2040/cfg.cmake) # please see /main/pbdrv.h +# puzzle bus drivers +include(drv/arduino/include.cmake) +# include(drv/rp2040/include.cmake) # please see /main/pbdrv.h diff --git a/lib/pbdrv/drv/arduino/cfg.cmake b/lib/pbdrv/drv/arduino/cfg.cmake index 36716e3..47f54a4 100644 --- a/lib/pbdrv/drv/arduino/cfg.cmake +++ b/lib/pbdrv/drv/arduino/cfg.cmake @@ -5,3 +5,6 @@ endif() target_sources(pbdrv-mod PRIVATE "${CMAKE_CURRENT_LIST_DIR}/mod.cpp") target_link_arduino_libraries(pbdrv-mod core Wire) +# arduino must use freertos +include("${CMAKE_CURRENT_LIST_DIR}/../../ext/freertos/include.cmake") + diff --git a/lib/pbdrv/drv/arduino/include.cmake b/lib/pbdrv/drv/arduino/include.cmake new file mode 100644 index 0000000..50f56d6 --- /dev/null +++ b/lib/pbdrv/drv/arduino/include.cmake @@ -0,0 +1,12 @@ +if(NOT DEFINED ARDUINO) + return() +endif() + +target_compile_definitions(pbdrv-mod PRIVATE PB_TARGET_FREERTOS) +target_sources(pbdrv-mod PRIVATE "${CMAKE_CURRENT_LIST_DIR}/mod.cpp") +target_link_arduino_libraries(pbdrv-mod core Wire) + +# freertos is used to defer the handling of i2c messages outside the receive +# interrupt service routine +include("${CMAKE_CURRENT_LIST_DIR}/../../ext/freertos/include.cmake") + diff --git a/lib/pbdrv/ext/freertos/include.cmake b/lib/pbdrv/ext/freertos/include.cmake new file mode 100644 index 0000000..d01afe7 --- /dev/null +++ b/lib/pbdrv/ext/freertos/include.cmake @@ -0,0 +1,10 @@ +target_sources(pbdrv-mod PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/pb-mem.c" + "${CMAKE_CURRENT_LIST_DIR}/pb-mod.c" + ) +target_link_libraries(pbdrv-mod + freertos_kernel + freertos_kernel_include + freertos_config + ) + diff --git a/lib/pbdrv/ext/freertos/pb-mem.c b/lib/pbdrv/ext/freertos/pb-mem.c new file mode 100644 index 0000000..96c48d1 --- /dev/null +++ b/lib/pbdrv/ext/freertos/pb-mem.c @@ -0,0 +1,31 @@ +#include + +#include "../../pb-mem.h" +#include "../../pb-types.h" + +__weak inline void * pb_malloc(size_t sz) { + return pvPortMalloc(sz); +} + +__weak inline void pb_free(void * ptr) { + vPortFree(ptr); +} + +__weak inline void * pb_realloc(void * ptr, size_t sz) { + return NULL; // shit out of luck (don't use mpack_writer_init_growable) +} + +__weak void * pb_memcpy(void * dest, const void * src, size_t sz) { + for (size_t offset = 0; offset < sz; offset++) + *((char*) dest + offset) = *((char*) src + offset); + return dest; +} + +int pb_memcmp(const void * a, const void * b, size_t sz) { + for (size_t offset = 0; offset < sz; offset++) { + int diff = *((char*) a + offset) - *((char*) b + offset); + if (diff != 0) return diff; + } + return 0; +} + diff --git a/lib/pbdrv/ext/freertos/pb-mod.c b/lib/pbdrv/ext/freertos/pb-mod.c new file mode 100644 index 0000000..75495be --- /dev/null +++ b/lib/pbdrv/ext/freertos/pb-mod.c @@ -0,0 +1,9 @@ +#include +#include + +#include "../../pb-types.h" + +__weak void pb_mod_blocking_delay_ms(unsigned long ms) { + vTaskDelay(ms / portTICK_PERIOD_MS); +} + diff --git a/lib/pbdrv/ext/stdlib/include.cmake b/lib/pbdrv/ext/stdlib/include.cmake new file mode 100644 index 0000000..fce788d --- /dev/null +++ b/lib/pbdrv/ext/stdlib/include.cmake @@ -0,0 +1,4 @@ +target_sources(pbdrv-mod PRIVATE + "${CMAKE_CURRENT_LIST_DIR}/pb-mem.c" + ) + diff --git a/lib/pbdrv/ext/stdlib/pb-mem.c b/lib/pbdrv/ext/stdlib/pb-mem.c new file mode 100644 index 0000000..1f59a84 --- /dev/null +++ b/lib/pbdrv/ext/stdlib/pb-mem.c @@ -0,0 +1,26 @@ +#include +#include + +#include "../../pb-mem.h" +#include "../../pb-types.h" + +__weak inline void * pb_malloc(size_t sz) { + return malloc(sz); +} + +__weak inline void pb_free(void * ptr) { + free(ptr); +} + +__weak inline void * pb_realloc(void * ptr, size_t sz) { + return realloc(ptr, sz); +} + +__weak void * pb_memcpy(void * dest, const void * src, size_t sz) { + return memcpy(dest, src, sz); +} + +int pb_memcmp(const void * a, const void * b, size_t sz) { + return memcmp(a, b, sz); +} + diff --git a/lib/pbdrv/mpack-config.h b/lib/pbdrv/mpack-config.h index f040698..994d9b7 100644 --- a/lib/pbdrv/mpack-config.h +++ b/lib/pbdrv/mpack-config.h @@ -9,6 +9,8 @@ #define MPACK_FREE pb_free #define MPACK_MALLOC pb_malloc #define MPACK_REALLOC pb_realloc +#define MPACK_MEMCPY pb_memcpy +#define MPACK_MEMCMP pb_memcmp // more reasonable buffer size (all messages are small) #define MPACK_BUFFER_SIZE 256 diff --git a/lib/pbdrv/pb-mem.c b/lib/pbdrv/pb-mem.c deleted file mode 100644 index 2ef4419..0000000 --- a/lib/pbdrv/pb-mem.c +++ /dev/null @@ -1,31 +0,0 @@ -#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 0da040c..72be214 100644 --- a/lib/pbdrv/pb-mem.h +++ b/lib/pbdrv/pb-mem.h @@ -10,6 +10,9 @@ void * pb_malloc(size_t sz); void pb_free(void * ptr); void * pb_realloc(void * ptr, size_t sz); +void * pb_memcpy(void * dest, const void * src, size_t sz); +int pb_memcmp(const void * a, const void * b, size_t sz); + #ifdef __cplusplus } #endif diff --git a/lib/pbdrv/pb-msg.c b/lib/pbdrv/pb-msg.c index 1d7f90b..7fd6662 100644 --- a/lib/pbdrv/pb-msg.c +++ b/lib/pbdrv/pb-msg.c @@ -1,9 +1,10 @@ +#include "mpack-config.h" + #include #include "pb-msg.h" #include "pb-serial.h" #include "pb-mem.h" -#include "mpack-config.h" pb_buf_t pb_msg_write(const pb_msg_t * msg) { pb_buf_t buf = { 0 }; diff --git a/lib/pbdrv/pb-route.c b/lib/pbdrv/pb-route.c index e991ef4..ee47700 100644 --- a/lib/pbdrv/pb-route.c +++ b/lib/pbdrv/pb-route.c @@ -1,10 +1,8 @@ -#include -#include - #include "pb-route.h" #include "pb-mod.h" #include "pb-send.h" #include "pb-types.h" +#include "pb-mem.h" __weak bool pb_hook_route_msg(pb_msg_t * msg) { return false; } __weak void pb_route_msg(pb_msg_t * msg) { @@ -85,11 +83,10 @@ __weak void pb_route_cmd_magic_req(pb_msg_t * msg) { // return early if magic has wrong size if (cmd->_magic_size != sizeof(pb_cmd_magic_req)) 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; + if (pb_memcmp(cmd->magic, pb_cmd_magic_req, sizeof(pb_cmd_magic_req)) != 0) return; // FIXME: this should be removed (see handover: RP2040 I2C limitations) - vTaskDelay(2000 / portTICK_PERIOD_MS); + pb_mod_blocking_delay_ms(2000); pb_buf_t buf = pb_send_magic_res(); pb_send_reply(msg, &buf); diff --git a/lib/pbdrv/pb-serial.c b/lib/pbdrv/pb-serial.c index 689db0c..b9ee4b1 100644 --- a/lib/pbdrv/pb-serial.c +++ b/lib/pbdrv/pb-serial.c @@ -1,5 +1,6 @@ #include +#include "pb-mem.h" #include "pb-serial.h" #include "pb-types.h" -- cgit v1.2.3