From 77cb71d45b38755ba4928fa3ed18fca018fadc16 Mon Sep 17 00:00:00 2001 From: Nathael Pajani Date: Mon, 11 Mar 2024 16:36:56 +0100 Subject: [PATCH] Update USB-UART communication, allow sending config and prepare for receiving new config over USB-UART link --- v10/comm.c | 61 +++++++++++++++++++++++++++++++++++++++++++++++++----- v10/comm.h | 3 +++ v10/main.c | 8 ++++++- 3 files changed, 66 insertions(+), 6 deletions(-) diff --git a/v10/comm.c b/v10/comm.c index 6deb60d..8c2b7a8 100644 --- a/v10/comm.c +++ b/v10/comm.c @@ -39,14 +39,65 @@ /***************************************************************************** */ +/* USB Uart */ + +volatile uint8_t usb_logs = 0; +volatile uint8_t usb_send_config = 0; + +enum comm_states { + Idle = 0, /* Waiting for next command */ + Command, /* Received command, waiting for command param */ + Config, /* Received config command, receiving configuration */ +}; + /* Rx interrupt handler for system configuration over USB */ void config_rx(uint8_t c) { - /* FAN control */ - if (c == 'f') { - gpio_set(fan_ctrl); - } else { - gpio_clear(fan_ctrl); + static uint8_t comm_state = 0; + + if (comm_state == Command) { + /* Handle command value */ + comm_state = Idle; + return; + } + + if (comm_state == Config) { + /* Receive new config */ + /* Check received config checksum */ + /* If config received, burn to flash and reset */ + comm_state = Idle; + return; + } + + /* Idle case */ + switch (c) { + /* FAN control */ + case 'F': + gpio_set(fan_ctrl); + break; + case 'f': + gpio_clear(fan_ctrl); + break; + /* Activate USB Logs */ + case 'L': + usb_logs = 1; + break; + case 'l': + usb_logs = 0; + break; + /* Enter command mode */ + case 'a': + comm_state = Command; + break; + /* Enter config mode */ + case 'C': + comm_state = Config; + break; + /* Get config */ + case 'g': + usb_send_config = 1; + break; + default: } } diff --git a/v10/comm.h b/v10/comm.h index c1d35bb..6053cd3 100644 --- a/v10/comm.h +++ b/v10/comm.h @@ -25,6 +25,9 @@ #include "data.h" /***************************************************************************** */ +extern volatile uint8_t usb_logs; +extern volatile uint8_t usb_send_config; + /* Rx interrupt handler for system configuration over USB */ void config_rx(uint8_t c); diff --git a/v10/main.c b/v10/main.c index 4ab0d44..1fb55f4 100644 --- a/v10/main.c +++ b/v10/main.c @@ -650,7 +650,7 @@ int main(void) slave_send_data(); /* Data Log */ - if (1) { + if (usb_logs == 1) { external_disable |= linky_test; uprintf(UART0, "#%c:%d:%d:%d:%d:%d:%d:", mode, loop++, solar_prod_value, home_conso_value, @@ -665,6 +665,12 @@ int main(void) msg = NULL; } } + if (usb_send_config == 1) { + char* conf = (char*)&sc_conf; + serial_write(UART0, "C", 1); + serial_write(UART0, conf, sizeof(struct scialys_config)); + usb_send_config = 0; + } } return 0; } -- 2.43.0