diff options
author | ThomasintAnker <thomasintanker1@gmail.com> | 2024-06-18 16:23:51 +0200 |
---|---|---|
committer | ThomasintAnker <thomasintanker1@gmail.com> | 2024-06-18 16:23:51 +0200 |
commit | a55d0bed6240c54f6173b1e38e80212c02c302de (patch) | |
tree | 07c15eebc8cd84e1071a3f72d3c74475017372f3 /main/i2c.c | |
parent | b45b5d04daa29fcdd456233a931dcbb5b287769f (diff) | |
parent | 245fde65808ce902064ab438296f04f691d007e7 (diff) |
Merge branch 'master' into wip/handover
Diffstat (limited to 'main/i2c.c')
-rw-r--r-- | main/i2c.c | 179 |
1 files changed, 56 insertions, 123 deletions
@@ -1,8 +1,5 @@ -#include "i2c.h" -#include "init.h" -#include "sock.h" -#include "pb/types.h" - +#include <FreeRTOS.h> +#include <task.h> #include <stdio.h> #include <stddef.h> #include <stdint.h> @@ -10,129 +7,65 @@ #include <pico/stdlib.h> #include <hardware/i2c.h> -#include <lwip/opt.h> -#include <lwip/sys.h> -#include <lwip/api.h> -#include <string.h> - -uint8_t found[MAX_SLAVES]; -extern struct netconn* current_connection; - -void init_i2c() { - i2c_init(I2C_PORT, 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_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_PORT, 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_PORT, addr, input, len, true); -} - -bool reserved_addr(uint8_t addr) { - return (addr & 0x78) == 0 || (addr & 0x78) == 0x78; -} - -void init_addr_array(uint8_t array[], int size) { - for(int i = 0; i < size; i++){ - array[i] = 0x00; - } -} - -int write_read_i2c(uint8_t addr, uint8_t *input, size_t input_len, uint8_t *output, size_t output_len){ - // herhaalde start conditie voor direct lezen na i2c write (?) - int ret = write_i2c(addr, input, input_len); - if (ret < 0) { - printf("Write failure while writing data to bus.\n"); - return ret; - } - - // wait for response - absolute_time_t start_time = get_absolute_time(); - while ( absolute_time_diff_us(start_time, get_absolute_time()) / 1000 < MAX_TIMEOUT_TIME ){ - ret = read_i2c(addr, output, output_len); - if( ret > 0 ) { - return ret; - } - sleep_ms(1); - } - - printf("Timeout occurred while waiting for slave response.\n"); - return -1; -} - -// Make sure that current addresses are checked (modules), and invalid addresses are ignore (neotrellis slave) -uint8_t* scan_bus(uint8_t *array) { - int ret; - int i = 0; - uint8_t * rxdata; - uint8_t * handshake_data; - init_addr_array(array, MAX_SLAVES); - - for(int addr = 1; addr < (1<<7); addr++) { - // fix handshake - ret = read_i2c(addr, rxdata, 1); - - if ( ret <= 0 ) - continue; - - char buf[80]; - size_t s = snprintf(buf, 80,"found i2c puzzle module at address: 0x%02x\n", addr); - netconn_write(current_connection, buf, s, NETCONN_COPY); - printf("%.*s", s, buf); - - // do handshake - ret = write_read_i2c(addr, (uint8_t*)pb_magic_msg, sizeof(pb_magic_msg), handshake_data, sizeof(pb_magic_res)); // fix data + length + everything - - if ( ret != sizeof(pb_magic_res)) - continue; - - if ( ret > 0 && (memcmp(handshake_data, pb_magic_res, sizeof(pb_magic_res)) == 0)) { - printf("this was an actual device!!!1111!\n"); - char buf[80]; - size_t s = snprintf(buf, 80,"found i2c puzzle module at address: 0x%02x\n"); - netconn_write(current_connection, buf, s, NETCONN_COPY); - - array[i] = addr; - i++; - } - } - - return array; +#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); } void bus_task() { // scan bus for slaves // send updates at regular intervals - await_init(); + vTaskDelay(1000 / portTICK_PERIOD_MS); - scan_bus(found); - - while(1) { - // add check if bus is in use - - uint8_t data; - for(int i = 0; i < MAX_SLAVES; i++){ - if( found[i] == 0x00 ) - continue; - - read_i2c(found[i], &data, 2); - if(data > 0) { - printf("Data: %d", data); - } - - } - - sleep_ms(1000); // wait for one second before next loop + // 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); } + + // 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(); + // } + // } } + |