107 lines
3.5 KiB
C
107 lines
3.5 KiB
C
/**
|
|
* @file pressureSensor.c
|
|
*
|
|
* @copyright Nehemis SARL reserves all rights even in the event of industrial
|
|
* property rights. We reserve all rights of disposal such as
|
|
* copying and passing on to third parties.
|
|
*
|
|
* @brief
|
|
*
|
|
*/
|
|
|
|
/******************************************************************************
|
|
* Include Header Files
|
|
******************************************************************************/
|
|
#include "pressureSensor.h"
|
|
|
|
/******************************************************************************
|
|
* Type declarations
|
|
******************************************************************************/
|
|
|
|
/******************************************************************************
|
|
* Macro Constant Declarations
|
|
******************************************************************************/
|
|
#define PRESSURE_SENSOR_ADC_REF_VOLTAGE 3.3f
|
|
#define PRESSURE_SENSOR_ADC_RESOLUTION 4095.0f /* 12-bit ADC */
|
|
|
|
/******************************************************************************
|
|
* Private function Declarations
|
|
******************************************************************************/
|
|
static float PressureSensorReadVoltage(PressureSensorMain_st *pressureSensor_pst);
|
|
|
|
/******************************************************************************
|
|
* Extern Function Definitions
|
|
******************************************************************************/
|
|
void PressureSensorInit(PressureSensorMain_st *pressureSensor_pst,
|
|
ADC_HandleTypeDef *adcHandle_pst, uint32_t channel_u32)
|
|
{
|
|
if (pressureSensor_pst == NULL || adcHandle_pst == NULL)
|
|
{
|
|
/* ERROR */
|
|
}
|
|
|
|
pressureSensor_pst->adcHandle_pst = adcHandle_pst;
|
|
pressureSensor_pst->channel_u32 = channel_u32;
|
|
}
|
|
|
|
|
|
void PressureSensorGetVal(PressureSensorMain_st *pressureSensor_pst)
|
|
{
|
|
if (pressureSensor_pst == NULL)
|
|
{
|
|
/* ERROR */
|
|
}
|
|
|
|
pressureSensor_pst->rawT_f32 = (PressureSensorReadVoltage(pressureSensor_pst) - 0.5f) / 0.2f;
|
|
}
|
|
|
|
|
|
/******************************************************************************
|
|
* Private function Definitions
|
|
******************************************************************************/
|
|
/**
|
|
* @brief Function to read ADC voltage data which is directly coming from the sensor.
|
|
*
|
|
* @param pressureSensor_pst : Pointer to main strucutre of the module.
|
|
*
|
|
* @return ADC output voltage based on sensor reading.
|
|
*
|
|
*/
|
|
static float32 PressureSensorReadVoltage(PressureSensorMain_st *pressureSensor_pst)
|
|
{
|
|
uint32 adcVal_u32 = 0uL;
|
|
if (pressureSensor_pst == NULL || pressureSensor_pst->adcHandle_pst == NULL)
|
|
{
|
|
return 0.0f; /* Error Case */
|
|
}
|
|
|
|
ADC_ChannelConfTypeDef sConfig = {0};
|
|
sConfig.Channel = pressureSensor_pst->channel_u32; /* Set the channel specific to this flowmeter */
|
|
sConfig.Rank = ADC_REGULAR_RANK_1;
|
|
sConfig.SamplingTime = ADC_SAMPLETIME_12CYCLES_5;
|
|
|
|
if (HAL_ADC_ConfigChannel(pressureSensor_pst->adcHandle_pst, &sConfig) != HAL_OK)
|
|
{
|
|
return 0.0f; /* ERROR */
|
|
}
|
|
|
|
/* Start ADC conversion and poll for completion */
|
|
if (HAL_ADC_Start(pressureSensor_pst->adcHandle_pst) == HAL_OK)
|
|
{
|
|
if (HAL_ADC_PollForConversion(pressureSensor_pst->adcHandle_pst, HAL_MAX_DELAY) == HAL_OK)
|
|
{
|
|
adcVal_u32 = HAL_ADC_GetValue(pressureSensor_pst->adcHandle_pst);
|
|
}
|
|
HAL_ADC_Stop(pressureSensor_pst->adcHandle_pst);
|
|
}
|
|
else
|
|
{
|
|
/* ERROR */
|
|
}
|
|
|
|
/* Convert ADC value to voltage (assuming 12-bit resolution and 3.3V reference) */
|
|
float32 voltage_f32 = (float32)adcVal_u32 * (PRESSURE_SENSOR_ADC_REF_VOLTAGE / PRESSURE_SENSOR_ADC_RESOLUTION);
|
|
|
|
return voltage_f32;
|
|
}
|