3-phase PV router
Loading...
Searching...
No Matches
Time-critical functions

Functions used by the ISR. More...

Files

file  load_priorities.h
 The load priority state machine: which load to switch at each mains cycle.
 

Classes

struct  adc_ctx_t
 Interrupt Service Routine - Interrupt-Driven Analog Conversion. More...
 

Functions

void confirmPolarity (const uint8_t phase)
 Confirms the polarity of the current voltage sample for a specific phase.
 
template<typename V, typename F>
static void mult16x8_q8 (int16_t &result, V value, F fraction)
 Optimized 16×8→16 signed multiplication with Q8 fractional.
 
template<typename A, typename B>
static void multS16x16_to32 (int32_t &result, A a, B b)
 Optimized 16×16→32 signed multiplication with assembly.
 
template<typename A, typename B>
static void multU16x16_to32 (uint32_t &result, A a, B b)
 Optimized 16×16→32 unsigned multiplication with assembly.
 
template<typename A, typename B>
static void multU24x16_to32_hi8 (uint32_t &result, A a, B b)
 Optimized 24×16→40 unsigned multiplication, returning bits 8..39.
 
void processCurrentRawSample (const uint8_t phase, const uint16_t rawSample)
 Processes the current raw sample for the specified phase.
 
void processDataLogging ()
 Process data logging at the end of each logging period.
 
void processLatestContribution (const uint8_t phase)
 Process the latest contribution after each phase-specific new cycle.
 
void processMinusHalfCycle (const uint8_t phase)
 Processes the start of a new negative half cycle for the specified phase.
 
void processPlusHalfCycle (const uint8_t phase)
 Process the start of a new positive half cycle for the specified phase.
 
void processPolarity (const uint8_t phase, const uint16_t rawSample)
 Processes the polarity of the current voltage sample for a specific phase.
 
void processRawSamples (const uint8_t phase)
 Processes raw voltage and current samples for the specified phase.
 
void processStartNewCycle ()
 Processes the start of a new mains cycle on phase 0.
 
void processStartUp (const uint8_t phase)
 Processes the startup period for the router.
 
void processVoltage (const uint8_t phase)
 Processes the current voltage sample for the specified phase.
 
void processVoltageRawSample (const uint8_t phase, const uint16_t rawSample)
 Processes the current voltage raw sample for the specified phase.
 
void updatePhysicalLoadStates ()
 Updates the physical load states based on logical load priorities and states.
 
void updatePortsStates ()
 Updates the control ports for each of the physical loads.
 

Detailed Description

Functions used by the ISR.

This group contains functions that are executed within the Interrupt Service Routine (ISR) or are closely tied to time-sensitive operations. These functions are optimized for speed and efficiency to ensure the system operates without delays or interruptions.

Note
These functions handle tasks such as ADC sampling, zero-crossing detection, and real-time data processing.

Function Documentation

◆ confirmPolarity()

void confirmPolarity ( const uint8_t phase)
inline

Confirms the polarity of the current voltage sample for a specific phase.

This routine prevents a zero-crossing point from being declared until a certain number of consecutive samples in the 'other' half of the waveform have been encountered.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
  • If the polarity of the most recent sample matches the last confirmed polarity, the persistence counter is reset.
  • If the polarity differs, the persistence counter is incremented.
  • Once the persistence counter exceeds a predefined threshold, the polarity is confirmed.

Definition at line 596 of file processing.cpp.

Here is the caller graph for this function:

◆ mult16x8_q8()

template<typename V, typename F>
static void mult16x8_q8 ( int16_t & result,
V value,
F fraction )
inlinestatic

Optimized 16×8→16 signed multiplication with Q8 fractional.

This function performs a signed 16-bit × unsigned 8-bit Q8 fractional multiplication with rounding, returning a 16-bit result. The 8-bit value is treated as a fraction (Q8 format: 8 fractional bits).

Parameters
resultReference to int16_t variable to store the result
value16-bit signed value
fraction8-bit unsigned fraction (Q8 format: 0.0 to 0.996)
Note
On AVR: ~9 cycles with built-in rounding
Q8 format: fraction = 256 * actual_fraction
Example: fraction=128 represents 0.5, fraction=64 represents 0.25
Based on avrfreertos and OpenMusicLabs techniques
Function provides type checking and debugging support

Definition at line 206 of file mult_asm.h.

Here is the caller graph for this function:

◆ multS16x16_to32()

template<typename A, typename B>
static void multS16x16_to32 ( int32_t & result,
A a,
B b )
inlinestatic

Optimized 16×16→32 signed multiplication with assembly.

This function performs a signed 16-bit × 16-bit multiplication returning a 32-bit result using hand-optimized AVR assembly. It's significantly faster than GCC's library multiplication functions.

Parameters
resultReference to int32_t variable to store the result
aFirst 16-bit signed value
bSecond 16-bit signed value
Note
On AVR: ~15-20 cycles vs ~50+ cycles for library calls
Fallback available for non-AVR platforms
Based on avrfreertos and OpenMusicLabs techniques
Function provides type checking and debugging support

Definition at line 47 of file mult_asm.h.

Here is the caller graph for this function:

◆ multU16x16_to32()

template<typename A, typename B>
static void multU16x16_to32 ( uint32_t & result,
A a,
B b )
inlinestatic

Optimized 16×16→32 unsigned multiplication with assembly.

This function performs an unsigned 16-bit × 16-bit multiplication returning a 32-bit result using hand-optimized AVR assembly. It's significantly faster than GCC's library multiplication functions.

Parameters
resultReference to uint32_t variable to store the result
aFirst 16-bit unsigned value
bSecond 16-bit unsigned value
Note
On AVR: ~15-20 cycles vs ~50+ cycles for library calls
Fallback available for non-AVR platforms
Based on avrfreertos and OpenMusicLabs techniques
Function provides type checking and debugging support

Definition at line 97 of file mult_asm.h.

Here is the caller graph for this function:

◆ multU24x16_to32_hi8()

template<typename A, typename B>
static void multU24x16_to32_hi8 ( uint32_t & result,
A a,
B b )
inlinestatic

Optimized 24×16→40 unsigned multiplication, returning bits 8..39.

Computes (a × b) >> 8 without a 64-bit multiply (which would call __muldi3). Used by the integer energy bucket, where the product of an average power (up to 2^18) and a 16-bit calibration constant needs ~35 bits.

Parameters
resultReference to uint32_t variable to store (a × b) >> 8
aUnsigned value, only its low 24 bits are used (must be < 2^24)
b16-bit unsigned value
Note
On AVR: 6 hardware multiplies, ~30 cycles
The low byte of the product is dropped, not rounded
Fallback available for non-AVR platforms

Definition at line 144 of file mult_asm.h.

Here is the caller graph for this function:

◆ processCurrentRawSample()

void processCurrentRawSample ( const uint8_t phase,
const uint16_t rawSample )
inline

Processes the current raw sample for the specified phase.

This function processes the raw current sample for a specific phase by applying filtering to remove DC offset, compensating for the high-pass filter effect of current transformers (CTs), and calculating the instantaneous power.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
rawSampleThe current raw sample for the specified phase.
  • The DC offset is removed from the raw sample.
  • Additional filtering is applied to offset the high-pass filter effect of CTs.
  • The instantaneous power is calculated and accumulated for the phase.

Definition at line 538 of file processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ processDataLogging()

void processDataLogging ( )
inline

Process data logging at the end of each logging period.

This function handles the data logging process by copying relevant variables for use by the main code and resetting them for the next logging period.

  • Copies cumulative power and voltage squared values for each phase.
  • Copies load ON counts and other diagnostic variables.
  • Resets variables for the next data logging period.
  • Signals the main processor that logging data is available after the startup period.

Definition at line 865 of file processing.cpp.

Here is the caller graph for this function:

◆ processLatestContribution()

void processLatestContribution ( const uint8_t phase)
inline

Process the latest contribution after each phase-specific new cycle.

This function calculates and updates the energy contribution for the specified phase after each new cycle. It ensures that the energy bucket is updated with the latest power measurements and applies necessary adjustments.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
  • Adds the latest energy contribution to the main energy bucket.
  • Applies adjustments for required export energy on phase 0.

Definition at line 806 of file processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ processMinusHalfCycle()

void processMinusHalfCycle ( const uint8_t phase)
inline

Processes the start of a new negative half cycle for the specified phase.

This function is called just after the zero-crossing point of a negative half cycle. It refreshes the DC offset used to remove the DC component from the voltage signal.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
  • l_filterDC_V is a Q17.15 integrator: processVoltage() adds every i_sampleVminusDC to it, so each sample feeds back with a gain of 1/32768.
  • Here the integrator is simply rescaled to the x64 (left-aligned ADC) domain to give the offset applied to the following cycle.
  • The integrator is free-running: it is never reset, and is not clamped. Its time constant is long enough that a plausible offset drift is tracked smoothly, while a stuck or disconnected VT settles to that input instead of to a fixed limit. See test/native/test_dc_offset_filter for the tracking and step-response cases.

Definition at line 782 of file processing.cpp.

Here is the caller graph for this function:

◆ processPlusHalfCycle()

void processPlusHalfCycle ( const uint8_t phase)
inline

Process the start of a new positive half cycle for the specified phase.

This function is called just after the zero-crossing point of a positive half cycle. It processes the latest energy contribution, updates performance metrics, and handles data logging for phase 0.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
  • Processes the latest energy contribution for the specified phase.
  • Updates the minimum number of ADC sample sets per mains cycle for phase 0.
  • Handles data logging at the end of the logging period for phase 0.
  • Counts the seconds without diversion, for phase 0.
  • Resets cumulative power and sample count for the phase.

Definition at line 922 of file processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ processPolarity()

void processPolarity ( const uint8_t phase,
const uint16_t rawSample )
inline

Processes the polarity of the current voltage sample for a specific phase.

This function removes the DC offset from the raw voltage sample and determines the polarity (positive or negative) of the sample. The polarity is stored for use in zero-crossing detection and other processing tasks.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
rawSampleThe current raw voltage sample for the specified phase.
  • The DC offset is subtracted from the raw sample using a low-pass filter (LPF).
  • The polarity is determined based on whether the filtered sample is greater than or less than zero.

Definition at line 511 of file processing.cpp.

Here is the caller graph for this function:

◆ processRawSamples()

void processRawSamples ( const uint8_t phase)
inline

Processes raw voltage and current samples for the specified phase.

This routine is called by the ISR when a pair of voltage and current samples becomes available. It handles the processing of raw samples, including polarity detection, zero-crossing handling, and half-cycle processing.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
  • Determines the polarity of the current sample and handles transitions between positive and negative half cycles.
  • Processes the start of new positive and negative half cycles.
  • For phase 0, it triggers the start of a new mains cycle and handles startup logic.

Definition at line 975 of file processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ processStartNewCycle()

void processStartNewCycle ( )
inline

Processes the start of a new mains cycle on phase 0.

This function is executed once per 20ms (for 50Hz), shortly after the start of each new mains cycle on phase 0. It manages the energy level and load states, ensuring proper operation of the system.

  • Handles recent transitions and updates the post-transition counter.
  • Adjusts energy thresholds and determines whether to add or remove loads.
  • Updates the physical load states and control ports.
  • Ensures the energy bucket level remains within defined limits.
  • Signals the new mains cycle to the main code (Shared::b_newMainsCycle), last.

Definition at line 719 of file processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ processStartUp()

void processStartUp ( const uint8_t phase)
inline

Processes the startup period for the router.

This function handles the initial startup period, allowing the DC-blocking filters to settle before normal operation begins. It ensures that the system is stable before processing energy and load states.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
  • At each crossing of the phase during the startup period, the phase starts its sums and its sample count afresh.
  • Once the filters have settled, the startup period is over: the flags are set for normal operation.

Definition at line 677 of file processing.cpp.

Here is the caller graph for this function:

◆ processVoltage()

void processVoltage ( const uint8_t phase)
inline

Processes the current voltage sample for the specified phase.

This function processes the voltage sample for a specific phase by calculating the cumulative voltage squared (V²) for RMS calculations, updating the low-pass filter for DC offset removal, and preparing for zero-crossing detection.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
  • The voltage squared (V²) is calculated and accumulated for RMS calculations.
  • The low-pass filter is updated to remove the DC offset from the voltage signal.
  • The polarity of the last confirmed sample is stored for zero-crossing detection.
  • The number of samples during the current mains cycle is incremented.

Definition at line 630 of file processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ processVoltageRawSample()

void processVoltageRawSample ( const uint8_t phase,
const uint16_t rawSample )
inline

Processes the current voltage raw sample for the specified phase.

This function processes the raw voltage sample for a specific phase by handling polarity detection, zero-crossing confirmation, and voltage processing. It ensures that the voltage sample is properly filtered and analyzed for further processing.

Parameters
phaseThe phase number [0..NO_OF_PHASES[.
rawSampleThe current raw voltage sample for the specified phase.
  • Determines and confirms the polarity of the raw voltage sample.
  • Handles zero-crossing detection and processes raw samples for the phase.
  • Updates voltage-related metrics and increments the sample set count for phase 0.

Definition at line 1032 of file processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updatePhysicalLoadStates()

void updatePhysicalLoadStates ( )

Updates the physical load states based on logical load priorities and states.

This function maps the logical load states to the physical load states. It ensures that the physical loads are updated according to the logical priorities and states. The function also handles priority rotation if enabled.

The array, logicalLoadState[], contains the on/off state of all logical loads, with element 0 being for the one with the highest priority. The array, physicalLoadState[], contains the on/off state of all physical loads.

The lowest 7 bits of element is the load number as defined in 'physicalLoadState'. The highest bit of each 'loadPrioritiesAndState' determines if the load is ON or OFF. The order of each element in 'loadPrioritiesAndState' determines the load priority. 'loadPrioritiesAndState[i] & loadStateMask' will extract the load number at position 'i' 'loadPrioritiesAndState[i] & loadStateOnBit' will extract the load state at position 'i'

Any other mapping relationships could be configured here.

Note
This function is called during each cycle to ensure the physical load states are synchronized with the logical load states.

Definition at line 455 of file processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ updatePortsStates()

void updatePortsStates ( )
inline

Updates the control ports for each of the physical loads.

This function determines the ON/OFF state of each physical load and updates the corresponding control ports.

  • If a load is OFF, its corresponding pin is added to the pinsOFF mask.
  • If a load is ON, its corresponding pin is added to the pinsON mask.
  • Finally, the pins are updated with setPinsState, one write per port, and only on the ports the load pins are on.
  • Overrides are already part of the physical load states, and the relays are driven by the relay engine only: no other pin is ever written here.

Definition at line 392 of file processing.cpp.

Here is the call graph for this function:
Here is the caller graph for this function: