Mode1 Optical Validation

Dependencies:   max32630fthr

Revision:
6:e1b7190d6cb5
Parent:
5:7f5a012747a5
Child:
7:23b067817ab8
diff -r 7f5a012747a5 -r e1b7190d6cb5 main.cpp
--- a/main.cpp	Thu Mar 25 04:08:19 2021 +0000
+++ b/main.cpp	Fri Mar 26 00:41:20 2021 +0000
@@ -44,14 +44,22 @@
 */
 /******************************************************************************
 * Tera Term output is set to 115200 baud rate.
-* ver: 210324
+* ver: 210325
 ******************************************************************************/
 
-//#define MAXM86146_CFG 1  // tested on MAXM86146+MAX32630FTHR
-//#define HSP2  // MAXREFDES103 not debugged yet
-#ifdef HSP2
+/*****************************************************************************/
+// define one and only one of the following three platforms
+//#define MAXM86146_CFG 1  // tested on MAXM86146EVSYS_sensorBrd+MAXM86161_ADAPTER_REVB+MAX32630FTHR
+#define MAXREFDES103_CFG  // tested on MAXREFDES103
+//#define MAX86161_CFG 1
+/*****************************************************************************/
+
+#define ALGO_ONLY 1  // define this if you only want the algo data, comment out if you want raw sensor+algo data
+
+
+#ifdef MAXREFDES103_CFG
 #include "MAX20303.h"
-I2C i2c_pmic(P5_7, P6_0);
+I2C sh_i2c_pmic(P5_7, P6_0);
 #endif
 
 #ifdef MAXM86146_CFG
@@ -63,7 +71,6 @@
 #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_ONLY 1
 #ifdef ALGO_ONLY
   #define TTL_SZ (ALGO_SZ)
 #else
@@ -81,10 +88,12 @@
 #define MFIO_PIN  P5_4
 DigitalOut rst(RST_PIN, PullUp);
 DigitalOut mfio(MFIO_PIN, PullUp);
-I2C i2c(P3_4, P3_5);
+I2C sh_i2c(P3_4, P3_5);
+
+//#define thread_sleep_for(x) wait_ms(x) // for older versions of mbed
 
 
-const int addr = 0xAA;//0x55;
+const int SH_ADDR = 0xAA;//0x55;
 int32_t Time_to_Read_PPG = 0;
 
 #define BLINKING_RATE_MS 1000ms
@@ -111,16 +120,16 @@
 #if 0
 // 2.1
     cmd[0] = 0x00; cmd[1] = 0x00;
-    i2c.write(addr, cmd, 2);
+    sh_i2c.write(SH_ADDR, cmd, 2);
     wait_us(100);
-    i2c.read(addr, rsp, 2);
+    sh_i2c.read(SH_ADDR, rsp, 2);
 //    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);
+    sh_i2c.write(SH_ADDR, cmd, 2);
     wait_us(100);
-    i2c.read(addr, rsp, 2);
+    sh_i2c.read(SH_ADDR, rsp, 2);
 //    pc.printf("2.2 Status: %x %x\n\r", rsp[0], rsp[1]);
     samples = rsp[1];
 //    pc.printf("num samples %d, (num*ttl)+1 %d\n\r",  rsp[1], TTL_SZ*samples+1);
@@ -128,10 +137,10 @@
     scnt = rsp[1];
 // 2.3
     cmd[0] = 0x12; cmd[1] = 0x01;
-    i2c.write(addr, cmd, 2);
+    sh_i2c.write(SH_ADDR, cmd, 2);
     wait_us(100);
 //    thread_sleep_for(1);
-    i2c.read(addr, rsp, 1+(TTL_SZ*samples));
+    sh_i2c.read(SH_ADDR, rsp, 1+(TTL_SZ*samples));
         status = rsp[0];
 
         sptr = 1;
@@ -198,13 +207,13 @@
         mfio = 1;
 }
 
-#ifdef HSP2
-void init_pmic(void) {
+#ifdef MAXREFDES103_CFG
+void init_max20303_pmic(void) {
     /* Wait for pmic to settle down */
     thread_sleep_for(800);
 
     //set_time(1544787300);  // Set RTC time to Wed, 28 Oct 2009 11:35:37
-    MAX20303 max20303(&i2c);
+    MAX20303 max20303(&sh_i2c);
     /*Set LDO1 to 1.8v*/
     max20303.LDO1Config();
 
@@ -214,25 +223,22 @@
     //max20303.BoostEnable();
     max20303.BuckBoostEnable();
 
-
     max20303.led0on(0);
     max20303.led1on(0);
     max20303.led2on(0);
 
     /* Wait for pmic to settle down */
     thread_sleep_for(200);
-    
+   
 }
-#endif // HSP2
+#endif // MAXREFDES103_CFG
 int main()
 {
-    i2c.frequency(400000);
+    sh_i2c.frequency(400000);
     char cmd[8], i, j;
     char rsp[256];
     int32_t ppg[12];
-#ifdef HSP2
-    init_pmic();
-#endif
+    int32_t ledcnt = 0;
     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 */
@@ -246,19 +252,21 @@
 
     mfio = 0; wait_us(300);
 
+#ifdef MAXREFDES103_CFG
+    init_max20303_pmic();
+#endif
 //read operating mode
     cmd[0] = 0x02; cmd[1] = 0x00;
-    i2c.write(addr, cmd, 2);
+    sh_i2c.write(SH_ADDR, cmd, 2);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 2);
-    mfio = 1; mfio = 0; wait_us(300);
-    pc.printf("02 00 Status, Operating Mode: %x %x\n\r", rsp[0], rsp[1]);
+    sh_i2c.read(SH_ADDR, rsp, 2);
+    mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
+    pc.printf("\n\r 0x02 0x00 Status, Read Operating Mode: %x %x\n\r", rsp[0], rsp[1]);
 //1.11 rd ver
     cmd[0] = 0xFF; cmd[1] = 0x03;
-    i2c.write(addr, cmd, 2);
+    sh_i2c.write(SH_ADDR, cmd, 2);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    wait_us(300);
-    i2c.read(addr, rsp, 4);
+    sh_i2c.read(SH_ADDR, rsp, 4);
     mfio = 1; mfio = 0; wait_us(300);
     pc.printf("Ver: %d %d %d %d\n\r", rsp[0], rsp[1], rsp[2], rsp[3]);
 
@@ -271,81 +279,81 @@
     cmd[2] = 0x03;  // sensor + algo data
     pc.printf("sens+algo \n\r");
 #endif
-    i2c.write(addr, cmd, 3);
+    sh_i2c.write(SH_ADDR, cmd, 3);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_ADDR, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
     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);
+    sh_i2c.write(SH_ADDR, cmd, 4);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_ADDR, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
     pc.printf("1.7 Status: %x\n\r", rsp[0]);
 // 1.8 AEC enable (default)
     cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x0B; cmd[3] = 0x01;
-    i2c.write(addr, cmd, 4);
+    sh_i2c.write(SH_ADDR, cmd, 4);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_ADDR, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
     pc.printf("1.8 Status: %x\n\r", rsp[0]);
 // 1.9 auto PD (default
     cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x12; cmd[3] = 0x01;
-    i2c.write(addr, cmd, 4);
+    sh_i2c.write(SH_ADDR, cmd, 4);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_ADDR, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
     pc.printf("1.9 Status: %x\n\r", rsp[0]);
 // 1.10 SCD (default)
     cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x0C; cmd[3] = 0x01;
-    i2c.write(addr, cmd, 4);
+    sh_i2c.write(SH_ADDR, cmd, 4);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_ADDR, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
     pc.printf("1.7 Status: %x\n\r", rsp[0]);
 #ifdef MAXM86146_CFG
 //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);
+    sh_i2c.write(SH_ADDR, cmd, 6);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_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);
+    sh_i2c.write(SH_ADDR, cmd, 5);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_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);
+    sh_i2c.write(SH_ADDR, cmd, 5);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_ADDR, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
     pc.printf("map SpO2 to slots/PDs %x\n\r", rsp[0]);
 #if 0
 //1.20 Sec 4.1  map leds to slots for MAXM86146
     cmd[0] = 0x50; cmd[1] = 0x07; cmd[2] = 0x19; cmd[3] = 0x15; cmd[4] = 0x60; cmd[5] = 0x00;
-    i2c.write(addr, cmd, 6);
+    sh_i2c.write(SH_ADDR, cmd, 6);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_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] = 0x01;
-    i2c.write(addr, cmd, 5);
+    sh_i2c.write(SH_ADDR, cmd, 5);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_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] = 0x20; cmd[4] = 0x10;
-    i2c.write(addr, cmd, 5);
+    sh_i2c.write(SH_ADDR, cmd, 5);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_ADDR, rsp, 1);
     mfio = 1; mfio = 0; wait_us(300);
     pc.printf("map SpO2 to slots/PDs %x\n\r", rsp[0]);
 #endif
@@ -353,34 +361,47 @@
 
 // 1.3 Enable HR, SpO2 algo
     cmd[0] = 0x52; cmd[1] = 0x07; cmd[2] = 0x01;
-    i2c.write(addr, cmd, 3);
+    sh_i2c.write(SH_ADDR, cmd, 3);
     thread_sleep_for(465);
-    i2c.read(addr, rsp, 1);
+    sh_i2c.read(SH_ADDR, rsp, 1);
     pc.printf("1.14 status: %x\n\r", rsp[0]);
     mfio = 1;
 
 #if 1
 //1.31 rd AFE part id
     cmd[0] = 0x41; cmd[1] = 0x00; cmd[2] = 0xFF;
-    i2c.write(addr, cmd, 3);
+    sh_i2c.write(SH_ADDR, cmd, 3);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 2);
+    sh_i2c.read(SH_ADDR, rsp, 2);
     mfio = 1; mfio = 0; wait_us(300);
     pc.printf("1.31 part id afe %x %x\n\r", rsp[0], rsp[1]);
 //1.32 rd accel who
     cmd[0] = 0x41; cmd[1] = 0x04; cmd[2] = 0x0F;
-    i2c.write(addr, cmd, 3);
+    sh_i2c.write(SH_ADDR, cmd, 3);
     mfio = 1; thread_sleep_for(2); mfio = 0; wait_us(300);
-    i2c.read(addr, rsp, 2);
+    sh_i2c.read(SH_ADDR, rsp, 2);
     mfio = 1; mfio = 0; wait_us(300);
     pc.printf("1.32 who accel %x %x\n\r", rsp[0], rsp[1]);
 #endif
 
-    ticker.attach(callback(&fifo_timer), 0.040f);
-//    Timer tmr1;
+#if defined(MAXREFDES103_CFG) || defined(MAXM86161_CFG)
+    Timer tmr1;
     while (1) {
+        tmr1.start();
+        if (tmr1.read_ms() > 40) {
             read_ppg();
-        if (Time_to_Read_PPG) {
+            if (ledcnt++ % 50)
+                gLED = !gLED;
         }
     }
+#else
+    ticker.attach(callback(&fifo_timer), 0.040f);
+    while (1) {
+        if (Time_to_Read_PPG) {
+            read_ppg();
+            if (ledcnt++ % 50)
+                gLED = !gLED;
+        }
+    }
+#endif
 }
\ No newline at end of file