Browse Source

Imported Bosch BME280 driver

Added ESP-IDF example for I2C bus
spi_display
Christian Loch 4 years ago
parent
commit
b12deb401c
4 changed files with 2490 additions and 25 deletions
  1. +1496
    -0
      main/bme280.c
  2. +246
    -0
      main/bme280.h
  3. +391
    -0
      main/bme280_defs.h
  4. +357
    -25
      main/main.c

+ 1496
- 0
main/bme280.c
File diff suppressed because it is too large
View File


+ 246
- 0
main/bme280.h View File

@@ -0,0 +1,246 @@
/**
* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
*
* BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bme280.h
* @date 21/01/2020
* @version 3.4.2
*
*/

/*! @file bme280.h
* @brief Sensor driver for BME280 sensor
*/

/*!
* @defgroup BME280 SENSOR API
*/
#ifndef BME280_H_
#define BME280_H_

/*! CPP guard */
#ifdef __cplusplus
extern "C" {
#endif

/* Header includes */
#include "bme280_defs.h"

/*!
* @brief This API is the entry point.
* It reads the chip-id and calibration data from the sensor.
*
* @param[in,out] dev : Structure instance of bme280_dev
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_init(struct bme280_dev *dev);

/*!
* @brief This API writes the given data to the register address
* of the sensor.
*
* @param[in] reg_addr : Register address from where the data to be written.
* @param[in] reg_data : Pointer to data buffer which is to be written
* in the sensor.
* @param[in] len : No of bytes of data to write..
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_set_regs(uint8_t *reg_addr, const uint8_t *reg_data, uint8_t len, const struct bme280_dev *dev);

/*!
* @brief This API reads the data from the given register address of the sensor.
*
* @param[in] reg_addr : Register address from where the data to be read
* @param[out] reg_data : Pointer to data buffer to store the read data.
* @param[in] len : No of bytes of data to be read.
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const struct bme280_dev *dev);

/*!
* @brief This API sets the oversampling, filter and standby duration
* (normal mode) settings in the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] desired_settings : Variable used to select the settings which
* are to be set in the sensor.
*
* @note : Below are the macros to be used by the user for selecting the
* desired settings. User can do OR operation of these macros for configuring
* multiple settings.
*
* Macros | Functionality
* -----------------------|----------------------------------------------
* BME280_OSR_PRESS_SEL | To set pressure oversampling.
* BME280_OSR_TEMP_SEL | To set temperature oversampling.
* BME280_OSR_HUM_SEL | To set humidity oversampling.
* BME280_FILTER_SEL | To set filter setting.
* BME280_STANDBY_SEL | To set standby duration setting.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error.
*/
int8_t bme280_set_sensor_settings(uint8_t desired_settings, const struct bme280_dev *dev);

/*!
* @brief This API gets the oversampling, filter and standby duration
* (normal mode) settings from the sensor.
*
* @param[in,out] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error.
*/
int8_t bme280_get_sensor_settings(struct bme280_dev *dev);

/*!
* @brief This API sets the power mode of the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[in] sensor_mode : Variable which contains the power mode to be set.
*
* sensor_mode | Macros
* ---------------------|-------------------
* 0 | BME280_SLEEP_MODE
* 1 | BME280_FORCED_MODE
* 3 | BME280_NORMAL_MODE
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_set_sensor_mode(uint8_t sensor_mode, const struct bme280_dev *dev);

/*!
* @brief This API gets the power mode of the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
* @param[out] sensor_mode : Pointer variable to store the power mode.
*
* sensor_mode | Macros
* ---------------------|-------------------
* 0 | BME280_SLEEP_MODE
* 1 | BME280_FORCED_MODE
* 3 | BME280_NORMAL_MODE
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_get_sensor_mode(uint8_t *sensor_mode, const struct bme280_dev *dev);

/*!
* @brief This API performs the soft reset of the sensor.
*
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error.
*/
int8_t bme280_soft_reset(const struct bme280_dev *dev);

/*!
* @brief This API reads the pressure, temperature and humidity data from the
* sensor, compensates the data and store it in the bme280_data structure
* instance passed by the user.
*
* @param[in] sensor_comp : Variable which selects which data to be read from
* the sensor.
*
* sensor_comp | Macros
* ------------|-------------------
* 1 | BME280_PRESS
* 2 | BME280_TEMP
* 4 | BME280_HUM
* 7 | BME280_ALL
*
* @param[out] comp_data : Structure instance of bme280_data.
* @param[in] dev : Structure instance of bme280_dev.
*
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_get_sensor_data(uint8_t sensor_comp, struct bme280_data *comp_data, struct bme280_dev *dev);

/*!
* @brief This API is used to parse the pressure, temperature and
* humidity data and store it in the bme280_uncomp_data structure instance.
*
* @param[in] reg_data : Contains register data which needs to be parsed
* @param[out] uncomp_data : Contains the uncompensated pressure, temperature
* and humidity data.
*/
void bme280_parse_sensor_data(const uint8_t *reg_data, struct bme280_uncomp_data *uncomp_data);

/*!
* @brief This API is used to compensate the pressure and/or
* temperature and/or humidity data according to the component selected by the
* user.
*
* @param[in] sensor_comp : Used to select pressure and/or temperature and/or
* humidity.
* @param[in] uncomp_data : Contains the uncompensated pressure, temperature and
* humidity data.
* @param[out] comp_data : Contains the compensated pressure and/or temperature
* and/or humidity data.
* @param[in] calib_data : Pointer to the calibration data structure.
*
* @return Result of API execution status.
* @retval zero -> Success / -ve value -> Error
*/
int8_t bme280_compensate_data(uint8_t sensor_comp,
const struct bme280_uncomp_data *uncomp_data,
struct bme280_data *comp_data,
struct bme280_calib_data *calib_data);

/*!
* @brief This API is used to calculate the maximum delay in milliseconds required for the
* temperature/pressure/humidity(which ever are enabled) measurement to complete.
* The delay depends upon the number of sensors enabled and their oversampling configuration.
*
* @param[in] settings : contains the oversampling configurations.
*
* @return delay required in milliseconds.
*/

uint32_t bme280_cal_meas_delay(const struct bme280_settings *settings);

#ifdef __cplusplus
}
#endif /* End of CPP guard */
#endif /* BME280_H_ */
/** @}*/

+ 391
- 0
main/bme280_defs.h View File

@@ -0,0 +1,391 @@
/**
* Copyright (c) 2020 Bosch Sensortec GmbH. All rights reserved.
*
* BSD-3-Clause
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
* IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
* @file bme280_defs.h
* @date 21/01/2020
* @version 3.4.2
*
*/

/*! @file bme280_defs.h
* @brief Sensor driver for BME280 sensor
*/

/*!
* @defgroup BME280 SENSOR API
* @brief
*/
#ifndef BME280_DEFS_H_
#define BME280_DEFS_H_

/********************************************************/
/* header includes */
#ifdef __KERNEL__
#include <linux/types.h>
#include <linux/kernel.h>
#else
#include <stdint.h>
#include <stddef.h>
#endif

/********************************************************/
/*! @name Common macros */
/********************************************************/

#if !defined(UINT8_C) && !defined(INT8_C)
#define INT8_C(x) S8_C(x)
#define UINT8_C(x) U8_C(x)
#endif

#if !defined(UINT16_C) && !defined(INT16_C)
#define INT16_C(x) S16_C(x)
#define UINT16_C(x) U16_C(x)
#endif

#if !defined(INT32_C) && !defined(UINT32_C)
#define INT32_C(x) S32_C(x)
#define UINT32_C(x) U32_C(x)
#endif

#if !defined(INT64_C) && !defined(UINT64_C)
#define INT64_C(x) S64_C(x)
#define UINT64_C(x) U64_C(x)
#endif

/**@}*/
/**\name C standard macros */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *) 0)
#endif
#endif

/********************************************************/

#ifndef BME280_FLOAT_ENABLE

/* #define BME280_FLOAT_ENABLE */
#endif

#ifndef BME280_FLOAT_ENABLE
#ifndef BME280_64BIT_ENABLE
#define BME280_64BIT_ENABLE
#endif
#endif

#ifndef TRUE
#define TRUE UINT8_C(1)
#endif
#ifndef FALSE
#define FALSE UINT8_C(0)
#endif

/**\name I2C addresses */
#define BME280_I2C_ADDR_PRIM UINT8_C(0x76)
#define BME280_I2C_ADDR_SEC UINT8_C(0x77)

/**\name BME280 chip identifier */
#define BME280_CHIP_ID UINT8_C(0x60)

/**\name Register Address */
#define BME280_CHIP_ID_ADDR UINT8_C(0xD0)
#define BME280_RESET_ADDR UINT8_C(0xE0)
#define BME280_TEMP_PRESS_CALIB_DATA_ADDR UINT8_C(0x88)
#define BME280_HUMIDITY_CALIB_DATA_ADDR UINT8_C(0xE1)
#define BME280_PWR_CTRL_ADDR UINT8_C(0xF4)
#define BME280_CTRL_HUM_ADDR UINT8_C(0xF2)
#define BME280_CTRL_MEAS_ADDR UINT8_C(0xF4)
#define BME280_CONFIG_ADDR UINT8_C(0xF5)
#define BME280_DATA_ADDR UINT8_C(0xF7)

/**\name API success code */
#define BME280_OK INT8_C(0)

/**\name API error codes */
#define BME280_E_NULL_PTR INT8_C(-1)
#define BME280_E_DEV_NOT_FOUND INT8_C(-2)
#define BME280_E_INVALID_LEN INT8_C(-3)
#define BME280_E_COMM_FAIL INT8_C(-4)
#define BME280_E_SLEEP_MODE_FAIL INT8_C(-5)
#define BME280_E_NVM_COPY_FAILED INT8_C(-6)

/**\name API warning codes */
#define BME280_W_INVALID_OSR_MACRO INT8_C(1)

/**\name Macros related to size */
#define BME280_TEMP_PRESS_CALIB_DATA_LEN UINT8_C(26)
#define BME280_HUMIDITY_CALIB_DATA_LEN UINT8_C(7)
#define BME280_P_T_H_DATA_LEN UINT8_C(8)

/**\name Sensor power modes */
#define BME280_SLEEP_MODE UINT8_C(0x00)
#define BME280_FORCED_MODE UINT8_C(0x01)
#define BME280_NORMAL_MODE UINT8_C(0x03)

/**\name Macro to combine two 8 bit data's to form a 16 bit data */
#define BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)

#define BME280_SET_BITS(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | \
((data << bitname##_POS) & bitname##_MSK))
#define BME280_SET_BITS_POS_0(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | \
(data & bitname##_MSK))

#define BME280_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \
(bitname##_POS))
#define BME280_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK))

/**\name Macros for bit masking */
#define BME280_SENSOR_MODE_MSK UINT8_C(0x03)
#define BME280_SENSOR_MODE_POS UINT8_C(0x00)

#define BME280_CTRL_HUM_MSK UINT8_C(0x07)
#define BME280_CTRL_HUM_POS UINT8_C(0x00)

#define BME280_CTRL_PRESS_MSK UINT8_C(0x1C)
#define BME280_CTRL_PRESS_POS UINT8_C(0x02)

#define BME280_CTRL_TEMP_MSK UINT8_C(0xE0)
#define BME280_CTRL_TEMP_POS UINT8_C(0x05)

#define BME280_FILTER_MSK UINT8_C(0x1C)
#define BME280_FILTER_POS UINT8_C(0x02)

#define BME280_STANDBY_MSK UINT8_C(0xE0)
#define BME280_STANDBY_POS UINT8_C(0x05)

/**\name Sensor component selection macros
* These values are internal for API implementation. Don't relate this to
* data sheet.
*/
#define BME280_PRESS UINT8_C(1)
#define BME280_TEMP UINT8_C(1 << 1)
#define BME280_HUM UINT8_C(1 << 2)
#define BME280_ALL UINT8_C(0x07)

/**\name Settings selection macros */
#define BME280_OSR_PRESS_SEL UINT8_C(1)
#define BME280_OSR_TEMP_SEL UINT8_C(1 << 1)
#define BME280_OSR_HUM_SEL UINT8_C(1 << 2)
#define BME280_FILTER_SEL UINT8_C(1 << 3)
#define BME280_STANDBY_SEL UINT8_C(1 << 4)
#define BME280_ALL_SETTINGS_SEL UINT8_C(0x1F)

/**\name Oversampling macros */
#define BME280_NO_OVERSAMPLING UINT8_C(0x00)
#define BME280_OVERSAMPLING_1X UINT8_C(0x01)
#define BME280_OVERSAMPLING_2X UINT8_C(0x02)
#define BME280_OVERSAMPLING_4X UINT8_C(0x03)
#define BME280_OVERSAMPLING_8X UINT8_C(0x04)
#define BME280_OVERSAMPLING_16X UINT8_C(0x05)

/**\name Measurement delay calculation macros */
#define BME280_MEAS_OFFSET UINT16_C(1250)
#define BME280_MEAS_DUR UINT16_C(2300)
#define BME280_PRES_HUM_MEAS_OFFSET UINT16_C(575)
#define BME280_MEAS_SCALING_FACTOR UINT16_C(1000)

/**\name Standby duration selection macros */
#define BME280_STANDBY_TIME_0_5_MS (0x00)
#define BME280_STANDBY_TIME_62_5_MS (0x01)
#define BME280_STANDBY_TIME_125_MS (0x02)
#define BME280_STANDBY_TIME_250_MS (0x03)
#define BME280_STANDBY_TIME_500_MS (0x04)
#define BME280_STANDBY_TIME_1000_MS (0x05)
#define BME280_STANDBY_TIME_10_MS (0x06)
#define BME280_STANDBY_TIME_20_MS (0x07)

/**\name Filter coefficient selection macros */
#define BME280_FILTER_COEFF_OFF (0x00)
#define BME280_FILTER_COEFF_2 (0x01)
#define BME280_FILTER_COEFF_4 (0x02)
#define BME280_FILTER_COEFF_8 (0x03)
#define BME280_FILTER_COEFF_16 (0x04)

#define BME280_STATUS_REG_ADDR (0xF3)
#define BME280_SOFT_RESET_COMMAND (0xB6)
#define BME280_STATUS_IM_UPDATE (0x01)

/*!
* @brief Interface selection Enums
*/
enum bme280_intf {
/*! SPI interface */
BME280_SPI_INTF,

/*! I2C interface */
BME280_I2C_INTF
};

/*!
* @brief Type definitions
*/
typedef int8_t (*bme280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len);
typedef void (*bme280_delay_fptr_t)(uint32_t period);

/*!
* @brief Calibration data
*/
struct bme280_calib_data
{
/**
* @ Trim Variables
*/

/**@{*/
uint16_t dig_t1;
int16_t dig_t2;
int16_t dig_t3;
uint16_t dig_p1;
int16_t dig_p2;
int16_t dig_p3;
int16_t dig_p4;
int16_t dig_p5;
int16_t dig_p6;
int16_t dig_p7;
int16_t dig_p8;
int16_t dig_p9;
uint8_t dig_h1;
int16_t dig_h2;
uint8_t dig_h3;
int16_t dig_h4;
int16_t dig_h5;
int8_t dig_h6;
int32_t t_fine;

/**@}*/
};

/*!
* @brief bme280 sensor structure which comprises of temperature, pressure and
* humidity data
*/
#ifdef BME280_FLOAT_ENABLE
struct bme280_data
{
/*! Compensated pressure */
double pressure;

/*! Compensated temperature */
double temperature;

/*! Compensated humidity */
double humidity;
};
#else
struct bme280_data
{
/*! Compensated pressure */
uint32_t pressure;

/*! Compensated temperature */
int32_t temperature;

/*! Compensated humidity */
uint32_t humidity;
};
#endif /* BME280_USE_FLOATING_POINT */

/*!
* @brief bme280 sensor structure which comprises of uncompensated temperature,
* pressure and humidity data
*/
struct bme280_uncomp_data
{
/*! un-compensated pressure */
uint32_t pressure;

/*! un-compensated temperature */
uint32_t temperature;

/*! un-compensated humidity */
uint32_t humidity;
};

/*!
* @brief bme280 sensor settings structure which comprises of mode,
* oversampling and filter settings.
*/
struct bme280_settings
{
/*! pressure oversampling */
uint8_t osr_p;

/*! temperature oversampling */
uint8_t osr_t;

/*! humidity oversampling */
uint8_t osr_h;

/*! filter coefficient */
uint8_t filter;

/*! standby time */
uint8_t standby_time;
};

/*!
* @brief bme280 device structure
*/
struct bme280_dev
{
/*! Chip Id */
uint8_t chip_id;

/*! Device Id */
uint8_t dev_id;

/*! SPI/I2C interface */
enum bme280_intf intf;

/*! Read function pointer */
bme280_com_fptr_t read;

/*! Write function pointer */
bme280_com_fptr_t write;

/*! Delay function pointer */
bme280_delay_fptr_t delay_ms;

/*! Trim data */
struct bme280_calib_data calib_data;

/*! Sensor settings */
struct bme280_settings settings;
};

#endif /* BME280_DEFS_H_ */
/** @}*/
/** @}*/

+ 357
- 25
main/main.c View File

@@ -5,38 +5,370 @@
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "driver/gpio.h"
#include "bme280.h"
#include "driver/i2c.h"

esp_err_t event_handler(void *ctx, system_event_t *event)
#define I2C_MASTER_TX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define I2C_MASTER_RX_BUF_DISABLE 0 /*!< I2C master doesn't need buffer */
#define WRITE_BIT I2C_MASTER_WRITE /*!< I2C master write */
#define READ_BIT I2C_MASTER_READ /*!< I2C master read */
#define ACK_CHECK_EN 0x1 /*!< I2C master will check ack from slave*/
#define ACK_CHECK_DIS 0x0 /*!< I2C master will not check ack from slave */
#define ACK_VAL 0x0 /*!< I2C ack value */
#define NACK_VAL 0x1 /*!< I2C nack value */

static struct {
struct arg_int *port;
struct arg_int *freq;
struct arg_int *sda;
struct arg_int *scl;
struct arg_end *end;
} i2cconfig_args;

static struct {
struct arg_int *chip_address;
struct arg_int *register_address;
struct arg_int *data_length;
struct arg_end *end;
} i2cget_args;

static struct {
struct arg_int *chip_address;
struct arg_int *register_address;
struct arg_int *data;
struct arg_end *end;
} i2cset_args;

static int do_i2cget_cmd(int argc, char **argv)
{
return ESP_OK;
int nerrors = arg_parse(argc, argv, (void **)&i2cget_args);
if (nerrors != 0) {
arg_print_errors(stderr, i2cget_args.end, argv[0]);
return 0;
}

/* Check chip address: "-c" option */
int chip_addr = i2cget_args.chip_address->ival[0];
/* Check register address: "-r" option */
int data_addr = -1;
if (i2cget_args.register_address->count) {
data_addr = i2cget_args.register_address->ival[0];
}
/* Check data length: "-l" option */
int len = 1;
if (i2cget_args.data_length->count) {
len = i2cget_args.data_length->ival[0];
}
uint8_t *data = malloc(len);

i2c_master_driver_initialize();
i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
if (data_addr != -1) {
i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
i2c_master_start(cmd);
}
i2c_master_write_byte(cmd, chip_addr << 1 | READ_BIT, ACK_CHECK_EN);
if (len > 1) {
i2c_master_read(cmd, data, len - 1, ACK_VAL);
}
i2c_master_read_byte(cmd, data + len - 1, NACK_VAL);
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) {
for (int i = 0; i < len; i++) {
printf("0x%02x ", data[i]);
if ((i + 1) % 16 == 0) {
printf("\r\n");
}
}
if (len % 16) {
printf("\r\n");
}
} else if (ret == ESP_ERR_TIMEOUT) {
ESP_LOGW(TAG, "Bus is busy");
} else {
ESP_LOGW(TAG, "Read failed");
}
free(data);
i2c_driver_delete(i2c_port);
return 0;
}

void app_main(void)
static int do_i2cset_cmd(int argc, char **argv)
{
nvs_flash_init();
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_config_t sta_config = {
.sta = {
.ssid = CONFIG_ESP_WIFI_SSID,
.password = CONFIG_ESP_WIFI_PASSWORD,
.bssid_set = false
int nerrors = arg_parse(argc, argv, (void **)&i2cset_args);
if (nerrors != 0) {
arg_print_errors(stderr, i2cset_args.end, argv[0]);
return 0;
}

/* Check chip address: "-c" option */
int chip_addr = i2cset_args.chip_address->ival[0];
/* Check register address: "-r" option */
int data_addr = 0;
if (i2cset_args.register_address->count) {
data_addr = i2cset_args.register_address->ival[0];
}
/* Check data: "-d" option */
int len = i2cset_args.data->count;

i2c_master_driver_initialize();
i2c_driver_install(i2c_port, I2C_MODE_MASTER, I2C_MASTER_RX_BUF_DISABLE, I2C_MASTER_TX_BUF_DISABLE, 0);
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, chip_addr << 1 | WRITE_BIT, ACK_CHECK_EN);
if (i2cset_args.register_address->count) {
i2c_master_write_byte(cmd, data_addr, ACK_CHECK_EN);
}
for (int i = 0; i < len; i++) {
i2c_master_write_byte(cmd, i2cset_args.data->ival[i], ACK_CHECK_EN);
}
i2c_master_stop(cmd);
esp_err_t ret = i2c_master_cmd_begin(i2c_port, cmd, 1000 / portTICK_RATE_MS);
i2c_cmd_link_delete(cmd);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Write OK");
} else if (ret == ESP_ERR_TIMEOUT) {
ESP_LOGW(TAG, "Bus is busy");
} else {
ESP_LOGW(TAG, "Write Failed");
}
i2c_driver_delete(i2c_port);
return 0;
}

static int do_i2cconfig_cmd(int argc, char **argv)
{
int nerrors = arg_parse(argc, argv, (void **)&i2cconfig_args);
if (nerrors != 0) {
arg_print_errors(stderr, i2cconfig_args.end, argv[0]);
return 0;
}

/* Check "--port" option */
if (i2cconfig_args.port->count) {
if (i2c_get_port(i2cconfig_args.port->ival[0], &i2c_port) != ESP_OK) {
return 1;
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
}
/* Check "--freq" option */
if (i2cconfig_args.freq->count) {
i2c_frequency = i2cconfig_args.freq->ival[0];
}
/* Check "--sda" option */
i2c_gpio_sda = i2cconfig_args.sda->ival[0];
/* Check "--scl" option */
i2c_gpio_scl = i2cconfig_args.scl->ival[0];
return 0;
}

int fd;

void user_delay_ms(uint32_t period);

void print_sensor_data(struct bme280_data *comp_data);

gpio_set_direction(GPIO_NUM_4, GPIO_MODE_OUTPUT);
int level = 0;
while (true) {
gpio_set_level(GPIO_NUM_4, level);
level = !level;
vTaskDelay(300 / portTICK_PERIOD_MS);
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);

int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);

int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);

/*!
* @brief This function reading the sensor's registers through I2C bus.
*/
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
write(fd, &reg_addr, 1);
read(fd, data, len);

return 0;
}

/*!
* @brief This function provides the delay for required time (Microseconds) as per the input provided in some of the
* APIs
*/
void user_delay_ms(uint32_t period)
{
/* Milliseconds convert to microseconds */
usleep(period * 1000);
}

/*!
* @brief This function for writing the sensor's registers through I2C bus.
*/
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
int8_t *buf;

buf = malloc(len + 1);
buf[0] = reg_addr;
memcpy(buf + 1, data, len);
if (write(fd, buf, len + 1) < len)
{
return BME280_E_COMM_FAIL;
}

free(buf);

return BME280_OK;
}

/*!
* @brief This API used to print the sensor temperature, pressure and humidity data.
*/
void print_sensor_data(struct bme280_data *comp_data)
{
float temp, press, hum;

#ifdef BME280_FLOAT_ENABLE
temp = comp_data->temperature;
press = 0.01 * comp_data->pressure;
hum = comp_data->humidity;
#else
#ifdef BME280_64BIT_ENABLE
temp = 0.01f * comp_data->temperature;
press = 0.0001f * comp_data->pressure;
hum = 1.0f / 1024.0f * comp_data->humidity;
#else
temp = 0.01f * comp_data->temperature;
press = 0.01f * comp_data->pressure;
hum = 1.0f / 1024.0f * comp_data->humidity;
#endif
#endif
printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
}

/*!
* @brief This API reads the sensor temperature, pressure and humidity data in forced mode.
*/
int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
{
/* Variable to define the result */
int8_t rslt = BME280_OK;

/* Variable to define the selecting sensors */
uint8_t settings_sel = 0;

/* Variable to store minimum wait time between consecutive measurement in force mode */
uint32_t req_delay;

/* Structure to get the pressure, temperature and humidity values */
struct bme280_data comp_data;

/* Recommended mode of operation: Indoor navigation */
dev->settings.osr_h = BME280_OVERSAMPLING_1X;
dev->settings.osr_p = BME280_OVERSAMPLING_16X;
dev->settings.osr_t = BME280_OVERSAMPLING_2X;
dev->settings.filter = BME280_FILTER_COEFF_16;

settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;

/* Set the sensor settings */
rslt = bme280_set_sensor_settings(settings_sel, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);

return rslt;
}

printf("Temperature, Pressure, Humidity\n");

/*Calculate the minimum delay required between consecutive measurement based upon the sensor enabled
* and the oversampling configuration. */
req_delay = bme280_cal_meas_delay(&dev->settings);

/* Continuously stream sensor data */
while (1)
{
/* Set the sensor to forced mode */
rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
break;
}

/* Wait for the measurement to complete and print data */
dev->delay_ms(req_delay);
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
break;
}

print_sensor_data(&comp_data);
}

return rslt;
}

static esp_err_t i2c_master_driver_initialize()
{
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = i2c_gpio_sda,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_io_num = i2c_gpio_scl,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = i2c_frequency
};
return i2c_param_config(i2c_port, &conf);
}

void app_main(void)
{

struct bme280_dev dev;

/* Variable to define the result */
int8_t rslt = BME280_OK;

/* Make sure to select BME280_I2C_ADDR_PRIM or BME280_I2C_ADDR_SEC as needed */
dev.dev_id = BME280_I2C_ADDR_PRIM;

/* dev.dev_id = BME280_I2C_ADDR_SEC; */
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_ms = user_delay_ms;

if ((fd = open(argv[1], O_RDWR)) < 0)
{
fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
exit(1);
}

#ifdef __KERNEL__
if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)
{
fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
#endif

/* Initialize the bme280 */
rslt = bme280_init(&dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
exit(1);
}

rslt = stream_sensor_data_forced_mode(&dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
exit(1);
}

return ;
}


Loading…
Cancel
Save