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