0
\$\begingroup\$

I'm writing software (for ATMega32) that reads pressure from Bosch BMP280 sensor. I'm using I2C bus. I have found this library: https://github.com/BoschSensortec/BMP280_driver

and I have a question about inizialization of I2C:

int8_t rslt;
struct bmp280_dev bmp;
/* Sensor interface over I2C with primary slave address  */
bmp.dev_id = BMP280_I2C_ADDR_PRIM;
bmp.intf = BMP280_I2C_INTF;
bmp.read = user_i2c_read;
bmp.write = user_i2c_write;
bmp.delay_ms = user_delay_ms;

What function should I use as user_i2c_read/user_i2c_write and user_delay_ms?

\$\endgroup\$
1
\$\begingroup\$

The struct bmp280_dev is defined in bmp280_defs.h The actual memebers are

bmp280_com_fptr_t read;
bmp280_com_fptr_t write;
bmp280_delay_fptr_t delay_ms;

If you search further in that file for the keyword

bmp280_com_fptr_t

you will find this declaration

typedef int8_t (*bmp280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr,
    uint8_t *data, uint16_t len);

which gives that the functions you asking for are function-pointers. Here is a nice tutorial about function pointers. Programs as Data: Function Pointers

furthermore, it is essential to create theese functions and function-pointers, and to initialize a default return-value of rslt=BMP280_OK. Here is a shorted example from my own code. You have to implant routines to get proper error-checking-

int8_t user_i2c_read(uint8_t dev_id, uint8_t reg_addr, 
uint8_t*reg_data, uint16_t len) 
{
  printf("\nuser_i2c_read\n"); //just to see that the function executes
  int8_t rslt = BMP280_OK;
   // implent more code here, to a start you can print out the
   // variables.and check that they are are 'set'
   if (rslt != BMP280_OK)
   {
      rslt = BMP280_E_COMM_FAIL; 
   }
   return rslt;
}

To the like for user_i2c_write and

void user_delay_ms(uint32_t ms){ usleep(ms); }

Thoose functions are called from bmp280.c . Also - when you from main run

rslt = bmp280_init(&bmp);

Without creating thoose functions, the bmp280_init will never give you the right return values.

Also it is worth mention that you do not need to name the functions to user_blabla. You can name them to whaatever you want - as long that you imitalize the functionpointers to the same name. That gives :you can re-initialize the function-pointrs to other names, and therefore make the read-call, the write-call etcetera to call other functions than they did previousley.

I might have some stuff in this to not be 100% accurate, because I just a few hours ago, after working many hours with this, found it out out myself, and I have been up all to many hours to get this working. And I am not naturally english speaking, so there we have the languge-barriar.

One last hint, the issues thread from the repo of BoschSensortec/BMP280_driver has some code worth looking at. BoschSensortec/BMP280_driver issuses

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.