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