-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlps22hb.c
179 lines (135 loc) · 4.23 KB
/
lps22hb.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* lps22hb.c
*
* Created on: Mar 13, 2023
* Author: Emre Emir Fidan
*/
#include "lps22hb.h"
#include "math.h"
#define SPI3WIRE
#define CHECK_STATUS
#define MA_FILTER_COUNT 20
#define CS_PIN_PORT GPIOA
#define CS_PIN GPIO_PIN_3
LPS22HB_STATUS lps22hb_writeReg(lps22hb_t *lps22hb, uint8_t address, uint8_t reg) {
uint8_t ret;
uint8_t buffer[2] = {address, reg};
HAL_GPIO_WritePin(CS_PIN_PORT, CS_PIN, GPIO_PIN_RESET);
ret = HAL_SPI_Transmit(lps22hb->hspi, buffer, 2, 100);
HAL_GPIO_WritePin(CS_PIN_PORT, CS_PIN, GPIO_PIN_SET);
if (ret != HAL_OK)
return LPS22HB_ERROR;
return LPS22HB_OK;
}
LPS22HB_STATUS lps22hb_readReg(lps22hb_t *lps22hb, uint8_t address, uint8_t *data, uint8_t size) {
uint8_t ret;
address |= (0x80U);
HAL_GPIO_WritePin(CS_PIN_PORT, CS_PIN, GPIO_PIN_RESET);
HAL_SPI_Transmit(lps22hb->hspi, &address, 1, 100);
ret = HAL_SPI_Receive(lps22hb->hspi, data, size, 100);
HAL_GPIO_WritePin(CS_PIN_PORT, CS_PIN, GPIO_PIN_SET);
if (ret != HAL_OK)
return LPS22HB_ERROR;
return LPS22HB_OK;
}
LPS22HB_STATUS lps22hb_enableSPI(lps22hb_t *lps22hb, SPI_HandleTypeDef *hspi) {
lps22hb->hspi = hspi;
HAL_GPIO_WritePin(CS_PIN_PORT, CS_PIN, GPIO_PIN_SET);
return LPS22HB_OK;
}
LPS22HB_STATUS lps22hb_init(lps22hb_t *lps22hb, lps22hb_odr_t odr, uint8_t lpfp_en, uint8_t lpfp_cfg) {
uint8_t ret = 0;
lps22hb->status = 0;
/* CTRL_REG1 */
ret = lps22hb_writeReg(lps22hb, LPS22HB_CTRL_REG1, (odr << 4) | (lpfp_en << 3) | (lpfp_cfg << 2) | (1));
/* Read CHIP ID (0xB1) */
ret = lps22hb_readReg(lps22hb, LPS22HB_WHO_AM_I, &lps22hb->chip_id, 1);
if (lps22hb->chip_id != LPS22HB_ID || ret != LPS22HB_OK) {
lps22hb->status = LPS22HB_ERROR;
return LPS22HB_ERROR;
}
/* Calculate Bandwith */
switch(odr) {
case 1:
lps22hb->bandwith = 1.0f;
break;
case 2:
lps22hb->bandwith = 10.0f;
break;
case 3:
lps22hb->bandwith = 25.0f;
break;
case 4:
lps22hb->bandwith = 50.0f;
break;
case 5:
lps22hb->bandwith = 75.0f;
break;
default:
lps22hb->bandwith = 0.0f;
break;
}
if (lpfp_en == 0) {
lps22hb->bandwith /= 2.0f;
}else if (lpfp_en == 1 && lpfp_cfg == 0) {
lps22hb->bandwith /= 9.0f;
}else if (lpfp_en == 1 && lpfp_cfg == 1) {
lps22hb->bandwith /= 20.0f;
}
lps22hb->compute_time = (uint32_t)(1000.0f / lps22hb->bandwith);
/* Set Altitude Offset Zero */
lps22hb->altitude_offset = 0;
lps22hb->status = LPS22HB_OK;
return LPS22HB_OK;
}
LPS22HB_STATUS lps22hb_readPressure(lps22hb_t *lps22hb) {
if (lps22hb->status == LPS22HB_OK) {
uint8_t ret;
uint8_t buffer[3];
ret = lps22hb_readReg(lps22hb, LPS22HB_PRESS_OUT_XL, buffer, 3);
if (ret != LPS22HB_OK)
return LPS22HB_ERROR;
lps22hb->raw_pressure = ((buffer[2] << 16) | (buffer[1] << 8) | buffer[0]);
lps22hb->pressure = (float)lps22hb->raw_pressure / 4096.0f;
return LPS22HB_OK;
}
return LPS22HB_NCP;
}
LPS22HB_STATUS lps22hb_readTemperature(lps22hb_t *lps22hb) {
if (lps22hb->status == LPS22HB_OK) {
uint8_t ret;
uint8_t buffer[2];
ret = lps22hb_readReg(lps22hb, LPS22HB_TEMP_OUT_L, buffer, 2);
if (ret != LPS22HB_OK)
return LPS22HB_ERROR;
lps22hb->temperature = (float)((int16_t)((buffer[1] << 8) | buffer[0]));
lps22hb->temperature /= 100.0f;
return LPS22HB_OK;
}
return LPS22HB_NCP;
}
LPS22HB_STATUS lps22hb_readAltitude(lps22hb_t *lps22hb) {
if (lps22hb->status == LPS22HB_OK) {
// https://www.weather.gov/media/epz/wxcalc/pressureAltitude.pdf
lps22hb->altitude = (1 - (pow((lps22hb->pressure / 1013.25f), 0.190284f))) * 44307.69f;
lps22hb->altitude -= lps22hb->altitude_offset;
return LPS22HB_OK;
}
return LPS22HB_NCP;
}
LPS22HB_STATUS lps22hb_setAltitudeOffset(lps22hb_t *lps22hb, float altitude) {
lps22hb->altitude_offset = altitude;
return LPS22HB_OK;
}
LPS22HB_STATUS lps22hb_setAltitudeZero(lps22hb_t *lps22hb) {
float alt_total;
for (uint8_t i = 0; i < MA_FILTER_COUNT; i++) {
lps22hb_readPressure(lps22hb);
lps22hb_readAltitude(lps22hb);
alt_total += lps22hb->altitude;
HAL_Delay(lps22hb->compute_time);
}
alt_total /= MA_FILTER_COUNT;
lps22hb_setAltitudeOffset(lps22hb, alt_total);
return LPS22HB_OK;
}