89 lines
1.6 KiB
C
89 lines
1.6 KiB
C
|
|
/*
|
||
|
|
* Baro_MS4525DO.c
|
||
|
|
*
|
||
|
|
* Created on: Jun 22, 2020
|
||
|
|
* Author: matth
|
||
|
|
*/
|
||
|
|
|
||
|
|
#include "Baro_MS4525DO_SPI.h"
|
||
|
|
|
||
|
|
bool Baro_MS4525S_init(Baro_MS4525S_t *baro, const char *name, SPI_DEV_t *dev, int Pmax, int Pmin, Baro_MS4525S_type typeAB)
|
||
|
|
{
|
||
|
|
baro->name = name;
|
||
|
|
baro->_dev = dev;
|
||
|
|
baro->Prange = 6895*(Pmax-Pmin);
|
||
|
|
baro->Pmin = 6895*Pmin;
|
||
|
|
baro->typeAB = typeAB;
|
||
|
|
baro->status = 0;
|
||
|
|
return true;
|
||
|
|
}
|
||
|
|
|
||
|
|
bool Baro_MS4525S_update(Baro_MS4525S_t *baro)
|
||
|
|
{
|
||
|
|
bool rslt;
|
||
|
|
rslt = false;
|
||
|
|
uint8_t buff[4];
|
||
|
|
|
||
|
|
if (!SPI_DEV_begin(baro->_dev,0)) {
|
||
|
|
baro->status = 1;
|
||
|
|
return rslt;
|
||
|
|
}
|
||
|
|
SPI_DEV_select(baro->_dev);
|
||
|
|
|
||
|
|
if (SPI_DEV_transfer(baro->_dev, NULL, 0, buff, 4, 2))
|
||
|
|
{
|
||
|
|
uint16_t bridge_data = ((buff[0] & 0x3F) << 8) + buff[1];
|
||
|
|
uint8_t status = ((buff[0] & 0xC0) >> 6);
|
||
|
|
uint16_t temp = (buff[2] << 3) + ((buff[3] & 0xE) >> 5);
|
||
|
|
|
||
|
|
if (status == 0x00)
|
||
|
|
{
|
||
|
|
switch (baro->typeAB)
|
||
|
|
{
|
||
|
|
case Baro_MS4525S_typeA:
|
||
|
|
if (bridge_data<0x666 || bridge_data>0x399A)
|
||
|
|
{
|
||
|
|
status = 3;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
baro->pressure = (bridge_data-0x666)*(baro->Prange)/(0x399A-0x666)+baro->Pmin;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
default:
|
||
|
|
if (bridge_data<0x333 || bridge_data>0x3CCB)
|
||
|
|
{
|
||
|
|
status = 3;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
baro->pressure = (bridge_data-0x333)*(baro->Prange)/(0x3CCB-0x333)+baro->Pmin;
|
||
|
|
}
|
||
|
|
break;
|
||
|
|
}
|
||
|
|
baro->temp = temp*200u/2047u-50;
|
||
|
|
if (status == 0)
|
||
|
|
{
|
||
|
|
baro->status = 0;
|
||
|
|
rslt = true;
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
baro->status = status;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
baro->status = (status << 2);
|
||
|
|
}
|
||
|
|
}
|
||
|
|
else
|
||
|
|
{
|
||
|
|
baro->status = 2;
|
||
|
|
}
|
||
|
|
|
||
|
|
SPI_DEV_unselect(baro->_dev);
|
||
|
|
SPI_DEV_end(baro->_dev);
|
||
|
|
return rslt;
|
||
|
|
}
|