aboutsummaryrefslogtreecommitdiff
path: root/main
diff options
context:
space:
mode:
authorLoek Le Blansch <loek@pipeframe.xyz>2024-06-14 17:23:22 +0200
committerLoek Le Blansch <loek@pipeframe.xyz>2024-06-14 17:23:22 +0200
commit15ee8bd8885c8b3d0b4650fe609c253780f04bdf (patch)
treedf952b8f62dce49283a8f3937771ccc572fc13ad /main
parentef162ca3445d9adb000d7dfd1b68b181ef958926 (diff)
WIP more puzzle bus driver code
Diffstat (limited to 'main')
-rw-r--r--main/i2c.c57
-rw-r--r--main/init.c3
-rw-r--r--main/pbdrv.c19
-rw-r--r--main/pbdrv.h11
4 files changed, 36 insertions, 54 deletions
diff --git a/main/i2c.c b/main/i2c.c
index 77f4750..cb42ca4 100644
--- a/main/i2c.c
+++ b/main/i2c.c
@@ -8,63 +8,12 @@
#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 pb_i2c_recv(const uint8_t * a, size_t b) {
- printf("%.*s", b, a);
-}
+#include "pbdrv.h"
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);
- pb_i2c_send(0x69, (uint8_t *) "bbbbbbbb", 9);
- }
-
- // while(1) {
- // // printf("Bus scan!");
- // scan_bus(found);
-
- // 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);
-
- // data = 0x02;
- // write_i2c(found[i], &data, 1);
- // // request update from slave addr at found[i]
- // //write_i2c();
- // }
- // }
+ bus_scan();
+ vTaskDelete(NULL);
}
diff --git a/main/init.c b/main/init.c
index 2d6f33d..6d29d19 100644
--- a/main/init.c
+++ b/main/init.c
@@ -32,6 +32,9 @@ static void init_i2c() {
gpio_set_function(CFG_SDA_PIN, GPIO_FUNC_I2C);
gpio_set_function(CFG_SCL_PIN, GPIO_FUNC_I2C);
+ gpio_pull_up(CFG_SDA_PIN);
+ gpio_pull_up(CFG_SCL_PIN);
+
pb_setup();
}
diff --git a/main/pbdrv.c b/main/pbdrv.c
index 323afbe..d809b86 100644
--- a/main/pbdrv.c
+++ b/main/pbdrv.c
@@ -3,12 +3,15 @@
#include "pb.h"
#include "pb-types.h"
#include "pb-mod.h"
+#include "pb-msg.h"
#include <hardware/i2c.h>
#include <hardware/gpio.h>
#include <pico/i2c_slave.h>
+#include <pico/stdio.h>
#include <FreeRTOS.h>
+#include <stdio.h>
#include <timers.h>
#define PB_I2C i2c0
@@ -70,3 +73,19 @@ __weak void pb_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) {
i2c_set_slave_mode(PB_I2C, true, PB_MOD_ADDR);
}
+void bus_scan() {
+ i2c_set_slave_mode(PB_I2C, false, PB_MOD_ADDR);
+
+ pb_buf_t buf = pb_msg_write_req_magic();
+
+ // check for all 7-bit addresses
+ uint16_t addr_max = 1 << 7;
+ for (uint16_t addr = 0x00; addr < addr_max; addr++) {
+ i2c_write_timeout_us(PB_I2C, addr, (uint8_t *) buf.data, buf.size, false, PB_TIMEOUT_US);
+ }
+
+ pb_buf_free(&buf);
+
+ i2c_set_slave_mode(PB_I2C, true, PB_MOD_ADDR);
+}
+
diff --git a/main/pbdrv.h b/main/pbdrv.h
index 0b4ca21..a751000 100644
--- a/main/pbdrv.h
+++ b/main/pbdrv.h
@@ -35,6 +35,17 @@ void pb_setup();
*/
void pb_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz);
+/**
+ * \brief Scan the bus for I2C slaves, and send handshake messages to ACK-ing
+ * slaves.
+ *
+ * As a result of the RP2040 hardware limitations detailed at the top of this
+ * file, this function is also implemented in this file, even through it does
+ * not belong to the puzzle bus driver. In order to not miss any handshake
+ * responses, the bus should remain busy during the entire scan.
+ */
+void bus_scan();
+
#ifdef __cplusplus
}
#endif