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