Rev 13 | Rev 20 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 13 | Rev 18 | ||
|---|---|---|---|
| Line 4... | Line 4... | ||
| 4 | typedef struct |
4 | typedef struct |
| 5 | { |
5 | { |
| 6 | uint8_t dev_addr; |
6 | uint8_t dev_addr; |
| 7 | } interface_t; |
7 | } interface_t; |
| 8 | 8 | ||
| 9 | // interface code |
9 | // interface code |
| 10 | /// pointer to I2C hardware |
10 | /// pointer to I2C hardware |
| 11 | static I2C_HandleTypeDef * hi2c; |
11 | static I2C_HandleTypeDef *hi2c; |
| 12 | 12 | ||
| 13 | /// @brief Generic I2C write function |
13 | /// @brief Generic I2C write function |
| 14 | /// @param i2c_addr I2C address |
14 | /// @param i2c_addr I2C address |
| 15 | /// @param reg_addr I2C register address |
15 | /// @param reg_addr I2C register address |
| 16 | /// @param reg_data pointer to data buffer to send |
16 | /// @param reg_data pointer to data buffer to send |
| 17 | /// @param len length of data to send |
17 | /// @param len length of data to send |
| 18 | /// @return |
18 | /// @return |
| 19 | static int8_t |
19 | static int8_t |
| 20 | user_i2c_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
20 | user_i2c_write(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
| 21 | uint32_t len) |
21 | uint16_t len) |
| 22 | { |
22 | { |
| 23 | HAL_StatusTypeDef st = HAL_I2C_Mem_Write(hi2c, i2c_addr << 1, reg_addr, 1, |
23 | HAL_StatusTypeDef st = HAL_I2C_Mem_Write(hi2c, i2c_addr << 1, reg_addr, 1, |
| 24 | reg_data, len, 1000); |
24 | reg_data, len, 1000); |
| 25 | 25 | ||
| 26 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
26 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
| Line 29... | Line 29... | ||
| 29 | /// @brief Generic I2C read function |
29 | /// @brief Generic I2C read function |
| 30 | /// @param i2c_addr I2C address |
30 | /// @param i2c_addr I2C address |
| 31 | /// @param reg_addr I2C register address |
31 | /// @param reg_addr I2C register address |
| 32 | /// @param reg_data pointer to data buffer to fill |
32 | /// @param reg_data pointer to data buffer to fill |
| 33 | /// @param len length of data to receive |
33 | /// @param len length of data to receive |
| 34 | /// @return |
34 | /// @return status code |
| 35 | static int8_t |
35 | static int8_t |
| 36 | user_i2c_read(uint8_t i2c_addr, uint8_t reg_addr, uint8_t *reg_data, |
36 | user_i2c_read(uint8_t i2c_addr, |
| - | 37 | uint8_t reg_addr, |
|
| - | 38 | uint8_t *reg_data, |
|
| 37 | uint32_t len) |
39 | uint16_t len) |
| 38 | { |
40 | { |
| 39 | HAL_StatusTypeDef st = HAL_I2C_Mem_Read(hi2c, i2c_addr << 1, reg_addr, 1, |
41 | HAL_StatusTypeDef st = HAL_I2C_Mem_Read(hi2c, i2c_addr << 1, reg_addr, 1, |
| 40 | reg_data, len, 1000); |
42 | reg_data, len, 1000); |
| 41 | 43 | ||
| 42 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
44 | return st != HAL_OK ? BMP280_E_COMM_FAIL : BMP280_OK; |
| 43 | } |
45 | } |
| 44 | 46 | ||
| 45 | static void |
47 | static void |
| 46 | user_delay_ms(uint32_t ms, void *handle) |
48 | user_delay_ms(uint32_t ms) |
| 47 | { |
49 | { |
| 48 | HAL_Delay(ms); |
50 | HAL_Delay(ms); |
| 49 | } |
51 | } |
| 50 | 52 | ||
| 51 | struct bmp280_dev bmp = |
53 | struct bmp280_dev bmp = |
| Line 56... | Line 58... | ||
| 56 | .delay_ms = user_delay_ms, |
58 | .delay_ms = user_delay_ms, |
| 57 | 59 | ||
| 58 | /* Update interface pointer with the structure that contains both device address and file descriptor */ |
60 | /* Update interface pointer with the structure that contains both device address and file descriptor */ |
| 59 | .dev_id = BMP280_I2C_ADDR_PRIM}; |
61 | .dev_id = BMP280_I2C_ADDR_PRIM}; |
| 60 | 62 | ||
| 61 | - | ||
| 62 | struct bmp280_config conf; |
63 | struct bmp280_config conf; |
| 63 | 64 | ||
| 64 | - | ||
| 65 | uint8_t init_bmp( I2C_HandleTypeDef * i2c) |
65 | uint8_t init_bmp(I2C_HandleTypeDef *i2c) |
| 66 | { |
66 | { |
| 67 | // copy the I2c handle over |
67 | // copy the I2c handle over |
| 68 | hi2c = i2c; |
68 | hi2c = i2c; |
| 69 | int8_t rslt = bmp280_init(&bmp); |
69 | int8_t rslt = bmp280_init(&bmp); |
| 70 | if (rslt != BMP280_OK) |
70 | if (rslt != BMP280_OK) |
| 71 | return rslt; |
71 | return rslt; |
| 72 | 72 | ||
| 73 | rslt = bmp280_get_config(&conf, &bmp); |
73 | rslt = bmp280_get_config(&conf, &bmp); |
| Line 89... | Line 89... | ||
| 89 | if (rslt != BMP280_OK) |
89 | if (rslt != BMP280_OK) |
| 90 | return rslt; |
90 | return rslt; |
| 91 | 91 | ||
| 92 | /* Always set the power mode after setting the configuration */ |
92 | /* Always set the power mode after setting the configuration */ |
| 93 | rslt = bmp280_set_power_mode(BMP280_NORMAL_MODE, &bmp); |
93 | rslt = bmp280_set_power_mode(BMP280_NORMAL_MODE, &bmp); |
| 94 | return rslt; |
94 | return rslt; |
| 95 | } |
95 | } |
| 96 | - | ||
| 97 | - | ||