Jump to main content

# 1.1.1 How the Metrology Driver Library Works

This driver has been designed to be used with PIC32CXMTSH or PIC32CXMTC devices, which are parts of the PIC32CXMTx device family.

Depending on which device is used, there are some requirements to be considered:

- PIC32CXMTSH connection
  - This device supports single phase and dual phase (2 voltages and 2 currents)
  - AFE is internally provided
- PIC32CXMTC connection
  - This device supports both single- and poly-phase
  - It requires an external AFE:
    - The PIC32CXMTC-DB board mounts an ATSense301 that can be used out-of-the-box
    - PIC32CXMTC-DB board can also be connected to an MCP391x Evaluation Board through flying wires, to use these AFEs instead of the one mounted on board. More details on how to do this connection can be found in Metering Demo Hardware Configuration.

The Energy Metering AFE collects the data from current sensors and voltage inputs and digitizes voltage and current data. This data is processed by the Metrology library, running on Core 1 of the PIC32CXMTSH/C part. The Metrology driver and metering application run on Core 0.

Figure 1-2.Metrology Block Diagram
Figure 1-3.Metrology Block Diagram Using External MCP Evaluation Board

Initialization Process

After a power up, the metrology driver is responsible of the following points:

- Initialize and configure IPC (Inter-Processor communication) peripheral
- Initialize the interface with the metrology library through shared memory
- If the reset cause of the main processor was not a Watchdog reset:
  - Control reset lines of the coprocessor (second processor)
  - Control clock lines of the coprocessor (second processor)
  - Copy the metrology library application file to the coprocessor execution memory
- If the reset cause is Watchdog, no actions are performed with coprocessor, as the system is designed to allow coprocessor continue running if a problem arises only on main processor

The metrology driver provides an assembly file to include the metrology library application file into the application of the main processor.

      .section .rodata
      .global met_bin_start
      .global met_bin_end

      .align 8
    met_bin_start:
      .incbin "./core1_metlib.bin"
      .align 8
    met_bin_end:

A couple of variables are created in order to set the start and end address where the metrology library file can be found in the main application.

    extern uint8_t met_bin_start;
    extern uint8_t met_bin_end;

    /* Metrology Driver Initialization Data */
    DRV_METROLOGY_INIT drvMetrologyInitData = {

        /* MET bin destination address */
        .regBaseAddress = DRV_METROLOGY_REG_BASE_ADDRESS,

        /* MET Binary start address */
        .binStartAddress = (uint32_t)&met_bin_start,

        /* MET Binary end address */
        .binEndAddress = (uint32_t)&met_bin_end,

    };

Once the metrology library addresses have been set in the main application, the metrology driver is in charge of copying its content in the execution memory of the coprocessor.

After initialization of the metrology driver, it is necessary to call DRV_METROLOGY_Open routine which is responsible of enabling IPC peripheral and waiting until metrology library application has been started. At this point, metrology control registers are configured and STATE_CTRL variable is set to INIT status, so metrology library starts its own initial configuration.

Figure 1-4.Application Initialization Sequence

At this point, the application must call the DRV_METROLOGY_GetStatus routine to wait until the status is equal to DRV_METROLOGY_STATUS_READY. Then the metrology driver is ready to be started by the application. For this, the DRV_METROLOGY_Start routine must be called in order to set the metrology library in Run mode and start the data acquisition through IPC interrupts after each integration period, and also after each mains cycle if Full Cycle interrupt is enabled to acquire per-cycle measurements.

Metrology library running in the second core is in charge to trigger a new integration period interrupt when the metrology data is ready. This interrupt is handled by the metrology driver. After processing the received data, a callback function is used to notify this event to the application. Same applies to per-cycle measurements in case Full Cycle interrupt is enabled.

Rev: E