From 752dca0a41581282498ccb0d3e2aedb15181e4a8 Mon Sep 17 00:00:00 2001 From: Loek Le Blansch Date: Tue, 18 Jun 2024 18:34:08 +0200 Subject: WIP main controller puzzle bus behavior + fix build system / client --- client/CMakeLists.txt | 1 + lib/pbdrv/CMakeLists.txt | 11 ++--------- lib/pbdrv/drv/arduino/cfg.cmake | 10 ---------- lib/pbdrv/drv/arduino/include.cmake | 1 - lib/pbdrv/ext/freertos/include.cmake | 9 ++++----- lib/pbdrv/ext/freertos/pb-mem.c | 6 +++--- lib/pbdrv/ext/stdlib/include.cmake | 4 +--- lib/pbdrv/ext/stdlib/pb-mem.c | 9 ++++----- lib/pbdrv/pb.h | 17 +++++++++-------- main/config.def.h | 20 +++++++++++++++----- main/i2c.c | 10 +++++++--- main/mod.c | 3 ++- puzzle/dummy/mod.c | 3 ++- 13 files changed, 50 insertions(+), 54 deletions(-) delete mode 100644 lib/pbdrv/drv/arduino/cfg.cmake diff --git a/client/CMakeLists.txt b/client/CMakeLists.txt index 7d492b0..989b837 100644 --- a/client/CMakeLists.txt +++ b/client/CMakeLists.txt @@ -13,6 +13,7 @@ project(pbc C CXX) add_subdirectory(lib/mpack) add_subdirectory(lib/i2ctcp) add_subdirectory(lib/pbdrv) +include(lib/pbdrv/ext/stdlib/include.cmake) add_executable(pbc main.cpp diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt index 91932b8..998ed4d 100644 --- a/lib/pbdrv/CMakeLists.txt +++ b/lib/pbdrv/CMakeLists.txt @@ -17,7 +17,7 @@ target_compile_definitions(mpack PRIVATE MPACK_HAS_CONFIG=1) target_include_directories(mpack PRIVATE .) # generic puzzle bus message handling library functions -add_library(pbdrv OBJECT +add_library(pbdrv STATIC pb-msg.c pb-serial.c pb-buf.c @@ -25,21 +25,14 @@ add_library(pbdrv OBJECT target_include_directories(pbdrv SYSTEM INTERFACE .) target_link_libraries(pbdrv mpack) -# TODO: pbdrv-mod should ideally only include sources specific to it, and link -# 'plain' pbdrv to provide the other functions. this seems to cause linker -# errors and I don't know why - # puzzle bus *module* specific code add_library(pbdrv-mod OBJECT - pb-msg.c - pb-serial.c - pb-buf.c pb-mod.c pb-send.c pb-route.c ) target_include_directories(pbdrv-mod SYSTEM INTERFACE .) -target_link_libraries(pbdrv-mod mpack) +target_link_libraries(pbdrv-mod pbdrv) # puzzle bus drivers include(drv/arduino/include.cmake) diff --git a/lib/pbdrv/drv/arduino/cfg.cmake b/lib/pbdrv/drv/arduino/cfg.cmake deleted file mode 100644 index 47f54a4..0000000 --- a/lib/pbdrv/drv/arduino/cfg.cmake +++ /dev/null @@ -1,10 +0,0 @@ -if(NOT DEFINED ARDUINO) - return() -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 index 50f56d6..1e2ff08 100644 --- a/lib/pbdrv/drv/arduino/include.cmake +++ b/lib/pbdrv/drv/arduino/include.cmake @@ -2,7 +2,6 @@ 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) diff --git a/lib/pbdrv/ext/freertos/include.cmake b/lib/pbdrv/ext/freertos/include.cmake index d01afe7..e7ab7fd 100644 --- a/lib/pbdrv/ext/freertos/include.cmake +++ b/lib/pbdrv/ext/freertos/include.cmake @@ -1,10 +1,9 @@ -target_sources(pbdrv-mod PRIVATE - "${CMAKE_CURRENT_LIST_DIR}/pb-mem.c" - "${CMAKE_CURRENT_LIST_DIR}/pb-mod.c" - ) -target_link_libraries(pbdrv-mod +target_sources(pbdrv PRIVATE "${CMAKE_CURRENT_LIST_DIR}/pb-mem.c") +target_link_libraries(pbdrv freertos_kernel freertos_kernel_include freertos_config ) +target_sources(pbdrv-mod PRIVATE "${CMAKE_CURRENT_LIST_DIR}/pb-mod.c") + diff --git a/lib/pbdrv/ext/freertos/pb-mem.c b/lib/pbdrv/ext/freertos/pb-mem.c index 96c48d1..b18d79f 100644 --- a/lib/pbdrv/ext/freertos/pb-mem.c +++ b/lib/pbdrv/ext/freertos/pb-mem.c @@ -3,11 +3,11 @@ #include "../../pb-mem.h" #include "../../pb-types.h" -__weak inline void * pb_malloc(size_t sz) { +inline void * pb_malloc(size_t sz) { return pvPortMalloc(sz); } -__weak inline void pb_free(void * ptr) { +inline void pb_free(void * ptr) { vPortFree(ptr); } @@ -21,7 +21,7 @@ __weak void * pb_memcpy(void * dest, const void * src, size_t sz) { return dest; } -int pb_memcmp(const void * a, const void * b, size_t sz) { +__weak 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; diff --git a/lib/pbdrv/ext/stdlib/include.cmake b/lib/pbdrv/ext/stdlib/include.cmake index fce788d..67fe80e 100644 --- a/lib/pbdrv/ext/stdlib/include.cmake +++ b/lib/pbdrv/ext/stdlib/include.cmake @@ -1,4 +1,2 @@ -target_sources(pbdrv-mod PRIVATE - "${CMAKE_CURRENT_LIST_DIR}/pb-mem.c" - ) +target_sources(pbdrv 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 index 1f59a84..b260c2c 100644 --- a/lib/pbdrv/ext/stdlib/pb-mem.c +++ b/lib/pbdrv/ext/stdlib/pb-mem.c @@ -2,21 +2,20 @@ #include #include "../../pb-mem.h" -#include "../../pb-types.h" -__weak inline void * pb_malloc(size_t sz) { +inline void * pb_malloc(size_t sz) { return malloc(sz); } -__weak inline void pb_free(void * ptr) { +inline void pb_free(void * ptr) { free(ptr); } -__weak inline void * pb_realloc(void * ptr, size_t sz) { +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) { +void * pb_memcpy(void * dest, const void * src, size_t sz) { return memcpy(dest, src, sz); } diff --git a/lib/pbdrv/pb.h b/lib/pbdrv/pb.h index b6efed0..0f2e9d1 100644 --- a/lib/pbdrv/pb.h +++ b/lib/pbdrv/pb.h @@ -10,13 +10,14 @@ #define PB_ADDR_ADA_NEO_3 0x30 #define PB_ADDR_ADA_NEO_4 0x32 -// TODO: ??? -#define PB_ADDR_MOD_NEOTRELLIS 0 -#define PB_ADDR_MOD_SOFTWARE 0 -#define PB_ADDR_MOD_HARDWARE 0 -#define PB_ADDR_MOD_VAULT 0 -// #define BUSADDR_MOD_AUTOMATION 0 +// Main controller +#define PB_ADDR_MOD_MAIN 0x08 -// main controller -#define PB_ADDR_MOD_MAIN 0x00 +// Puzzle modules +#define PB_ADDR_MOD_NEOTRELLIS 0x21 +#define PB_ADDR_MOD_SOFTWARE 0x22 +#define PB_ADDR_MOD_HARDWARE 0x23 +#define PB_ADDR_MOD_VAULT 0x24 +// #define BUSADDR_MOD_AUTOMATION 0x25 +#define PB_ADDR_MOD_DUMMY 0x69 diff --git a/main/config.def.h b/main/config.def.h index 3d325fe..e9503ed 100644 --- a/main/config.def.h +++ b/main/config.def.h @@ -56,11 +56,10 @@ #endif /** \} */ -#ifndef CFG_LED_PIN -//! status LED pin -#define CFG_LED_PIN CYW43_WL_GPIO_LED_PIN -#endif - +/** + * \name I2C configuration + * \{ + */ #ifndef CFG_SDA_PIN //! I^2^C SDA pin #define CFG_SDA_PIN 16 @@ -69,4 +68,15 @@ //! I^2^C SCL pin #define CFG_SCL_PIN 17 #endif +/** \} */ + +#ifndef CFG_LED_PIN +//! status LED pin +#define CFG_LED_PIN CYW43_WL_GPIO_LED_PIN +#endif + +#ifndef CFG_PB_MOD_MAX +//! maximum number of simultaniously connected puzzle modules +#define CFG_PB_MOD_MAX 8 +#endif diff --git a/main/i2c.c b/main/i2c.c index ccd6b46..612075b 100644 --- a/main/i2c.c +++ b/main/i2c.c @@ -9,15 +9,19 @@ #include "i2c.h" #include "pb-mod.h" #include "pbdrv.h" +#include "config.h" + +i2c_addr_t modules[CFG_PB_MOD_MAX]; +size_t modules_size = 0; void bus_task() { - vTaskDelay(1000 / portTICK_PERIOD_MS); - bus_scan(); vTaskDelete(NULL); } void pb_route_cmd_magic_res(pb_msg_t * msg) { - printf("got a magic response from 0x%02x!\n", msg->sender); + if (modules_size == CFG_PB_MOD_MAX) return; + modules[modules_size++] = msg->sender; + printf("i2c: registered puzzle module w/ address 0x%02x\n", msg->sender); } diff --git a/main/mod.c b/main/mod.c index b234f97..11a1bb7 100644 --- a/main/mod.c +++ b/main/mod.c @@ -1,5 +1,6 @@ +#include "pb.h" #include "pb-mod.h" const char * PB_MOD_NAME = "main controller"; -const i2c_addr_t PB_MOD_ADDR = 0x20; +const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_MAIN; diff --git a/puzzle/dummy/mod.c b/puzzle/dummy/mod.c index 018ea68..058a585 100644 --- a/puzzle/dummy/mod.c +++ b/puzzle/dummy/mod.c @@ -1,5 +1,6 @@ +#include "pb.h" #include "pb-mod.h" const char * PB_MOD_NAME = "dummy"; -const i2c_addr_t PB_MOD_ADDR = 0x69; +const i2c_addr_t PB_MOD_ADDR = PB_ADDR_MOD_DUMMY; -- cgit v1.2.3