aboutsummaryrefslogtreecommitdiff
path: root/lib/pbdrv
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pbdrv')
-rw-r--r--lib/pbdrv/CMakeLists.txt9
-rw-r--r--lib/pbdrv/drv/arduino/mod.cpp4
-rw-r--r--lib/pbdrv/mpack-config.h19
-rw-r--r--lib/pbdrv/pb-mem.c31
-rw-r--r--lib/pbdrv/pb-mem.h20
-rw-r--r--lib/pbdrv/pb-route.c8
-rw-r--r--lib/pbdrv/pb-send.c2
-rw-r--r--lib/pbdrv/pb-types.h3
8 files changed, 75 insertions, 21 deletions
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