aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/pbdrv/CMakeLists.txt37
-rw-r--r--lib/pbdrv/drv/arduino/cfg.cmake7
-rw-r--r--lib/pbdrv/drv/arduino/mod.h2
-rw-r--r--lib/pbdrv/drv/rp2040/cfg.cmake7
-rw-r--r--lib/pbdrv/drv/rp2040/mod.c50
-rw-r--r--lib/pbdrv/drv/rp2040/mod.h13
6 files changed, 87 insertions, 29 deletions
diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt
index dfaac01..e70cb5b 100644
--- a/lib/pbdrv/CMakeLists.txt
+++ b/lib/pbdrv/CMakeLists.txt
@@ -12,34 +12,17 @@ project(pbdrv C CXX)
add_subdirectory(../mpack ${CMAKE_CURRENT_BINARY_DIR}/mpack)
-if(DEFINED ARDUINO)
- set(PBDRV_ARDUINO true)
-endif()
-
-include_directories(.)
-add_library(pbdrv STATIC
- pb-read.c
- pb-write.c
- )
-target_link_libraries(pbdrv
- mpack
- )
+# generic puzzle bus (de)serializer library
+add_library(pbdrv STATIC pb-read.c pb-write.c)
+target_link_libraries(pbdrv mpack)
target_include_directories(pbdrv SYSTEM INTERFACE .)
-list(APPEND PBDRV_SRCS pb-mod.c)
-
-if(PBDRV_ARDUINO)
- list(APPEND PBDRV_SRCS drv/arduino/mod.cpp)
-endif()
-
-add_library(pbdrv-mod STATIC ${PBDRV_SRCS})
+# puzzle bus module specific code (superset of pbdrv)
+add_library(pbdrv-mod STATIC pb-mod.c)
target_link_libraries(pbdrv-mod pbdrv)
-# add_dependencies(pbdrv-mod pbdrv)
-
-if(PBDRV_ARDUINO)
- target_link_arduino_libraries(pbdrv-mod
- core
- Wire
- )
-endif()
+target_include_directories(pbdrv-mod SYSTEM INTERFACE .)
+
+# supported puzzle bus drivers
+include(drv/arduino/cfg.cmake)
+include(drv/rp2040/cfg.cmake)
diff --git a/lib/pbdrv/drv/arduino/cfg.cmake b/lib/pbdrv/drv/arduino/cfg.cmake
new file mode 100644
index 0000000..36716e3
--- /dev/null
+++ b/lib/pbdrv/drv/arduino/cfg.cmake
@@ -0,0 +1,7 @@
+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)
+
diff --git a/lib/pbdrv/drv/arduino/mod.h b/lib/pbdrv/drv/arduino/mod.h
index e545d9b..079941a 100644
--- a/lib/pbdrv/drv/arduino/mod.h
+++ b/lib/pbdrv/drv/arduino/mod.h
@@ -1,7 +1,5 @@
#pragma once
-#include "../../pb-mod.h"
-
#ifdef __cplusplus
extern "C" {
#endif
diff --git a/lib/pbdrv/drv/rp2040/cfg.cmake b/lib/pbdrv/drv/rp2040/cfg.cmake
new file mode 100644
index 0000000..0fbad18
--- /dev/null
+++ b/lib/pbdrv/drv/rp2040/cfg.cmake
@@ -0,0 +1,7 @@
+if(NOT PICO_PLATFORM STREQUAL "rp2040")
+ return()
+endif()
+
+target_sources(pbdrv-mod PRIVATE "${CMAKE_CURRENT_LIST_DIR}/mod.c")
+target_link_libraries(pbdrv-mod hardware_i2c pico_i2c_slave)
+
diff --git a/lib/pbdrv/drv/rp2040/mod.c b/lib/pbdrv/drv/rp2040/mod.c
new file mode 100644
index 0000000..26882f7
--- /dev/null
+++ b/lib/pbdrv/drv/rp2040/mod.c
@@ -0,0 +1,50 @@
+#include "mod.h"
+
+#include "../../pb.h"
+#include "../../pb-types.h"
+#include "../../pb-mod.h"
+
+#include <hardware/i2c.h>
+#include <hardware/gpio.h>
+#include <pico/i2c_slave.h>
+
+#define PBDRV_I2C i2c0
+#define BUF_SIZE 256
+
+// NOTE: this function is called from the I2C ISR, and should return as quickly
+// as possible.
+static void recv_event(i2c_inst_t *i2c, i2c_slave_event_t event) {
+ uint8_t data[BUF_SIZE];
+ size_t size = 0;
+ // pbdrv_i2c_recv(NULL, 0);
+
+ switch (event) {
+ case I2C_SLAVE_RECEIVE: {
+ if (size == BUF_SIZE) return;
+ data[size++] = i2c_read_byte_raw(PBDRV_I2C);
+ break;
+ }
+ case I2C_SLAVE_FINISH: {
+ // TODO: handle this w/ queue mechanism instead?
+ pbdrv_i2c_recv(data, size);
+ size = 0;
+ break;
+ }
+ default: break;
+ }
+}
+
+void pbdrv_setup() {
+ i2c_init(PBDRV_I2C, PB_CLOCK_SPEED_HZ);
+ i2c_slave_init(PBDRV_I2C, PBDRV_MOD_ADDR, &recv_event);
+}
+
+__weak void pbdrv_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) {
+ i2c_set_slave_mode(PBDRV_I2C, false, PBDRV_MOD_ADDR);
+
+ // false to write stop condition to i2c bus
+ i2c_write_timeout_us(PBDRV_I2C, addr, buf, sz, false, PB_TIMEOUT_US);
+
+ i2c_set_slave_mode(PBDRV_I2C, true, PBDRV_MOD_ADDR);
+}
+
diff --git a/lib/pbdrv/drv/rp2040/mod.h b/lib/pbdrv/drv/rp2040/mod.h
new file mode 100644
index 0000000..0cf2e63
--- /dev/null
+++ b/lib/pbdrv/drv/rp2040/mod.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//! puzzle bus driver setup
+void pbdrv_setup();
+
+#ifdef __cplusplus
+}
+#endif
+