/** 
 * Title:        Producer/Consumer example 
 * Company:      Ajile 
 * @author Phil WIley 
 */ 
package producerconsumerexample; 
import com.ajile.jem.rawJEM; 
  
  
/** 
 * Example program of how to manually setup up an interrupt handler, and 
 * of how to disable the interrupt from inside of the interrupt handler. 
 * 
 * This example configures GPIO port a to generate an interrupt when the bit 
 * changes state. After the interrupt is received it disables the interrupt. 
 */ 
public class App   { 

    /** 
     * addresses of various registers used to configure GPIO-A 
     * These addresses are defined in the aj-100 reference manual 
     */ 
    private static final int GPIOA_OUTPUT_ENABLE = 0x9002000 ; 
    private static final int GPIOA_INPUT         = 0x9002008 ; 
    private static final int GPIOA_POLARITY      = 0x9002010 ; 
    private static final int GPIOA_LEVEL_EDGE    = 0x9002014 ; 
    private static final int GPIOA_EDGE_MODE     = 0x9002018 ; 
    private static final int GPIOA_INT_ENABLE    = 0x900201C ; 
    private static final int GPIOA_PENDING       = 0x9002020 ; 
    private static final int GPIOA_CLEAR_INT     = 0x9002024 ; 

    public static void bp(int i) {
        i++;
    }

    /** 
     * The interrupt number assigned to the GPIO Interrupt. This value
     * matches the Priority field for the GPIOA interrupt found in the
     * JemBuilder:JVM0|aj100Interrupts page.
     */ 
    private static final int GPIO_INTERRUPT_NUMBER = 14; 

    private static ConsumerThread consumerThread = null; 
    static  int interruptCnt = 0; 

    /** 
     * constant, specifies the mask for bit 5 
     */ 
    private static final int BIT5_MASK = 0x20; 

    /** 
     * main entry point for the program. 
     */ 
    public static void main(String[] args) { 
      
        bp(1);
        consumerThread = new ConsumerThread(); 
        bp(2);
        consumerThread.start(); 
        bp(3);

        configureGPIOA(); 
        bp(4);
    } 
  

    /** 
     * The method called when GPIO port A interrupt is generated. This
     * is configured in JemBuilder:JVM0|aj100Interrupts page. The
     * value listed in the GPIOA field is:
     * disableintexample.App.GPIOInterruptHandler()V
     */ 
    static public void GPIOInterruptHandler() { 

        // send the data to the consuming thread 
        consumerThread.wakeUp(interruptCnt++); 

        // reset the interrupt. Since the interrupt is set to occur on transitions 
        // this call must be made to clear the edge detector. 
        rawJEM.set(GPIOA_CLEAR_INT,BIT5_MASK); 

    } 
  

    /** 
     * Manually configure the GPIOA port to generate interrupts on
     * edge transition.  Further information on configuring the GPIO
     * registers can be found in the aj100 reference manual.
     */ 
    static private void configureGPIOA() { 
        rawJEM.setField(0,GPIOA_OUTPUT_ENABLE,5,1); // clear bit 5, configure as input bit 
        rawJEM.set(GPIOA_INT_ENABLE,BIT5_MASK); // enable gpioA.io5 
        rawJEM.set(GPIOA_LEVEL_EDGE,0xff);      // set to edge level triggering 
        rawJEM.set(GPIOA_EDGE_MODE,0); 

        //enable the GPIO interrupt 
        rawJEM.enableInt(GPIO_INTERRUPT_NUMBER); 

    } 
  

} // end class 
  
  
  

