final

Dependencies:   MAX30003 max32630fthr DS1307

Committer:
parthsagar2010
Date:
Sat Jan 22 01:42:50 2022 +0000
Revision:
27:3b45b47d735d
Parent:
26:c41e561a02de
k

Who changed what in which revision?

UserRevisionLine numberNew contents of line
kidecha_rahul 23:288c5d1babd1 1
parthsagar2010 8:6b9359f81cc0 2 #include "MAX30003.h"
coreyharris 0:38c49bc37c7c 3 #include "mbed.h"
coreyharris 0:38c49bc37c7c 4 #include "max32630fthr.h"
kidecha_rahul 23:288c5d1babd1 5 #include "ds1307.h"
parthsagar2010 11:300afaf2696b 6 //#include <BufferedSerial.h>
kidecha_rahul 23:288c5d1babd1 7 #include <string>
parthsagar2010 10:54aa50490b15 8 //#include <Serial.h>
kidecha_rahul 23:288c5d1babd1 9
kidecha_rahul 18:101042d225ef 10 #define TARGET_TX_PIN P3_1
kidecha_rahul 18:101042d225ef 11 #define TARGET_RX_PIN P3_0
kidecha_rahul 23:288c5d1babd1 12
kidecha_rahul 25:06ba30a29754 13 //#define SDA P3_4
kidecha_rahul 25:06ba30a29754 14 //#define SCL P3_5
kidecha_rahul 24:98332ec40aff 15
kidecha_rahul 24:98332ec40aff 16
kidecha_rahul 25:06ba30a29754 17 //#define I2C_SDA P5_7
kidecha_rahul 25:06ba30a29754 18 //#define I2C_SCL P6_0
kidecha_rahul 24:98332ec40aff 19
kidecha_rahul 25:06ba30a29754 20 //Ds3231 rtc(SDA, SCL);
kidecha_rahul 24:98332ec40aff 21
kidecha_rahul 22:07eec382c267 22 //Timer timer_fast;
kidecha_rahul 23:288c5d1babd1 23 Timer t;
coreyharris 0:38c49bc37c7c 24 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
kidecha_rahul 23:288c5d1babd1 25
kidecha_rahul 23:288c5d1babd1 26 void task_fast(void);
kidecha_rahul 18:101042d225ef 27 //DigitalOut ledA(LED2);
kidecha_rahul 23:288c5d1babd1 28
kidecha_rahul 25:06ba30a29754 29 DigitalOut S0(P0_4);
kidecha_rahul 25:06ba30a29754 30 DigitalOut S1(P0_5);
kidecha_rahul 23:288c5d1babd1 31 //DigitalOut S2(P7_2);
kidecha_rahul 23:288c5d1babd1 32 //DigitalOut S3(P7_2);
kidecha_rahul 19:7a084dc2f09d 33
kidecha_rahul 25:06ba30a29754 34 // Input pins
kidecha_rahul 25:06ba30a29754 35 DigitalIn A(P1_3); //P6_3
kidecha_rahul 25:06ba30a29754 36 DigitalIn B(P1_5); //P1_5
kidecha_rahul 25:06ba30a29754 37 DigitalIn C(P1_4); //P1_4
kidecha_rahul 25:06ba30a29754 38 //DigitalIn D(6_3); //P6_3 for hardware RTC
kidecha_rahul 25:06ba30a29754 39
kidecha_rahul 25:06ba30a29754 40 const float battery_max = 4.20; //maximum voltage of battery
kidecha_rahul 25:06ba30a29754 41 const float battery_min = 3.0; //minimum voltage of battery before shutdown
kidecha_rahul 25:06ba30a29754 42 uint8_t output = 0;
kidecha_rahul 25:06ba30a29754 43
kidecha_rahul 25:06ba30a29754 44 //AnalogIn ain_4(AIN_4);
kidecha_rahul 25:06ba30a29754 45 AnalogIn ain_5(AIN_5);
kidecha_rahul 21:193a10530741 46
kidecha_rahul 23:288c5d1babd1 47
coreyharris 0:38c49bc37c7c 48 void ecg_config(MAX30003 &ecgAFE);
parthsagar2010 11:300afaf2696b 49 //BufferedSerial pc(P3_1,P3_0); // Use USB debug probe for serial link static Unbuffered
parthsagar2010 15:0356e9454a60 50 static BufferedSerial pc(TARGET_TX_PIN, TARGET_RX_PIN, 230400); // 230400 works well
kidecha_rahul 23:288c5d1babd1 51
parthsagar2010 10:54aa50490b15 52 //Serial uart_1(USBTX, USBRX); // Use USB debug probe for serial link static Unbuffered
parthsagar2010 10:54aa50490b15 53 //
parthsagar2010 10:54aa50490b15 54 //Serial pc(P3_1,P3_0);
coreyharris 0:38c49bc37c7c 55 volatile bool ecgFIFOIntFlag = 0;
kidecha_rahul 23:288c5d1babd1 56 volatile bool timerflag = 0;
kidecha_rahul 23:288c5d1babd1 57
parthsagar2010 11:300afaf2696b 58 FileHandle *mbed::mbed_override_console(int fd)
parthsagar2010 11:300afaf2696b 59 {
parthsagar2010 11:300afaf2696b 60 return &pc;
parthsagar2010 11:300afaf2696b 61 }
kidecha_rahul 23:288c5d1babd1 62
parthsagar2010 8:6b9359f81cc0 63 void ecgFIFO_callback_1() { // Triggered when the ECG FIFO is about to be full
coreyharris 0:38c49bc37c7c 64
coreyharris 0:38c49bc37c7c 65 ecgFIFOIntFlag = 1;
coreyharris 0:38c49bc37c7c 66 }
kidecha_rahul 23:288c5d1babd1 67
kidecha_rahul 23:288c5d1babd1 68 time_t asUnixTime(int year, int mon, int mday, int hour, int min, int sec) {
kidecha_rahul 23:288c5d1babd1 69 struct tm t;
kidecha_rahul 23:288c5d1babd1 70 t.tm_year = year - 1900;
kidecha_rahul 23:288c5d1babd1 71 t.tm_mon = mon - 1; // convert to 0 based month
kidecha_rahul 23:288c5d1babd1 72 t.tm_mday = mday;
kidecha_rahul 23:288c5d1babd1 73 t.tm_hour = hour;
kidecha_rahul 23:288c5d1babd1 74 t.tm_min = min;
kidecha_rahul 23:288c5d1babd1 75 t.tm_sec = sec;
kidecha_rahul 23:288c5d1babd1 76 t.tm_isdst = -1; // Is Daylight saving time on? 1 = yes, 0 = no, -1 = unknown
kidecha_rahul 23:288c5d1babd1 77
kidecha_rahul 23:288c5d1babd1 78 return mktime(&t); // returns seconds elapsed since January 1, 1970 (begin of the Epoch)
kidecha_rahul 23:288c5d1babd1 79 }
parthsagar2010 11:300afaf2696b 80 int main(void)
coreyharris 3:420d5efbd967 81 {
kidecha_rahul 23:288c5d1babd1 82 uint16_t tic=0;
kidecha_rahul 23:288c5d1babd1 83
parthsagar2010 11:300afaf2696b 84 // Constants
coreyharris 4:06e258ff0b97 85 const int EINT_STATUS_MASK = 1 << 23;
coreyharris 6:86ac850c718d 86 const int FIFO_OVF_MASK = 0x7;
coreyharris 6:86ac850c718d 87 const int FIFO_VALID_SAMPLE_MASK = 0x0;
coreyharris 6:86ac850c718d 88 const int FIFO_FAST_SAMPLE_MASK = 0x1;
coreyharris 6:86ac850c718d 89 const int ETAG_BITS_MASK = 0x7;
kidecha_rahul 17:f0008ebb1040 90
kidecha_rahul 23:288c5d1babd1 91 // timer_fast.start();
kidecha_rahul 25:06ba30a29754 92 DigitalOut RGBr(P1_2);
kidecha_rahul 25:06ba30a29754 93 DigitalOut RGBg(P1_1);
kidecha_rahul 25:06ba30a29754 94 DigitalOut RGBb(P1_0);
kidecha_rahul 25:06ba30a29754 95
parthsagar2010 27:3b45b47d735d 96
kidecha_rahul 25:06ba30a29754 97
parthsagar2010 27:3b45b47d735d 98
kidecha_rahul 25:06ba30a29754 99
kidecha_rahul 25:06ba30a29754 100 S0=0;
kidecha_rahul 25:06ba30a29754 101 S1=0;
parthsagar2010 27:3b45b47d735d 102
kidecha_rahul 25:06ba30a29754 103
parthsagar2010 10:54aa50490b15 104 // pc.baud(9600);
coreyharris 3:420d5efbd967 105
parthsagar2010 11:300afaf2696b 106 //pc.set_baud(115200); // Baud rate = 115200
parthsagar2010 11:300afaf2696b 107 //pc.set_format(
parthsagar2010 11:300afaf2696b 108 // /* bits */ 8,
parthsagar2010 11:300afaf2696b 109 // /* parity */ BufferedSerial::None,
parthsagar2010 11:300afaf2696b 110 // /* stop bit */ 1 //1
parthsagar2010 11:300afaf2696b 111 //);
parthsagar2010 10:54aa50490b15 112 //uart_1.baud(115200);
kidecha_rahul 23:288c5d1babd1 113
kidecha_rahul 23:288c5d1babd1 114 //--------------------------------- ECG Initialization ---------------------------------------------//
coreyharris 1:86843c27cc81 115 InterruptIn ecgFIFO_int(P5_4); // Config P5_4 as int. in for the
parthsagar2010 8:6b9359f81cc0 116 ecgFIFO_int.fall(&ecgFIFO_callback_1); // ecg FIFO interrupt at falling edge
coreyharris 0:38c49bc37c7c 117
coreyharris 1:86843c27cc81 118 SPI spiBus(SPI2_MOSI, SPI2_MISO, SPI2_SCK); // SPI bus, P5_1 = MOSI,
coreyharris 1:86843c27cc81 119 // P5_2 = MISO, P5_0 = SCK
parthsagar2010 10:54aa50490b15 120
coreyharris 4:06e258ff0b97 121 MAX30003 ecgAFE(spiBus, P5_3); // New MAX30003 on spiBus, CS = P5_3
parthsagar2010 8:6b9359f81cc0 122
coreyharris 4:06e258ff0b97 123 ecg_config(ecgAFE); // Config ECG
parthsagar2010 12:10b49ad3c492 124
coreyharris 0:38c49bc37c7c 125
coreyharris 4:06e258ff0b97 126 ecgAFE.writeRegister( MAX30003::SYNCH , 0);
coreyharris 0:38c49bc37c7c 127
kidecha_rahul 23:288c5d1babd1 128 //-------------------------------------------------------------------------------------------------//
kidecha_rahul 23:288c5d1babd1 129
coreyharris 4:06e258ff0b97 130 uint32_t ecgFIFO, readECGSamples, idx, ETAG[32], status;
coreyharris 1:86843c27cc81 131 int16_t ecgSample[32];
parthsagar2010 9:24ecf16eab0f 132 //bool timerflag = false;
kidecha_rahul 23:288c5d1babd1 133 int16_t ecgSample_1sec[256];
kidecha_rahul 25:06ba30a29754 134 // uint8_t ecg_1 = 0;
kidecha_rahul 25:06ba30a29754 135 // uint8_t ecg_2 = 0;
parthsagar2010 10:54aa50490b15 136 uint16_t onesec_counter = 0;
parthsagar2010 10:54aa50490b15 137 uint16_t onesec_counter_temp = 0;
parthsagar2010 10:54aa50490b15 138
kidecha_rahul 22:07eec382c267 139 // int16_t sample = 300;
kidecha_rahul 23:288c5d1babd1 140 // uint8_t final[10];
parthsagar2010 12:10b49ad3c492 141
parthsagar2010 10:54aa50490b15 142 uint16_t checksum_ = 0;
parthsagar2010 10:54aa50490b15 143 uint16_t mod_checksum = 0;
parthsagar2010 10:54aa50490b15 144
parthsagar2010 10:54aa50490b15 145 uint8_t p_1 = 0;
parthsagar2010 10:54aa50490b15 146 uint8_t p_2 = 0;
parthsagar2010 10:54aa50490b15 147 uint8_t p_3 = 0;
parthsagar2010 10:54aa50490b15 148 uint8_t p_4 = 0;
kidecha_rahul 18:101042d225ef 149 // uint8_t channel_num= 1;
kidecha_rahul 20:2e999e6568c1 150 uint8_t channel_num[1]= {0};
parthsagar2010 10:54aa50490b15 151 uint8_t data_len_1 = 0;
parthsagar2010 10:54aa50490b15 152 uint8_t data_len_2 = 0;
parthsagar2010 12:10b49ad3c492 153 uint32_t packet_1 = 0;
parthsagar2010 10:54aa50490b15 154 uint8_t cksm_1 = 0;
parthsagar2010 10:54aa50490b15 155 uint8_t cksm_2 = 0;
parthsagar2010 26:c41e561a02de 156 //uint8_t header_device_id[3] = {0,0,210}; //OLD device ID
parthsagar2010 27:3b45b47d735d 157 // uint8_t header_device_id[3] = {0,0,12}; //
parthsagar2010 27:3b45b47d735d 158 uint8_t header_device_id[3] = {0,0,1};
parthsagar2010 10:54aa50490b15 159 uint8_t header_packet_type[2] = {0,2};
kidecha_rahul 25:06ba30a29754 160 uint8_t battery_level[1] ={0};
parthsagar2010 10:54aa50490b15 161
parthsagar2010 10:54aa50490b15 162 uint8_t ending[5] = {'@','#','%','!','7'};
parthsagar2010 10:54aa50490b15 163
parthsagar2010 15:0356e9454a60 164 bool flip = true;
kidecha_rahul 23:288c5d1babd1 165 int sampleps = 0;
parthsagar2010 15:0356e9454a60 166
kidecha_rahul 25:06ba30a29754 167 char buf[30];
parthsagar2010 15:0356e9454a60 168 // pc.write("Welcome",8*sizeof(char));
parthsagar2010 15:0356e9454a60 169 // printf("In the main loop"); //printf("In the main loop");
kidecha_rahul 23:288c5d1babd1 170 bool flag_first = false;
kidecha_rahul 23:288c5d1babd1 171 bool timestamp_reader = false;
kidecha_rahul 17:f0008ebb1040 172
kidecha_rahul 17:f0008ebb1040 173
kidecha_rahul 23:288c5d1babd1 174 //---------------------------------software RTC Start---------------------------------------------//
kidecha_rahul 23:288c5d1babd1 175 do
kidecha_rahul 23:288c5d1babd1 176 {
kidecha_rahul 23:288c5d1babd1 177 if (pc.readable())
kidecha_rahul 23:288c5d1babd1 178 {
kidecha_rahul 23:288c5d1babd1 179 scanf("%s",buf);
kidecha_rahul 23:288c5d1babd1 180 //printf("The entered string is %s : \n ",buf);*/
kidecha_rahul 23:288c5d1babd1 181 // buf[20] = 1621297647;
kidecha_rahul 23:288c5d1babd1 182 packet_1 = atoi(buf);
kidecha_rahul 23:288c5d1babd1 183 set_time(packet_1);
parthsagar2010 12:10b49ad3c492 184
kidecha_rahul 23:288c5d1babd1 185 if ((packet_1 % 60) == 0)
kidecha_rahul 23:288c5d1babd1 186 {
kidecha_rahul 23:288c5d1babd1 187 for (int u = 0;u<3;u++)
kidecha_rahul 23:288c5d1babd1 188 {
kidecha_rahul 23:288c5d1babd1 189 if (pc.readable())
kidecha_rahul 23:288c5d1babd1 190 {
kidecha_rahul 23:288c5d1babd1 191 scanf("%s",buf);
kidecha_rahul 23:288c5d1babd1 192 printf("Entered the minute string %s : \n ",buf);
kidecha_rahul 23:288c5d1babd1 193
kidecha_rahul 23:288c5d1babd1 194 packet_1 = atoi(buf);
kidecha_rahul 23:288c5d1babd1 195 set_time(packet_1);
kidecha_rahul 23:288c5d1babd1 196 }
kidecha_rahul 23:288c5d1babd1 197 }
kidecha_rahul 23:288c5d1babd1 198 timestamp_reader = true;
kidecha_rahul 23:288c5d1babd1 199 }
kidecha_rahul 23:288c5d1babd1 200 else
kidecha_rahul 23:288c5d1babd1 201 {
parthsagar2010 27:3b45b47d735d 202 RGBr=1;
parthsagar2010 27:3b45b47d735d 203 RGBg=1;
kidecha_rahul 23:288c5d1babd1 204 }
kidecha_rahul 23:288c5d1babd1 205 flag_first = true;
kidecha_rahul 23:288c5d1babd1 206
kidecha_rahul 23:288c5d1babd1 207 }
kidecha_rahul 23:288c5d1babd1 208
kidecha_rahul 23:288c5d1babd1 209 } while (timestamp_reader==false); // take the third timestamp
kidecha_rahul 23:288c5d1babd1 210
kidecha_rahul 23:288c5d1babd1 211 //---------------------------------software RTC End---------------------------------------------//
parthsagar2010 27:3b45b47d735d 212
kidecha_rahul 17:f0008ebb1040 213
parthsagar2010 16:520034b124b6 214 while(1)
parthsagar2010 9:24ecf16eab0f 215 {
kidecha_rahul 23:288c5d1babd1 216
kidecha_rahul 25:06ba30a29754 217 unsigned short raw;
kidecha_rahul 25:06ba30a29754 218 raw = ain_5.read_u16();
kidecha_rahul 25:06ba30a29754 219 float vin;
kidecha_rahul 25:06ba30a29754 220 vin = ((raw*5*1.2)/ 65535.0);
kidecha_rahul 25:06ba30a29754 221 output = ((vin - battery_min) / (battery_max - battery_min)) * 100;
kidecha_rahul 25:06ba30a29754 222
kidecha_rahul 25:06ba30a29754 223 if (output < 100)
kidecha_rahul 25:06ba30a29754 224 battery_level[0] = {output};
kidecha_rahul 25:06ba30a29754 225 else
kidecha_rahul 25:06ba30a29754 226 battery_level[0] ={99};
parthsagar2010 27:3b45b47d735d 227
parthsagar2010 27:3b45b47d735d 228 if (output <10 )
parthsagar2010 27:3b45b47d735d 229 RGBr=1;
parthsagar2010 27:3b45b47d735d 230 RGBg=1;
parthsagar2010 27:3b45b47d735d 231
parthsagar2010 27:3b45b47d735d 232
kidecha_rahul 25:06ba30a29754 233 if ((onesec_counter>=125))
kidecha_rahul 23:288c5d1babd1 234 {
kidecha_rahul 23:288c5d1babd1 235 tic++;
kidecha_rahul 23:288c5d1babd1 236
kidecha_rahul 18:101042d225ef 237 pc.write((uint8_t *)header_device_id, sizeof(header_device_id)); // device ID
parthsagar2010 10:54aa50490b15 238
kidecha_rahul 18:101042d225ef 239 pc.write((uint8_t *)header_packet_type, sizeof(header_packet_type)); // packet type
kidecha_rahul 18:101042d225ef 240
kidecha_rahul 23:288c5d1babd1 241 time_t seconds = time(NULL);
kidecha_rahul 23:288c5d1babd1 242 packet_1 = seconds;
kidecha_rahul 23:288c5d1babd1 243
kidecha_rahul 17:f0008ebb1040 244 //time_t seconds = time(NULL); // if remove it the timestamp will be static
kidecha_rahul 23:288c5d1babd1 245 //packet_1 ++;
kidecha_rahul 23:288c5d1babd1 246 // printf("Time as seconds since January 1, 1970 = %d\n", packet_1);
parthsagar2010 10:54aa50490b15 247 p_1 = packet_1 & 0xff;
parthsagar2010 10:54aa50490b15 248 p_2 = (packet_1 >> 8) & 0xff;
parthsagar2010 10:54aa50490b15 249 p_3 = (packet_1 >> 16) & 0xff;
parthsagar2010 10:54aa50490b15 250 p_4 = (packet_1 >> 24) & 0xff;
parthsagar2010 10:54aa50490b15 251 // checksum_ = checksum_ + (packet_1 & 0xffff) + ((packet_1 >> 16) & 0xffff) ;
parthsagar2010 10:54aa50490b15 252 checksum_ = checksum_ + p_1 + p_2 + p_3 + p_4 ;
parthsagar2010 10:54aa50490b15 253 uint8_t header_packet_id[4] = {p_4,p_3,p_2,p_1};
kidecha_rahul 18:101042d225ef 254 pc.write((uint8_t *)header_packet_id, sizeof(header_packet_id)); // packet ID
kidecha_rahul 18:101042d225ef 255
kidecha_rahul 18:101042d225ef 256
parthsagar2010 16:520034b124b6 257 pc.write((uint8_t *)channel_num, sizeof(channel_num));
kidecha_rahul 18:101042d225ef 258
kidecha_rahul 25:06ba30a29754 259 pc.write((uint8_t *)battery_level, sizeof(battery_level));
kidecha_rahul 25:06ba30a29754 260
parthsagar2010 10:54aa50490b15 261 onesec_counter_temp = onesec_counter * 2;
parthsagar2010 10:54aa50490b15 262 data_len_1 = onesec_counter_temp & 0xff;
parthsagar2010 10:54aa50490b15 263 data_len_2 = (onesec_counter_temp >> 8) & 0xff;
parthsagar2010 10:54aa50490b15 264 checksum_ = checksum_ + data_len_1 + data_len_2;
parthsagar2010 10:54aa50490b15 265 //pc.write((uint32_t *)packet_1, sizeof(packet_1));
parthsagar2010 10:54aa50490b15 266 uint8_t header_ecg_datalen[2] = {data_len_2,data_len_1};
parthsagar2010 10:54aa50490b15 267 pc.write((uint8_t *)header_ecg_datalen, sizeof(header_ecg_datalen));
kidecha_rahul 18:101042d225ef 268
parthsagar2010 10:54aa50490b15 269 mod_checksum = checksum_ % 65536 ;
parthsagar2010 10:54aa50490b15 270 cksm_1 = mod_checksum & 0xff;
parthsagar2010 10:54aa50490b15 271 cksm_2 = (mod_checksum >> 8) & 0xff;
parthsagar2010 10:54aa50490b15 272 uint8_t header_ecg_checksum[2] = {cksm_2,cksm_1};
parthsagar2010 10:54aa50490b15 273 pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum));
parthsagar2010 10:54aa50490b15 274
parthsagar2010 10:54aa50490b15 275 pc.write((int16_t *)ecgSample_1sec,onesec_counter * sizeof(int16_t));
parthsagar2010 15:0356e9454a60 276 // printf("Samples per second %d \n", (onesec_counter));
parthsagar2010 10:54aa50490b15 277 onesec_counter = 0;
parthsagar2010 10:54aa50490b15 278 // memset(ecgSample_1sec, 0, sizeof(ecgSample_1sec));
parthsagar2010 10:54aa50490b15 279
parthsagar2010 10:54aa50490b15 280 pc.write((uint8_t *)ending, sizeof(ending));
parthsagar2010 10:54aa50490b15 281 checksum_ = 0;
parthsagar2010 15:0356e9454a60 282 //t.stop();
parthsagar2010 11:300afaf2696b 283 // auto us = t.elapsed_time().count();
parthsagar2010 15:0356e9454a60 284 // float time_taken = us/1000000;
parthsagar2010 15:0356e9454a60 285 // printf("Timer time: %lu ms \n", (time_taken*100));
parthsagar2010 15:0356e9454a60 286 // t.reset();
parthsagar2010 15:0356e9454a60 287 // timer_fast.reset();
kidecha_rahul 19:7a084dc2f09d 288
kidecha_rahul 23:288c5d1babd1 289
parthsagar2010 26:c41e561a02de 290 if (tic==60) //C1
kidecha_rahul 19:7a084dc2f09d 291 {
kidecha_rahul 19:7a084dc2f09d 292 S0=1;
kidecha_rahul 19:7a084dc2f09d 293 S1=0;
parthsagar2010 27:3b45b47d735d 294
kidecha_rahul 20:2e999e6568c1 295 channel_num[0]= {1};
kidecha_rahul 19:7a084dc2f09d 296 }
parthsagar2010 26:c41e561a02de 297 if (tic==120) //C2
kidecha_rahul 19:7a084dc2f09d 298 {
kidecha_rahul 19:7a084dc2f09d 299 S0=0;
kidecha_rahul 19:7a084dc2f09d 300 S1=1;
parthsagar2010 27:3b45b47d735d 301
kidecha_rahul 20:2e999e6568c1 302 channel_num[0]= {2};
kidecha_rahul 19:7a084dc2f09d 303 }
kidecha_rahul 19:7a084dc2f09d 304
parthsagar2010 26:c41e561a02de 305 if (tic==180) // C0
kidecha_rahul 19:7a084dc2f09d 306 {
kidecha_rahul 19:7a084dc2f09d 307 S0=0;
kidecha_rahul 19:7a084dc2f09d 308 S1=0;
parthsagar2010 27:3b45b47d735d 309
kidecha_rahul 20:2e999e6568c1 310 channel_num[0]= {0};
kidecha_rahul 23:288c5d1babd1 311 tic=0;}
kidecha_rahul 23:288c5d1babd1 312
kidecha_rahul 21:193a10530741 313
kidecha_rahul 23:288c5d1babd1 314 // for channel selection
kidecha_rahul 23:288c5d1babd1 315
kidecha_rahul 25:06ba30a29754 316 if (A==0) //
parthsagar2010 26:c41e561a02de 317 {tic=185;
kidecha_rahul 22:07eec382c267 318 S0=0;
kidecha_rahul 22:07eec382c267 319 S1=0;
parthsagar2010 27:3b45b47d735d 320
kidecha_rahul 25:06ba30a29754 321
kidecha_rahul 23:288c5d1babd1 322 // S2=0;
kidecha_rahul 23:288c5d1babd1 323 // S3=0;
kidecha_rahul 22:07eec382c267 324 channel_num[0]= {0};}
kidecha_rahul 22:07eec382c267 325
kidecha_rahul 23:288c5d1babd1 326 if (B==0) //
parthsagar2010 26:c41e561a02de 327 {tic=190;
kidecha_rahul 21:193a10530741 328 S0=1;
kidecha_rahul 21:193a10530741 329 S1=0;
parthsagar2010 27:3b45b47d735d 330
kidecha_rahul 23:288c5d1babd1 331 // S2=0;
kidecha_rahul 23:288c5d1babd1 332 // S3=0;
kidecha_rahul 22:07eec382c267 333 channel_num[0]= {1};}
kidecha_rahul 22:07eec382c267 334
kidecha_rahul 23:288c5d1babd1 335 if (C==0) //
parthsagar2010 26:c41e561a02de 336 {tic=200;
kidecha_rahul 21:193a10530741 337 S0=0;
kidecha_rahul 21:193a10530741 338 S1=1;
parthsagar2010 27:3b45b47d735d 339
kidecha_rahul 25:06ba30a29754 340
kidecha_rahul 23:288c5d1babd1 341 // S2=0;
kidecha_rahul 23:288c5d1babd1 342 // S3=0;
kidecha_rahul 25:06ba30a29754 343 channel_num[0]= {2};}
kidecha_rahul 23:288c5d1babd1 344
kidecha_rahul 19:7a084dc2f09d 345
parthsagar2010 9:24ecf16eab0f 346 }
coreyharris 4:06e258ff0b97 347 // Read back ECG samples from the FIFO
kidecha_rahul 23:288c5d1babd1 348 else if((ecgFIFOIntFlag==1))// && (timerflag == 0))
parthsagar2010 9:24ecf16eab0f 349 {
coreyharris 2:812d40f1853d 350
coreyharris 5:f8d1f651bef5 351 ecgFIFOIntFlag = 0;
coreyharris 4:06e258ff0b97 352 status = ecgAFE.readRegister( MAX30003::STATUS ); // Read the STATUS register
coreyharris 2:812d40f1853d 353
coreyharris 3:420d5efbd967 354 // Check if EINT interrupt asserted
parthsagar2010 9:24ecf16eab0f 355 if ( ( status & EINT_STATUS_MASK ) == EINT_STATUS_MASK )
parthsagar2010 9:24ecf16eab0f 356 {
coreyharris 3:420d5efbd967 357
coreyharris 4:06e258ff0b97 358 readECGSamples = 0; // Reset sample counter
coreyharris 3:420d5efbd967 359
coreyharris 2:812d40f1853d 360 do {
coreyharris 6:86ac850c718d 361 ecgFIFO = ecgAFE.readRegister( MAX30003::ECG_FIFO ); // Read FIFO
coreyharris 4:06e258ff0b97 362 ecgSample[readECGSamples] = ecgFIFO >> 8; // Isolate voltage data
parthsagar2010 10:54aa50490b15 363 ecgSample[readECGSamples] = ((ecgSample[readECGSamples]<<8)&0xFF00)|((ecgSample[readECGSamples]>>8)&0x00FF);
coreyharris 6:86ac850c718d 364 ETAG[readECGSamples] = ( ecgFIFO >> 3 ) & ETAG_BITS_MASK; // Isolate ETAG
coreyharris 4:06e258ff0b97 365 readECGSamples++; // Increment sample counter
coreyharris 3:420d5efbd967 366
coreyharris 3:420d5efbd967 367 // Check that sample is not last sample in FIFO
coreyharris 6:86ac850c718d 368 } while ( ETAG[readECGSamples-1] == FIFO_VALID_SAMPLE_MASK ||
coreyharris 6:86ac850c718d 369 ETAG[readECGSamples-1] == FIFO_FAST_SAMPLE_MASK );
coreyharris 2:812d40f1853d 370
coreyharris 3:420d5efbd967 371 // Check if FIFO has overflowed
parthsagar2010 9:24ecf16eab0f 372 if( ETAG[readECGSamples - 1] == FIFO_OVF_MASK )
parthsagar2010 9:24ecf16eab0f 373 {
coreyharris 3:420d5efbd967 374 ecgAFE.writeRegister( MAX30003::FIFO_RST , 0); // Reset FIFO
parthsagar2010 27:3b45b47d735d 375 //notifies the user that an over flow occured
coreyharris 3:420d5efbd967 376 }
parthsagar2010 10:54aa50490b15 377 //uint8_t header_ecg_checksum[2] = {'%','%'};
parthsagar2010 10:54aa50490b15 378 //pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum));
coreyharris 3:420d5efbd967 379
parthsagar2010 10:54aa50490b15 380 //pc.write((int16_t *)ecgSample,readECGSamples * sizeof(int16_t ));
parthsagar2010 10:54aa50490b15 381 //memcpy(ecgSample_1sec , ecgSample, sizeof(ecgSample));
parthsagar2010 10:54aa50490b15 382
parthsagar2010 10:54aa50490b15 383 //memcpy(ecgSample_1sec + (onesec_counter * sizeof(int16_t)), ecgSample, sizeof(ecgSample));
parthsagar2010 15:0356e9454a60 384
parthsagar2010 15:0356e9454a60 385 if (flip)
parthsagar2010 15:0356e9454a60 386 {
parthsagar2010 15:0356e9454a60 387 sampleps = 12;
parthsagar2010 15:0356e9454a60 388 //printf("Flipped 12\r\n");
parthsagar2010 15:0356e9454a60 389 }
parthsagar2010 15:0356e9454a60 390 else
parthsagar2010 15:0356e9454a60 391 {
parthsagar2010 15:0356e9454a60 392 sampleps = 13;
parthsagar2010 15:0356e9454a60 393 // printf("Flipped 13\r\n");
parthsagar2010 15:0356e9454a60 394 }
parthsagar2010 15:0356e9454a60 395
parthsagar2010 15:0356e9454a60 396 for( idx = 0; idx < sampleps; idx++ )
parthsagar2010 9:24ecf16eab0f 397 {
parthsagar2010 9:24ecf16eab0f 398 //pc.printf("%6d\r\n", ecgSample[idx]);
parthsagar2010 9:24ecf16eab0f 399 ecgSample_1sec[onesec_counter] = ecgSample[idx];
parthsagar2010 10:54aa50490b15 400
parthsagar2010 10:54aa50490b15 401 cksm_1 = ecgSample[idx] & 0xff;
parthsagar2010 10:54aa50490b15 402 cksm_2 = (ecgSample[idx] >> 8) & 0xff;
parthsagar2010 10:54aa50490b15 403 checksum_ += cksm_1 + cksm_2;
parthsagar2010 9:24ecf16eab0f 404 onesec_counter++;
coreyharris 6:86ac850c718d 405 }
parthsagar2010 15:0356e9454a60 406 flip =!flip;
parthsagar2010 27:3b45b47d735d 407
coreyharris 0:38c49bc37c7c 408 }
parthsagar2010 10:54aa50490b15 409
coreyharris 0:38c49bc37c7c 410 }
coreyharris 0:38c49bc37c7c 411 }
coreyharris 0:38c49bc37c7c 412 }
kidecha_rahul 23:288c5d1babd1 413
kidecha_rahul 23:288c5d1babd1 414
kidecha_rahul 23:288c5d1babd1 415
kidecha_rahul 23:288c5d1babd1 416
coreyharris 0:38c49bc37c7c 417 void ecg_config(MAX30003& ecgAFE) {
kidecha_rahul 23:288c5d1babd1 418
coreyharris 1:86843c27cc81 419 // Reset ECG to clear registers
coreyharris 1:86843c27cc81 420 ecgAFE.writeRegister( MAX30003::SW_RST , 0);
coreyharris 0:38c49bc37c7c 421
coreyharris 1:86843c27cc81 422 // General config register setting
coreyharris 1:86843c27cc81 423 MAX30003::GeneralConfiguration_u CNFG_GEN_r;
coreyharris 3:420d5efbd967 424 CNFG_GEN_r.bits.en_ecg = 1; // Enable ECG channel
coreyharris 3:420d5efbd967 425 CNFG_GEN_r.bits.rbiasn = 1; // Enable resistive bias on negative input
coreyharris 3:420d5efbd967 426 CNFG_GEN_r.bits.rbiasp = 1; // Enable resistive bias on positive input
coreyharris 3:420d5efbd967 427 CNFG_GEN_r.bits.en_rbias = 1; // Enable resistive bias
coreyharris 3:420d5efbd967 428 CNFG_GEN_r.bits.imag = 2; // Current magnitude = 10nA
coreyharris 3:420d5efbd967 429 CNFG_GEN_r.bits.en_dcloff = 1; // Enable DC lead-off detection
parthsagar2010 12:10b49ad3c492 430 //CNFG_GEN_r.bits.fmstr = 1; //125 sps FMSTR 1
coreyharris 1:86843c27cc81 431 ecgAFE.writeRegister( MAX30003::CNFG_GEN , CNFG_GEN_r.all);
coreyharris 1:86843c27cc81 432
coreyharris 1:86843c27cc81 433
coreyharris 1:86843c27cc81 434 // ECG Config register setting
coreyharris 1:86843c27cc81 435 MAX30003::ECGConfiguration_u CNFG_ECG_r;
coreyharris 3:420d5efbd967 436 CNFG_ECG_r.bits.dlpf = 1; // Digital LPF cutoff = 40Hz
coreyharris 3:420d5efbd967 437 CNFG_ECG_r.bits.dhpf = 1; // Digital HPF cutoff = 0.5Hz
coreyharris 3:420d5efbd967 438 CNFG_ECG_r.bits.gain = 3; // ECG gain = 160V/V
kidecha_rahul 23:288c5d1babd1 439 CNFG_ECG_r.bits.rate = 2; // Sample rate = 128 sps
coreyharris 1:86843c27cc81 440 ecgAFE.writeRegister( MAX30003::CNFG_ECG , CNFG_ECG_r.all);
coreyharris 1:86843c27cc81 441
coreyharris 1:86843c27cc81 442
coreyharris 1:86843c27cc81 443 //R-to-R configuration
coreyharris 1:86843c27cc81 444 MAX30003::RtoR1Configuration_u CNFG_RTOR_r;
coreyharris 3:420d5efbd967 445 CNFG_RTOR_r.bits.en_rtor = 1; // Enable R-to-R detection
coreyharris 1:86843c27cc81 446 ecgAFE.writeRegister( MAX30003::CNFG_RTOR1 , CNFG_RTOR_r.all);
coreyharris 1:86843c27cc81 447
coreyharris 1:86843c27cc81 448
coreyharris 1:86843c27cc81 449 //Manage interrupts register setting
coreyharris 1:86843c27cc81 450 MAX30003::ManageInterrupts_u MNG_INT_r;
coreyharris 3:420d5efbd967 451 MNG_INT_r.bits.efit = 0b00011; // Assert EINT w/ 4 unread samples
coreyharris 3:420d5efbd967 452 MNG_INT_r.bits.clr_rrint = 0b01; // Clear R-to-R on RTOR reg. read back
coreyharris 1:86843c27cc81 453 ecgAFE.writeRegister( MAX30003::MNGR_INT , MNG_INT_r.all);
coreyharris 0:38c49bc37c7c 454
coreyharris 0:38c49bc37c7c 455
coreyharris 1:86843c27cc81 456 //Enable interrupts register setting
coreyharris 1:86843c27cc81 457 MAX30003::EnableInterrupts_u EN_INT_r;
coreyharris 5:f8d1f651bef5 458 EN_INT_r.all = 0;
coreyharris 3:420d5efbd967 459 EN_INT_r.bits.en_eint = 1; // Enable EINT interrupt
coreyharris 4:06e258ff0b97 460 EN_INT_r.bits.en_rrint = 0; // Disable R-to-R interrupt
coreyharris 3:420d5efbd967 461 EN_INT_r.bits.intb_type = 3; // Open-drain NMOS with internal pullup
coreyharris 1:86843c27cc81 462 ecgAFE.writeRegister( MAX30003::EN_INT , EN_INT_r.all);
coreyharris 1:86843c27cc81 463
coreyharris 1:86843c27cc81 464
coreyharris 1:86843c27cc81 465 //Dyanmic modes config
coreyharris 1:86843c27cc81 466 MAX30003::ManageDynamicModes_u MNG_DYN_r;
coreyharris 3:420d5efbd967 467 MNG_DYN_r.bits.fast = 0; // Fast recovery mode disabled
coreyharris 1:86843c27cc81 468 ecgAFE.writeRegister( MAX30003::MNGR_DYN , MNG_DYN_r.all);
coreyharris 5:f8d1f651bef5 469
coreyharris 5:f8d1f651bef5 470 // MUX Config
coreyharris 5:f8d1f651bef5 471 MAX30003::MuxConfiguration_u CNFG_MUX_r;
coreyharris 5:f8d1f651bef5 472 CNFG_MUX_r.bits.openn = 0; // Connect ECGN to AFE channel
coreyharris 5:f8d1f651bef5 473 CNFG_MUX_r.bits.openp = 0; // Connect ECGP to AFE channel
coreyharris 5:f8d1f651bef5 474 ecgAFE.writeRegister( MAX30003::CNFG_EMUX , CNFG_MUX_r.all);
coreyharris 1:86843c27cc81 475
coreyharris 1:86843c27cc81 476 return;
coreyharris 0:38c49bc37c7c 477 }
kidecha_rahul 23:288c5d1babd1 478