|
- #include "freertos/FreeRTOS.h"
- #include "esp_wifi.h"
- #include "esp_system.h"
- #include "esp_event.h"
- #include "esp_event_loop.h"
- #include "nvs_flash.h"
- #include "driver/gpio.h"
- #include "bme280.h"
- #include "driver/i2c.h"
-
- #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)
- {
- 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;
- }
-
- static int do_i2cset_cmd(int argc, char **argv)
- {
- 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;
- }
- }
- /* 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);
-
- 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, ®_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 ;
- }
|