-
Notifications
You must be signed in to change notification settings - Fork 0
/
PCIE.c
109 lines (88 loc) · 2.72 KB
/
PCIE.c
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <stdio.h>
#include <string.h>
#include "PCIE.h"
#if defined(__GNUC__)
#include <dlfcn.h> // dlopen/dlclsoe for linxu
#else
#define dlopen(x,y) LoadLibrary(x)
#define dlclose FreeLibrary
#define dlsym GetProcAddress
#define dlerror() "LoadLibrary failed"
#endif //
LPPCIE_Open PCIE_Open;
LPPCIE_Close PCIE_Close;
LPPCIE_Read32 PCIE_Read32;
LPPCIE_Write32 PCIE_Write32;
LPPCIE_Read16 PCIE_Read16;
LPPCIE_Write16 PCIE_Write16;
LPPCIE_Read8 PCIE_Read8;
LPPCIE_Write8 PCIE_Write8;
LPPCIE_DmaWrite PCIE_DmaWrite;
LPPCIE_DmaRead PCIE_DmaRead;
LPPCIE_DmaFifoWrite PCIE_DmaFifoWrite;
LPPCIE_DmaFifoRead PCIE_DmaFifoRead;
void QueryModualName(char szName[]){
#if defined(__GNUC__)
strcpy(szName, "./terasic_pcie_qsys.so");
#else
// windows
//check OS
BOOL bIsWow64 = FALSE;
typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);
LPFN_ISWOW64PROCESS fnIsWow64Process;
fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress( GetModuleHandle("kernel32"),"IsWow64Process");
if (NULL != fnIsWow64Process)
{
fnIsWow64Process(GetCurrentProcess(),&bIsWow64);
}
if(bIsWow64)
{
strcpy(szName, "TERASIC_PCIE_QSYSx64.DLL");
}
else
{
strcpy(szName, "TERASIC_PCIE_QSYS.DLL");
}
#endif
}
void *PCIE_Load(void){
BOOL bSuccess = TRUE;
void *lib_handle;
char szName[256];
QueryModualName(szName);
lib_handle = dlopen(szName, RTLD_NOW);
if (!lib_handle){
printf("Load %s error: %s\r\n", szName, dlerror());
bSuccess = FALSE;
}
if(bSuccess){
PCIE_Open = (LPPCIE_Open)dlsym(lib_handle, "PCIE_Open");
PCIE_Close = (LPPCIE_Close)dlsym(lib_handle, "PCIE_Close");
PCIE_Read32 = (LPPCIE_Read32)dlsym(lib_handle, "PCIE_Read32");
PCIE_Write32 = (LPPCIE_Write32)dlsym(lib_handle, "PCIE_Write32");
PCIE_Read16 = (LPPCIE_Read16)dlsym(lib_handle, "PCIE_Read16");
PCIE_Write16 = (LPPCIE_Write16)dlsym(lib_handle, "PCIE_Write16");
PCIE_Read8 = (LPPCIE_Read8)dlsym(lib_handle, "PCIE_Read8");
PCIE_Write8 = (LPPCIE_Write8)dlsym(lib_handle, "PCIE_Write8");
PCIE_DmaWrite = (LPPCIE_DmaWrite)dlsym(lib_handle, "PCIE_DmaWrite");
PCIE_DmaRead = (LPPCIE_DmaRead)dlsym(lib_handle, "PCIE_DmaRead");
PCIE_DmaFifoWrite = (LPPCIE_DmaFifoWrite)dlsym(lib_handle, "PCIE_DmaFifoWrite");
PCIE_DmaFifoRead = (LPPCIE_DmaFifoRead)dlsym(lib_handle, "PCIE_DmaFifoRead");
if (!PCIE_Open || !PCIE_Close ||
!PCIE_Read32 || !PCIE_Write32 ||
!PCIE_Read16 || !PCIE_Write16 ||
!PCIE_Read8 || !PCIE_Write8 ||
!PCIE_DmaWrite || !PCIE_DmaRead ||
!PCIE_DmaFifoWrite || !PCIE_DmaFifoRead
)
bSuccess = FALSE;
if (!bSuccess){
dlclose(lib_handle);
lib_handle = 0;
}
}
return lib_handle;
}
void PCIE_Unload(void *lib_handle){
dlclose(lib_handle);
}