Mercurial > public > ostc4
annotate Small_CPU/Src/pressure.c @ 366:3c7030d6d67a MotionDetection
Fix misalignment of do while loop
author | ideenmodellierer |
---|---|
date | Mon, 17 Jun 2019 19:56:38 +0200 |
parents | 8e9c502c0b06 |
children | b4c578caaafb |
rev | line source |
---|---|
38 | 1 /** |
2 ****************************************************************************** | |
3 * @file pressure.c | |
4 * @author heinrichs weikamp gmbh | |
5 * @date 2014 | |
6 * @version V0.0.2 | |
7 * @since 20-Oct-2016 | |
8 * @brief | |
9 * | |
10 @verbatim | |
11 ============================================================================== | |
12 ##### How to use ##### | |
13 ============================================================================== | |
14 V0.0.2 18-Oct-2016 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015 | |
15 | |
16 @endverbatim | |
17 ****************************************************************************** | |
18 * @attention | |
19 * | |
20 * <h2><center>© COPYRIGHT(c) 2016 heinrichs weikamp</center></h2> | |
21 * | |
22 ****************************************************************************** | |
23 */ | |
24 | |
25 | |
26 | |
27 /* surface time | |
28 the last 30 minutes will be saved once per minute in a endless loop | |
29 at the beginning of a dive the oldest value will be used | |
30 */ | |
31 | |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
32 #include "scheduler.h" |
38 | 33 #include "pressure.h" |
34 #include "i2c.h" | |
35 #include "rtc.h" | |
36 | |
37 #define CMD_RESET 0x1E // ADC reset command | |
38 #define CMD_ADC_READ 0x00 // ADC read command | |
39 #define CMD_ADC_CONV 0x40 // ADC conversion command | |
40 #define CMD_ADC_D1 0x00 // ADC D1 conversion | |
41 #define CMD_ADC_D2 0x10 // ADC D2 conversion | |
42 #define CMD_ADC_256 0x00 // ADC OSR=256 | |
43 #define CMD_ADC_512 0x02 // ADC OSR=512 | |
44 #define CMD_ADC_1024 0x04 // ADC OSR=1024 | |
45 #define CMD_ADC_2048 0x06 // ADC OSR=2056 | |
46 #define CMD_ADC_4096 0x08 // ADC OSR=4096 | |
47 #define CMD_PROM_RD 0xA0 // Prom read command | |
48 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
49 static uint16_t get_ci_by_coef_num(uint8_t coef_num); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
50 //void pressure_calculation_new(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
51 //void pressure_calculation_old(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
52 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
53 static uint8_t crc4(uint16_t n_prom[]); |
38 | 54 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
55 static HAL_StatusTypeDef pressure_sensor_get_data(void); |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
56 static uint32_t get_adc(void); |
38 | 57 uint8_t pressureSensorInitSuccess = 0; |
58 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
59 static uint16_t C[8] = { 1 }; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
60 static uint32_t D1 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
61 static uint32_t D2 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
62 static uint8_t n_crc; |
38 | 63 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
64 static int64_t C5_x_2p8 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
65 static int64_t C2_x_2p16 = 1; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
66 static int64_t C1_x_2p15 = 1; |
38 | 67 |
68 /* | |
69 short C2plus10000 = -1; | |
70 short C3plus200 = -1; | |
71 short C4minus250 = -1; | |
72 short UT1 = -1; | |
73 short C6plus100 = -1; | |
74 */ | |
75 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
76 static float ambient_temperature = 0; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
77 static float ambient_pressure_mbar = 0; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
78 static float surface_pressure_mbar = 1000; |
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
79 static float surface_ring_mbar[31] = { 0 }; |
38 | 80 |
81 uint8_t secondCounterSurfaceRing = 0; | |
82 | |
83 float get_temperature(void) | |
84 { | |
85 return ambient_temperature; | |
86 } | |
87 | |
88 float get_pressure_mbar(void) | |
89 { | |
90 return ambient_pressure_mbar; | |
91 } | |
92 | |
93 float get_surface_mbar(void) | |
94 { | |
95 return surface_pressure_mbar; | |
96 } | |
97 | |
98 | |
99 void init_surface_ring(void) | |
100 { | |
101 surface_ring_mbar[0] = 0; | |
102 for(int i=1; i<31; i++) | |
103 surface_ring_mbar[i] = ambient_pressure_mbar; | |
104 surface_pressure_mbar = ambient_pressure_mbar; | |
105 } | |
106 | |
107 | |
108 /* the ring has one place with 0 | |
109 * after that comes the oldest value | |
110 * the new pressure is written in this hole | |
111 * the oldest value is read and then the new hole | |
112 */ | |
113 void update_surface_pressure(uint8_t call_rhythm_seconds) | |
114 { | |
115 secondCounterSurfaceRing += call_rhythm_seconds; | |
116 | |
117 if(secondCounterSurfaceRing < 60) | |
118 return; | |
119 | |
120 secondCounterSurfaceRing = 0; | |
121 | |
122 int hole; | |
123 for(hole=30;hole>0;hole--) | |
124 if(surface_ring_mbar[hole] == 0) { break; } | |
125 | |
126 surface_ring_mbar[hole] = ambient_pressure_mbar; | |
127 | |
128 hole++; | |
129 if(hole > 30) | |
130 hole = 0; | |
131 surface_pressure_mbar = surface_ring_mbar[hole]; | |
132 surface_ring_mbar[hole] = 0; | |
133 } | |
134 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
135 #ifdef DEMOMODE |
38 | 136 float demo_modify_temperature_helper(float bottom_mbar_diff_to_surface) |
137 { | |
138 const float temperature_surface = 31.0; | |
139 const float temperature_bottom = 14.0; | |
140 | |
141 const float temperature_difference = temperature_bottom - temperature_surface; | |
142 | |
143 // range 0.0 - 1.0 | |
144 float position_now = (ambient_pressure_mbar - surface_pressure_mbar) / bottom_mbar_diff_to_surface; | |
145 | |
146 if(position_now <= 0) | |
147 return temperature_surface; | |
148 | |
149 if(position_now >= 1) | |
150 return temperature_bottom; | |
151 | |
152 return temperature_surface + (temperature_difference * position_now); | |
153 } | |
154 | |
155 | |
156 uint32_t demo_modify_temperature_and_pressure(int32_t divetime_in_seconds, uint8_t subseconds, float ceiling_mbar) | |
157 { | |
158 | |
159 const float descent_rate = 4000/60; | |
160 const float ascent_rate = 1000/60; | |
161 const uint32_t seconds_descend = (1 * 60) + 30; | |
162 const uint32_t turbo_seconds_at_bottom_start = (0 * 60) + 0; | |
163 const uint32_t seconds_descend_and_bottomtime = seconds_descend + turbo_seconds_at_bottom_start + (2 * 60) + 0; | |
164 uint32_t time_elapsed_in_seconds; | |
165 static float ambient_pressure_mbar_memory = 0; | |
166 static uint32_t time_last_call = 0; | |
167 | |
168 if(divetime_in_seconds <= seconds_descend) | |
169 { | |
170 ambient_pressure_mbar = (divetime_in_seconds * descent_rate) + ((float)(subseconds) * descent_rate) + surface_pressure_mbar; | |
171 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
172 | |
173 time_last_call = divetime_in_seconds; | |
174 return 0; | |
175 } | |
176 else | |
177 if(divetime_in_seconds <= seconds_descend + turbo_seconds_at_bottom_start) | |
178 { | |
179 ambient_pressure_mbar = (seconds_descend * descent_rate) + surface_pressure_mbar; | |
180 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
181 ambient_pressure_mbar_memory = ambient_pressure_mbar; | |
182 time_last_call = divetime_in_seconds; | |
183 return turbo_seconds_at_bottom_start; | |
184 } | |
185 else | |
186 if(divetime_in_seconds <= seconds_descend_and_bottomtime) | |
187 { | |
188 ambient_pressure_mbar = (seconds_descend * descent_rate) + surface_pressure_mbar; | |
189 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
190 ambient_pressure_mbar_memory = ambient_pressure_mbar; | |
191 time_last_call = divetime_in_seconds; | |
192 return 0; | |
193 } | |
194 else | |
195 { | |
196 time_elapsed_in_seconds = divetime_in_seconds - time_last_call; | |
197 ambient_pressure_mbar = ambient_pressure_mbar_memory - time_elapsed_in_seconds * ascent_rate; | |
198 | |
199 if(ambient_pressure_mbar < surface_pressure_mbar) | |
200 ambient_pressure_mbar = surface_pressure_mbar; | |
201 else if(ambient_pressure_mbar < ceiling_mbar) | |
202 ambient_pressure_mbar = ceiling_mbar; | |
203 | |
204 ambient_temperature = demo_modify_temperature_helper(descent_rate * seconds_descend); | |
205 ambient_pressure_mbar_memory = ambient_pressure_mbar; | |
206 time_last_call = divetime_in_seconds; | |
207 return 0; | |
208 } | |
209 } | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
210 #endif |
38 | 211 |
212 | |
213 /* called just once on power on */ | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
214 /* TBD old DR5 code? */ |
38 | 215 void init_pressure_DRx(void) |
216 { | |
217 uint8_t resetCommand[1] = {0x1E}; | |
218 | |
219 I2C_Master_Transmit( DEVICE_PRESSURE, resetCommand, 1); | |
220 HAL_Delay(3); | |
221 | |
222 C[1] = get_ci_by_coef_num(0x02); | |
223 C[2] = get_ci_by_coef_num(0x04); | |
224 C[3] = get_ci_by_coef_num(0x06); | |
225 C[4] = get_ci_by_coef_num(0x08); | |
226 C[5] = get_ci_by_coef_num(0x0A); | |
227 C[6] = get_ci_by_coef_num(0x0C); | |
228 | |
229 C5_x_2p8 = C[5] * 256; | |
230 C2_x_2p16 = C[2] * 65536; | |
231 C1_x_2p15 = C[1] * 32768; | |
232 pressure_update(); | |
233 } | |
234 | |
235 uint8_t is_init_pressure_done(void) | |
236 { | |
237 return pressureSensorInitSuccess; | |
238 } | |
239 | |
240 uint8_t init_pressure(void) | |
241 { | |
242 uint8_t buffer[1]; | |
243 buffer[0] = 0x1e; | |
244 uint8_t retValue = 0xFF; | |
245 | |
246 | |
247 retValue = I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1); | |
248 if(retValue != HAL_OK) | |
249 { | |
250 return (HAL_StatusTypeDef)retValue; | |
251 } | |
252 HAL_Delay(3); | |
253 | |
254 for(uint8_t i=0;i<8;i++) | |
255 { | |
256 C[i] = get_ci_by_coef_num(i); | |
257 } | |
258 n_crc = crc4(C); // no evaluation at the moment hw 151026 | |
259 | |
260 C5_x_2p8 = C[5] * 256; | |
261 C2_x_2p16 = C[2] * 65536; | |
262 C1_x_2p15 = C[1] * 32768; | |
263 | |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
264 if(global.I2C_SystemStatus == HAL_OK) |
38 | 265 { |
266 pressureSensorInitSuccess = 1; | |
267 } | |
268 return pressure_update(); | |
269 } | |
270 | |
271 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
272 static uint32_t get_adc(void) |
38 | 273 { |
274 uint8_t buffer[1]; | |
275 uint8_t resivebuf[4]; | |
276 uint32_t answer = 0; | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
277 |
38 | 278 buffer[0] = 0x00; // Get ADC |
279 I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1); | |
280 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 4); | |
281 resivebuf[3] = 0; | |
282 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2]; | |
283 | |
284 return answer; | |
285 } | |
286 | |
287 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
288 static uint16_t get_ci_by_coef_num(uint8_t coef_num) |
38 | 289 { |
290 uint8_t resivebuf[2]; | |
291 | |
292 uint8_t cmd = CMD_PROM_RD+coef_num*2; | |
293 I2C_Master_Transmit( DEVICE_PRESSURE, &cmd, 1); | |
294 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 2); | |
295 return (256*(uint16_t)resivebuf[0]) + (uint16_t)resivebuf[1]; | |
296 } | |
297 | |
298 | |
299 | |
300 uint8_t pressure_update(void) | |
301 { | |
302 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT; | |
303 | |
304 statusReturn = pressure_sensor_get_data(); | |
305 pressure_calculation(); | |
306 return (uint8_t)statusReturn; | |
307 } | |
308 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
309 /* Switch between pressure and temperature measurement with every successful read operation */ |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
310 void pressure_update_alternating(void) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
311 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
312 static uint8_t getTemperature= 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
313 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
314 if(getTemperature) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
315 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
316 if(pressure_sensor_get_temperature_raw() == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
317 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
318 getTemperature = 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
319 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
320 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
321 else |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
322 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
323 if(pressure_sensor_get_pressure_raw() == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
324 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
325 getTemperature = 1; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
326 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
327 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
328 pressure_calculation(); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
329 return; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
330 } |
38 | 331 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
332 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn) |
38 | 333 { |
334 uint8_t command = CMD_ADC_CONV + cmd; | |
335 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT; | |
336 | |
337 statusReturnTemp = I2C_Master_Transmit( DEVICE_PRESSURE, &command, 1); | |
338 | |
339 if(statusReturn) | |
340 { | |
341 *statusReturn = statusReturnTemp; | |
342 } | |
343 | |
344 switch (cmd & 0x0f) // wait necessary conversion time | |
345 { | |
346 case CMD_ADC_256 : HAL_Delay(1); break; | |
347 case CMD_ADC_512 : HAL_Delay(3); break; | |
348 case CMD_ADC_1024: HAL_Delay(4); break; | |
349 case CMD_ADC_2048: HAL_Delay(6); break; | |
350 case CMD_ADC_4096: HAL_Delay(10); break; | |
351 } | |
352 return get_adc(); | |
353 } | |
354 | |
355 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
356 static HAL_StatusTypeDef pressure_sensor_get_data(void) |
38 | 357 { |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
358 uint32_t requestedValue = 0; |
38 | 359 HAL_StatusTypeDef statusReturn1 = HAL_TIMEOUT; |
360 HAL_StatusTypeDef statusReturn2 = HAL_TIMEOUT; | |
361 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
362 |
38 | 363 |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
364 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_1024, &statusReturn2); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
365 if (statusReturn2 == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
366 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
367 D2 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
368 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
369 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
370 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_1024, &statusReturn1); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
371 if (statusReturn1 == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
372 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
373 D1 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
374 } |
38 | 375 if(statusReturn2 > statusReturn1) // if anything is not HAL_OK (0x00) or worse |
376 return statusReturn2; | |
377 else | |
378 return statusReturn1; | |
379 } | |
380 | |
381 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
382 HAL_StatusTypeDef pressure_sensor_get_pressure_raw(void) |
38 | 383 { |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
384 uint32_t requestedValue = 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
385 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
386 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
387 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D1 + CMD_ADC_1024, &statusReturn); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
388 if (statusReturn == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
389 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
390 D1 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
391 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
392 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
393 return statusReturn; |
38 | 394 } |
395 | |
396 | |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
397 HAL_StatusTypeDef pressure_sensor_get_temperature_raw(void) |
38 | 398 { |
276
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
399 uint32_t requestedValue = 0; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
400 HAL_StatusTypeDef statusReturn = HAL_TIMEOUT; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
401 |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
402 requestedValue = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_1024, &statusReturn); |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
403 if (statusReturn == HAL_OK) |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
404 { |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
405 D2 = requestedValue; |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
406 } |
8e9c502c0b06
Rework pressure/ temperature data aquisition
ideenmodellierer
parents:
241
diff
changeset
|
407 return statusReturn; |
38 | 408 } |
409 | |
410 | |
411 void pressure_calculation(void) | |
412 { | |
241
2b9775f71e30
cleanup: factor out I2C1_Status() and cleanup type
Jan Mulder <jlmulder@xs4all.nl>
parents:
186
diff
changeset
|
413 if(global.I2C_SystemStatus != HAL_OK) |
38 | 414 return; |
415 | |
416 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(); | |
417 } | |
418 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
419 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void) |
38 | 420 { |
421 uint32_t local_D1; // ADC value of the pressure conversion | |
422 uint32_t local_D2; // ADC value of the temperature conversion | |
423 int32_t local_Px10; // compensated pressure value | |
424 int32_t local_Tx100; // compensated temperature value | |
425 int64_t local_dT; // int32_t, difference between actual and measured temperature | |
426 int64_t local_OFF; // offset at actual temperature | |
427 int64_t local_SENS; // sensitivity at actual temperature | |
428 | |
429 int64_t T2; | |
430 int64_t OFF2; | |
431 int64_t SENS2; | |
432 | |
433 local_D1 = D1; | |
434 local_D2 = D2; | |
435 | |
436 local_dT = ((int64_t)local_D2) - ((int64_t)C[5]) * 256; //pow(2,8); | |
437 local_OFF = ((int64_t)C[2]) * 65536 + local_dT * ((int64_t)C[4]) / 128; // pow(2,16), pow(2,7) | |
438 local_SENS = ((int64_t)C[1]) * 32768 + local_dT * ((int64_t)C[3]) / 256; // pow(2,15), pow(2,8) | |
439 | |
440 local_Tx100 = (int32_t)(2000 + (local_dT * ((int64_t)C[6])) / 8388608);// pow(2,23) | |
441 | |
442 | |
443 if(local_Tx100 < 2000) // low temperature | |
444 { | |
445 T2 = 3 * local_dT; | |
446 T2 *= local_dT; | |
447 T2 /= 8589934592; | |
448 | |
449 OFF2 = ((int64_t)local_Tx100) - 2000; | |
450 OFF2 *= OFF2; | |
451 OFF2 *= 3; | |
452 OFF2 /= 2; | |
453 | |
454 SENS2 = ((int64_t)local_Tx100) - 2000; | |
455 SENS2 *= SENS2; | |
456 SENS2 *= 5; | |
457 SENS2 /= 8; | |
458 | |
459 local_Tx100 -= (int32_t)T2; | |
460 local_OFF -= OFF2; | |
461 local_SENS -= SENS2; | |
462 } | |
463 else | |
464 { | |
465 T2 = 7 * local_dT; | |
466 T2 *= local_dT; | |
467 T2 /= 137438953472; | |
468 | |
469 OFF2 = ((int64_t)local_Tx100) - 2000; | |
470 OFF2 *= OFF2; | |
471 OFF2 /= 16; | |
472 | |
473 local_Tx100 -= (int32_t)T2; | |
474 local_OFF -= OFF2; | |
475 } | |
476 | |
477 local_Px10 = (int32_t)( | |
478 (((int64_t)((local_D1 * local_SENS) / 2097152)) - local_OFF) | |
479 / 8192 );// )) / 10; // pow(2,21), pow(2,13) | |
480 | |
481 ambient_temperature = ((float)local_Tx100) / 100; | |
482 ambient_pressure_mbar = ((float)local_Px10) / 10; | |
483 } | |
484 | |
485 | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
486 /* |
38 | 487 void pressure_calculation_new(void) |
488 { | |
489 #define POW2_8 (256) | |
490 #define POW2_17 (131072) | |
491 #define POW2_6 (64) | |
492 #define POW2_16 (65536) | |
493 #define POW2_7 (128) | |
494 #define POW2_23 (8388608) | |
495 #define POW2_21 (2097152) | |
496 #define POW2_15 (32768) | |
497 #define POW2_13 (8192) | |
498 #define POW2_37 (137438953472) | |
499 #define POW2_4 (16) | |
500 #define POW2_33 (8589934592) | |
501 #define POW2_3 (8) | |
502 | |
503 int32_t P; // compensated pressure value | |
504 int32_t T; // compensated temperature value | |
505 int32_t dT; // difference between actual and measured temperature | |
506 int64_t OFF; // offset at actual temperature | |
507 int64_t SENS; | |
508 | |
509 int32_t T2; | |
510 int64_t OFF2; | |
511 int64_t SENS2; | |
512 | |
513 dT = ((int32_t)D2) - ((int32_t)C[5]) * POW2_8; | |
514 OFF = ((int64_t)C[2]) * POW2_16 + ((int64_t)dT) * ((int64_t)C[4]) / POW2_7; | |
515 SENS = ((int64_t)C[1]) * POW2_15 + ((int64_t)dT) * ((int64_t)C[3]) / POW2_8; | |
516 | |
517 T = 2000 + (dT * ((int32_t)C[6])) / POW2_23; | |
518 | |
519 | |
520 if(T < 2000) // low temperature | |
521 { | |
522 T2 = 3 * dT * dT; | |
523 T2 /= POW2_33; | |
524 OFF2 = ((int64_t)T) - 2000; | |
525 OFF2 *= OFF2; | |
526 OFF2 *= 3; | |
527 OFF2 /= 2; | |
528 SENS2 = ((int64_t)T) - 2000; | |
529 SENS2 *= SENS2; | |
530 SENS2 *= 5; | |
531 SENS2 /= POW2_3; | |
532 } | |
533 else // high temperature | |
534 { | |
535 T2 = 7 * dT * dT; | |
536 T2 /= POW2_37; | |
537 OFF2 = ((int64_t)T) - 2000; | |
538 OFF2 *= OFF2; | |
539 OFF2 /= POW2_4; | |
540 SENS2 = 0; | |
541 } | |
542 | |
543 T = T - T2; | |
544 OFF = OFF - OFF2; | |
545 SENS = SENS - SENS2; | |
546 | |
547 P = (int32_t)(((((int64_t)D1) * SENS) / POW2_21 - OFF) / POW2_13); | |
548 | |
549 ambient_temperature = ((float)T) / 100; | |
550 ambient_pressure_mbar = ((float)P) / 10; | |
551 } | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
552 */ |
38 | 553 |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
554 /* |
38 | 555 void pressure_calculation_old(void) { |
556 // | |
557 double ambient_temperature_centigrad = 0; | |
558 double ambient_pressure_decimbar = 0; | |
559 | |
560 // static for debug | |
561 static int64_t dt = 0; | |
562 static int64_t temp = 0; | |
563 static int64_t ms_off = 0; | |
564 static int64_t sens = 0; | |
565 // | |
566 static int64_t ms_off2 = 0; | |
567 static int64_t sens2 = 0; | |
568 static int64_t t2 = 0; | |
569 | |
570 if((D2 == 0) || (D1 == 0)) | |
571 return; | |
572 // | |
573 | |
574 // dT = D2 - C[5] * POW2_8; | |
575 // T = 2000 + (dT * C[6]) / POW2_23; | |
576 dt = (int64_t)D2 - C5_x_2p8; | |
577 //temp ; // in 10 milliGrad Celcius | |
578 ambient_temperature_centigrad = 2000 + dt * C[6] / 8388608; | |
579 | |
580 | |
581 if(ambient_temperature_centigrad < 2000) // low temperature | |
582 { | |
583 t2 = 3 * dt; | |
584 t2 *= dt; | |
585 t2 /= 8589934592; | |
586 ms_off2 = ambient_temperature_centigrad - 2000; | |
587 ms_off2 *= ms_off2; | |
588 sens2 = ms_off2; | |
589 ms_off2 *= 3; | |
590 ms_off2 /= 2; | |
591 sens2 *= 5; | |
592 sens2 /= 8; | |
593 } | |
594 else // high temperature | |
595 { | |
596 t2 = 7 * dt; | |
597 t2 *= dt; | |
598 t2 /= 137438953472; | |
599 ms_off2 = ambient_temperature_centigrad - 2000; | |
600 ms_off2 *= ms_off2; | |
601 ms_off2 /= 16; | |
602 sens2 = 0; | |
603 } | |
604 | |
605 | |
606 // | |
607 | |
608 // pressure | |
609 // OFF = C[2] * POW2_16 + dT * C[4] / POW2_7; | |
610 // SENS = C[1] * POW2_15 + dT * C[3] / POW2_8; | |
611 ms_off = C[4] * dt; | |
612 ms_off /= 128; | |
613 ms_off += C2_x_2p16; | |
614 // | |
615 sens = C[3] * dt; | |
616 sens /= 256; | |
617 sens += C1_x_2p15; | |
618 | |
619 // 2nd order correction | |
620 ambient_temperature_centigrad -= t2; | |
621 ms_off -= ms_off2; | |
622 sens -= sens2; | |
623 | |
624 ambient_temperature = ambient_temperature_centigrad / 100; | |
625 // P = (D1 * SENS / POW2_21 - OFF) / POW2_13; | |
626 temp = D1 * sens; | |
627 temp /= 2097152; | |
628 temp -= ms_off; | |
629 temp /= 8192; | |
630 ambient_pressure_decimbar = temp; // to float/double | |
631 ambient_pressure_mbar = ambient_pressure_decimbar / 10; | |
632 } | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
633 */ |
38 | 634 |
635 | |
636 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011 | |
637 * short and int are both 16bit according to AVR/GCC google results | |
638 */ | |
186
f11f0bf6ef2d
cleanup: remove obsolete code, make static, etc.
Jan Mulder <jlmulder@xs4all.nl>
parents:
38
diff
changeset
|
639 static uint8_t crc4(uint16_t n_prom[]) |
38 | 640 { |
641 uint16_t cnt; // simple counter | |
642 uint16_t n_rem; // crc reminder | |
643 uint16_t crc_read; // original value of the crc | |
644 uint8_t n_bit; | |
645 n_rem = 0x00; | |
646 crc_read=n_prom[7]; //save read CRC | |
647 n_prom[7]=(0xFF00 & (n_prom[7])); //CRC byte is replaced by 0 | |
648 for (cnt = 0; cnt < 16; cnt++) // operation is performed on bytes | |
649 { // choose LSB or MSB | |
650 if (cnt%2==1) n_rem ^= (uint16_t) ((n_prom[cnt>>1]) & 0x00FF); | |
651 else n_rem ^= (uint16_t) (n_prom[cnt>>1]>>8); | |
652 for (n_bit = 8; n_bit > 0; n_bit--) | |
653 { | |
654 if (n_rem & (0x8000)) | |
655 { | |
656 n_rem = (n_rem << 1) ^ 0x3000; | |
657 } | |
658 else | |
659 { | |
660 n_rem = (n_rem << 1); | |
661 } | |
662 } | |
663 } | |
664 n_rem= (0x000F & (n_rem >> 12)); // // final 4-bit reminder is CRC code | |
665 n_prom[7]=crc_read; // restore the crc_read to its original place | |
666 return (n_rem ^ 0x00); | |
667 } | |
668 /* | |
669 void test_calculation(void) | |
670 { | |
671 C1 = 29112; | |
672 C2 = 26814; | |
673 C3 = 19125; | |
674 C4 = 17865; | |
675 C5 = 32057; | |
676 C6 = 31305; | |
677 | |
678 C2_x_2p16 = C2 * 65536; | |
679 C1_x_2p15 = C1 * 32768; | |
680 | |
681 D1 = 4944364; | |
682 D2 = 8198974; | |
683 pressure_calculation() ; | |
684 }; | |
685 */ | |
686 |