]> git.itanic.dy.fi Git - BME280_driver/blobdiff - examples/linux_userspace.c
Updated interface design and examples.
[BME280_driver] / examples / linux_userspace.c
index 62c8eb5cce8ac05ed257852fe7ef0ba36a8a426a..0b8fc9a8305ede0843c746ba5776475f52232c85 100644 (file)
@@ -1,13 +1,18 @@
-/*\r
- * Copyright (C) 2020 Bosch Sensortec GmbH\r
- *\r
- * The license is available at root folder\r
+/**\\r
+ * Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.\r
  *\r
+ * SPDX-License-Identifier: BSD-3-Clause\r
+ **/\r
+\r
+/**\r
+ * \ingroup bme280\r
+ * \defgroup bme280Examples Examples\r
+ * @brief Reference Examples\r
  */\r
 \r
 /*!\r
- * @ingroup bme280GroupExample\r
- * @defgroup bme280GroupExample linux_userspace\r
+ * @ingroup bme280Examples\r
+ * @defgroup bme280GroupExampleLU 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
 /*!                         Own header files                                  */\r
 #include "bme280.h"\r
 \r
-/*****************************************************************************/\r
-/*!                         Global variables                                 */\r
-int fd;\r
+/******************************************************************************/\r
+/*!                               Structures                                  */\r
+\r
+/* Structure that contains identifier details used in example */\r
+struct identifier\r
+{\r
+    /* Variable to hold device address */\r
+    uint8_t dev_addr;\r
+\r
+    /* Variable that contains file descriptor */\r
+    int8_t fd;\r
+};\r
 \r
 /****************************************************************************/\r
 /*!                         Functions                                       */\r
@@ -43,11 +57,13 @@ int fd;
 /*!\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
+ * @param[in] period              : Delay in microseconds.\r
+ * @param[in, out] intf_ptr       : Void pointer that can enable the linking of descriptors\r
+ *                                  for interface related call backs\r
  *  @return void.\r
  *\r
  */\r
-void user_delay_ms(uint32_t period);\r
+void user_delay_us(uint32_t period, void *intf_ptr);\r
 \r
 /*!\r
  * @brief Function for print the temperature, humidity and pressure data.\r
@@ -68,10 +84,11 @@ void print_sensor_data(struct bme280_data *comp_data);
 /*!\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
+ *  @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
+ *  @param[in, out] intf_ptr  : Void pointer that can enable the linking of descriptors\r
+ *                                  for interface related call backs.\r
  *\r
  *  @return Status of execution\r
  *\r
@@ -79,15 +96,16 @@ void print_sensor_data(struct bme280_data *comp_data);
  *  @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
+int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr);\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
+ *  @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
+ *  @param[in, out] intf_ptr  : Void pointer that can enable the linking of descriptors\r
+ *                                  for interface related call backs\r
  *\r
  *  @return Status of execution\r
  *\r
@@ -95,7 +113,7 @@ int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
  *  @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
+int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr);\r
 \r
 /*!\r
  * @brief Function reads temperature, humidity and pressure data in forced mode.\r
@@ -119,6 +137,8 @@ int main(int argc, char* argv[])
 {\r
     struct bme280_dev dev;\r
 \r
+    struct identifier id;\r
+\r
     /* Variable to define the result */\r
     int8_t rslt = BME280_OK;\r
 \r
@@ -128,29 +148,32 @@ int main(int argc, char* argv[])
         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
+    if ((id.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
+    if (ioctl(id.fd, I2C_SLAVE, id.dev_addr) < 0)\r
     {\r
         fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");\r
         exit(1);\r
     }\r
+\r
 #endif\r
 \r
+    /* Make sure to select BME280_I2C_ADDR_PRIM or BME280_I2C_ADDR_SEC as needed */\r
+    id.dev_addr = BME280_I2C_ADDR_PRIM;\r
+\r
+    dev.intf = BME280_I2C_INTF;\r
+    dev.read = user_i2c_read;\r
+    dev.write = user_i2c_write;\r
+    dev.delay_us = user_delay_us;\r
+\r
+    /* Update interface pointer with the structure that contains both device address and file descriptor */\r
+    dev.intf_ptr = &id;\r
+\r
     /* Initialize the bme280 */\r
     rslt = bme280_init(&dev);\r
     if (rslt != BME280_OK)\r
@@ -172,10 +195,14 @@ int main(int argc, char* argv[])
 /*!\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
+int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr)\r
 {\r
-    write(fd, &reg_addr, 1);\r
-    read(fd, data, len);\r
+    struct identifier id;\r
+\r
+    id = *((struct identifier *)intf_ptr);\r
+\r
+    write(id.fd, &reg_addr, 1);\r
+    read(id.fd, data, len);\r
 \r
     return 0;\r
 }\r
@@ -184,23 +211,25 @@ int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
  * @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
+void user_delay_us(uint32_t period, void *intf_ptr)\r
 {\r
-    /* Milliseconds convert to microseconds */\r
-    usleep(period * 1000);\r
+    usleep(period);\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
+int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr)\r
 {\r
-    int8_t *buf;\r
+    uint8_t *buf;\r
+    struct identifier id;\r
+\r
+    id = *((struct identifier *)intf_ptr);\r
 \r
     buf = malloc(len + 1);\r
     buf[0] = reg_addr;\r
     memcpy(buf + 1, data, len);\r
-    if (write(fd, buf, len + 1) < len)\r
+    if (write(id.fd, buf, len + 1) < (uint16_t)len)\r
     {\r
         return BME280_E_COMM_FAIL;\r
     }\r
@@ -287,7 +316,7 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
         }\r
 \r
         /* Wait for the measurement to complete and print data */\r
-        dev->delay_ms(req_delay);\r
+        dev->delay_us(req_delay, dev->intf_ptr);\r
         rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);\r
         if (rslt != BME280_OK)\r
         {\r