aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--main/i2c.c62
-rw-r--r--main/i2c.h8
-rw-r--r--main/main.c2
3 files changed, 70 insertions, 2 deletions
diff --git a/main/i2c.c b/main/i2c.c
index 7ab1d14..3ecc2ee 100644
--- a/main/i2c.c
+++ b/main/i2c.c
@@ -8,7 +8,7 @@
void init_i2c() {
stdio_init_all();
- i2c_init(i2c_default, 100 * 1000);
+ i2c_init(i2c_default, 100 * 1000); // currently at 100kHz
// Initialize I2C pins - sda(16), scl(17)
gpio_set_function(SDA_PIN, GPIO_FUNC_I2C);
@@ -27,3 +27,63 @@ int write_i2c(uint8_t addr, uint8_t *input, size_t len) {
// true to keep master control of bus
return i2c_write_blocking (i2c_default, addr, input, len, true);
}
+
+bool reserved_addr(uint8_t addr) {
+ return (addr & 0x78) == 0 || (addr & 0x78) == 0x78;
+}
+
+void init_addr_array(uint8_t array[]) {
+ for(int i = 0; i < MAX_SLAVES; i++){
+ array[i] = 0x00;
+ }
+}
+
+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
+ if( reserved_addr(addr) ){
+ ret = PICO_ERROR_GENERIC;
+ }else{
+ ret = i2c_read_blocking(i2c_default, addr, &rxdata, 1, false);
+ }
+
+ // if acknowledged -> ret == number of bytes sent
+ if(ret > 0){
+ array[i] = addr;
+ i++;
+ }
+ }
+
+ return array;
+}
+
+void bus_task() {
+ // scan bus for slaves
+ // send updates at regular intervals
+
+ int i = 0;
+ uint8_t *found[MAX_SLAVES];
+ init_addr_array(&found);
+
+ while(1) {
+ scan_bus(&found);
+
+ for(int i = 0; i < MAX_SLAVES; i++){
+ if( found[i] == 0x00 )
+ break;
+
+ // send data to found slave address
+ write_i2c(found[i], 0x01, 1);
+
+ write_i2c(found[i], 0x00, 1);
+ // request update from slave addr at found[i]
+ //write_i2c();
+ }
+ }
+
+}
diff --git a/main/i2c.h b/main/i2c.h
index d12bca1..405ae1f 100644
--- a/main/i2c.h
+++ b/main/i2c.h
@@ -6,6 +6,7 @@
#define SDA_PIN 16
#define SCL_PIN 17
+#define MAX_SLAVES 10
/**
* \brief initialize all required gpio for i2c usage on the pico
@@ -28,9 +29,14 @@ int read_i2c(uint8_t addr, uint8_t *output, size_t len);
/**
* \brief write data to addr with length len from i2c bus.
- *
+ * \param addr
+ * \param input
+ * \param len
* This functions writes data to a specific address on the i2c bus,
* the input var holds the data which will be written to the given
* address with length len.
*/
int write_i2c(uint8_t addr, uint8_t *input, size_t len);
+
+/** \brief looking for slave addresses and requesting updates */
+void bus_task();
diff --git a/main/main.c b/main/main.c
index 73b6708..b38030f 100644
--- a/main/main.c
+++ b/main/main.c
@@ -7,6 +7,7 @@
#include "config.h"
#include "init.h"
#include "sock.h"
+#include "i2c.h"
void blink_task() {
await_init(); // `blink_task` uses GPIO
@@ -24,6 +25,7 @@ int main() {
xTaskCreate((TaskFunction_t) blink_task, "blink", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
xTaskCreate((TaskFunction_t) serve_task, "serve", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
+ xTaskCreate((TaskFunction_t) bus_task, "bus", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY + 2, NULL);
vTaskStartScheduler();
}