You are here: Home / easyFPGA sample code / Java
German
English
Friday, 2024-03-29

Software development for your ideas

Software Development
for your ideas.

Impossible is nothing

Impossible is nothing.

We make it yours

We make it yours.

Supporting your business

Supporting your business.

Software development for your ideas

Definition of the FPGA

Without the need of delving into hardware description languages, the FPGA's behavior can be defined using ready-made modules (easyCores) in Java. For that purpose the implementation of the build() method of an annotated class derived from FPGA is used in Java:


/*
 * Use an annotated class that extends FPGA to create your easyCores and
 * connect them to the board's pin headers
 */
@EasyFPGA
public class MyEasyFPGA extends FPGA {
    private PWM16 pwm;
    private GPIO8 gpio;
    private I2CMaster i2cMaster;

    /*
     * Override the build() method to define your FPGA
     */
    @Override
    public void build() throws Exception {

        // Construct the easyCores
        pwm = new PWM16();
        gpio = new GPIO8();
        i2cMaster = new I2CMaster();

        // Connect PWM output to Bank0.0
        connect(pwm.getPin(PWM.PIN.OUT), getPin(0,0));

        // Connect all pins of GPIO8 from Bank1.0 to Bank1.7 using a Bus
        connect(gpio.getBus(), getBus(getPin(1,0), 8));

        // Connect I2CMaster to Bank2.22 and Bank2.23
        connect(i2cMaster.getPin(I2CMaster.PIN.SDA), getPin(2,22));
        connect(i2cMaster.getPin(I2CMaster.PIN.SCL), getPin(2,23));
    }

    /*
     * easyCore getter methods:
     */
    public PWM16 getPWM() {
        return pwm;
    }

    public GPIO8 getGPIO() {
        return gpio;
    }

    public I2CMaster getI2C() {
        return i2cMaster;
    }
}

Communication via USB

As shown below, an arbitrary Java application can connect to the easyFPGA board and communicate with the easyCores.


/*
 * An arbitrary Java project can connect to the board via USB
 * and communicate with the easyCores
 */
public class ExampleProject {

    private void run() throws Exception {

        // Open a connection to MyFPGA
        MyEasyFPGA fpga = new MyEasyFPGA();
        fpga.init();

        // Get the easyCores
        PWM16 pwm = fpga.getPWM();
        GPIO8 gpio = fpga.getGPIO();
        I2CMaster i2c = fpga.getI2C();

        // PWM: Set a 16-bit dutycycle
        pwm.setDutyCycle(0x1234);

        // GPIO: Make pin #2 an output an set it to high
        gpio.makeOutput(2);
        gpio.setOutput(2, true);

        // GPIO: Make pin #3 an input an get the logic level
        gpio.makeInput(3);
        boolean pin3 = gpio.getInput(3);
        System.out.println("Pin #3: " + pin3);

        // I2C: Initialize in fast mode (350 kbit/s)
        i2c.init(I2CMaster.MODE_FAST);
        final int I2C_DEVICE_ADDRESS = 0xEA;
        final int STATUS_REGISTER = 0x01;
        final int CONTROL_REGISTER = 0x02;

        // I2C: Read and print the status register of an I2C device
        int status = i2c.readByte(I2C_DEVICE_ADDRESS, STATUS_REGISTER);
        System.out.println(String.format("Status: 0x%02X", status));

        // I2C: Write data to the control register
        i2c.writeByte(I2C_DEVICE_ADDRESS, CONTROL_REGISTER, 0x40);

        // close connection
        fpga.quit();
    }

    public static void main(String[] args) {
        ExampleProject project = new ExampleProject();
        try {
            project.run();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}