 1Content-type: application/x-Unknown; charset=UTF8package nmc.sidekick;
import com.ajile.jem.rawJEM;
import com.ajile.drivers.spi.*;


public class Adc {
   private static Adc TheAdc = new Adc();
    /**
     * Starts the application.
     * @param args an array of command-line arguments
     */
    public static void main(java.lang.String[] args) {

        Adc adc = new Adc();
        
        adc.initSpiForAdcTest(); // duplicates other code in full sidekick

        adc.initAdcCsN();        // but this needs to be done in real code

        int r = 0;
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                DebugMessage.println("got InterruptedException: " + e);
            }
            r = adc.getReading(2);
            DebugMessage.println("adc Reading was " + r + " = 0x"
                               + Integer.toHexString(r));
        }
        
    }

    static boolean isInitialized=false;
    static final int ADCCSN = (1<<4);

    public static Adc getInstance() {
        if(TheAdc !=null)
          return TheAdc;
        else{
          DebugMessage.println("Adc.getInstance() no instance -- returning null");
          return null;
        }
    }

    private void initSpiForAdcTest() {
        spi.setClkDivider(spi.SPI_CLOCK_DIVIDER_64);
        spi.setSlaveSelect(spi.SPI_SLAVE_SELECT_NONE);
    }

    // The following is NOT THREAD-SAFE
    // If anything else is doing a read-modify-write of regE at the same time,
    // Heisenbugs are likely to appear
    //
    public void initAdcCsN() {
        if(isInitialized==false){
        	DebugMessage.println("Initializing initAdcCsN ");

        // set the value of AdcCsN to 1 (chip select not asserted)
        // int regEOut = rawJEM.getInt(AjileAddr.GPIOE_OR);
        // regEOut |= ADCCSN;
        // rawJEM.set(AjileAddr.GPIOE_OR, regEOut);

        // This SHOULD be THREAD-SAFE
        DebugMessage.println("Adc.initAdcCsN() (REG_E_OR 4.1)");
        rawJEM.setField(1,AjileAddr.GPIOE_OR,4,1);

        // now enable the output driver on the pin connected to AdcCsN (E4)
        // int regEEn  = rawJEM.getInt(AjileAddr.GPIOE_OER);
        // regEEn |= ADCCSN;
        // rawJEM.set(AjileAddr.GPIOE_OER, regEEn);

        // This SHOULD be THREAD-SAFE
        DebugMessage.println("Adc.initAdcCsN() (REG_E_OER 4.1)");//was OR
        rawJEM.setField(1,AjileAddr.GPIOE_OER,4,1);
        isInitialized=true;
        }else
         	DebugMessage.println("initAdcCsN already initialized");
        
    }

    protected SpiMaster spi = SpiMaster.getInstance();

    // There should be some mutual exclusion between this and the PtSensor
    // (and anything else that does a read-modify-write on REG_E, especially
    // in an interrupt handler, as the PtSensor does).
    int getReading(int channel) throws IllegalArgumentException {
        int dataIn;
        if (channel > 2) {
            throw new IllegalArgumentException("Channel " + channel
                                               + " doesn't exist on the adc");
        }
        channel |= 0x8; // set the EN bit; must be set to take reading
        int retval;
        int high;
        int low;
        int regE;
        synchronized (spi) {
        	// Set up SPI
            spi.setSlaveSelect(spi.SPI_SLAVE_SELECT_NONE); // just in case
	// Write channel number w/ CS high (disabled)
            spi.writeReadData(channel); // with AdcCsN high
            
            // Now set the CS (active low)
            // This way is NOT thread safe:
            //regE = rawJEM.getInt(AjileAddr.GPIOE_OR);
            //regE &= ~ADCCSN; // set E4 low, leave everything else alone
            //rawJEM.set(AjileAddr.GPIOE_OR, regE);
            DebugMessage.println("Adc.getReading() (REG_E_OR 4.0)");
            // This SHOULD be THREAD-SAFE
            rawJEM.setField(0,AjileAddr.GPIOE_OR,4,1);

            // Read data out (msb first)            
            high = spi.writeReadData(0);  // data sent to ADC is ignored
            low  = spi.writeReadData(0);
            // remember the high bit of low is sign-extended to 32 bits
        } // we now let go of the spi so someone else can use it

        // we could do all the stuff below in one line, but this is
        // easier to read
        //
        retval = (0x1f & high);
        retval <<= 8;
        retval |= (0xff & low);
        retval >>= 1;

        //regE = rawJEM.getInt(AjileAddr.GPIOE_OR);
        //regE |= ADCCSN; // set E4 high again
        //rawJEM.set(AjileAddr.GPIOE_OR, regE);
        
        // This SHOULD be THREAD-SAFE
            DebugMessage.println("Adc.getReading() (REG_E_OR 4.1)");
        rawJEM.setField(1,AjileAddr.GPIOE_OR,4,1);
        
        return retval;
    }

}
