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