aboutsummaryrefslogtreecommitdiff
path: root/main/i2c.c
diff options
context:
space:
mode:
Diffstat (limited to 'main/i2c.c')
-rw-r--r--main/i2c.c84
1 files changed, 31 insertions, 53 deletions
diff --git a/main/i2c.c b/main/i2c.c
index b0a0d11..2503560 100644
--- a/main/i2c.c
+++ b/main/i2c.c
@@ -3,69 +3,47 @@
#include <stdio.h>
#include <stddef.h>
#include <stdint.h>
-#include <string.h>
#include <pico/stdlib.h>
#include <hardware/i2c.h>
#include "i2c.h"
#include "pb-mod.h"
-
-// uint8_t* scan_bus(uint8_t *array) {
-// int ret;
-// int i = 0;
-// uint8_t rxdata;
-//
-// for(int addr = 0; addr < (1<<7); addr++) {
-// // ignore reserved addresses
-// // These are any addresses of the form 000 0xxx or 111 1xxx
-// // ret = i2c_read_blocking(I2C_PORT, addr, &rxdata, 1, false);
-//
-// // if acknowledged -> ret == number of bytes sent
-// if(ret > 0){
-// printf("found i2c slave on addr: %d\n", addr);
-// array[i] = addr;
-// i++;
-// }
-// }
-//
-// return array;
-// }
-
-void pbdrv_i2c_recv(const uint8_t * a, size_t b) {
- printf("%.*s", b, a);
+#include "pbdrv.h"
+#include "config.h"
+#include "pb-buf.h"
+#include "pb-send.h"
+
+i2c_addr_t modules[CFG_PB_MOD_MAX];
+size_t modules_size = 0;
+
+static void state_exchange() {
+ for (size_t i = 0; i < modules_size; i++) {
+ pb_buf_t buf = pb_send_state_req();
+
+ pb_buf_free(&buf);
+ }
}
void bus_task() {
- // scan bus for slaves
- // send updates at regular intervals
- vTaskDelay(1000 / portTICK_PERIOD_MS);
-
- // int i = 0;
- // uint8_t found[MAX_SLAVES];
- // init_addr_array(found, MAX_SLAVES);
-
- while (true) {
- vTaskDelay(10 / portTICK_PERIOD_MS);
- pbdrv_i2c_send(0x69, (uint8_t *) "bbbbbbbb", 9);
- }
+ // do a scan of the bus
+ bus_scan();
- // while(1) {
- // // printf("Bus scan!");
- // scan_bus(found);
+ // 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);
- // for(int i = 0; i < MAX_SLAVES; i++){
- // if( found[i] == 0x00 )
- // break;
- //
- // uint8_t data = 0x01;
- // // send data to found slave address
- // write_i2c(found[i], &data, 1);
+ while(1) {
+ // send my state to all puzzle modules
+ state_exchange();
+
+ // wait 1 second
+ pb_mod_blocking_delay_ms(1e3);
+ }
+}
- // data = 0x02;
- // write_i2c(found[i], &data, 1);
- // // request update from slave addr at found[i]
- // //write_i2c();
- // }
- // }
+void pb_route_cmd_magic_res(pb_msg_t * msg) {
+ if (modules_size == CFG_PB_MOD_MAX) return;
+ modules[modules_size++] = msg->sender;
+ printf("i2c: registered puzzle module w/ address 0x%02x\n", msg->sender);
}