diff options
-rw-r--r-- | main/i2c.c | 47 | ||||
-rw-r--r-- | main/i2c.h | 1 | ||||
-rw-r--r-- | main/init.c | 6 | ||||
-rw-r--r-- | main/main.c | 16 | ||||
-rw-r--r-- | main/sock.c | 17 |
5 files changed, 71 insertions, 16 deletions
@@ -7,6 +7,11 @@ #include <pico/stdlib.h> #include <hardware/i2c.h> +#include <FreeRTOS.h> +#include <queue.h> + +extern QueueHandle_t queue; + void init_i2c() { i2c_init(I2C_PORT, 100 * 1000); // currently at 100kHz @@ -38,19 +43,22 @@ void init_addr_array(uint8_t array[], int size) { } } +// 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; - for(int addr = 0; addr < (1<<7); addr++) { + for(int addr = 1; 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_PORT, addr, &rxdata, 1, false); - } + + // 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){ @@ -71,23 +79,36 @@ void bus_task() { int i = 0; uint8_t found[MAX_SLAVES]; init_addr_array(found, MAX_SLAVES); + scan_bus(found); while(1) { // printf("Bus scan!"); - scan_bus(found); + uint8_t data; for(int i = 0; i < MAX_SLAVES; i++){ if( found[i] == 0x00 ) - break; + continue; - uint8_t data = 0x01; - // send data to found slave address - write_i2c(found[i], &data, 1); + read_i2c(found[i], &data, 2); + if(data > 0) { + printf("Data: %d", data); + } + + // data = 0x01; + // // send data to found slave address + // write_i2c(found[i], &data, 1); + + // data = 0x02; + // write_i2c(found[i], &data, 1); - data = 0x02; - write_i2c(found[i], &data, 1); // request update from slave addr at found[i] //write_i2c(); } + + uint8_t rcvData[2]; + if(xQueueReceive(queue, &rcvData, 5) == pdPASS){ + printf("Send to address: %d, datat: %d\n", rcvData[0], rcvData[1]); + write_i2c(rcvData[0], &rcvData[1], sizeof(rcvData[1])); + } } } @@ -1,5 +1,6 @@ #pragma once // https://github.com/raspberrypi/pico-examples/tree/master/i2c +// https://www.raspberrypi.com/documentation/microcontrollers/raspberry-pi-pico.html #include <stddef.h> #include <stdint.h> diff --git a/main/init.c b/main/init.c index 08177c7..4ab373e 100644 --- a/main/init.c +++ b/main/init.c @@ -24,8 +24,10 @@ 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"); + /* WERKT GEWOON NIET MET DEZE LIJNEN CODE */ + 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"); + /* WERKT GEWOON NIET MET DEZE LIJNEN CODE */ printf("connected to Wi-Fi\n"); diff --git a/main/main.c b/main/main.c index 19dd3cd..33912ec 100644 --- a/main/main.c +++ b/main/main.c @@ -1,4 +1,5 @@ #include <FreeRTOS.h> +#include <queue.h> #include <task.h> #include <pico/stdlib.h> @@ -9,6 +10,8 @@ #include "sock.h" #include "i2c.h" +QueueHandle_t queue; + void blink_task() { await_init(); // `blink_task` uses GPIO @@ -23,10 +26,21 @@ 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) 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(); + + while(1) { + // we should have never gotten here + printf("Why are we here?!\n"); + } + + return 0; } diff --git a/main/sock.c b/main/sock.c index fe932bb..33da03c 100644 --- a/main/sock.c +++ b/main/sock.c @@ -5,11 +5,16 @@ #include <lwip/api.h> #include <string.h> +#include <FreeRTOS.h> +#include <queue.h> + #include "init.h" #include "config.h" #include "i2ctcpv1.h" #include "sock.h" +extern QueueHandle_t queue; + struct netconn* current_connection = NULL; i2ctcp_msg_t recv_msg; @@ -35,15 +40,25 @@ 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) { + /* printf("address: 0x%02x\n", addr); printf("data: \"%.*s\"\n", data_size, data); // send message back char reply[] = "Test message back!"; i2c_send(0x69, reply, strlen(reply)); + */ // 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("Sending data over i2c"); + uint8_t i2cData[2] = {addr, 0x00}; + if(xQueueSend(queue, &i2cData, portMAX_DELAY) == pdPASS) { + printf("Socket send data to address: %d, data: %d", i2cData[0], i2cData[1]); + } + } void recv_handler(struct netconn* conn, struct netbuf* buf) { @@ -55,8 +70,10 @@ void recv_handler(struct netconn* conn, struct netbuf* buf) { netbuf_data(buf, (void**)&data, &len); // continue early if more data is needed to complete message + printf("yeetus deletus defeatus"); if (!i2ctcp_read(&recv_msg, data, len)) continue; + printf("yeetus deletus defeatus v2!"); // forward received message to puzzle bus i2c_recv(recv_msg.addr, recv_msg.data, recv_msg.length); free(recv_msg.data); |