Space / Mbed 2 deprecated DORA_lv

Dependencies:   mbed

Revision:
1:00cee5df0f76
Parent:
0:8f7c961a5966
Child:
2:155d07b145c6
--- a/main.cpp	Thu Jun 30 08:34:30 2022 +0000
+++ b/main.cpp	Fri Jul 15 14:00:59 2022 +0000
@@ -1,652 +1,583 @@
+
+// in data 13/07/2022 abbiamo testato spacecraft-mima avendo come spacecraft
+// la seriale che parla su STM, mentre mima l'usb del pc
+
 // Librerie
 #include "mbed.h"
 #include <math.h>
 #include <string.h>
 #include <stdlib.h>
-#include "tlc_tlm_list.h"
+#include <stdbool.h>
 
 // Macros
-#define cmd_num 32
-#define buf_max_size 512
-#define tlc_to_tx 16
-#define tlm_size 16
-#define tlm_hsk_size 912
-#define tlm_hsk_sd_size 90608
-#define identifier_field 5
-#define max31865_buf_size 13
-#define channel_size 5
-#define size 1
+#define PACKETDIM 15 // temperature
+#define word 1 
+#define packet 2 // 2 bytes
+#define hsk_buf 113 
+#define hsk_rx 114 // 114 bytes - 57 telemetry words(housekeeping)
+#define hsk_sd_buf 11211
+#define hsk_sd 11212 // 11212 bytes - 5606 interferogram data words
+#define channel_size 10 // temeprature channel from sen30202
 
 // Serial e periferiche 
-Serial spacecraft(USBTX, USBRX);
+Serial spacecraft(USBTX, USBRX); // switchare questo e le print con mima
 Serial mima(PC_4, PC_5);
-Serial max31865(PA_9, PA_10);
-
-// Buffer
-volatile int sizeBuffer;
-int CMD[cmd_num];
-char TLM[tlm_size];
-char TLM_HSK[tlm_hsk_size];
-char TLM_HSK_SD[tlm_hsk_sd_size];
-char tlc_rx[buf_max_size];
-char caRxPacket[max31865_buf_size];
-char tempCH1[channel_size];
-char tempCH2[channel_size];
-char CH1_to_string[2*channel_size];
-char CH2_to_string[2*channel_size];
-
-// L16 Actuonix - Relay (spare commands)
-PwmOut M1 (PA_6);
-PwmOut M2 (PA_8);
+Serial max31865(PA_0, PA_1);
+// L16 Actuonix: 
+PwmOut actuonix (PB_3);
+float offset;
+// Relay: configurazione pull-up
 DigitalOut Relay5V (PA_7);
 DigitalOut Relay12V (PB_4);
 DigitalOut Relay24V (PB_5);
 DigitalOut Relay15_15_5V (PB_10);
-float offset = 0.0f;
+
+// Buffer: telecommand
+volatile int rxTlcPtr; // telecommand receiving buffer
+int rxTlcDataCounter = 0; 
+char rx_TLC[word+1]; // buffer
+char data_TLC[word+1]; // telecommand
+
+// Buffer: standard telemetry
+volatile int rxTlmPtr; 
+int rxTlmDataCounter = 0;
+char rx_TLM[word+1]; // buffer
+char data_TLM[word+1]; // standard telemetry
+
+// Buffer: TLMODE telemetry option 1
+volatile int rxTlmOpt1Ptr; 
+int rxTlmOpt1DataCounter = 0;
+char rx_TLMOpt1[hsk_buf+1]; 
+char data_TLMOpt1[hsk_buf+1];
+
+// Buffer: TLMODE telemetry option 1
+volatile int rxTlmOpt2Ptr; 
+int rxTlmOpt2DataCounter = 0;
+char rx_TLMOpt2[hsk_sd_buf+1]; 
+char data_TLMOpt2[hsk_sd_buf+1];
 
 // Timer
-Timer timer;
-float TIMEOUT = 10, tTimeStart, tTimeStop, tTimeRead;
+Timer timer; // telecommand
+Timer rx; // rx window
+float rxWindow = 300 /* seconds */, rxTimeStart, rxTimeStop, rxTimeRead;
+float TIMEOUT = 10 /* seconds */, tTimeStart, tTimeStop, tTimeRead;
 
-// Vars definitions
-int temperature_flag;
-volatile int payload_to_dec;
-volatile int tlmode_option_1;
-volatile int tlmode_option_2;
-volatile int commandId;
-volatile int nRxCharCount;
-volatile int tlc_rx_length;
-volatile int checkZerosIndex, checkOnesIndex;
-volatile float CH1, CH2;
-volatile int increment_read;
-volatile int indexJ, indexT;
+// TLMODE
+volatile int tlmode_option_1, tlmode_option_2, tlmode_tlm;
+volatile bool TlcSend;
 
-// Functions
+// SC-MIMA communication: TX/RX
 void RxTelecommand();
 void TxTelecommand();
 void RxTelemetry();
 void TxTelemetry();
-void TxTemperature();
-void RxTemperature();
-void SpareCommand(int command);
-void printMyArray(Telecommand *myArray);
+// System utility
 void clearBuffer(char *arr, int arrLen);
-void checksum_tlc(Telecommand *toCheck_tlc);
-void checksum_tlm(Telemetry *toCheck_tlm);
-int BinToDec(int t, int dim, char *a);
+
+// Temperature settings
+char tempCH1[channel_size];
+char tempCH2[channel_size];
+volatile int j; // first channel index
+volatile int t; // second channel index
+volatile char caRxPacket[PACKETDIM]; // temperature buffer
+volatile char nRxCharCount; // temperature char counter
+
+// Routines dedicate alla ricezione di dati della temperatura (Pt100)
+void SerialeRxInterrupt(void)
+{
+    char cReadChar; 
+    while((max31865.readable()))
+    {
+        cReadChar = max31865.getc(); 
+        nRxCharCount++; 
+        caRxPacket[nRxCharCount] = cReadChar;
+    }  
+    
+    if (nRxCharCount == PACKETDIM) // if it reaches max -> reset
+    {
+        nRxCharCount = 0;
+    }
+}
 
 // Main
 int main()
 {
     // Baudrate
     spacecraft.baud(115200);
+    //spacecraft.baud(9600);
     mima.baud(9600);
+    //mima.baud(115200);
     max31865.baud(115200);
 
-    // Interrupts
-    spacecraft.attach(&RxTelecommand, Serial::RxIrq);
-    //max31865.attach(&RxTemperature, Serial::RxIrq);
+    // Diagnostica
+    spacecraft.printf(" Avvio Spacecraft... \n\r");
+    //mima.printf(" Avvio MIMA... \n\r");
     
-    while (true)
+    // Setup per temperatura
+    int nIndex;
+    nRxCharCount = 0;
+    for(nIndex = 0; nIndex < PACKETDIM; nIndex++)
     {
-        /**********************************************************************
-                        RICEZIONE DEI DATI DI TEMPERATURA
-        **********************************************************************/
-        
-        if ((commandId == 0) && (payload_to_dec == 32) && (temperature_flag == 1))
-        {
-            TxTemperature();
-            increment_read++;
-
-            if (increment_read == 40) 
-            {
-                temperature_flag = 0;
-            }
-        }
-        
-        /**********************************************************************
-                         RICEZIONE DI TELEMETRIE DA MIMA
-        **********************************************************************/
-
-        // Scrittura del telecomando
-        cmd00[(size-1)].PAYLOAD = "00000000"; 
-        if (cmd00[(size-1)].PAYLOAD != '\0')
-        {
-            checksum_tlc(&cmd00[(size-1)]);   
-            printMyArray(&cmd00[(size-1)]);
-        }                   
-        
-        // In arrivo da periferica mima
-        sizeBuffer = 0; // reset 
-        clearBuffer(TLM, sizeof(TLM)/sizeof(TLM[0]));
-        clearBuffer(TLM_HSK, sizeof(TLM_HSK)/sizeof(TLM_HSK[0]));
-        clearBuffer(TLM_HSK_SD, sizeof(TLM_HSK_SD)/sizeof(TLM_HSK_SD[0]));
-
+        caRxPacket[nIndex] = 0;
+    }   
+    
+    // Setup per Actuonix L16      
+    actuonix.period_us(1000);
+    offset = actuonix.read();
+    
+    // Setup per Relay: 1 = OFF (1->OFF, 0->ON)
+    Relay5V         = 1;
+    Relay12V        = 1;
+    Relay15_15_5V   = 1;
+    Relay24V        = 1;
+    
+    // Bool flags 
+    TlcSend = false;
+    
+    // Interrupt routine 
+    max31865.attach(&SerialeRxInterrupt, Serial::RxIrq);
+    spacecraft.attach(&RxTelecommand, Serial::RxIrq);
+    mima.attach(&RxTelemetry, Serial::RxIrq);
+    
+    // Main loop
+    while (true)
+    {    
+        // Telecommand
         timer.start();
-        tTimeStart = timer.read(); 
+        tTimeStart = timer.read();  // in secondi 
         tTimeRead = tTimeStart;
         
-        // Interrupt: entra nella routine quando riceve dalla seriale 
-        mima.attach(&RxTelemetry, Serial::RxIrq);
-
-        // Diagnostica del pacchetto ricevuto
-        if (commandId == 5)
+        while ((rxTlcPtr < packet) && ((tTimeRead - tTimeStart) < TIMEOUT))
         {
-            /* Ricezione di tutte le telemetrie. 57 parole da 16 bit contenenti
-            tutte le telemetrie di tipo HOUSEKEEPING. */
-            if (tlmode_option_1 == 0)
+            tTimeRead = timer.read();
+        }
+        
+        timer.stop();
+        
+        if ((rxTlcPtr == packet) && ((tTimeRead - tTimeStart) < TIMEOUT))
+        { 
+            __disable_irq(); // disable interrupts so data doesn't arrive while we are doing this
+            memcpy(data_TLC, rx_TLC, rxTlcPtr);  
+            rxTlcDataCounter = rxTlcPtr;
+            rxTlcPtr = 0;                                                 
+            //__enable_irq(); // re-enable interrupts
+            
+            data_TLC[1] = data_TLC[1] >> 1; // read payload data ignoring the LSB
+            
+            spacecraft.printf("\n\r .... Telecomando ricevuto su DORA! \n\r");
+                        
+            for (int i = 0; i < sizeof(data_TLC)/sizeof(char); i++)
             {
-                while ((sizeBuffer < tlm_hsk_size) && ((tTimeRead - tTimeStart) < TIMEOUT))
+                spacecraft.printf("> Carattere: %f \n\r", (float)data_TLC[i]);
+            }            
+            
+            data_TLC[rxTlcDataCounter] = 0; // add a null just in case the received data didn't have one at the end
+          
+            // hw control: set-point init
+            offset = 0;
+            
+            // RX settings
+            if (((float)data_TLC[0] != 40) || ((float)data_TLC[0] != 41))
+            {
+                tlmode_option_1 = 1;
+                tlmode_option_2 = 1;
+                tlmode_tlm = 0;
+            }
+            
+            // L16 setpoint
+            if ((float)data_TLC[0] == 0)
+            {
+                offset = (float)data_TLC[1];    
+                offset = offset/100;    
+                actuonix.write(offset);
+            }
+            // Temperature request
+            else if (((float)data_TLC[0] == 16) && ((float)data_TLC[1] == 32))
+            {
+                // Cleaning buffers
+                clearBuffer(tempCH1, sizeof(tempCH1)/sizeof(char));
+                clearBuffer(tempCH2, sizeof(tempCH2)/sizeof(char));
+
+                // Index setup
+                nRxCharCount = 0;
+                int y = 0, u = 0;
+                t = 0; j = 0;
+
+                for (int i = 0; i < PACKETDIM; i++)
                 {
-                    tTimeRead = timer.read();
-                }
+                    if (caRxPacket[i] == 'S')
+                    {
+                        j = i + 1;
+
+                        while (j < PACKETDIM)
+                        {
+                            if (caRxPacket[j] == ';')
+                            {
+                                break;
+                            }
+                            else
+                            {
+                                tempCH1[y++] = caRxPacket[j];
+                            }
+
+                            j++;
+                        }
+
+                        t = j + 1;
 
-                timer.stop();
-
-                if ((tTimeRead - tTimeStart) >= TIMEOUT)
+                        while (t < PACKETDIM)
+                        {
+                            if (caRxPacket[t] == 'P')
+                            {
+                                break;
+                            }
+                            else
+                            {
+                                tempCH2[u++] = caRxPacket[t];
+                            }
+                            t++;
+                        }
+                        break;
+                    }
+                }
+                // Send
+                spacecraft.puts(tempCH1);
+                spacecraft.puts(";");
+                spacecraft.puts(tempCH2);
+            }
+            else if (((float)data_TLC[0] == 121) && ((float)data_TLC[1] == 42))
+            {
+                Relay5V = 1;
+                Relay12V = 1;
+                Relay15_15_5V = 1;
+                Relay24V = 1;
+            }
+            else if (((float)data_TLC[0] == 120) && ((float)data_TLC[1] == 85))
+            {
+                Relay5V = 0;
+                Relay12V = 0;
+                Relay15_15_5V = 0;
+                Relay24V = 0; 
+            }
+            else if (((float)data_TLC[0] == 120)) // Relay 
+            {
+                if (((float)data_TLC[1] == 0))
                 {
-                    sizeBuffer = 0;
-                    clearBuffer(TLM_HSK, tlm_hsk_size);
+                    Relay12V = 1;
+                }
+                else if (((float)data_TLC[1] == 1))
+                {
+                    Relay12V = 0;
+                    // since the linear actuator is driven by 12V power
+                    offset = actuonix.read();
+                    actuonix.write(offset);
+                }
+                else if (((float)data_TLC[1] == 2))
+                {
+                    Relay5V = 1; 
                 }
-                else
+                else if (((float)data_TLC[1] == 3))
+                {
+                    Relay5V = 0; 
+                }
+                else if (((float)data_TLC[1] == 6))
+                {
+                    Relay15_15_5V = 1;
+                }
+                else if (((float)data_TLC[1] == 7))
                 {
-                    TxTelemetry();
+                    Relay15_15_5V = 0;
+                }
+                else if (((float)data_TLC[1] == 8))
+                {
+                    Relay24V = 1;
+                }
+                else if (((float)data_TLC[1] == 15))
+                {
+                    Relay24V = 0;
                 }
             }
-            /* Ricezione di tutte le telemetrie e dati scientifici
-            5663 parole da 16 bit così strutturate: 57 HSKP data packets,
-            5606 di scientific data dell'interferometro su MIMA, */
-            else if (tlmode_option_2 == 0)
+            else if (((float)data_TLC[0] == 40) && ((float)data_TLC[1] == 24))
+            {
+                tlmode_option_1 = 0;
+                tlmode_option_2 = 1;
+                tlmode_tlm = 1;
+                rxTlmPtr = 0;
+                rxTlmOpt1Ptr = 0;
+                rxTlmOpt2Ptr = 0;
+                spacecraft.printf("> Sono entrato in TLMODE 1 \n\r");
+            }
+            else if (((float)data_TLC[0] == 41) && ((float)data_TLC[1] == 127))
+            {
+                tlmode_option_2 = 0;
+                tlmode_option_1 = 1;
+                tlmode_tlm = 1;
+                rxTlmPtr = 0;
+                rxTlmOpt1Ptr = 0;
+                rxTlmOpt2Ptr = 0;
+                spacecraft.printf("> Sono entrato in TLMODE 2 \n\r");
+            }
+            // Reset global vars 
+            else if (((float)data_TLC[0] == 241) && ((float)data_TLC[1] == 127))
             {
-                while ((sizeBuffer < tlm_hsk_sd_size) && ((tTimeRead - tTimeStart) < TIMEOUT))
+                nRxCharCount = 0;
+                rxTlcPtr = 0;
+                rxTlmOpt1Ptr = 0;
+                rxTlmOpt2Ptr = 0;
+                rxTlcDataCounter = 0;
+                rxTlmOpt1DataCounter = 0;
+                rxTlmOpt2DataCounter = 0;
+                tlmode_tlm = 1;
+                tlmode_option_1 = 1;
+                tlmode_option_2 = 1;
+                
+                for(nIndex = 0; nIndex < PACKETDIM; nIndex++)
                 {
-                    tTimeRead = timer.read();
-                }
+                    caRxPacket[nIndex] = 0;
+                }   
+                
+                clearBuffer(data_TLC, sizeof(data_TLC)/sizeof(char));
+                clearBuffer(data_TLM, sizeof(data_TLM)/sizeof(char));
+                clearBuffer(data_TLMOpt1, sizeof(data_TLMOpt1)/sizeof(char));
+                clearBuffer(data_TLMOpt2, sizeof(data_TLMOpt2)/sizeof(char));
+                
+                spacecraft.printf(".... Reset variabili effettuato! \n\r");
+            }
+        
+            // Send after setting flags and clear
+            TxTelecommand();
+            spacecraft.printf(".... Telecomando inviato a MIMA ....");
+            __enable_irq(); // *********************************************************************
+            clearBuffer(rx_TLC, sizeof(rx_TLC)/sizeof(char));
+            clearBuffer(data_TLC, sizeof(data_TLC)/sizeof(char));
+            
+            spacecraft.printf(".... Ho ripulito i buffer di ricezione TLC .... \n\r");
+       
+            /* *********************************************************** */
+            
+            //__disable_irq(); // da lavorare su queste chiamate perché se da un lato prevengono 
+            // blocco del telecomando durante ricezione, dall'altro impediscono al comando di telemetria di ricevere
+            
+            if ((TlcSend) && (tlmode_option_1 == 1) && (tlmode_option_2 == 1) && (tlmode_tlm == 0))
+            { 
+                clearBuffer(data_TLM, sizeof(data_TLM)/sizeof(char));
 
-                timer.stop();
-
-                if ((tTimeRead - tTimeStart) >= TIMEOUT)
+                // Standard telemetry frame
+                rx.start();
+                rxTimeStart = rx.read(); 
+                rxTimeRead = rxTimeStart;
+                
+                while ((rxTlmPtr < packet) && ((rxTimeRead - rxTimeStart) < rxWindow))
                 {
-                    sizeBuffer = 0;
-                    clearBuffer(TLM_HSK_SD, tlm_hsk_sd_size);
+                    rxTimeRead = rx.read();
+                }
+                
+                rx.stop();
+                
+                if ((rxTlmPtr == packet) && ((rxTimeRead - rxTimeStart) < rxWindow))
+                { 
+                    __disable_irq(); 
+                    memcpy(data_TLM, rx_TLM, rxTlmPtr);  
+                    rxTlmDataCounter = rxTlmPtr;
+                    rxTlmPtr = 0;                                                 
+                    //__enable_irq();
+      
+                    data_TLM[rxTlmDataCounter] = 0;
+                    
+                    TxTelemetry();
+                    __enable_irq(); // ***************************************************
+                    spacecraft.printf("\n\r Ricevuto Telemetry Standard \n\r");
+
                 }
-                else
+                else if ((rxTimeRead - rxTimeStart) >= rxWindow)
                 {
-                    TxTelemetry();
+                    spacecraft.printf("\n\r !!! Scattato il timeout in Telemetry !!! \n\r");
+                    rxTlmPtr = 0;
+                    for (int i = 0; i < packet; i++)
+                    {
+                        rx_TLM[i] = '\0';
+                        data_TLM[i] = '\0';
+                    }
+                } // end-else-if:telecommand-send+timeout   
+                
+                TlcSend = false;
+            } // end-if-send-telecommand
+            else if ((TlcSend) && (tlmode_option_1 == 0) && (tlmode_option_2 == 1) && (tlmode_tlm == 1))
+            {   
+                spacecraft.printf("\n\r > Sono entrato in TLMODE 1 - TX Telemetry \n\r");
+                clearBuffer(data_TLMOpt1, sizeof(data_TLMOpt1)/sizeof(char));  
+                          
+                rx.start();
+                rxTimeStart = rx.read(); 
+                rxTimeRead = rxTimeStart;
+                                
+                while ((rxTlmOpt1Ptr < hsk_rx) && ((rxTimeRead - rxTimeStart) < rxWindow))
+                {
+                    rxTimeRead = rx.read();
                 }
-            }
-        }
-        /* Ricezione di un dato di telemetria da 16 bit. */
-        else    
-        {
-            while ((sizeBuffer < tlm_size) && ((tTimeRead - tTimeStart) < TIMEOUT))
-            {
-                tTimeRead = timer.read();
-            }
+                
+                rx.stop();
+                
+                if ((rxTlmOpt1Ptr == hsk_rx) && ((rxTimeRead - rxTimeStart) < rxWindow))
+                { 
+                    __disable_irq(); 
+                    memcpy(data_TLMOpt1, rx_TLMOpt1, rxTlmOpt1Ptr);  
+                    rxTlmOpt1DataCounter = rxTlmOpt1Ptr;
+                    rxTlmOpt1Ptr = 0;                                                 
+                    //__enable_irq();
+      
+                    data_TLMOpt1[rxTlmOpt1DataCounter] = 0;
+                    
+                    TxTelemetry();
+                    __enable_irq(); // ***************************************************
+                    spacecraft.printf("\n\r > Ricevuto Telemetry Opt 1 \n\r");
+
+                }
+                else if ((rxTimeRead - rxTimeStart) >= rxWindow)
+                {
+                    mima.printf("\n\r !!! Scattato il timeout in Telemetry Opt 1 !!! \n\r");
+                    rxTlmOpt1Ptr = 0;
+                    for (int i = 0; i < packet; i++)
+                    {
+                        rx_TLMOpt1[i] = '\0';
+                        data_TLMOpt1[i] = '\0';
+                    }
+                } 
+                TlcSend = false;
+            } // end-else-if-send-telecommand
+            else if ((TlcSend) && (tlmode_option_1 == 1) && (tlmode_option_2 == 0) && (tlmode_tlm == 1))
+            {   
+                spacecraft.printf("\n\r > Sono entrato in TLMODE 2 - TX Telemetry \n\r");
+                clearBuffer(data_TLMOpt2, sizeof(data_TLMOpt2)/sizeof(char)); 
+                           
+                rx.start();
+                rxTimeStart = rx.read(); 
+                rxTimeRead = rxTimeStart;
+                                  
+                while ((rxTlmOpt2Ptr < hsk_sd) && ((rxTimeRead - rxTimeStart) < rxWindow))
+                {
+                    rxTimeRead = rx.read();
+                }
+                
+                rx.stop();
+                
+                if ((rxTlmOpt2Ptr == hsk_sd) && ((rxTimeRead - rxTimeStart) < rxWindow))
+                { 
+                    __disable_irq(); 
+                    memcpy(data_TLMOpt2, rx_TLMOpt2, rxTlmOpt2Ptr);  
+                    rxTlmOpt2DataCounter = rxTlmOpt2Ptr;
+                    rxTlmOpt2Ptr = 0;                                                 
+                    //__enable_irq();
+      
+                    data_TLMOpt2[rxTlmOpt2DataCounter] = 0;
+                    
+                    TxTelemetry();
+                    __enable_irq(); // ***************************************************
+                    spacecraft.printf("\n\r Ricevuto Telemetry Opt 2 \n\r");
 
-            timer.stop();
-
-            if ((tTimeRead - tTimeStart) >= TIMEOUT)
+                }
+                else if ((rxTimeRead - rxTimeStart) >= rxWindow)
+                {
+                    spacecraft.printf("\n\r !!! Scattato il timeout in Telemetry Opt 2 !!! \n\r");
+                    rxTlmOpt2Ptr = 0;
+                    for (int i = 0; i < packet; i++)
+                    {
+                        rx_TLMOpt2[i] = '\0';
+                        data_TLMOpt2[i] = '\0';
+                    }
+                } 
+                TlcSend = false;
+            } // end-2nd-else-if-send-telecommand
+            
+            spacecraft.printf("\n\r .... Ho inviato il telecomando entro la finestra di timeout .... \n\r");
+            spacecraft.printf(" *************************************** \n\r");
+            
+            //__enable_irq();
+            
+        } // end-if:telecommand 
+        else if ((tTimeRead - tTimeStart) >= TIMEOUT)
+        {      
+            rxTlcPtr = 0;
+            for (int i = 0; i < packet; i++)
             {
-                sizeBuffer = 0;
-                clearBuffer(TLM, tlm_size);
+                rx_TLC[i] = '\0';
+                data_TLC[i] = '\0';
             }
-            else
-            {
-                TxTelemetry();
-            }
+            
+            spacecraft.printf(" .... Telecomando non inviato: timeout, riprovare! \n\r");
+        } // end-else-if:telecommand+timeout   
+    } // end-while:true
+} // end-Main
+
+
+/* ************************************************************* */
+/* ******** SYSTEM INTERRPUTS MANAGEMENT AND FUNCTIONS ********* */
+/* ************************************************************* */
+
+// Interrupt: DORA receives a byte from Spacecraft
+void RxTelecommand()
+{
+    char txChar;
+    while (spacecraft.readable()) 
+    { 
+        txChar = spacecraft.getc();
+        rx_TLC[rxTlcPtr++] = txChar;
+    }
+}
+
+// Interrupt: DORA receives a byte from MIMA
+void RxTelemetry()
+{
+    char rxChar;
+    while (mima.readable()) 
+    { 
+        rxChar = mima.getc();
+        
+        if (tlmode_tlm == 0) // Standard telemetry message
+        {
+            rx_TLM[rxTlmPtr++] = rxChar;
+        }
+        else if (tlmode_option_1 == 0) // TLMODE 1 (all housekeeping telemetries)
+        {
+            rx_TLMOpt1[rxTlmOpt1Ptr++] = rxChar;
+        }
+        else if (tlmode_option_2 == 0) // TLMODE 2 (hsk+scientific data)
+        {
+            rx_TLMOpt2[rxTlmOpt2Ptr++] = rxChar;
         }
     }
 }
 
-
-/* Gestione delle routine di ricezione e trasmissione del driver */
-
-// DORA riceve telecomando da Space/craft
-void RxTelecommand()
+// Once received a full telecommand frame, send it to MIMA
+void TxTelecommand()
 {
-    while (spacecraft.readable())
-    {
-        clearBuffer(tlc_rx, sizeof(tlc_rx) / sizeof(tlc_rx[0]));
-        spacecraft.gets(tlc_rx, sizeof(tlc_rx));
-        tlc_rx_length = strlen(tlc_rx) - 2; // rimozione tag di NR
-
-        if (tlc_rx_length == tlc_to_tx)
-        {
-            int bit_B0 = 0, bit_B7 = 7, k = 0; 
-            commandId = 0;  // init
-            payload_to_dec = 0; // init
-
-            commandId = BinToDec(bit_B0, identifier_field, tlc_rx);
-            payload_to_dec = BinToDec(bit_B7, (tlc_to_tx - 1), tlc_rx);
-
-            k = commandId;
-            
-            if (k == 2 || k == 15 || k == 30)
-            {
-                // Spare
-                break;
-            }
-
-            /* Gestiione della lettura della temperatura */
-            else if ((k == 0) && (tlc_rx[5] == '0') && (tlc_rx[6] == '0') && (payload_to_dec == 32))
-            {
-                temperature_flag = 1;
-                break;
-            }
-
-            /* Gestione delle perifieriche HW: relay, attuatori, alimentazione */
-            else if ((k == 0) && (tlc_rx[5] == '0') && (tlc_rx[6] == '0'))
-            {
-                //SpareCommand(payload_to_dec);
-                break;
-            }
-            else if (k == 5)
-            {
-            /* Istruzione che serve a scorrere il Payload e a settare un determinato flag
-            per consentire a DORA di aprire o meno determinati canali di comunicazione
-            (buffer da riempire). */
-            checkZerosIndex = bit_B7;
-
-            while (checkZerosIndex < tlc_to_tx)
-            {
-                if (tlc_rx[checkZerosIndex] == '0')
-                {
-                    tlmode_option_1 = 0;
-                    checkZerosIndex++;
-                }
-                else
-                {
-                    tlmode_option_1 = 1;
-                    break;
-                }
-            }
-
-            checkOnesIndex = bit_B7;
-            while (checkOnesIndex < tlc_to_tx)
-            {
-                if (tlc_rx[checkOnesIndex] == '1')
-                {
-                    tlmode_option_2 = 0;
-                    checkOnesIndex++;
-                }
-                else
-                {
-                    tlmode_option_2 = 1;
-                    break;
-                }
-            }
-            }
-        }
-        TxTelecommand();
-    }
+    mima.puts(data_TLC);
+    TlcSend = true;
 }
 
-// Trasmetti il pacchetto
-void TxTelecommand()
-{
-    for (int i = 0; i < tlc_to_tx + 1; i++)
-    {
-        mima.putc(tlc_rx[i]);
-    }
-}
-
-// Riceve dalla seriale mima
-void RxTelemetry()
-{
-    char data;
-    
-    while((mima.readable()))
-    {
-        data = mima.getc(); 
-        
-        if (tlmode_option_1 == 0)
-        {
-            TLM_HSK[sizeBuffer++] = data;
-        } 
-        else if (tlmode_option_2 == 0)
-        {
-            TLM_HSK_SD[sizeBuffer++] = data;
-        }
-        else 
-        {
-            TLM[sizeBuffer++] = data;
-        }
-            
-    }
-}
-
-// Dora trasmette il pacchetto allo space/craft
+// Once received a full telemetry frame, send it to Spacecraft
 void TxTelemetry()
 {
-    if (tlmode_option_1 == 0)
-    {        
-        for (int i = 0; i < tlm_hsk_size; i++)
-        {
-             spacecraft.putc(TLM_HSK[i]);
-        } 
+    if (tlmode_tlm == 0)
+    {
+        spacecraft.puts(data_TLM);
+    }
+    else if (tlmode_option_1 == 0)
+    {
+        spacecraft.puts(data_TLMOpt1);
     }
     else if (tlmode_option_2 == 0)
     {
-        for (int i = 0; i < tlm_hsk_sd_size; i++)
-        {
-            spacecraft.putc(TLM_HSK_SD[i]);
-        }
-    }
-    else 
-    {
-        for (int i = 0; i < tlm_size; i++)
-        {
-            spacecraft.putc(TLM[i]);
-        }
-    }
-    
-}
-
-/* Routines dedicate alla ricezione di dati della temperatura (Pt100) */
-
-void RxTemperature()
-{
-    char cReadChar; 
-    
-    while((max31865.readable()))
-    {
-        cReadChar = max31865.getc();
-        nRxCharCount++; 
-        caRxPacket[nRxCharCount] = cReadChar;
-    }
-} 
-
-void TxTemperature(void)
-{
-    nRxCharCount = 0; // reset dell'indice del buffer
-
-    int y = 0, u = 0; // indici dei buffer temporanei per i due canali
-
-    for (int i = 0; i < max31865_buf_size; i++)
-    {
-        if (caRxPacket[i] == 'S')
-        {
-            indexJ = i + 1;
-
-            while (indexJ < max31865_buf_size)
-            {
-                if (caRxPacket[indexJ] == ';')
-                {
-                    break;
-                }
-                else
-                {
-                    tempCH1[y++] = caRxPacket[indexJ];
-                }
-
-                indexJ++;
-            } // se trova ; esce dall'istruzione e salva il valore di uscita dell'indice
-
-            indexT = indexJ + 1;
-
-            while (indexT < max31865_buf_size)
-            {
-                if (caRxPacket[indexT] == 'P')
-                {
-                    break;
-                }
-                else
-                {
-                    tempCH2[u++] = caRxPacket[indexT];
-                }
-
-                indexT++;
-            }
-            
-            // trasforma il dato da sequenza di caratteri a numero float utile per analisi
-
-            strcpy(CH1_to_string, tempCH1);
-            CH1 = atof(CH1_to_string);
-
-            strcpy(CH2_to_string, tempCH2);
-            CH2 = atof(CH2_to_string);
-
-            spacecraft.printf("> TEMPERATURE - CH1: %4.8f[Celsius], CH2: %4.8f[Celsius]\n\r", CH1, CH2);
-
-            break;
-        }
-    }
-
-    wait_ms(150); // pausa per garantire sincronizzazione tra riempimento buffer e svuotamento
-}
-
-/* Comandi spare/not used */
-
-void SpareCommand(int command)                                                      // Spare command (Relay, L16)
-{        
-    if((command == 128) && (offset < 1.0f)) 
-    { 
-        offset += 0.2f; 
-        M1.write(offset);
-        M2.write(offset); 
-        spacecraft.printf("> Duty Cycle %.2f / estensione \n\r", offset);    
-    } 
-    else if((command == 64) && (offset > 0.0f)) 
-    {
-        offset -= 0.2f; 
-        M1.write(offset);
-        M2.write(offset); 
-        spacecraft.printf("> Duty Cycle %.2f / estensione \n\r", offset);  
-    }
-    else if (command == 1) 
-    {
-        Relay5V = 0;
-        spacecraft.printf("\r\nRelay 5V ON\r\n");       
-    } 
-    else if (command == 0) 
-    {
-        Relay5V = 1;
-        spacecraft.printf("\r\nRelay 5V OFF\r\n");
-    } 
-    else if (command == 3) 
-    {
-        Relay12V = 0;
-        spacecraft.printf("\r\nRelay 12V ON\r\n");           
-    }  
-    else if (command == 2) 
-    {
-        Relay12V = 1;
-        spacecraft.printf("\r\nRelay 12V OFF\r\n");
-    } 
-    else if (command == 7) 
-    {
-        Relay15_15_5V = 0;
-        spacecraft.printf("\r\nRelay +15V, -15V, -5V ON\r\n");     
-    } 
-    else if (command == 6) 
-    {
-        Relay15_15_5V = 1;
-        spacecraft.printf("\r\nRelay +15V, -15V, -5V OFF\r\n");
-    } 
-    else if (command == 15) 
-    {
-        Relay24V = 0;
-        spacecraft.printf("\r\nRelay 24V ON\r\n");        
-    } 
-    else if (command == 8) 
-    {
-        Relay24V = 1;
-        spacecraft.printf("\r\nRelay 24V OFF\r\n");   
-    } 
-    else if (command == 255) 
-    {
-        Relay5V = 1;
-        Relay12V = 1;
-        Relay15_15_5V = 1; // +15, -15, -5
-        Relay24V = 1;
-        offset = 0.0f;
-            
-        spacecraft.printf("**** SHUTTING DOWN ALL RELAYS... ****\n\r");
-        M1.write(offset);
-        M2.write(offset); 
-        spacecraft.printf("> Duty Cycle %.2f / extension \n\r", offset);
-    }
-    else if ((command == 128) || (command == 64) && (Relay12V == 1))
-    {
-        offset = 0.0f;
-        M1.write(offset);
-        M2.write(offset); 
-    }                   
-}
-
-/* Funzionalità di sistema */
-
-// Funzione che serve a ripulire un buffer di caratteri  
-void clearBuffer(char *arr, int arrLen)
-{
-    int nIndex;
-    
-    for (nIndex = 0; nIndex < arrLen; nIndex++)
-    {
-        arr[nIndex] = '\0';
+        spacecraft.puts(data_TLMOpt2);
     }
 }
 
-// Inizializzazione del set di identificativi dello Spacecraft
-void CMD_REF()
-{
-    int id = 0;
-    
-    for (int i = 0; i < cmd_num; i++)
-    {
-        CMD[id++] = i;
-    }
-}
-
-// Trasformazione da binario a decimale per una sequenza di bit in un array
-int BinToDec(int t, int dim, char *a)                                               
+// Reset
+void clearBuffer(char *arr, int arrLen)
 {
-    volatile int result = 0;
-
-    while (t < dim)
-    {
-        if (a[t] == '1')
-        {
-            int raise = dim - 1 - t;
-            result += pow((float) 2, (float) raise);
-        }
-        else
-        {
-            result += 0;
-        }
-        t++;
-    }
-    return result;
-}
-
-// Print
-void printMyArray(Telecommand *myArray)
-{
-    for (int i = 0; i < sizeof(myArray)/sizeof(myArray[0]); i++)
-    {
-        mima.puts(myArray[i].ID);
-        mima.puts(myArray[i].TAG);
-        mima.puts(myArray[i].PAYLOAD);
-        mima.putc(myArray[i].LSB);
-  
-        spacecraft.printf("\n\r");
-    }
-}
-
-
-// Checksum per telecomandi
-void checksum_tlc(Telecommand *toCheck_tlc)
-{
-    int ones_tlc = 0;
-    
-    for (int i = 0; i < sizeof(toCheck_tlc)/sizeof(toCheck_tlc[0]); i++)
+    int myIndex;
+    for (myIndex = 0; myIndex < arrLen; myIndex++)
     {
-        // contatore degli "1" nel campo "identifier"
-        for (int j = 0; i < sizeof(toCheck_tlc[i].ID)/sizeof(toCheck_tlc[i].ID[0]); j++)
-        {
-            if (toCheck_tlc[i].ID[j] == '1')
-            {
-                ones_tlc++;
-            }
-        }
-        
-        // contatore degli "1" nel campo "tag"
-        for (int k = 0; i < sizeof(toCheck_tlc[i].TAG)/sizeof(toCheck_tlc[i].TAG[0]); k++)
-        {
-            if (toCheck_tlc[i].TAG[k] == '1')
-            {
-                ones_tlc++;
-            }
-        }
-
-        // contatore degli "1" nel campo "payload" (da assegnare nel file main)
-        for (int l = 0; i < sizeof(toCheck_tlc[i].TAG)/sizeof(toCheck_tlc[i].TAG[0]); l++)
-        {
-            if (toCheck_tlc[i].PAYLOAD[l] == '1')
-            {
-                ones_tlc++;
-            }
-        }
-        
-        // Scrivi LSB
-        if (ones_tlc % 2 == 0)
-        {
-            toCheck_tlc[i].LSB = '0';  // If the number of ones in[B0÷B14] bits is even
-        }
-        else
-        {
-            toCheck_tlc[i].LSB = '1';  // If the number of ones in[B0÷B14] bits is odd
-        }
-        
-        
-    }
-}
-
-// Checksum per telemetrie housekeeping
-void checksum_tlm(Telemetry *toCheck_tlm)
-{
-    int ones_tlm = 0;
-    
-    for (int i = 0; i < sizeof(toCheck_tlm)/sizeof(toCheck_tlm[0]); i++)
-    {
-        // contatore degli "1" nel campo "identifier"
-        for (int j = 0; i < sizeof(toCheck_tlm[i].ID)/sizeof(toCheck_tlm[i].ID[0]); j++)
-        {
-            if (toCheck_tlm[i].ID[j] == '1')
-            {
-                ones_tlm++;
-            }
-        }
-        
-        // contatore degli "1" nel campo "tag"
-        for (int k = 0; i < sizeof(toCheck_tlm[i].TAG)/sizeof(toCheck_tlm[i].TAG[0]); k++)
-        {
-            if (toCheck_tlm[i].TAG[k] == '1')
-            {
-                ones_tlm++;
-            }
-        }
-
-        // contatore degli "1" nel campo "payload" (da assegnare nel file main)
-        for (int l = 0; i < sizeof(toCheck_tlm[i].TAG)/sizeof(toCheck_tlm[i].TAG[0]); l++)
-        {
-            if (toCheck_tlm[i].PAYLOAD[l] == '1')
-            {
-                ones_tlm++;
-            }
-        }
-        
-        // Scrivi LSB
-        if (ones_tlm % 2 == 0)
-        {
-            toCheck_tlm[i].LSB = '0';  // If the number of ones in[B0÷B14] bits is even
-        }
-        else
-        {
-            toCheck_tlm[i].LSB = '1';  // If the number of ones in[B0÷B14] bits is odd
-        }
-        
-        
+        arr[myIndex] = '\0'; // terminatore
     }
 }
\ No newline at end of file