Class ScaDepp


  • public class ScaDepp
    extends Sc

    This class encapsulates the I/O interface with the SCA ASIC.

    [For historical reasons this class is called ScaDepp. It was developed using the Digilent Adept DEPP host API but it is compatible with any RegisterIo interface, not only DEPP.]

    Transactions with the SCA are through an HDLC command-response protocol. For efficiency, this class buffers a sequence of SCA commands which are sent as a single IO transaction to muDaq through the host interface.

    A muDaq HDLC engine sends the commands to the SCA, one at a time, waiting for the response before sending the next. The responses are buffered by muDaq and can be read through the host interface in a single transaction. There is a one-to-one correspondence between the 16-byte command buffer slot and 16-byte response buffer slot.

    Sca and ScaRx implement a list and map of the possible commands and responses. These descriptor lists are unmarshalled from XML files.

    A typical code snippet for a transaction might be:

    
      allocateTxRxBuffers(6); // Allocate 6 slots
      infoTx("NC_W_CRB",0x3e); // Command 1 of 6
      infoTx("NC_W_CRC",0x00);
      infoTx("NC_W_CRD",0x38);
      infoTx("NC_R_CRB",0x00);
      infoTx("NC_R_CRC",0x00);
      infoTx("NC_R_CRD",0x00); // Command 6 of 6
      putBytes( 0, true ); // Send the commands to muDaq
      cache(); // Read and cache the responses buffer
      byte [] dataB = getResponseBytes("NC_R_CRB"); // Extract the data
      byte [] dataC = getResponseBytes("NC_R_CRC"); // Extract the data
      byte [] dataD = getResponseBytes("NC_R_CRD"); // Extract the data
    
    

    For simple transactions like the above, getResponseBytes(java.lang.String) is a convenient way to extract the data from a named response. For larger or more complex tansactions it may be more appropriate to decode the hdlcrx response cache directly.

    • Field Summary

      Fields 
      Modifier and Type Field Description
      (package private) boolean debug
      Flag used to enable or disable debug to the GUI text area (or console).
      (package private) byte[] hdlcrx
      The SCA response buffer cache.
      (package private) int responseIndex
      The current slot in the SCA response buffer cache.
      (package private) java.nio.ByteBuffer scaTx
      Buffer that holds a batch of SCA commands.
    • Constructor Summary

      Constructors 
      Constructor Description
      ScaDepp​(ScType sc)
      Constructor called by unmarshaller via ScAdapter.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void allocateTxRxBuffers​(int nFrames)
      Allocates a buffer to hold SCA commands.
      void cache()
      Replaces hdlcrx cache with bytes read from the hardware.
      java.lang.String configure()
      Configures the SCA.
      void connect()
      Sends SCA connect command (UF_SABM).
      int crc​(byte[] buf)
      Computes the CRC for an SCA command.
      boolean enableInterfaces()
      Activates required PDMDB interfaces.
      int getAdc​(int adc)
      Reads ADC value.
      int getAdcGain()
      Gets ADC gain register.
      java.util.BitSet getGpioState()
      Reads the current state of the GPIOs.
      void getI2c​(int i2cBus, int i2cSlaveAddress, int i2cSlaveSpeed, byte[] i2cBuf, int offset)
      Reads a byte array from the hardware through the SCA interface.
      byte[] getResponseBytes​(java.lang.String command)
      Extracts the data from the SCA responses.
      void gpioControl​(java.lang.String[] gpios)
      Controls the GPIOs.
      void infoTx​(java.lang.String command, byte[] info)
      Appends an SCA command to the TX buffer.
      void infoTx​(java.lang.String command, int info)
      Appends an SCA command to the TX buffer.
      void infoTx​(java.lang.String command, java.util.BitSet info)
      Appends an SCA command to the TX buffer.
      GuiJPanel makeExpertPanel()
      Makes an GUI panel for SCA expert operations.
      GuiJPanel makePanel()
      Makes a standard GUI panel
      void putBytes​(int addr, boolean waitflag)
      Puts bytes to the IO interface.
      void putI2c​(int i2cBus, int i2cSlaveAddress, int i2cSlaveSpeed, byte[] i2cBuf, int offset)
      Writes a byte array to the hardware through the SCA interface.
      java.lang.String readId()
      Reads SCA ID.
      void reset()
      Sends SCA reset command (UF_RSET).
      void resetBuffers()
      Resets (clears) an existing SCA command buffer.
      void sendRaw​(java.lang.String raw)
      Sends a raw command buffer.
      void setAdcCurrentSource​(int adcId, int flag)
      Sets or clears the SCA ADC current source enable.
      void setAdcGain​(int gain)
      Sets ADC gain register.
      void setDac​(int dacId, int dacValue)
      Sets the DAC value.
      void setGpio​(int gpioId, Sc.GpioDirection direction)
      Sets the GPIO direction.
      void setGpio​(int gpioId, Sc.GpioState gpioState)
      Sets the GPIO value.
      void setSpiSs​(int ss)
      Manually sets SPI SS.
      void setup()
      Initial setup.
      void sfTx​(HdlcControl ctl)
      Sends supervisory frame.
      void showBuffer()
      Displays the current content of the command buffer to System.err.
      void showText​(java.lang.String txt)
      Displays text in the text area.
      • Methods inherited from class cbsw.lhcb.pdmdb.Sc

        init
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • scaTx

        java.nio.ByteBuffer scaTx

        Buffer that holds a batch of SCA commands.

      • debug

        boolean debug

        Flag used to enable or disable debug to the GUI text area (or console).

      • hdlcrx

        byte[] hdlcrx

        The SCA response buffer cache.

      • responseIndex

        int responseIndex

        The current slot in the SCA response buffer cache.

    • Constructor Detail

      • ScaDepp

        public ScaDepp​(ScType sc)

        Constructor called by unmarshaller via ScAdapter.

        Parameters:
        sc - Unmarshalled data to construct this Sc type.
    • Method Detail

      • makePanel

        public GuiJPanel makePanel()

        Makes a standard GUI panel

        Returns:
        The panel
      • makeExpertPanel

        public GuiJPanel makeExpertPanel()

        Makes an GUI panel for SCA expert operations.

        Returns:
        The panel
      • allocateTxRxBuffers

        public void allocateTxRxBuffers​(int nFrames)

        Allocates a buffer to hold SCA commands.

        Parameters:
        nFrames - Number of command frames.
      • resetBuffers

        public void resetBuffers()

        Resets (clears) an existing SCA command buffer.

      • showText

        public void showText​(java.lang.String txt)

        Displays text in the text area.

        Parameters:
        txt - The text to display.
      • cache

        public void cache()

        Replaces hdlcrx cache with bytes read from the hardware.

        Reads the bytes through the IO interface. The number of bytes read spans the responses in responseList.

      • showBuffer

        public void showBuffer()

        Displays the current content of the command buffer to System.err.

      • putBytes

        public void putBytes​(int addr,
                             boolean waitflag)

        Puts bytes to the IO interface.

        Adds header information to the TX buffer and performs a single IO transaction to write the buffer to the hardware. The transaction is retried up to 10 times in the event of timeout.

        Parameters:
        addr - Offset to data in the hardware.
        waitflag - true=enable an IO wait feature.
      • crc

        public int crc​(byte[] buf)

        Computes the CRC for an SCA command.

        Parameters:
        buf - The command bytes.
        Returns:
        int The computed CRC.
      • sendRaw

        public void sendRaw​(java.lang.String raw)

        Sends a raw command buffer.

        An HDLC info transaction looks like this (each token is one byte):

        SOF,Addr,Control,ID,Ch,Len,Cmd,D0,D1,D2,D3,FCS,EOF

        For SCA, Addr=0, ID=0x00 and 0xFF are reserved.

        Parameters:
        raw - Hex encoded command string.
      • getResponseBytes

        public byte[] getResponseBytes​(java.lang.String command)

        Extracts the data from the SCA responses.

        The user is responsible for calling cache() to update hdlcrx before calling this method.

        Performs a search-forward operation through responseList starting at responseIndex until the next instance of the named command is found. Extracts and returns the data from the corresponding slot in hdlcrx.

        Can be called multiple times for each call to cache(). The search will continue from the position reached by the previous call.

        Calls to cache() or allocateTxRxBuffers(int) reset responseIndex to the first slot.

        Parameters:
        command - SCA command name.
        Returns:
        The extracted data or null if response not found.
      • infoTx

        public void infoTx​(java.lang.String command,
                           java.util.BitSet info)

        Appends an SCA command to the TX buffer.

        Parameters:
        command - Command name.
        info - Command data encoded as a BitSet.
        See Also:
        infoTx(java.lang.String,byte[]), putBytes(int,boolean)
      • infoTx

        public void infoTx​(java.lang.String command,
                           byte[] info)

        Appends an SCA command to the TX buffer.

        An HDLC info transaction looks like this (each token is one byte):

        SOF,Addr,Control,ID,Ch,Len,Cmd,D0,D1,D2,D3,FCS,EOF

        For the SCA, Addr=0. ID=0x00 and 0xFF are reserved.

        Parameters:
        command - Command name.
        info - Command data.
        See Also:
        putBytes(int,boolean)
      • sfTx

        public void sfTx​(HdlcControl ctl)

        Sends supervisory frame.

        Parameters:
        ctl - The HDLC command.
      • connect

        public void connect()

        Sends SCA connect command (UF_SABM).

      • reset

        public void reset()

        Sends SCA reset command (UF_RSET).

      • setup

        public void setup()

        Initial setup.

        Configures some global settings needed by the PDM.

        Sets the output state and direction of the GPIOs. Puts the SPI interface into manual slave select mode and sets the SPI frequency. Sets the I2C mode for the busses used by the PDM. Some parameters are determined from data unmarshalled during the GUI configuration.

      • enableInterfaces

        public boolean enableInterfaces()

        Activates required PDMDB interfaces.

        The following interfaces that are used by the PDM are activated: GPIO, SPI, I2C0, I2C1, I2C2, ADC, DAC and JTAG.

        Returns:
        true if verification of the registers was successful, false otherwise.
      • readId

        public java.lang.String readId()

        Reads SCA ID.

        Returns:
        ID as hex encoded string.
      • setDac

        public void setDac​(int dacId,
                           int dacValue)

        Sets the DAC value.

        Specified by:
        setDac in class Sc
        Parameters:
        dacId - The DAC channel number.
        dacValue - The DAC value [0,255].
      • setGpio

        public void setGpio​(int gpioId,
                            Sc.GpioState gpioState)

        Sets the GPIO value.

        Initial settings are based on data unmarshalled from XML during GUI configuration. Care should be exercised to avoid potential signal contention when changing this setting. For example, the GPIOs used as GBTX resets should always be deasserted (gpioValue=0).

        Specified by:
        setGpio in class Sc
        Parameters:
        gpioId - The GPIO number.
        gpioState - Sc.GpioState.ASSERTED or Sc.GpioState.DEASSERTED.
      • setGpio

        public void setGpio​(int gpioId,
                            Sc.GpioDirection direction)

        Sets the GPIO direction.

        Initial settings are based on data unmarshalled from XML during GUI configuration. Care should be exercised to avoid potential signal contention when changing this setting.

        Specified by:
        setGpio in class Sc
        Parameters:
        gpioId - The GPIO number.
        direction - 0=input, 1=output.
      • setAdcCurrentSource

        public void setAdcCurrentSource​(int adcId,
                                        int flag)

        Sets or clears the SCA ADC current source enable.

        Initial settings are based on data unmarshalled from XML during GUI configuration. Care should be exercised to avoid potential signal contention when changing this setting.

        Specified by:
        setAdcCurrentSource in class Sc
        Parameters:
        adcId - The SCA ADC channel.
        flag - 0=disable, 1=enable.
      • setSpiSs

        public void setSpiSs​(int ss)

        Manually sets SPI SS.

        Parameters:
        ss - Slave select enable bit mask
      • gpioControl

        public void gpioControl​(java.lang.String[] gpios)

        Controls the GPIOs.

        Specified by:
        gpioControl in class Sc
        Parameters:
        gpios - gpio:direction:value|gpio:direction:value...
      • getGpioState

        public java.util.BitSet getGpioState()

        Reads the current state of the GPIOs.

        Returns:
        The asserted GPIOs.
      • getAdc

        public int getAdc​(int adc)

        Reads ADC value.

        Specified by:
        getAdc in class Sc
        Parameters:
        adc - The ADC channel number.
        Returns:
        The ADC value.
      • setAdcGain

        public void setAdcGain​(int gain)

        Sets ADC gain register.

        Specified by:
        setAdcGain in class Sc
        Parameters:
        gain - The ADC gain.
      • getAdcGain

        public int getAdcGain()

        Gets ADC gain register.

        Specified by:
        getAdcGain in class Sc
        Returns:
        The ADC gain.
      • putI2c

        public void putI2c​(int i2cBus,
                           int i2cSlaveAddress,
                           int i2cSlaveSpeed,
                           byte[] i2cBuf,
                           int offset)

        Writes a byte array to the hardware through the SCA interface.

        Each SCA-I2C transaction sends 2 pointer bytes + 14 data bytes.

        For synchronisation, it may be better for transaction to be split into smaller blocks, each taking less than 10ms (DEPP timeout). But for now everything is in one transaction and rely on sleeps in the user code where needed.

        Parameters:
        i2cBus - The SCS I2C bus number.
        i2cSlaveAddress - The I2C slave address.
        i2cSlaveSpeed - The I2C transaction speed.
        i2cBuf - Array of register values.
        offset - Offset to location where to start writing.
      • getI2c

        public void getI2c​(int i2cBus,
                           int i2cSlaveAddress,
                           int i2cSlaveSpeed,
                           byte[] i2cBuf,
                           int offset)

        Reads a byte array from the hardware through the SCA interface.

        Parameters:
        i2cBus - The I2C bus number.
        i2cSlaveAddress - The I2C slave address.
        i2cSlaveSpeed - The I2C transaction speed.
        i2cBuf - Array of register values.
        offset - Offset to location where to start reading.