Mode1 Optical Validation

Dependencies:   max32630fthr

Revision:
3:da975696b936
Parent:
2:563d90a111b3
Child:
4:4f1e9439f048
--- a/main.cpp	Thu Mar 18 20:48:49 2021 +0000
+++ b/main.cpp	Fri Mar 19 00:14:42 2021 +0000
@@ -30,11 +30,10 @@
 * ownership rights.
 *******************************************************************************
 */
-//#include "max32630fthr.h"
-//#include "USBSerial.h"
 
 #include "mbed.h"
 #include "platform/mbed_thread.h"
+#include "mbed.h"
 
 #define MAXM86146_CFG 1
 #ifdef MAXM86146_CFG
@@ -44,8 +43,8 @@
 #endif
 #define ACCEL_SZ 6  // accel
 #define SENSOR_SZ (PPG_SZ+ACCEL_SZ)
-#define ALGO_SZ 20  // 24 bytes is the algo normal size for 3x.12.0
-//#define ALGO_SZ 24  // 24 bytes is the algo normal size for 3x.13.x
+//#define ALGO_SZ 20  // 24 bytes is the algo normal size for 3x.12.0
+#define ALGO_SZ 24  // 24 bytes is the algo normal size for 3x.13.x
 //#define ALGO_ONLY 1
 #ifdef ALGO_ONLY
   #define TTL_SZ (ALGO_SZ)
@@ -53,6 +52,7 @@
   #define TTL_SZ (PPG_SZ+ACCEL_SZ+ALGO_SZ)
 #endif
 
+Serial pc(USBTX, USBRX, 115200);
 DigitalOut rLED(LED1);
 DigitalOut gLED(LED2);
 DigitalOut bLED(LED3);
@@ -79,7 +79,7 @@
     char rsp[3000];
     int32_t ppg[12];
     int32_t accel[3];
-    int32_t status, opmode, hr, hr_conf, spo2, spo2_conf, scd;
+    int32_t status, opmode, hr, hr_conf, ibi, ibi_conf, act, r, spo2, spo2_conf, scd;
     int32_t scnt = 0;
     int32_t ptr = 0;
     int32_t sptr = 0;
@@ -91,23 +91,19 @@
     cmd[1] = 0x00;
     i2c.write(addr, cmd, 2);
     wait_us(100);
-    rsp[0] = 0xAA;
-    rsp[1] = 0xAA;
     i2c.read(addr, rsp, 2);
-//    printf("2.1 Status: %x %x\n", rsp[0], rsp[1]);
+//    pc.printf("2.1 Status: %x %x\n\r", rsp[0], rsp[1]);
 #endif
 // 2.2
     cmd[0] = 0x12;
     cmd[1] = 0x00;
     i2c.write(addr, cmd, 2);
     wait_us(100);
-    rsp[0] = 0xAA;
-    rsp[1] = 0xAA;
     i2c.read(addr, rsp, 2);
-//    printf("2.2 Status: %x %x\n", rsp[0], rsp[1]);
+//    pc.printf("2.2 Status: %x %x\n\r", rsp[0], rsp[1]);
     samples = rsp[1];
-//    printf("num samples %d, (num*ttl)+1 %d\n",  rsp[1], TTL_SZ*samples+1);
-//    printf("num smpls %d \n",  samples);
+//    pc.printf("num samples %d, (num*ttl)+1 %d\n\r",  rsp[1], TTL_SZ*samples+1);
+//    pc.printf("num smpls %d \n\r",  samples);
     scnt = rsp[1];
 // 2.3
     cmd[0] = 0x12;
@@ -127,136 +123,43 @@
             ppg[0] = (rsp[ptr+0] << 16) | (rsp[ptr+1] << 8) | (rsp[ptr+2]);
             ppg[1] = (rsp[ptr+3] << 16) | (rsp[ptr+4] << 8) | (rsp[ptr+5]);
             ppg[2] = (rsp[ptr+6] << 16) | (rsp[ptr+7] << 8) | (rsp[ptr+8]);
-            printf("%d,%d,%d,", ppg[0], ppg[1], ppg[2]);
+            ppg[3] = (rsp[ptr+9] << 16) | (rsp[ptr+10] << 8) | (rsp[ptr+11]);
+            pc.printf("%d,%d,%d,%d,", ppg[0], ppg[1], ppg[2], ppg[3]);
             ptr = sptr + SENSOR_SZ;
 #endif
-//            printf("ptr %d ttlsiz %d ", ptr, TTL_SZ);
+//            pc.printf("ptr %d ttlsiz %d ", ptr, TTL_SZ);
             opmode = rsp[ptr];
             hr =  (rsp[ptr+1] << 8) + rsp[ptr+2];
             hr_conf = rsp[ptr+3];
+            ibi = rsp[ptr+4];
+            ibi_conf = (rsp[ptr+5] << 8) + rsp[ptr+6];
+            act = rsp[ptr+7];
+            r = (rsp[ptr+8] << 8) + rsp[ptr+9];
             spo2_conf = rsp[ptr+10];
             spo2 = (rsp[ptr+11] << 8) + rsp[ptr+12];
             scd = rsp[ptr+19];
 
             sptr += (TTL_SZ);
 
-            printf("%d,%d,%d,%d,%d", hr, hr_conf, spo2, spo2_conf, scd);
-            printf("\n");
+            pc.printf("%d,%d,%d,%d,%d", hr, hr_conf, spo2, spo2_conf, scd);
+            pc.printf("\n\r");
         }
         mfio = 1;
 }
 
-
-
 int main()
 {   
-    i2c.frequency(200000);
+    i2c.frequency(400000);
     char cmd[8], i, j;
     char rsp[256];
     int32_t ppg[12];
-        
+
     rLED = LED_OFF;
     gLED = LED_ON;
     bLED = LED_OFF;
     Ticker ticker;   // calls a callback repeatedly with a timeout
     //ticker.attach(callback(&blink_timer), BLINKING_RATE_MS);  /* set timer for one second */
 
-//==========================================
-#if 0  // BL, app switching
-    // BL    
-    rst = 0;
-    mfio = 0;
-    thread_sleep_for(10);
-    rst = 1;
-    thread_sleep_for(50);
-
-    mfio = 0; wait_us(300);
-    cmd[0] = 0x02;
-    cmd[1] = 0x00;
-    i2c.write(addr, cmd, 2);
-    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    thread_sleep_for(2);
-    rsp[0] = 0x00;
-    rsp[1] = 0x00;
-    i2c.read(addr, rsp, 2);
-    mfio = 1; mfio = 0; wait_us(300);
-    printf("02 00 Status, Operating Mode: %x %x\n", rsp[0], rsp[1]);
-
-// rd BL ver    
-    cmd[0] = 0x81;
-    cmd[1] = 0x00;
-    i2c.write(addr, cmd, 2);
-    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    thread_sleep_for(2);
-    rsp[0] = 0xAA;
-    rsp[1] = 0xAA;
-    rsp[2] = 0xAA;
-    rsp[3] = 0xAA;
-    i2c.read(addr, rsp, 4);
-    mfio = 1; mfio = 0; wait_us(300);
-    printf("Ver: %d %d %d %d\n", rsp[0], rsp[1], rsp[2], rsp[3]);
-
-    // application
-    rst = 0;
-    mfio = 1;
-    thread_sleep_for(10);
-    rst = 1;
-    thread_sleep_for(1500);
-
-    mfio = 1; mfio = 0; wait_us(300);
-    cmd[0] = 0x02;
-    cmd[1] = 0x00;
-    i2c.write(addr, cmd, 2);
-    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    rsp[0] = 0x00;
-    rsp[1] = 0x00;
-    i2c.read(addr, rsp, 2);
-    mfio = 1; mfio = 0; wait_us(300);
-    printf("02 00 Status, Operating Mode: %x %x\n", rsp[0], rsp[1]);
-// rd ver    
-    cmd[0] = 0xFF;
-    cmd[1] = 0x03;
-    i2c.write(addr, cmd, 2);
-    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    rsp[0] = 0xAA;
-    rsp[1] = 0xAA;
-    rsp[2] = 0xAA;
-    rsp[3] = 0xAA;
-    i2c.read(addr, rsp, 4);
-    printf("Ver: %d %d %d %d\n", rsp[0], rsp[1], rsp[2], rsp[3]);
-    mfio = 1;
-   
-    
-     // BL    
-    rst = 0;
-    mfio = 0;
-    thread_sleep_for(10);
-    rst = 1;
-    thread_sleep_for(50);
-
-    cmd[0] = 0x02;
-    cmd[1] = 0x00;
-    i2c.write(addr, cmd, 2);
-    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    rsp[0] = 0xAA;
-    rsp[1] = 0xAA;
-    i2c.read(addr, rsp, 2);
-    printf("02 00 Status, Operating Mode: %x %x\n", rsp[0], rsp[1]);
-// rd BL ver    
-    mfio = 1; mfio = 0; wait_us(300);
-    cmd[0] = 0x81;
-    cmd[1] = 0x00;
-    i2c.write(addr, cmd, 2);
-    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    rsp[0] = 0xAA;
-    rsp[1] = 0xAA;
-    rsp[2] = 0xAA;
-    rsp[3] = 0xAA;
-    i2c.read(addr, rsp, 4);
-    printf("Ver: %d %d %d %d\n", rsp[0], rsp[1], rsp[2], rsp[3]);
-    mfio = 1;
-#endif  // end BL app switching
-//==========================================
     // application
     rst = 0;
     mfio = 1;
@@ -269,27 +172,19 @@
     cmd[1] = 0x00;
     i2c.write(addr, cmd, 2);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    rsp[0] = 0xAA;
-    rsp[1] = 0xAA;
     i2c.read(addr, rsp, 2);
     mfio = 1; mfio = 0; wait_us(300);
-    printf("02 00 Status, Operating Mode: %x %x\n", rsp[0], rsp[1]);
+    pc.printf("02 00 Status, Operating Mode: %x %x\n\r", rsp[0], rsp[1]);
 // rd ver    
     cmd[0] = 0xFF;
     cmd[1] = 0x03;
     i2c.write(addr, cmd, 2);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
     wait_us(300);
-    rsp[0] = 0xAA;
-    rsp[1] = 0xAA;
-    rsp[2] = 0xAA;
-    rsp[3] = 0xAA;
     i2c.read(addr, rsp, 4);
     mfio = 1; mfio = 0; wait_us(300);
-    printf("Ver: %d %d %d %d\n", rsp[0], rsp[1], rsp[2], rsp[3]);
+    pc.printf("Ver: %d %d %d %d\n\r", rsp[0], rsp[1], rsp[2], rsp[3]);
 
-
-// aec mode
 // 1.3 sensor and algo data
     cmd[0] = 0x10;
     cmd[1] = 0x00;
@@ -300,10 +195,19 @@
 #endif
     i2c.write(addr, cmd, 3);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    rsp[0] = 0xAA;
     i2c.read(addr, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
-    printf("1.3 Status: %x\n", rsp[0]);
+    pc.printf("1.3 Status: %x\n\r", rsp[0]);
+// 1.7 cont hr, spo2
+    cmd[0] = 0x50;
+    cmd[1] = 0x07;
+    cmd[2] = 0x0A;
+    cmd[3] = 0x00;
+    i2c.write(addr, cmd, 4);
+    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
+    i2c.read(addr, rsp, 1);
+    mfio = 1; mfio = 0; wait_us(300);
+    pc.printf("1.7 Status: %x\n\r", rsp[0]);
 //1.11 rd AFE part id
     cmd[0] = 0x41;
     cmd[1] = 0x00;
@@ -312,7 +216,7 @@
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
     i2c.read(addr, rsp, 2);
     mfio = 1; mfio = 0; wait_us(300);
-    printf("1.11 part id afe %x %x\n", rsp[0], rsp[1]);
+    pc.printf("1.11 part id afe %x %x\n\r", rsp[0], rsp[1]);
 //1.13 rd accel who
     cmd[0] = 0x41;
     cmd[1] = 0x04;
@@ -321,8 +225,45 @@
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
     i2c.read(addr, rsp, 2);
     mfio = 1; mfio = 0; wait_us(300);
-    printf("1.12 who accel %x %x\n", rsp[0], rsp[1]);
-
+    pc.printf("1.12 who accel %x %x\n\r", rsp[0], rsp[1]);
+#ifdef MAXM86146_CFG
+#if 1
+//1.20 Sec 4.1  map leds to slots for MAXM86146
+    cmd[0] = 0x50;
+    cmd[1] = 0x07;
+    cmd[2] = 0x19;
+    cmd[3] = 0x13;
+    cmd[4] = 0x56;
+    cmd[5] = 0x00;
+    i2c.write(addr, cmd, 6);
+    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
+    i2c.read(addr, rsp, 1);
+    mfio = 1; mfio = 0; wait_us(300);
+    pc.printf("map leds to slots%x\n\r", rsp[0]);
+//1.21  map HR inputs to slots
+    cmd[0] = 0x50;
+    cmd[1] = 0x07;
+    cmd[2] = 0x17;
+    cmd[3] = 0x00;
+    cmd[4] = 0x11;
+    i2c.write(addr, cmd, 5);
+    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
+    i2c.read(addr, rsp, 1);
+    mfio = 1; mfio = 0; wait_us(300);
+    pc.printf("map HR to slots/PDs %x\n\r", rsp[0]);
+//1.22  map SpO2 inputs to slots
+    cmd[0] = 0x50;
+    cmd[1] = 0x07;
+    cmd[2] = 0x18;
+    cmd[3] = 0x30;
+    cmd[4] = 0x20;
+    i2c.write(addr, cmd, 5);
+    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
+    i2c.read(addr, rsp, 1);
+    mfio = 1; mfio = 0; wait_us(300);
+    pc.printf("map SpO2 to slots/PDs %x\n\r", rsp[0]);
+#endif
+#if 0
 //1.20 Sec 4.1  map leds to slots for MAXM86146
     cmd[0] = 0x50;
     cmd[1] = 0x07;
@@ -334,7 +275,7 @@
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
     i2c.read(addr, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
-    printf("map leds to slots%x\n", rsp[0]);
+    pc.printf("map leds to slots%x\n\r", rsp[0]);
 //1.21  map HR inputs to slots
     cmd[0] = 0x50;
     cmd[1] = 0x07;
@@ -345,8 +286,7 @@
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
     i2c.read(addr, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
-    printf("map HR to slots/PDs %x\n", rsp[0]);
-
+    pc.printf("map HR to slots/PDs %x\n\r", rsp[0]);
 //1.22  map SpO2 inputs to slots
     cmd[0] = 0x50;
     cmd[1] = 0x07;
@@ -357,7 +297,9 @@
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
     i2c.read(addr, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
-    printf("map SpO2 to slots/PDs %x\n", rsp[0]);
+    pc.printf("map SpO2 to slots/PDs %x\n\r", rsp[0]);
+#endif
+#endif // MAXM86146_CFG
 
 // 1.3
     cmd[0] = 0x52;
@@ -365,13 +307,12 @@
     cmd[2] = 0x01;
     i2c.write(addr, cmd, 3);
     thread_sleep_for(465);
-    rsp[0] = 0xAA;
     i2c.read(addr, rsp, 1);
-    printf("1.14 status: %x\n", rsp[0]);
+    pc.printf("1.14 status: %x\n\r", rsp[0]);
     mfio = 1;
-    wait_us(300);
 
-    ticker.attach(callback(&fifo_timer), 40ms);
+    ticker.attach(callback(&fifo_timer), 0.040f);
+//    Timer tmr1;
     while (1) {
         if (Time_to_Read_PPG) {
             read_ppg();