initialize i2c device needs help

vibnwis
Posts: 89
Joined: Thu Aug 24, 2017 1:13 am

initialize i2c device needs help

Postby vibnwis » Sat Sep 09, 2017 10:20 am

hi there,
I have a I2C code which is working, but I don't understand how it works. Hence, appreciate if someone able to give me a hand, please!
Here is the code.

My question is,
In I2C_master_init(), it does not create a handle. In void task_bme280_normal_mode(void *ignore), the read/write of I2C functions do not reference to any handle. Withtout the handle, how would it know what/which I2C bus it is using? assuming there are two I2C buses being used in the project. I have checked, there is no global variable storing the handle as well.

Appreciate any comments.

Thank you
vi

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);
}

void task_bme280_normal_mode(void *ignore)
{
struct bme280_t bme280 = {
.bus_write = CCS811_I2C_bus_write,
.bus_read = CCS811_I2C_bus_read,
.dev_addr = CCS811_I2C_ADDRESS,
.delay_msec = CCS811_delay_msek
};


s32 com_rslt = 0;
s32 v_uncomp_pressure_s32;
s32 v_uncomp_temperature_s32;
s32 v_uncomp_humidity_s32;

com_rslt = bme280_init(&bme280);
ESP_LOGE(TAG_CCS811, "bme280_init: %d", com_rslt);

com_rslt += bme280_set_oversamp_pressure(BME280_OVERSAMP_16X);
ESP_LOGE(TAG_CCS811, "bme280_set_oversamp_pressure BME280_OVERSAMP_16X: %d", com_rslt);

com_rslt += bme280_set_oversamp_temperature(BME280_OVERSAMP_2X);
ESP_LOGE(TAG_CCS811, "bme280_set_oversamp_temperature BME280_OVERSAMP_2X: %d", com_rslt);

com_rslt += bme280_set_oversamp_humidity(BME280_OVERSAMP_1X);
ESP_LOGE(TAG_CCS811, "bme280_set_oversamp_humidity BME280_OVERSAMP_1X: %d", com_rslt);

com_rslt += bme280_set_standby_durn(BME280_STANDBY_TIME_1_MS);
ESP_LOGE(TAG_CCS811, "bme280_set_standby_durn BME280_STANDBY_TIME_1_MS: %d", com_rslt);

com_rslt += bme280_set_filter(BME280_FILTER_COEFF_16);
ESP_LOGE(TAG_CCS811, "bme280_set_filter BME280_FILTER_COEFF_16: %d", com_rslt);

com_rslt += bme280_set_power_mode(BME280_NORMAL_MODE);
ESP_LOGE(TAG_CCS811, "bme280_set_power_mode BME280_NORMAL_MODE: %d", com_rslt);

if (com_rslt == SUCCESS) {
while(true) {
//vTaskDelay(40/portTICK_PERIOD_MS);
vTaskDelay(pdMS_TO_TICKS(1000));

if (xSemaphoreTake(resourceManager,1000)) {


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_CCS811, "%.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_CCS811, "measure error. code: %d", com_rslt);
}
xSemaphoreGive(resourceManager);
}
}
} else {
ESP_LOGE(TAG_CCS811, "init or setting error. code: %d", com_rslt);
}

vTaskDelete(NULL);
}

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: initialize i2c device needs help

Postby WiFive » Sat Sep 09, 2017 2:01 pm

Probably in the source for bme280 functions

vibnwis
Posts: 89
Joined: Thu Aug 24, 2017 1:13 am

Re: initialize i2c device needs help

Postby vibnwis » Sun Sep 10, 2017 1:52 am

Well, having gone through the I2C.c, I understand what my misunderstanding was. Thanks for the comments.

Who is online

Users browsing this forum: No registered users and 99 guests