aboutsummaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-10 16:40:07 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-10 16:40:07 +0200
commit0e6fb935a8f079089e7d524ff2aa98d9be798a15 (patch)
treec880ae8466761d34f5790be98d61ef4c879e9917 /main
parenta3ecfc2a6e6ace7bcb7666afc37ff97f9ce401b4 (diff)
rp2040 driver works (ugly)
Diffstat (limited to 'main')
-rw-r--r--main/CMakeLists.txt4
-rw-r--r--main/init.c2
-rw-r--r--main/pbdrv.c72
-rw-r--r--main/pbdrv.h41
4 files changed, 117 insertions, 2 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt
index 7a0b136..e71f419 100644
--- a/main/CMakeLists.txt
+++ b/main/CMakeLists.txt
@@ -23,18 +23,20 @@ add_executable(main
mod.c
tasks.c
blink.c
+ pbdrv.c
)
pico_enable_stdio_usb(main 1)
pico_enable_stdio_uart(main 0)
pico_add_extra_outputs(main)
-include_directories(lib/pico-sdk/lib/lwip/contrib/ports/freertos/include)
+# include_directories(lib/pico-sdk/lib/lwip/contrib/ports/freertos/include)
target_include_directories(main PRIVATE .)
target_link_libraries(main
pico_cyw43_arch_lwip_sys_freertos
pico_stdlib
+ pico_i2c_slave
hardware_i2c
FreeRTOS-Kernel
FreeRTOS-Kernel-Heap4
diff --git a/main/init.c b/main/init.c
index bd00c04..c1b6e9b 100644
--- a/main/init.c
+++ b/main/init.c
@@ -7,7 +7,7 @@
#include "config.h"
#include "init.h"
#include "tasks.h"
-#include "drv/rp2040/mod.h"
+#include "pbdrv.h"
static void init_stdio() {
stdio_init_all();
diff --git a/main/pbdrv.c b/main/pbdrv.c
new file mode 100644
index 0000000..9d9e499
--- /dev/null
+++ b/main/pbdrv.c
@@ -0,0 +1,72 @@
+#include "pbdrv.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>
+
+#include <FreeRTOS.h>
+#include <timers.h>
+
+#define PBDRV_I2C i2c0
+#define BUF_SIZE 256
+#define MSGS_MAX 4
+
+typedef struct {
+ uint8_t data[BUF_SIZE];
+ size_t size;
+} i2c_msg_buf_t;
+
+static i2c_msg_buf_t msgs[MSGS_MAX];
+static size_t msg_index = 0;
+
+static void async_pbdrv_i2c_recv(void * _msg, uint32_t _) {
+ i2c_msg_buf_t * msg = _msg;
+ pbdrv_i2c_recv(msg->data, msg->size);
+}
+
+static void msg_complete(i2c_msg_buf_t * msg) {
+ // defer pbdrv_i2c_recv call to FreeRTOS scheduler as pbdrv_i2c_recv takes
+ // too long to return from an ISR
+ xTimerPendFunctionCallFromISR(async_pbdrv_i2c_recv, msg, 0, NULL);
+
+ // prepare next message for use
+ msg_index = (msg_index + 1) % MSGS_MAX;
+ msgs[msg_index].size = 0;
+}
+
+// This function is called from the I2C ISR
+static void recv_event(i2c_inst_t *i2c, i2c_slave_event_t event) {
+ i2c_msg_buf_t * msg = &msgs[msg_index];
+
+ switch (event) {
+ case I2C_SLAVE_RECEIVE: {
+ if (msg->size == BUF_SIZE) return;
+ msg->data[msg->size++] = i2c_read_byte_raw(PBDRV_I2C);
+ break;
+ }
+ case I2C_SLAVE_FINISH: {
+ msg_complete(msg);
+ 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/main/pbdrv.h b/main/pbdrv.h
new file mode 100644
index 0000000..d0d70c2
--- /dev/null
+++ b/main/pbdrv.h
@@ -0,0 +1,41 @@
+#pragma once
+
+#include "pb-types.h"
+
+/**
+ * This is the RP2040 puzzle bus driver. This file is no longer inside
+ * lib/pbdrv/drv/rp2040 as it is tightly coupled to both the pico-sdk and
+ * freertos functions. I have tried to get FreeRTOS to play nicely with the
+ * CMake subproject layout, but the pico-sdk and the rp2040 port of freertos
+ * both rely on CMake's import() functionality, which makes using FreeRTOS in a
+ * libary context extremely messy.
+ *
+ * The workaround implemented in this driver was already kind of messy, and a
+ * different microcontroller should be used for the main controller instead.
+ */
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+//! puzzle bus driver setup
+void pbdrv_setup();
+
+/**
+ * While the RP2040's datasheet claims it supports multi-master configurations
+ * by implementing bus arbitration, it does not natively support a mode where
+ * it is configured as a (multi-)master with a slave address, such that it can
+ * be addressed by other multi-masters. This function includes a hacky
+ * workaround that teporarily sets the RP2040 to I2C master mode to send a
+ * message, and then restores it back to slave mode.
+ *
+ * This approach results in some received frames being (partially) dropped in
+ * the time period between the invocation of this function and the bus becoming
+ * idle (and the message is sent).
+ */
+void pbdrv_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz);
+
+#ifdef __cplusplus
+}
+#endif
+