chen
2024-09-20 292ed46c6066d47289f1330b1c2bcc6d74761f95
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * 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 "mainex.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;
   
   /* Assign/register platform specific bus handlers*/
    cnn.read_byte=&test_read_byte;
    cnn.read_block=&test_read_block;
    cnn.write_byte=&test_write_byte;
 
   /*If platform doesn't support delay or sleep
    *please assign NULL to this callback i.e cnn.delayms = NULL
    */
    cnn.delayms = &test_wait_ms;
 
    /*First call _init
     * this function verifies chip with appropriate id and 
     * reads and stores calibration data, configures the sensor 
     * to meet default configuration set in dps310.h.
     * This also puts the sensor in background mode
     * making it measure both pressure and temperature continuously    
     */
    int ret = dps310_init(&drv_state,&cnn);
 
 //   printf("init ret val = %d\n",ret);
 
    /* once sensor is put into background mode
     * pressure and temperature values are available and can be read
     * Here get_processed_data synchrounuously reads 6 bytes of raw data
     * and returns computed double precision pressure and temperature value
     */ 
    ret = dps310_get_processed_data(&drv_state,&pressure,&temperature);
//    printf("get_processed_data ret val = %d, pressure = %lf, temp = %lf\n",ret,pressure,temperature);
 
   /*To change configuration we first need to put sensor in
    *idle mode by calling _standby
    */
//    ret = dps310_standby(&drv_state);
 
//    printf("standby ret val = %d\n",ret);
 
//    /* Now lets call _config to meet different output data rate (ODR)
//     * and oversampling rate (OSR) based on scenario and usecase
//     * For valid combinations please refer to Page 25 of datasheet
//     */
//    ret = dps310_config(&drv_state,
//                       OSR_2,
//                       TMP_MR_2,
//                       OSR_8,
//                       PM_MR_64,
//                       drv_state.tmp_ext);
 
//    printf("config ret val = %d\n",ret);
//   
//    /*Resume the sensor in background mode again*/
//    ret = dps310_resume(&drv_state);
 
//    printf("resume ret val = %d\n",ret);
   
    return 0;
}
float PressToHeight(float press,float temper)
{
    float temp1,temp2;
    temp1 = 8.51*temper/284;
    temp2 = log10f(101325/press);
    return temp1*temp2;
}
float height;
void GetPressAndTemp(void)
{  u8 ret;    
 
    /*Instantiate driver state*/
  
    ret = dps310_get_processed_data(&drv_state,&pressure,&temperature);
    //height=PressToHeight(pressure,temperature);
     printf("get_processed_data ret val = %d, pressure = %lf, temp = %lf\n",ret,pressure,temperature);
   }