|
|
@@ -6,6 +6,7 @@ |
|
|
|
//#include "nvs_flash.h" |
|
|
|
//#include "driver/gpio.h" |
|
|
|
#include "bme280.h" |
|
|
|
#include "bme280_defs.h" |
|
|
|
//#include "ssd1366.h" |
|
|
|
#include "driver/i2c.h" |
|
|
|
#include <u8g2.h> |
|
|
@@ -62,6 +63,56 @@ void i2c_detect() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
int8_t i2c_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) { |
|
|
|
//printf("Reading from bus: dev_id=%x, reg_addr=%x, data=%p, length=%u\n", dev_id, reg_addr, data, len); |
|
|
|
/*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);*/ |
|
|
|
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); |
|
|
|
i2c_master_start(cmd); |
|
|
|
i2c_master_write_byte(cmd, dev_id << 1 | I2C_MASTER_WRITE, 1); |
|
|
|
i2c_master_write_byte(cmd, reg_addr, 1); |
|
|
|
i2c_master_start(cmd); |
|
|
|
i2c_master_write_byte(cmd, dev_id << 1 | I2C_MASTER_READ, 1); |
|
|
|
if (len > 1) { |
|
|
|
i2c_master_read(cmd, data, len - 1, I2C_MASTER_ACK); |
|
|
|
} |
|
|
|
i2c_master_read_byte(cmd, data + len - 1, I2C_MASTER_NACK); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
i2c_master_cmd_begin(I2C_NUM_0, cmd, 500 / portTICK_RATE_MS); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
int8_t i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *data, uint16_t len) { |
|
|
|
//printf("Writing to bus: dev_id=%x, reg_addr=%x, data=%p, length=%u\n", dev_id, reg_addr, data, len); |
|
|
|
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); |
|
|
|
i2c_master_start(cmd); |
|
|
|
i2c_master_write_byte(cmd, (dev_id << 1) | I2C_MASTER_WRITE, 1); |
|
|
|
i2c_master_write_byte(cmd, reg_addr, 1); |
|
|
|
i2c_master_write(cmd, data, len, 1); |
|
|
|
i2c_master_stop(cmd); |
|
|
|
i2c_master_cmd_begin(I2C_NUM_0, cmd, 500 / portTICK_RATE_MS); |
|
|
|
i2c_cmd_link_delete(cmd); |
|
|
|
return 0; |
|
|
|
} |
|
|
|
|
|
|
|
void i2c_delay(uint32_t period) { |
|
|
|
vTaskDelay(period / portTICK_PERIOD_MS); |
|
|
|
} |
|
|
|
|
|
|
|
void i2c_shutdown() |
|
|
|
{ |
|
|
|
printf("Shutting down I²C bus... "); |
|
|
@@ -75,8 +126,48 @@ void i2c_shutdown() |
|
|
|
void app_main(void) |
|
|
|
{ |
|
|
|
i2c_setup(); |
|
|
|
i2c_detect(); |
|
|
|
struct bme280_dev dev; |
|
|
|
int8_t rslt = BME280_OK; |
|
|
|
|
|
|
|
dev.dev_id = 0x76; |
|
|
|
dev.intf = BME280_I2C_INTF; |
|
|
|
dev.read = i2c_read; |
|
|
|
dev.write = i2c_write; |
|
|
|
dev.delay_ms = i2c_delay; |
|
|
|
|
|
|
|
rslt = bme280_init(&dev); |
|
|
|
|
|
|
|
uint8_t settings_sel; |
|
|
|
uint32_t req_delay; |
|
|
|
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; |
|
|
|
|
|
|
|
rslt = bme280_set_sensor_settings(settings_sel, &dev); |
|
|
|
|
|
|
|
/*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)); |
|
|
|
|
|
|
|
printf("Temperature, Pressure, Humidity\r\n"); |
|
|
|
/* Continuously stream sensor data */ |
|
|
|
while (1) { |
|
|
|
rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, &dev); |
|
|
|
/* Wait for the measurement to complete and print data @25Hz */ |
|
|
|
dev.delay_ms(req_delay); |
|
|
|
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, &dev); |
|
|
|
printf("%i °c, %i hPa, %i %%\r\n", comp_data.temperature/100, comp_data.pressure/100, comp_data.humidity/1024); |
|
|
|
vTaskDelay(50 / portTICK_PERIOD_MS); |
|
|
|
} |
|
|
|
|
|
|
|
i2c_shutdown(); |
|
|
|
|
|
|
|
u8g2_esp32_hal_t u8g2_esp32_hal = U8G2_ESP32_HAL_DEFAULT; |
|
|
|
u8g2_esp32_hal.sda = 18; |
|
|
|
u8g2_esp32_hal.scl = 19; |
|
|
|