You are here: Home / easyFPGA sample code / C++
German
English
Friday, 2024-04-19

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). For that purpose the implementation of the abstract method defineStructure() of the class EasyFPGA is used in C++:


// pwm_fpga.h

#ifndef MYPROJECT_PWM_FPGA_H_
#define MYPROJECT_PWM_FPGA_H_

#include "easyfpga/easyfpga.h"
#include "easyfpga/easycores/pwm/pwm8_ptr.h"

/* FPGA description template for the user application */
class PwmFpga : public EasyFpga
{
    public:
        // easyCores will be instantiated in the constructor
        PwmFpga();
        ~PwmFpga();

        // here, the easyCores will be added and connected
        void defineStructure(void);

        // the easyCore getter method to be called in the host application
        pwm8_ptr getPwm8(void);

    private:
        pwm8_ptr _pwm8;
};

/* This macro is necessary for the binary generation process. Please
make sure, that the macro's parameter has the same value as the name of
this binary description class (here: PwmFpga). */
EASY_FPGA_DESCRIPTION_CLASS(PwmFpga)

#endif  // MYPROJECT_PWM_FPGA_H_

// pwm_fpga.cc

#include "pwm_fpga.h"
#include "easyfpga/easycores/pwm/pwm8.h"

PwmFpga::PwmFpga() :
    _pwm8(std::make_shared<Pwm8>())
{
}

PwmFpga::~PwmFpga()
{
}

void PwmFpga::defineStructure(void)
{
    // prior to connecting an easyCore it has to be added
    this->addEasyCore(_pwm8);

    // connect to an external pin
    this->connect(_pwm8, Pwm8::PIN::PWM_OUT, GPIOPIN::BANK0_PIN0);
}

pwm8_ptr PwmFpga::getPwm8(void)
{
    return _pwm8;
}

Communication via USB

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


// host_application.cc

#include "pwm_fpga.h"

#include "easyfpga/easycores/pwm/pwm8.h"
#include "easyfpga/easycores/pwm/pwm8_ptr.h"

#include <memory> // for shared pointers

int main(int argc, char** argv)
{
    // Initialize the FPGA with the init() member. That does:
    // 1) Check if the specified binary exists or tries to create it.
    // 2) Connect to an easyFPGA board specified by a serial number,
    //    here 0, which will connect the framework to first easyFPGA
    //    board found.
    // 3) Upload the generated binary and configure it into the FPGA's
    //    RAM if this isn't already done before with this binary.
    std::shared_ptr<PwmFpga> fpga = std::make_shared<PwmFpga>();
    fpga->init(0, "PwmFpga.bin");

    // Gets an easyCore instance from the description class
    pwm8_ptr pwm = fpga->getPwm8();

    // Start communication by using the respective core API.
    pwm->setDutyCycle(0x32);

    return 0;
}