final
Dependencies: MAX30003 max32630fthr DS1307
main.cpp@26:c41e561a02de, 2021-12-21 (annotated)
- Committer:
- parthsagar2010
- Date:
- Tue Dec 21 01:17:08 2021 +0000
- Revision:
- 26:c41e561a02de
- Parent:
- 25:06ba30a29754
- Child:
- 27:3b45b47d735d
k
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kidecha_rahul | 23:288c5d1babd1 | 1 | |
parthsagar2010 | 8:6b9359f81cc0 | 2 | #include "MAX30003.h" |
coreyharris | 0:38c49bc37c7c | 3 | #include "mbed.h" |
coreyharris | 0:38c49bc37c7c | 4 | #include "max32630fthr.h" |
kidecha_rahul | 23:288c5d1babd1 | 5 | #include "ds1307.h" |
parthsagar2010 | 11:300afaf2696b | 6 | //#include <BufferedSerial.h> |
kidecha_rahul | 23:288c5d1babd1 | 7 | #include <string> |
parthsagar2010 | 10:54aa50490b15 | 8 | //#include <Serial.h> |
kidecha_rahul | 23:288c5d1babd1 | 9 | |
kidecha_rahul | 18:101042d225ef | 10 | #define TARGET_TX_PIN P3_1 |
kidecha_rahul | 18:101042d225ef | 11 | #define TARGET_RX_PIN P3_0 |
kidecha_rahul | 23:288c5d1babd1 | 12 | |
kidecha_rahul | 25:06ba30a29754 | 13 | //#define SDA P3_4 |
kidecha_rahul | 25:06ba30a29754 | 14 | //#define SCL P3_5 |
kidecha_rahul | 24:98332ec40aff | 15 | |
kidecha_rahul | 24:98332ec40aff | 16 | |
kidecha_rahul | 25:06ba30a29754 | 17 | //#define I2C_SDA P5_7 |
kidecha_rahul | 25:06ba30a29754 | 18 | //#define I2C_SCL P6_0 |
kidecha_rahul | 24:98332ec40aff | 19 | |
kidecha_rahul | 25:06ba30a29754 | 20 | //Ds3231 rtc(SDA, SCL); |
kidecha_rahul | 24:98332ec40aff | 21 | |
kidecha_rahul | 22:07eec382c267 | 22 | //Timer timer_fast; |
kidecha_rahul | 23:288c5d1babd1 | 23 | Timer t; |
coreyharris | 0:38c49bc37c7c | 24 | MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3); |
kidecha_rahul | 23:288c5d1babd1 | 25 | |
kidecha_rahul | 23:288c5d1babd1 | 26 | void task_fast(void); |
kidecha_rahul | 18:101042d225ef | 27 | //DigitalOut ledA(LED2); |
kidecha_rahul | 23:288c5d1babd1 | 28 | |
kidecha_rahul | 25:06ba30a29754 | 29 | DigitalOut S0(P0_4); |
kidecha_rahul | 25:06ba30a29754 | 30 | DigitalOut S1(P0_5); |
kidecha_rahul | 23:288c5d1babd1 | 31 | //DigitalOut S2(P7_2); |
kidecha_rahul | 23:288c5d1babd1 | 32 | //DigitalOut S3(P7_2); |
kidecha_rahul | 19:7a084dc2f09d | 33 | |
kidecha_rahul | 25:06ba30a29754 | 34 | // Input pins |
kidecha_rahul | 25:06ba30a29754 | 35 | DigitalIn A(P1_3); //P6_3 |
kidecha_rahul | 25:06ba30a29754 | 36 | DigitalIn B(P1_5); //P1_5 |
kidecha_rahul | 25:06ba30a29754 | 37 | DigitalIn C(P1_4); //P1_4 |
kidecha_rahul | 25:06ba30a29754 | 38 | //DigitalIn D(6_3); //P6_3 for hardware RTC |
kidecha_rahul | 25:06ba30a29754 | 39 | |
kidecha_rahul | 25:06ba30a29754 | 40 | const float battery_max = 4.20; //maximum voltage of battery |
kidecha_rahul | 25:06ba30a29754 | 41 | const float battery_min = 3.0; //minimum voltage of battery before shutdown |
kidecha_rahul | 25:06ba30a29754 | 42 | uint8_t output = 0; |
kidecha_rahul | 25:06ba30a29754 | 43 | |
kidecha_rahul | 25:06ba30a29754 | 44 | //AnalogIn ain_4(AIN_4); |
kidecha_rahul | 25:06ba30a29754 | 45 | AnalogIn ain_5(AIN_5); |
kidecha_rahul | 21:193a10530741 | 46 | |
kidecha_rahul | 23:288c5d1babd1 | 47 | |
coreyharris | 0:38c49bc37c7c | 48 | void ecg_config(MAX30003 &ecgAFE); |
parthsagar2010 | 11:300afaf2696b | 49 | //BufferedSerial pc(P3_1,P3_0); // Use USB debug probe for serial link static Unbuffered |
parthsagar2010 | 15:0356e9454a60 | 50 | static BufferedSerial pc(TARGET_TX_PIN, TARGET_RX_PIN, 230400); // 230400 works well |
kidecha_rahul | 23:288c5d1babd1 | 51 | |
parthsagar2010 | 10:54aa50490b15 | 52 | //Serial uart_1(USBTX, USBRX); // Use USB debug probe for serial link static Unbuffered |
parthsagar2010 | 10:54aa50490b15 | 53 | // |
parthsagar2010 | 10:54aa50490b15 | 54 | //Serial pc(P3_1,P3_0); |
coreyharris | 0:38c49bc37c7c | 55 | volatile bool ecgFIFOIntFlag = 0; |
kidecha_rahul | 23:288c5d1babd1 | 56 | volatile bool timerflag = 0; |
kidecha_rahul | 23:288c5d1babd1 | 57 | |
parthsagar2010 | 11:300afaf2696b | 58 | FileHandle *mbed::mbed_override_console(int fd) |
parthsagar2010 | 11:300afaf2696b | 59 | { |
parthsagar2010 | 11:300afaf2696b | 60 | return &pc; |
parthsagar2010 | 11:300afaf2696b | 61 | } |
kidecha_rahul | 23:288c5d1babd1 | 62 | |
parthsagar2010 | 8:6b9359f81cc0 | 63 | void ecgFIFO_callback_1() { // Triggered when the ECG FIFO is about to be full |
coreyharris | 0:38c49bc37c7c | 64 | |
coreyharris | 0:38c49bc37c7c | 65 | ecgFIFOIntFlag = 1; |
coreyharris | 0:38c49bc37c7c | 66 | } |
kidecha_rahul | 23:288c5d1babd1 | 67 | |
kidecha_rahul | 23:288c5d1babd1 | 68 | time_t asUnixTime(int year, int mon, int mday, int hour, int min, int sec) { |
kidecha_rahul | 23:288c5d1babd1 | 69 | struct tm t; |
kidecha_rahul | 23:288c5d1babd1 | 70 | t.tm_year = year - 1900; |
kidecha_rahul | 23:288c5d1babd1 | 71 | t.tm_mon = mon - 1; // convert to 0 based month |
kidecha_rahul | 23:288c5d1babd1 | 72 | t.tm_mday = mday; |
kidecha_rahul | 23:288c5d1babd1 | 73 | t.tm_hour = hour; |
kidecha_rahul | 23:288c5d1babd1 | 74 | t.tm_min = min; |
kidecha_rahul | 23:288c5d1babd1 | 75 | t.tm_sec = sec; |
kidecha_rahul | 23:288c5d1babd1 | 76 | t.tm_isdst = -1; // Is Daylight saving time on? 1 = yes, 0 = no, -1 = unknown |
kidecha_rahul | 23:288c5d1babd1 | 77 | |
kidecha_rahul | 23:288c5d1babd1 | 78 | return mktime(&t); // returns seconds elapsed since January 1, 1970 (begin of the Epoch) |
kidecha_rahul | 23:288c5d1babd1 | 79 | } |
parthsagar2010 | 11:300afaf2696b | 80 | int main(void) |
coreyharris | 3:420d5efbd967 | 81 | { |
kidecha_rahul | 23:288c5d1babd1 | 82 | uint16_t tic=0; |
kidecha_rahul | 23:288c5d1babd1 | 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 | 23:288c5d1babd1 | 91 | // timer_fast.start(); |
kidecha_rahul | 25:06ba30a29754 | 92 | DigitalOut RGBr(P1_2); |
kidecha_rahul | 25:06ba30a29754 | 93 | DigitalOut RGBg(P1_1); |
kidecha_rahul | 25:06ba30a29754 | 94 | DigitalOut RGBb(P1_0); |
kidecha_rahul | 25:06ba30a29754 | 95 | |
kidecha_rahul | 25:06ba30a29754 | 96 | DigitalOut rLed(P0_6); |
kidecha_rahul | 25:06ba30a29754 | 97 | |
kidecha_rahul | 25:06ba30a29754 | 98 | rLed=1; |
kidecha_rahul | 25:06ba30a29754 | 99 | |
kidecha_rahul | 25:06ba30a29754 | 100 | S0=0; |
kidecha_rahul | 25:06ba30a29754 | 101 | S1=0; |
kidecha_rahul | 25:06ba30a29754 | 102 | RGBr=0; |
kidecha_rahul | 25:06ba30a29754 | 103 | RGBg=1; |
kidecha_rahul | 25:06ba30a29754 | 104 | RGBb=1; |
kidecha_rahul | 25:06ba30a29754 | 105 | |
parthsagar2010 | 10:54aa50490b15 | 106 | // pc.baud(9600); |
coreyharris | 3:420d5efbd967 | 107 | |
parthsagar2010 | 11:300afaf2696b | 108 | //pc.set_baud(115200); // Baud rate = 115200 |
parthsagar2010 | 11:300afaf2696b | 109 | //pc.set_format( |
parthsagar2010 | 11:300afaf2696b | 110 | // /* bits */ 8, |
parthsagar2010 | 11:300afaf2696b | 111 | // /* parity */ BufferedSerial::None, |
parthsagar2010 | 11:300afaf2696b | 112 | // /* stop bit */ 1 //1 |
parthsagar2010 | 11:300afaf2696b | 113 | //); |
parthsagar2010 | 10:54aa50490b15 | 114 | //uart_1.baud(115200); |
kidecha_rahul | 23:288c5d1babd1 | 115 | |
kidecha_rahul | 23:288c5d1babd1 | 116 | //--------------------------------- ECG Initialization ---------------------------------------------// |
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 | |
kidecha_rahul | 23:288c5d1babd1 | 130 | //-------------------------------------------------------------------------------------------------// |
kidecha_rahul | 23:288c5d1babd1 | 131 | |
coreyharris | 4:06e258ff0b97 | 132 | uint32_t ecgFIFO, readECGSamples, idx, ETAG[32], status; |
coreyharris | 1:86843c27cc81 | 133 | int16_t ecgSample[32]; |
parthsagar2010 | 9:24ecf16eab0f | 134 | //bool timerflag = false; |
kidecha_rahul | 23:288c5d1babd1 | 135 | int16_t ecgSample_1sec[256]; |
kidecha_rahul | 25:06ba30a29754 | 136 | // uint8_t ecg_1 = 0; |
kidecha_rahul | 25:06ba30a29754 | 137 | // uint8_t ecg_2 = 0; |
parthsagar2010 | 10:54aa50490b15 | 138 | uint16_t onesec_counter = 0; |
parthsagar2010 | 10:54aa50490b15 | 139 | uint16_t onesec_counter_temp = 0; |
parthsagar2010 | 10:54aa50490b15 | 140 | |
kidecha_rahul | 22:07eec382c267 | 141 | // int16_t sample = 300; |
kidecha_rahul | 23:288c5d1babd1 | 142 | // uint8_t final[10]; |
parthsagar2010 | 12:10b49ad3c492 | 143 | |
parthsagar2010 | 10:54aa50490b15 | 144 | uint16_t checksum_ = 0; |
parthsagar2010 | 10:54aa50490b15 | 145 | uint16_t mod_checksum = 0; |
parthsagar2010 | 10:54aa50490b15 | 146 | |
parthsagar2010 | 10:54aa50490b15 | 147 | uint8_t p_1 = 0; |
parthsagar2010 | 10:54aa50490b15 | 148 | uint8_t p_2 = 0; |
parthsagar2010 | 10:54aa50490b15 | 149 | uint8_t p_3 = 0; |
parthsagar2010 | 10:54aa50490b15 | 150 | uint8_t p_4 = 0; |
kidecha_rahul | 18:101042d225ef | 151 | // uint8_t channel_num= 1; |
kidecha_rahul | 20:2e999e6568c1 | 152 | uint8_t channel_num[1]= {0}; |
parthsagar2010 | 10:54aa50490b15 | 153 | uint8_t data_len_1 = 0; |
parthsagar2010 | 10:54aa50490b15 | 154 | uint8_t data_len_2 = 0; |
parthsagar2010 | 12:10b49ad3c492 | 155 | uint32_t packet_1 = 0; |
parthsagar2010 | 10:54aa50490b15 | 156 | uint8_t cksm_1 = 0; |
parthsagar2010 | 10:54aa50490b15 | 157 | uint8_t cksm_2 = 0; |
parthsagar2010 | 26:c41e561a02de | 158 | //uint8_t header_device_id[3] = {0,0,210}; //OLD device ID |
parthsagar2010 | 26:c41e561a02de | 159 | uint8_t header_device_id[3] = {0,0,12}; // |
parthsagar2010 | 10:54aa50490b15 | 160 | uint8_t header_packet_type[2] = {0,2}; |
kidecha_rahul | 25:06ba30a29754 | 161 | uint8_t battery_level[1] ={0}; |
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 | 23:288c5d1babd1 | 166 | int sampleps = 0; |
parthsagar2010 | 15:0356e9454a60 | 167 | |
kidecha_rahul | 25:06ba30a29754 | 168 | char buf[30]; |
parthsagar2010 | 15:0356e9454a60 | 169 | // pc.write("Welcome",8*sizeof(char)); |
parthsagar2010 | 15:0356e9454a60 | 170 | // printf("In the main loop"); //printf("In the main loop"); |
kidecha_rahul | 23:288c5d1babd1 | 171 | bool flag_first = false; |
kidecha_rahul | 23:288c5d1babd1 | 172 | bool timestamp_reader = false; |
kidecha_rahul | 17:f0008ebb1040 | 173 | |
kidecha_rahul | 17:f0008ebb1040 | 174 | |
kidecha_rahul | 23:288c5d1babd1 | 175 | //---------------------------------software RTC Start---------------------------------------------// |
kidecha_rahul | 23:288c5d1babd1 | 176 | do |
kidecha_rahul | 23:288c5d1babd1 | 177 | { |
kidecha_rahul | 23:288c5d1babd1 | 178 | if (pc.readable()) |
kidecha_rahul | 23:288c5d1babd1 | 179 | { |
kidecha_rahul | 23:288c5d1babd1 | 180 | scanf("%s",buf); |
kidecha_rahul | 23:288c5d1babd1 | 181 | //printf("The entered string is %s : \n ",buf);*/ |
kidecha_rahul | 23:288c5d1babd1 | 182 | // buf[20] = 1621297647; |
kidecha_rahul | 23:288c5d1babd1 | 183 | packet_1 = atoi(buf); |
kidecha_rahul | 23:288c5d1babd1 | 184 | set_time(packet_1); |
parthsagar2010 | 12:10b49ad3c492 | 185 | |
kidecha_rahul | 23:288c5d1babd1 | 186 | if ((packet_1 % 60) == 0) |
kidecha_rahul | 23:288c5d1babd1 | 187 | { |
kidecha_rahul | 23:288c5d1babd1 | 188 | for (int u = 0;u<3;u++) |
kidecha_rahul | 23:288c5d1babd1 | 189 | { |
kidecha_rahul | 23:288c5d1babd1 | 190 | if (pc.readable()) |
kidecha_rahul | 23:288c5d1babd1 | 191 | { |
kidecha_rahul | 23:288c5d1babd1 | 192 | scanf("%s",buf); |
kidecha_rahul | 23:288c5d1babd1 | 193 | printf("Entered the minute string %s : \n ",buf); |
kidecha_rahul | 23:288c5d1babd1 | 194 | |
kidecha_rahul | 23:288c5d1babd1 | 195 | packet_1 = atoi(buf); |
kidecha_rahul | 23:288c5d1babd1 | 196 | set_time(packet_1); |
kidecha_rahul | 23:288c5d1babd1 | 197 | } |
kidecha_rahul | 23:288c5d1babd1 | 198 | } |
kidecha_rahul | 23:288c5d1babd1 | 199 | timestamp_reader = true; |
kidecha_rahul | 23:288c5d1babd1 | 200 | } |
kidecha_rahul | 23:288c5d1babd1 | 201 | else |
kidecha_rahul | 23:288c5d1babd1 | 202 | { |
kidecha_rahul | 23:288c5d1babd1 | 203 | //printf("The timestamp is not divisible by 60 \n"); |
kidecha_rahul | 23:288c5d1babd1 | 204 | } |
kidecha_rahul | 23:288c5d1babd1 | 205 | flag_first = true; |
kidecha_rahul | 23:288c5d1babd1 | 206 | |
kidecha_rahul | 23:288c5d1babd1 | 207 | } |
kidecha_rahul | 23:288c5d1babd1 | 208 | |
kidecha_rahul | 23:288c5d1babd1 | 209 | } while (timestamp_reader==false); // take the third timestamp |
kidecha_rahul | 23:288c5d1babd1 | 210 | |
kidecha_rahul | 23:288c5d1babd1 | 211 | //---------------------------------software RTC End---------------------------------------------// |
kidecha_rahul | 25:06ba30a29754 | 212 | rLed=0; |
kidecha_rahul | 17:f0008ebb1040 | 213 | |
parthsagar2010 | 16:520034b124b6 | 214 | while(1) |
parthsagar2010 | 9:24ecf16eab0f | 215 | { |
kidecha_rahul | 23:288c5d1babd1 | 216 | |
kidecha_rahul | 25:06ba30a29754 | 217 | unsigned short raw; |
kidecha_rahul | 25:06ba30a29754 | 218 | raw = ain_5.read_u16(); |
kidecha_rahul | 25:06ba30a29754 | 219 | float vin; |
kidecha_rahul | 25:06ba30a29754 | 220 | vin = ((raw*5*1.2)/ 65535.0); |
kidecha_rahul | 25:06ba30a29754 | 221 | output = ((vin - battery_min) / (battery_max - battery_min)) * 100; |
kidecha_rahul | 25:06ba30a29754 | 222 | |
kidecha_rahul | 25:06ba30a29754 | 223 | if (output < 100) |
kidecha_rahul | 25:06ba30a29754 | 224 | battery_level[0] = {output}; |
kidecha_rahul | 25:06ba30a29754 | 225 | else |
kidecha_rahul | 25:06ba30a29754 | 226 | battery_level[0] ={99}; |
kidecha_rahul | 25:06ba30a29754 | 227 | |
kidecha_rahul | 25:06ba30a29754 | 228 | if ((onesec_counter>=125)) |
kidecha_rahul | 23:288c5d1babd1 | 229 | { |
kidecha_rahul | 23:288c5d1babd1 | 230 | tic++; |
kidecha_rahul | 23:288c5d1babd1 | 231 | |
kidecha_rahul | 18:101042d225ef | 232 | pc.write((uint8_t *)header_device_id, sizeof(header_device_id)); // device ID |
parthsagar2010 | 10:54aa50490b15 | 233 | |
kidecha_rahul | 18:101042d225ef | 234 | pc.write((uint8_t *)header_packet_type, sizeof(header_packet_type)); // packet type |
kidecha_rahul | 18:101042d225ef | 235 | |
kidecha_rahul | 23:288c5d1babd1 | 236 | time_t seconds = time(NULL); |
kidecha_rahul | 23:288c5d1babd1 | 237 | packet_1 = seconds; |
kidecha_rahul | 23:288c5d1babd1 | 238 | |
kidecha_rahul | 17:f0008ebb1040 | 239 | //time_t seconds = time(NULL); // if remove it the timestamp will be static |
kidecha_rahul | 23:288c5d1babd1 | 240 | //packet_1 ++; |
kidecha_rahul | 23:288c5d1babd1 | 241 | // printf("Time as seconds since January 1, 1970 = %d\n", packet_1); |
parthsagar2010 | 10:54aa50490b15 | 242 | p_1 = packet_1 & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 243 | p_2 = (packet_1 >> 8) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 244 | p_3 = (packet_1 >> 16) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 245 | p_4 = (packet_1 >> 24) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 246 | // checksum_ = checksum_ + (packet_1 & 0xffff) + ((packet_1 >> 16) & 0xffff) ; |
parthsagar2010 | 10:54aa50490b15 | 247 | checksum_ = checksum_ + p_1 + p_2 + p_3 + p_4 ; |
parthsagar2010 | 10:54aa50490b15 | 248 | uint8_t header_packet_id[4] = {p_4,p_3,p_2,p_1}; |
kidecha_rahul | 18:101042d225ef | 249 | pc.write((uint8_t *)header_packet_id, sizeof(header_packet_id)); // packet ID |
kidecha_rahul | 18:101042d225ef | 250 | |
kidecha_rahul | 18:101042d225ef | 251 | |
parthsagar2010 | 16:520034b124b6 | 252 | pc.write((uint8_t *)channel_num, sizeof(channel_num)); |
kidecha_rahul | 18:101042d225ef | 253 | |
kidecha_rahul | 25:06ba30a29754 | 254 | pc.write((uint8_t *)battery_level, sizeof(battery_level)); |
kidecha_rahul | 25:06ba30a29754 | 255 | |
parthsagar2010 | 10:54aa50490b15 | 256 | onesec_counter_temp = onesec_counter * 2; |
parthsagar2010 | 10:54aa50490b15 | 257 | data_len_1 = onesec_counter_temp & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 258 | data_len_2 = (onesec_counter_temp >> 8) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 259 | checksum_ = checksum_ + data_len_1 + data_len_2; |
parthsagar2010 | 10:54aa50490b15 | 260 | //pc.write((uint32_t *)packet_1, sizeof(packet_1)); |
parthsagar2010 | 10:54aa50490b15 | 261 | uint8_t header_ecg_datalen[2] = {data_len_2,data_len_1}; |
parthsagar2010 | 10:54aa50490b15 | 262 | pc.write((uint8_t *)header_ecg_datalen, sizeof(header_ecg_datalen)); |
kidecha_rahul | 18:101042d225ef | 263 | |
parthsagar2010 | 10:54aa50490b15 | 264 | mod_checksum = checksum_ % 65536 ; |
parthsagar2010 | 10:54aa50490b15 | 265 | cksm_1 = mod_checksum & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 266 | cksm_2 = (mod_checksum >> 8) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 267 | uint8_t header_ecg_checksum[2] = {cksm_2,cksm_1}; |
parthsagar2010 | 10:54aa50490b15 | 268 | pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum)); |
parthsagar2010 | 10:54aa50490b15 | 269 | |
parthsagar2010 | 10:54aa50490b15 | 270 | pc.write((int16_t *)ecgSample_1sec,onesec_counter * sizeof(int16_t)); |
parthsagar2010 | 15:0356e9454a60 | 271 | // printf("Samples per second %d \n", (onesec_counter)); |
parthsagar2010 | 10:54aa50490b15 | 272 | onesec_counter = 0; |
parthsagar2010 | 10:54aa50490b15 | 273 | // memset(ecgSample_1sec, 0, sizeof(ecgSample_1sec)); |
parthsagar2010 | 10:54aa50490b15 | 274 | |
parthsagar2010 | 10:54aa50490b15 | 275 | pc.write((uint8_t *)ending, sizeof(ending)); |
parthsagar2010 | 10:54aa50490b15 | 276 | checksum_ = 0; |
parthsagar2010 | 15:0356e9454a60 | 277 | //t.stop(); |
parthsagar2010 | 11:300afaf2696b | 278 | // auto us = t.elapsed_time().count(); |
parthsagar2010 | 15:0356e9454a60 | 279 | // float time_taken = us/1000000; |
parthsagar2010 | 15:0356e9454a60 | 280 | // printf("Timer time: %lu ms \n", (time_taken*100)); |
parthsagar2010 | 15:0356e9454a60 | 281 | // t.reset(); |
parthsagar2010 | 15:0356e9454a60 | 282 | // timer_fast.reset(); |
kidecha_rahul | 19:7a084dc2f09d | 283 | |
kidecha_rahul | 23:288c5d1babd1 | 284 | |
parthsagar2010 | 26:c41e561a02de | 285 | if (tic==60) //C1 |
kidecha_rahul | 19:7a084dc2f09d | 286 | { |
kidecha_rahul | 19:7a084dc2f09d | 287 | S0=1; |
kidecha_rahul | 19:7a084dc2f09d | 288 | S1=0; |
kidecha_rahul | 25:06ba30a29754 | 289 | RGBr=1; |
kidecha_rahul | 25:06ba30a29754 | 290 | RGBg=0; |
kidecha_rahul | 25:06ba30a29754 | 291 | RGBb=1; |
kidecha_rahul | 20:2e999e6568c1 | 292 | channel_num[0]= {1}; |
kidecha_rahul | 19:7a084dc2f09d | 293 | } |
parthsagar2010 | 26:c41e561a02de | 294 | if (tic==120) //C2 |
kidecha_rahul | 19:7a084dc2f09d | 295 | { |
kidecha_rahul | 19:7a084dc2f09d | 296 | S0=0; |
kidecha_rahul | 19:7a084dc2f09d | 297 | S1=1; |
kidecha_rahul | 25:06ba30a29754 | 298 | RGBr=1; |
kidecha_rahul | 25:06ba30a29754 | 299 | RGBg=1; |
kidecha_rahul | 25:06ba30a29754 | 300 | RGBb=0; |
kidecha_rahul | 20:2e999e6568c1 | 301 | channel_num[0]= {2}; |
kidecha_rahul | 19:7a084dc2f09d | 302 | } |
kidecha_rahul | 19:7a084dc2f09d | 303 | |
parthsagar2010 | 26:c41e561a02de | 304 | if (tic==180) // C0 |
kidecha_rahul | 19:7a084dc2f09d | 305 | { |
kidecha_rahul | 19:7a084dc2f09d | 306 | S0=0; |
kidecha_rahul | 19:7a084dc2f09d | 307 | S1=0; |
kidecha_rahul | 25:06ba30a29754 | 308 | RGBr=0; |
kidecha_rahul | 25:06ba30a29754 | 309 | RGBg=1; |
kidecha_rahul | 25:06ba30a29754 | 310 | RGBb=1; |
kidecha_rahul | 20:2e999e6568c1 | 311 | channel_num[0]= {0}; |
kidecha_rahul | 23:288c5d1babd1 | 312 | tic=0;} |
kidecha_rahul | 23:288c5d1babd1 | 313 | |
kidecha_rahul | 21:193a10530741 | 314 | |
kidecha_rahul | 23:288c5d1babd1 | 315 | // for channel selection |
kidecha_rahul | 23:288c5d1babd1 | 316 | |
kidecha_rahul | 25:06ba30a29754 | 317 | if (A==0) // |
parthsagar2010 | 26:c41e561a02de | 318 | {tic=185; |
kidecha_rahul | 22:07eec382c267 | 319 | S0=0; |
kidecha_rahul | 22:07eec382c267 | 320 | S1=0; |
kidecha_rahul | 25:06ba30a29754 | 321 | RGBr=0; |
kidecha_rahul | 25:06ba30a29754 | 322 | RGBg=1; |
kidecha_rahul | 25:06ba30a29754 | 323 | RGBb=1; |
kidecha_rahul | 25:06ba30a29754 | 324 | |
kidecha_rahul | 23:288c5d1babd1 | 325 | // S2=0; |
kidecha_rahul | 23:288c5d1babd1 | 326 | // S3=0; |
kidecha_rahul | 22:07eec382c267 | 327 | channel_num[0]= {0};} |
kidecha_rahul | 22:07eec382c267 | 328 | |
kidecha_rahul | 23:288c5d1babd1 | 329 | if (B==0) // |
parthsagar2010 | 26:c41e561a02de | 330 | {tic=190; |
kidecha_rahul | 21:193a10530741 | 331 | S0=1; |
kidecha_rahul | 21:193a10530741 | 332 | S1=0; |
kidecha_rahul | 25:06ba30a29754 | 333 | RGBr=1; |
kidecha_rahul | 25:06ba30a29754 | 334 | RGBg=0; |
kidecha_rahul | 25:06ba30a29754 | 335 | RGBb=1; |
kidecha_rahul | 25:06ba30a29754 | 336 | |
kidecha_rahul | 23:288c5d1babd1 | 337 | // S2=0; |
kidecha_rahul | 23:288c5d1babd1 | 338 | // S3=0; |
kidecha_rahul | 22:07eec382c267 | 339 | channel_num[0]= {1};} |
kidecha_rahul | 22:07eec382c267 | 340 | |
kidecha_rahul | 23:288c5d1babd1 | 341 | if (C==0) // |
parthsagar2010 | 26:c41e561a02de | 342 | {tic=200; |
kidecha_rahul | 21:193a10530741 | 343 | S0=0; |
kidecha_rahul | 21:193a10530741 | 344 | S1=1; |
kidecha_rahul | 25:06ba30a29754 | 345 | RGBr=1; |
kidecha_rahul | 25:06ba30a29754 | 346 | RGBg=1; |
kidecha_rahul | 25:06ba30a29754 | 347 | RGBb=0; |
kidecha_rahul | 25:06ba30a29754 | 348 | |
kidecha_rahul | 23:288c5d1babd1 | 349 | // S2=0; |
kidecha_rahul | 23:288c5d1babd1 | 350 | // S3=0; |
kidecha_rahul | 25:06ba30a29754 | 351 | channel_num[0]= {2};} |
kidecha_rahul | 23:288c5d1babd1 | 352 | |
kidecha_rahul | 19:7a084dc2f09d | 353 | |
parthsagar2010 | 9:24ecf16eab0f | 354 | } |
coreyharris | 4:06e258ff0b97 | 355 | // Read back ECG samples from the FIFO |
kidecha_rahul | 23:288c5d1babd1 | 356 | else if((ecgFIFOIntFlag==1))// && (timerflag == 0)) |
parthsagar2010 | 9:24ecf16eab0f | 357 | { |
coreyharris | 2:812d40f1853d | 358 | |
coreyharris | 5:f8d1f651bef5 | 359 | ecgFIFOIntFlag = 0; |
coreyharris | 4:06e258ff0b97 | 360 | status = ecgAFE.readRegister( MAX30003::STATUS ); // Read the STATUS register |
coreyharris | 2:812d40f1853d | 361 | |
coreyharris | 3:420d5efbd967 | 362 | // Check if EINT interrupt asserted |
parthsagar2010 | 9:24ecf16eab0f | 363 | if ( ( status & EINT_STATUS_MASK ) == EINT_STATUS_MASK ) |
parthsagar2010 | 9:24ecf16eab0f | 364 | { |
coreyharris | 3:420d5efbd967 | 365 | |
coreyharris | 4:06e258ff0b97 | 366 | readECGSamples = 0; // Reset sample counter |
coreyharris | 3:420d5efbd967 | 367 | |
coreyharris | 2:812d40f1853d | 368 | do { |
coreyharris | 6:86ac850c718d | 369 | ecgFIFO = ecgAFE.readRegister( MAX30003::ECG_FIFO ); // Read FIFO |
coreyharris | 4:06e258ff0b97 | 370 | ecgSample[readECGSamples] = ecgFIFO >> 8; // Isolate voltage data |
parthsagar2010 | 10:54aa50490b15 | 371 | ecgSample[readECGSamples] = ((ecgSample[readECGSamples]<<8)&0xFF00)|((ecgSample[readECGSamples]>>8)&0x00FF); |
coreyharris | 6:86ac850c718d | 372 | ETAG[readECGSamples] = ( ecgFIFO >> 3 ) & ETAG_BITS_MASK; // Isolate ETAG |
coreyharris | 4:06e258ff0b97 | 373 | readECGSamples++; // Increment sample counter |
coreyharris | 3:420d5efbd967 | 374 | |
coreyharris | 3:420d5efbd967 | 375 | // Check that sample is not last sample in FIFO |
coreyharris | 6:86ac850c718d | 376 | } while ( ETAG[readECGSamples-1] == FIFO_VALID_SAMPLE_MASK || |
coreyharris | 6:86ac850c718d | 377 | ETAG[readECGSamples-1] == FIFO_FAST_SAMPLE_MASK ); |
coreyharris | 2:812d40f1853d | 378 | |
coreyharris | 3:420d5efbd967 | 379 | // Check if FIFO has overflowed |
parthsagar2010 | 9:24ecf16eab0f | 380 | if( ETAG[readECGSamples - 1] == FIFO_OVF_MASK ) |
parthsagar2010 | 9:24ecf16eab0f | 381 | { |
coreyharris | 3:420d5efbd967 | 382 | ecgAFE.writeRegister( MAX30003::FIFO_RST , 0); // Reset FIFO |
johnGreeneMaxim | 7:cf0855a0450a | 383 | rLed = 1;//notifies the user that an over flow occured |
coreyharris | 3:420d5efbd967 | 384 | } |
parthsagar2010 | 10:54aa50490b15 | 385 | //uint8_t header_ecg_checksum[2] = {'%','%'}; |
parthsagar2010 | 10:54aa50490b15 | 386 | //pc.write((uint8_t *)header_ecg_checksum, sizeof(header_ecg_checksum)); |
coreyharris | 3:420d5efbd967 | 387 | |
parthsagar2010 | 10:54aa50490b15 | 388 | //pc.write((int16_t *)ecgSample,readECGSamples * sizeof(int16_t )); |
parthsagar2010 | 10:54aa50490b15 | 389 | //memcpy(ecgSample_1sec , ecgSample, sizeof(ecgSample)); |
parthsagar2010 | 10:54aa50490b15 | 390 | |
parthsagar2010 | 10:54aa50490b15 | 391 | //memcpy(ecgSample_1sec + (onesec_counter * sizeof(int16_t)), ecgSample, sizeof(ecgSample)); |
parthsagar2010 | 15:0356e9454a60 | 392 | |
parthsagar2010 | 15:0356e9454a60 | 393 | if (flip) |
parthsagar2010 | 15:0356e9454a60 | 394 | { |
parthsagar2010 | 15:0356e9454a60 | 395 | sampleps = 12; |
parthsagar2010 | 15:0356e9454a60 | 396 | //printf("Flipped 12\r\n"); |
parthsagar2010 | 15:0356e9454a60 | 397 | } |
parthsagar2010 | 15:0356e9454a60 | 398 | else |
parthsagar2010 | 15:0356e9454a60 | 399 | { |
parthsagar2010 | 15:0356e9454a60 | 400 | sampleps = 13; |
parthsagar2010 | 15:0356e9454a60 | 401 | // printf("Flipped 13\r\n"); |
parthsagar2010 | 15:0356e9454a60 | 402 | } |
parthsagar2010 | 15:0356e9454a60 | 403 | |
parthsagar2010 | 15:0356e9454a60 | 404 | for( idx = 0; idx < sampleps; idx++ ) |
parthsagar2010 | 9:24ecf16eab0f | 405 | { |
parthsagar2010 | 9:24ecf16eab0f | 406 | //pc.printf("%6d\r\n", ecgSample[idx]); |
parthsagar2010 | 9:24ecf16eab0f | 407 | ecgSample_1sec[onesec_counter] = ecgSample[idx]; |
parthsagar2010 | 10:54aa50490b15 | 408 | |
parthsagar2010 | 10:54aa50490b15 | 409 | cksm_1 = ecgSample[idx] & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 410 | cksm_2 = (ecgSample[idx] >> 8) & 0xff; |
parthsagar2010 | 10:54aa50490b15 | 411 | checksum_ += cksm_1 + cksm_2; |
parthsagar2010 | 9:24ecf16eab0f | 412 | onesec_counter++; |
coreyharris | 6:86ac850c718d | 413 | } |
parthsagar2010 | 15:0356e9454a60 | 414 | flip =!flip; |
parthsagar2010 | 15:0356e9454a60 | 415 | rLed = ! rLed; |
coreyharris | 0:38c49bc37c7c | 416 | } |
parthsagar2010 | 10:54aa50490b15 | 417 | |
coreyharris | 0:38c49bc37c7c | 418 | } |
coreyharris | 0:38c49bc37c7c | 419 | } |
coreyharris | 0:38c49bc37c7c | 420 | } |
kidecha_rahul | 23:288c5d1babd1 | 421 | |
kidecha_rahul | 23:288c5d1babd1 | 422 | |
kidecha_rahul | 23:288c5d1babd1 | 423 | |
kidecha_rahul | 23:288c5d1babd1 | 424 | |
coreyharris | 0:38c49bc37c7c | 425 | void ecg_config(MAX30003& ecgAFE) { |
kidecha_rahul | 23:288c5d1babd1 | 426 | |
coreyharris | 1:86843c27cc81 | 427 | // Reset ECG to clear registers |
coreyharris | 1:86843c27cc81 | 428 | ecgAFE.writeRegister( MAX30003::SW_RST , 0); |
coreyharris | 0:38c49bc37c7c | 429 | |
coreyharris | 1:86843c27cc81 | 430 | // General config register setting |
coreyharris | 1:86843c27cc81 | 431 | MAX30003::GeneralConfiguration_u CNFG_GEN_r; |
coreyharris | 3:420d5efbd967 | 432 | CNFG_GEN_r.bits.en_ecg = 1; // Enable ECG channel |
coreyharris | 3:420d5efbd967 | 433 | CNFG_GEN_r.bits.rbiasn = 1; // Enable resistive bias on negative input |
coreyharris | 3:420d5efbd967 | 434 | CNFG_GEN_r.bits.rbiasp = 1; // Enable resistive bias on positive input |
coreyharris | 3:420d5efbd967 | 435 | CNFG_GEN_r.bits.en_rbias = 1; // Enable resistive bias |
coreyharris | 3:420d5efbd967 | 436 | CNFG_GEN_r.bits.imag = 2; // Current magnitude = 10nA |
coreyharris | 3:420d5efbd967 | 437 | CNFG_GEN_r.bits.en_dcloff = 1; // Enable DC lead-off detection |
parthsagar2010 | 12:10b49ad3c492 | 438 | //CNFG_GEN_r.bits.fmstr = 1; //125 sps FMSTR 1 |
coreyharris | 1:86843c27cc81 | 439 | ecgAFE.writeRegister( MAX30003::CNFG_GEN , CNFG_GEN_r.all); |
coreyharris | 1:86843c27cc81 | 440 | |
coreyharris | 1:86843c27cc81 | 441 | |
coreyharris | 1:86843c27cc81 | 442 | // ECG Config register setting |
coreyharris | 1:86843c27cc81 | 443 | MAX30003::ECGConfiguration_u CNFG_ECG_r; |
coreyharris | 3:420d5efbd967 | 444 | CNFG_ECG_r.bits.dlpf = 1; // Digital LPF cutoff = 40Hz |
coreyharris | 3:420d5efbd967 | 445 | CNFG_ECG_r.bits.dhpf = 1; // Digital HPF cutoff = 0.5Hz |
coreyharris | 3:420d5efbd967 | 446 | CNFG_ECG_r.bits.gain = 3; // ECG gain = 160V/V |
kidecha_rahul | 23:288c5d1babd1 | 447 | CNFG_ECG_r.bits.rate = 2; // Sample rate = 128 sps |
coreyharris | 1:86843c27cc81 | 448 | ecgAFE.writeRegister( MAX30003::CNFG_ECG , CNFG_ECG_r.all); |
coreyharris | 1:86843c27cc81 | 449 | |
coreyharris | 1:86843c27cc81 | 450 | |
coreyharris | 1:86843c27cc81 | 451 | //R-to-R configuration |
coreyharris | 1:86843c27cc81 | 452 | MAX30003::RtoR1Configuration_u CNFG_RTOR_r; |
coreyharris | 3:420d5efbd967 | 453 | CNFG_RTOR_r.bits.en_rtor = 1; // Enable R-to-R detection |
coreyharris | 1:86843c27cc81 | 454 | ecgAFE.writeRegister( MAX30003::CNFG_RTOR1 , CNFG_RTOR_r.all); |
coreyharris | 1:86843c27cc81 | 455 | |
coreyharris | 1:86843c27cc81 | 456 | |
coreyharris | 1:86843c27cc81 | 457 | //Manage interrupts register setting |
coreyharris | 1:86843c27cc81 | 458 | MAX30003::ManageInterrupts_u MNG_INT_r; |
coreyharris | 3:420d5efbd967 | 459 | MNG_INT_r.bits.efit = 0b00011; // Assert EINT w/ 4 unread samples |
coreyharris | 3:420d5efbd967 | 460 | MNG_INT_r.bits.clr_rrint = 0b01; // Clear R-to-R on RTOR reg. read back |
coreyharris | 1:86843c27cc81 | 461 | ecgAFE.writeRegister( MAX30003::MNGR_INT , MNG_INT_r.all); |
coreyharris | 0:38c49bc37c7c | 462 | |
coreyharris | 0:38c49bc37c7c | 463 | |
coreyharris | 1:86843c27cc81 | 464 | //Enable interrupts register setting |
coreyharris | 1:86843c27cc81 | 465 | MAX30003::EnableInterrupts_u EN_INT_r; |
coreyharris | 5:f8d1f651bef5 | 466 | EN_INT_r.all = 0; |
coreyharris | 3:420d5efbd967 | 467 | EN_INT_r.bits.en_eint = 1; // Enable EINT interrupt |
coreyharris | 4:06e258ff0b97 | 468 | EN_INT_r.bits.en_rrint = 0; // Disable R-to-R interrupt |
coreyharris | 3:420d5efbd967 | 469 | EN_INT_r.bits.intb_type = 3; // Open-drain NMOS with internal pullup |
coreyharris | 1:86843c27cc81 | 470 | ecgAFE.writeRegister( MAX30003::EN_INT , EN_INT_r.all); |
coreyharris | 1:86843c27cc81 | 471 | |
coreyharris | 1:86843c27cc81 | 472 | |
coreyharris | 1:86843c27cc81 | 473 | //Dyanmic modes config |
coreyharris | 1:86843c27cc81 | 474 | MAX30003::ManageDynamicModes_u MNG_DYN_r; |
coreyharris | 3:420d5efbd967 | 475 | MNG_DYN_r.bits.fast = 0; // Fast recovery mode disabled |
coreyharris | 1:86843c27cc81 | 476 | ecgAFE.writeRegister( MAX30003::MNGR_DYN , MNG_DYN_r.all); |
coreyharris | 5:f8d1f651bef5 | 477 | |
coreyharris | 5:f8d1f651bef5 | 478 | // MUX Config |
coreyharris | 5:f8d1f651bef5 | 479 | MAX30003::MuxConfiguration_u CNFG_MUX_r; |
coreyharris | 5:f8d1f651bef5 | 480 | CNFG_MUX_r.bits.openn = 0; // Connect ECGN to AFE channel |
coreyharris | 5:f8d1f651bef5 | 481 | CNFG_MUX_r.bits.openp = 0; // Connect ECGP to AFE channel |
coreyharris | 5:f8d1f651bef5 | 482 | ecgAFE.writeRegister( MAX30003::CNFG_EMUX , CNFG_MUX_r.all); |
coreyharris | 1:86843c27cc81 | 483 | |
coreyharris | 1:86843c27cc81 | 484 | return; |
coreyharris | 0:38c49bc37c7c | 485 | } |
kidecha_rahul | 23:288c5d1babd1 | 486 |