]> git.itanic.dy.fi Git - BME280_driver/blobdiff - examples/bsd_userspace.c
Updated interface design and examples.
[BME280_driver] / examples / bsd_userspace.c
index 5e32da3562fbfa24c49e3e6c0ae43f72ea8a3a69..411a459aef6ce8db7d68f7a961e2754ceddaf871 100644 (file)
@@ -4,7 +4,15 @@
  * tested: NanoPi NEO.\r
  * Use like: ./bme280 /dev/iic0\r
  */\r
-#include "bme280.h"\r
+\r
+#ifdef __KERNEL__\r
+#include <sys/ioctl.h>\r
+#include <dev/iicbus/iic.h>\r
+#endif\r
+\r
+/******************************************************************************/\r
+/*!                         System header files                               */\r
+\r
 #include <string.h>\r
 #include <stdio.h>\r
 #include <stdlib.h>\r
 #include <sys/types.h>\r
 #include <fcntl.h>\r
 \r
-#include <sys/ioctl.h>\r
-#include <dev/iicbus/iic.h>\r
-\r
-int fd;\r
+/******************************************************************************/\r
+/*!                         Own header files                                  */\r
+#include <iic.h>\r
+#include "bme280.h"\r
 \r
-int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);\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
-void print_sensor_data(struct bme280_data *comp_data);\r
-int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);\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
+\r
+/*!\r
+ *  @brief Function for reading the sensor's registers through I2C bus.\r
+ *\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
+ *  @retval 0 -> Success\r
+ *  @retval > 0 -> Failure Info\r
+ *\r
+ */\r
+int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr);\r
+\r
+/*!\r
+ *  @brief Function that creates a mandatory delay required in some of the APIs.\r
+ *\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_us(uint32_t period, void *intf_ptr);\r
+\r
+/*!\r
+ *  @brief Function for writing the sensor's registers through I2C bus.\r
+ *\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
+ *  @retval BME280_OK -> Success\r
+ *  @retval BME280_E_COMM_FAIL -> Communication failure.\r
+ *\r
+ */\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 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
+static void print_sensor_data(struct bme280_data *comp_data);\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
+static int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);\r
 \r
-int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)\r
+/*!\r
+ * @brief This function reading the sensor's registers through I2C bus.\r
+ */\r
+int8_t user_i2c_read(uint8_t reg_addr, uint8_t *data, uint32_t len, void *intf_ptr)\r
 {\r
+    struct identifier id;\r
+\r
+    id = *((struct identifier *)intf_ptr);\r
+\r
     struct iic_msg msgs[2] = {\r
-        {id << 1 | IIC_M_WR, IIC_M_WR, 1, &reg_addr},\r
-        {id << 1 | IIC_M_RD, IIC_M_RD, len, data},\r
+        { id.dev_addr << 1 | IIC_M_WR, IIC_M_WR, 1, &reg_addr }, { id.dev_addr << 1 | IIC_M_RD, IIC_M_RD, len, data },\r
     };\r
 \r
-    struct iic_rdwr_data rdwr_data = {msgs, 2};\r
+    struct iic_rdwr_data rdwr_data = { msgs, 2 };\r
+\r
+    int error = ioctl(id.fd, I2CRDWR, &rdwr_data);\r
 \r
-    int error = ioctl(fd, I2CRDWR, &rdwr_data);\r
-    if (error) {\r
+    if (error)\r
+    {\r
         return BME280_E_COMM_FAIL;\r
     }\r
 \r
     return BME280_OK;\r
 }\r
 \r
-\r
-void user_delay_ms(uint32_t period)\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_us(uint32_t period, void *intf_ptr)\r
 {\r
-    usleep(period * 1000);\r
+    usleep(period);\r
 }\r
 \r
-\r
-int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)\r
+/*!\r
+ * @brief This function for writing the sensor's registers through I2C bus.\r
+ */\r
+int8_t user_i2c_write(uint8_t reg_addr, const uint8_t *data, uint32_t len, void *intf_ptr)\r
 {\r
-    uint8_t *buf = malloc( (1 + len) * sizeof(uint8_t) );\r
-    if (buf == NULL) {\r
+    struct identifier id;\r
+\r
+    id = *((struct identifier *)intf_ptr);\r
+\r
+    uint8_t *buf = malloc((1 + len) * sizeof(uint8_t));\r
+\r
+    if (buf == NULL)\r
+    {\r
         return BME280_E_COMM_FAIL;\r
     }\r
 \r
     buf[0] = reg_addr;\r
 \r
-    for (int i = 0; i < len; i++) {\r
-        buf[i+1] = data[i];\r
+    for (uint8_t i = 0; i < len; i++)\r
+    {\r
+        buf[i + 1] = data[i];\r
     }\r
 \r
     struct iic_msg msg;\r
 \r
-    msg.slave = id << 1 | !IIC_M_RD;\r
+    msg.slave = id.dev_addr << 1 | !IIC_M_RD;\r
     msg.flags = !IIC_M_RD;\r
     msg.len = (1 + len) * sizeof(uint8_t);\r
     msg.buf = buf;\r
 \r
-    struct iic_rdwr_data rdwr_data = {&msg, 1};\r
+    struct iic_rdwr_data rdwr_data = { &msg, 1 };\r
+\r
+    int error = ioctl(id.fd, I2CRDWR, &rdwr_data);\r
 \r
-    int error = ioctl(fd, I2CRDWR, &rdwr_data);\r
-    if (error) {\r
+    if (error)\r
+    {\r
         free(buf);\r
+\r
         return BME280_E_COMM_FAIL;\r
     }\r
 \r
@@ -81,10 +197,13 @@ int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
     return BME280_OK;\r
 }\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
@@ -103,7 +222,9 @@ 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
+/*!\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
@@ -122,10 +243,12 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
     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
     /* Continuously stream sensor data */\r
     while (1)\r
     {\r
@@ -135,55 +258,58 @@ int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
             fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);\r
             break;\r
         }\r
+\r
         /* Wait for the measurement to complete and print data @25Hz */\r
-        dev->delay_ms(40);\r
+        dev->delay_us(40000, dev->intf_ptr);\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
-        dev->delay_ms(1000);\r
+        dev->delay_us(1000000, dev->intf_ptr);\r
     }\r
 \r
     return rslt;\r
 }\r
 \r
-\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
     int8_t rslt = BME280_OK;\r
 \r
+    struct identifier id;\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
+    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
+    /*\r
+     * make sure to select BME280_I2C_ADDR_PRIM\r
+     * or BME280_I2C_ADDR_SEC as needed\r
+     */\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_ms = user_delay_ms;\r
+    dev.delay_us = user_delay_us;\r
 \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
+    /* Update interface pointer with the structure that contains both device address and file descriptor */\r
+    dev.intf_ptr = &id;\r
 \r
     rslt = bme280_init(&dev);\r
     if (rslt != BME280_OK)\r
@@ -198,5 +324,6 @@ int main(int argc, char* argv[])
         fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);\r
         exit(1);\r
     }\r
+\r
     return 0;\r
 }\r