F439_CPP_BMP390_Barometric_Pressure_and_Altimeter_01 1.0
STM32F439 BMP390 barometric pressure and altimeter tutorial
Loading...
Searching...
No Matches
common.c
1
7#include <stdint.h>
8#include <stdlib.h>
9#include <stdio.h>
10
11#include <main.h>
12#include "bmp3.h"
13//include "coines.h"
14#include "common.h"
15#include <i2c_spi_interface.h>
16
17
19#define BMP3_SHUTTLE_ID 0xD3
20
21/* Variable to store the device address */
22static uint8_t dev_addr;
23
27BMP3_INTF_RET_TYPE bmp3_i2c_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
28{
29 HAL_StatusTypeDef halStatus = HAL_OK;
30 I2C_SPI_Interface *i2cSpiPtr = (I2C_SPI_Interface *) intf_ptr;
31 uint32_t devAddr = i2cSpiPtr->i2cAddress;
32 int8_t rslt = BMP3_OK;
33
34 halStatus = HAL_I2C_Mem_Read(i2cSpiPtr->i2c, devAddr, reg_addr, I2C_MEMADD_SIZE_8BIT, reg_data, len, HAL_MAX_DELAY);
35
36 if (halStatus != HAL_OK) {
37 rslt = BMP3_E_COMM_FAIL;
38 }
39
40 return rslt;
41}
42
46BMP3_INTF_RET_TYPE bmp3_i2c_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
47{
48 HAL_StatusTypeDef halStatus = HAL_OK;
49 I2C_SPI_Interface *i2cSpiPtr = (I2C_SPI_Interface *)intf_ptr;
50 uint32_t devAddr = i2cSpiPtr->i2cAddress;
51 int8_t rslt = BMP3_OK;
52
53 halStatus = HAL_I2C_Mem_Write(i2cSpiPtr->i2c, devAddr, reg_addr, I2C_MEMADD_SIZE_8BIT, (uint8_t *)reg_data, len, HAL_MAX_DELAY);
54
55 if (halStatus != HAL_OK) {
56 rslt = BMP3_E_COMM_FAIL;
57 } else {
58 halStatus = HAL_I2C_Mem_Write(i2cSpiPtr->i2c, devAddr, reg_addr, I2C_MEMADD_SIZE_8BIT, (uint8_t *)reg_data, len, HAL_MAX_DELAY);
59
60 if (halStatus != HAL_OK) {
61 rslt = BMP3_E_COMM_FAIL;
62 }
63 }
64
65 return rslt;
66}
67
71BMP3_INTF_RET_TYPE bmp3_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
72{
73#ifdef __STM32F4xx_HAL_SPI_H
74 uint8_t device_addr = *(uint8_t*)intf_ptr;
75
76 (void)intf_ptr;
77
78#endif
79 return 0;
80}
81
85BMP3_INTF_RET_TYPE bmp3_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
86{
87#ifdef __STM32F4xx_HAL_SPI_H
88 uint8_t device_addr = *(uint8_t*)intf_ptr;
89
90 (void)intf_ptr;
91
92#endif
93 return 0;
94}
95
99void bmp3_delay_us(uint32_t period, void *intf_ptr)
100{
101 (void)intf_ptr;
102
103 /* delayUS() takes uint16_t (max 65535); every call site in bmp3.c
104 * passes 2000 or 5000, well within range, so a direct cast is safe. */
105 delayUS((uint16_t)period);
106}
107
108void bmp3_check_rslt(const char api_name[], int8_t rslt)
109{
110 switch (rslt)
111 {
112 case BMP3_OK:
113
114 /* Do nothing */
115 break;
116 case BMP3_E_NULL_PTR:
117 printf("API [%s] Error [%d] : Null pointer\r\n", api_name, rslt);
118 break;
119 case BMP3_W_MIN_PRES:
120 printf("API [%s] Error [%d] : Minimum Pressure LT 30000\r\n", api_name, rslt);
121 break;
122 case BMP3_W_MAX_PRES:
123 printf("API [%s] Error [%d] : Maximum Pressure GT 125000\r\n", api_name, rslt);
124 break;
125 case BMP3_E_COMM_FAIL:
126 printf("API [%s] Error [%d] : Communication failure\r\n", api_name, rslt);
127 break;
128 case BMP3_E_INVALID_LEN:
129 printf("API [%s] Error [%d] : Incorrect length parameter\r\n", api_name, rslt);
130 break;
131 case BMP3_E_DEV_NOT_FOUND:
132 printf("API [%s] Error [%d] : Device not found\r\n", api_name, rslt);
133 break;
134 case BMP3_E_CONFIGURATION_ERR:
135 printf("API [%s] Error [%d] : Configuration Error\r\n", api_name, rslt);
136 break;
137 case BMP3_W_SENSOR_NOT_ENABLED:
138 printf("API [%s] Error [%d] : Warning when Sensor not enabled\r\n", api_name, rslt);
139 break;
140 case BMP3_W_INVALID_FIFO_REQ_FRAME_CNT:
141 printf("API [%s] Error [%d] : Warning when Fifo watermark level is not in limit\r\n", api_name, rslt);
142 break;
143 default:
144 printf("API [%s] Error [%d] : Unknown error code\r\n", api_name, rslt);
145 break;
146 }
147}
148
149BMP3_INTF_RET_TYPE bmp3_interface_init(struct bmp3_dev *bmp3, uint8_t intf)
150{
151 int8_t rslt = BMP3_OK;
152
153 if (bmp3 != NULL)
154 {
155 /* Bus configuration : I2C */
156 if (intf == BMP3_I2C_INTF)
157 {
158 printf("I2C Interface\n");
159 dev_addr = (BMP3_ADDR_I2C_SEC << 1);
160 bmp3->read = bmp3_i2c_read;
161 bmp3->write = bmp3_i2c_write;
162 bmp3->intf = BMP3_I2C_INTF;
163
164 I2C_SPI_Interface *i2cPtr = bmp3->intf_ptr;
165 i2cPtr->i2cAddress = dev_addr;
166 }
167 /* Bus configuration : SPI */
168 else if (intf == BMP3_SPI_INTF)
169 {
170 printf("SPI Interface\n");
171 // dev_addr = COINES_SHUTTLE_PIN_7;
172 bmp3->read = bmp3_spi_read;
173 bmp3->write = bmp3_spi_write;
174 bmp3->intf = BMP3_SPI_INTF;
175 }
176
177 bmp3->delay_us = bmp3_delay_us;
178 }
179 else
180 {
181 rslt = BMP3_E_NULL_PTR;
182 }
183
184 return rslt;
185}
186
187void bmp3_coines_deinit(void)
188{
189 (void)fflush(stdout);
190#ifdef COMMENT
191 (void)coines_set_shuttleboard_vdd_vddio_config(0, 0);
192 coines_delay_msec(1000);
193
194 /* Coines interface reset */
195 coines_soft_reset();
196 coines_delay_msec(1000);
197 (void)coines_close_comm_intf(COINES_COMM_INTF_USB, NULL);
198#endif
199}
: Header for main.c file. This file contains the common defines of the application.
void delayUS(uint16_t us)
Busy-waits for the given number of microseconds using TIM1 (1 MHz counter – see MX_TIM1_Init()'s pres...
Definition main.c:109