45inline int16_t
watts(
const int32_t
averagePower,
const uint16_t calFixed,
const uint8_t shift)
53 const uint8_t remaining{
static_cast< uint8_t
>(shift - 8) };
54 uint32_t rounded{ (product + (
static_cast< uint32_t
>(1) << (remaining - 1))) >> remaining };
55 if (rounded > INT16_MAX)
60 const auto result{
static_cast< int16_t
>(rounded) };
61 return negative ?
static_cast< int16_t
>(-result) : result;
76constexpr uint8_t
powerShift(
const uint32_t maxSampleSets)
78 return maxSampleSets > (
static_cast< uint32_t
>(INT32_MAX) >> 17) ? 4 : 0;
95template< u
int8_t SHIFT,
typename Count >
96inline int32_t
averagePower(
const int32_t sum,
const Count sampleSets)
98 const auto n{
static_cast< int32_t
>(sampleSets) };
99 if constexpr (SHIFT == 0)
105 const int32_t quotient{ sum / n };
106 const int32_t remainder{ sum % n };
107 return quotient * (
static_cast< int32_t
>(1) << SHIFT) + remainder * (
static_cast< int32_t
>(1) << SHIFT) / n;
122#define DATALOG_SQRT_NORMALISE \
126 "mov %D[val], %C[val] \n\t" \
127 "mov %C[val], %B[val] \n\t" \
128 "mov %B[val], %A[val] \n\t" \
130 "subi %[shift], -4 \n\t" \
134 "cpi %D[val], 0x40 \n\t" \
144 "inc %[shift] \n\t" \
150#define DATALOG_SQRT_DIGITS \
154 "clr %A[twice] \n\t" \
155 "clr %B[twice] \n\t" \
156 "clr %C[twice] \n\t" \
157 "ldi %[bytes], 4 \n\t" \
159 "ldi %[bits], 4 \n\t" \
169 "lsl %A[twice] \n\t" \
170 "rol %B[twice] \n\t" \
171 "rol %C[twice] \n\t" \
172 "cp %A[twice], %A[rem] \n\t" \
173 "cpc %B[twice], %B[rem] \n\t" \
174 "cpc %C[twice], %C[rem] \n\t" \
177 "sbc %A[rem], %A[twice] \n\t" \
178 "sbc %B[rem], %B[twice] \n\t" \
179 "sbc %C[rem], %C[twice] \n\t" \
180 "ori %A[twice], 2 \n\t" \
184 "mov %D[val], %C[val] \n\t" \
185 "mov %C[val], %B[val] \n\t" \
186 "mov %B[val], %A[val] \n\t" \
187 "dec %[bytes] \n\t" \
190#define DATALOG_SQRT_OPERANDS \
191 [rem] "=&r"(remainder), [twice] "=&d"(twiceRoot), [val] "+d"(value), [bytes] "=&d"(bytes), [bits] "=&d"(bits), [shift] "+d"(shift)
212template<
bool Normalise >
213inline uint16_t
squareRoot(uint32_t value, uint8_t& fractionBits)
219 if (Normalise && !value)
228 if constexpr (Normalise)
230 asm(DATALOG_SQRT_NORMALISE DATALOG_SQRT_DIGITS:DATALOG_SQRT_OPERANDS : :
"cc");
234 asm(DATALOG_SQRT_DIGITS:DATALOG_SQRT_OPERANDS : :
"cc");
237 if constexpr (Normalise)
239 while (value < (1UL << 24))
244 while (value < (1UL << 30))
252 for (uint8_t step = 16; step; --step)
254 remainder = (remainder << 2) | (value >> 30);
257 if (remainder > twiceRoot)
259 remainder -= twiceRoot + 1;
265 fractionBits = shift;
266 auto root{
static_cast< uint16_t
>(twiceRoot >> 1) };
268 if ((remainder > root) && (root < UINT16_MAX))
276#undef DATALOG_SQRT_NORMALISE
277#undef DATALOG_SQRT_DIGITS
278#undef DATALOG_SQRT_OPERANDS
287inline uint16_t
isqrt(
const uint32_t value)
289 uint8_t fractionBits;
300inline uint16_t
sqrtScaled(
const uint32_t meanSquare, uint8_t& fractionBits)
314constexpr uint16_t
vrmsFactor(
const uint8_t datalogPeriodInSeconds)
316 return datalogPeriodInSeconds > 10 ? 400 : 100;
326constexpr uint8_t
voltageShift(
const uint16_t factor,
const float (&cal)[N])
340 for (
const auto value : cal)
358inline uint16_t
vrms(
const uint32_t meanSquare,
const uint16_t calFixed,
const uint8_t shift)
360 uint8_t fractionBits;
361 const uint16_t root{
sqrtScaled(meanSquare, fractionBits) };
368 uint8_t total{
static_cast< uint8_t
>(fractionBits + shift) };
382 product = ((product >> total) + 1) >> 1;
384 return product > UINT16_MAX ? UINT16_MAX :
static_cast< uint16_t
>(product);
407 for (uint8_t phase = 0; phase < N; ++phase)
426 for (uint8_t phase = 0; phase < N; ++phase)
428 fixed.value[phase] =
Energy::toFixed(
static_cast< float >(factor) * cal[phase], fixed.shift);
Integer arithmetic of the energy bucket.
static void multU16x16_to32(uint32_t &result, A a, B b)
Optimized 16×16→32 unsigned multiplication with assembly.
static void multU24x16_to32_hi8(uint32_t &result, A a, B b)
Optimized 24×16→40 unsigned multiplication, returning bits 8..39.
Assembly-optimized multiplication functions for AVR microcontrollers.
constexpr uint16_t vrmsFactor(const uint8_t datalogPeriodInSeconds)
Scaling of the datalog voltage, Vrms_L_x100.
constexpr FixedCalibration< N > voltageCalibration(const uint16_t factor, const float(&cal)[N])
The voltage calibration in fixed point, for vrms(), built at compile time.
constexpr uint8_t voltageShift(const uint16_t factor, const float(&cal)[N])
Largest shift that keeps every (factor x calibration value) within 16 bits.
uint16_t squareRoot(uint32_t value, uint8_t &fractionBits)
Square root, rounded to the nearest integer, of the value or of the value normalised.
int16_t watts(const int32_t averagePower, const uint16_t calFixed, const uint8_t shift)
The average power of a phase, in watts.
uint16_t isqrt(const uint32_t value)
Integer square root, rounded to the nearest integer.
constexpr FixedCalibration< N > powerCalibration(const float(&cal)[N])
The power calibration in fixed point, for watts(), built at compile time.
constexpr bool isValidVoltageCalibration(const uint16_t factor, const float(&cal)[N])
Check that the voltage calibration values can be represented.
constexpr uint8_t powerShift(const uint32_t maxSampleSets)
The shift by which the ISR scales the power down before adding it to the datalog sum.
int32_t averagePower(const int32_t sum, const Count sampleSets)
The average power of a datalog period, back at the x1 scale of V_ADC x I_ADC.
uint16_t vrms(const uint32_t meanSquare, const uint16_t calFixed, const uint8_t shift)
The RMS voltage of a phase.
uint16_t sqrtScaled(const uint32_t meanSquare, uint8_t &fractionBits)
The square root of a mean square, with as many fractional bits as fit.
constexpr uint8_t calibrationShift(const float(&cal)[N])
Largest shift that keeps every calibration value within 16 bits.
constexpr uint16_t toFixed(const float cal, const uint8_t shift)
A calibration value as a 16-bit fixed-point constant.
constexpr uint8_t shiftFor(const float largest)
Largest shift that keeps a value within 16 bits once scaled by 2^shift.
constexpr float largestOf(const float(&cal)[N])
Largest of the calibration values.
Calibration values of all phases in 16-bit fixed point, with their shared shift.