/* * * This work is supported by Department of Energy * contract DE-AC03-76SF00515. * * Robert Steele * Klystron Department * Stanford Linear Accelerator Center (SLAC) * * * * class to encapsulate all hardware references * * all writes and reads are cached for diagnostics * accesses are assumed to be synchronous. */ #ifndef pllHardware_hxx #define pllHardware_hxx #include "drvPll.h" #include "debugPrint.h" /* macros */ /* only works on one dimensional arrays! */ #define array_length(a) (sizeof(a)/sizeof((a)[0])) #define Max(a,b) ((a)>(b)?(a):(b)) #define Min(a,b) ((a)<(b)?(a):(b)) /* limit the value to min through max inclusive */ #define Limit(v,a,b) Min(Max(v,a),b) #ifdef __cplusplus extern "C" { #endif /* debug */ void pllhw_print(); /* initialization */ void pllhw_initialize(); #ifdef __cplusplus } // debug only extern int pllHardwareFlag; /* the low level interface to the hardware. * * a copy is kept of the last values read/written to hardware * to avoid having to read/write the hardware for the last values written. * */ class PllHardware { private: /* hardware physical memory mapping */ typedef struct { volatile short pllhw_control_register; #define Pllhw_adc_fifo_fill_mask Bit(0) #define Pllhw_adc_counter_reset_mask Bit(1) #define Pllhw_adc_overflow_mask Bit(2) #define Pllhw_dac_counter_reset_mask Bit(3) #define Pllhw_dac_counter_trigger_or_clock_start_mask Bit(4) #define Pllhw_trigger_mask Bit(5) #define Pllhw_feedback_auto_manual_mask Bit(6) #define Pllhw_feedback_track_hold_mask Bit(7) #define Pllhw_dac_fine_source_mask Bit(8) #define Pllhw_adc_fifo_read_counter_mask Bit(9) #define Pllhw_software_timing_diagnostic_pin_mask Bit(10) volatile short adc_fifo_write_clock_divisor; /* the data itself, read only */ volatile short adc_fifo_data_read; // the count of data read before the interrupt volatile short adc_fifo_interrupt_count; volatile short adc_fifo_read_count; volatile short dac_coarse_write; volatile short dac_fine_write; volatile short dac_fifo_depth; volatile short trigger_pulse_stop; volatile short trigger_pulse_start; volatile short serial_number; volatile short multiplier; } PllHardwareImage; public: // control register methods inline void enable_adc_external_triggering() { clearControl( Pllhw_adc_counter_reset_mask ); } inline void disable_adc_external_triggering() { setControl( Pllhw_adc_counter_reset_mask ); } inline void adc_reset() { // in case it woke up high, set it low first enable_adc_external_triggering(); // transition low to high and back disable_adc_external_triggering(); // reset happens on rising edge enable_adc_external_triggering(); } inline bool adc_data_is_full() { locRead.pllhw_control_register = hp->pllhw_control_register; return ( locRead.pllhw_control_register & Pllhw_adc_fifo_fill_mask ) != 0; } inline void adc_fifo_read_counter_reset() { setControl( Pllhw_adc_fifo_read_counter_mask ); clearControl( Pllhw_adc_fifo_read_counter_mask ); } inline bool adc_data_overflow_occurred() { locRead.pllhw_control_register = hp->pllhw_control_register; return ( locRead.pllhw_control_register & Pllhw_adc_overflow_mask ) != 0; } inline void adc_data_overflow_clear() { setControl( Pllhw_adc_overflow_mask ); clearControl( Pllhw_adc_overflow_mask ); } // fifo methodsPllhw_adc_fifo_fill_mask inline void dac_counter_trigger_or_clock_start() { // toggle setControl( Pllhw_dac_counter_trigger_or_clock_start_mask ); clearControl( Pllhw_dac_counter_trigger_or_clock_start_mask ); } // control bits inline void trigger_enable() { setControl( Pllhw_trigger_mask ); } inline void trigger_disable() { clearControl( Pllhw_trigger_mask ); } inline void feedback_auto() { setControl( Pllhw_feedback_auto_manual_mask ); } inline void feedback_manual() { clearControl( Pllhw_feedback_auto_manual_mask ); } inline void feedback_track() { setControl( Pllhw_feedback_track_hold_mask ); } inline void feedback_hold() { clearControl( Pllhw_feedback_track_hold_mask ); } inline void fine_source_tracking() { clearControl( Pllhw_dac_fine_source_mask ); } inline void fine_source_test() { setControl( Pllhw_dac_fine_source_mask ); } inline void set_software_timing_diagnostic_pin() { setControl( Pllhw_software_timing_diagnostic_pin_mask ); } inline void clear_software_timing_diagnostic_pin() { clearControl( Pllhw_software_timing_diagnostic_pin_mask ); } inline void setInterruptCount( int n ) { locWrite.adc_fifo_interrupt_count = n; hp->adc_fifo_interrupt_count = n; // debug only! locRead.adc_fifo_interrupt_count = hp->adc_fifo_interrupt_count; DEBUGPRINT(DP_INFO, pllHardwareFlag, ("adc_fifo_interrupt_count = 0x%02x\n", (unsigned int) locRead.adc_fifo_interrupt_count )); } // general hardware functions static inline const int getDacTestSize() { return 2048; } void coarse_dac( short fixedValue ); // convenience method for single fixed value void fine_dac( short fixedValue ); // convenience method for single fixed value void coarse_dac( const short* fixedValues, int size ); void fine_dac( const short* fixedValues, int size ); void trigger_pulse_start( int clocks ); void trigger_pulse_stop( int clocks ); inline short serial_number() { return locRead.serial_number = hp->serial_number; } // new 2/14/8 void multiplier( int value ); short *dmaReadFifos(); // temp!!!!!!!!!!!!!! const short *readAdcFifo( int size ); const short *getRawData() { return rawData; } // diagnostics void print(); const short *getLastHardwareImage() { return (const short *) &locRead; } int getLastHardwareImageSize() { return sizeof(locRead); } // initialization void setHp( void * ); PllHardware(); void initialize(); // currently the registers are mapped to memory. this tests them quickly. // Fri Oct 5 16:07:59 PDT 2007 void registerTest();// fixme: remove shortly?!!!!!!!!!!!!!!!! // used to prequalify addresses so they don't produce a fault // current implementation likely enforces too strict a set of limitations. static bool addressCheck( unsigned int address ); private: // the pointer to the raw hardware // note that initially this is set to a safe location in memory // so there is no access until the bus has been configured properly // during initialization. volatile PllHardwareImage *hp; // the in memory copy of the last values read/written to hardware PllHardwareImage locRead; PllHardwareImage locWrite; void print( volatile PllHardwareImage *wp, volatile PllHardwareImage *p ); // general control register utilities inline void flushControl() { hp->pllhw_control_register = locWrite.pllhw_control_register; DEBUGPRINT(DP_INFO, pllHardwareFlag, ("control_reg = *0x%08x = 0x%02x\n", (unsigned int) &hp->pllhw_control_register, (unsigned int) locWrite.pllhw_control_register )); } inline void setControl( short mask ) { locWrite.pllhw_control_register |= mask; flushControl(); } inline void clearControl( short mask ) { locWrite.pllhw_control_register &= ~mask; flushControl(); } // digital to analog output control // typically used for hardware diagnostics inline void dac_fifo_depth( int depth ) { locWrite.dac_fifo_depth = depth; hp->dac_fifo_depth = locWrite.dac_fifo_depth; } inline void dac_counter_reset() { // ron: is this correct? // ron: when should this be applied? are coarse/fine locked together? yes setControl( Pllhw_dac_counter_reset_mask ); clearControl( Pllhw_dac_counter_reset_mask ); } // data storage int dataOffset; // the number of samples to skip before processing int dataSize; // the number of samples to process, i.e. the window width int nToRead; // total raw samples to read short* rawData; // raw data from the trigger to the offset // including the fifo startup data }; #endif #endif