You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2019/08/06 22:12:16 UTC

[GitHub] [mynewt-core] kasjer commented on a change in pull request #1946: hw/drivers/lps33hw:Added support to FIFO

kasjer commented on a change in pull request #1946: hw/drivers/lps33hw:Added support to FIFO
URL: https://github.com/apache/mynewt-core/pull/1946#discussion_r311293894
 
 

 ##########
 File path: hw/drivers/sensors/lps33hw/src/lps33hw.c
 ##########
 @@ -1160,6 +1194,233 @@ lps33hw_sensor_read(struct sensor *sensor, sensor_type_t type,
     return rc;
 }
 
+static int lps33hw_read_fifo(struct sensor_itf *itf,
+			     struct sensor_temp_data *std,
+			     struct sensor_press_data *spd)
+{
+    int rc;
+    uint8_t fifo_data[5];
+    int32_t int_press;
+    int16_t int_temp;
+
+    rc = lps33hw_get_regs(itf, LPS33HW_PRESS_OUT, 5, fifo_data);
+    if (rc) {
+	return rc;
+    }
+
+    int_press = ((int8_t)fifo_data[2] << 16) |
+			(fifo_data[1] << 8) | 
+			 fifo_data[0];
+    spd->spd_press_is_valid = 1;
+    spd->spd_press = lps33hw_reg_to_pa(int_press);
+
+    int_temp = ((int8_t)fifo_data[4] << 8) | fifo_data[3];
+    std->std_temp_is_valid = 1;
+    std->std_temp = lps33hw_reg_to_degc(int_temp);
+
+    return 0;
+}
+
+static int lps33hw_wait_interrupt(struct lps33hw_int *interrupt)
+{
+    os_error_t error;
+
+    error = os_sem_pend(&interrupt->wait, LPS33HW_MAX_INT_WAIT);
+    if (error == OS_TIMEOUT) {
+	return error;
+    }
+    assert(error == OS_OK);
+
+    return OS_OK;
+}
+
+/**
+ * Wake tasks waiting on interrupt->wait lock
+ *
+ * This call resume task waiting on lps33hw_wake_interrupt lock
+ *
+ * @param the interrupt structure
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static void lps33hw_wake_interrupt(struct lps33hw_int *interrupt)
+{
+    os_error_t error;
+
+    /* Release semaphore to wait_interrupt routine */
+    error = os_sem_release(&interrupt->wait);
+    assert(error == OS_OK);
+}
+
+/**
+ * Wake tasks waiting on interrupt->wait lock
+ *
+ * This call resume task waiting on lps33hw_wake_interrupt lock
+ *
+ * @param the interrupt structure
+ *
+ * @return 0 on success, non-zero on failure
+ */
+static void lps33hw_int_irq_handler(void *arg)
+{
+    struct sensor *sensor = arg;
+    struct lps33hw *lps33hw;
+
+    lps33hw = (struct lps33hw *)SENSOR_GET_DEVICE(sensor);
+
+    if(lps33hw->pdd.interrupt) {
+        lps33hw_wake_interrupt(lps33hw->pdd.interrupt);
+    }
+}
+
+/**
+ * Read data samples from FIFO
+ *
+ * FIFO store both SENSOR_TYPE_TEMPERATURE
+ * and SENSOR_TYPE_PRESSURE sample data
+ *
+ * @param sensor The sensor object
+ * @param type sensor type bitmask
+ * @param data_func callback register data function
+ * @param data_arg function arguments
+ * @param time_ms
+ *
+ * @return 0 on success, non-zero error on failure.
+ */
+int lps33hw_stream_read(struct sensor *sensor, sensor_type_t sensor_type,
+                        sensor_data_func_t read_func, void *data_arg,
+                        uint32_t time_ms)
+{
+    struct lps33hw *lps33hw;
+    struct lps33hw_private_driver_data *pdd;
+    struct sensor_itf *itf;
+    uint8_t fifo_samples;
+    os_time_t time_ticks;
+    os_time_t stop_ticks = 0;
+    int rc;
+    struct sensor_temp_data std;
+    struct sensor_press_data spd;
+
+    /* Temperature/Pressure only */
+    if (!(sensor_type & SENSOR_TYPE_TEMPERATURE) &&
+        !(sensor_type & SENSOR_TYPE_PRESSURE)) {
+        return SYS_EINVAL;
+    }
+
+    lps33hw  = (struct lps33hw *)SENSOR_GET_DEVICE(sensor);
+    if (lps33hw->cfg.read_mode != LPS33HW_READ_STREAM) {
+        return SYS_EINVAL;
+    }
+
+    pdd = &lps33hw->pdd;
+
+    itf = SENSOR_GET_ITF(sensor);
+
+    /* Enable FIFO */
+    rc = lps33hw_set_value(itf, LPS33HW_FIFO_CTRL_MODE,
+			    LPS33HW_FIFO_CONTINUOUS);
+    if (rc) {
+        goto err;
+    }
+
+    rc = lps33hw_set_value(itf, LPS33HW_CTRL_REG2_FIFO_EN, 1);
+    if (rc) {
+        goto err;
+    }
+
+    if (pdd->interrupt) {
+	goto err;
+    }
+
+    rc = lps33hw_enable_interrupt(sensor, lps33hw_int_irq_handler, sensor);
+    if (rc) {
+        goto err;
+    }
+
+    /* Calculate timeout */
+    if (time_ms > 0) {
+        rc = os_time_ms_to_ticks(time_ms, &time_ticks);
+        if (rc) {
+            goto err;
+        }
+        stop_ticks = os_time_get() + time_ticks;
+    }
+
+    for (;;) {
+	/* Force at least one read for cases when fifo is disabled */
+	rc = lps33hw_wait_interrupt(lps33hw->pdd.interrupt);
+	if (rc) {
+	    goto err;
+	}
+
+	rc = lps33hw_get_value(itf, LPS33HW_FIFO_FSS, &fifo_samples);
+	if (rc) {
+	    goto err;
+	}
+
+	if (fifo_samples == 0)
+	    continue;
+
+	do {
+	    /* Read fifo samples */
+	    rc = lps33hw_read_fifo(itf, &std, &spd);
+	    if (rc) {
+		goto err;
+	    }
+
+	    if (sensor_type & SENSOR_TYPE_PRESSURE) {
+		rc = read_func(sensor, data_arg, &spd, SENSOR_TYPE_PRESSURE);
+		if (rc) {
+		    goto err;
+		}
+	    }
+	    if (sensor_type & SENSOR_TYPE_TEMPERATURE) {
+		rc = read_func(sensor, data_arg, &std, SENSOR_TYPE_TEMPERATURE);
+		if (rc) {
+		    goto err;
+		}
+	    }
+	fifo_samples--;
 
 Review comment:
   wrong indentation

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


With regards,
Apache Git Services