comparison Small_CPU/Src/pressure.c @ 356:cb3870f79e9d

Add Support for new end-2019 hardware: new pressure sensor MS5837 support with autodetect in pressure.c define the sensors in i2c.h minor typo fixed in scheduler.c advance version number and update release date in baseCPU2.c Beware: Messing up the I2C function may require factory maintance in gen 1 and gen 2 hardware!
author heinrichsweikamp
date Sat, 23 Nov 2019 15:36:38 +0100
parents 1f24022345d1
children 6f30f2011667
comparison
equal deleted inserted replaced
355:afa55a50cc72 356:cb3870f79e9d
59 #define PRESSURE_SURFACE_DETECT_STABLE_CNT (5u) /* Event count to detect stable condition */ 59 #define PRESSURE_SURFACE_DETECT_STABLE_CNT (5u) /* Event count to detect stable condition */
60 #define PRESSURE_SURFACE_UNSTABLE_LIMIT (50u) /* Define pressure as not stable if delta (mBar) is larger than this value */ 60 #define PRESSURE_SURFACE_UNSTABLE_LIMIT (50u) /* Define pressure as not stable if delta (mBar) is larger than this value */
61 #define PRESSURE_SURFACE_DETECT_UNSTABLE_CNT (3u) /* Event count to detect unstable condition */ 61 #define PRESSURE_SURFACE_DETECT_UNSTABLE_CNT (3u) /* Event count to detect unstable condition */
62 62
63 63
64 static uint8_t PRESSURE_ADDRESS = DEVICE_PRESSURE_MS5803; /* Default Address */
65
64 static uint16_t get_ci_by_coef_num(uint8_t coef_num); 66 static uint16_t get_ci_by_coef_num(uint8_t coef_num);
65 //void pressure_calculation_new(void); 67 //void pressure_calculation_new(void);
66 //void pressure_calculation_old(void); 68 //void pressure_calculation_old(void);
67 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void); 69 static void pressure_calculation_AN520_004_mod_MS5803_30BA__09_2015(void);
68 static uint8_t crc4(uint16_t n_prom[]); 70 //static uint8_t crc4(uint16_t n_prom[]);
69 71
70 static HAL_StatusTypeDef pressure_sensor_get_data(void); 72 static HAL_StatusTypeDef pressure_sensor_get_data(void);
71 static uint32_t get_adc(void); 73 static uint32_t get_adc(void);
72 uint8_t pressureSensorInitSuccess = 0; 74 uint8_t pressureSensorInitSuccess = 0;
73 75
74 static uint16_t C[8] = { 1 }; 76 static uint16_t C[8] = { 1 };
75 static uint32_t D1 = 1; 77 static uint32_t D1 = 1;
76 static uint32_t D2 = 1; 78 static uint32_t D2 = 1;
77 static uint8_t n_crc; 79 //static uint8_t n_crc;
78 80
79 static int64_t C5_x_2p8 = 1; 81 static int64_t C5_x_2p8 = 1;
80 static int64_t C2_x_2p16 = 1; 82 static int64_t C2_x_2p16 = 1;
81 static int64_t C1_x_2p15 = 1; 83 static int64_t C1_x_2p15 = 1;
82 84
336 } 338 }
337 339
338 uint8_t init_pressure(void) 340 uint8_t init_pressure(void)
339 { 341 {
340 uint8_t buffer[1]; 342 uint8_t buffer[1];
341 buffer[0] = 0x1e; 343 buffer[0] = 0x1E; // Reset Command
342 uint8_t retValue = 0xFF; 344 uint8_t retValue = 0xFF;
343 345
344 pressureSensorInitSuccess = false; 346 pressureSensorInitSuccess = false;
345 init_pressure_history(); 347 init_pressure_history();
346 348
349 /* Probe new sensor first */
350 retValue = I2C_Master_Transmit( DEVICE_PRESSURE_MS5837, buffer, 1);
351 if(retValue != HAL_OK)
352 {
353 PRESSURE_ADDRESS = DEVICE_PRESSURE_MS5803; // use old sensor
354 HAL_Delay(100);
355 MX_I2C1_Init();
356 if (global.I2C_SystemStatus != HAL_OK)
357 {
358 if (MX_I2C1_TestAndClear() == GPIO_PIN_RESET) {
359 MX_I2C1_TestAndClear(); // do it a second time
360 }
361 MX_I2C1_Init();
362 }
363 }
364 else
365 {
366 PRESSURE_ADDRESS = DEVICE_PRESSURE_MS5837; // Success, use new sensor
367 }
368 HAL_Delay(3); //2.8ms according to datasheet
369
370 buffer[0] = 0x1E; // Reset Command
371 retValue = 0xFF;
372
347 /* Send reset request to pressure sensor */ 373 /* Send reset request to pressure sensor */
348 retValue = I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1); 374 retValue = I2C_Master_Transmit( PRESSURE_ADDRESS, buffer, 1);
349 if(retValue != HAL_OK) 375 if(retValue != HAL_OK)
350 { 376 {
351 return (HAL_StatusTypeDef)retValue; 377 return (HAL_StatusTypeDef)retValue;
352 } 378 }
353 HAL_Delay(3); 379 HAL_Delay(3); //2.8ms according to datasheet
354 380
355 for(uint8_t i=0;i<8;i++) 381 for(uint8_t i=0;i<7;i++)
356 { 382 {
357 C[i] = get_ci_by_coef_num(i); 383 C[i] = get_ci_by_coef_num(i);
358 } 384 }
359 n_crc = crc4(C); // no evaluation at the moment hw 151026 385 // n_crc = crc4(C); // no evaluation at the moment hw 151026
360 386
361 C5_x_2p8 = C[5] * 256; 387 C5_x_2p8 = C[5] * 256;
362 C2_x_2p16 = C[2] * 65536; 388 C2_x_2p16 = C[2] * 65536;
363 C1_x_2p15 = C[1] * 32768; 389 C1_x_2p15 = C[1] * 32768;
364 390
377 uint8_t buffer[1]; 403 uint8_t buffer[1];
378 uint8_t resivebuf[4]; 404 uint8_t resivebuf[4];
379 uint32_t answer = 0; 405 uint32_t answer = 0;
380 406
381 buffer[0] = 0x00; // Get ADC 407 buffer[0] = 0x00; // Get ADC
382 I2C_Master_Transmit( DEVICE_PRESSURE, buffer, 1); 408 I2C_Master_Transmit( PRESSURE_ADDRESS, buffer, 1);
383 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 4); 409 I2C_Master_Receive( PRESSURE_ADDRESS, resivebuf, 4);
384 resivebuf[3] = 0; 410 resivebuf[3] = 0;
385 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2]; 411 answer = 256*256 *(uint32_t)resivebuf[0] + 256 * (uint32_t)resivebuf[1] + (uint32_t)resivebuf[2];
386 412
387 return answer; 413 return answer;
388 } 414 }
391 static uint16_t get_ci_by_coef_num(uint8_t coef_num) 417 static uint16_t get_ci_by_coef_num(uint8_t coef_num)
392 { 418 {
393 uint8_t resivebuf[2]; 419 uint8_t resivebuf[2];
394 420
395 uint8_t cmd = CMD_PROM_RD+coef_num*2; 421 uint8_t cmd = CMD_PROM_RD+coef_num*2;
396 I2C_Master_Transmit( DEVICE_PRESSURE, &cmd, 1); 422 I2C_Master_Transmit( PRESSURE_ADDRESS, &cmd, 1);
397 I2C_Master_Receive( DEVICE_PRESSURE, resivebuf, 2); 423 I2C_Master_Receive( PRESSURE_ADDRESS, resivebuf, 2);
398 return (256*(uint16_t)resivebuf[0]) + (uint16_t)resivebuf[1]; 424 return (256*(uint16_t)resivebuf[0]) + (uint16_t)resivebuf[1];
399 } 425 }
400 426
401 427
402 428
435 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn) 461 static uint32_t pressure_sensor_get_one_value(uint8_t cmd, HAL_StatusTypeDef *statusReturn)
436 { 462 {
437 uint8_t command = CMD_ADC_CONV + cmd; 463 uint8_t command = CMD_ADC_CONV + cmd;
438 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT; 464 HAL_StatusTypeDef statusReturnTemp = HAL_TIMEOUT;
439 465
440 statusReturnTemp = I2C_Master_Transmit( DEVICE_PRESSURE, &command, 1); 466 statusReturnTemp = I2C_Master_Transmit( PRESSURE_ADDRESS, &command, 1);
441 467
442 if(statusReturn) 468 if(statusReturn)
443 { 469 {
444 *statusReturn = statusReturnTemp; 470 *statusReturn = statusReturnTemp;
445 } 471 }
703 729
704 730
705 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011 731 /* taken from AN520 by meas-spec.com dated 9. Aug. 2011
706 * short and int are both 16bit according to AVR/GCC google results 732 * short and int are both 16bit according to AVR/GCC google results
707 */ 733 */
708 static uint8_t crc4(uint16_t n_prom[]) 734 /*static uint8_t crc4(uint16_t n_prom[])
709 { 735 {
710 uint16_t cnt; // simple counter 736 uint16_t cnt; // simple counter
711 uint16_t n_rem; // crc reminder 737 uint16_t n_rem; // crc reminder
712 uint16_t crc_read; // original value of the crc 738 uint16_t crc_read; // original value of the crc
713 uint8_t n_bit; 739 uint8_t n_bit;
732 } 758 }
733 n_rem= (0x000F & (n_rem >> 12)); // // final 4-bit reminder is CRC code 759 n_rem= (0x000F & (n_rem >> 12)); // // final 4-bit reminder is CRC code
734 n_prom[7]=crc_read; // restore the crc_read to its original place 760 n_prom[7]=crc_read; // restore the crc_read to its original place
735 return (n_rem ^ 0x00); 761 return (n_rem ^ 0x00);
736 } 762 }
737 /* 763
738 void test_calculation(void) 764 void test_calculation(void)
739 { 765 {
740 C1 = 29112; 766 C1 = 29112;
741 C2 = 26814; 767 C2 = 26814;
742 C3 = 19125; 768 C3 = 19125;