From 5e7481e9170c1334d74a57e84640e89a40cf74c3 Mon Sep 17 00:00:00 2001 From: lonkaars Date: Tue, 21 May 2024 13:34:26 +0200 Subject: fix brokey --- main/i2c.c | 116 +++++++++++++++++++++++++++++++----------------------------- main/init.c | 4 +-- 2 files changed, 61 insertions(+), 59 deletions(-) diff --git a/main/i2c.c b/main/i2c.c index 3ecc2ee..cbf6938 100644 --- a/main/i2c.c +++ b/main/i2c.c @@ -7,83 +7,85 @@ #include void init_i2c() { - stdio_init_all(); + stdio_init_all(); i2c_init(i2c_default, 100 * 1000); // currently at 100kHz // Initialize I2C pins - sda(16), scl(17) - gpio_set_function(SDA_PIN, GPIO_FUNC_I2C); - gpio_set_function(SCL_PIN, GPIO_FUNC_I2C); + gpio_set_function(SDA_PIN, GPIO_FUNC_I2C); + gpio_set_function(SCL_PIN, GPIO_FUNC_I2C); - gpio_pull_up(SDA_PIN); - gpio_pull_up(SCL_PIN); + gpio_pull_up(SDA_PIN); + gpio_pull_up(SCL_PIN); } int read_i2c(uint8_t addr, uint8_t *output, size_t len) { - // false - finished with bus - return i2c_read_blocking (i2c_default, addr, output, len, false); + // false - finished with bus + return i2c_read_blocking (i2c_default, addr, output, len, false); } 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); + // 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; + 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; - } + 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; +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(); - } - } + // 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; + + uint8_t data = 1; + // send data to found slave address + write_i2c(found[i], &data, 1); + + data = 0; + write_i2c(found[i], &data, 1); + // request update from slave addr at found[i] + //write_i2c(); + } + } } diff --git a/main/init.c b/main/init.c index f336ad0..08177c7 100644 --- a/main/init.c +++ b/main/init.c @@ -24,8 +24,8 @@ static void init_wifi() { // enable 'station' mode (connect to an access point instead of acting like one) cyw43_arch_enable_sta_mode(); - if (cyw43_arch_wifi_connect_timeout_ms(CONF_NET_SSID, CONF_NET_PASS, CONF_NET_AUTH, CONF_NET_CONN_TIMEOUT)) - panic("cyw43_arch_wifi_connect failed\n"); + // if (cyw43_arch_wifi_connect_timeout_ms(CONF_NET_SSID, CONF_NET_PASS, CONF_NET_AUTH, CONF_NET_CONN_TIMEOUT)) + // panic("cyw43_arch_wifi_connect failed\n"); printf("connected to Wi-Fi\n"); -- cgit v1.2.3