-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathmain.c
188 lines (150 loc) · 4.94 KB
/
main.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
#include "driver/gpio.h"
#include "driver/i2c.h"
#include "esp_err.h"
#include "esp_log.h"
#include "freertos/task.h"
#include "sdkconfig.h" // generated by "make menuconfig"
#include "bme280.h"
#define SDA_PIN GPIO_NUM_15
#define SCL_PIN GPIO_NUM_2
#define TAG_BME280 "BME280"
#define I2C_MASTER_ACK 0
#define I2C_MASTER_NACK 1
void i2c_master_init()
{
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = SDA_PIN,
.scl_io_num = SCL_PIN,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = 1000000
};
i2c_param_config(I2C_NUM_0, &i2c_config);
i2c_driver_install(I2C_NUM_0, I2C_MODE_MASTER, 0, 0, 0);
}
s8 BME280_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BME280_INIT_VALUE;
esp_err_t espRc;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev_addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, reg_addr, true);
i2c_master_write(cmd, reg_data, cnt, true);
i2c_master_stop(cmd);
espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
if (espRc == ESP_OK) {
iError = SUCCESS;
} else {
iError = FAIL;
}
i2c_cmd_link_delete(cmd);
return (s8)iError;
}
s8 BME280_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BME280_INIT_VALUE;
esp_err_t espRc;
i2c_cmd_handle_t cmd = i2c_cmd_link_create();
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev_addr << 1) | I2C_MASTER_WRITE, true);
i2c_master_write_byte(cmd, reg_addr, true);
i2c_master_start(cmd);
i2c_master_write_byte(cmd, (dev_addr << 1) | I2C_MASTER_READ, true);
if (cnt > 1) {
i2c_master_read(cmd, reg_data, cnt-1, I2C_MASTER_ACK);
}
i2c_master_read_byte(cmd, reg_data+cnt-1, I2C_MASTER_NACK);
i2c_master_stop(cmd);
espRc = i2c_master_cmd_begin(I2C_NUM_0, cmd, 10/portTICK_PERIOD_MS);
if (espRc == ESP_OK) {
iError = SUCCESS;
} else {
iError = FAIL;
}
i2c_cmd_link_delete(cmd);
return (s8)iError;
}
void BME280_delay_msek(u32 msek)
{
vTaskDelay(msek/portTICK_PERIOD_MS);
}
void task_bme280_normal_mode(void *ignore)
{
struct bme280_t bme280 = {
.bus_write = BME280_I2C_bus_write,
.bus_read = BME280_I2C_bus_read,
.dev_addr = BME280_I2C_ADDRESS2,
.delay_msec = BME280_delay_msek
};
s32 com_rslt;
s32 v_uncomp_pressure_s32;
s32 v_uncomp_temperature_s32;
s32 v_uncomp_humidity_s32;
com_rslt = bme280_init(&bme280);
com_rslt += bme280_set_oversamp_pressure(BME280_OVERSAMP_16X);
com_rslt += bme280_set_oversamp_temperature(BME280_OVERSAMP_2X);
com_rslt += bme280_set_oversamp_humidity(BME280_OVERSAMP_1X);
com_rslt += bme280_set_standby_durn(BME280_STANDBY_TIME_1_MS);
com_rslt += bme280_set_filter(BME280_FILTER_COEFF_16);
com_rslt += bme280_set_power_mode(BME280_NORMAL_MODE);
if (com_rslt == SUCCESS) {
while(true) {
vTaskDelay(40/portTICK_PERIOD_MS);
com_rslt = bme280_read_uncomp_pressure_temperature_humidity(
&v_uncomp_pressure_s32, &v_uncomp_temperature_s32, &v_uncomp_humidity_s32);
if (com_rslt == SUCCESS) {
ESP_LOGI(TAG_BME280, "%.2f degC / %.3f hPa / %.3f %%",
bme280_compensate_temperature_double(v_uncomp_temperature_s32),
bme280_compensate_pressure_double(v_uncomp_pressure_s32)/100, // Pa -> hPa
bme280_compensate_humidity_double(v_uncomp_humidity_s32));
} else {
ESP_LOGE(TAG_BME280, "measure error. code: %d", com_rslt);
}
}
} else {
ESP_LOGE(TAG_BME280, "init or setting error. code: %d", com_rslt);
}
vTaskDelete(NULL);
}
void task_bme280_forced_mode(void *ignore) {
struct bme280_t bme280 = {
.bus_write = BME280_I2C_bus_write,
.bus_read = BME280_I2C_bus_read,
.dev_addr = BME280_I2C_ADDRESS2,
.delay_msec = BME280_delay_msek
};
s32 com_rslt;
s32 v_uncomp_pressure_s32;
s32 v_uncomp_temperature_s32;
s32 v_uncomp_humidity_s32;
com_rslt = bme280_init(&bme280);
com_rslt += bme280_set_oversamp_pressure(BME280_OVERSAMP_1X);
com_rslt += bme280_set_oversamp_temperature(BME280_OVERSAMP_1X);
com_rslt += bme280_set_oversamp_humidity(BME280_OVERSAMP_1X);
com_rslt += bme280_set_filter(BME280_FILTER_COEFF_OFF);
if (com_rslt == SUCCESS) {
while(true) {
com_rslt = bme280_get_forced_uncomp_pressure_temperature_humidity(
&v_uncomp_pressure_s32, &v_uncomp_temperature_s32, &v_uncomp_humidity_s32);
if (com_rslt == SUCCESS) {
ESP_LOGI(TAG_BME280, "%.2f degC / %.3f hPa / %.3f %%",
bme280_compensate_temperature_double(v_uncomp_temperature_s32),
bme280_compensate_pressure_double(v_uncomp_pressure_s32)/100, // Pa -> hPa
bme280_compensate_humidity_double(v_uncomp_humidity_s32));
} else {
ESP_LOGE(TAG_BME280, "measure error. code: %d", com_rslt);
}
}
} else {
ESP_LOGE(TAG_BME280, "init or setting error. code: %d", com_rslt);
}
vTaskDelete(NULL);
}
void app_main(void)
{
i2c_master_init();
xTaskCreate(&task_bme280_normal_mode, "bme280_normal_mode", 2048, NULL, 6, NULL);
// xTaskCreate(&task_bme280_forced_mode, "bme280_forced_mode", 2048, NULL, 6, NULL);
}