RTC_MUX_3Channel_Working

Dependencies:   MAX30003 max32630fthr DS1307

Revision:
10:54aa50490b15
Parent:
9:24ecf16eab0f
Child:
11:300afaf2696b
--- a/main.cpp	Wed Aug 19 19:38:17 2020 +0000
+++ b/main.cpp	Mon Sep 21 23:13:36 2020 +0000
@@ -3,26 +3,29 @@
 #include "mbed.h"
 #include "max32630fthr.h"
 #include "ds1307.h"
-
+#include <BufferedSerial.h>
+#include <string>
+//#include <Serial.h>
 Timer timer_fast;
+Timer t;
 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
 
 void task_fast(void);
 DigitalOut ledA(LED2);
 void ecg_config(MAX30003 &ecgAFE);
-
+BufferedSerial pc(P3_1,P3_0);            // Use USB debug probe for serial link static Unbuffered
+//Serial uart_1(USBTX, USBRX);            // Use USB debug probe for serial link static Unbuffered
+//
+//Serial pc(P3_1,P3_0);
 volatile bool ecgFIFOIntFlag = 0; 
-volatile int16_t onesec_counter = 0;
 volatile bool timerflag = 0;
 void ecgFIFO_callback_1()  { // Triggered when the ECG FIFO is about to be full
     
     ecgFIFOIntFlag = 1;
-
 }  
 
 int main()
 {   
-
     // Constants
     const int EINT_STATUS_MASK =  1 << 23;
     const int FIFO_OVF_MASK =  0x7;
@@ -31,16 +34,21 @@
     const int ETAG_BITS_MASK = 0x7;
     timer_fast.start();
     DigitalOut rLed(LED1, LED_OFF);
+//    pc.baud(9600);
     
-    Serial pc(USBTX, USBRX);            // Use USB debug probe for serial link
-    pc.baud(115200);                    // Baud rate = 115200
-    
+    pc.set_baud(115200);                    // Baud rate = 115200
+    pc.set_format(
+        /* bits */ 8,   
+        /* parity */ BufferedSerial::None,
+        /* stop bit */ 1 //1
+    );
+    //uart_1.baud(115200);  
     InterruptIn ecgFIFO_int(P5_4);          // Config P5_4 as int. in for the
     ecgFIFO_int.fall(&ecgFIFO_callback_1);    // ecg FIFO interrupt at falling edge
     
     SPI spiBus(SPI2_MOSI, SPI2_MISO, SPI2_SCK);     // SPI bus, P5_1 = MOSI, 
                                                     // P5_2 = MISO, P5_0 = SCK
-    
+      
     MAX30003 ecgAFE(spiBus, P5_3);          // New MAX30003 on spiBus, CS = P5_3 
     
     ecg_config(ecgAFE);                    // Config ECG 
@@ -51,29 +59,80 @@
     uint32_t ecgFIFO, readECGSamples, idx, ETAG[32], status;
     int16_t ecgSample[32];
     //bool timerflag = false;
-    int16_t ecgSample_1sec[700];
-   int16_t onesec_counter = 0;
+    int16_t ecgSample_1sec[200];
+    uint8_t ecg_1 = 0;
+    uint8_t ecg_2 = 0;
+   uint16_t onesec_counter = 0;
+   uint16_t onesec_counter_temp = 0;
+   
     int16_t sample = 300;
+    uint8_t final[10];
+    uint32_t packet_1 = 1600674360;
+    uint16_t checksum_ = 0;
+    uint16_t mod_checksum = 0;
+    
+    uint8_t p_1 = 0;
+    uint8_t p_2 = 0;
+    uint8_t p_3 = 0;
+    uint8_t p_4 = 0;
+    
+    uint8_t data_len_1 = 0;
+    uint8_t data_len_2 = 0;
+    
+    uint8_t cksm_1 = 0;
+    uint8_t cksm_2 = 0;
+    uint8_t header_device_id[3] = {0,0,210};
+    uint8_t header_packet_type[2] = {0,2};
+    
+    uint8_t ending[5] = {'@','#','%','!','7'};
+    
+    char buf[5];
     
     while(1) 
     {
+       
     
-    if (timer_fast.read() > 1) 
+    if (timer_fast.read() > .9) 
     {
-      //timerflag = 1;
-     ledA = !ledA;
+     t.start();
+     //ledA = !ledA;   
+
+      
+      pc.write((uint8_t *)header_device_id, sizeof(header_device_id));
+      
+      pc.write((uint8_t *)header_packet_type, sizeof(header_packet_type));
+      packet_1 ++;
+      p_1 = packet_1 & 0xff;
+      p_2 = (packet_1 >> 8) & 0xff;
+      p_3 = (packet_1 >> 16) & 0xff;
+      p_4 = (packet_1 >> 24) & 0xff;
+     // checksum_  = checksum_ + (packet_1 & 0xffff) + ((packet_1 >> 16) & 0xffff) ;
+     checksum_  = checksum_ + p_1 + p_2 + p_3 + p_4 ;
+     uint8_t header_packet_id[4] = {p_4,p_3,p_2,p_1};
+      pc.write((uint8_t *)header_packet_id, sizeof(header_packet_id));
+      onesec_counter_temp = onesec_counter * 2;
+      data_len_1 = onesec_counter_temp & 0xff;
+      data_len_2 = (onesec_counter_temp >> 8) & 0xff;
+      checksum_  = checksum_ + data_len_1 + data_len_2;
+      //pc.write((uint32_t *)packet_1, sizeof(packet_1));
+      uint8_t header_ecg_datalen[2] = {data_len_2,data_len_1};
+      pc.write((uint8_t *)header_ecg_datalen, sizeof(header_ecg_datalen));
+      mod_checksum = checksum_ % 65536 ;
+      cksm_1 = mod_checksum & 0xff;
+      cksm_2 = (mod_checksum >> 8) & 0xff;
+      uint8_t header_ecg_checksum[2] = {cksm_2,cksm_1};
+      pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum));
+      
+      pc.write((int16_t *)ecgSample_1sec,onesec_counter * sizeof(int16_t));
+      onesec_counter = 0;
+       // memset(ecgSample_1sec, 0, sizeof(ecgSample_1sec));
+              
+        pc.write((uint8_t *)ending, sizeof(ending));
+        checksum_ = 0;
+        t.stop();
+        auto us = t.elapsed_time().count();
+        printf("Timer time: %llu ms \n", us);
       timer_fast.reset();
-      int8_t header_device_id[3] = {0,0,210};
-      int8_t header_packet_type[2] = {0,2};
-      int8_t header_packet_id[4] = {0,0,0,0};
-      int8_t header_ecg_datalen[2] = {0,onesec_counter};
-      int8_t header_ecg_checksum[2] = {0,0};
-      
-      
-      //pc.printf("%6d\r\n", sample);
-      //pc.printf("%6d\r\n", onesec_counter);
-      
-      onesec_counter = 0;
        } 
         // Read back ECG samples from the FIFO 
         else if( (ecgFIFOIntFlag==1))// && (timerflag == 0)) 
@@ -91,6 +150,7 @@
                 do {
                     ecgFIFO = ecgAFE.readRegister( MAX30003::ECG_FIFO );       // Read FIFO
                     ecgSample[readECGSamples] = ecgFIFO >> 8;                  // Isolate voltage data
+                    ecgSample[readECGSamples] = ((ecgSample[readECGSamples]<<8)&0xFF00)|((ecgSample[readECGSamples]>>8)&0x00FF);
                     ETAG[readECGSamples] = ( ecgFIFO >> 3 ) & ETAG_BITS_MASK;  // Isolate ETAG
                     readECGSamples++;                                          // Increment sample counter
                 
@@ -104,31 +164,27 @@
                     ecgAFE.writeRegister( MAX30003::FIFO_RST , 0); // Reset FIFO
                     rLed = 1;//notifies the user that an over flow occured
                 }
+                //uint8_t header_ecg_checksum[2] = {'%','%'};
+                //pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum));
                 
-                // Print results 
-                for( idx = 0; idx < readECGSamples; idx++ ) 
+                //pc.write((int16_t *)ecgSample,readECGSamples * sizeof(int16_t ));
+                //memcpy(ecgSample_1sec , ecgSample, sizeof(ecgSample));
+                
+                //memcpy(ecgSample_1sec + (onesec_counter * sizeof(int16_t)), ecgSample, sizeof(ecgSample));
+                 for( idx = 0; idx < readECGSamples; idx++ ) 
                 {
                     //pc.printf("%6d\r\n", ecgSample[idx]);     
                     ecgSample_1sec[onesec_counter] = ecgSample[idx];
+                    
+                    cksm_1 = ecgSample[idx] & 0xff;
+                    cksm_2 = (ecgSample[idx] >> 8) & 0xff;
+                    checksum_ += cksm_1 + cksm_2;
                     onesec_counter++;      
                 } 
+                //onesec_counter += readECGSamples;
                                
             }
-        //    else
-          //  {
-            //    timerflag = 0;
-              //  }
-            //else if (timerflag == 1)
-            //{
-                //timerflag = 0;
-                //pc.printf("1 sec ends :\n");
-                //onesec_counter = 0;
-                //timer_fast.reset();
-                //timer_fast.start();
-                //delete[] ecgSample_1sec;
-                //}
-              
-            
+           
         }
     }
 }