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