Mode1 Optical Validation

Dependencies:   max32630fthr

Committer:
phonemacro
Date:
Thu Apr 01 01:51:52 2021 +0000
Revision:
12:17a0bf823462
Parent:
11:a16b6bf38841
Child:
13:1baccc6275a7
Add extended algo FIFO format. Fix IBI SpO2 for swapped LSB/MSB.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
phonemacro 0:6f65cae31c54 1 /*******************************************************************************
phonemacro 9:12720bbacfe2 2 * Copyright (C) 2021 Maxim Integrated Products, Inc., All Rights Reserved.
phonemacro 0:6f65cae31c54 3 *
phonemacro 0:6f65cae31c54 4 * Permission is hereby granted, free of charge, to any person obtaining a
phonemacro 0:6f65cae31c54 5 * copy of this software and associated documentation files (the "Software"),
phonemacro 0:6f65cae31c54 6 * to deal in the Software without restriction, including without limitation
phonemacro 0:6f65cae31c54 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
phonemacro 0:6f65cae31c54 8 * and/or sell copies of the Software, and to permit persons to whom the
phonemacro 0:6f65cae31c54 9 * Software is furnished to do so, subject to the following conditions:
phonemacro 0:6f65cae31c54 10 *
phonemacro 0:6f65cae31c54 11 * The above copyright notice and this permission notice shall be included
phonemacro 0:6f65cae31c54 12 * in all copies or substantial portions of the Software.
phonemacro 0:6f65cae31c54 13 *
phonemacro 0:6f65cae31c54 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
phonemacro 0:6f65cae31c54 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
phonemacro 0:6f65cae31c54 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
phonemacro 0:6f65cae31c54 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
phonemacro 0:6f65cae31c54 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
phonemacro 0:6f65cae31c54 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
phonemacro 0:6f65cae31c54 20 * OTHER DEALINGS IN THE SOFTWARE.
phonemacro 0:6f65cae31c54 21 *
phonemacro 0:6f65cae31c54 22 * Except as contained in this notice, the name of Maxim Integrated
phonemacro 0:6f65cae31c54 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
phonemacro 0:6f65cae31c54 24 * Products, Inc. Branding Policy.
phonemacro 0:6f65cae31c54 25 *
phonemacro 0:6f65cae31c54 26 * The mere transfer of this software does not imply any licenses
phonemacro 0:6f65cae31c54 27 * of trade secrets, proprietary technology, copyrights, patents,
phonemacro 0:6f65cae31c54 28 * trademarks, maskwork rights, or any other form of intellectual
phonemacro 0:6f65cae31c54 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
phonemacro 0:6f65cae31c54 30 * ownership rights.
phonemacro 0:6f65cae31c54 31 *******************************************************************************
phonemacro 0:6f65cae31c54 32 */
phonemacro 0:6f65cae31c54 33
phonemacro 0:6f65cae31c54 34 #include "mbed.h"
phonemacro 0:6f65cae31c54 35 #include "platform/mbed_thread.h"
phonemacro 3:da975696b936 36 #include "mbed.h"
phonemacro 4:4f1e9439f048 37 /******************************************************************************
phonemacro 4:4f1e9439f048 38 * Warning, if using the either
phonemacro 4:4f1e9439f048 39 * MAX32630FTHR+MAXM86161_ADPTER_REVB+MAXM86146EVSYS sensor brd or
phonemacro 4:4f1e9439f048 40 * MAX32630FTHR+MAXM86161_ADPTER_REVB+MAXM86161EVSYS sensor brd,
phonemacro 4:4f1e9439f048 41 * The VLED is connected to USB power which is noisy. The VLED should be
phonemacro 4:4f1e9439f048 42 * connected to a regulated power supply if you are testing accuracy.
phonemacro 4:4f1e9439f048 43 *******************************************************************************
phonemacro 4:4f1e9439f048 44 */
phonemacro 4:4f1e9439f048 45 /******************************************************************************
phonemacro 4:4f1e9439f048 46 * Tera Term output is set to 115200 baud rate.
phonemacro 6:e1b7190d6cb5 47 * ver: 210325
phonemacro 5:7f5a012747a5 48 ******************************************************************************/
phonemacro 5:7f5a012747a5 49
phonemacro 6:e1b7190d6cb5 50 /*****************************************************************************/
phonemacro 6:e1b7190d6cb5 51 // define one and only one of the following three platforms
phonemacro 11:a16b6bf38841 52 #define MAXM86146_CFG 1 // tested on MAXM86146EVSYS_sensorBrd+MAXM86161_ADAPTER_REVB+MAX32630FTHR
phonemacro 12:17a0bf823462 53 //#define MAXREFDES103_CFG
phonemacro 6:e1b7190d6cb5 54 //#define MAX86161_CFG 1
phonemacro 6:e1b7190d6cb5 55 /*****************************************************************************/
phonemacro 6:e1b7190d6cb5 56
phonemacro 6:e1b7190d6cb5 57 #define ALGO_ONLY 1 // define this if you only want the algo data, comment out if you want raw sensor+algo data
phonemacro 12:17a0bf823462 58 //#define EXTENDED_ALGO 1 // define this if you want the extended algo format
phonemacro 6:e1b7190d6cb5 59
phonemacro 6:e1b7190d6cb5 60
phonemacro 6:e1b7190d6cb5 61 #ifdef MAXREFDES103_CFG
phonemacro 5:7f5a012747a5 62 #include "MAX20303.h"
phonemacro 6:e1b7190d6cb5 63 I2C sh_i2c_pmic(P5_7, P6_0);
phonemacro 5:7f5a012747a5 64 #endif
phonemacro 5:7f5a012747a5 65
phonemacro 2:563d90a111b3 66 #ifdef MAXM86146_CFG
phonemacro 0:6f65cae31c54 67 #define PPG_SZ 36 //maxm86146
phonemacro 0:6f65cae31c54 68 #else
phonemacro 0:6f65cae31c54 69 #define PPG_SZ 18 //maxm86161, max86141
phonemacro 0:6f65cae31c54 70 #endif
phonemacro 0:6f65cae31c54 71 #define ACCEL_SZ 6 // accel
phonemacro 0:6f65cae31c54 72 #define SENSOR_SZ (PPG_SZ+ACCEL_SZ)
phonemacro 12:17a0bf823462 73 #ifdef EXTENDED_ALGO
phonemacro 12:17a0bf823462 74 //#define ALGO_SZ 52 // 52 bytes is the extended algo size for 3x.12.0
phonemacro 12:17a0bf823462 75 #define ALGO_SZ 56 // 56 bytes is the extended algo size for 3x.13.x
phonemacro 12:17a0bf823462 76 #else
phonemacro 12:17a0bf823462 77 //#define ALGO_SZ 20 // 24 bytes is the algo normal size for 3x.12.0
phonemacro 12:17a0bf823462 78 #define ALGO_SZ 24 // 24 bytes is the algo normal size for 3x.13.x
phonemacro 12:17a0bf823462 79 #endif
phonemacro 0:6f65cae31c54 80 #ifdef ALGO_ONLY
phonemacro 0:6f65cae31c54 81 #define TTL_SZ (ALGO_SZ)
phonemacro 0:6f65cae31c54 82 #else
phonemacro 0:6f65cae31c54 83 #define TTL_SZ (PPG_SZ+ACCEL_SZ+ALGO_SZ)
phonemacro 0:6f65cae31c54 84 #endif
phonemacro 0:6f65cae31c54 85
phonemacro 3:da975696b936 86 Serial pc(USBTX, USBRX, 115200);
phonemacro 0:6f65cae31c54 87 DigitalOut rLED(LED1);
phonemacro 0:6f65cae31c54 88 DigitalOut gLED(LED2);
phonemacro 0:6f65cae31c54 89 DigitalOut bLED(LED3);
phonemacro 5:7f5a012747a5 90 /******************************************************************************
phonemacro 5:7f5a012747a5 91 * MAX32630FTHR GPIOs
phonemacro 5:7f5a012747a5 92 ******************************************************************************/
phonemacro 0:6f65cae31c54 93 #define RST_PIN P5_6
phonemacro 0:6f65cae31c54 94 #define MFIO_PIN P5_4
phonemacro 0:6f65cae31c54 95 DigitalOut rst(RST_PIN, PullUp);
phonemacro 0:6f65cae31c54 96 DigitalOut mfio(MFIO_PIN, PullUp);
phonemacro 6:e1b7190d6cb5 97 I2C sh_i2c(P3_4, P3_5);
phonemacro 6:e1b7190d6cb5 98
phonemacro 6:e1b7190d6cb5 99 //#define thread_sleep_for(x) wait_ms(x) // for older versions of mbed
phonemacro 0:6f65cae31c54 100
phonemacro 5:7f5a012747a5 101
phonemacro 6:e1b7190d6cb5 102 const int SH_ADDR = 0xAA;//0x55;
phonemacro 0:6f65cae31c54 103 int32_t Time_to_Read_PPG = 0;
phonemacro 0:6f65cae31c54 104
phonemacro 0:6f65cae31c54 105 #define BLINKING_RATE_MS 1000ms
phonemacro 0:6f65cae31c54 106 void blink_timer(void) {
phonemacro 0:6f65cae31c54 107 gLED = !gLED; /* blink the green LED */
phonemacro 0:6f65cae31c54 108 }
phonemacro 0:6f65cae31c54 109
phonemacro 0:6f65cae31c54 110 void fifo_timer(void) {
phonemacro 0:6f65cae31c54 111 Time_to_Read_PPG = 1;
phonemacro 0:6f65cae31c54 112 }
phonemacro 0:6f65cae31c54 113
phonemacro 10:192ea88e964b 114 void read_sh_fifo(void) {
phonemacro 0:6f65cae31c54 115 char cmd[8], i, j, samples;
phonemacro 0:6f65cae31c54 116 char rsp[3000];
phonemacro 0:6f65cae31c54 117 int32_t ppg[12];
phonemacro 5:7f5a012747a5 118 int16_t accel[3];
phonemacro 12:17a0bf823462 119 int32_t status, opmode, hr, hr_conf, ibi, ibi_conf;
phonemacro 12:17a0bf823462 120 #ifdef EXTENDED_ALGO
phonemacro 12:17a0bf823462 121 int32_t walk_stp, run_stp, energy, amr, iadj1_rqt, iadj1, iadj2_rqt, iadj2, iadj3_rqt, iadj3;
phonemacro 12:17a0bf823462 122 int32_t intadj_rqt, intadj, smpladj_rqt, smpladj, rqt_smplave, afestatehr, hr_motion;
phonemacro 12:17a0bf823462 123 #endif
phonemacro 12:17a0bf823462 124 int32_t act, r, spo2, spo2_conf;
phonemacro 4:4f1e9439f048 125 int32_t spo2_compl, spo2_lo, spo2_mo, spo2_lopi, spo2_unrel, spo2_state, ibi_offset, scd;
phonemacro 2:563d90a111b3 126 int32_t scnt = 0;
phonemacro 0:6f65cae31c54 127 int32_t ptr = 0;
phonemacro 0:6f65cae31c54 128 int32_t sptr = 0;
phonemacro 1:da792e46a385 129 mfio = 0; wait_us(300);
phonemacro 0:6f65cae31c54 130 Time_to_Read_PPG = 0;
phonemacro 0:6f65cae31c54 131 #if 0
phonemacro 0:6f65cae31c54 132 // 2.1
phonemacro 4:4f1e9439f048 133 cmd[0] = 0x00; cmd[1] = 0x00;
phonemacro 6:e1b7190d6cb5 134 sh_i2c.write(SH_ADDR, cmd, 2);
phonemacro 0:6f65cae31c54 135 wait_us(100);
phonemacro 6:e1b7190d6cb5 136 sh_i2c.read(SH_ADDR, rsp, 2);
phonemacro 3:da975696b936 137 // pc.printf("2.1 Status: %x %x\n\r", rsp[0], rsp[1]);
phonemacro 0:6f65cae31c54 138 #endif
phonemacro 0:6f65cae31c54 139 // 2.2
phonemacro 4:4f1e9439f048 140 cmd[0] = 0x12; cmd[1] = 0x00;
phonemacro 6:e1b7190d6cb5 141 sh_i2c.write(SH_ADDR, cmd, 2);
phonemacro 0:6f65cae31c54 142 wait_us(100);
phonemacro 6:e1b7190d6cb5 143 sh_i2c.read(SH_ADDR, rsp, 2);
phonemacro 3:da975696b936 144 // pc.printf("2.2 Status: %x %x\n\r", rsp[0], rsp[1]);
phonemacro 0:6f65cae31c54 145 samples = rsp[1];
phonemacro 3:da975696b936 146 // pc.printf("num samples %d, (num*ttl)+1 %d\n\r", rsp[1], TTL_SZ*samples+1);
phonemacro 3:da975696b936 147 // pc.printf("num smpls %d \n\r", samples);
phonemacro 0:6f65cae31c54 148 scnt = rsp[1];
phonemacro 0:6f65cae31c54 149 // 2.3
phonemacro 4:4f1e9439f048 150 cmd[0] = 0x12; cmd[1] = 0x01;
phonemacro 6:e1b7190d6cb5 151 sh_i2c.write(SH_ADDR, cmd, 2);
phonemacro 0:6f65cae31c54 152 wait_us(100);
phonemacro 0:6f65cae31c54 153 // thread_sleep_for(1);
phonemacro 6:e1b7190d6cb5 154 sh_i2c.read(SH_ADDR, rsp, 1+(TTL_SZ*samples));
phonemacro 0:6f65cae31c54 155 status = rsp[0];
phonemacro 5:7f5a012747a5 156
phonemacro 0:6f65cae31c54 157 sptr = 1;
phonemacro 0:6f65cae31c54 158 for (i = 0; i < scnt; i++) {
phonemacro 0:6f65cae31c54 159 #ifdef ALGO_ONLY
phonemacro 0:6f65cae31c54 160 ptr = sptr;
phonemacro 0:6f65cae31c54 161 #else
phonemacro 2:563d90a111b3 162 ptr = sptr;
phonemacro 2:563d90a111b3 163 ppg[0] = (rsp[ptr+0] << 16) | (rsp[ptr+1] << 8) | (rsp[ptr+2]);
phonemacro 2:563d90a111b3 164 ppg[1] = (rsp[ptr+3] << 16) | (rsp[ptr+4] << 8) | (rsp[ptr+5]);
phonemacro 2:563d90a111b3 165 ppg[2] = (rsp[ptr+6] << 16) | (rsp[ptr+7] << 8) | (rsp[ptr+8]);
phonemacro 3:da975696b936 166 ppg[3] = (rsp[ptr+9] << 16) | (rsp[ptr+10] << 8) | (rsp[ptr+11]);
phonemacro 4:4f1e9439f048 167 ppg[4] = (rsp[ptr+12] << 16) | (rsp[ptr+13] << 8) | (rsp[ptr+14]);
phonemacro 4:4f1e9439f048 168 ppg[5] = (rsp[ptr+15] << 16) | (rsp[ptr+16] << 8) | (rsp[ptr+17]);
phonemacro 3:da975696b936 169 pc.printf("%d,%d,%d,%d,", ppg[0], ppg[1], ppg[2], ppg[3]);
phonemacro 4:4f1e9439f048 170 // pc.printf("%d,%d,", ppg[4], ppg[5]);
phonemacro 5:7f5a012747a5 171 accel[0] = (rsp[PPG_SZ+0] << 8) | (rsp[PPG_SZ+1]);
phonemacro 5:7f5a012747a5 172 accel[1] = (rsp[PPG_SZ+2] << 8) | (rsp[PPG_SZ+3]);
phonemacro 5:7f5a012747a5 173 accel[2] = (rsp[PPG_SZ+4] << 8) | (rsp[PPG_SZ+5]);
phonemacro 5:7f5a012747a5 174 pc.printf("%d,%d,%d,", accel[0], accel[1], accel[2]);
phonemacro 5:7f5a012747a5 175
phonemacro 0:6f65cae31c54 176 ptr = sptr + SENSOR_SZ;
phonemacro 0:6f65cae31c54 177 #endif
phonemacro 12:17a0bf823462 178 #ifdef EXTENDED_ALGO
phonemacro 3:da975696b936 179 // pc.printf("ptr %d ttlsiz %d ", ptr, TTL_SZ);
phonemacro 0:6f65cae31c54 180 opmode = rsp[ptr];
phonemacro 0:6f65cae31c54 181 hr = (rsp[ptr+1] << 8) + rsp[ptr+2];
phonemacro 0:6f65cae31c54 182 hr_conf = rsp[ptr+3];
phonemacro 12:17a0bf823462 183 ibi = (rsp[ptr+4] << 8) + rsp[ptr+5];
phonemacro 12:17a0bf823462 184
phonemacro 12:17a0bf823462 185 ibi_conf = rsp[ptr+6];
phonemacro 12:17a0bf823462 186 act = rsp[ptr+7];
phonemacro 12:17a0bf823462 187 walk_stp = (rsp[ptr+8] << 24) + (rsp[ptr+9] << 16) + (rsp[ptr+10] << 8) + rsp[ptr+11];
phonemacro 12:17a0bf823462 188 run_stp = (rsp[ptr+12] << 24) + (rsp[ptr+13] << 16) + (rsp[ptr+14] << 8) + rsp[ptr+15];
phonemacro 12:17a0bf823462 189
phonemacro 12:17a0bf823462 190 energy = (rsp[ptr+16] << 24) + (rsp[ptr+17] << 16) + (rsp[ptr+18] << 8) + rsp[ptr+19];
phonemacro 12:17a0bf823462 191 amr = (rsp[ptr+20] << 24) + (rsp[ptr+21] << 16) + (rsp[ptr+22] << 8) + rsp[ptr+23];
phonemacro 12:17a0bf823462 192 iadj1_rqt = rsp[ptr+24];
phonemacro 12:17a0bf823462 193 iadj1 = (rsp[ptr+25] << 8) + rsp[ptr+26];
phonemacro 12:17a0bf823462 194
phonemacro 12:17a0bf823462 195 iadj2_rqt = rsp[ptr+27];
phonemacro 12:17a0bf823462 196 iadj2 = (rsp[ptr+28] << 8) + rsp[ptr+29];
phonemacro 12:17a0bf823462 197 iadj3_rqt = rsp[ptr+30];
phonemacro 12:17a0bf823462 198 iadj3 = (rsp[ptr+31] << 8) + rsp[ptr+32];
phonemacro 12:17a0bf823462 199
phonemacro 12:17a0bf823462 200 intadj_rqt = rsp[ptr+33];
phonemacro 12:17a0bf823462 201 intadj = rsp[ptr+34];
phonemacro 12:17a0bf823462 202 smpladj_rqt = rsp[ptr+35];
phonemacro 12:17a0bf823462 203 smpladj = rsp[ptr+36];
phonemacro 12:17a0bf823462 204
phonemacro 12:17a0bf823462 205 rqt_smplave = rsp[ptr+37];
phonemacro 12:17a0bf823462 206 afestatehr = rsp[ptr+38];
phonemacro 12:17a0bf823462 207 hr_motion = rsp[ptr+39];
phonemacro 12:17a0bf823462 208 scd = rsp[ptr+40];
phonemacro 12:17a0bf823462 209
phonemacro 12:17a0bf823462 210
phonemacro 12:17a0bf823462 211 r = (rsp[ptr+41] << 8) + rsp[ptr+42];
phonemacro 12:17a0bf823462 212 spo2_conf = rsp[ptr+43];
phonemacro 12:17a0bf823462 213
phonemacro 12:17a0bf823462 214 spo2 = (rsp[ptr+44] << 8) + rsp[ptr+45];
phonemacro 12:17a0bf823462 215 spo2_compl = rsp[ptr+46];
phonemacro 12:17a0bf823462 216 spo2_lo = rsp[ptr+47];
phonemacro 12:17a0bf823462 217 spo2_mo = rsp[ptr+48];
phonemacro 12:17a0bf823462 218
phonemacro 12:17a0bf823462 219 spo2_lopi = rsp[ptr+49];
phonemacro 12:17a0bf823462 220 spo2_unrel = rsp[ptr+50];
phonemacro 12:17a0bf823462 221 spo2_state = rsp[ptr+51];
phonemacro 12:17a0bf823462 222
phonemacro 12:17a0bf823462 223 ibi_offset = rsp[ptr+52];
phonemacro 12:17a0bf823462 224
phonemacro 12:17a0bf823462 225 sptr += (TTL_SZ);
phonemacro 12:17a0bf823462 226 #if 1
phonemacro 12:17a0bf823462 227 pc.printf("%d,%d,%d,%d,", opmode, hr, hr_conf, ibi);
phonemacro 12:17a0bf823462 228 pc.printf("%d,%d,", ibi_conf, act);
phonemacro 12:17a0bf823462 229 pc.printf("%d,%d,%d,", walk_stp, run_stp, energy);
phonemacro 12:17a0bf823462 230 pc.printf("%d,%d,%d,%d,", amr, iadj1_rqt, iadj1, iadj2_rqt);
phonemacro 12:17a0bf823462 231 pc.printf("%d,%d,%d,%d,", iadj2, iadj3_rqt, iadj3, intadj_rqt);
phonemacro 12:17a0bf823462 232 pc.printf("%d,%d,%d,%d,", intadj, smpladj_rqt, smpladj, rqt_smplave);
phonemacro 12:17a0bf823462 233 pc.printf("%d,%d,", afestatehr, hr_motion);
phonemacro 12:17a0bf823462 234 pc.printf("%d,", scd);
phonemacro 12:17a0bf823462 235 pc.printf("%d,%d,%d,%d,", spo2, spo2_compl, spo2_lo, spo2_mo);
phonemacro 12:17a0bf823462 236 pc.printf("%d,%d,%d,", spo2_lopi,spo2_unrel, spo2_state);
phonemacro 12:17a0bf823462 237 pc.printf("%d,", ibi_offset);
phonemacro 12:17a0bf823462 238 #else
phonemacro 12:17a0bf823462 239 pc.printf("%d,%d,", hr, hr_conf);
phonemacro 12:17a0bf823462 240 pc.printf("%d,%d,", spo2, spo2_conf);
phonemacro 12:17a0bf823462 241 pc.printf("%d,", spo2_lo);
phonemacro 12:17a0bf823462 242 pc.printf("%d,%d,", spo2_unrel, scd);
phonemacro 12:17a0bf823462 243 #endif
phonemacro 12:17a0bf823462 244 #else // normal algo size
phonemacro 12:17a0bf823462 245 // pc.printf("ptr %d ttlsiz %d ", ptr, TTL_SZ);
phonemacro 12:17a0bf823462 246 opmode = rsp[ptr];
phonemacro 12:17a0bf823462 247 hr = (rsp[ptr+1] << 8) + rsp[ptr+2];
phonemacro 12:17a0bf823462 248 hr_conf = rsp[ptr+3];
phonemacro 12:17a0bf823462 249 ibi = (rsp[ptr+4] << 8) + rsp[ptr+5];
phonemacro 4:4f1e9439f048 250
phonemacro 11:a16b6bf38841 251 ibi_conf = rsp[ptr+6];
phonemacro 3:da975696b936 252 act = rsp[ptr+7];
phonemacro 3:da975696b936 253 r = (rsp[ptr+8] << 8) + rsp[ptr+9];
phonemacro 0:6f65cae31c54 254 spo2_conf = rsp[ptr+10];
phonemacro 4:4f1e9439f048 255
phonemacro 0:6f65cae31c54 256 spo2 = (rsp[ptr+11] << 8) + rsp[ptr+12];
phonemacro 4:4f1e9439f048 257 spo2_compl = rsp[ptr+13];
phonemacro 4:4f1e9439f048 258 spo2_lo = rsp[ptr+14];
phonemacro 4:4f1e9439f048 259 spo2_mo = rsp[ptr+15];
phonemacro 4:4f1e9439f048 260
phonemacro 4:4f1e9439f048 261 spo2_lopi = rsp[ptr+16];
phonemacro 4:4f1e9439f048 262 spo2_unrel = rsp[ptr+17];
phonemacro 4:4f1e9439f048 263 spo2_state = rsp[ptr+18];
phonemacro 2:563d90a111b3 264 scd = rsp[ptr+19];
phonemacro 0:6f65cae31c54 265
phonemacro 4:4f1e9439f048 266 ibi_offset = rsp[ptr+20];
phonemacro 0:6f65cae31c54 267
phonemacro 4:4f1e9439f048 268 sptr += (TTL_SZ);
phonemacro 4:4f1e9439f048 269 #if 0
phonemacro 4:4f1e9439f048 270 pc.printf("%d,%d,%d,%d,", opmode, hr, hr_conf, ibi);
phonemacro 4:4f1e9439f048 271 pc.printf("%d,%d,%d,%d,", ibi_conf, act, r, spo2_conf);
phonemacro 4:4f1e9439f048 272 pc.printf("%d,%d,%d,%d,", spo2, spo2_compl, spo2_lo, spo2_mo);
phonemacro 4:4f1e9439f048 273 pc.printf("%d,%d,%d,%d,", spo2_lopi,spo2_unrel, spo2_state, scd);
phonemacro 4:4f1e9439f048 274 pc.printf("%d,", ibi_offset);
phonemacro 4:4f1e9439f048 275 #else
phonemacro 4:4f1e9439f048 276 pc.printf("%d,%d,", hr, hr_conf);
phonemacro 4:4f1e9439f048 277 pc.printf("%d,%d,", spo2, spo2_conf);
phonemacro 4:4f1e9439f048 278 pc.printf("%d,", spo2_lo);
phonemacro 4:4f1e9439f048 279 pc.printf("%d,%d,", spo2_unrel, scd);
phonemacro 4:4f1e9439f048 280 #endif
phonemacro 12:17a0bf823462 281 #endif // end normal algo size
phonemacro 4:4f1e9439f048 282
phonemacro 4:4f1e9439f048 283
phonemacro 3:da975696b936 284 pc.printf("\n\r");
phonemacro 0:6f65cae31c54 285 }
phonemacro 0:6f65cae31c54 286 mfio = 1;
phonemacro 0:6f65cae31c54 287 }
phonemacro 0:6f65cae31c54 288
phonemacro 6:e1b7190d6cb5 289 #ifdef MAXREFDES103_CFG
phonemacro 6:e1b7190d6cb5 290 void init_max20303_pmic(void) {
phonemacro 5:7f5a012747a5 291 /* Wait for pmic to settle down */
phonemacro 5:7f5a012747a5 292 thread_sleep_for(800);
phonemacro 5:7f5a012747a5 293
phonemacro 5:7f5a012747a5 294 //set_time(1544787300); // Set RTC time to Wed, 28 Oct 2009 11:35:37
phonemacro 6:e1b7190d6cb5 295 MAX20303 max20303(&sh_i2c);
phonemacro 5:7f5a012747a5 296 /*Set LDO1 to 1.8v*/
phonemacro 5:7f5a012747a5 297 max20303.LDO1Config();
phonemacro 5:7f5a012747a5 298
phonemacro 5:7f5a012747a5 299 /*Set LDO2 to 3v*/
phonemacro 5:7f5a012747a5 300 max20303.LDO2Config();
phonemacro 5:7f5a012747a5 301
phonemacro 5:7f5a012747a5 302 //max20303.BoostEnable();
phonemacro 5:7f5a012747a5 303 max20303.BuckBoostEnable();
phonemacro 5:7f5a012747a5 304
phonemacro 5:7f5a012747a5 305 max20303.led0on(0);
phonemacro 5:7f5a012747a5 306 max20303.led1on(0);
phonemacro 5:7f5a012747a5 307 max20303.led2on(0);
phonemacro 5:7f5a012747a5 308
phonemacro 5:7f5a012747a5 309 /* Wait for pmic to settle down */
phonemacro 5:7f5a012747a5 310 thread_sleep_for(200);
phonemacro 6:e1b7190d6cb5 311
phonemacro 5:7f5a012747a5 312 }
phonemacro 6:e1b7190d6cb5 313 #endif // MAXREFDES103_CFG
phonemacro 10:192ea88e964b 314 void init_sh(void) {
phonemacro 0:6f65cae31c54 315 char cmd[8], i, j;
phonemacro 0:6f65cae31c54 316 char rsp[256];
phonemacro 10:192ea88e964b 317 // switch to application mode
phonemacro 0:6f65cae31c54 318 rst = 0;
phonemacro 0:6f65cae31c54 319 mfio = 1;
phonemacro 0:6f65cae31c54 320 thread_sleep_for(10);
phonemacro 0:6f65cae31c54 321 rst = 1;
phonemacro 0:6f65cae31c54 322 thread_sleep_for(1500);
phonemacro 10:192ea88e964b 323 #ifdef MAXREFDES103_CFG
phonemacro 10:192ea88e964b 324 init_max20303_pmic();
phonemacro 10:192ea88e964b 325 #endif
phonemacro 0:6f65cae31c54 326
phonemacro 1:da792e46a385 327 mfio = 0; wait_us(300);
phonemacro 4:4f1e9439f048 328
phonemacro 4:4f1e9439f048 329 //read operating mode
phonemacro 4:4f1e9439f048 330 cmd[0] = 0x02; cmd[1] = 0x00;
phonemacro 6:e1b7190d6cb5 331 sh_i2c.write(SH_ADDR, cmd, 2);
phonemacro 1:da792e46a385 332 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 333 sh_i2c.read(SH_ADDR, rsp, 2);
phonemacro 6:e1b7190d6cb5 334 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 335 pc.printf("\n\r 0x02 0x00 Status, Read Operating Mode: %x %x\n\r", rsp[0], rsp[1]);
phonemacro 5:7f5a012747a5 336 //1.11 rd ver
phonemacro 4:4f1e9439f048 337 cmd[0] = 0xFF; cmd[1] = 0x03;
phonemacro 6:e1b7190d6cb5 338 sh_i2c.write(SH_ADDR, cmd, 2);
phonemacro 1:da792e46a385 339 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 340 sh_i2c.read(SH_ADDR, rsp, 4);
phonemacro 1:da792e46a385 341 mfio = 1; mfio = 0; wait_us(300);
phonemacro 3:da975696b936 342 pc.printf("Ver: %d %d %d %d\n\r", rsp[0], rsp[1], rsp[2], rsp[3]);
phonemacro 0:6f65cae31c54 343
phonemacro 0:6f65cae31c54 344 // 1.3 sensor and algo data
phonemacro 4:4f1e9439f048 345 cmd[0] = 0x10; cmd[1] = 0x00;
phonemacro 2:563d90a111b3 346 #ifdef ALGO_ONLY
phonemacro 0:6f65cae31c54 347 cmd[2] = 0x02; // algo data
phonemacro 4:4f1e9439f048 348 pc.printf("algo only \n\r");
phonemacro 2:563d90a111b3 349 #else
phonemacro 2:563d90a111b3 350 cmd[2] = 0x03; // sensor + algo data
phonemacro 4:4f1e9439f048 351 pc.printf("sens+algo \n\r");
phonemacro 2:563d90a111b3 352 #endif
phonemacro 6:e1b7190d6cb5 353 sh_i2c.write(SH_ADDR, cmd, 3);
phonemacro 1:da792e46a385 354 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 355 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 1:da792e46a385 356 mfio = 1; mfio = 0; wait_us(300);
phonemacro 3:da975696b936 357 pc.printf("1.3 Status: %x\n\r", rsp[0]);
phonemacro 3:da975696b936 358 // 1.7 cont hr, spo2
phonemacro 4:4f1e9439f048 359 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x0A; cmd[3] = 0x00;
phonemacro 6:e1b7190d6cb5 360 sh_i2c.write(SH_ADDR, cmd, 4);
phonemacro 3:da975696b936 361 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 362 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 3:da975696b936 363 mfio = 1; mfio = 0; wait_us(300);
phonemacro 3:da975696b936 364 pc.printf("1.7 Status: %x\n\r", rsp[0]);
phonemacro 4:4f1e9439f048 365 // 1.8 AEC enable (default)
phonemacro 4:4f1e9439f048 366 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x0B; cmd[3] = 0x01;
phonemacro 6:e1b7190d6cb5 367 sh_i2c.write(SH_ADDR, cmd, 4);
phonemacro 1:da792e46a385 368 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 369 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 1:da792e46a385 370 mfio = 1; mfio = 0; wait_us(300);
phonemacro 4:4f1e9439f048 371 pc.printf("1.8 Status: %x\n\r", rsp[0]);
phonemacro 4:4f1e9439f048 372 // 1.9 auto PD (default
phonemacro 4:4f1e9439f048 373 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x12; cmd[3] = 0x01;
phonemacro 6:e1b7190d6cb5 374 sh_i2c.write(SH_ADDR, cmd, 4);
phonemacro 1:da792e46a385 375 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 376 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 1:da792e46a385 377 mfio = 1; mfio = 0; wait_us(300);
phonemacro 4:4f1e9439f048 378 pc.printf("1.9 Status: %x\n\r", rsp[0]);
phonemacro 4:4f1e9439f048 379 // 1.10 SCD (default)
phonemacro 4:4f1e9439f048 380 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x0C; cmd[3] = 0x01;
phonemacro 6:e1b7190d6cb5 381 sh_i2c.write(SH_ADDR, cmd, 4);
phonemacro 4:4f1e9439f048 382 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 383 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 4:4f1e9439f048 384 mfio = 1; mfio = 0; wait_us(300);
phonemacro 4:4f1e9439f048 385 pc.printf("1.7 Status: %x\n\r", rsp[0]);
phonemacro 3:da975696b936 386 #ifdef MAXM86146_CFG
phonemacro 3:da975696b936 387 //1.20 Sec 4.1 map leds to slots for MAXM86146
phonemacro 4:4f1e9439f048 388 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x19; cmd[3] = 0x13; cmd[4] = 0x56; cmd[5] = 0x00;
phonemacro 6:e1b7190d6cb5 389 sh_i2c.write(SH_ADDR, cmd, 6);
phonemacro 3:da975696b936 390 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 391 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 3:da975696b936 392 mfio = 1; mfio = 0; wait_us(300);
phonemacro 3:da975696b936 393 pc.printf("map leds to slots%x\n\r", rsp[0]);
phonemacro 3:da975696b936 394 //1.21 map HR inputs to slots
phonemacro 4:4f1e9439f048 395 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x17; cmd[3] = 0x00; cmd[4] = 0x11;
phonemacro 6:e1b7190d6cb5 396 sh_i2c.write(SH_ADDR, cmd, 5);
phonemacro 3:da975696b936 397 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 398 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 3:da975696b936 399 mfio = 1; mfio = 0; wait_us(300);
phonemacro 3:da975696b936 400 pc.printf("map HR to slots/PDs %x\n\r", rsp[0]);
phonemacro 3:da975696b936 401 //1.22 map SpO2 inputs to slots
phonemacro 4:4f1e9439f048 402 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x18; cmd[3] = 0x30; cmd[4] = 0x20;
phonemacro 6:e1b7190d6cb5 403 sh_i2c.write(SH_ADDR, cmd, 5);
phonemacro 3:da975696b936 404 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 405 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 3:da975696b936 406 mfio = 1; mfio = 0; wait_us(300);
phonemacro 3:da975696b936 407 pc.printf("map SpO2 to slots/PDs %x\n\r", rsp[0]);
phonemacro 3:da975696b936 408 #if 0
phonemacro 2:563d90a111b3 409 //1.20 Sec 4.1 map leds to slots for MAXM86146
phonemacro 4:4f1e9439f048 410 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x19; cmd[3] = 0x15; cmd[4] = 0x60; cmd[5] = 0x00;
phonemacro 6:e1b7190d6cb5 411 sh_i2c.write(SH_ADDR, cmd, 6);
phonemacro 1:da792e46a385 412 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 413 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 1:da792e46a385 414 mfio = 1; mfio = 0; wait_us(300);
phonemacro 3:da975696b936 415 pc.printf("map leds to slots%x\n\r", rsp[0]);
phonemacro 2:563d90a111b3 416 //1.21 map HR inputs to slots
phonemacro 4:4f1e9439f048 417 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x17; cmd[3] = 0x00; cmd[4] = 0x01;
phonemacro 6:e1b7190d6cb5 418 sh_i2c.write(SH_ADDR, cmd, 5);
phonemacro 1:da792e46a385 419 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 420 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 1:da792e46a385 421 mfio = 1; mfio = 0; wait_us(300);
phonemacro 3:da975696b936 422 pc.printf("map HR to slots/PDs %x\n\r", rsp[0]);
phonemacro 2:563d90a111b3 423 //1.22 map SpO2 inputs to slots
phonemacro 4:4f1e9439f048 424 cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x18; cmd[3] = 0x20; cmd[4] = 0x10;
phonemacro 6:e1b7190d6cb5 425 sh_i2c.write(SH_ADDR, cmd, 5);
phonemacro 1:da792e46a385 426 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 427 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 1:da792e46a385 428 mfio = 1; mfio = 0; wait_us(300);
phonemacro 3:da975696b936 429 pc.printf("map SpO2 to slots/PDs %x\n\r", rsp[0]);
phonemacro 3:da975696b936 430 #endif
phonemacro 3:da975696b936 431 #endif // MAXM86146_CFG
phonemacro 0:6f65cae31c54 432
phonemacro 4:4f1e9439f048 433 // 1.3 Enable HR, SpO2 algo
phonemacro 12:17a0bf823462 434 #ifdef EXTENDED_ALGO
phonemacro 12:17a0bf823462 435 cmd[0] = 0x52; cmd[1] = 0x07; cmd[2] = 0x02;
phonemacro 12:17a0bf823462 436 #else
phonemacro 4:4f1e9439f048 437 cmd[0] = 0x52; cmd[1] = 0x07; cmd[2] = 0x01;
phonemacro 12:17a0bf823462 438 #endif
phonemacro 6:e1b7190d6cb5 439 sh_i2c.write(SH_ADDR, cmd, 3);
phonemacro 0:6f65cae31c54 440 thread_sleep_for(465);
phonemacro 6:e1b7190d6cb5 441 sh_i2c.read(SH_ADDR, rsp, 1);
phonemacro 3:da975696b936 442 pc.printf("1.14 status: %x\n\r", rsp[0]);
phonemacro 0:6f65cae31c54 443 mfio = 1;
phonemacro 0:6f65cae31c54 444
phonemacro 4:4f1e9439f048 445 #if 1
phonemacro 4:4f1e9439f048 446 //1.31 rd AFE part id
phonemacro 4:4f1e9439f048 447 cmd[0] = 0x41; cmd[1] = 0x00; cmd[2] = 0xFF;
phonemacro 6:e1b7190d6cb5 448 sh_i2c.write(SH_ADDR, cmd, 3);
phonemacro 4:4f1e9439f048 449 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 450 sh_i2c.read(SH_ADDR, rsp, 2);
phonemacro 4:4f1e9439f048 451 mfio = 1; mfio = 0; wait_us(300);
phonemacro 4:4f1e9439f048 452 pc.printf("1.31 part id afe %x %x\n\r", rsp[0], rsp[1]);
phonemacro 4:4f1e9439f048 453 //1.32 rd accel who
phonemacro 4:4f1e9439f048 454 cmd[0] = 0x41; cmd[1] = 0x04; cmd[2] = 0x0F;
phonemacro 6:e1b7190d6cb5 455 sh_i2c.write(SH_ADDR, cmd, 3);
phonemacro 4:4f1e9439f048 456 mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
phonemacro 6:e1b7190d6cb5 457 sh_i2c.read(SH_ADDR, rsp, 2);
phonemacro 4:4f1e9439f048 458 mfio = 1; mfio = 0; wait_us(300);
phonemacro 4:4f1e9439f048 459 pc.printf("1.32 who accel %x %x\n\r", rsp[0], rsp[1]);
phonemacro 4:4f1e9439f048 460 #endif
phonemacro 10:192ea88e964b 461 }
phonemacro 10:192ea88e964b 462
phonemacro 10:192ea88e964b 463 int main()
phonemacro 10:192ea88e964b 464 {
phonemacro 10:192ea88e964b 465 sh_i2c.frequency(400000);
phonemacro 10:192ea88e964b 466 int32_t ledcnt = 0;
phonemacro 10:192ea88e964b 467 rLED = LED_OFF; gLED = LED_ON; bLED = LED_OFF;
phonemacro 10:192ea88e964b 468 Ticker ticker; // calls a callback repeatedly with a timeout
phonemacro 10:192ea88e964b 469 //ticker.attach(callback(&blink_timer), BLINKING_RATE_MS); /* set timer for one second */
phonemacro 10:192ea88e964b 470
phonemacro 10:192ea88e964b 471 init_sh();
phonemacro 4:4f1e9439f048 472
phonemacro 8:e6ae69cfecbf 473 #if defined(MAXREFDES103_CFG) || defined(MAXM86161_CFG) || defined(MAXM86146_CFG)
phonemacro 6:e1b7190d6cb5 474 Timer tmr1;
phonemacro 0:6f65cae31c54 475 while (1) {
phonemacro 6:e1b7190d6cb5 476 tmr1.start();
phonemacro 6:e1b7190d6cb5 477 if (tmr1.read_ms() > 40) {
phonemacro 7:23b067817ab8 478 tmr1.reset();
phonemacro 10:192ea88e964b 479 read_sh_fifo();
phonemacro 7:23b067817ab8 480 if ((ledcnt++ % 50) == 0)
phonemacro 6:e1b7190d6cb5 481 gLED = !gLED;
phonemacro 0:6f65cae31c54 482 }
phonemacro 0:6f65cae31c54 483 }
phonemacro 6:e1b7190d6cb5 484 #else
phonemacro 6:e1b7190d6cb5 485 ticker.attach(callback(&fifo_timer), 0.040f);
phonemacro 6:e1b7190d6cb5 486 while (1) {
phonemacro 6:e1b7190d6cb5 487 if (Time_to_Read_PPG) {
phonemacro 10:192ea88e964b 488 read_sh_fifo();
phonemacro 7:23b067817ab8 489 if ((ledcnt++ % 50) == 0)
phonemacro 6:e1b7190d6cb5 490 gLED = !gLED;
phonemacro 6:e1b7190d6cb5 491 }
phonemacro 6:e1b7190d6cb5 492 }
phonemacro 6:e1b7190d6cb5 493 #endif
phonemacro 0:6f65cae31c54 494 }