aboutsummaryrefslogtreecommitdiff
path: root/main/i2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/i2c.c')
-rw-r--r--main/i2c.c35
1 files changed, 29 insertions, 6 deletions
diff --git a/main/i2c.c b/main/i2c.c
index 87ff204..d37a9b7 100644
--- a/main/i2c.c
+++ b/main/i2c.c
@@ -8,15 +8,31 @@
#include "i2c.h"
#include "pb-mod.h"
-#include "pbdrv.h"
#include "config.h"
#include "pb-buf.h"
#include "pb-send.h"
static pb_global_state_t _global_state = PB_GS_IDLE;
pb_puzzle_module_t modules[CFG_PB_MOD_MAX];
+// stolen from lib/pico-sdk/src/rp2_common/hardware_i2c/i2c.c
+#define i2c_reserved_addr(addr) (((addr) & 0x78) == 0 || ((addr) & 0x78) == 0x78)
size_t modules_size = 0;
+static void bus_scan() {
+ pb_buf_t buf = pb_send_magic_req();
+
+ // check for all 7-bit addresses
+ uint16_t addr_max = 1 << 7;
+ for (uint16_t addr = 0x00; addr < addr_max; addr++) {
+ if (i2c_reserved_addr(addr)) continue;
+ if (addr == PB_MOD_ADDR) continue;
+
+ pb_i2c_send(addr, (uint8_t *) buf.data, buf.size);
+ }
+
+ pb_buf_free(&buf);
+}
+
static void state_exchange() {
@@ -74,19 +90,26 @@ void bus_task() {
// do a scan of the bus
bus_scan();
- // FIXME: this should be removed (see handover: RP2040 I2C limitations)
- // wait for 5 seconds until all handshake responses are received
- pb_mod_blocking_delay_ms(5e3);
-
while(1) {
// send my state to all puzzle modules
state_exchange();
// wait 1 second
- pb_mod_blocking_delay_ms(1e3);
+ vTaskDelay(1e3 / portTICK_PERIOD_MS);
}
}
+/**
+ * \ingroup main_pb_override
+ * \anchor main_route_cmd_magic_res
+ *
+ * This function registers the I2C address of the puzzle module that replied to
+ * the \c MAGIC \c REQ command into a list of "known puzzle modules", which are
+ * then periodically updated during gameplay.
+ *
+ * \note Up to \ref CFG_PB_MOD_MAX puzzle modules can be registered
+ * simultaniously.
+ */
void pb_route_cmd_magic_res(pb_msg_t * msg) {
if (modules_size == CFG_PB_MOD_MAX) return;
pb_puzzle_module_t tmp_module = {msg->sender, PB_GS_NOINIT};