Rev 13 | Go to most recent revision | Details | 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 | |||
9 | // interface code |
||
10 | /// pointer to I2C hardware |
||
11 | static I2C_HandleTypeDef * hi2c; |
||
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 | /// @return |
||
19 | static int8_t |
||
20 | user_i2c_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
||
21 | uint32_t len) |
||
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 |
||
34 | /// @return |
||
35 | static int8_t |
||
36 | user_i2c_read(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
||
37 | uint32_t len) |
||
38 | { |
||
39 | HAL_StatusTypeDef st = HAL_I2C_Mem_Read(hi2c, i2c_addr << 1, reg_addr, 1, |
||
40 | reg_data, len, 1000); |
||
41 | |||
42 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
||
43 | } |
||
44 | |||
45 | static void |
||
46 | user_delay_ms(uint32_t ms, void *handle) |
||
47 | { |
||
48 | HAL_Delay(ms); |
||
49 | } |
||
50 | |||
51 | struct bmp280_dev bmp = |
||
52 | { |
||
53 | .intf = BMP280_I2C_INTF, |
||
54 | .read = user_i2c_read, |
||
55 | .write = user_i2c_write, |
||
56 | .delay_ms = user_delay_ms, |
||
57 | |||
58 | /* Update interface pointer with the structure that contains both device address and file descriptor */ |
||
59 | .dev_id = BMP280_I2C_ADDR_PRIM}; |
||
60 | |||
61 | |||
62 | struct bmp280_config conf; |
||
63 | |||
64 | |||
65 | uint8_t init_bmp( I2C_HandleTypeDef * i2c) |
||
66 | { |
||
67 | // copy the I2c handle over |
||
68 | hi2c = i2c; |
||
69 | int8_t rslt = bmp280_init(&bmp); |
||
70 | if (rslt != BMP280_OK) |
||
71 | return rslt; |
||
72 | |||
73 | rslt = bmp280_get_config(&conf, &bmp); |
||
74 | if (rslt != BMP280_OK) |
||
75 | return rslt; |
||
76 | /* configuring the temperature oversampling, filter coefficient and output data rate */ |
||
77 | /* Overwrite the desired settings */ |
||
78 | conf.filter = BMP280_FILTER_COEFF_2; |
||
79 | |||
80 | /* Temperature oversampling set at 4x */ |
||
81 | conf.os_temp = BMP280_OS_4X; |
||
82 | |||
83 | /* Pressure over sampling none (disabling pressure measurement) */ |
||
84 | conf.os_pres = BMP280_OS_4X; |
||
85 | |||
86 | /* Setting the output data rate as 2HZ(500ms) */ |
||
87 | conf.odr = BMP280_ODR_500_MS; |
||
88 | rslt = bmp280_set_config(&conf, &bmp); |
||
89 | if (rslt != BMP280_OK) |
||
90 | return rslt; |
||
91 | |||
92 | /* Always set the power mode after setting the configuration */ |
||
93 | rslt = bmp280_set_power_mode(BMP280_NORMAL_MODE, &bmp); |
||
94 | return rslt; |
||
95 | } |
||
96 | |||
97 |