-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathfpga.h
86 lines (72 loc) · 2.37 KB
/
fpga.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#pragma once
#include <string>
#include <fstream>
#include <vector>
constexpr int FPGA_RB_IGNORE_BYTES_CONTROL = 48;
constexpr int FPGA_RB_IGNORE_BYTES_DATA = 44;
enum {
FPGA_WR_FLAG_FULL_BS = 0,
FPGA_WR_FLAG_PARTIAL_BS = 1
};
enum {
FPGA_RB_FLAG_TYPE_CONTROL = 0,
FPGA_RB_FLAG_TYPE_DATA = 1
};
class FPGAManager {
std::string sysfsFirm, sysfsFlags, sysfsRBImage, sysfsRBFlags;
void setFPGAFlags(int flags) {
std::ofstream flagfile(sysfsFlags);
flagfile << flags;
flagfile.close();
if (!flagfile)
throw std::runtime_error("Could not write fpga flags");
}
void setFPGAFirmware(const std::string &firmware) {
std::ofstream firmfile(sysfsFirm);
firmfile << firmware;
firmfile.close();
if (!firmfile)
throw std::runtime_error("Could not write fpga firmware");
}
void setFPGAReadbackFlags(int flags) {
std::ofstream flagfile(sysfsRBFlags);
flagfile << flags;
flagfile.close();
if (!flagfile)
throw std::runtime_error("Could not write readback flags");
}
std::vector<char> performFPGAReadback(int seekoffset) {
std::ifstream readbackfile(sysfsRBImage, std::ios::binary);
readbackfile.ignore(seekoffset);
std::vector<char> filedata((std::istreambuf_iterator<char>(readbackfile)),
std::istreambuf_iterator<char>());
readbackfile.close();
if (!readbackfile)
throw std::runtime_error("Could not open readback file");
return filedata;
}
public:
FPGAManager(int fpga_id) {
std::string sysfsRoot = "/sys/class/fpga_manager/fpga" + std::to_string(fpga_id);
sysfsFirm = sysfsRoot + "/firmware";
sysfsFlags = sysfsRoot + "/flags";
sysfsRBImage = "/sys/kernel/debug/fpga/fpga" + std::to_string(fpga_id) + "/image";
sysfsRBFlags = "/sys/module/zynqmp_fpga/parameters/readback_type";
}
std::vector<char> readbackImage() {
setFPGAReadbackFlags(FPGA_RB_FLAG_TYPE_DATA);
return performFPGAReadback(FPGA_RB_IGNORE_BYTES_DATA);
}
std::vector<char> readbackConfig() {
setFPGAReadbackFlags(FPGA_RB_FLAG_TYPE_CONTROL);
return performFPGAReadback(FPGA_RB_IGNORE_BYTES_CONTROL);
}
void loadFull(const std::string &firmware) {
setFPGAFlags(FPGA_WR_FLAG_FULL_BS);
setFPGAFirmware(firmware);
}
void loadPartial(const std::string &firmware) {
setFPGAFlags(FPGA_WR_FLAG_PARTIAL_BS);
setFPGAFirmware(firmware);
}
};