/
app.c
260 lines (202 loc) · 5.07 KB
/
app.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include "PCIE.h"
#define DEMO_PCIE_USER_BAR PCIE_BAR0
#define DEMO_PCIE_IO_LED_ADDR 0x00
#define DEMO_PCIE_IO_BUTTON_ADDR 0x20
#define DEMO_PCIE_FIFO_WRITE_ADDR 0x40
#define DEMO_PCIE_FIFO_STATUS_ADDR 0x60
#define DEMO_PCIE_FIFO_READ_ADDR 0x80
#define DEMO_PCIE_MEM_ADDR 0x20000
#define MEM_SIZE (128*1024) //128KB
#define FIFO_SIZE (16*1024) // 2KBx8
typedef enum{
MENU_LED = 0,
MENU_BUTTON,
MENU_DMA_MEMORY,
MENU_DMA_FIFO,
MENU_QUIT = 99
}MENU_ID;
void UI_ShowMenu(void){
printf("==============================\r\n");
printf("[%d]: Led control\r\n", MENU_LED);
printf("[%d]: Button Status Read\r\n", MENU_BUTTON);
printf("[%d]: DMA Memory Test\r\n", MENU_DMA_MEMORY);
printf("[%d]: DMA FifoTest\r\n", MENU_DMA_FIFO);
printf("[%d]: Quit\r\n", MENU_QUIT);
printf("Plesae input your selection:");
}
int UI_UserSelect(void){
int nSel;
scanf("%d",&nSel);
return nSel;
}
BOOL TEST_LED(PCIE_HANDLE hPCIe){
BOOL bPass;
int Mask;
printf("Please input led conrol mask:");
scanf("%d", &Mask);
bPass = PCIE_Write32(hPCIe, DEMO_PCIE_USER_BAR, DEMO_PCIE_IO_LED_ADDR,(DWORD)Mask);
if (bPass)
printf("Led control success, mask=%xh\r\n", Mask);
else
printf("Led conrol failed\r\n");
return bPass;
}
BOOL TEST_BUTTON(PCIE_HANDLE hPCIe){
BOOL bPass = TRUE;
DWORD Status;
bPass = PCIE_Read32(hPCIe, DEMO_PCIE_USER_BAR, DEMO_PCIE_IO_BUTTON_ADDR,&Status);
if (bPass)
printf("Button status mask:=%xh\r\n", Status);
else
printf("Failed to read button status\r\n");
return bPass;
}
char PAT_GEN(int nIndex){
char Data;
Data = nIndex & 0xFF;
return Data;
}
BOOL TEST_DMA_MEMORY(PCIE_HANDLE hPCIe){
BOOL bPass;
int i;
const int nTestSize = MEM_SIZE;
const PCIE_LOCAL_ADDRESS LocalAddr = DEMO_PCIE_MEM_ADDR;
char *pWrite;
char *pRead;
char szError[256];
pWrite = (char *)malloc(nTestSize);
pRead = (char *)malloc(nTestSize);
if (!pWrite || !pRead){
bPass = FALSE;
sprintf(szError, "DMA Memory:malloc failed\r\n");
}
// init test pattern
for(i=0;i<nTestSize && bPass;i++)
*(pWrite+i) = PAT_GEN(i);
// write test pattern
if (bPass){
bPass = PCIE_DmaWrite(hPCIe, LocalAddr, pWrite, nTestSize);
if (!bPass)
sprintf(szError, "DMA Memory:PCIE_DmaWrite failed\r\n");
}
// read back test pattern and verify
if (bPass){
bPass = PCIE_DmaRead(hPCIe, LocalAddr, pRead, nTestSize);
if (!bPass){
sprintf(szError, "DMA Memory:PCIE_DmaRead failed\r\n");
}else{
for(i=0;i<nTestSize && bPass;i++){
if (*(pRead+i) != PAT_GEN(i)){
bPass = FALSE;
sprintf(szError, "DMA Memory:Read-back verify unmatch, index = %d, read=%xh, expected=%xh\r\n", i, *(pRead+i), PAT_GEN(i));
}
}
}
}
// free resource
if (pWrite)
free(pWrite);
if (pRead)
free(pRead);
if (!bPass)
printf("%s", szError);
else
printf("DMA-Memory (Size = %d byes) pass\r\n", nTestSize);
return bPass;
}
BOOL TEST_DMA_FIFO(PCIE_HANDLE hPCIe){
BOOL bPass;
int i;
const int nTestSize = FIFO_SIZE;
const PCIE_LOCAL_ADDRESS FifoID_Write = DEMO_PCIE_FIFO_WRITE_ADDR;
const PCIE_LOCAL_ADDRESS FifoID_Read = DEMO_PCIE_FIFO_READ_ADDR;
char *pBuff;
char szError[256];
pBuff = (char *)malloc(nTestSize);
if (!pBuff){
bPass = FALSE;
sprintf(szError, "DMA Fifo: malloc failed\r\n");
}
// init test pattern
if (bPass){
for(i=0;i<nTestSize;i++)
*(pBuff+i) = PAT_GEN(i);
}
// write test pattern into fifo
if (bPass){
bPass = PCIE_DmaFifoWrite(hPCIe, FifoID_Write, pBuff, nTestSize);
if (!bPass)
sprintf(szError, "DMA Fifo: PCIE_DmaFifoWrite failed\r\n");
}
// read back test pattern and verify
if (bPass){
memset(pBuff, 0, nTestSize); // reset buffer content
bPass = PCIE_DmaFifoRead(hPCIe, FifoID_Read, pBuff, nTestSize);
if (!bPass){
sprintf(szError, "DMA Fifo: PCIE_DmaFifoRead failed\r\n");
}else{
for(i=0;i<nTestSize && bPass;i++){
if (*(pBuff+i) != PAT_GEN(i)){
bPass = FALSE;
sprintf(szError, "DMA Fifo: Read-back verify unmatch, index = %d, read=%xh, expected=%xh\r\n", i, *(pBuff+i), PAT_GEN(i));
}
}
}
}
// free resource
if (pBuff)
free(pBuff);
if (!bPass)
printf("%s", szError);
else
printf("DMA-Fifo (Size = %d byes) pass\r\n", nTestSize);
return bPass;
}
int main1(void)
{
void *lib_handle;
PCIE_HANDLE hPCIE;
BOOL bQuit = FALSE;
int nSel;
printf("== Terasic: PCIe Demo Program ==\r\n");
lib_handle = PCIE_Load();
if (!lib_handle){
printf("PCIE_Load failed!\r\n");
return 0;
}
hPCIE = PCIE_Open(DEFAULT_PCIE_VID,DEFAULT_PCIE_DID,0);
if (!hPCIE){
printf("PCIE_Open failed\r\n");
}else{
while(!bQuit){
UI_ShowMenu();
nSel = UI_UserSelect();
switch(nSel){
case MENU_LED:
TEST_LED(hPCIE);
break;
case MENU_BUTTON:
TEST_BUTTON(hPCIE);
break;
case MENU_DMA_MEMORY:
TEST_DMA_MEMORY(hPCIE);
break;
case MENU_DMA_FIFO:
TEST_DMA_FIFO(hPCIE);
break;
case MENU_QUIT:
bQuit = TRUE;
printf("Bye!\r\n");
break;
default:
printf("Invalid selection\r\n");
} // switch
}// while
PCIE_Close(hPCIE);
}
PCIE_Unload(lib_handle);
return 0;
}