bexus_pressure_spi.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #include "bexus_pressure_spi.h"
  2. uint8_t m_buff[3],spi_adresse[]={0xEF,0x48,0x1E,0x00,0xA0,0x58},prom_buff[16];//<-Buffer für eingehende PROM Byte-Kette
  3. // /\ /\
  4. // | |
  5. //Buffer für reingehende Byte-Kette |
  6. // |
  7. //Adresse vom CHIP(i2c, hier nichtmehr gebraucht),Druck-Conversion, Config, 1.PROM-Register, Temp-Conversion
  8. struct Dataset *data_spi;
  9. uint16_t cal_factor[6];
  10. int32_t comp_dt,comp_temp,comp_pressure;
  11. int64_t comp_off,comp_sens;
  12. void Pressure_Spi_Init() {
  13. //CS high
  14. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
  15. //Reset
  16. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
  17. HAL_SPI_Transmit(&hspi4,&spi_adresse[2],1,40);
  18. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
  19. //HAL_Delay(4);
  20. while(millis()<4);
  21. //read PROM
  22. for(int n=0; n<8;n+=2){
  23. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
  24. //HAL_SPI_TransmitReceive(&hspi4,&spi_adresse[4]+n,&prom_buff[n],2,40);
  25. HAL_SPI_Transmit(&hspi4,&spi_adresse[4]+n,1,40);
  26. HAL_SPI_Receive(&hspi4,&prom_buff[n],2,100);
  27. //HAL_Delay(3);
  28. while(millis()<3);
  29. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
  30. //HAL_Delay(3);
  31. while(millis()<13);
  32. }
  33. for(int i=0 ; i<6;i+=2){
  34. cal_factor[i]=prom_buff[i+1] + (prom_buff[i]<<8);
  35. }
  36. //HAL_Delay(10);
  37. }
  38. int Pressure_Spi_Read() {
  39. data_spi = &Dataset_Master[buff_sensor_data_index];
  40. /*--------------------read_sensor-------------------------*/
  41. //read pressure
  42. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
  43. if(HAL_OK == HAL_SPI_Transmit(&hspi4,&spi_adresse[1],1,40)){
  44. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
  45. HAL_Delay(3);
  46. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
  47. if(HAL_OK != HAL_SPI_TransmitReceive(&hspi4,&spi_adresse[3],m_buff,3,40)){
  48. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
  49. //return HAL_ERROR;
  50. }
  51. }
  52. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
  53. data_spi->pressure_spi_raw=m_buff[2]+(m_buff[1]<<8)+(m_buff[0]<<16);
  54. //read temperature
  55. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
  56. if(HAL_OK == HAL_SPI_Transmit(&hspi4,&spi_adresse[5],1,40)){
  57. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
  58. HAL_Delay(3);
  59. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_RESET);
  60. if(HAL_OK != HAL_SPI_TransmitReceive(&hspi4,&spi_adresse[3],m_buff,3,40)){
  61. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
  62. //return HAL_ERROR;
  63. }
  64. }
  65. HAL_GPIO_WritePin(GPIOG,GPIO_PIN_8,GPIO_PIN_SET);
  66. data_spi->temperature_spi_raw=m_buff[2]+(m_buff[1]<<8)+(m_buff[0]<<16);
  67. /*--------------------------------------------------------*/
  68. data_spi->pressure_spi=(data_spi->pressure_spi_raw-0xF00000)*.000024;
  69. data_spi->temperature_spi=(data_spi->temperature_spi_raw-0xF00000)*.000024;
  70. /*--------------------calculation-------------------------
  71. //pre-calc. compensation factors
  72. comp_dt = data_spi->temperature_spi_raw - cal_factor[4] * 256;
  73. comp_temp = 20 + comp_dt * cal_factor[5] / 8388608;
  74. comp_off = cal_factor[5] * 131072 + (cal_factor[3] * comp_dt) / 64;
  75. comp_sens = cal_factor[0] * 65536 + (cal_factor[2] * comp_dt) / 128;
  76. comp_pressure = (data_spi->pressure_spi_raw * comp_sens / 2097152 - comp_off) / 32768;
  77. //calculate more precise factors for lower temperatures
  78. if(data_spi->temperature_spi < 20){//°C
  79. data_spi->temperature_spi = comp_temp - (comp_dt * comp_dt) / 2147483648;
  80. if(data_spi->temperature_spi < -15){//°C
  81. comp_off = comp_off - (61 * (15 * ((comp_temp - 1500) *(comp_temp - 1500))) + (61 * ((comp_temp - 2000) *(comp_temp - 2000)) / 16));//faktor?
  82. comp_sens = comp_sens - (8 * ((comp_temp - 1500) *(comp_temp - 1500))) + (2 * ((comp_temp - 2000) *(comp_temp - 2000)));//faktor
  83. }
  84. else{
  85. comp_off = comp_off - (61 * ((comp_temp - 2000) *(comp_temp - 2000)) / 16);//faktor?
  86. comp_sens = comp_sens - (2 * ((comp_temp - 2000) *(comp_temp - 2000)));//faktor
  87. }
  88. }
  89. else{
  90. data_spi->temperature_spi = comp_temp;
  91. }
  92. //calculate pressure and temperature
  93. data_spi->pressure_spi = data_spi->pressure_spi *comp_sens - comp_off;
  94. data_spi->temperature_spi = 20 + comp_dt * data_spi->temperature_spi;
  95. ---------------------------------------------------------*/
  96. return HAL_OK;
  97. }