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