diff options
author | ThomasintAnker <thomasintanker1@gmail.com> | 2024-05-31 02:19:28 +0200 |
---|---|---|
committer | ThomasintAnker <thomasintanker1@gmail.com> | 2024-05-31 02:19:28 +0200 |
commit | 377644354f15d283f37450bd7a473153d681316d (patch) | |
tree | 58013f13661df14c2cdad37ed83c63c64ae1fcc4 /main | |
parent | 519ffcefbe607dcb89dd9da654628dd3b0c588eb (diff) |
added queue for bbetween thread-communication
Diffstat (limited to 'main')
-rw-r--r-- | main/i2c.c | 15 | ||||
-rw-r--r-- | main/main.c | 14 | ||||
-rw-r--r-- | main/sock.c | 15 |
3 files changed, 43 insertions, 1 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 @@ -77,11 +82,12 @@ void bus_task() { // printf("Bus scan!"); scan_bus(found); + uint8_t data; for(int i = 0; i < MAX_SLAVES; i++){ if( found[i] == 0x00 ) break; - uint8_t data = 0x01; + data = 0x01; // send data to found slave address write_i2c(found[i], &data, 1); @@ -90,5 +96,12 @@ void bus_task() { // request update from slave addr at found[i] //write_i2c(); } + + uint8_t rcvData[2]; + if(xQueueReceive(queue, &rcvData, portMAX_DELAY) == pdPASS){ + write_i2c(rcvData[0], &rcvData[1], sizeof(rcvData[1])); + } else { + printf("Something went wrong at queue receive!\n"); + } } } diff --git a/main/main.c b/main/main.c index b38030f..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) 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 4f50981..5c8644b 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 "puzbusv1.h" #include "sock.h" +extern QueueHandle_t queue; + struct netconn* current_connection = NULL; struct pb_msg 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("Something went wrong!"); + } + } void recv_handler(struct netconn* conn, struct netbuf* buf) { |