DPS310 pressure sensor driver - unit issue

Hello all,

For DPS310 pressure sensor, to convert pressure unit from pascal to hectopascal, the pressure value has to be divided by 10^2. For example, from driver side, if we send 932.34 hPa (after conversion), the application retrieves it as 93234 hPa (without decimal). If we send the pressure in Pa value divided by 10^5, only then the application retrieves it as 932.34 hPa (as expected). Can someone reach out to this?

Thanks,
Subashini.

This is your driver and your application, right? So the problem is
entirely internal to your implementation. How do you expect to get help
here?

Mark Roddy

What data type are you using to communicate these values between your driver and application?

@Mark_Roddy, @MBond2 :

I used the application reference: https://github.com/microsoft/Windows-universal-samples/tree/main/Samples/Barometer/cpp

From driver: Here the resultant pressure value is stored in m_CachedData. Its datatype is float.

    m_Pressure = ((UINT32)DataBuffer[0] << 16) | ((UINT32)DataBuffer[1] << 8) | (UINT32)DataBuffer[2];
    TraceInformation("m_Pressure value before 2's complement %x", m_Pressure);
    TwosComplement(&m_Pressure, 24);
    TraceInformation("m_Pressure value after 2's complement %x", m_Pressure);
    TraceInformation("m_Pressure value after 2's complement (in float) %f", m_Pressure);
    m_Pressuresc = (float)m_Pressure / SCALE_FACTOR;
    TraceInformation("m_Pressuresc value = %f",m_Pressuresc);
    m_Pressurecomp = m_c00 + m_Pressuresc * (m_c10 + m_Pressuresc * (m_c20 + m_Pressuresc * m_c30)) + m_tempsc * m_c01 + m_tempsc * 
    m_Pressuresc * (m_c11 + m_Pressuresc * m_c21);
    TraceInformation("m_Pressurecomp value = %f",m_Pressurecomp);
    //m_CachedData = m_Pressurecomp/100;                            // This is the actual conversion
    m_CachedData = m_Pressurecomp/100000;                        
    TraceError("Calculated pressure %f", m_CachedData);

From application: BarometerReading is a class. Pressure value from driver is retrieved using its method GetCurrentReading().

void Scenario2_Polling::ScenarioGetData(Platform::Object^ sender, Windows::UI::Xaml::RoutedEventArgs^ e)
{
if (nullptr != sensor)
{
BarometerReading^ reading = sensor->GetCurrentReading();
if (nullptr != reading)
{
ScenarioOutput_hPa->Text = reading->StationPressureInHectopascals.ToString();
}
}
else
{
rootPage->NotifyUser(“No barometer found”, NotifyType::ErrorMessage);
}
}

OK, but where is the spec that tells you what units you need to store in m_CachedData? Does the hardware deliver Pa?

DPS310 datasheet is referred.

you clearly have some kind of data type or unit conversion problem. careful review of all declarations and interface contracts is how to find a problem like this. Code snips without the declarations and links to large documents don’t help much. But on thing to consider is an expression like this

m_Pressure = ((UINT32)DataBuffer[0] << 16) | ((UINT32)DataBuffer[1] << 8) | (UINT32)DataBuffer[2];

I spend most of my time reviewing code written by others and this is the sort of thing that I routinely insist is rewritten. Not because it produces the wrong result necessarily, but because it requires a detailed analysis of the operator associativity and precedence of each part to determine exactly what it means. And an equivalent sequence that is unambiguous will produce the same machine code.

please feel free to ignore my ranting on this point