cancel
Showing results for 
Search instead for 
Did you mean: 

Integration of the example code for ilps28qsw

Jens4
Associate II

Hello everyone,
I want to connect my pressure sensor (ilps28qsw) and have written the code in C and using I2C. If I use the example code as in the following link, and do nothing else, it works. For example, if I pull the variables (see below) out of this function and make them usable outside of the function (ilps28qsw_read_data_polling()) or want to execute other functions before or after this code, I get an error.

I cannot understand this behavior. If anyone can explain it to me, I would be very grateful.

 

Example code: https://github.com/STMicroelectronics/STMems_Standard_C_drivers/blob/master/ilps28qsw_STdC/examples/ilps28qsw_read_data_polling.c

Variables:
ilps28qsw_all_sources_t all_sources;
ilps28qsw_bus_mode_t bus_mode;
ilps28qsw_stat_t status;
stmdev_ctx_t dev_ctx;
ilps28qsw_id_t id;
ilps28qsw_md_t md;

5 REPLIES 5
Andrew Neil
Evangelist III
Jens4
Associate II

Hi Andrew,

sure!

My code is the following:

void ilps28qsw_read_data_polling(void)
{
    ilps28qsw_all_sources_t all_sources;
    ilps28qsw_bus_mode_t bus_mode;
    ilps28qsw_stat_t status;
    stmdev_ctx_t dev_ctx;
    ilps28qsw_id_t id;
    ilps28qsw_md_t md;

    /* Initialize mems driver interface */
    dev_ctx.write_reg = platform_write;
    dev_ctx.read_reg = platform_read;
    dev_ctx.mdelay = platform_delay;
    dev_ctx.handle = (void *)(uintptr_t)ILPS28QSW_I2C_ADDRESS;

    // i2c_scan();
    // ESP_LOGI(TAG, "Scan Done and out");

    i2c_master_init();

    /* Init test platform */
    platform_init(dev_ctx.handle);

    /* Wait sensor boot time */
    platform_delay(BOOT_TIME);

    /* Check device ID */
    ilps28qsw_id_get(&dev_ctx, &id);
    if (id.whoami != ILPS28QSW_ID)
        while (1)
            ;

    /* Restore default configuration */
    ilps28qsw_init_set(&dev_ctx, ILPS28QSW_RESET);
    do
    {
        ilps28qsw_status_get(&dev_ctx, &status);
    } while (status.sw_reset);

    /* Disable AH/QVAR to save power consumption */
    ilps28qsw_ah_qvar_disable(&dev_ctx);

    /* Set bdu and if_inc recommended for driver usage */
    ilps28qsw_init_set(&dev_ctx, ILPS28QSW_DRV_RDY);

    /* Select bus interface */
    bus_mode.filter = ILPS28QSW_AUTO;
    ilps28qsw_bus_mode_set(&dev_ctx, &bus_mode);

    /* Set Output Data Rate */
    md.odr = ILPS28QSW_4Hz;
    md.avg = ILPS28QSW_16_AVG;
    md.lpf = ILPS28QSW_LPF_ODR_DIV_4;
    md.fs = ILPS28QSW_1260hPa;
    ilps28qsw_mode_set(&dev_ctx, &md);
    /* Read samples in polling mode (no int) */
    while (1)
    {
        /* Read output only if new values are available */
        ilps28qsw_all_sources_get(&dev_ctx, &all_sources);
        if (all_sources.drdy_pres | all_sources.drdy_temp)
        {

            ilps28qsw_data_get(&dev_ctx, &md, &data);

            ESP_LOGI(TAG, "pressure [hPa]: %6.2f temperature [degC]: %6.2f", data.pressure.hpa, data.heat.deg_c);

            tx_com(tx_buffer, strlen((char const *)tx_buffer));
        }
        vTaskDelay(100 / portTICK_PERIOD_MS);
    }
}

 

static void i2c_master_init(void)
{

    i2c_config_t conf;
    conf.mode = I2C_MODE_MASTER;
    conf.sda_io_num = SDA_PIN;
    conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
    conf.scl_io_num = SCL_PIN;
    conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
    conf.master.clk_speed = I2C_MASTER_FREQ_HZ;

    i2c_param_config(I2C_MASTER_NUM, &conf);

    i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER, 0, 0, 0);
    
}


And if I call this code from my main I do not get an error.

If I take one variable eg. and put it in front of the function like this:

ilps28qsw_all_sources_t all_sources;

/* Main Example --------------------------------------------------------------*/
void ilps28qsw_read_data_polling(void)
{

    ilps28qsw_bus_mode_t bus_mode;
    ilps28qsw_stat_t status;
    stmdev_ctx_t dev_ctx;
    ilps28qsw_id_t id;
    ilps28qsw_md_t md;

    /* Initialize mems driver interface */
    dev_ctx.write_reg = platform_write;

....

 

I get this error:

i2c: i2c_param_config(741): i2c clock choice is invalid, please check flag and frequency

 


@Jens4 wrote:

 i2c clock choice is invalid 


So your clock values are invalid - fix them!

 


@Jens4 wrote:

 please check flag and frequency


 Have you done that?

You said you'd had it working, so you must have had valid settings for that?

Jens4
Associate II

I am still able to create the code without any errors. 
As long as the variables mentioned above remain in the function, there is no error. If I pull one of these variables out of this function, I get this error and I can't understand why, because I don't change anything in the clock or the glass and frequency.


@Jens4 wrote:

I am still able to create the code without any errors. 


It's not a C build error - it's saying that the values are not valid.

Something in the code is checking this at run time - in your STM32 - so step through the code, and see where it's happening.

Or do a text search through the code for the text of that message.

Something you're doing when you "pull variables out of this function" is meaning that incorrect values are being given to something.

 

EDIT:

i2c: i2c_param_config(741): i2c clock choice is invalid, please check flag and frequency

 

I would guess that the i2c_param_config there refers to this:

static void i2c_master_init(void)
{

    i2c_config_t conf;
    conf.mode = I2C_MODE_MASTER;
    conf.sda_io_num = SDA_PIN;
    conf.sda_pullup_en = GPIO_PULLUP_ENABLE;
    conf.scl_io_num = SCL_PIN;
    conf.scl_pullup_en = GPIO_PULLUP_ENABLE;
    conf.master.clk_speed = I2C_MASTER_FREQ_HZ;

    i2c_param_config( I2C_MASTER_NUM, &conf );     <<<<<<<<<<< HERE !!
    ^^^^^^^^^^^^^^^^

    i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER, 0, 0, 0);
    
}

So step into that function, and see what's going on ...