battery_code
Dependencies: MAX30003 max32630fthr DS1307
main.cpp@23:288c5d1babd1, 2021-08-03 (annotated)
- Committer:
- kidecha_rahul
- Date:
- Tue Aug 03 19:39:42 2021 +0000
- Revision:
- 23:288c5d1babd1
- Parent:
- 22:07eec382c267
- Child:
- 24:98332ec40aff
832021;
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 | |
kidecha_rahul | 23:288c5d1babd1 | 21 | DigitalOut S0(P4_0); |
kidecha_rahul | 23:288c5d1babd1 | 22 | DigitalOut S1(P5_6); |
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 |
kidecha_rahul | 23:288c5d1babd1 | 27 | DigitalIn A(P6_0); //P17 |
kidecha_rahul | 23:288c5d1babd1 | 28 | DigitalIn B(P3_5); //P18 |
kidecha_rahul | 23:288c5d1babd1 | 29 | DigitalIn C(P3_4); //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 | 15:0356e9454a60 | 90 | DigitalOut rLed(LED2, LED_ON); |
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 | 10:54aa50490b15 | 138 | uint8_t data_len_1 = 0; |
parthsagar2010 | 10:54aa50490b15 | 139 | uint8_t data_len_2 = 0; |
parthsagar2010 | 12:10b49ad3c492 | 140 | uint32_t packet_1 = 0; |
parthsagar2010 | 10:54aa50490b15 | 141 | uint8_t cksm_1 = 0; |
parthsagar2010 | 10:54aa50490b15 | 142 | uint8_t cksm_2 = 0; |
parthsagar2010 | 10:54aa50490b15 | 143 | uint8_t header_device_id[3] = {0,0,210}; |
kidecha_rahul | 23:288c5d1babd1 | 144 | //uint8_t header_device_id[3] = {0,0,1}; //for rahul |
parthsagar2010 | 10:54aa50490b15 | 145 | uint8_t header_packet_type[2] = {0,2}; |
parthsagar2010 | 10:54aa50490b15 | 146 | |
parthsagar2010 | 10:54aa50490b15 | 147 | uint8_t ending[5] = {'@','#','%','!','7'}; |
parthsagar2010 | 10:54aa50490b15 | 148 | |
parthsagar2010 | 15:0356e9454a60 | 149 | bool flip = true; |
kidecha_rahul | 23:288c5d1babd1 | 150 | int sampleps = 0; |
parthsagar2010 | 15:0356e9454a60 | 151 | |
kidecha_rahul | 23:288c5d1babd1 | 152 | char buf[20]; |
parthsagar2010 | 15:0356e9454a60 | 153 | // pc.write("Welcome",8*sizeof(char)); |
parthsagar2010 | 15:0356e9454a60 | 154 | // printf("In the main loop"); //printf("In the main loop"); |
kidecha_rahul | 23:288c5d1babd1 | 155 | bool flag_first = false; |
kidecha_rahul | 23:288c5d1babd1 | 156 | bool timestamp_reader = false; |
kidecha_rahul | 17:f0008ebb1040 | 157 | |
kidecha_rahul | 17:f0008ebb1040 | 158 | |
kidecha_rahul | 23:288c5d1babd1 | 159 | //---------------------------------software RTC Start---------------------------------------------// |
kidecha_rahul | 23:288c5d1babd1 | 160 | do |
kidecha_rahul | 23:288c5d1babd1 | 161 | { |
kidecha_rahul | 23:288c5d1babd1 | 162 | if (pc.readable()) |
kidecha_rahul | 23:288c5d1babd1 | 163 | { |
kidecha_rahul | 23:288c5d1babd1 | 164 | scanf("%s",buf); |
kidecha_rahul | 23:288c5d1babd1 | 165 | //printf("The entered string is %s : \n ",buf);*/ |
kidecha_rahul | 23:288c5d1babd1 | 166 | // buf[20] = 1621297647; |
kidecha_rahul | 23:288c5d1babd1 | 167 | packet_1 = atoi(buf); |
kidecha_rahul | 23:288c5d1babd1 | 168 | set_time(packet_1); |
parthsagar2010 | 12:10b49ad3c492 | 169 | |
kidecha_rahul | 23:288c5d1babd1 | 170 | if ((packet_1 % 60) == 0) |
kidecha_rahul | 23:288c5d1babd1 | 171 | { |
kidecha_rahul | 23:288c5d1babd1 | 172 | for (int u = 0;u<3;u++) |
kidecha_rahul | 23:288c5d1babd1 | 173 | { |
kidecha_rahul | 23:288c5d1babd1 | 174 | if (pc.readable()) |
kidecha_rahul | 23:288c5d1babd1 | 175 | { |
kidecha_rahul | 23:288c5d1babd1 | 176 | scanf("%s",buf); |
kidecha_rahul | 23:288c5d1babd1 | 177 | printf("Entered the minute string %s : \n ",buf); |
kidecha_rahul | 23:288c5d1babd1 | 178 | |
kidecha_rahul | 23:288c5d1babd1 | 179 | packet_1 = atoi(buf); |
kidecha_rahul | 23:288c5d1babd1 | 180 | set_time(packet_1); |
kidecha_rahul | 23:288c5d1babd1 | 181 | } |
kidecha_rahul | 23:288c5d1babd1 | 182 | } |
kidecha_rahul | 23:288c5d1babd1 | 183 | timestamp_reader = true; |
kidecha_rahul | 23:288c5d1babd1 | 184 | } |
kidecha_rahul | 23:288c5d1babd1 | 185 | else |
kidecha_rahul | 23:288c5d1babd1 | 186 | { |
kidecha_rahul | 23:288c5d1babd1 | 187 | //printf("The timestamp is not divisible by 60 \n"); |
kidecha_rahul | 23:288c5d1babd1 | 188 | } |
kidecha_rahul | 23:288c5d1babd1 | 189 | flag_first = true; |
kidecha_rahul | 23:288c5d1babd1 | 190 | |
kidecha_rahul | 23:288c5d1babd1 | 191 | } |
kidecha_rahul | 23:288c5d1babd1 | 192 | |
kidecha_rahul | 23:288c5d1babd1 | 193 | } while (timestamp_reader==false); // take the third timestamp |
kidecha_rahul | 23:288c5d1babd1 | 194 | |
kidecha_rahul | 23:288c5d1babd1 | 195 | //---------------------------------software RTC End---------------------------------------------// |
kidecha_rahul | 23:288c5d1babd1 | 196 | |
kidecha_rahul | 17:f0008ebb1040 | 197 | |
parthsagar2010 | 16:520034b124b6 | 198 | while(1) |
parthsagar2010 | 9:24ecf16eab0f | 199 | { |
kidecha_rahul | 23:288c5d1babd1 | 200 | |
kidecha_rahul | 23:288c5d1babd1 | 201 | |
kidecha_rahul | 23:288c5d1babd1 | 202 | if ((onesec_counter>=112)) |
kidecha_rahul | 23:288c5d1babd1 | 203 | { |
kidecha_rahul | 23:288c5d1babd1 | 204 | tic++; |
kidecha_rahul | 23:288c5d1babd1 | 205 | |
kidecha_rahul | 18:101042d225ef | 206 | pc.write((uint8_t *)header_device_id, sizeof(header_device_id)); // device ID |
parthsagar2010 | 10:54aa50490b15 | 207 | |
kidecha_rahul | 18:101042d225ef | 208 | pc.write((uint8_t *)header_packet_type, sizeof(header_packet_type)); // packet type |
kidecha_rahul | 18:101042d225ef | 209 | |
kidecha_rahul | 23:288c5d1babd1 | 210 | time_t seconds = time(NULL); |
kidecha_rahul | 23:288c5d1babd1 | 211 | packet_1 = seconds; |
kidecha_rahul | 23:288c5d1babd1 | 212 | |
kidecha_rahul | 17:f0008ebb1040 | 213 | //time_t seconds = time(NULL); // if remove it the timestamp will be static |
kidecha_rahul | 23:288c5d1babd1 | 214 | //packet_1 ++; |
kidecha_rahul | 23:288c5d1babd1 | 215 | // printf("Time as seconds since January 1, 1970 = %d\n", packet_1); |
parthsagar2010 | 10:54aa50490b15 | 216 | p_1 = packet_1 & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 217 | p_2 = (packet_1 >> 8) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 218 | p_3 = (packet_1 >> 16) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 219 | p_4 = (packet_1 >> 24) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 220 | // checksum_ = checksum_ + (packet_1 & 0xffff) + ((packet_1 >> 16) & 0xffff) ; |
parthsagar2010 | 10:54aa50490b15 | 221 | checksum_ = checksum_ + p_1 + p_2 + p_3 + p_4 ; |
parthsagar2010 | 10:54aa50490b15 | 222 | uint8_t header_packet_id[4] = {p_4,p_3,p_2,p_1}; |
kidecha_rahul | 18:101042d225ef | 223 | pc.write((uint8_t *)header_packet_id, sizeof(header_packet_id)); // packet ID |
kidecha_rahul | 18:101042d225ef | 224 | |
kidecha_rahul | 18:101042d225ef | 225 | |
parthsagar2010 | 16:520034b124b6 | 226 | pc.write((uint8_t *)channel_num, sizeof(channel_num)); |
kidecha_rahul | 18:101042d225ef | 227 | |
parthsagar2010 | 10:54aa50490b15 | 228 | onesec_counter_temp = onesec_counter * 2; |
parthsagar2010 | 10:54aa50490b15 | 229 | data_len_1 = onesec_counter_temp & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 230 | data_len_2 = (onesec_counter_temp >> 8) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 231 | checksum_ = checksum_ + data_len_1 + data_len_2; |
parthsagar2010 | 10:54aa50490b15 | 232 | //pc.write((uint32_t *)packet_1, sizeof(packet_1)); |
parthsagar2010 | 10:54aa50490b15 | 233 | uint8_t header_ecg_datalen[2] = {data_len_2,data_len_1}; |
parthsagar2010 | 10:54aa50490b15 | 234 | pc.write((uint8_t *)header_ecg_datalen, sizeof(header_ecg_datalen)); |
kidecha_rahul | 18:101042d225ef | 235 | |
parthsagar2010 | 10:54aa50490b15 | 236 | mod_checksum = checksum_ % 65536 ; |
parthsagar2010 | 10:54aa50490b15 | 237 | cksm_1 = mod_checksum & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 238 | cksm_2 = (mod_checksum >> 8) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 239 | uint8_t header_ecg_checksum[2] = {cksm_2,cksm_1}; |
parthsagar2010 | 10:54aa50490b15 | 240 | pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum)); |
parthsagar2010 | 10:54aa50490b15 | 241 | |
parthsagar2010 | 10:54aa50490b15 | 242 | pc.write((int16_t *)ecgSample_1sec,onesec_counter * sizeof(int16_t)); |
parthsagar2010 | 15:0356e9454a60 | 243 | // printf("Samples per second %d \n", (onesec_counter)); |
parthsagar2010 | 10:54aa50490b15 | 244 | onesec_counter = 0; |
parthsagar2010 | 10:54aa50490b15 | 245 | // memset(ecgSample_1sec, 0, sizeof(ecgSample_1sec)); |
parthsagar2010 | 10:54aa50490b15 | 246 | |
parthsagar2010 | 10:54aa50490b15 | 247 | pc.write((uint8_t *)ending, sizeof(ending)); |
parthsagar2010 | 10:54aa50490b15 | 248 | checksum_ = 0; |
parthsagar2010 | 15:0356e9454a60 | 249 | //t.stop(); |
parthsagar2010 | 11:300afaf2696b | 250 | // auto us = t.elapsed_time().count(); |
parthsagar2010 | 15:0356e9454a60 | 251 | // float time_taken = us/1000000; |
parthsagar2010 | 15:0356e9454a60 | 252 | // printf("Timer time: %lu ms \n", (time_taken*100)); |
parthsagar2010 | 15:0356e9454a60 | 253 | // t.reset(); |
parthsagar2010 | 15:0356e9454a60 | 254 | // timer_fast.reset(); |
kidecha_rahul | 19:7a084dc2f09d | 255 | |
kidecha_rahul | 23:288c5d1babd1 | 256 | |
kidecha_rahul | 23:288c5d1babd1 | 257 | if (tic==10) //C1 |
kidecha_rahul | 19:7a084dc2f09d | 258 | { |
kidecha_rahul | 19:7a084dc2f09d | 259 | S0=1; |
kidecha_rahul | 19:7a084dc2f09d | 260 | S1=0; |
kidecha_rahul | 23:288c5d1babd1 | 261 | // S2=0; |
kidecha_rahul | 23:288c5d1babd1 | 262 | // S3=0; |
kidecha_rahul | 20:2e999e6568c1 | 263 | channel_num[0]= {1}; |
kidecha_rahul | 19:7a084dc2f09d | 264 | } |
kidecha_rahul | 23:288c5d1babd1 | 265 | if (tic==20) //C2 |
kidecha_rahul | 19:7a084dc2f09d | 266 | { |
kidecha_rahul | 19:7a084dc2f09d | 267 | S0=0; |
kidecha_rahul | 19:7a084dc2f09d | 268 | S1=1; |
kidecha_rahul | 23:288c5d1babd1 | 269 | // S2=0; |
kidecha_rahul | 23:288c5d1babd1 | 270 | // S3=0; |
kidecha_rahul | 20:2e999e6568c1 | 271 | channel_num[0]= {2}; |
kidecha_rahul | 19:7a084dc2f09d | 272 | } |
kidecha_rahul | 19:7a084dc2f09d | 273 | |
kidecha_rahul | 23:288c5d1babd1 | 274 | if (tic==30) // C0 |
kidecha_rahul | 19:7a084dc2f09d | 275 | { |
kidecha_rahul | 19:7a084dc2f09d | 276 | S0=0; |
kidecha_rahul | 19:7a084dc2f09d | 277 | S1=0; |
kidecha_rahul | 23:288c5d1babd1 | 278 | // S2=0; |
kidecha_rahul | 23:288c5d1babd1 | 279 | // S3=0; |
kidecha_rahul | 20:2e999e6568c1 | 280 | channel_num[0]= {0}; |
kidecha_rahul | 23:288c5d1babd1 | 281 | tic=0;} |
kidecha_rahul | 23:288c5d1babd1 | 282 | |
kidecha_rahul | 21:193a10530741 | 283 | |
kidecha_rahul | 23:288c5d1babd1 | 284 | // for channel selection |
kidecha_rahul | 23:288c5d1babd1 | 285 | |
kidecha_rahul | 23:288c5d1babd1 | 286 | if (A==0) // |
kidecha_rahul | 23:288c5d1babd1 | 287 | {tic=50; |
kidecha_rahul | 22:07eec382c267 | 288 | S0=0; |
kidecha_rahul | 22:07eec382c267 | 289 | S1=0; |
kidecha_rahul | 23:288c5d1babd1 | 290 | // S2=0; |
kidecha_rahul | 23:288c5d1babd1 | 291 | // S3=0; |
kidecha_rahul | 22:07eec382c267 | 292 | channel_num[0]= {0};} |
kidecha_rahul | 22:07eec382c267 | 293 | |
kidecha_rahul | 23:288c5d1babd1 | 294 | if (B==0) // |
kidecha_rahul | 23:288c5d1babd1 | 295 | {tic=60; |
kidecha_rahul | 21:193a10530741 | 296 | S0=1; |
kidecha_rahul | 21:193a10530741 | 297 | S1=0; |
kidecha_rahul | 23:288c5d1babd1 | 298 | // S2=0; |
kidecha_rahul | 23:288c5d1babd1 | 299 | // S3=0; |
kidecha_rahul | 22:07eec382c267 | 300 | channel_num[0]= {1};} |
kidecha_rahul | 22:07eec382c267 | 301 | |
kidecha_rahul | 23:288c5d1babd1 | 302 | if (C==0) // |
kidecha_rahul | 23:288c5d1babd1 | 303 | {tic=70; |
kidecha_rahul | 21:193a10530741 | 304 | S0=0; |
kidecha_rahul | 21:193a10530741 | 305 | S1=1; |
kidecha_rahul | 23:288c5d1babd1 | 306 | // S2=0; |
kidecha_rahul | 23:288c5d1babd1 | 307 | // S3=0; |
kidecha_rahul | 21:193a10530741 | 308 | channel_num[0]= {2};} |
kidecha_rahul | 23:288c5d1babd1 | 309 | |
kidecha_rahul | 23:288c5d1babd1 | 310 | |
kidecha_rahul | 23:288c5d1babd1 | 311 | |
kidecha_rahul | 23:288c5d1babd1 | 312 | |
kidecha_rahul | 23:288c5d1babd1 | 313 | |
kidecha_rahul | 23:288c5d1babd1 | 314 | |
kidecha_rahul | 23:288c5d1babd1 | 315 | |
kidecha_rahul | 23:288c5d1babd1 | 316 | |
kidecha_rahul | 19:7a084dc2f09d | 317 | |
parthsagar2010 | 9:24ecf16eab0f | 318 | } |
coreyharris | 4:06e258ff0b97 | 319 | // Read back ECG samples from the FIFO |
kidecha_rahul | 23:288c5d1babd1 | 320 | else if((ecgFIFOIntFlag==1))// && (timerflag == 0)) |
parthsagar2010 | 9:24ecf16eab0f | 321 | { |
coreyharris | 2:812d40f1853d | 322 | |
coreyharris | 5:f8d1f651bef5 | 323 | ecgFIFOIntFlag = 0; |
coreyharris | 4:06e258ff0b97 | 324 | status = ecgAFE.readRegister( MAX30003::STATUS ); // Read the STATUS register |
coreyharris | 2:812d40f1853d | 325 | |
coreyharris | 3:420d5efbd967 | 326 | // Check if EINT interrupt asserted |
parthsagar2010 | 9:24ecf16eab0f | 327 | if ( ( status & EINT_STATUS_MASK ) == EINT_STATUS_MASK ) |
parthsagar2010 | 9:24ecf16eab0f | 328 | { |
coreyharris | 3:420d5efbd967 | 329 | |
coreyharris | 4:06e258ff0b97 | 330 | readECGSamples = 0; // Reset sample counter |
coreyharris | 3:420d5efbd967 | 331 | |
coreyharris | 2:812d40f1853d | 332 | do { |
coreyharris | 6:86ac850c718d | 333 | ecgFIFO = ecgAFE.readRegister( MAX30003::ECG_FIFO ); // Read FIFO |
coreyharris | 4:06e258ff0b97 | 334 | ecgSample[readECGSamples] = ecgFIFO >> 8; // Isolate voltage data |
parthsagar2010 | 10:54aa50490b15 | 335 | ecgSample[readECGSamples] = ((ecgSample[readECGSamples]<<8)&0xFF00)|((ecgSample[readECGSamples]>>8)&0x00FF); |
coreyharris | 6:86ac850c718d | 336 | ETAG[readECGSamples] = ( ecgFIFO >> 3 ) & ETAG_BITS_MASK; // Isolate ETAG |
coreyharris | 4:06e258ff0b97 | 337 | readECGSamples++; // Increment sample counter |
coreyharris | 3:420d5efbd967 | 338 | |
coreyharris | 3:420d5efbd967 | 339 | // Check that sample is not last sample in FIFO |
coreyharris | 6:86ac850c718d | 340 | } while ( ETAG[readECGSamples-1] == FIFO_VALID_SAMPLE_MASK || |
coreyharris | 6:86ac850c718d | 341 | ETAG[readECGSamples-1] == FIFO_FAST_SAMPLE_MASK ); |
coreyharris | 2:812d40f1853d | 342 | |
coreyharris | 3:420d5efbd967 | 343 | // Check if FIFO has overflowed |
parthsagar2010 | 9:24ecf16eab0f | 344 | if( ETAG[readECGSamples - 1] == FIFO_OVF_MASK ) |
parthsagar2010 | 9:24ecf16eab0f | 345 | { |
coreyharris | 3:420d5efbd967 | 346 | ecgAFE.writeRegister( MAX30003::FIFO_RST , 0); // Reset FIFO |
johnGreeneMaxim | 7:cf0855a0450a | 347 | rLed = 1;//notifies the user that an over flow occured |
coreyharris | 3:420d5efbd967 | 348 | } |
parthsagar2010 | 10:54aa50490b15 | 349 | //uint8_t header_ecg_checksum[2] = {'%','%'}; |
parthsagar2010 | 10:54aa50490b15 | 350 | //pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum)); |
coreyharris | 3:420d5efbd967 | 351 | |
parthsagar2010 | 10:54aa50490b15 | 352 | //pc.write((int16_t *)ecgSample,readECGSamples * sizeof(int16_t )); |
parthsagar2010 | 10:54aa50490b15 | 353 | //memcpy(ecgSample_1sec , ecgSample, sizeof(ecgSample)); |
parthsagar2010 | 10:54aa50490b15 | 354 | |
parthsagar2010 | 10:54aa50490b15 | 355 | //memcpy(ecgSample_1sec + (onesec_counter * sizeof(int16_t)), ecgSample, sizeof(ecgSample)); |
parthsagar2010 | 15:0356e9454a60 | 356 | |
parthsagar2010 | 15:0356e9454a60 | 357 | if (flip) |
parthsagar2010 | 15:0356e9454a60 | 358 | { |
parthsagar2010 | 15:0356e9454a60 | 359 | sampleps = 12; |
parthsagar2010 | 15:0356e9454a60 | 360 | //printf("Flipped 12\r\n"); |
parthsagar2010 | 15:0356e9454a60 | 361 | } |
parthsagar2010 | 15:0356e9454a60 | 362 | else |
parthsagar2010 | 15:0356e9454a60 | 363 | { |
parthsagar2010 | 15:0356e9454a60 | 364 | sampleps = 13; |
parthsagar2010 | 15:0356e9454a60 | 365 | // printf("Flipped 13\r\n"); |
parthsagar2010 | 15:0356e9454a60 | 366 | } |
parthsagar2010 | 15:0356e9454a60 | 367 | |
parthsagar2010 | 15:0356e9454a60 | 368 | for( idx = 0; idx < sampleps; idx++ ) |
parthsagar2010 | 9:24ecf16eab0f | 369 | { |
parthsagar2010 | 9:24ecf16eab0f | 370 | //pc.printf("%6d\r\n", ecgSample[idx]); |
parthsagar2010 | 9:24ecf16eab0f | 371 | ecgSample_1sec[onesec_counter] = ecgSample[idx]; |
parthsagar2010 | 10:54aa50490b15 | 372 | |
parthsagar2010 | 10:54aa50490b15 | 373 | cksm_1 = ecgSample[idx] & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 374 | cksm_2 = (ecgSample[idx] >> 8) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 375 | checksum_ += cksm_1 + cksm_2; |
parthsagar2010 | 9:24ecf16eab0f | 376 | onesec_counter++; |
coreyharris | 6:86ac850c718d | 377 | } |
parthsagar2010 | 15:0356e9454a60 | 378 | flip =!flip; |
parthsagar2010 | 15:0356e9454a60 | 379 | rLed = ! rLed; |
coreyharris | 0:38c49bc37c7c | 380 | } |
parthsagar2010 | 10:54aa50490b15 | 381 | |
coreyharris | 0:38c49bc37c7c | 382 | } |
coreyharris | 0:38c49bc37c7c | 383 | } |
coreyharris | 0:38c49bc37c7c | 384 | } |
kidecha_rahul | 23:288c5d1babd1 | 385 | |
kidecha_rahul | 23:288c5d1babd1 | 386 | |
kidecha_rahul | 23:288c5d1babd1 | 387 | |
kidecha_rahul | 23:288c5d1babd1 | 388 | |
coreyharris | 0:38c49bc37c7c | 389 | void ecg_config(MAX30003& ecgAFE) { |
kidecha_rahul | 23:288c5d1babd1 | 390 | |
coreyharris | 1:86843c27cc81 | 391 | // Reset ECG to clear registers |
coreyharris | 1:86843c27cc81 | 392 | ecgAFE.writeRegister( MAX30003::SW_RST , 0); |
coreyharris | 0:38c49bc37c7c | 393 | |
coreyharris | 1:86843c27cc81 | 394 | // General config register setting |
coreyharris | 1:86843c27cc81 | 395 | MAX30003::GeneralConfiguration_u CNFG_GEN_r; |
coreyharris | 3:420d5efbd967 | 396 | CNFG_GEN_r.bits.en_ecg = 1; // Enable ECG channel |
coreyharris | 3:420d5efbd967 | 397 | CNFG_GEN_r.bits.rbiasn = 1; // Enable resistive bias on negative input |
coreyharris | 3:420d5efbd967 | 398 | CNFG_GEN_r.bits.rbiasp = 1; // Enable resistive bias on positive input |
coreyharris | 3:420d5efbd967 | 399 | CNFG_GEN_r.bits.en_rbias = 1; // Enable resistive bias |
coreyharris | 3:420d5efbd967 | 400 | CNFG_GEN_r.bits.imag = 2; // Current magnitude = 10nA |
coreyharris | 3:420d5efbd967 | 401 | CNFG_GEN_r.bits.en_dcloff = 1; // Enable DC lead-off detection |
parthsagar2010 | 12:10b49ad3c492 | 402 | //CNFG_GEN_r.bits.fmstr = 1; //125 sps FMSTR 1 |
coreyharris | 1:86843c27cc81 | 403 | ecgAFE.writeRegister( MAX30003::CNFG_GEN , CNFG_GEN_r.all); |
coreyharris | 1:86843c27cc81 | 404 | |
coreyharris | 1:86843c27cc81 | 405 | |
coreyharris | 1:86843c27cc81 | 406 | // ECG Config register setting |
coreyharris | 1:86843c27cc81 | 407 | MAX30003::ECGConfiguration_u CNFG_ECG_r; |
coreyharris | 3:420d5efbd967 | 408 | CNFG_ECG_r.bits.dlpf = 1; // Digital LPF cutoff = 40Hz |
coreyharris | 3:420d5efbd967 | 409 | CNFG_ECG_r.bits.dhpf = 1; // Digital HPF cutoff = 0.5Hz |
coreyharris | 3:420d5efbd967 | 410 | CNFG_ECG_r.bits.gain = 3; // ECG gain = 160V/V |
kidecha_rahul | 23:288c5d1babd1 | 411 | CNFG_ECG_r.bits.rate = 2; // Sample rate = 128 sps |
coreyharris | 1:86843c27cc81 | 412 | ecgAFE.writeRegister( MAX30003::CNFG_ECG , CNFG_ECG_r.all); |
coreyharris | 1:86843c27cc81 | 413 | |
coreyharris | 1:86843c27cc81 | 414 | |
coreyharris | 1:86843c27cc81 | 415 | //R-to-R configuration |
coreyharris | 1:86843c27cc81 | 416 | MAX30003::RtoR1Configuration_u CNFG_RTOR_r; |
coreyharris | 3:420d5efbd967 | 417 | CNFG_RTOR_r.bits.en_rtor = 1; // Enable R-to-R detection |
coreyharris | 1:86843c27cc81 | 418 | ecgAFE.writeRegister( MAX30003::CNFG_RTOR1 , CNFG_RTOR_r.all); |
coreyharris | 1:86843c27cc81 | 419 | |
coreyharris | 1:86843c27cc81 | 420 | |
coreyharris | 1:86843c27cc81 | 421 | //Manage interrupts register setting |
coreyharris | 1:86843c27cc81 | 422 | MAX30003::ManageInterrupts_u MNG_INT_r; |
coreyharris | 3:420d5efbd967 | 423 | MNG_INT_r.bits.efit = 0b00011; // Assert EINT w/ 4 unread samples |
coreyharris | 3:420d5efbd967 | 424 | MNG_INT_r.bits.clr_rrint = 0b01; // Clear R-to-R on RTOR reg. read back |
coreyharris | 1:86843c27cc81 | 425 | ecgAFE.writeRegister( MAX30003::MNGR_INT , MNG_INT_r.all); |
coreyharris | 0:38c49bc37c7c | 426 | |
coreyharris | 0:38c49bc37c7c | 427 | |
coreyharris | 1:86843c27cc81 | 428 | //Enable interrupts register setting |
coreyharris | 1:86843c27cc81 | 429 | MAX30003::EnableInterrupts_u EN_INT_r; |
coreyharris | 5:f8d1f651bef5 | 430 | EN_INT_r.all = 0; |
coreyharris | 3:420d5efbd967 | 431 | EN_INT_r.bits.en_eint = 1; // Enable EINT interrupt |
coreyharris | 4:06e258ff0b97 | 432 | EN_INT_r.bits.en_rrint = 0; // Disable R-to-R interrupt |
coreyharris | 3:420d5efbd967 | 433 | EN_INT_r.bits.intb_type = 3; // Open-drain NMOS with internal pullup |
coreyharris | 1:86843c27cc81 | 434 | ecgAFE.writeRegister( MAX30003::EN_INT , EN_INT_r.all); |
coreyharris | 1:86843c27cc81 | 435 | |
coreyharris | 1:86843c27cc81 | 436 | |
coreyharris | 1:86843c27cc81 | 437 | //Dyanmic modes config |
coreyharris | 1:86843c27cc81 | 438 | MAX30003::ManageDynamicModes_u MNG_DYN_r; |
coreyharris | 3:420d5efbd967 | 439 | MNG_DYN_r.bits.fast = 0; // Fast recovery mode disabled |
coreyharris | 1:86843c27cc81 | 440 | ecgAFE.writeRegister( MAX30003::MNGR_DYN , MNG_DYN_r.all); |
coreyharris | 5:f8d1f651bef5 | 441 | |
coreyharris | 5:f8d1f651bef5 | 442 | // MUX Config |
coreyharris | 5:f8d1f651bef5 | 443 | MAX30003::MuxConfiguration_u CNFG_MUX_r; |
coreyharris | 5:f8d1f651bef5 | 444 | CNFG_MUX_r.bits.openn = 0; // Connect ECGN to AFE channel |
coreyharris | 5:f8d1f651bef5 | 445 | CNFG_MUX_r.bits.openp = 0; // Connect ECGP to AFE channel |
coreyharris | 5:f8d1f651bef5 | 446 | ecgAFE.writeRegister( MAX30003::CNFG_EMUX , CNFG_MUX_r.all); |
coreyharris | 1:86843c27cc81 | 447 | |
coreyharris | 1:86843c27cc81 | 448 | return; |
coreyharris | 0:38c49bc37c7c | 449 | } |
kidecha_rahul | 23:288c5d1babd1 | 450 |