Skip to content

Commit

Permalink
Merge pull request #225 from es-ude/214-import-pac193x-sensor-library…
Browse files Browse the repository at this point in the history
…-improvements

214 import pac193x sensor library improvements
  • Loading branch information
DavidFederl authored Jun 29, 2024
2 parents f90a4ed + 819b73b commit 2527541
Show file tree
Hide file tree
Showing 11 changed files with 660 additions and 469 deletions.
428 changes: 213 additions & 215 deletions src/sensor/pac193x/Pac193x.c

Large diffs are not rendered by default.

67 changes: 31 additions & 36 deletions src/sensor/pac193x/Pac193xInternal.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@
#include <stdbool.h>
#include <stdint.h>

typedef union pac193xInternalDataBuffer {
uint64_t value;
uint8_t raw[8];
} pac193xInternalDataBuffer_t;

/* region SENSOR COMMUNICATION */

static pac193xErrorCode_t pac193xInternalCheckSensorAvailable(pac193xSensorConfiguration_t sensor);

static pac193xErrorCode_t
pac193xInternalSetDefaultConfiguration(pac193xSensorConfiguration_t sensor);

/*! send configuration to the sensor
/*!
* @brief send configuration to the sensor
*
* @param registerToWrite[in] address of the register where the settings should
* be stored
Expand All @@ -24,31 +30,8 @@ pac193xInternalSendConfigurationToSensor(pac193xSensorConfiguration_t sensor,
pac193xRegisterAddress_t registerToWrite,
pac193xSettings_t settingsToWrite);

/*! triggers reload of configuration and freezes accumulator
*
* \Information
* 1. reloads configuration\n
* 2. store current values of accumulator until next execution of refresh()\n
* 3. resets the accumulator
*
* @param sensor[in] sensor to refresh
* @return returns the error code (0 if everything passed)
*/
static pac193xErrorCode_t pac193xInternalRefresh(pac193xSensorConfiguration_t sensor);

/*! triggers reload of configuration and freezes accumulator
*
* \Information
* 1. reloads configuration\n
* 2. store current values of accumulator until next execution of refresh()\n
* 3. \b NOT resetting the accumulator
*
* @param sensor[in] sensor to refresh
* @return returns the error code (0 if everything passed)
*/
static pac193xErrorCode_t pac193xInternalRefreshV(pac193xSensorConfiguration_t sensor);

/*! send request for register to read to sensor
/*!
* @brief send request for register to read to sensor
*
* @param registerToRead[in] address of register to read
* @return return the error code (0 if everything passed)
Expand All @@ -57,7 +40,8 @@ static pac193xErrorCode_t
pac193xInternalSendRequestToSensor(pac193xSensorConfiguration_t sensor,
pac193xRegisterAddress_t registerToRead);

/*! receive data from sensor
/*!
* @brief receive data from sensor
*
* @param responseBuffer[out] byte buffer where the received will be stored
* @param sizeOfResponseBuffer[in] size of the buffer for the response
Expand All @@ -68,8 +52,10 @@ static pac193xErrorCode_t pac193xInternalReceiveDataFromSensor(pac193xSensorConf
uint8_t *responseBuffer,
uint8_t sizeOfResponseBuffer);

/*! requests and receives data from the sensor\n
* combines: pac193xInternalSendRequestToSensor(...) and pac193xInternalReceiveDataFromSensor(...)
/*!
* @brief requests and receives data from the sensor
*
* combines: \a pac193xInternalSendRequestToSensor and \a pac193xInternalReceiveDataFromSensor
*
* @param responseBuffer[out]
* @param sizeOfResponseBuffer[in]
Expand All @@ -90,9 +76,10 @@ static bool pac193xInternalCheckChannelIsActive(pac193xUsedChannels_t usedChanne

static uint8_t pac193xInternalTranslateChannelToRSenseArrayIndex(pac193xChannel_t channel);

/*! store adequate properties for the requested measurement
/*!
* @brief store adequate properties for the requested measurement
*
* \Information
* @information
* - register to start read\n
* - function to convert raw data\n
* - number of requested bytes
Expand All @@ -119,17 +106,25 @@ static uint64_t pac193xInternalTransformResponseBufferToUInt64(const uint8_t *re

static float pac193xInternalConvertToFloat(uint64_t input);

static float pac193xInternalCalculateAccumulatorCount(uint64_t input,
__attribute((__unused__)) float resistor,
__attribute((__unused__)) uint8_t sampleRate);

static float pac193xInternalCalculateVoltageOfSense(uint64_t input,
__attribute__((unused)) float resistor);
__attribute((__unused__)) float resistor,
__attribute((__unused__)) uint8_t sampleRate);

static float pac193xInternalCalculateVoltageOfSource(uint64_t input,
__attribute__((unused)) float resistor);
__attribute((__unused__)) float resistor,
__attribute((__unused__)) uint8_t sampleRate);

static float pac193xInternalCalculateCurrentOfSense(uint64_t input, float resistor);
static float pac193xInternalCalculateCurrentOfSense(uint64_t input, float resistor,
__attribute((__unused__)) uint8_t sampleRate);

static float pac193xInternalCalculateActualPower(uint64_t input, float resistor);
static float pac193xInternalCalculatePower(uint64_t input, float resistor,
__attribute((__unused__)) uint8_t sampleRate);

static float pac193xInternalCalculateEnergy(uint64_t input, float resistor);
static float pac193xInternalCalculateEnergy(uint64_t input, float resistor, uint8_t sampleRate);

/* endregion DATA TRANSFORMATION */

Expand Down
2 changes: 1 addition & 1 deletion src/sensor/pac193x/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ int main(void) {

// Example: Read Values from Channel
pac193xMeasurements_t measurements;
errorCode = pac193xGetAllMeasurementsForChannel(sensor, PAC193X_CHANNEL01, &measurements);
errorCode = pac193xGetMeasurementsForChannel(sensor, PAC193X_CHANNEL01, &measurements);
if (errordCode != PAC193X_NO_ERROR) {
return errorCode;
}
Expand Down
165 changes: 97 additions & 68 deletions src/sensor/pac193x/include/Pac193x.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,19 @@
#include <stdint.h>

// tag::prototypes[]
/*
* IMPORTANT:
* We highly recommend using the settings from the `enV5_hw_configuration`-library,
* to set up the sensor configuration struct!
*/

/* region GENERAL FUNCTIONS */

/*!
* @brief power up the sensor by setting PWRDN Pin to HIGH
* @IMPORTANT We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
*
* @warning needs ~1.5ms to reach idle state after power up
*
*
* @param sensor[in] configuration for sensor to use
* @return returns the error code (0 if everything passed)
Expand All @@ -19,8 +27,8 @@ pac193xErrorCode_t pac193xPowerUpSensor(pac193xSensorConfiguration_t sensor);

/*!
* @brief power down the sensor by setting PWRDN Pin to LOW
* @IMPORTANT - After powered up again all settings will be set to default!
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
*
* @important After powered up again all settings will be set to default!
*
* @param sensor[in] configuration for sensor to use
* @return returns the error code (0 if everything passed)
Expand All @@ -29,12 +37,10 @@ pac193xErrorCode_t pac193xPowerDownSensor(pac193xSensorConfiguration_t sensor);

/*!
* @brief initializes the power sensor
* @IMPORTANT
* - function has to be called before the sensor can be used \n
* needs max 1.5ms for idle state after power up \n
* function pac193xPowerUpSensor(...) has to be called beforehand
* @IMPORTANT
* - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
*
* @warning requires 1ms to reach idle state
*
* @important \a pac193xPowerUpSensor has to be called beforehand!
*
* @param sensor[in] configuration for sensor to use
* @return returns the error code (0 if everything passed)
Expand All @@ -43,20 +49,72 @@ pac193xErrorCode_t pac193xInit(pac193xSensorConfiguration_t sensor);

/*!
* @brief updates the number of used channels
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
*
* @param sensor[in] sensor configuration with updated channels
* @return returns the error code (0 if everything passed)
*/
pac193xErrorCode_t pac193xSetChannelsInUse(pac193xSensorConfiguration_t sensor);

/* endregion GENERAL FUNCTIONS */
/*!
* @brief start continuous measurement as accumulator/average with 1024 samples/second
*
* @param sensor[in] configuration of the sensor to use
* @param sampleRate[in] sample rate to be used
* @return returns the error code (0 if everything passed)
*/
pac193xErrorCode_t pac193xStartAccumulation(pac193xSensorConfiguration_t sensor);

/*!
* @brief stop continuous measurement and return to single shot mode
*
* @param sensor[in] configuration of the sensor to use
* @return returns the error code (0 if everything passed)
*/
pac193xErrorCode_t pac193XStopAccumulation(pac193xSensorConfiguration_t sensor);

/* region SINGLE SHOT MEASUREMENTS */
/*!
* @brief set the sample rate for the sensor in accumulation mode
*
* @param sensor[in] configuration of the sensor to use
* @param sampleRate[in] sample rate to be used
* @return returns the error code (0 if everything passed)
*
*/
pac193xErrorCode_t pac193xSetSampleRate(pac193xSensorConfiguration_t sensor,
pac193xSampleRate_t sampleRate);

/*!
* @brief this function transfers the latest accumulated values to the data register
* and \b resets all accumulating registers afterward.
*
* @important the values can be read from the data registers until another Refresh command is
* issued!
*
* @warning sensor needs 1ms to reach idle state
*
* @param sensor[in] configuration if the sensor to use
* @return returns the error code (0 if everything passed)
*/
pac193xErrorCode_t pac193xRefreshDataAndResetAccumulator(pac193xSensorConfiguration_t sensor);

/*!
* @brief this function transfers the latest accumulated values to the data register
* \b without resetting the accumulating registers.
*
* @important the values can be read from the data registers until another Refresh command is
* issued!
*
* @warning sensor needs 1ms to reach idle state
*
* @param sensor[in] configuration if the sensor to use
* @return returns the error code (0 if everything passed)
*/
pac193xErrorCode_t pac193xRefreshData(pac193xSensorConfiguration_t sensor);

/* endregion GENERAL FUNCTIONS */

/*!
* @brief retrieve the production information from the sensor
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
*
* @param sensor[in] configuration of the sensor to use
* @param info[out] struct that holds the information read from the sensor
Expand All @@ -65,9 +123,13 @@ pac193xErrorCode_t pac193xSetChannelsInUse(pac193xSensorConfiguration_t sensor);
pac193xErrorCode_t pac193xGetSensorInfo(pac193xSensorConfiguration_t sensor,
pac193xSensorId_t *info);

/* region READ MEASUREMENTS */

/*!
* @brief read a specific value from the sensor for a specific channel
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
*
* @important
* \a pac193xRefreshDataAndResetAccumulator or \a pac193xRefreshData has to be called beforehand!
*
* @param sensor[in] configuration of the sensor to use
* @param channel[in] channel where the measurement should be taken from
Expand All @@ -82,79 +144,46 @@ pac193xErrorCode_t pac193xGetMeasurementForChannel(pac193xSensorConfiguration_t

/*!
* @brief read \b all available single shot values from the sensor for a specific channel
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
*
* @param sensor[in] configuration of the sensor to use
* @param channel[in] channel where the measurement should be taken from
* @param measurements[out] memory where the struct with the measured values will be stored
* @return returns the error code (0 if everything passed)
*/
pac193xErrorCode_t pac193xGetAllMeasurementsForChannel(pac193xSensorConfiguration_t sensor,
pac193xChannel_t channel,
pac193xMeasurements_t *measurements);

/* endregion SINGLE SHOT MEASUREMENTS */

/* region CONTINUOUS MEASUREMENTS */
pac193xErrorCode_t pac193xGetMeasurementsForChannel(pac193xSensorConfiguration_t sensor,
pac193xChannel_t channel,
pac193xMeasurements_t *measurements);

/*!
* @brief start continuous measurement as accumulator/average with 1024 samples/second
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
* @brief read all rolling averages from sensor
*
* @param sensor[in] configuration of the sensor to use
* @return returns the error code (o if everything passed)
*/
pac193xErrorCode_t pac193xStartAccumulation(pac193xSensorConfiguration_t sensor);

/*!
* @brief stop continuous measurement and return to single shot mode
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
*
* @param sensor[in] configuration of the sensor to use
* @return returns the error code (o if everything passed)
*/
pac193xErrorCode_t pac193XStopAccumulation(pac193xSensorConfiguration_t sensor);

/*!
* @brief get the counter of accumulated values and the accumulated power values
* for all channels of the sensor
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
* @important
* \a pac193xRefreshDataAndResetAccumulator or \a pac193xRefreshData has to be called beforehand!
*
* @param sensor[in] configuration of the sensor to use
* @param channel[in] channel where the measurement should be taken from
* @param measurements[out] memory where the struct with the measured values will be stored
* @return returns the error code (o if everything passed)
* @return returns the error code (0 if everything passed)
*/
pac193xErrorCode_t
pac193xReadAccumulatedPowerForAllChannels(pac193xSensorConfiguration_t sensor,
pac193xPowerMeasurements_t *measurements);
pac193xErrorCode_t pac193xGetAveragesForChannel(pac193xSensorConfiguration_t sensor,
pac193xChannel_t channel,
pac193xMeasurements_t *measurements);

/*!
* @brief read an rolling average value of from sensor
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
* @brief get the counter of accumulated values and the accumulated power values
* for all channels of the sensor
*
* @param sensor[in] configuration of the sensor to use
* @param channel[in] channel where the measurement should be taken from
* @param valueToMeasure[in] value to be measured
* @param value[out] memory where the retrieved value will be stored
* @return returns the error code (o if everything passed)
*/
pac193xErrorCode_t pac193xReadAverageMeasurement(pac193xSensorConfiguration_t sensor,
pac193xChannel_t channel,
pac193xValueToMeasure_t valueToMeasure,
float *value);

/*!
* @brief read an rolling average value of from sensor
* @IMPORTANT - We highly recommend using the "enV5_hw_configuration_rev_[x]" -library
* @important
* \a pac193xRefreshDataAndResetAccumulator or \a pac193xRefreshData has to be called beforehand!
*
* @param sensor[in] configuration of the sensor to use
* @param channel[in] channel where the measurement should be taken from
* @param measurements[out] memory where the struct with the measured values will be stored
* @return returns the error code (o if everything passed)
* @return returns the error code (0 if everything passed)
*/
pac193xErrorCode_t pac193xReadAllAverageMeasurementsForChannel(pac193xSensorConfiguration_t sensor,
pac193xChannel_t channel,
pac193xMeasurements_t *measurements);
/* endregion CONTINUOUS MEASUREMENTS */
pac193xErrorCode_t pac193xReadEnergyForAllChannels(pac193xSensorConfiguration_t sensor,
pac193xEnergyMeasurements_t *measurements);

/* endregion ACCUMULATED MEASUREMENTS */
// end::prototypes[]

#endif /* ENV5_PAC193X_HEADER */
Loading

0 comments on commit 2527541

Please sign in to comment.