final

Dependencies:   MAX30003 max32630fthr DS1307

Revision:
23:288c5d1babd1
Parent:
22:07eec382c267
Child:
24:98332ec40aff
--- a/main.cpp	Fri Jul 02 16:22:17 2021 +0000
+++ b/main.cpp	Tue Aug 03 19:39:42 2021 +0000
@@ -1,76 +1,84 @@
-
+ 
 #include "MAX30003.h"
 #include "mbed.h"
 #include "max32630fthr.h"
-//#include "ds1307.h"
+#include "ds1307.h"
 //#include <BufferedSerial.h>
-//#include <string>
+#include <string>
 //#include <Serial.h>
-#include "ds3231.h"
-
+ 
 #define TARGET_TX_PIN P3_1
 #define TARGET_RX_PIN P3_0
-
-#define SDA P3_4
-#define SCL P3_5
-
+ 
+ 
 //Timer timer_fast;
-//Timer t;
+Timer t;
 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
-
-//void task_fast(void);
+ 
+void task_fast(void);
 //DigitalOut ledA(LED2);
-
-//DigitalOut EN(P1_7);
-//DigitalOut A0(P1_4);
-//DigitalOut A1(P7_2);
-
-// mux controller pins
-DigitalOut S0(P5_6);
-DigitalOut S1(P5_5);
-DigitalOut S2(P3_3);
-DigitalOut S3(P3_2);
+ 
+DigitalOut S0(P4_0);
+DigitalOut S1(P5_6);
+//DigitalOut S2(P7_2);
+//DigitalOut S3(P7_2);
 
 //channel feedback
-DigitalIn A(P5_7); //P17
-DigitalIn B(P6_0); //P18
-DigitalIn C(P4_0); //P19
+DigitalIn A(P6_0); //P17
+DigitalIn B(P3_5); //P18
+DigitalIn C(P3_4); //P19
 
-
-
+ 
 void ecg_config(MAX30003 &ecgAFE);
 //BufferedSerial pc(P3_1,P3_0);            // Use USB debug probe for serial link static Unbuffered
 static BufferedSerial pc(TARGET_TX_PIN, TARGET_RX_PIN, 230400); // 230400 works well
-
+ 
 //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 bool timerflag = 0;
-
+volatile bool timerflag = 0;
+ 
 FileHandle *mbed::mbed_override_console(int fd)
 {
     return &pc;
 }
-
+ 
 void ecgFIFO_callback_1()  { // Triggered when the ECG FIFO is about to be full
     
     ecgFIFOIntFlag = 1;
 }  
-
-
+ 
+time_t asUnixTime(int year, int mon, int mday, int hour, int min, int sec) {
+    struct tm   t;
+    t.tm_year = year - 1900;
+    t.tm_mon =  mon - 1;        // convert to 0 based month
+    t.tm_mday = mday;
+    t.tm_hour = hour;
+    t.tm_min = min;
+    t.tm_sec = sec;
+    t.tm_isdst = -1;            // Is Daylight saving time on? 1 = yes, 0 = no, -1 = unknown
+ 
+    return mktime(&t);          // returns seconds elapsed since January 1, 1970 (begin of the Epoch)
+}
 int main(void)
 {   
- int t=0;
-
-
-      S0=0; 
-      S1=0;
-      S2=0;
-      S3=0;
-
-
+ uint16_t tic=0;
+ 
+ /* S1S0
+    00 - C0
+    01 - C1
+    10 - C2
+    11 - C3 */
+    
+ 
+     S0=0;    //C0
+     S1=0;
+  // S2=0;
+  // S3=0;
    
+ 
+ 
    // Constants
     const int EINT_STATUS_MASK =  1 << 23;
     const int FIFO_OVF_MASK =  0x7;
@@ -78,35 +86,7 @@
     const int FIFO_FAST_SAMPLE_MASK =  0x1;
     const int ETAG_BITS_MASK = 0x7;
     
-//-------------------------------------------------------------RTC-------------------------------------------------------------//     
-   //instantiate rtc object
-Ds3231 rtc(SDA, SCL); 
-
- uint16_t rtn_val;
- 
- //time = 12:00:00 AM 12hr mode
-ds3231_time_t time = {50,30,11,1,0};  //seconds, min, hours, am_pm, mode
-
- 
-rtn_val = rtc.set_time(time);
- 
-  
-  //see datasheet for calendar format
- ds3231_calendar_t calendar = {6,2,7,21}; //day date month year
-
-
- rtn_val = rtc.set_calendar(calendar);
- 
- 
-//see datasheet for calendar format
-
- ds3231_cntl_stat_t data = {0x1C, 0x08}; 
-
- rtn_val = rtc.set_cntl_stat_reg(data);
- //------------------------------------------------------------------------------------------------------------------------------//
-    
-    
-    //timer_fast.start();
+  //  timer_fast.start();
     DigitalOut rLed(LED2, LED_ON);
 //    pc.baud(9600);
     
@@ -117,6 +97,8 @@
     //    /* stop bit */ 1 //1
     //);
     //uart_1.baud(115200);  
+
+//--------------------------------- ECG Initialization ---------------------------------------------//
     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
     
@@ -130,17 +112,19 @@
     
     ecgAFE.writeRegister( MAX30003::SYNCH , 0);
     
+//-------------------------------------------------------------------------------------------------//
+    
     uint32_t ecgFIFO, readECGSamples, idx, ETAG[32], status;
     int16_t ecgSample[32];
     //bool timerflag = false;
-    int16_t ecgSample_1sec[125];
-    uint8_t ecg_1 = 0;
-    uint8_t ecg_2 = 0;
+    int16_t ecgSample_1sec[256];
+ //   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];
+  //  uint8_t final[10];
     
     uint16_t checksum_ = 0;
     uint16_t mod_checksum = 0;
@@ -157,51 +141,78 @@
     uint8_t cksm_1 = 0;
     uint8_t cksm_2 = 0;
     uint8_t header_device_id[3] = {0,0,210};
-   // uint8_t header_device_id[3] = {0,0,1};     //for rahul
+    //uint8_t header_device_id[3] = {0,0,1};     //for rahul
     uint8_t header_packet_type[2] = {0,2};
     
     uint8_t ending[5] = {'@','#','%','!','7'};
     
     bool flip = true;
-     uint8_t sampleps = 0;
+     int sampleps = 0;
      
-    char buf[20];//={"1622326341"};
+    char buf[20];
    // pc.write("Welcome",8*sizeof(char));
    // printf("In the main loop"); //printf("In the main loop");
-     bool flag_first = false;
-       bool timestamp_reader = false;
+    bool flag_first = false;
+    bool timestamp_reader = false;
        
        
-//---------------------------------hardware RTC check---------------------------------------------//
-       time_t epoch_time;  
-       
-           do {
-    rtc.get_time(&time);
-    rtc.get_calendar(&calendar); 
-    epoch_time = rtc.get_epoch();
-    //printf("seconds since the Epoch: %ld\n", epoch_time);
+//---------------------------------software RTC Start---------------------------------------------//
+  do
+    {
+       if (pc.readable()) 
+     {     
+     scanf("%s",buf);
+    //printf("The entered string is %s : \n ",buf);*/
+   // buf[20] = 1621297647;
+     packet_1 = atoi(buf);
+     set_time(packet_1);
      
-    packet_1 = epoch_time;
-      }while((epoch_time % 60)!=0);   // come out at the start of minute
-      
-      
+     if ((packet_1 % 60) == 0)
+         { 
+             for (int u = 0;u<3;u++)
+             {
+                 if (pc.readable()) 
+                {     
+                    scanf("%s",buf);
+                    printf("Entered the minute string  %s : \n ",buf);
+                 
+                    packet_1 = atoi(buf);
+                    set_time(packet_1);
+                }
+             }
+       timestamp_reader = true;
+    }
+       else
+        {
+             //printf("The timestamp is not divisible by 60 \n");
+     }
+     flag_first = true;
+    
+        }
+        
+     }   while (timestamp_reader==false);  // take the third timestamp 
+    
+//---------------------------------software RTC End---------------------------------------------//
+ 
   
 while(1) 
     {
-           
-    if ((onesec_counter>=125))
-    { t++;
-     //t.start();
-     //ledA = !ledA;   
+       
+    
+    if ((onesec_counter>=112))
+    {
+      tic++;
+        
       pc.write((uint8_t *)header_device_id, sizeof(header_device_id));     // device ID
       
       pc.write((uint8_t *)header_packet_type, sizeof(header_packet_type));   // packet type
       
-       epoch_time = rtc.get_epoch();
+      time_t seconds = time(NULL);
+      packet_1 = seconds;
+ 
        //time_t seconds = time(NULL);          // if remove it the timestamp will be static 
-      packet_1 = epoch_time;                     //
-      //packet_1 ++;
-     // printf("Time as seconds since January 1, 1970 = %d\n", packet_1);
+       //packet_1 ++;
+       // printf("Time as seconds since January 1, 1970 = %d\n", packet_1);
       p_1 = packet_1 & 0xff;
       p_2 = (packet_1 >> 8) & 0xff;
       p_3 = (packet_1 >> 16) & 0xff;
@@ -242,59 +253,71 @@
       // t.reset();
     //  timer_fast.reset();
     
-    if (t==10)       //C1    
+    
+       if (tic==10)       //C1    
      {
       S0=1; 
       S1=0;
-      S2=0;
-      S3=0;
+   //   S2=0;
+   //   S3=0;
      channel_num[0]= {1};
      } 
-     if (t==20)    //C2
+     if (tic==20)    //C2
      {
       S0=0; 
       S1=1;
-      S2=0;
-      S3=0;
+    //  S2=0;
+    //  S3=0;
       channel_num[0]= {2};
       }
          
-     if (t==30)          // C0  
+     if (tic==30)          // C0  
      {
       S0=0; 
       S1=0;
-      S2=0;
-      S3=0;
+    // S2=0;
+    // S3=0;
      channel_num[0]= {0};
-     t=0;} 
+     tic=0;} 
+     
      
-    if (A==0)  //5_7 = P17
-    {//t=50; 
+     // for channel selection
+     
+        if (A==0)  //
+    {tic=50; 
       S0=0; 
       S1=0;
-      S2=0;
-      S3=0;
+    //  S2=0;
+   //  S3=0;
      channel_num[0]= {0};}
      
-     if (B==0)  // 6_0 = P18
-     { 
+     if (B==0)  // 
+     {tic=60;
       S0=1; 
       S1=0;
-      S2=0;
-      S3=0;
+   //   S2=0;
+    //  S3=0;
       channel_num[0]= {1};}
       
-      if (C==0)  // 4_0 = P19
-     { 
+      if (C==0)  // 
+     {tic=70; 
       S0=0; 
       S1=1;
-      S2=0;
-      S3=0;
+   //   S2=0;
+    //  S3=0;
       channel_num[0]= {2};}
+     
+     
+    
+    
+    
+    
+    
+    
     
        } 
         // Read back ECG samples from the FIFO 
-        else if(ecgFIFOIntFlag==1)// && (timerflag == 0)) 
+        else if((ecgFIFOIntFlag==1))// && (timerflag == 0)) 
         {
             
             ecgFIFOIntFlag = 0; 
@@ -359,19 +382,18 @@
         }
     }
 }
-
-
-
-
+ 
+ 
+ 
+ 
 void ecg_config(MAX30003& ecgAFE) { 
-
+ 
     // Reset ECG to clear registers
     ecgAFE.writeRegister( MAX30003::SW_RST , 0);
     
     // General config register setting
     MAX30003::GeneralConfiguration_u CNFG_GEN_r;
     CNFG_GEN_r.bits.en_ecg = 1;     // Enable ECG channel
-    CNFG_GEN_r.bits.fmstr = 1;                                           // set frequency to 32000kHz
     CNFG_GEN_r.bits.rbiasn = 1;     // Enable resistive bias on negative input
     CNFG_GEN_r.bits.rbiasp = 1;     // Enable resistive bias on positive input
     CNFG_GEN_r.bits.en_rbias = 1;   // Enable resistive bias
@@ -386,7 +408,7 @@
     CNFG_ECG_r.bits.dlpf = 1;       // Digital LPF cutoff = 40Hz
     CNFG_ECG_r.bits.dhpf = 1;       // Digital HPF cutoff = 0.5Hz
     CNFG_ECG_r.bits.gain = 3;       // ECG gain = 160V/V
-    CNFG_ECG_r.bits.rate = 2;                                               // Sample rate = 125 sps
+    CNFG_ECG_r.bits.rate = 2;       // Sample rate = 128 sps
     ecgAFE.writeRegister( MAX30003::CNFG_ECG , CNFG_ECG_r.all);
       
     
@@ -425,4 +447,4 @@
        
     return;
 }  
-
+ 
\ No newline at end of file