diff options
Diffstat (limited to 'main')
-rw-r--r-- | main/CMakeLists.txt | 1 | ||||
-rw-r--r-- | main/i2c.c | 75 | ||||
-rw-r--r-- | main/i2c.h | 6 | ||||
-rw-r--r-- | main/main.c | 4 | ||||
-rw-r--r-- | main/sock.c | 14 |
5 files changed, 71 insertions, 29 deletions
diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 6390d7c..cf23839 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -8,6 +8,7 @@ set(PICO_BOARD pico_w) include(lib/pico-sdk/pico_sdk_init.cmake) include(lib/FreeRTOS-Kernel/portable/ThirdParty/GCC/RP2040/FreeRTOS_Kernel_import.cmake) include(../i2ctcp/include.cmake) +include(../shared/include.cmake) project(puzzlebox_main C CXX ASM) @@ -1,12 +1,23 @@ #include "i2c.h" #include "init.h" +#include "sock.h" +#include "pb/types.h" #include <stdio.h> #include <stddef.h> #include <stdint.h> +#include <string.h> #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 @@ -38,26 +49,55 @@ void init_addr_array(uint8_t array[], int size) { } } +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 rxdata, handshake_data; + init_addr_array(array, MAX_SLAVES); + + for(int addr = 1; addr < (1<<7); addr++) { + // fix handshake + ret = read_i2c(addr, &rxdata, 1); - for(int addr = 1; addr < (1<<7); addr++) { - // ignore reserved addresses - // These are any addresses of the form 000 0xxx or 111 1xxx + if ( ret <= 0 ) + continue; + + printf("found possible i2c slave on addr: %d\n", addr); + + // do handshake + ret = write_read_i2c(addr, (uint8_t*)pb_magic_msg, sizeof(pb_magic_msg), (uint8_t*)handshake_data, sizeof(pb_magic_res)); // fix data + length + everything - // if( reserved_addr(addr) ){ - // ret = PICO_ERROR_GENERIC; - // }else{ - // - 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); + if ( ret != sizeof(pb_magic_res)) + continue; + + if ( ret > 0 && (memcmp(handshake_data, pb_magic_res, sizeof(pb_magic_res)) == 0)) { + char buf[80]; + size_t s = snprintf(buf, "found i2c puzzle module at address: 0x%02x\n"); + netconn_write(current_connection, buf, s, NETCONN_COPY); + array[i] = addr; i++; } @@ -71,13 +111,10 @@ void bus_task() { // send updates at regular intervals await_init(); - int i = 0; - uint8_t found[MAX_SLAVES]; - init_addr_array(found, MAX_SLAVES); scan_bus(found); while(1) { - // printf("Bus scan!"); + // add check if bus is in use uint8_t data; for(int i = 0; i < MAX_SLAVES; i++){ @@ -90,5 +127,7 @@ void bus_task() { } } + + sleep_ms(1000); // wait for one second before next loop } } @@ -10,6 +10,7 @@ #define SCL_PIN 17 #define I2C_PORT i2c0 #define MAX_SLAVES 10 +#define MAX_TIMEOUT_TIME 50 //ms /** * \brief initialize all required gpio for i2c usage on the pico @@ -41,5 +42,10 @@ int read_i2c(uint8_t addr, uint8_t *output, size_t len); */ int write_i2c(uint8_t addr, uint8_t *input, size_t len); +/** + * \brief +*/ +int write_read_i2c(uint8_t addr, uint8_t *input, size_t input_len, uint8_t *output, size_t output_len); + /** \brief looking for slave addresses and requesting updates */ void bus_task(); diff --git a/main/main.c b/main/main.c index 051f500..7558a0b 100644 --- a/main/main.c +++ b/main/main.c @@ -23,10 +23,6 @@ void blink_task() { int main() { init(); - // change queue size(?) + queue - uint8_t i2cData[2]; - queue = xQueueCreate(10, sizeof(i2cData)); - 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); diff --git a/main/sock.c b/main/sock.c index 0755a5d..08bec6b 100644 --- a/main/sock.c +++ b/main/sock.c @@ -11,8 +11,7 @@ #include "sock.h" #include <hardware/i2c.h> -extern QueueHandle_t queue; - +extern uint8_t found[MAX_SLAVES]; struct netconn* current_connection = NULL; i2ctcp_msg_t recv_msg; @@ -40,26 +39,27 @@ void i2c_send(uint16_t addr, const char * data, size_t data_size) { void i2c_recv(uint16_t addr, const char * data, size_t data_size) { // TODO: this function should forward the recieved message onto the puzzle // bus instead of printing/replying - // using queueu -> i2c_write only accepts uint8_t addr, and uint8_t data ._. printf("Addr: %lu, Data: %c, Data_size: %lu\n", addr, data[0], data_size); i2c_write_blocking(i2c0, addr, data, data_size, false); } void recv_handler(struct netconn* conn, struct netbuf* buf) { - i2ctcp_read_reset(&recv_msg); + // i2ctcp_read_reset(&recv_msg); do { char* data; uint16_t len; netbuf_data(buf, (void**)&data, &len); + + scan_bus(found); // continue early if more data is needed to complete message - if (i2ctcp_read(&recv_msg, data, len) > 0) continue; + // if (i2ctcp_read(&recv_msg, data, len) > 0) continue; // forward received message to puzzle bus - i2c_recv(recv_msg.addr, recv_msg.data, recv_msg.length); - free(recv_msg.data); + // i2c_recv(recv_msg.addr, recv_msg.data, recv_msg.length); + // free(recv_msg.data); } while (netbuf_next(buf) >= 0); netbuf_delete(buf); |