vrahane commented on a change in pull request #1088: [WIP] fix self test
URL: https://github.com/apache/mynewt-core/pull/1088#discussion_r189121986
 
 

 ##########
 File path: hw/drivers/sensors/lis2dw12/src/lis2dw12.c
 ##########
 @@ -1863,60 +1915,94 @@ int lis2dw12_get_int1_on_int2_map(struct sensor_itf 
*itf, uint8_t *val)
 int lis2dw12_run_self_test(struct sensor_itf *itf, int *result)
 {
     int rc;
-    int16_t base[3], pos[3];
+    /*configure min and max values for reading 5 samples, and accounting for
+     * both negative and positive offset */
+    int min = LIS2DW12_ST_MIN*5*2;
+    int max = LIS2DW12_ST_MAX*5*2;
+
+    int16_t data[3], diff[3] = {0,0,0};
     int i;
-    int16_t change;
-    uint8_t fs;
+    uint8_t prev_config[6];
+    //set config per datasheet, with positive self test mode enabled.
+    uint8_t st_config[] = {0x44, 0x08, 0x40, 0x00, 0x00, 0x10};
 
-    rc = lis2dw12_get_fs(itf, &fs);
+    rc = lis2dw12_readlen(itf, LIS2DW12_REG_CTRL_REG1, prev_config, 6);
     if (rc) {
         return rc;
     }
-
-    /* ensure self test mode is disabled */
-    rc = lis2dw12_set_self_test(itf, LIS2DW12_ST_MODE_DISABLE);
+    rc = lis2dw12_writelen(itf, LIS2DW12_REG_CTRL_REG2, st_config, 5);
+    rc = lis2dw12_writelen(itf, LIS2DW12_REG_CTRL_REG1, st_config, 1);
     if (rc) {
         return rc;
     }
 
-    os_time_delay(OS_TICKS_PER_SEC / 10);
-
-    /* take base reading */
-    rc = lis2dw12_get_data(itf, fs, &(base[0]), &(base[1]), &(base[2]));
+    /* go into self test mode 1 */
+    rc = lis2dw12_set_self_test(itf, LIS2DW12_ST_MODE_MODE1);
     if (rc) {
         return rc;
     }
 
-    /* set self test mode to positive self test */
-    rc = lis2dw12_set_self_test(itf, LIS2DW12_ST_MODE_MODE1);
+    //wait 100ms
+    os_time_delay(OS_TICKS_PER_SEC / 100);
+    rc = lis2dw12_get_data(itf, 2, &(data[0]), &(data[1]), &(data[2]));
     if (rc) {
         return rc;
     }
 
-    os_time_delay(OS_TICKS_PER_SEC / 10);
+    /* take positive offset reading */
+    for(i=0; i<5; i++)
+    {
+
+        rc = lis2dw12_get_data(itf, 2, &(data[0]), &(data[1]), &(data[2]));
+        if (rc) {
+            return rc;
+        }
+        diff[0] += data[0];
+        diff[1] += data[1];
+        diff[2] += data[2];
+        //wait at least 20 ms
+        os_time_delay(OS_TICKS_PER_SEC / 50 + 1);
+    }
 
-    /* take self test reading */
-    rc = lis2dw12_get_data(itf, fs, &(pos[0]), &(pos[1]), &(pos[2]));
+    /* go into self test mode 2 */
+    rc = lis2dw12_set_self_test(itf, LIS2DW12_ST_MODE_MODE2);
     if (rc) {
         return rc;
     }
 
-    /* disable self test mod */
-    rc = lis2dw12_set_self_test(itf, LIS2DW12_ST_MODE_DISABLE);
+    os_time_delay(OS_TICKS_PER_SEC / 50 + 1);
+    rc = lis2dw12_get_data(itf, 2, &(data[0]), &(data[1]), &(data[2]));
     if (rc) {
         return rc;
     }
 
-    /* calculate accel data difference */
-    change = 0;
-    for(i = 0; i < 3; i++) {
-        change += pos[i] - base[i];
-    }
+    /* take negative offset reading */
+    for(i=0; i<5; i++)
+        {
 
-    if ((change > 70) && (change < 1500)) {
-        *result = 0;
-    } else {
-        *result = -1;
+            rc = lis2dw12_get_data(itf, 2, &(data[0]), &(data[1]), &(data[2]));
+            if (rc) {
+                return rc;
+            }
+            diff[0] -= data[0];
+            diff[1] -= data[1];
+            diff[2] -= data[2];
+            //wait at least 20 ms
+            os_time_delay(OS_TICKS_PER_SEC / 50 + 1);
+        }
+
+    /* disable self test mod */
+    rc = lis2dw12_writelen(itf, LIS2DW12_REG_CTRL_REG1, prev_config, 6);
+        if (rc) {
+            return rc;
+        }
+
+    /* compare values to thresholds */
+    *result = 0;
+    for(i = 0; i < 3; i++) {
+       if((diff[i] < min) || (diff[i] > max)) {
 
 Review comment:
   coding_style: A space is needed after `if`

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to