Update USB-UART communication, allow sending config and prepare for receiving new...
authorNathael Pajani <nathael.pajani@ed3l.fr>
Mon, 11 Mar 2024 15:36:56 +0000 (16:36 +0100)
committerNathael Pajani <nathael.pajani@ed3l.fr>
Mon, 11 Mar 2024 15:36:56 +0000 (16:36 +0100)
v10/comm.c
v10/comm.h
v10/main.c

index 6deb60d..8c2b7a8 100644 (file)
 
 
 /***************************************************************************** */
+/* 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:
        }
 }
 
index c1d35bb..6053cd3 100644 (file)
@@ -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);
 
index 4ab0d44..1fb55f4 100644 (file)
@@ -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;
 }