Definition des FPGAs
Ganz ohne die Verwendung einer Hardwarebeschreibungssprache kann die Struktur eines FPGAs mit Hilfe vorgefertigter Module (easyCores) definiert werden. Dazu dient in C++ die Implementierung der abstrakten defineStructure()
Methode der Klasse EasyFpga
:
// 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;
}
Kommunikation über USB
Aus einer beliebigen C++-Anwendung kann eine Verbindung mit dem easyFPGA Board aufgebaut werden. Über diese können die easyCores angesteuert werden:
// 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;
}