/*
|
* 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);
|
}
|