zhyinch
2022-04-19 e1cb73d3e104a4dd0e52407cad1db5c177384f3a
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
/*
 * Copyright (c) 2015-2016 Infineon Technologies AG
 *
 * Driver for Infineon DPS310 Digital Barometric Pressure Sensor
 *
 *
 */
#include <stdio.h>
#include <stdlib.h>
#include "dps310.h"
 
#include "lis3dh_driver.h"
/*bus communication protocol stubs
* Please wrap around platform specific implementation
* for low level bus handling (I2C or SPI), that matches 
* prototypes provided by dps310_bus_connection structure
* in dps310.h
*/
void delay_us(uint32_t nTimer);
/* Should return -1 in case of failure otherwise valid contents*/
s16 test_read_byte(u8 ReadAddr)
{
    
  uint8_t temp=0;                                                                                   
    IIC2_Start();  
    IIC2_Send_Byte(0xee);         //·¢ËÍдÃüÁî
    IIC2_Wait_Ack();       
    IIC2_Send_Byte(ReadAddr);   //·¢ËͶÁµØÖ·
    IIC2_Wait_Ack();        
    IIC2_Start();              
    IIC2_Send_Byte(0xef);       //·¢ËͶÁÃüÁ½øÈë½ÓÊÕģʽ           
    IIC2_Wait_Ack();     
    temp=IIC2_Read_Byte(0);           
    IIC2_Stop();                                //²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ         
    return temp;
}
 
/* Should return -1 or negative value in case of failure otherwise length of 
* read contents in read_buffer
* and shall place read contents in read_buffer
*/
s16 test_read_block(u8 ReadAddr, u8 Len, u8 *read_buffer)
{
       uint8_t t;
    uint32_t temp=0;
    for(t=0;t<Len;t++)
    {
        *read_buffer=test_read_byte(ReadAddr+t);          
         read_buffer++;        
    }
    //return temp;    
    return Len;
}
 
/* Should return -1 in case of failure otherwise non negative number*/
s16 test_write_byte(u8 WriteAddr, u8 DataToWrite)
{
    IIC2_Start();  
    IIC2_Send_Byte(0xee);        //·¢ËÍдÃüÁî
    IIC2_Wait_Ack();
    IIC2_Send_Byte(WriteAddr);                    //·¢Ë͵ØÖ·
    IIC2_Wait_Ack();                                                                 
    IIC2_Send_Byte(DataToWrite);     //·¢ËÍ×Ö½Ú                               
    IIC2_Wait_Ack();                     
    IIC2_Stop();//²úÉúÒ»¸öÍ£Ö¹Ìõ¼þ 
    delay_us(10);    
}
 
/* Shall implement delay in milliseconds*/
void test_wait_ms(u8 delay)
{
     printf("Waiting for %dms\n",delay);
}
struct dps310_state drv_state;
f64 pressure,temperature; 
int BarInit(void)
{
 
    /*Instantiate driver state*/
   
 
    /*Instantiate bus connection callback holder*/
    dps310_bus_connection cnn;
   IIC2_Init();
   /* Assign/register platform specific bus handlers*/
    cnn.read_byte=&test_read_byte;
    cnn.read_block=&test_read_block;
    cnn.write_byte=&test_write_byte;
    cnn.delayms = &test_wait_ms;
    int ret = dps310_init(&drv_state,&cnn);
 ret = dps310_get_processed_data(&drv_state,&pressure,&temperature);
    GetPressAndHeight();
    return ret;
}
float Height;
float GetPressAndHeight(void)
{  u8 ret;    
    
    
    /*Instantiate driver state*/
  
    ret = dps310_get_processed_data(&drv_state,&pressure,&temperature);
    Height = 44330 * (1.0 - pow((pressure / 1013.25), (1 / 5.255)));
    if(pressure==0)
    Height = 0;
       
    return Height;
    //height=PressToHeight(pressure,temperature);
    // printf("get_processed_data ret val = %d, pressure = %lf, temp = %lf\n",ret,pressure,temperature);
   }