Rev 21 | Details | Compare with Previous | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 2 | mjames | 1 | #include "main.h" |
| 2 | #include "libBMP280/bmp280.h" |
||
| 3 | /* USER CODE BEGIN PV */ |
||
| 4 | typedef struct |
||
| 5 | { |
||
| 6 | uint8_t dev_addr; |
||
| 7 | } interface_t; |
||
| 8 | |||
| 18 | mjames | 9 | // interface code |
| 2 | mjames | 10 | /// pointer to I2C hardware |
| 18 | mjames | 11 | static I2C_HandleTypeDef *hi2c; |
| 2 | mjames | 12 | |
| 13 | /// @brief Generic I2C write function |
||
| 14 | /// @param i2c_addr I2C address |
||
| 15 | /// @param reg_addr I2C register address |
||
| 16 | /// @param reg_data pointer to data buffer to send |
||
| 17 | /// @param len length of data to send |
||
| 18 | mjames | 18 | /// @return |
| 2 | mjames | 19 | static int8_t |
| 20 | user_i2c_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
||
| 18 | mjames | 21 | uint16_t len) |
| 2 | mjames | 22 | { |
| 23 | HAL_StatusTypeDef st = HAL_I2C_Mem_Write(hi2c, i2c_addr << 1, reg_addr, 1, |
||
| 24 | reg_data, len, 1000); |
||
| 25 | |||
| 26 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
||
| 27 | } |
||
| 28 | |||
| 29 | /// @brief Generic I2C read function |
||
| 30 | /// @param i2c_addr I2C address |
||
| 31 | /// @param reg_addr I2C register address |
||
| 32 | /// @param reg_data pointer to data buffer to fill |
||
| 33 | /// @param len length of data to receive |
||
| 18 | mjames | 34 | /// @return status code |
| 2 | mjames | 35 | static int8_t |
| 18 | mjames | 36 | user_i2c_read(uint8_t i2c_addr, |
| 37 | uint8_t reg_addr, |
||
| 38 | uint8_t *reg_data, |
||
| 39 | uint16_t len) |
||
| 2 | mjames | 40 | { |
| 41 | HAL_StatusTypeDef st = HAL_I2C_Mem_Read(hi2c, i2c_addr << 1, reg_addr, 1, |
||
| 42 | reg_data, len, 1000); |
||
| 43 | |||
| 44 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
||
| 45 | } |
||
| 46 | |||
| 47 | static void |
||
| 18 | mjames | 48 | user_delay_ms(uint32_t ms) |
| 2 | mjames | 49 | { |
| 50 | HAL_Delay(ms); |
||
| 51 | } |
||
| 52 | |||
| 21 | mjames | 53 | struct bmp280_dev bmpManifold = |
| 2 | mjames | 54 | { |
| 55 | .intf = BMP280_I2C_INTF, |
||
| 56 | .read = user_i2c_read, |
||
| 57 | .write = user_i2c_write, |
||
| 58 | .delay_ms = user_delay_ms, |
||
| 59 | |||
| 60 | /* Update interface pointer with the structure that contains both device address and file descriptor */ |
||
| 61 | .dev_id = BMP280_I2C_ADDR_PRIM}; |
||
| 62 | |||
| 21 | mjames | 63 | struct bmp280_dev bmpAtmosphere = |
| 20 | mjames | 64 | { |
| 65 | .intf = BMP280_I2C_INTF, |
||
| 66 | .read = user_i2c_read, |
||
| 67 | .write = user_i2c_write, |
||
| 68 | .delay_ms = user_delay_ms, |
||
| 69 | |||
| 70 | /* Update interface pointer with the structure that contains both device address and file descriptor */ |
||
| 71 | .dev_id = BMP280_I2C_ADDR_SEC}; |
||
| 72 | |||
| 21 | mjames | 73 | // config for manifold sensor |
| 74 | struct bmp280_config confManifold; |
||
| 75 | // config for atmospheric sensor |
||
| 76 | struct bmp280_config confAtmosphere; |
||
| 2 | mjames | 77 | |
| 21 | mjames | 78 | uint8_t init_bmp(I2C_HandleTypeDef *i2c,struct bmp280_dev * bmp,struct bmp280_config *conf) |
| 2 | mjames | 79 | { |
| 18 | mjames | 80 | // copy the I2c handle over |
| 81 | hi2c = i2c; |
||
| 20 | mjames | 82 | int8_t rslt = bmp280_init(bmp); |
| 2 | mjames | 83 | if (rslt != BMP280_OK) |
| 84 | return rslt; |
||
| 85 | |||
| 21 | mjames | 86 | rslt = bmp280_get_config(conf, bmp); |
| 2 | mjames | 87 | if (rslt != BMP280_OK) |
| 88 | return rslt; |
||
| 89 | /* configuring the temperature oversampling, filter coefficient and output data rate */ |
||
| 90 | /* Overwrite the desired settings */ |
||
| 21 | mjames | 91 | conf->filter = BMP280_FILTER_COEFF_4; |
| 2 | mjames | 92 | |
| 20 | mjames | 93 | /* Temperature oversampling set at 1x */ |
| 21 | mjames | 94 | conf->os_temp = BMP280_OS_1X; |
| 2 | mjames | 95 | |
| 20 | mjames | 96 | /* Pressure over sampling 4x */ |
| 21 | mjames | 97 | conf->os_pres = BMP280_OS_4X; |
| 2 | mjames | 98 | |
| 22 | mjames | 99 | /* Setting the output data rate as 8HZ(125ms) */ |
| 21 | mjames | 100 | conf->odr = BMP280_ODR_125_MS; |
| 101 | rslt = bmp280_set_config(conf, bmp); |
||
| 2 | mjames | 102 | if (rslt != BMP280_OK) |
| 103 | return rslt; |
||
| 104 | |||
| 105 | /* Always set the power mode after setting the configuration */ |
||
| 20 | mjames | 106 | rslt = bmp280_set_power_mode(BMP280_NORMAL_MODE, bmp); |
| 18 | mjames | 107 | return rslt; |
| 2 | mjames | 108 | } |