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