-
Notifications
You must be signed in to change notification settings - Fork 12
/
bmx280_example_with_i2c_master.c
64 lines (56 loc) · 1.91 KB
/
bmx280_example_with_i2c_master.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "esp_log.h"
#include "bmx280.h"
#include "driver/i2c_types.h"
#define I2C_PORT_AUTO -1
#define BMX280_SDA_NUM GPIO_NUM_13
#define BMX280_SCL_NUM GPIO_NUM_14
i2c_master_bus_handle_t i2c_bus_init(uint8_t sda_io, uint8_t scl_io)
{
i2c_master_bus_config_t i2c_bus_config = {
.i2c_port = I2C_PORT_AUTO,
.sda_io_num = sda_io,
.scl_io_num = scl_io,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = true,
};
i2c_master_bus_handle_t bus_handle;
ESP_ERROR_CHECK(i2c_new_master_bus(&i2c_bus_config, &bus_handle));
ESP_LOGI("test","I2C master bus created");
return bus_handle;
}
esp_err_t bmx280_dev_init(bmx280_t** bmx280,i2c_master_bus_handle_t bus_handle)
{
*bmx280 = bmx280_create_master(bus_handle);
if (!*bmx280) {
ESP_LOGE("test", "Could not create bmx280 driver.");
return ESP_FAIL;
}
ESP_ERROR_CHECK(bmx280_init(*bmx280));
bmx280_config_t bmx_cfg = BMX280_DEFAULT_CONFIG;
ESP_ERROR_CHECK(bmx280_configure(*bmx280, &bmx_cfg));
return ESP_OK;
}
void app_main(void)
{
// Entry Point
//ESP_ERROR_CHECK(nvs_flash_init());
i2c_master_bus_handle_t bus_handle = i2c_bus_init(BMX280_SDA_NUM, BMX280_SCL_NUM);
bmx280_t* bmx280 = NULL;
ESP_ERROR_CHECK(bmx280_dev_init(&bmx280,bus_handle));
ESP_ERROR_CHECK(bmx280_setMode(bmx280, BMX280_MODE_CYCLE));
float temp = 0, pres = 0, hum = 0;
for(int i = 0; i < 10; i++)
{
do {
vTaskDelay(pdMS_TO_TICKS(1));
} while(bmx280_isSampling(bmx280));
ESP_ERROR_CHECK(bmx280_readoutFloat(bmx280, &temp, &pres, &hum));
ESP_LOGI("test", "Read Values: temp = %f, pres = %f, hum = %f", temp, pres, hum);
vTaskDelay(pdMS_TO_TICKS(1000));
}
bmx280_close(bmx280);
i2c_del_master_bus(bus_handle);
ESP_LOGI("test", "Restarting now.");
esp_restart();
}