]> git.itanic.dy.fi Git - BME280_driver/blobdiff - examples/linux_userspace.c
Updated license and formatting
[BME280_driver] / examples / linux_userspace.c
index 6ce36618cad0918a980ad81dffd2165b7eafbfff..c360eb07f2ad9350812e5f99ac6abb49a24da810 100644 (file)
-/*\r
- * Linux userspace test code, simple and mose code directy from the doco.\r
+/**\r
+* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.\r
+*\r
+* BSD-3-Clause\r
+*\r
+* Redistribution and use in source and binary forms, with or without\r
+* modification, are permitted provided that the following conditions are met:\r
+*\r
+* 1. Redistributions of source code must retain the above copyright\r
+*    notice, this list of conditions and the following disclaimer.\r
+*\r
+* 2. Redistributions in binary form must reproduce the above copyright\r
+*    notice, this list of conditions and the following disclaimer in the\r
+*    documentation and/or other materials provided with the distribution.\r
+*\r
+* 3. Neither the name of the copyright holder nor the names of its\r
+*    contributors may be used to endorse or promote products derived from\r
+*    this software without specific prior written permission.\r
+*\r
+* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\r
+* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\r
+* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS\r
+* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE\r
+* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,\r
+* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\r
+* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\r
+* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\r
+* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\r
+* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\r
+* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\r
+* POSSIBILITY OF SUCH DAMAGE.\r
+*\r
+* @file linux_userspace.c\r
+* @date 10/01/2020\r
+* @version\r
+*\r
+*/\r
+\r
+/*!\r
+ * @ingroup bme280GroupExample\r
+ * @defgroup bme280GroupExample linux_userspace\r
+ * @brief Linux userspace test code, simple and mose code directly from the doco.\r
  * compile like this: gcc linux_userspace.c ../bme280.c -I ../ -o bme280\r
  * tested: Raspberry Pi.\r
  * Use like: ./bme280 /dev/i2c-0\r
+ * \include linux_userspace.c\r
  */\r
-#include "bme280.h"\r
-#include <string.h>\r
-#include <stdio.h>\r
-#include <stdlib.h>\r
-#include <unistd.h>\r
+\r
 #ifdef __KERNEL__\r
 #include <linux/i2c-dev.h>\r
 #include <sys/ioctl.h>\r
 #endif\r
+\r
+/******************************************************************************/\r
+/*!                         System header files                               */\r
+#include <string.h>\r
+#include <stdio.h>\r
+#include <stdlib.h>\r
+#include <unistd.h>\r
 #include <sys/types.h>\r
 #include <fcntl.h>\r
 \r
+/******************************************************************************/\r
+/*!                         Own header files                                  */\r
+#include "bme280.h"\r
+\r
+/*****************************************************************************/\r
+/*!                         Global variables                                 */\r
 int fd;\r
 \r
-int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);\r
+/****************************************************************************/\r
+/*!                         Functions                                       */\r
+\r
+/*!\r
+ *  @brief Function that creates a mandatory delay required in some of the APIs.\r
+ *\r
+ *  @param[in] period  : The required wait time in microseconds.\r
+ *  @return void.\r
+ *\r
+ */\r
 void user_delay_ms(uint32_t period);\r
-int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);\r
+\r
+/*!\r
+ * @brief Function for print the temperature, humidity and pressure data.\r
+ *\r
+ * @param[out] comp_data    :   Structure instance of bme280_data\r
+ *\r
+ * @note Sensor data whose can be read\r
+ *\r
+ * sens_list\r
+ * --------------\r
+ * Pressure\r
+ * Temperature\r
+ * Humidity\r
+ *\r
+ */\r
 void print_sensor_data(struct bme280_data *comp_data);\r
+\r
+/*!\r
+ *  @brief Function for reading the sensor's registers through I2C bus.\r
+ *\r
+ *  @param[in] id       : Sensor I2C address.\r
+ *  @param[in] reg_addr : Register address.\r
+ *  @param[out] data    : Pointer to the data buffer to store the read data.\r
+ *  @param[in] len      : No of bytes to read.\r
+ *\r
+ *  @return Status of execution\r
+ *\r
+ *  @retval 0 -> Success\r
+ *  @retval > 0 -> Failure Info\r
+ *\r
+ */\r
+int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);\r
+\r
+/*!\r
+ *  @brief Function for writing the sensor's registers through I2C bus.\r
+ *\r
+ *  @param[in] id       : Sensor I2C address.\r
+ *  @param[in] reg_addr : Register address.\r
+ *  @param[in] data     : Pointer to the data buffer whose value is to be written.\r
+ *  @param[in] len      : No of bytes to write.\r
+ *\r
+ *  @return Status of execution\r
+ *\r
+ *  @retval BME280_OK -> Success\r
+ *  @retval BME280_E_COMM_FAIL -> Communication failure.\r
+ *\r
+ */\r
+int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);\r
+\r
+/*!\r
+ * @brief Function reads temperature, humidity and pressure data in forced mode.\r
+ *\r
+ * @param[in] dev   :   Structure instance of bme280_dev.\r
+ *\r
+ * @return Result of API execution status\r
+ *\r
+ * @retval BME280_OK - Success.\r
+ * @retval BME280_E_NULL_PTR - Error: Null pointer error\r
+ * @retval BME280_E_COMM_FAIL - Error: Communication fail error\r
+ * @retval BME280_E_NVM_COPY_FAILED - Error: NVM copy failed\r
+ *\r
+ */\r
 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);\r
 \r
+/*!\r
+ * @brief This function starts execution of the program.\r
+ */\r
+int main(int argc, char* argv[])\r
+{\r
+    struct bme280_dev dev;\r
+\r
+    /* Variable to define the result */\r
+    int8_t rslt = BME280_OK;\r
+\r
+    if (argc < 2)\r
+    {\r
+        fprintf(stderr, "Missing argument for i2c bus.\n");\r
+        exit(1);\r
+    }\r
+\r
+    /* Make sure to select BME280_I2C_ADDR_PRIM or BME280_I2C_ADDR_SEC as needed */\r
+    dev.dev_id = BME280_I2C_ADDR_PRIM;\r
+\r
+    /* dev.dev_id = BME280_I2C_ADDR_SEC; */\r
+    dev.intf = BME280_I2C_INTF;\r
+    dev.read = user_i2c_read;\r
+    dev.write = user_i2c_write;\r
+    dev.delay_ms = user_delay_ms;\r
+\r
+    if ((fd = open(argv[1], O_RDWR)) < 0)\r
+    {\r
+        fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);\r
+        exit(1);\r
+    }\r
+\r
+#ifdef __KERNEL__\r
+    if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)\r
+    {\r
+        fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");\r
+        exit(1);\r
+    }\r
+#endif\r
+\r
+    /* Initialize the bme280 */\r
+    rslt = bme280_init(&dev);\r
+    if (rslt != BME280_OK)\r
+    {\r
+        fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);\r
+        exit(1);\r
+    }\r
+\r
+    rslt = stream_sensor_data_forced_mode(&dev);\r
+    if (rslt != BME280_OK)\r
+    {\r
+        fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);\r
+        exit(1);\r
+    }\r
+\r
+    return 0;\r
+}\r
+\r
+/*!\r
+ * @brief This function reading the sensor's registers through I2C bus.\r
+ */\r
 int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)\r
 {\r
     write(fd, &reg_addr, 1);\r
@@ -32,11 +211,19 @@ int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
     return 0;\r
 }\r
 \r
+/*!\r
+ * @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the\r
+ * APIs\r
+ */\r
 void user_delay_ms(uint32_t period)\r
 {\r
+    /* Milliseconds convert to microseconds */\r
     usleep(period * 1000);\r
 }\r
 \r
+/*!\r
+ * @brief This function for writing the sensor's registers through I2C bus.\r
+ */\r
 int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)\r
 {\r
     int8_t *buf;\r
@@ -45,14 +232,22 @@ int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
     buf[0] = reg_addr;\r
     memcpy(buf + 1, data, len);\r
     if (write(fd, buf, len + 1) < len)\r
+    {\r
         return BME280_E_COMM_FAIL;\r
+    }\r
+\r
     free(buf);\r
-       return BME280_OK;\r
+\r
+    return BME280_OK;\r
 }\r
 \r
+/*!\r
+ * @brief This API used to print the sensor temperature, pressure and humidity data.\r
+ */\r
 void print_sensor_data(struct bme280_data *comp_data)\r
 {\r
     float temp, press, hum;\r
+\r
 #ifdef BME280_FLOAT_ENABLE\r
     temp = comp_data->temperature;\r
     press = 0.01 * comp_data->pressure;\r
@@ -71,10 +266,21 @@ void print_sensor_data(struct bme280_data *comp_data)
     printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);\r
 }\r
 \r
+/*!\r
+ * @brief This API reads the sensor temperature, pressure and humidity data in forced mode.\r
+ */\r
 int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)\r
 {\r
-    int8_t rslt;\r
-    uint8_t settings_sel;\r
+    /* Variable to define the result */\r
+    int8_t rslt = BME280_OK;\r
+\r
+    /* Variable to define the selecting sensors */\r
+    uint8_t settings_sel = 0;\r
+\r
+    /* Variable to store minimum wait time between consecutive measurement in force mode */\r
+    uint32_t req_delay;\r
+\r
+    /* Structure to get the pressure, temperature and humidity values */\r
     struct bme280_data comp_data;\r
 \r
     /* Recommended mode of operation: Indoor navigation */\r
@@ -85,87 +291,43 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
 \r
     settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;\r
 \r
+    /* Set the sensor settings */\r
     rslt = bme280_set_sensor_settings(settings_sel, dev);\r
-       if (rslt != BME280_OK)\r
-       {\r
-               fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);\r
-               return rslt;\r
-       }\r
+    if (rslt != BME280_OK)\r
+    {\r
+        fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);\r
+\r
+        return rslt;\r
+    }\r
 \r
     printf("Temperature, Pressure, Humidity\n");\r
+\r
+    /*Calculate the minimum delay required between consecutive measurement based upon the sensor enabled\r
+     *  and the oversampling configuration. */\r
+    req_delay = bme280_cal_meas_delay(&dev->settings);\r
+\r
     /* Continuously stream sensor data */\r
     while (1)\r
     {\r
+        /* Set the sensor to forced mode */\r
         rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);\r
         if (rslt != BME280_OK)\r
         {\r
             fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);\r
             break;\r
         }\r
-        /* Wait for the measurement to complete and print data @25Hz */\r
-        dev->delay_ms(40);\r
+\r
+        /* Wait for the measurement to complete and print data */\r
+        dev->delay_ms(req_delay);\r
         rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);\r
         if (rslt != BME280_OK)\r
         {\r
             fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);\r
             break;\r
         }\r
+\r
         print_sensor_data(&comp_data);\r
     }\r
 \r
     return rslt;\r
 }\r
-\r
-int main(int argc, char* argv[])\r
-{\r
-    struct bme280_dev dev;\r
-    int8_t rslt = BME280_OK;\r
-\r
-    if (argc < 2)\r
-    {\r
-        fprintf(stderr, "Missing argument for i2c bus.\n");\r
-        exit(1);\r
-    }\r
-    \r
-    // make sure to select BME280_I2C_ADDR_PRIM\r
-    // or BME280_I2C_ADDR_SEC as needed\r
-    dev.dev_id =\r
-#if 1\r
-        BME280_I2C_ADDR_PRIM\r
-#else\r
-        BME280_I2C_ADDR_SEC\r
-#endif\r
-;\r
-\r
-    dev.intf = BME280_I2C_INTF;\r
-    dev.read = user_i2c_read;\r
-    dev.write = user_i2c_write;\r
-    dev.delay_ms = user_delay_ms;\r
-\r
-    if ((fd = open(argv[1], O_RDWR)) < 0)\r
-    {\r
-        fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);\r
-    exit(1);\r
-    }\r
-#ifdef __KERNEL__\r
-    if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)\r
-    {\r
-        fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");\r
-        exit(1);\r
-    }\r
-#endif\r
-\r
-    rslt = bme280_init(&dev);\r
-    if (rslt != BME280_OK)\r
-    {\r
-        fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);\r
-        exit(1);\r
-}\r
-    rslt = stream_sensor_data_forced_mode(&dev);\r
-    if (rslt != BME280_OK)\r
-    {\r
-        fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);\r
-        exit(1);\r
-    }\r
-    return 0;\r
-}\r