diff --git a/src/sensor/pac193x/CMakeLists.txt b/src/sensor/pac193x/CMakeLists.txt index 25e8b081..58f3bd1d 100644 --- a/src/sensor/pac193x/CMakeLists.txt +++ b/src/sensor/pac193x/CMakeLists.txt @@ -6,4 +6,6 @@ target_link_libraries(sensor_lib_pac193x INTERFACE common_lib sleep_interface gpio_interface - i2c_interface) + i2c_interface + CException +) diff --git a/src/sensor/pac193x/Pac193x.c b/src/sensor/pac193x/Pac193x.c index 9582d09f..f12eda78 100644 --- a/src/sensor/pac193x/Pac193x.c +++ b/src/sensor/pac193x/Pac193x.c @@ -3,6 +3,8 @@ #include #include +#include "CException.h" + #include "Common.h" #include "Gpio.h" #include "I2c.h" @@ -36,338 +38,230 @@ static const float pac193xInternalEnergyDenominator = (float)(1ULL << 28); /* region GENERAL FUNCTIONS */ -pac193xErrorCode_t pac193xPowerUpSensor(pac193xSensorConfiguration_t sensor) { - gpioErrorCode_t gpioErrorCode = gpioInitPin(sensor.powerPin, GPIO_OUTPUT); +void pac193xPowerUpSensor(const pac193xSensorConfiguration_t *sensor) { + gpioErrorCode_t gpioErrorCode = gpioInitPin(sensor->powerPin, GPIO_OUTPUT); if (gpioErrorCode != GPIO_NO_ERROR) { - return PAC193X_INIT_ERROR; + Throw(PAC193X_INIT_ERROR); } - gpioSetPin(sensor.powerPin, GPIO_PIN_LOW); + gpioSetPin(sensor->powerPin, GPIO_PIN_LOW); /* sleep to make sure the sensor is idle */ sleep_for_ms(10); - - return PAC193X_NO_ERROR; } -pac193xErrorCode_t pac193xPowerDownSensor(pac193xSensorConfiguration_t sensor) { - gpioErrorCode_t gpioErrorCode = gpioInitPin(sensor.powerPin, GPIO_OUTPUT); +void pac193xPowerDownSensor(const pac193xSensorConfiguration_t *sensor) { + gpioErrorCode_t gpioErrorCode = gpioInitPin(sensor->powerPin, GPIO_OUTPUT); if (gpioErrorCode != GPIO_NO_ERROR) { - return PAC193X_INIT_ERROR; + Throw(PAC193X_INIT_ERROR); } - gpioSetPin(sensor.powerPin, GPIO_PIN_HIGH); - - return PAC193X_NO_ERROR; + gpioSetPin(sensor->powerPin, GPIO_PIN_HIGH); } -pac193xErrorCode_t pac193xInit(pac193xSensorConfiguration_t sensor) { - if (PAC193X_NO_ERROR != pac193xInternalCheckSensorAvailable(sensor)) { - return PAC193X_INIT_ERROR; - } - - pac193xErrorCode_t errorCode = pac193xInternalSetDefaultConfiguration(sensor); - if (errorCode != PAC193X_NO_ERROR) { - return PAC193X_INIT_ERROR; - } - - return pac193xRefreshData(sensor); +void pac193xInit(pac193xSensorConfiguration_t *sensor) { + pac193xInternalCheckSensorAvailable(sensor); + pac193xInternalSetDefaultConfiguration(sensor); + pac193xRefreshData(sensor); } -pac193xErrorCode_t pac193xSetChannelsInUse(pac193xSensorConfiguration_t sensor) { +void pac193xSetChannelsInUse(const pac193xSensorConfiguration_t *sensor) { /* check if used channels are valid */ - if (sensor.usedChannels.uint_channelsInUse > 0b00010000) { - return PAC193X_INIT_ERROR; + if (sensor->usedChannels.uint_channelsInUse > 0b00010000) { + Throw(PAC193X_INIT_ERROR); } /* send configuration to sensor */ - uint8_t channelsInUse = (sensor.usedChannels.uint_channelsInUse << 4) ^ 0xF0; - pac193xErrorCode_t errorCode = - pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_CHANNEL_DIS, channelsInUse); - if (errorCode != PAC193X_NO_ERROR) { - return PAC193X_INIT_ERROR; - } - - return pac193xRefreshData(sensor); + uint8_t channelsInUse = (sensor->usedChannels.uint_channelsInUse << 4) ^ 0xF0; + pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_CHANNEL_DIS, channelsInUse); + pac193xRefreshData(sensor); } -pac193xErrorCode_t pac193xStartAccumulation(pac193xSensorConfiguration_t sensor) { +void pac193xStartAccumulation(const pac193xSensorConfiguration_t *sensor) { /* samplerate = 1024, disable sleep, disable single-show-mode, * enable alert, enable overflow alert */ - if (PAC193X_NO_ERROR != - pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_CTRL, 0b00001010)) { - return PAC193X_INIT_ERROR; - } + pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_CTRL, 0b00001010); /* refresh sensor to apply changes */ - if (PAC193X_NO_ERROR != pac193xRefreshData(sensor)) { - return PAC193X_INIT_ERROR; - } - - return PAC193X_NO_ERROR; + pac193xRefreshData(sensor); } -pac193xErrorCode_t pac193XStopAccumulation(pac193xSensorConfiguration_t sensor) { - pac193xErrorCode_t errorCode = pac193xInternalSetDefaultConfiguration(sensor); - if (errorCode != PAC193X_NO_ERROR) { - return errorCode; - } - +void pac193XStopAccumulation(const pac193xSensorConfiguration_t *sensor) { + pac193xInternalSetDefaultConfiguration(sensor); /* refresh sensor to apply changes */ - errorCode = pac193xRefreshData(sensor); - if (errorCode != PAC193X_NO_ERROR) { - return errorCode; - } - - return PAC193X_NO_ERROR; + pac193xRefreshData(sensor); } -pac193xErrorCode_t pac193xSetSampleRate(pac193xSensorConfiguration_t sensor, - pac193xSampleRate_t sampleRate) { - pac193xErrorCode_t errorCode; +void pac193xSetSampleRate(pac193xSensorConfiguration_t *sensor, pac193xSampleRate_t sampleRate) { uint8_t ctrlRegister; - errorCode = pac193xInternalGetDataFromSensor(sensor, &ctrlRegister, 1, PAC193X_CMD_CTRL); - if (errorCode != PAC193X_NO_ERROR) { - return PAC193X_RECEIVE_DATA_ERROR; - } - + pac193xInternalGetDataFromSensor(sensor, &ctrlRegister, 1, PAC193X_CMD_CTRL); ctrlRegister = (ctrlRegister & 0x3F) | (sampleRate << 6); - - errorCode = pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_CTRL, ctrlRegister); - if (errorCode != PAC193X_NO_ERROR) { - return PAC193X_INIT_ERROR; - } - - sensor.sampleRate = sampleRate; - - return PAC193X_NO_ERROR; + pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_CTRL, ctrlRegister); + sensor->sampleRate = sampleRate; } -pac193xErrorCode_t pac193xRefreshData(pac193xSensorConfiguration_t sensor) { +void pac193xRefreshData(const pac193xSensorConfiguration_t *sensor) { /* essentially performing a REFRESH_V command*/ PRINT_DEBUG("send pac193xInternalRefreshV signal to sensor"); - pac193xErrorCode_t errorCode = - pac193xInternalSendRequestToSensor(sensor, PAC193X_CMD_REFRESH_V); - if (errorCode != PAC193X_NO_ERROR) { - PRINT_DEBUG("pac193xInternalRefreshV send failed, error was %02X", errorCode); - return errorCode; - } + pac193xInternalSendRequestToSensor(sensor, PAC193X_CMD_REFRESH_V); PRINT_DEBUG("pac193xInternalRefreshV successful"); - return PAC193X_NO_ERROR; } -pac193xErrorCode_t pac193xRefreshDataAndResetAccumulator(pac193xSensorConfiguration_t sensor) { +void pac193xRefreshDataAndResetAccumulator(const pac193xSensorConfiguration_t *sensor) { /* essentially performing a REFRESH command*/ PRINT_DEBUG("send refresh signal to sensor"); - pac193xErrorCode_t errorCode = pac193xInternalSendRequestToSensor(sensor, PAC193X_CMD_REFRESH); - if (errorCode != PAC193X_NO_ERROR) { - PRINT_DEBUG("refresh send failed, error was %02X", errorCode); - return errorCode; - } + pac193xInternalSendRequestToSensor(sensor, PAC193X_CMD_REFRESH); PRINT_DEBUG("refresh successful"); - return PAC193X_NO_ERROR; } /* endregion GENERAL FUNCTIONS */ -pac193xErrorCode_t pac193xGetSensorInfo(pac193xSensorConfiguration_t sensor, - pac193xSensorId_t *info) { +pac193xSensorId_t pac193xGetSensorInfo(const pac193xSensorConfiguration_t *sensor) { + + CEXCEPTION_T e; uint8_t sizeOfResponseBuffer = 1; /* needs to be called once before reading values to get the latest values*/ - pac193xErrorCode_t errorCode = pac193xRefreshData(sensor); - if (errorCode != PAC193X_NO_ERROR) { - return errorCode; - } + pac193xRefreshData(sensor); - errorCode = pac193xInternalGetDataFromSensor(sensor, &info->product_id, sizeOfResponseBuffer, - PAC193X_CMD_READ_PRODUCT_ID); - if (errorCode != PAC193X_NO_ERROR) { - info->product_id = 0; - info->manufacturer_id = 0; - info->revision_id = 0; - return errorCode; - } + pac193xSensorId_t info; - errorCode = pac193xInternalGetDataFromSensor( - sensor, &info->manufacturer_id, sizeOfResponseBuffer, PAC193X_CMD_READ_MANUFACTURER_ID); - if (errorCode != PAC193X_NO_ERROR) { - info->product_id = 0; - info->manufacturer_id = 0; - info->revision_id = 0; - return errorCode; - } + Try { + pac193xInternalGetDataFromSensor(sensor, &info.product_id, sizeOfResponseBuffer, + PAC193X_CMD_READ_PRODUCT_ID); + pac193xInternalGetDataFromSensor(sensor, &info.manufacturer_id, sizeOfResponseBuffer, + PAC193X_CMD_READ_MANUFACTURER_ID); - errorCode = pac193xInternalGetDataFromSensor(sensor, &info->revision_id, sizeOfResponseBuffer, - PAC193X_CMD_READ_REVISION_ID); - if (errorCode != PAC193X_NO_ERROR) { - info->product_id = 0; - info->manufacturer_id = 0; - info->revision_id = 0; - return errorCode; + pac193xInternalGetDataFromSensor(sensor, &info.revision_id, sizeOfResponseBuffer, + PAC193X_CMD_READ_REVISION_ID); } - - return PAC193X_NO_ERROR; + Catch(e) { + info.product_id = 0; + info.manufacturer_id = 0; + info.revision_id = 0; + Throw(e); + } + return info; } /* region READ MEASUREMENTS */ -pac193xErrorCode_t pac193xGetMeasurementForChannel(pac193xSensorConfiguration_t sensor, - pac193xChannel_t channel, - pac193xValueToMeasure_t valueToMeasure, - float *value) { - if (!pac193xInternalCheckChannelIsActive(sensor.usedChannels, channel)) { - return PAC193X_INVALID_CHANNEL; +float pac193xGetMeasurementForChannel(const pac193xSensorConfiguration_t *sensor, + pac193xChannel_t channel, + pac193xValueToMeasure_t valueToMeasure) { + CEXCEPTION_T e; + if (!pac193xInternalCheckChannelIsActive(sensor->usedChannels, channel)) { + Throw(PAC193X_INVALID_CHANNEL); } - pac193xErrorCode_t errorCode = pac193xInternalGetData(sensor, channel, valueToMeasure, value); - if (errorCode != PAC193X_NO_ERROR) { - *value = 0; - return errorCode; + float value; + Try { + pac193xInternalGetData(sensor, channel, valueToMeasure, &value); + } + Catch(e) { + value = 0; + Throw(e); } - return PAC193X_NO_ERROR; + return value; } -pac193xErrorCode_t pac193xGetMeasurementsForChannel(pac193xSensorConfiguration_t sensor, - pac193xChannel_t channel, - pac193xMeasurements_t *measurements) { - if (!pac193xInternalCheckChannelIsActive(sensor.usedChannels, channel)) { - measurements->voltageSource = 0; - measurements->voltageSense = 0; - measurements->currentSense = 0; - measurements->power = 0; - return PAC193X_INVALID_CHANNEL; - } +pac193xMeasurements_t pac193xGetMeasurementsForChannel(const pac193xSensorConfiguration_t *sensor, + pac193xChannel_t channel) { - pac193xErrorCode_t errorCode; + pac193xMeasurements_t measurements; - errorCode = - pac193xInternalGetData(sensor, channel, PAC193X_VSOURCE, &measurements->voltageSource); - if (errorCode != PAC193X_NO_ERROR) { - measurements->voltageSource = 0; - measurements->voltageSense = 0; - measurements->currentSense = 0; - measurements->power = 0; - return errorCode; + CEXCEPTION_T e; + if (!pac193xInternalCheckChannelIsActive(sensor->usedChannels, channel)) { + measurements.voltageSource = 0; + measurements.voltageSense = 0; + measurements.currentSense = 0; + measurements.power = 0; + Throw(PAC193X_INVALID_CHANNEL); } - errorCode = - pac193xInternalGetData(sensor, channel, PAC193X_VSENSE, &measurements->voltageSense); - if (errorCode != PAC193X_NO_ERROR) { - measurements->voltageSource = 0; - measurements->voltageSense = 0; - measurements->currentSense = 0; - measurements->power = 0; - return errorCode; + Try { + pac193xInternalGetData(sensor, channel, PAC193X_VSOURCE, &measurements.voltageSource); + pac193xInternalGetData(sensor, channel, PAC193X_VSENSE, &measurements.voltageSense); + pac193xInternalGetData(sensor, channel, PAC193X_CURRENT, &measurements.currentSense); + pac193xInternalGetData(sensor, channel, PAC193X_POWER, &measurements.power); } - - errorCode = - pac193xInternalGetData(sensor, channel, PAC193X_CURRENT, &measurements->currentSense); - if (errorCode != PAC193X_NO_ERROR) { - measurements->voltageSource = 0; - measurements->voltageSense = 0; - measurements->currentSense = 0; - measurements->power = 0; - return errorCode; + Catch(e) { + measurements.voltageSource = 0; + measurements.voltageSense = 0; + measurements.currentSense = 0; + measurements.power = 0; + Throw(e); } - errorCode = pac193xInternalGetData(sensor, channel, PAC193X_POWER, &measurements->power); - if (errorCode != PAC193X_NO_ERROR) { - measurements->voltageSource = 0; - measurements->voltageSense = 0; - measurements->currentSense = 0; - measurements->power = 0; - return errorCode; - } - - return PAC193X_NO_ERROR; + return measurements; } -pac193xErrorCode_t pac193xGetAveragesForChannel(pac193xSensorConfiguration_t sensor, - pac193xChannel_t channel, - pac193xMeasurements_t *measurements) { - if (!pac193xInternalCheckChannelIsActive(sensor.usedChannels, channel)) { - return PAC193X_INVALID_CHANNEL; - } +pac193xMeasurements_t pac193xGetAveragesForChannel(const pac193xSensorConfiguration_t *sensor, + pac193xChannel_t channel) { + CEXCEPTION_T e; - pac193xErrorCode_t errorCode; + pac193xMeasurements_t measurements; - errorCode = - pac193xInternalGetData(sensor, channel, PAC193X_VSOURCE_AVG, &measurements->voltageSource); - if (errorCode != PAC193X_NO_ERROR) { - measurements->voltageSource = 0; - measurements->voltageSense = 0; - measurements->currentSense = 0; - measurements->power = 0; - return errorCode; + if (!pac193xInternalCheckChannelIsActive(sensor->usedChannels, channel)) { + Throw(PAC193X_INVALID_CHANNEL); } - errorCode = - pac193xInternalGetData(sensor, channel, PAC193X_VSENSE_AVG, &measurements->voltageSense); - if (errorCode != PAC193X_NO_ERROR) { - measurements->voltageSource = 0; - measurements->voltageSense = 0; - measurements->currentSense = 0; - measurements->power = 0; - return errorCode; + Try { + pac193xInternalGetData(sensor, channel, PAC193X_VSOURCE_AVG, &measurements.voltageSource); + pac193xInternalGetData(sensor, channel, PAC193X_VSENSE_AVG, &measurements.voltageSense); + pac193xInternalGetData(sensor, channel, PAC193X_CURRENT_AVG, &measurements.currentSense); } - - errorCode = - pac193xInternalGetData(sensor, channel, PAC193X_CURRENT_AVG, &measurements->currentSense); - if (errorCode != PAC193X_NO_ERROR) { - measurements->voltageSource = 0; - measurements->voltageSense = 0; - measurements->currentSense = 0; - measurements->power = 0; - return errorCode; + Catch(e) { + measurements.voltageSource = 0; + measurements.voltageSense = 0; + measurements.currentSense = 0; + measurements.power = 0; + Throw(e); } /* no average for power => set to zero */ - measurements->power = 0; + measurements.power = 0; - return PAC193X_NO_ERROR; + return measurements; } -pac193xErrorCode_t pac193xReadEnergyForAllChannels(pac193xSensorConfiguration_t sensor, - pac193xEnergyMeasurements_t *measurements) { - pac193xErrorCode_t errorCode; +pac193xEnergyMeasurements_t +pac193xReadEnergyForAllChannels(const pac193xSensorConfiguration_t *sensor) { + pac193xEnergyMeasurements_t measurements; /* read values counter */ uint8_t responseBuffer[28]; - errorCode = pac193xInternalGetDataFromSensor(sensor, responseBuffer, 3, PAC193X_CMD_CTRL); - if (errorCode != PAC193X_NO_ERROR) { - return PAC193X_RECEIVE_DATA_ERROR; - } + pac193xInternalGetDataFromSensor(sensor, responseBuffer, 3, PAC193X_CMD_CTRL); - measurements->overflow = responseBuffer[0] & 0x01; + measurements.overflow = responseBuffer[0] & 0x01; - measurements->numberOfAccumulatedValues = + measurements.numberOfAccumulatedValues = pac193xInternalTransformResponseBufferToUInt64(&responseBuffer[1], 3); - measurements->energyChannel1 = pac193xInternalCalculateEnergy( + measurements.energyChannel1 = pac193xInternalCalculateEnergy( pac193xInternalTransformResponseBufferToUInt64(&responseBuffer[4], 6), - sensor.rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(PAC193X_CHANNEL01)], - sensor.sampleRate); - measurements->energyChannel2 = pac193xInternalCalculateEnergy( + sensor->rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(PAC193X_CHANNEL01)], + sensor->sampleRate); + measurements.energyChannel2 = pac193xInternalCalculateEnergy( pac193xInternalTransformResponseBufferToUInt64(&responseBuffer[10], 6), - sensor.rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(PAC193X_CHANNEL02)], - sensor.sampleRate); - measurements->energyChannel3 = pac193xInternalCalculateEnergy( + sensor->rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(PAC193X_CHANNEL02)], + sensor->sampleRate); + measurements.energyChannel3 = pac193xInternalCalculateEnergy( pac193xInternalTransformResponseBufferToUInt64(&responseBuffer[16], 6), - sensor.rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(PAC193X_CHANNEL03)], - sensor.sampleRate); - measurements->energyChannel4 = pac193xInternalCalculateEnergy( + sensor->rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(PAC193X_CHANNEL03)], + sensor->sampleRate); + measurements.energyChannel4 = pac193xInternalCalculateEnergy( pac193xInternalTransformResponseBufferToUInt64(&responseBuffer[22], 6), - sensor.rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(PAC193X_CHANNEL04)], - sensor.sampleRate); + sensor->rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(PAC193X_CHANNEL04)], + sensor->sampleRate); - return PAC193X_NO_ERROR; + return measurements; } /* endregion READ MEASUREMENTS*/ @@ -376,113 +270,85 @@ pac193xErrorCode_t pac193xReadEnergyForAllChannels(pac193xSensorConfiguration_t /* region STATIC FUNCTION IMPLEMENTATIONS */ -static pac193xErrorCode_t pac193xInternalCheckSensorAvailable(pac193xSensorConfiguration_t sensor) { +static void pac193xInternalCheckSensorAvailable(const pac193xSensorConfiguration_t *sensor) { uint8_t sizeOfCommandBuffer = 1; uint8_t commandBuffer[sizeOfCommandBuffer]; commandBuffer[0] = PAC193X_CMD_READ_MANUFACTURER_ID; - i2cErrorCode_t i2CErrorCode = i2cWriteCommand(sensor.i2c_host, sensor.i2c_slave_address, + i2cErrorCode_t i2CErrorCode = i2cWriteCommand(sensor->i2c_host, sensor->i2c_slave_address, commandBuffer, sizeOfCommandBuffer); if (i2CErrorCode != I2C_NO_ERROR) { - return PAC193X_INIT_ERROR; + Throw(PAC193X_INIT_ERROR); } - return PAC193X_NO_ERROR; } -static pac193xErrorCode_t -pac193xInternalSetDefaultConfiguration(pac193xSensorConfiguration_t sensor) { - pac193xErrorCode_t errorCode = pac193xSetChannelsInUse(sensor); - if (errorCode != PAC193X_NO_ERROR) { - return PAC193X_INIT_ERROR; - } +static void pac193xInternalSetDefaultConfiguration(pac193xSensorConfiguration_t *sensor) { + pac193xSetChannelsInUse(sensor); /* enable single-shot mode, enable slow mode, disable alert/overflow pin */ - errorCode = pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_CTRL, 0b11010000); - if (errorCode != PAC193X_NO_ERROR) { - return PAC193X_INIT_ERROR; - } - sensor.sampleRate = PAC193X_8_SAMPLES_PER_SEC; + pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_CTRL, 0b11010000); + sensor->sampleRate = PAC193X_8_SAMPLES_PER_SEC; /* sets measurement to unipolar mode */ - errorCode = pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_NEG_PWR, 0b00000000); - if (errorCode != PAC193X_NO_ERROR) { - return PAC193X_INIT_ERROR; - } + pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_NEG_PWR, 0b00000000); /* enable limited refresh function and set POR to 0 */ - errorCode = pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_SLOW, 0b00010100); - if (errorCode != PAC193X_NO_ERROR) { - return PAC193X_INIT_ERROR; - } - - return PAC193X_NO_ERROR; + pac193xInternalSendConfigurationToSensor(sensor, PAC193X_CMD_SLOW, 0b00010100); } -static pac193xErrorCode_t -pac193xInternalSendConfigurationToSensor(pac193xSensorConfiguration_t sensor, - pac193xRegisterAddress_t registerToWrite, - pac193xSettings_t settingsToWrite) { +static void pac193xInternalSendConfigurationToSensor(const pac193xSensorConfiguration_t *sensor, + pac193xRegisterAddress_t registerToWrite, + pac193xSettings_t settingsToWrite) { uint8_t sizeOfCommandBuffer = 2; uint8_t commandBuffer[sizeOfCommandBuffer]; commandBuffer[0] = registerToWrite; commandBuffer[1] = settingsToWrite; PRINT_DEBUG("send configuration to sensor"); - i2cErrorCode_t i2cErrorCode = i2cWriteCommand(sensor.i2c_host, sensor.i2c_slave_address, + i2cErrorCode_t i2cErrorCode = i2cWriteCommand(sensor->i2c_host, sensor->i2c_slave_address, commandBuffer, sizeOfCommandBuffer); if (i2cErrorCode != I2C_NO_ERROR) { PRINT_DEBUG("send configuration failed, error was %02X", i2cErrorCode); - return PAC193X_SEND_COMMAND_ERROR; + Throw(PAC193X_SEND_COMMAND_ERROR); } PRINT_DEBUG("configuration send successful"); - - return PAC193X_NO_ERROR; } -static pac193xErrorCode_t -pac193xInternalSendRequestToSensor(pac193xSensorConfiguration_t sensor, - pac193xRegisterAddress_t registerToRead) { +static void pac193xInternalSendRequestToSensor(const pac193xSensorConfiguration_t *sensor, + pac193xRegisterAddress_t registerToRead) { uint8_t sizeOfCommandBuffer = 1; uint8_t commandBuffer[sizeOfCommandBuffer]; commandBuffer[0] = registerToRead; PRINT_DEBUG("request data from sensor"); - i2cErrorCode_t errorCode = i2cWriteCommand(sensor.i2c_host, sensor.i2c_slave_address, + i2cErrorCode_t errorCode = i2cWriteCommand(sensor->i2c_host, sensor->i2c_slave_address, commandBuffer, sizeOfCommandBuffer); if (errorCode != I2C_NO_ERROR) { PRINT_DEBUG("sending request failed, error was %02X", errorCode); - return PAC193X_SEND_COMMAND_ERROR; + Throw(PAC193X_SEND_COMMAND_ERROR); } - - return PAC193X_NO_ERROR; } -static pac193xErrorCode_t pac193xInternalReceiveDataFromSensor(pac193xSensorConfiguration_t sensor, - uint8_t *responseBuffer, - uint8_t sizeOfResponseBuffer) { +static void pac193xInternalReceiveDataFromSensor(const pac193xSensorConfiguration_t *sensor, + uint8_t *responseBuffer, + uint8_t sizeOfResponseBuffer) { PRINT_DEBUG("receiving data from sensor"); - i2cErrorCode_t errorCode = i2cReadData(sensor.i2c_host, sensor.i2c_slave_address, + i2cErrorCode_t errorCode = i2cReadData(sensor->i2c_host, sensor->i2c_slave_address, responseBuffer, sizeOfResponseBuffer); if (errorCode != I2C_NO_ERROR) { PRINT_DEBUG("receiving data failed, error was %02X", errorCode); - return PAC193X_RECEIVE_DATA_ERROR; + Throw(PAC193X_RECEIVE_DATA_ERROR); } PRINT_DEBUG("received data successful"); - - return PAC193X_NO_ERROR; } -static pac193xErrorCode_t -pac193xInternalGetDataFromSensor(pac193xSensorConfiguration_t sensor, uint8_t *responseBuffer, - uint8_t sizeOfResponseBuffer, - pac193xRegisterAddress_t registerToRead) { - pac193xErrorCode_t errorCode = pac193xInternalSendRequestToSensor(sensor, registerToRead); - if (errorCode != PAC193X_NO_ERROR) { - return errorCode; - } +static void pac193xInternalGetDataFromSensor(const pac193xSensorConfiguration_t *sensor, + uint8_t *responseBuffer, uint8_t sizeOfResponseBuffer, + pac193xRegisterAddress_t registerToRead) { + pac193xInternalSendRequestToSensor(sensor, registerToRead); - return pac193xInternalReceiveDataFromSensor(sensor, responseBuffer, sizeOfResponseBuffer); + pac193xInternalReceiveDataFromSensor(sensor, responseBuffer, sizeOfResponseBuffer); } static bool pac193xInternalCheckChannelIsActive(pac193xUsedChannels_t usedChannels, @@ -530,9 +396,8 @@ static uint8_t pac193xInternalTranslateChannelToRSenseArrayIndex(pac193xChannel_ return channelIndex; } -static pac193xErrorCode_t -pac193xInternalSetMeasurementProperties(pac193xMeasurementProperties_t *properties, - pac193xValueToMeasure_t valueToMeasure) { +static void pac193xInternalSetMeasurementProperties(pac193xMeasurementProperties_t *properties, + pac193xValueToMeasure_t valueToMeasure) { PRINT_DEBUG("setting properties for requested measurement"); switch (valueToMeasure) { @@ -581,17 +446,15 @@ pac193xInternalSetMeasurementProperties(pac193xMeasurementProperties_t *properti properties->sizeOfResponseBuffer = 3; default: PRINT_DEBUG("invalid properties"); - return PAC193X_INVALID_MEASUREMENT; + Throw(PAC193X_INVALID_MEASUREMENT); } PRINT_DEBUG("settings applied successful"); - return PAC193X_NO_ERROR; } -static pac193xErrorCode_t pac193xInternalGetData(pac193xSensorConfiguration_t sensor, - pac193xChannel_t channel, - pac193xValueToMeasure_t valueToMeasure, - float *value) { +static void pac193xInternalGetData(const pac193xSensorConfiguration_t *sensor, + pac193xChannel_t channel, pac193xValueToMeasure_t valueToMeasure, + float *value) { /* store configurations for measurements */ pac193xMeasurementProperties_t properties; /* set channel offset for property @@ -599,28 +462,19 @@ static pac193xErrorCode_t pac193xInternalGetData(pac193xSensorConfiguration_t se * -> CMD_READ_VBUS1 = 0x07, CMD_READ_VBUS2 = 0x08, ... */ properties.startReadAddress = channel; - pac193xErrorCode_t errorCode = - pac193xInternalSetMeasurementProperties(&properties, valueToMeasure); - if (errorCode != PAC193X_NO_ERROR) { - return errorCode; - } + pac193xInternalSetMeasurementProperties(&properties, valueToMeasure); /* retrieve data from sensor */ uint8_t responseBuffer[properties.sizeOfResponseBuffer]; - errorCode = pac193xInternalGetDataFromSensor( - sensor, responseBuffer, properties.sizeOfResponseBuffer, properties.startReadAddress); - if (errorCode != PAC193X_NO_ERROR) { - return errorCode; - } + pac193xInternalGetDataFromSensor(sensor, responseBuffer, properties.sizeOfResponseBuffer, + properties.startReadAddress); /* transform raw data */ uint64_t rawValue = pac193xInternalTransformResponseBufferToUInt64( responseBuffer, properties.sizeOfResponseBuffer); *value = (*properties.calculationFunction)( - rawValue, sensor.rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(channel)], - sensor.sampleRate); - - return PAC193X_NO_ERROR; + rawValue, sensor->rSense[pac193xInternalTranslateChannelToRSenseArrayIndex(channel)], + sensor->sampleRate); } static uint64_t pac193xInternalTransformResponseBufferToUInt64(const uint8_t *responseBuffer, diff --git a/src/sensor/pac193x/Pac193xInternal.h b/src/sensor/pac193x/Pac193xInternal.h index 4c7f4c3b..752a415b 100644 --- a/src/sensor/pac193x/Pac193xInternal.h +++ b/src/sensor/pac193x/Pac193xInternal.h @@ -12,10 +12,9 @@ typedef union pac193xInternalDataBuffer { /* region SENSOR COMMUNICATION */ -static pac193xErrorCode_t pac193xInternalCheckSensorAvailable(pac193xSensorConfiguration_t sensor); +static void pac193xInternalCheckSensorAvailable(const pac193xSensorConfiguration_t *sensor); -static pac193xErrorCode_t -pac193xInternalSetDefaultConfiguration(pac193xSensorConfiguration_t sensor); +static void pac193xInternalSetDefaultConfiguration(pac193xSensorConfiguration_t *sensor); /*! * @brief send configuration to the sensor @@ -23,34 +22,28 @@ pac193xInternalSetDefaultConfiguration(pac193xSensorConfiguration_t sensor); * @param registerToWrite[in] address of the register where the settings should * be stored * @param settingsToWrite[in] byte to store as settings - * @return return the error code (0 if everything passed) */ -static pac193xErrorCode_t -pac193xInternalSendConfigurationToSensor(pac193xSensorConfiguration_t sensor, - pac193xRegisterAddress_t registerToWrite, - pac193xSettings_t settingsToWrite); +static void pac193xInternalSendConfigurationToSensor(const pac193xSensorConfiguration_t *sensor, + pac193xRegisterAddress_t registerToWrite, + pac193xSettings_t settingsToWrite); /*! * @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) */ -static pac193xErrorCode_t -pac193xInternalSendRequestToSensor(pac193xSensorConfiguration_t sensor, - pac193xRegisterAddress_t registerToRead); +static void pac193xInternalSendRequestToSensor(const pac193xSensorConfiguration_t *sensor, + pac193xRegisterAddress_t registerToRead); /*! * @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 - * @return return the error code (0 if everything - * passed) */ -static pac193xErrorCode_t pac193xInternalReceiveDataFromSensor(pac193xSensorConfiguration_t sensor, - uint8_t *responseBuffer, - uint8_t sizeOfResponseBuffer); +static void pac193xInternalReceiveDataFromSensor(const pac193xSensorConfiguration_t *sensor, + uint8_t *responseBuffer, + uint8_t sizeOfResponseBuffer); /*! * @brief requests and receives data from the sensor @@ -60,12 +53,10 @@ static pac193xErrorCode_t pac193xInternalReceiveDataFromSensor(pac193xSensorConf * @param responseBuffer[out] * @param sizeOfResponseBuffer[in] * @param registerToRead[in] - * @return */ -static pac193xErrorCode_t pac193xInternalGetDataFromSensor(pac193xSensorConfiguration_t sensor, - uint8_t *responseBuffer, - uint8_t sizeOfResponseBuffer, - pac193xRegisterAddress_t registerToRead); +static void pac193xInternalGetDataFromSensor(const pac193xSensorConfiguration_t *sensor, + uint8_t *responseBuffer, uint8_t sizeOfResponseBuffer, + pac193xRegisterAddress_t registerToRead); /* endregion SENSOR COMMUNICATION */ @@ -88,14 +79,12 @@ static uint8_t pac193xInternalTranslateChannelToRSenseArrayIndex(pac193xChannel_ * @param valueToMeasure[in] defines which value should be measured * @return return the error code (0 if everything passed) */ -static pac193xErrorCode_t -pac193xInternalSetMeasurementProperties(pac193xMeasurementProperties_t *properties, - pac193xValueToMeasure_t valueToMeasure); - -static pac193xErrorCode_t pac193xInternalGetData(pac193xSensorConfiguration_t sensor, - pac193xChannel_t channel, - pac193xValueToMeasure_t valueToMeasure, - float *value); +static void pac193xInternalSetMeasurementProperties(pac193xMeasurementProperties_t *properties, + pac193xValueToMeasure_t valueToMeasure); + +static void pac193xInternalGetData(const pac193xSensorConfiguration_t *sensor, + pac193xChannel_t channel, pac193xValueToMeasure_t valueToMeasure, + float *value); /* endregion SETUP MEASUREMENTS */ diff --git a/src/sensor/pac193x/README.adoc b/src/sensor/pac193x/README.adoc index 7520b0e6..b3c9fc51 100644 --- a/src/sensor/pac193x/README.adoc +++ b/src/sensor/pac193x/README.adoc @@ -35,15 +35,23 @@ pac193xSensorConfiguration_t sensor = { }; // <1> int main(void) { - pac193xErrorCode_t errorCode = pac193xInit(sensor); // <2> - if (errordCode != PAC193X_NO_ERROR) { - return errorCode; // <3> + CEXCEPTION_t exception; + Try { + pac193xInit(sensor); // <2> + } + Catch(exception) { + if (exception != PAC193X_NO_ERROR) { + Throw(exception); // <3> + } } pac193xMeasurements_t measurements; - errorCode = pac193xGetAllMeasurementsForChannel(sensor, PAC193X_CHANNEL01, &measurements); // <4> - if (errordCode != PAC193X_NO_ERROR) { - return errorCode; // <3> + Try{ + pac193xGetAllMeasurementsForChannel(sensor, PAC193X_CHANNEL01, &measurements); // <4> + } + Catch(exception) { + if (exception != PAC193X_NO_ERROR) { + Throw(exception); // <3> } return 0; diff --git a/src/sensor/pac193x/README.md b/src/sensor/pac193x/README.md index b2168237..b595c375 100644 --- a/src/sensor/pac193x/README.md +++ b/src/sensor/pac193x/README.md @@ -23,19 +23,29 @@ pac193xSensorConfiguration_t sensor = { int main(void) { + CEXCEPTION_t exception; + // Initialize Sensor (ALWAYS REQUIRED) - pac193xErrorCode_t errorCode = pac193xInit(sensor); - if (errordCode != PAC193X_NO_ERROR) { - return errorCode; + Try { + pac193xInit(sensor); + } + Catch(exception){ + if (exception != PAC193X_NO_ERROR) { + Throw(exception); + } } // DO STUFF // Example: Read Values from Channel pac193xMeasurements_t measurements; - errorCode = pac193xGetMeasurementsForChannel(sensor, PAC193X_CHANNEL01, &measurements); - if (errordCode != PAC193X_NO_ERROR) { - return errorCode; + + Try { + errorCode = pac193xGetMeasurementsForChannel(sensor, PAC193X_CHANNEL01, &measurements); + } + Catch(exception) { + if (exception != PAC193X_NO_ERROR) { + Throw(exception); } // ... diff --git a/src/sensor/pac193x/include/Pac193x.h b/src/sensor/pac193x/include/Pac193x.h index 44d516da..cdfc7f4a 100644 --- a/src/sensor/pac193x/include/Pac193x.h +++ b/src/sensor/pac193x/include/Pac193x.h @@ -21,9 +21,8 @@ * * * @param sensor[in] configuration for sensor to use - * @return returns the error code (0 if everything passed) */ -pac193xErrorCode_t pac193xPowerUpSensor(pac193xSensorConfiguration_t sensor); +void pac193xPowerUpSensor(const pac193xSensorConfiguration_t *sensor); /*! * @brief power down the sensor by setting PWRDN Pin to LOW @@ -31,9 +30,8 @@ pac193xErrorCode_t pac193xPowerUpSensor(pac193xSensorConfiguration_t sensor); * @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) */ -pac193xErrorCode_t pac193xPowerDownSensor(pac193xSensorConfiguration_t sensor); +void pac193xPowerDownSensor(const pac193xSensorConfiguration_t *sensor); /*! * @brief initializes the power sensor @@ -43,45 +41,37 @@ pac193xErrorCode_t pac193xPowerDownSensor(pac193xSensorConfiguration_t sensor); * @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) */ -pac193xErrorCode_t pac193xInit(pac193xSensorConfiguration_t sensor); +void pac193xInit(pac193xSensorConfiguration_t *sensor); /*! * @brief updates the number of used channels * * @param sensor[in] sensor configuration with updated channels - * @return returns the error code (0 if everything passed) */ -pac193xErrorCode_t pac193xSetChannelsInUse(pac193xSensorConfiguration_t sensor); +void pac193xSetChannelsInUse(const pac193xSensorConfiguration_t *sensor); /*! * @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); +void pac193xStartAccumulation(const 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); +void pac193XStopAccumulation(const pac193xSensorConfiguration_t *sensor); /*! * @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); +void pac193xSetSampleRate(pac193xSensorConfiguration_t *sensor, pac193xSampleRate_t sampleRate); /*! * @brief this function transfers the latest accumulated values to the data register @@ -93,9 +83,8 @@ pac193xErrorCode_t pac193xSetSampleRate(pac193xSensorConfiguration_t sensor, * @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); +void pac193xRefreshDataAndResetAccumulator(const pac193xSensorConfiguration_t *sensor); /*! * @brief this function transfers the latest accumulated values to the data register @@ -107,21 +96,18 @@ pac193xErrorCode_t pac193xRefreshDataAndResetAccumulator(pac193xSensorConfigurat * @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); +void pac193xRefreshData(const pac193xSensorConfiguration_t *sensor); /* endregion GENERAL FUNCTIONS */ /*! * @brief retrieve the production information from the sensor * - * @param sensor[in] configuration of the sensor to use - * @param info[out] struct that holds the information read from the sensor - * @return returns the error code (0 if everything passed) + * @param sensor[in] configuration of the sensor to use + * @returns struct containing information read from the sensor */ -pac193xErrorCode_t pac193xGetSensorInfo(pac193xSensorConfiguration_t sensor, - pac193xSensorId_t *info); +pac193xSensorId_t pac193xGetSensorInfo(const pac193xSensorConfiguration_t *sensor); /* region READ MEASUREMENTS */ @@ -134,25 +120,21 @@ pac193xErrorCode_t pac193xGetSensorInfo(pac193xSensorConfiguration_t 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 (0 if everything passed) + * @returns retrieved value (float) */ -pac193xErrorCode_t pac193xGetMeasurementForChannel(pac193xSensorConfiguration_t sensor, - pac193xChannel_t channel, - pac193xValueToMeasure_t valueToMeasure, - float *value); +float pac193xGetMeasurementForChannel(const pac193xSensorConfiguration_t *sensor, + pac193xChannel_t channel, + pac193xValueToMeasure_t valueToMeasure); /*! * @brief read \b all available single shot values from the sensor for a specific channel * - * @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) + * @param sensor[in] configuration of the sensor to use + * @param channel[in] channel where the measurement should be taken from + * @returns struct containing measured values */ -pac193xErrorCode_t pac193xGetMeasurementsForChannel(pac193xSensorConfiguration_t sensor, - pac193xChannel_t channel, - pac193xMeasurements_t *measurements); +pac193xMeasurements_t pac193xGetMeasurementsForChannel(const pac193xSensorConfiguration_t *sensor, + pac193xChannel_t channel); /*! * @brief read all rolling averages from sensor @@ -162,12 +144,10 @@ pac193xErrorCode_t pac193xGetMeasurementsForChannel(pac193xSensorConfiguration_t * * @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) + * @returns struct containing measured values */ -pac193xErrorCode_t pac193xGetAveragesForChannel(pac193xSensorConfiguration_t sensor, - pac193xChannel_t channel, - pac193xMeasurements_t *measurements); +pac193xMeasurements_t pac193xGetAveragesForChannel(const pac193xSensorConfiguration_t *sensor, + pac193xChannel_t channel); /*! * @brief get the counter of accumulated values and the accumulated power values @@ -176,12 +156,11 @@ pac193xErrorCode_t pac193xGetAveragesForChannel(pac193xSensorConfiguration_t sen * @important * \a pac193xRefreshDataAndResetAccumulator or \a pac193xRefreshData has to be called beforehand! * - * @param sensor[in] configuration of the sensor to use - * @param measurements[out] memory where the struct with the measured values will be stored - * @return returns the error code (0 if everything passed) + * @param sensor[in] configuration of the sensor to use + * @returns struct containing measured values */ -pac193xErrorCode_t pac193xReadEnergyForAllChannels(pac193xSensorConfiguration_t sensor, - pac193xEnergyMeasurements_t *measurements); +pac193xEnergyMeasurements_t +pac193xReadEnergyForAllChannels(const pac193xSensorConfiguration_t *sensor); /* endregion ACCUMULATED MEASUREMENTS */ // end::prototypes[] diff --git a/test/hardware/Sensors/HardwaretestDualPac193x.c b/test/hardware/Sensors/HardwaretestDualPac193x.c index 44667825..48d08b92 100644 --- a/test/hardware/Sensors/HardwaretestDualPac193x.c +++ b/test/hardware/Sensors/HardwaretestDualPac193x.c @@ -6,6 +6,8 @@ #include #include +#include "CException.h" + #include "Common.h" #include "EnV5HwConfiguration.h" #include "I2c.h" @@ -15,12 +17,14 @@ static void measureValue(pac193xSensorConfiguration_t sensor, pac193xChannel_t channel) { float measurement; + CEXCEPTION_T e; + + Try { + measurement = pac193xGetMeasurementForChannel(&sensor, channel, PAC193X_VSOURCE); + } - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(sensor, channel, PAC193X_VSOURCE, &measurement); - if (errorCode != PAC193X_NO_ERROR) { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); - return; + Catch(e) { + PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", e); } PRINT("VSource=%4.6fV", measurement); } @@ -89,10 +93,12 @@ static void getValuesFromSensor2() { static void getSerialNumber() { pac193xSensorId_t sensorID; + CEXCEPTION_T e; PRINT("Requesting serial number of sensor 1."); - pac193xErrorCode_t errorCode = pac193xGetSensorInfo(sensor1, &sensorID); - if (errorCode == PAC193X_NO_ERROR) { + + Try { + sensorID = pac193xGetSensorInfo(&sensor1); PRINT( " Expected: Product ID: 0x%02X to 0x%02X; Manufacture ID: 0x%02X; Revision ID: 0x%02X", 0x58, 0x5B, 0x5D, 0x03); @@ -105,13 +111,16 @@ static void getSerialNumber() { } else { PRINT(" \033[0;31mFAILED\033[0m; IDs do not match!"); } - } else { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); + } + + Catch(e) { + PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", e); } PRINT("Requesting serial number of sensor 2"); - errorCode = pac193xGetSensorInfo(sensor2, &sensorID); - if (errorCode == PAC193X_NO_ERROR) { + + Try { + sensorID = pac193xGetSensorInfo(&sensor2); PRINT( " Expected: Product ID: 0x%02X to 0x%02X; Manufacture ID: 0x%02X; Revision ID: 0x%02X", 0x58, 0x5B, 0x5D, 0x03); @@ -124,8 +133,9 @@ static void getSerialNumber() { } else { PRINT(" \033[0;31mFAILED\033[0m; IDs do not match!"); } - } else { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); + } + Catch(e) { + PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", e); } } @@ -134,6 +144,8 @@ static void enterBootMode() { } int main(void) { + + CEXCEPTION_T e; /* enable print to console */ stdio_init_all(); // wait for user console to connect @@ -155,26 +167,30 @@ int main(void) { /* initialize PAC193X sensors */ PRINT("===== START PAC_1 INIT ====="); - pac193xErrorCode_t errorCode; while (1) { - errorCode = pac193xInit(sensor1); - if (errorCode == PAC193X_NO_ERROR) { + Try { + pac193xInit(&sensor1); PRINT("Initialised PAC193X sensor 1.\n"); break; } - PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", errorCode); + Catch(e) { + PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", e); + } + sleep_ms(500); } PRINT("===== START PAC_2 INIT ====="); - errorCode; while (1) { - errorCode = pac193xInit(sensor2); - if (errorCode == PAC193X_NO_ERROR) { + Try { + pac193xInit(&sensor2); PRINT("Initialised PAC193X sensor 2.\n"); break; } - PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", errorCode); + Catch(e) { + PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", e); + } + sleep_ms(500); } @@ -203,6 +219,4 @@ int main(void) { break; } } - - return 0; } diff --git a/test/hardware/Sensors/HardwaretestPac193x.c b/test/hardware/Sensors/HardwaretestPac193x.c index 6cd22de2..4a321b17 100644 --- a/test/hardware/Sensors/HardwaretestPac193x.c +++ b/test/hardware/Sensors/HardwaretestPac193x.c @@ -6,6 +6,8 @@ #include #include +#include "CException.h" + #include "Common.h" #include "EnV5HwConfiguration.h" #include "I2c.h" @@ -51,12 +53,14 @@ static pac193xSensorConfiguration_t sensor1 = { static void getValuesOfChannelWifi() { pac193xMeasurements_t measurements; + CEXCEPTION_T e; PRINT("Requesting measurements for wifi board."); - pac193xErrorCode_t errorCode = - pac193xGetMeasurementsForChannel(sensor1, PAC193X_CHANNEL_WIFI, &measurements); - if (errorCode != PAC193X_NO_ERROR) { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); + Try { + measurements = pac193xGetMeasurementsForChannel(&sensor1, PAC193X_CHANNEL_WIFI); + } + Catch(e) { + PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", e); return; } @@ -86,12 +90,14 @@ static void getValuesOfChannelWifi() { static void getValuesOfChannelSensors() { pac193xMeasurements_t measurements; + CEXCEPTION_T e; PRINT("Requesting measurements for sensors."); - pac193xErrorCode_t errorCode = - pac193xGetMeasurementsForChannel(sensor1, PAC193X_CHANNEL_SENSORS, &measurements); - if (errorCode != PAC193X_NO_ERROR) { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); + Try { + measurements = pac193xGetMeasurementsForChannel(&sensor1, PAC193X_CHANNEL_SENSORS); + } + Catch(e) { + PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", e); return; } @@ -121,10 +127,11 @@ static void getValuesOfChannelSensors() { static void getSerialNumber() { pac193xSensorId_t sensorID; + CEXCEPTION_T e; PRINT("Requesting serial number."); - pac193xErrorCode_t errorCode = pac193xGetSensorInfo(sensor1, &sensorID); - if (errorCode == PAC193X_NO_ERROR) { + Try { + sensorID = pac193xGetSensorInfo(&sensor1); PRINT( " Expected: Product ID: 0x%02X to 0x%02X; Manufacture ID: 0x%02X; Revision ID: 0x%02X", 0x58, 0x5B, 0x5D, 0x03); @@ -137,8 +144,9 @@ static void getSerialNumber() { } else { PRINT(" \033[0;31mFAILED\033[0m; IDs do not match!"); } - } else { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); + } + Catch(e) { + PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", e); } } @@ -168,14 +176,17 @@ int main(void) { /* initialize PAC193X sensor */ PRINT("===== START PAC193X INIT ====="); - pac193xErrorCode_t errorCode; + CEXCEPTION_T e; while (1) { - errorCode = pac193xInit(sensor1); - if (errorCode == PAC193X_NO_ERROR) { + Try { + pac193xInit(&sensor1); PRINT("Initialised PAC193X.\n"); break; } - PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", errorCode); + Catch(e) { + PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", e); + } + sleep_ms(500); } diff --git a/test/hardware/Sensors/HardwaretestPac193xAccumulator.c b/test/hardware/Sensors/HardwaretestPac193xAccumulator.c index 4dbbfe64..5746551a 100644 --- a/test/hardware/Sensors/HardwaretestPac193xAccumulator.c +++ b/test/hardware/Sensors/HardwaretestPac193xAccumulator.c @@ -5,6 +5,8 @@ #include #include +#include "CException.h" + #include "Common.h" #include "EnV5HwConfiguration.h" #include "EnV5HwController.h" @@ -60,39 +62,44 @@ static void initializeCommunication(void) { /* initialize PAC193X sensor */ PRINT("===== START PAC193X INIT ====="); - pac193xErrorCode_t errorCode; + CEXCEPTION_T e; while (1) { - errorCode = pac193xInit(sensor); - if (errorCode == PAC193X_NO_ERROR) { + Try { + pac193xInit(&sensor); PRINT("Initialised PAC193X.\n"); break; } - PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", errorCode); + Catch(e) { + PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", e); + } + sleep_ms(500); } } _Noreturn static void runTest(void) { PRINT("===== START TEST ====="); - pac193xStartAccumulation(sensor); - pac193xSetSampleRate(sensor, PAC193X_256_SAMPLES_PER_SEC); - pac193xRefreshDataAndResetAccumulator(sensor); + pac193xStartAccumulation(&sensor); + pac193xSetSampleRate(&sensor, PAC193X_256_SAMPLES_PER_SEC); + pac193xRefreshDataAndResetAccumulator(&sensor); sleep_for_ms(1000); while (true) { - pac193xRefreshData(sensor); + pac193xRefreshData(&sensor); sleep_for_ms(10); pac193xEnergyMeasurements_t measurements; - pac193xErrorCode_t error = pac193xReadEnergyForAllChannels(sensor, &measurements); - if (PAC193X_NO_ERROR != error) { - PRINT("Error occurred: 0x%02X", error); - } else { + CEXCEPTION_T e; + Try { + measurements = pac193xReadEnergyForAllChannels(&sensor); PRINT("Overflow: %b", measurements.overflow); PRINT("Got %lu values:\n\t%f\n\t%f\n\t%f\n\t%f", measurements.numberOfAccumulatedValues, measurements.energyChannel1, measurements.energyChannel2, measurements.energyChannel3, measurements.energyChannel4); } + Catch(e) { + PRINT("Error occurred: 0x%02X", e); + } PRINT("Sleeping for 2 seconds"); sleep_for_ms(2000); diff --git a/test/hardware/Sensors/HardwaretestPac193xBuffer.c b/test/hardware/Sensors/HardwaretestPac193xBuffer.c index 638c5382..77ee8710 100644 --- a/test/hardware/Sensors/HardwaretestPac193xBuffer.c +++ b/test/hardware/Sensors/HardwaretestPac193xBuffer.c @@ -6,6 +6,8 @@ #include #include +#include "CException.h" + #include "Common.h" #include "EnV5HwConfiguration.h" #include "I2c.h" @@ -50,9 +52,13 @@ static pac193xSensorConfiguration_t sensor2 = { /* endregion SENSOR DEFINITIONS */ static void sensorTest(pac193xSensorConfiguration_t sensor) { - pac193xErrorCode_t errorCode = pac193xStartAccumulation(sensor); - if (errorCode != PAC193X_NO_ERROR) { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); + CEXCEPTION_T e; + + Try { + pac193xStartAccumulation(&sensor); + } + Catch(e) { + PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", e); return; } @@ -60,14 +66,12 @@ static void sensorTest(pac193xSensorConfiguration_t sensor) { sleep_ms(2000); pac193xEnergyMeasurements_t measurements; - errorCode = pac193xReadEnergyForAllChannels(sensor, &measurements); - if (errorCode != PAC193X_NO_ERROR) { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); - return; + Try { + measurements = pac193xReadEnergyForAllChannels(&sensor); + pac193XStopAccumulation(&sensor); } - errorCode = pac193XStopAccumulation(sensor); - if (errorCode != PAC193X_NO_ERROR) { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); + Catch(e) { + PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", e); return; } @@ -103,14 +107,17 @@ int main(void) { } PRINT("===== START INIT PAC_1 ====="); - pac193xErrorCode_t errorCode; + CEXCEPTION_T e; while (1) { - errorCode = pac193xInit(sensor1); - if (errorCode == PAC193X_NO_ERROR) { + Try { + pac193xInit(&sensor1); PRINT("Initialised PAC193X sensor 1.\n"); break; } - PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", errorCode); + Catch(e) { + PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", e); + } + sleep_ms(500); } diff --git a/test/hardware/TestNetworking/HardwaretestDurability.c b/test/hardware/TestNetworking/HardwaretestDurability.c index 71063c33..7631735c 100644 --- a/test/hardware/TestNetworking/HardwaretestDurability.c +++ b/test/hardware/TestNetworking/HardwaretestDurability.c @@ -7,6 +7,7 @@ */ // src headers +#include "CException.h" #include "Common.h" #include "Esp.h" #include "FreeRtosQueueWrapper.h" @@ -20,6 +21,7 @@ #include // external headers + #include #include @@ -219,12 +221,14 @@ void updateCounter(countUpdateMessage_t *msg, uint16_t *counter) { } } float measureValue(pac193xSensorConfiguration_t sensor, pac193xChannel_t channel) { + CEXCEPTION_T e; float measurement; - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(sensor, channel, PAC193X_VSOURCE_AVG, &measurement); - if (errorCode != PAC193X_NO_ERROR) { - PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", errorCode); + Try { + measurement = pac193xGetMeasurementForChannel(&sensor, channel, PAC193X_VSOURCE_AVG); + } + Catch(e) { + PRINT(" \033[0;31mFAILED\033[0m; pac193x_ERROR: %02X", e); return -1; } return measurement; @@ -272,25 +276,31 @@ _Noreturn void sensorTask(void) { void initSensors(void) { PRINT("===== INIT SENSOR 1 ====="); - pac193xErrorCode_t errorCode; + CEXCEPTION_T e; while (1) { - errorCode = pac193xInit(sensor1); - if (errorCode == PAC193X_NO_ERROR) { + Try { + pac193xInit(&sensor1); PRINT("Initialised PAC193X sensor 1.\n"); break; } - PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", errorCode); + Catch(e) { + PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", e); + } + sleep_ms(500); } PRINT("===== INIT SENSOR 2 ====="); while (1) { - errorCode = pac193xInit(sensor2); - if (errorCode == PAC193X_NO_ERROR) { + Try { + pac193xInit(&sensor2); PRINT("Initialised PAC193X sensor 1.\n"); break; } - PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", errorCode); + Catch(e) { + PRINT("Initialise PAC193X failed; pac193x_ERROR: %02X\n", e); + } + sleep_ms(500); } } diff --git a/test/unit/UnittestPac193x.c b/test/unit/UnittestPac193x.c index 7c2607a6..9e44bd8f 100644 --- a/test/unit/UnittestPac193x.c +++ b/test/unit/UnittestPac193x.c @@ -1,3 +1,4 @@ +#include "CException.h" #include "Common.h" #include "I2cUnitTest.h" #include "Pac193x.h" @@ -38,7 +39,7 @@ void setUp(void) { i2cUnittestWriteCommand = i2cUnittestWriteCommandPassForPac193x; i2cUnittestReadCommand = i2cUnittestReadCommandPassForPac193x; - pac193xSetChannelsInUse(SENSOR); + pac193xSetChannelsInUse(&SENSOR); } void tearDown(void) {} @@ -46,67 +47,98 @@ void tearDown(void) {} /* region pac193x_GetSensorInfo */ void pac193xGetSensorInfoReturnSendCommandErrorIfHardwareFails(void) { - pac193xSensorId_t info; - i2cUnittestWriteCommand = i2cUnittestWriteCommandHardwareDefect; - - pac193xErrorCode_t errorCode = pac193xGetSensorInfo(SENSOR, &info); - TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + i2cUnittestWriteCommand = i2cUnittestWriteCommandHardwareDefect; + pac193xSensorId_t info = pac193xGetSensorInfo(&SENSOR); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, e); + } } void pac193xGetSensorInfoReturnSendCommandErrorIfAckMissing(void) { - pac193xSensorId_t info; - i2cUnittestWriteCommand = i2cUnittestWriteCommandAckMissing; - - pac193xErrorCode_t errorCode = pac193xGetSensorInfo(SENSOR, &info); - TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + i2cUnittestWriteCommand = i2cUnittestWriteCommandAckMissing; + pac193xSensorId_t info = pac193xGetSensorInfo(&SENSOR); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, e); + } } void pac193xGetSensorInfoReturnReceiveDataErrorIfHardwareFails(void) { pac193xSensorId_t info; - i2cUnittestReadCommand = i2cUnittestReadCommandHardwareDefect; - - pac193xErrorCode_t errorCode = pac193xGetSensorInfo(SENSOR, &info); - TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + i2cUnittestReadCommand = i2cUnittestReadCommandHardwareDefect; + pac193xSensorId_t info = pac193xGetSensorInfo(&SENSOR); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, e); + } } void pac193xGetSensorInfoReturnReceiveDataErrorIfAckMissing(void) { pac193xSensorId_t info; - i2cUnittestReadCommand = i2cUnittestReadCommandAckMissing; + CEXCEPTION_T e; + + Try { + i2cUnittestReadCommand = i2cUnittestReadCommandAckMissing; - pac193xErrorCode_t errorCode = pac193xGetSensorInfo(SENSOR, &info); - TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, errorCode); + pac193xSensorId_t info = pac193xGetSensorInfo(&SENSOR); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, e); + } } void pac193xGetSensorInfoReadSuccessful(void) { pac193xSensorId_t info; - - pac193xErrorCode_t errorCode = pac193xGetSensorInfo(SENSOR, &info); - TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + pac193xSensorId_t info = pac193xGetSensorInfo(&SENSOR); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, e); + } } void pac193xGetSensorInfoReadCorrectValue(void) { pac193xSensorId_t expectedInfo, actualInfo; + CEXCEPTION_T e; expectedInfo.product_id = byteZero; expectedInfo.manufacturer_id = byteZero; expectedInfo.revision_id = byteZero; - pac193xGetSensorInfo(SENSOR, &actualInfo); - - TEST_ASSERT_EQUAL_UINT8(expectedInfo.product_id, actualInfo.product_id); - TEST_ASSERT_EQUAL_UINT8(expectedInfo.manufacturer_id, actualInfo.manufacturer_id); - TEST_ASSERT_EQUAL_UINT8(expectedInfo.revision_id, actualInfo.revision_id); + Try { + pac193xSensorId_t info = pac193xGetSensorInfo(&SENSOR); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(expectedInfo.product_id, actualInfo.product_id); + TEST_ASSERT_EQUAL_UINT8(expectedInfo.manufacturer_id, actualInfo.manufacturer_id); + TEST_ASSERT_EQUAL_UINT8(expectedInfo.revision_id, actualInfo.revision_id); + } } void pac193xMemoryNotPassedToGetSensorInfoRemainsUntouched(void) { uint8_t memory[512]; pac193xSensorId_t info; - memset(memory, 0, 512); - - pac193xGetSensorInfo(SENSOR, &info); - - TEST_ASSERT_EACH_EQUAL_UINT8(0, memory, 512); + CEXCEPTION_T e; + + Try { + pac193xSensorId_t info = pac193xGetSensorInfo(&SENSOR); + } + Catch(e) { + TEST_ASSERT_EACH_EQUAL_UINT8(0, memory, 512); + } } /* endregion */ @@ -122,135 +154,166 @@ void testAssertUint64tEquals(void) { } void pac193xGetMeasurementForChannelReturnSendCommandErrorIfHardwareFails(void) { - float result; i2cUnittestWriteCommand = i2cUnittestWriteCommandHardwareDefect; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, e); + } } void pac193xGetMeasurementForChannelReturnSendCommandErrorIfAckMissing(void) { - float result; i2cUnittestWriteCommand = i2cUnittestWriteCommandAckMissing; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, e); + } } void pac193xGetMeasurementForChannelReturnReceiveDataErrorIfHardwareFails(void) { - float result; i2cUnittestReadCommand = i2cUnittestReadCommandHardwareDefect; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, e); + } } void pac193xGetMeasurementForChannelReturnReceiveDataErrorIfAckMissing(void) { - float result; i2cUnittestReadCommand = i2cUnittestReadCommandAckMissing; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, e); + } } void pac193xGetMeasurementForChannelReturnInvalidChannelErrorIfChannelWrong(void) { - float result; i2cUnittestReadCommand = i2cUnittestReadCommandAckMissing; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, 0x10, PAC193X_VSOURCE, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_INVALID_CHANNEL, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, 0x10, PAC193X_VSOURCE); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_INVALID_CHANNEL, e); + } } /* region V_SOURCE */ void pac193xGetMeasurementForChannelReadSuccessfulValueVsource(void) { - float result; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, e); + } } void pac193xGetMeasurementForChannelReadCorrectValueVsource(void) { float expectedValue = 0, actualValue = 0; + CEXCEPTION_T e; uint64_t expected_rawValue = ((uint64_t)byteZero << 8) | (uint64_t)byteZero; expectedValue = (32.0f * (((float)expected_rawValue) / pac193xInternalUnipolarVoltageDenominator)); - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE, &actualValue); - - TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + Try { + actualValue = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_VSOURCE); + } + Catch(e) { + TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + } } /* endregion V_SOURCE */ /* region V_SENSE */ void pac193xGetMeasurementForChannelReadSuccessfulValueVsense(void) { - float result; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_VSENSE, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_VSENSE); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, e); + } } void pac193xGetMeasurementForChannelReadCorrectValueVsense(void) { float expectedValue = 0, actualValue = 0; + CEXCEPTION_T e; uint64_t expected_rawValue = ((uint64_t)byteZero << 8) | (uint64_t)byteZero; expectedValue = 0.1f * ((float)expected_rawValue) / pac193xInternalUnipolarVoltageDenominator; - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_VSENSE, &actualValue); - - TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + Try { + actualValue = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_VSENSE); + } + Catch(e) { + TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + } } /* endregion V_SENSE */ /* region CURRENT */ void pac193xGetMeasurementForChannelReadSuccessfulValueCurrent(void) { - float result; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_CURRENT, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_CURRENT); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, e); + } } void pac193xGetMeasurementForChannelReadCorrectValueCurrent(void) { float expectedValue = 0, actualValue = 0; + CEXCEPTION_T e; uint64_t expected_rawValue = ((uint64_t)byteZero << 8) | (uint64_t)byteZero; float FSC = 0.1f / SENSOR.rSense[usedChannelIndex]; expectedValue = FSC * (((float)expected_rawValue) / pac193xInternalUnipolarVoltageDenominator); - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_CURRENT, &actualValue); - - TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + Try { + actualValue = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_CURRENT); + } + Catch(e) { + TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + } } /* endregion CURRENT */ /* region POWER */ void pac193xGetMeasurementForChannelReadSuccessfulValuePower(void) { - float result; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_ENERGY, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_ENERGY); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, e); + } } void pac193xGetMeasurementForChannelReadCorrectValuePower(void) { float expectedValue = 0, actualValue = 0; + CEXCEPTION_T e; uint64_t rawValue = (((uint64_t)byteZero << 24) | ((uint64_t)byteZero << 16) | ((uint64_t)byteZero << 8) | (uint64_t)byteZero); @@ -259,25 +322,31 @@ void pac193xGetMeasurementForChannelReadCorrectValuePower(void) { float pProp = expectedRawValue / pac193xInternalUnipolarPowerDenominator; expectedValue = powerFSR * pProp; - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_POWER, &actualValue); - - TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + Try { + actualValue = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_POWER); + } + Catch(e) { + TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + } } /* endregion POWER */ /* region ENERGY */ void pac193xGetMeasurementForChannelReadSuccessfulValueEnergy(void) { - float result; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_ENERGY, &result); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + float result = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_ENERGY); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, e); + } } void pac193xGetMeasurementForChannelReadCorrectValueEnergy(void) { float expectedValue = 0, actualValue = 0; + CEXCEPTION_T e; uint64_t expected_rawValue = (((uint64_t)byteZero << 40) | ((uint64_t)byteZero << 32) | (uint64_t)byteZero << 24) | @@ -286,9 +355,12 @@ void pac193xGetMeasurementForChannelReadCorrectValueEnergy(void) { float powerFSR = 3.2f / SENSOR.rSense[usedChannelIndex]; expectedValue = (float)expected_rawValue * powerFSR / (pac193xInternalEnergyDenominator * 8.0f); - pac193xGetMeasurementForChannel(SENSOR, PAC193X_CHANNEL02, PAC193X_ENERGY, &actualValue); - - TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + Try { + actualValue = pac193xGetMeasurementForChannel(&SENSOR, PAC193X_CHANNEL02, PAC193X_ENERGY); + } + Catch(e) { + TEST_ASSERT_EQUAL_FLOAT(expectedValue, actualValue); + } } /* endregion ENERGY */ @@ -296,52 +368,67 @@ void pac193xGetMeasurementForChannelReadCorrectValueEnergy(void) { /* region pac193x_GetAllMeasurementsForChannel */ void pac193xGetAllMeasurementsForChannelReturnSendCommandErrorIfHardwareFails(void) { - pac193xMeasurements_t measurements; i2cUnittestWriteCommand = i2cUnittestWriteCommandHardwareDefect; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementsForChannel(SENSOR, PAC193X_CHANNEL02, &measurements); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + pac193xMeasurements_t measurements = + pac193xGetMeasurementsForChannel(&SENSOR, PAC193X_CHANNEL02); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, e); + } } void pac193xGetAllMeasurementsForChannelReturnSendCommandErrorIfAckMissing(void) { - pac193xMeasurements_t measurements; i2cUnittestWriteCommand = i2cUnittestWriteCommandAckMissing; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementsForChannel(SENSOR, PAC193X_CHANNEL02, &measurements); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + pac193xMeasurements_t measurements = + pac193xGetMeasurementsForChannel(&SENSOR, PAC193X_CHANNEL02); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_SEND_COMMAND_ERROR, e); + } } void pac193xGetAllMeasurementsForChannelReturnReceiveDataErrorIfHardwareFails(void) { - pac193xMeasurements_t measurements; i2cUnittestReadCommand = i2cUnittestReadCommandHardwareDefect; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementsForChannel(SENSOR, PAC193X_CHANNEL02, &measurements); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + pac193xMeasurements_t measurements = + pac193xGetMeasurementsForChannel(&SENSOR, PAC193X_CHANNEL02); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, e); + } } void pac193xGetAllMeasurementsForChannelReturnReceiveDataErrorIfAckMissing(void) { - pac193xMeasurements_t measurements; i2cUnittestReadCommand = i2cUnittestReadCommandAckMissing; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementsForChannel(SENSOR, PAC193X_CHANNEL02, &measurements); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + pac193xMeasurements_t measurements = + pac193xGetMeasurementsForChannel(&SENSOR, PAC193X_CHANNEL02); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_RECEIVE_DATA_ERROR, e); + } } void pac193xGetAllMeasurementsForChannelReadSuccessful(void) { - pac193xMeasurements_t measurements; - - pac193xErrorCode_t errorCode = - pac193xGetMeasurementsForChannel(SENSOR, PAC193X_CHANNEL02, &measurements); - - TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, errorCode); + CEXCEPTION_T e; + + Try { + pac193xMeasurements_t measurements = + pac193xGetMeasurementsForChannel(&SENSOR, PAC193X_CHANNEL02); + } + Catch(e) { + TEST_ASSERT_EQUAL_UINT8(PAC193X_NO_ERROR, e); + } } /* endregion */