firmware_tools/verification/lib/tests/utils/fpm6Utils.h

199 lines
6.8 KiB
C++

/**************************************************************/
/* fpm6Utils.h Date: 2020/04/29 */
/* */
/* Copyright (c) 2020 GIS. All rights reserved. */
/* */
/* Utilities class definition for configuring & running FPM6 */
/* */
/**************************************************************/
#include <stdio.h>
#include <stdlib.h>
#ifndef _FPM6UTILS_H_
#define _FPM6UTILS_H_
// Before this file is included, EL-HAL definitions for the core
// and the FPM6 (and its subcomponents) must be included
class fpm6Utils
{
public:
static const uint32_t REG_WR_LATENCY_NS = 60;
static const uint32_t STARTUP_LATENCY_NS = 2000; //18000;
// Constructor
fpm6Utils() {};
protected:
// ------------------------------------
// Set up FPM for PD emulation
//
void configFpmForEmulPulses(CCoreAuto* pCore,
double encFreqKHz,
double divRatio,
uint32_t numSubPixPhases,
double clkFreqMHz = 100.0,
uint32_t seq[] = NULL,
uint32_t seqLen = 0,
uint32_t pgSel = 0)
{
configPdEmulator (pCore, divRatio, encFreqKHz, clkFreqMHz);
fpmConfig (pCore, divRatio, numSubPixPhases, seq, seqLen, pgSel);
};
// ------------------------------------
// Configure the FPM's pulse generator
//
int fpmConfig(CCoreAuto* pCore, double divRatio, uint32_t numSubPixPhases, uint32_t seq[], uint32_t seqLen, uint32_t pgSel = 0)
{
CPulseGenerator3Auto* pPulseGen;
switch(pgSel)
{
case 0 : pPulseGen = pCore->pPulseGenerator30; break;
case 1 : pPulseGen = pCore->pPulseGenerator31; break;
case 2 : pPulseGen = pCore->pPulseGenerator32; break;
case 3 : pPulseGen = pCore->pPulseGenerator33; break;
}
// Program the sequence if a buffer pointer is provided and the length isn't zero.
if (seq != NULL && seqLen != 0)
{
// Load the sequencer with default program
uint32_t seqBaseAddr = (uint32_t)pPulseGen->GetCsrBaseAddr() + CSR_PULSE_GENERATOR_3_CHANNEL_CONTROLLER_SEQ;
for(int idx = 0; idx < seqLen; idx++)
{
csrWriteMem(seqBaseAddr + idx*4, seq[idx]);
}
}
// Disable/Re-enable first the whole pulse_generator
pPulseGen->pPulseGenControl->SetPulseGenControl(0);
pPulseGen->pPulseGenControl->SetEnable(1);
pPulseGen->pPulseGenControl->SetEnableOutput(1);
// Disable the Channel Controller sequencer
pPulseGen->pChannelControllerControl->SetChannelControllerControl(0);
// Set the number of sub-pixel phases
pPulseGen->pSubpixelPhaseNum->SetSubpixelPhaseNum(numSubPixPhases);
// Set the integer part of the div-ratio
uint64_t ratio_int = ((divRatio/(double)numSubPixPhases) * (double)(1UL << 24));
pPulseGen->pDividerRatioInt->SetDividerRatioInt((uint32_t)(ratio_int >> 24));
// Set the fractional part of the div-ratio
pPulseGen->pDividerRatioFraction->SetDividerRatioFraction((uint32_t)(ratio_int & ((1UL << 24)-1)));
// ----
// Disable/Enable the fractional divider
pPulseGen->pFractionalDividerControl->SetFractionalDividerControl(0);
pPulseGen->pFractionalDividerControl->SetFractionalDividerControl(CSR_PULSE_GENERATOR_3_FRACTIONAL_DIVIDER_CONTROL_ENABLE_MASK |
CSR_PULSE_GENERATOR_3_FRACTIONAL_DIVIDER_CONTROL_UDP_MAX_POS_MASK |
CSR_PULSE_GENERATOR_3_FRACTIONAL_DIVIDER_CONTROL_UDP_MIN_POS_MASK);
// enable the ch-ctrl sequencer
pPulseGen->pGenericSequencer15Channel->pCtrl->SetSeqEnable(1);
// Issue a start_req to the ch-ctrl sequencer
pPulseGen->pChannelControllerControl->SetChannelControllerControl(1);
};
// ------------------------------------
// Issue a product detect
//
inline void issuePd (CEncPdEmul1Auto* pEmul, bool setEnables = false)
{
if (setEnables)
{
// Enable everything
pEmul->pOutEnable->SetOutEnable(0xff);
}
pEmul->pPdCtrl->SetPdCtrl(CSR_ENC_PD_EMUL_1_PD_CTRL_PD_EN_MASK | CSR_ENC_PD_EMUL_1_PD_CTRL_PD_GEN_MASK);
};
// ------------------------------------
// Configure the product detect emulator
//
void configPdEmulator(CCoreAuto* pCore, double divRatio, double encFreqKHz, double clkFreqMHz = 100.0)
{
pCore->pEncPdEmul1->pEncCtrl->SetSetAbsPos(1);
// Select source of pulses
pCore->pFpmCtrl6->pSignalselection->SetFirePulseSel(1);
pCore->pFpmCtrl6->pSignalselection->SetEncPdEmulEn(1);
pCore->pFpmCtrl6->pSignalselection->SetSebEpdEn(0);
pCore->pFpmCtrl6->pSignalselection->SetSebEpdLinkoutSel(0);
// Run the encoder
runEnc(pCore->pEncPdEmul1, (encFreqKHz*divRatio)/1000.0, 1, 1, clkFreqMHz);
};
// ------------------------------------
// Generate a set of product detects
// spaced at a given period
//
void generatePDs(CCoreAuto* pCore, uint32_t numPDs, uint32_t periodNs)
{
for (int idx = 0; idx < numPDs; idx++)
{
issuePd(pCore->pEncPdEmul1);
if (idx == 0)
{
nsleepSim(periodNs - REG_WR_LATENCY_NS + STARTUP_LATENCY_NS);
}
else
{
nsleepSim(periodNs - REG_WR_LATENCY_NS);
}
}
};
private:
// ------------------------------------
// convert a frequency into an NCO inc
//
inline uint32_t freq2NCO(double freqMHz, double clkFreqMHz)
{
// The NCO is 32 bits wide
return (uint32_t)(((freqMHz/4)/clkFreqMHz)*((double)(1 << 16)*(double)(1 << 16)));
}
// ------------------------------------
// Configure and run the PD emulator
//
void runEnc(CEncPdEmul1Auto* pEmul, double freqMHz, int enable, int init_phase_en, double clkFreqMHz)
{
// Make sure all the outputs are enabled
pEmul->pOutEnable->SetOutEnable(0xff);
// Set the NCO frequency
pEmul->pEncNcoInc->SetEncNcoInc(freq2NCO(freqMHz, clkFreqMHz));
// Re-initialise the NCO phase (Phase-1 and Phase-2 both to zero)
if(init_phase_en)
{
pEmul->pEncCtrl->SetNcoInit(1);
}
if(enable)
{
pEmul->pEncCtrl->SetNcoEn(1);
}
};
};
#endif