-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab2_main.c
executable file
·347 lines (296 loc) · 9.92 KB
/
lab2_main.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* EE109 Starter Code
* Spring 2012
* Sam D'Amico
*
* Minor edits by Chris Copeland in 2013
*/
#include <stdio.h>
#include "xparameters.h"
#include "xil_cache.h"
#include "xintc.h"
#include "intc_header.h"
#include "xbasic_types.h"
#include "xgpio.h"
#include "gpio_header.h"
#include "xemaclite.h"
#include "xemaclite_example.h"
#include "emaclite_header.h"
#include "uartlite_header.h"
#include "xemaclite_example.h"
#include "xaxivdma.h"
#include "xaxidma.h"
// Size of the frames being sent
#define EMACLITE_TEST_FRAME_SIZE 1000
void InitVideo();
static int EmacLiteSendFrame(XEmacLite *InstancePtr, u32 PayloadSize);
static int EmacLiteRecvFrame(u32 PayloadSize);
static u8 LocalAddress[XEL_MAC_ADDR_SIZE] =
{
0x00, 0x0A, 0x35, 0x01, 0x02, 0x03
};
static u8 RemoteAddress[XEL_MAC_ADDR_SIZE] =
{
0x00, 0x10, 0xa4, 0xb6, 0xfd, 0x09
};
// Framebuffer for video
static XAxiVdma vdma;
static XAxiVdma_DmaSetup dmasetup;
static u32 frames[640*480*3];
static u16 camera[640*480];
// Framebuffer pointer
unsigned char *fptr;
XIntc intcInstance;
unsigned int t[] = {1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,1,1,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,1,1,0,0,0,0,0,0,0,0,0};
int main()
{
static XIntc intc;
Xil_ICacheEnable();
Xil_DCacheEnable();
xil_printf("Testing UART output.\r\n");
// Camera DMA Configuration
XAxiDma_Config *dmaconf = XAxiDma_LookupConfig(XPAR_AXI_DMA_0_DEVICE_ID);
XAxiDma dma;
XAxiDma_CfgInitialize(&dma, dmaconf);
XAxiDma_Resume(&dma);
XAxiDma_Reset(&dma);
while(!XAxiDma_ResetIsDone(&dma));
// Initialize Video
InitVideo();
// Example camera DMA read
// Note - transfer MUST be 4096B
// Data format is 20-bit pixel number (stored in a 32 bit integer) followed by 16-bit RGB values
// This will not work until you implement camera_stream.v.
// (or at least, until you have it barely working)
//int resdde = XAxiDma_SimpleTransfer(&dma, (u32)((unsigned char*)&camera), 4096, XAXIDMA_DEVICE_TO_DMA);
//while(XAxiDma_Busy(&dma,XAXIDMA_DEVICE_TO_DMA));
int Status;
XEmacLite *EmacLiteInstPtr = &EmacLiteInstance;
u32 PhyAddress = 0;
RecvFrameLength = 0;
XEmacLite_Config *ConfigPtr;
/*
* Initialize the EmacLite device.
*/
ConfigPtr = XEmacLite_LookupConfig(XPAR_ETHERNET_LITE_DEVICE_ID);
if (ConfigPtr == NULL) {
return XST_FAILURE;
}
Status = XEmacLite_CfgInitialize(EmacLiteInstPtr,
ConfigPtr,
ConfigPtr->BaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
/*
* Set the MAC address.
*/
XEmacLite_SetMacAddress(EmacLiteInstPtr, LocalAddress);
/*
* Empty any existing receive frames.
*/
XEmacLite_FlushReceive(EmacLiteInstPtr);
/*
* Check if there is a TX buffer available, if there isn't it is an
* error.
*/
if (XEmacLite_TxBufferAvailable(EmacLiteInstPtr) != TRUE) {
return XST_FAILURE;
}
/*
* Reset the receive frame length to zero.
*/
RecvFrameLength = 0;
// Example program that sends packets and changes the color of an on-screen box upon receiving a packet.
unsigned char c = 0;
unsigned char r=0xFF, g=0x0, b=0x0;
while(1) {
c++;
if(XEmacLite_IsTxDone(ConfigPtr->BaseAddress)) {
Status = EmacLiteSendFrame(EmacLiteInstPtr, EMACLITE_TEST_FRAME_SIZE);
if (Status != XST_SUCCESS) {
if (XEmacLite_IsMdioConfigured(EmacLiteInstPtr)) {
return XST_FAILURE;
}
}
}
else {
RecvFrameLength = XEmacLite_Recv(EmacLiteInstPtr, (u8 *)RxFrame);
if (RecvFrameLength > 0) {
xil_printf("Received a packet!\r\n");
unsigned char oldr = r;
r=g;
g=b;
b=oldr;
}
// Example of writing data to the screen
int x, y;
for (x = 0; x < 20; x++)
for (y = 0; y < 20; y++)
if (t[y*20 + x]) {
fptr[y*640*4 + x*4] = b;
fptr[y*640*4 + x*4 + 1] = g;
fptr[y*640*4 + x*4 + 2] = r;
}
}
}
Xil_DCacheDisable();
Xil_ICacheDisable();
return 0;
}
void InitVideo()
{
XAxiVdma_CfgInitialize(&vdma, XAxiVdma_LookupConfig(XPAR_AXI_VDMA_0_DEVICE_ID), XPAR_AXI_VDMA_0_BASEADDR);
XAxiVdma_Reset(&vdma, XAXIVDMA_READ);
while(XAxiVdma_ResetNotDone(&vdma, XAXIVDMA_READ));
dmasetup.VertSizeInput = 480;
dmasetup.HoriSizeInput = 640*4;
dmasetup.FrameDelay = 0;
dmasetup.EnableCircularBuf = 0;
dmasetup.EnableSync = 0;
dmasetup.PointNum = 0;
dmasetup.EnableFrameCounter = 0;
dmasetup.Stride = 2560;
fptr = (unsigned char*)&frames;
dmasetup.FixedFrameStoreAddr = 0;
dmasetup.FrameStoreStartAddr[0] = (int)fptr;
dmasetup.FrameStoreStartAddr[1] = (int)fptr+dmasetup.Stride*480;
dmasetup.FrameStoreStartAddr[2] = (int)fptr+dmasetup.Stride*480*2;
XAxiVdma_DmaConfig(&vdma, XAXIVDMA_READ, &dmasetup);
XAxiVdma_DmaSetBufferAddr(&vdma, XAXIVDMA_READ, dmasetup.FrameStoreStartAddr);
XAxiVdma_DmaStart(&vdma, XAXIVDMA_READ);
}
/******************************************************************************/
/**
*
* This function sends a frame of given size.
*
* @param XEmacInstancePtr is a pointer to the XEmacLite instance.
* @param PayloadSize is the size of the frame to create. The size only
* reflects the payload size, it does not include the Ethernet
* header size (14 bytes) nor the Ethernet CRC size (4 bytes).
*
* @return XST_SUCCESS if successful, else a driver-specific return code.
*
* @note None.
*
******************************************************************************/
static int EmacLiteSendFrame(XEmacLite *InstancePtr, u32 PayloadSize)
{
u8 *FramePtr;
int Index;
FramePtr = (u8 *)TxFrame;
/*
* Set up the destination address as the local address for
* Phy Loopback.
*/
if (XEmacLite_IsMdioConfigured(InstancePtr)) {
*FramePtr++ = LocalAddress[0];
*FramePtr++ = LocalAddress[1];
*FramePtr++ = LocalAddress[2];
*FramePtr++ = LocalAddress[3];
*FramePtr++ = LocalAddress[4];
*FramePtr++ = LocalAddress[5];
} else {
/*
* Fill in the valid Destination MAC address if
* the Loopback is not enabled.
*/
*FramePtr++ = RemoteAddress[0];
*FramePtr++ = RemoteAddress[1];
*FramePtr++ = RemoteAddress[2];
*FramePtr++ = RemoteAddress[3];
*FramePtr++ = RemoteAddress[4];
*FramePtr++ = RemoteAddress[5];
}
/*
* Fill in the source MAC address.
*/
*FramePtr++ = LocalAddress[0];
*FramePtr++ = LocalAddress[1];
*FramePtr++ = LocalAddress[2];
*FramePtr++ = LocalAddress[3];
*FramePtr++ = LocalAddress[4];
*FramePtr++ = LocalAddress[5];
/*
* Set up the type/length field - be sure its in network order.
*/
*((u16 *)FramePtr) = Xil_Htons(PayloadSize);
FramePtr++;
FramePtr++;
/*
* Now fill in the data field with known values so we can verify them
* on receive.
*/
for (Index = 0; Index < PayloadSize; Index++) {
*FramePtr++ = (u8)Index;
}
/*
* Now send the frame.
*/
return XEmacLite_Send(InstancePtr, (u8 *)TxFrame,
PayloadSize + XEL_HEADER_SIZE);
}
/******************************************************************************/
/**
*
* This function receives a frame of given size. This function assumes interrupt
* mode, receives the frame and verifies its contents.
*
* @param PayloadSize is the size of the frame to receive.
* The size only reflects the payload size, it does not include the
* Ethernet header size (14 bytes) nor the Ethernet CRC size (4
* bytes).
*
* @return XST_SUCCESS if successful, a driver-specific return code if not.
*
* @note None.
*
******************************************************************************/
static int EmacLiteRecvFrame(u32 PayloadSize)
{
u8 *FramePtr;
/*
* This assumes MAC does not strip padding or CRC.
*/
if (RecvFrameLength != 0) {
int Index;
/*
* Verify length, which should be the payload size.
*/
if ((RecvFrameLength- (XEL_HEADER_SIZE + XEL_FCS_SIZE)) !=
PayloadSize) {
return XST_LOOPBACK_ERROR;
}
/*
* Verify the contents of the Received Frame.
*/
FramePtr = (u8 *)RxFrame;
FramePtr += XEL_HEADER_SIZE; /* Get past the header */
for (Index = 0; Index < PayloadSize; Index++) {
if (*FramePtr++ != (u8)Index) {
return XST_LOOPBACK_ERROR;
}
}
}
return XST_SUCCESS;
}