comparison Small_CPU/Src/pressure.c @ 186:f11f0bf6ef2d cleanup-2

cleanup: remove obsolete code, make static, etc. Some rather trivial cleanup things like putting demo code into ifdefs, making functions static where possible, and against my normal policy of hard removing unused code, commenting it out at this point in time. Somehow, I think that this commented code might be useful in the near future as a new pressure sensor is coming. And finally, fixed some typo's in comment. Signed-off-by: Jan Mulder <jlmulder@xs4all.nl>
author Jan Mulder <jlmulder@xs4all.nl>
date Fri, 15 Mar 2019 12:39:28 +0100
parents 5f11787b4f42
children 2b9775f71e30
comparison
equal deleted inserted replaced
185:1bb24fd3fc92 186:f11f0bf6ef2d
44 #define CMD_ADC_1024 0x04 // ADC OSR=1024 44 #define CMD_ADC_1024 0x04 // ADC OSR=1024
45 #define CMD_ADC_2048 0x06 // ADC OSR=2056 45 #define CMD_ADC_2048 0x06 // ADC OSR=2056
46 #define CMD_ADC_4096 0x08 // ADC OSR=4096 46 #define CMD_ADC_4096 0x08 // ADC OSR=4096
47 #define CMD_PROM_RD 0xA0 // Prom read command 47 #define CMD_PROM_RD 0xA0 // Prom read command
48 48
49 49 static uint16_t get_ci_by_coef_num(uint8_t coef_num);
50 //uint16_t get_ci(uint8_t cmd); 50 //void pressure_calculation_new(void);
51 //uint8_t get_ci_crc(void); 51 //void pressure_calculation_old(void);
52 uint16_t get_ci_by_coef_num(uint8_t coef_num); 52 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void);
53 void pressure_calculation_new(void); 53 static uint8_t crc4(uint16_t n_prom[]);
54 void pressure_calculation_old(void); 54
55 void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void); 55 static HAL_StatusTypeDef pressure_sensor_get_data(void);
56 56 static uint32_t get_adc(void);
57 uint8_t crc4(uint16_t n_prom[]);
58
59 HAL_StatusTypeDef pressure_sensor_get_data(void);
60 uint32_t get_adc(void);
61 uint8_t pressureSensorInitSuccess = 0; 57 uint8_t pressureSensorInitSuccess = 0;
62 58
63 //void test_calculation(void); 59 static uint16_t C[8] = { 1 };
64 60 static uint32_t D1 = 1;
65 uint16_t C[8] = { 1 }; 61 static uint32_t D2 = 1;
66 uint32_t D1 = 1; 62 static uint8_t n_crc;
67 uint32_t D2 = 1; 63
68 uint8_t n_crc; 64 static int64_t C5_x_2p8 = 1;
69 65 static int64_t C2_x_2p16 = 1;
70 int64_t C5_x_2p8 = 1; 66 static int64_t C1_x_2p15 = 1;
71 int64_t C2_x_2p16 = 1;
72 int64_t C1_x_2p15 = 1;
73 67
74 /* 68 /*
75 short C2plus10000 = -1; 69 short C2plus10000 = -1;
76 short C3plus200 = -1; 70 short C3plus200 = -1;
77 short C4minus250 = -1; 71 short C4minus250 = -1;
78 short UT1 = -1; 72 short UT1 = -1;
79 short C6plus100 = -1; 73 short C6plus100 = -1;
80 */ 74 */
81 75
82 float ambient_temperature = 0; 76 static float ambient_temperature = 0;
83 float ambient_pressure_mbar = 0; 77 static float ambient_pressure_mbar = 0;
84 float surface_pressure_mbar = 1000; 78 static float surface_pressure_mbar = 1000;
85 float surface_ring_mbar[31] = { 0 }; 79 static float surface_ring_mbar[31] = { 0 };
86 80
87 uint8_t secondCounterSurfaceRing = 0; 81 uint8_t secondCounterSurfaceRing = 0;
88 82
89 float get_temperature(void) 83 float get_temperature(void)
90 { 84 {
91 return ambient_temperature; 85 return ambient_temperature;
92 } 86 }
93 87
94 //float test = 1000;
95
96 float get_pressure_mbar(void) 88 float get_pressure_mbar(void)
97 { 89 {
98 // return test;
99 return ambient_pressure_mbar; 90 return ambient_pressure_mbar;
100 } 91 }
101
102 92
103 float get_surface_mbar(void) 93 float get_surface_mbar(void)
104 { 94 {
105 return surface_pressure_mbar; 95 return surface_pressure_mbar;
106 } 96 }
140 hole = 0; 130 hole = 0;
141 surface_pressure_mbar = surface_ring_mbar[hole]; 131 surface_pressure_mbar = surface_ring_mbar[hole];
142 surface_ring_mbar[hole] = 0; 132 surface_ring_mbar[hole] = 0;
143 } 133 }
144 134
145 135 #ifdef DEMOMODE
146 float demo_modify_temperature_helper(float bottom_mbar_diff_to_surface) 136 float demo_modify_temperature_helper(float bottom_mbar_diff_to_surface)
147 { 137 {
148 const float temperature_surface = 31.0; 138 const float temperature_surface = 31.0;
149 const float temperature_bottom = 14.0; 139 const float temperature_bottom = 14.0;
150 140
215 ambient_pressure_mbar_memory = ambient_pressure_mbar; 205 ambient_pressure_mbar_memory = ambient_pressure_mbar;
216 time_last_call = divetime_in_seconds; 206 time_last_call = divetime_in_seconds;
217 return 0; 207 return 0;
218 } 208 }
219 } 209 }
210 #endif
220 211
221 212
222 /* called just once on power on */ 213 /* called just once on power on */
214 /* TBD old DR5 code? */
223 void init_pressure_DRx(void) 215 void init_pressure_DRx(void)
224 { 216 {
225 uint8_t resetCommand[1] = {0x1E}; 217 uint8_t resetCommand[1] = {0x1E};
226 218
227 I2C_Master_Transmit( DEVICE_PRESSURE, resetCommand, 1); 219 I2C_Master_Transmit( DEVICE_PRESSURE, resetCommand, 1);
275 } 267 }
276 return pressure_update(); 268 return pressure_update();
277 } 269 }
278 270
279 271
280 uint32_t get_adc(void) 272 static uint32_t get_adc(void)
281 { 273 {
282 uint8_t buffer[1]; 274 uint8_t buffer[1];
283 uint8_t resivebuf[4]; 275 uint8_t resivebuf[4];
284 uint32_t answer = 0; 276 uint32_t answer = 0;
285 // 277
286 buffer[0] = 0x00; // Get ADC 278 buffer[0] = 0x00; // Get ADC
287 I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1); 279 I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1);
288 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 4); 280 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 4);
289 resivebuf[3] = 0; 281 resivebuf[3] = 0;
290 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2]; 282 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2];
291 283
292 return answer; 284 return answer;
293 } 285 }
294 286
295 287
296 uint16_t get_ci_by_coef_num(uint8_t coef_num) 288 static uint16_t get_ci_by_coef_num(uint8_t coef_num)
297 { 289 {
298 uint8_t resivebuf[2]; 290 uint8_t resivebuf[2];
299 291
300 uint8_t cmd = CMD_PROM_RD+coef_num*2; 292 uint8_t cmd = CMD_PROM_RD+coef_num*2;
301 I2C_Master_Transmit( DEVICE_PRESSURE, &cmd, 1); 293 I2C_Master_Transmit( DEVICE_PRESSURE, &cmd, 1);
313 pressure_calculation(); 305 pressure_calculation();
314 return (uint8_t)statusReturn; 306 return (uint8_t)statusReturn;
315 } 307 }
316 308
317 309
318 uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn) 310 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn)
319 { 311 {
320 uint8_t command = CMD_ADC_CONV + cmd; 312 uint8_t command = CMD_ADC_CONV + cmd;
321 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT; 313 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT;
322 314
323 statusReturnTemp = I2C_Master_Transmit( DEVICE_PRESSURE, &command, 1); 315 statusReturnTemp = I2C_Master_Transmit( DEVICE_PRESSURE, &command, 1);
337 } 329 }
338 return get_adc(); 330 return get_adc();
339 } 331 }
340 332
341 333
342 HAL_StatusTypeDef pressure_sensor_get_data(void) 334 static HAL_StatusTypeDef pressure_sensor_get_data(void)
343 { 335 {
344 HAL_StatusTypeDef statusReturn1 = HAL_TIMEOUT; 336 HAL_StatusTypeDef statusReturn1 = HAL_TIMEOUT;
345 HAL_StatusTypeDef statusReturn2 = HAL_TIMEOUT; 337 HAL_StatusTypeDef statusReturn2 = HAL_TIMEOUT;
346 338
347 D2 = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_4096, &statusReturn1); 339 D2 = pressure_sensor_get_one_value(CMD_ADC_D2 + CMD_ADC_4096, &statusReturn1);
370 { 362 {
371 if(I2C1_Status() != HAL_OK) 363 if(I2C1_Status() != HAL_OK)
372 return; 364 return;
373 365
374 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(); 366 pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015();
375 return; 367 }
376 368
377 // before October 2016: pressure_calculation_old(); 369 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void)
378
379 // pressure_calculation_new();
380 }
381
382 void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void)
383 { 370 {
384 uint32_t local_D1; // ADC value of the pressure conversion 371 uint32_t local_D1; // ADC value of the pressure conversion
385 uint32_t local_D2; // ADC value of the temperature conversion 372 uint32_t local_D2; // ADC value of the temperature conversion
386 int32_t local_Px10; // compensated pressure value 373 int32_t local_Px10; // compensated pressure value
387 int32_t local_Tx100; // compensated temperature value 374 int32_t local_Tx100; // compensated temperature value
444 ambient_temperature = ((float)local_Tx100) / 100; 431 ambient_temperature = ((float)local_Tx100) / 100;
445 ambient_pressure_mbar = ((float)local_Px10) / 10; 432 ambient_pressure_mbar = ((float)local_Px10) / 10;
446 } 433 }
447 434
448 435
436 /*
449 void pressure_calculation_new(void) 437 void pressure_calculation_new(void)
450 { 438 {
451 #define POW2_8 (256) 439 #define POW2_8 (256)
452 #define POW2_17 (131072) 440 #define POW2_17 (131072)
453 #define POW2_6 (64) 441 #define POW2_6 (64)
509 P = (int32_t)(((((int64_t)D1) * SENS) / POW2_21 - OFF) / POW2_13); 497 P = (int32_t)(((((int64_t)D1) * SENS) / POW2_21 - OFF) / POW2_13);
510 498
511 ambient_temperature = ((float)T) / 100; 499 ambient_temperature = ((float)T) / 100;
512 ambient_pressure_mbar = ((float)P) / 10; 500 ambient_pressure_mbar = ((float)P) / 10;
513 } 501 }
514 502 */
515 503
504 /*
516 void pressure_calculation_old(void) { 505 void pressure_calculation_old(void) {
517 // 506 //
518 double ambient_temperature_centigrad = 0; 507 double ambient_temperature_centigrad = 0;
519 double ambient_pressure_decimbar = 0; 508 double ambient_pressure_decimbar = 0;
520 509
526 // 515 //
527 static int64_t ms_off2 = 0; 516 static int64_t ms_off2 = 0;
528 static int64_t sens2 = 0; 517 static int64_t sens2 = 0;
529 static int64_t t2 = 0; 518 static int64_t t2 = 0;
530 519
531 /* info
532 uint16_t C[8] = { 1 };
533 uint32_t D1 = 1;
534 uint32_t D2 = 1;
535 uint8_t n_crc;
536 */
537 if((D2 == 0) || (D1 == 0)) 520 if((D2 == 0) || (D1 == 0))
538 return; 521 return;
539 // 522 //
540 523
541 // dT = D2 - C[5] * POW2_8; 524 // dT = D2 - C[5] * POW2_8;
595 temp -= ms_off; 578 temp -= ms_off;
596 temp /= 8192; 579 temp /= 8192;
597 ambient_pressure_decimbar = temp; // to float/double 580 ambient_pressure_decimbar = temp; // to float/double
598 ambient_pressure_mbar = ambient_pressure_decimbar / 10; 581 ambient_pressure_mbar = ambient_pressure_decimbar / 10;
599 } 582 }
583 */
600 584
601 585
602 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011 586 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011
603 * short and int are both 16bit according to AVR/GCC google results 587 * short and int are both 16bit according to AVR/GCC google results
604 */ 588 */
605 uint8_t crc4(uint16_t n_prom[]) 589 static uint8_t crc4(uint16_t n_prom[])
606 { 590 {
607 uint16_t cnt; // simple counter 591 uint16_t cnt; // simple counter
608 uint16_t n_rem; // crc reminder 592 uint16_t n_rem; // crc reminder
609 uint16_t crc_read; // original value of the crc 593 uint16_t crc_read; // original value of the crc
610 uint8_t n_bit; 594 uint8_t n_bit;