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