diff options
-rw-r--r-- | lib/pbdrv/CMakeLists.txt | 2 | ||||
-rw-r--r-- | lib/pbdrv/drv/rp2040/cfg.cmake | 7 | ||||
-rw-r--r-- | lib/pbdrv/drv/rp2040/mod.c | 73 | ||||
-rw-r--r-- | lib/pbdrv/drv/rp2040/mod.h | 13 | ||||
-rw-r--r-- | main/CMakeLists.txt | 4 | ||||
-rw-r--r-- | main/init.c | 2 | ||||
-rw-r--r-- | main/pbdrv.c | 72 | ||||
-rw-r--r-- | main/pbdrv.h | 41 |
8 files changed, 118 insertions, 96 deletions
diff --git a/lib/pbdrv/CMakeLists.txt b/lib/pbdrv/CMakeLists.txt index ca85b2b..fe464a9 100644 --- a/lib/pbdrv/CMakeLists.txt +++ b/lib/pbdrv/CMakeLists.txt @@ -24,5 +24,5 @@ target_include_directories(pbdrv-mod SYSTEM INTERFACE .) # supported puzzle bus drivers include(drv/arduino/cfg.cmake) -include(drv/rp2040/cfg.cmake) +# include(drv/rp2040/cfg.cmake) # moved to /main/pbdrv.c diff --git a/lib/pbdrv/drv/rp2040/cfg.cmake b/lib/pbdrv/drv/rp2040/cfg.cmake deleted file mode 100644 index 0fbad18..0000000 --- a/lib/pbdrv/drv/rp2040/cfg.cmake +++ /dev/null @@ -1,7 +0,0 @@ -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 deleted file mode 100644 index 6becdee..0000000 --- a/lib/pbdrv/drv/rp2040/mod.c +++ /dev/null @@ -1,73 +0,0 @@ -#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 - -static volatile bool pbdrv_i2c_msg_avail = false; -static uint8_t data[BUF_SIZE]; -static size_t size = 0; - -// TODO: create event group instead of pbdrv_i2c_msg_avail -// TODO: create freertos task that monitors the pbdrv_i2c_msg_avail flag and -// calls pbdrv_i2c_recv when a message is received - - -/** - * \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) { - // message needs to be handled first - if (pbdrv_i2c_msg_avail) return; - - 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); - pbdrv_i2c_msg_avail = true; - 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); -} - -/** - * 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). - */ -__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 deleted file mode 100644 index 0cf2e63..0000000 --- a/lib/pbdrv/drv/rp2040/mod.h +++ /dev/null @@ -1,13 +0,0 @@ -#pragma once - -#ifdef __cplusplus -extern "C" { -#endif - -//! puzzle bus driver setup -void pbdrv_setup(); - -#ifdef __cplusplus -} -#endif - 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 + |