-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvideo_frame_buffer.c
460 lines (383 loc) · 14.6 KB
/
video_frame_buffer.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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
//----------------------------------------------------------------
// _____
// / \
// /____ \____
// / \===\ \==/
// /___\===\___\/ AVNET
// \======/
// \====/
//---------------------------------------------------------------
//
// This design is the property of Avnet. Publication of this
// design is not authorized without written consent from Avnet.
//
// Please direct any questions to: technical.support@avnet.com
//
// Disclaimer:
// Avnet, Inc. makes no warranty for the use of this code or design.
// This code is provided "As Is". Avnet, Inc assumes no responsibility for
// any errors, which may appear in this code, nor does it make a commitment
// to update the information contained herein. Avnet, Inc specifically
// disclaims any implied warranties of fitness for a particular purpose.
// Copyright(c) 2011 Avnet, Inc.
// All rights reserved.
//
//----------------------------------------------------------------
//
// Create Date: Dec 23, 2011
// Design Name: Video Frame Buffer
// Module Name: video_frame_buffer.c
// Project Name: FMC-IMAGEON
// Target Devices: Zynq-7000 SoC
// Hardware Boards: ZC702 + FMC-IMAGEON
//
// Tool versions: ISE 14.2
//
// Description: Video Frame Buffer
// - use with AXI_VDMA pcore
//
// Dependencies:
//
// Revision: Dec 23, 2011: 1.00 Initial Version
// Sep 17, 2012: 1.01 Remove TX initialization
//
//----------------------------------------------------------------
#include <stdio.h>
//#include "platform.h"
#include "xparameters.h"
#include "video_resolution.h"
#include "xaxivdma.h"
#define NUMBER_OF_READ_FRAMES XPAR_AXIVDMA_0_NUM_FSTORES
#define NUMBER_OF_WRITE_FRAMES XPAR_AXIVDMA_0_NUM_FSTORES
int vfb_common_init( u16 uDeviceId, XAxiVdma *pAxiVdma )
{
int Status;
XAxiVdma_Config *Config;
Config = XAxiVdma_LookupConfig( uDeviceId );
if (!Config)
{
xil_printf( "No video DMA found for ID %d\n\r", uDeviceId);
return 1;
}
/* Initialize DMA engine */
Status = XAxiVdma_CfgInitialize(pAxiVdma, Config, Config->BaseAddress);
if (Status != XST_SUCCESS)
{
xil_printf( "Initialization failed %d\n\r", Status);
return 1;
}
return 0;
}
int vfb_rx_init( XAxiVdma *pAxiVdma, XAxiVdma_DmaSetup *pWriteCfg, Xuint32 uVideoResolution, Xuint32 uStorageResolution, Xuint32 uMemAddr, Xuint32 uNumFrames )
{
int Status;
XAxiVdma_FrameCounter FrameCfg;
u32 uBaseAddr;
u32 uDMACR;
/* Setup the write channel */
Status = vfb_rx_setup(pAxiVdma,pWriteCfg,uVideoResolution,uStorageResolution,uMemAddr,uNumFrames);
if (Status != XST_SUCCESS) {
xdbg_printf(XDBG_DEBUG_ERROR,
"Write channel setup failed %d\r\n", Status);
return 1;
}
/* Start the DMA engine to transfer
*/
Status = vfb_rx_start(pAxiVdma);
if (Status != XST_SUCCESS) {
return 1;
}
XAxiVdma_FsyncSrcSelect(pAxiVdma, XAXIVDMA_S2MM_TUSER_FSYNC, XAXIVDMA_WRITE);
}
int vfb_tx_init( XAxiVdma *pAxiVdma, XAxiVdma_DmaSetup *pReadCfg, Xuint32 uVideoResolution, Xuint32 uStorageResolution, Xuint32 uMemAddr, Xuint32 uNumFrames )
{
int Status;
XAxiVdma_FrameCounter FrameCfg;
u32 uBaseAddr;
u32 uDMACR;
/* Setup the read channel */
Status = vfb_tx_setup(pAxiVdma,pReadCfg,uVideoResolution,uStorageResolution,uMemAddr,uNumFrames);
if (Status != XST_SUCCESS) {
xdbg_printf(XDBG_DEBUG_ERROR,
"Read channel setup failed %d\n\r", Status);
return 1;
}
/* Start the DMA engine to transfer
*/
Status = vfb_tx_start(pAxiVdma);
if (Status != XST_SUCCESS) {
return 1;
}
#if 0
// This function returns prematurely due to (!Channel->GenLock) evaluating to false
XAxiVdma_GenLockSourceSelect(pAxiVdma, XAXIVDMA_INTERNAL_GENLOCK, XAXIVDMA_READ);
#else
uBaseAddr = pAxiVdma->BaseAddr;
//xil_printf( "\t MM2S_DMACR = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_TX_OFFSET+XAXIVDMA_CR_OFFSET )) );
uDMACR = *((volatile int *)(uBaseAddr+XAXIVDMA_TX_OFFSET+XAXIVDMA_CR_OFFSET ));
uDMACR |= 0x00000080;
*((volatile int *)(uBaseAddr+XAXIVDMA_TX_OFFSET+XAXIVDMA_CR_OFFSET )) = uDMACR;
//xil_printf( "\t MM2S_DMACR = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_TX_OFFSET+XAXIVDMA_CR_OFFSET )) );
#endif
}
int vfb_rx_setup(XAxiVdma *pAxiVdma, XAxiVdma_DmaSetup *pWriteCfg, Xuint32 uVideoResolution, Xuint32 uStorageResolution, Xuint32 uMemAddr, Xuint32 uNumFrames )
{
int i;
u32 Addr;
int Status;
Xuint32 video_width, video_height;
Xuint32 storage_width, storage_height, storage_stride, storage_size, storage_offset;
// Get Video dimensions
video_height = vres_get_height( uVideoResolution ); // in lines
video_width = vres_get_width ( uVideoResolution ) << 1; // in bytes
// Get Storage dimensions
storage_height = vres_get_height( uStorageResolution ); // in lines
storage_width = vres_get_width ( uStorageResolution ) << 1; // in bytes
storage_stride = storage_width;
storage_size = storage_width * storage_height;
storage_offset = ((storage_height-video_height)>>1)*storage_width + ((storage_width-video_width)>>1);
pWriteCfg->VertSizeInput = video_height;
pWriteCfg->HoriSizeInput = video_width;
pWriteCfg->Stride = storage_stride;
pWriteCfg->FrameDelay = 0; /* This example does not test frame delay */
pWriteCfg->EnableCircularBuf = 1;
pWriteCfg->EnableSync = 1;
pWriteCfg->PointNum = 1;
pWriteCfg->EnableFrameCounter = 0; /* Endless transfers */
pWriteCfg->FixedFrameStoreAddr = 0; /* We are not doing parking */
Status = XAxiVdma_DmaConfig(pAxiVdma, XAXIVDMA_WRITE, pWriteCfg);
if (Status != XST_SUCCESS) {
xdbg_printf(XDBG_DEBUG_ERROR,
"Write channel config failed %d\r\n", Status);
return XST_FAILURE;
}
/* Initialize buffer addresses
*
* Use physical addresses
*/
Addr = uMemAddr + storage_offset;
for(i = 0; i < uNumFrames; i++)
{
pWriteCfg->FrameStoreStartAddr[i] = Addr;
Addr += storage_size;
}
/* Set the buffer addresses for transfer in the DMA engine
*/
Status = XAxiVdma_DmaSetBufferAddr(pAxiVdma, XAXIVDMA_WRITE,
pWriteCfg->FrameStoreStartAddr);
if (Status != XST_SUCCESS) {
xdbg_printf(XDBG_DEBUG_ERROR,
"Write channel set buffer address failed %d\r\n", Status);
return XST_FAILURE;
}
return XST_SUCCESS;
}
int vfb_tx_setup(XAxiVdma *pAxiVdma, XAxiVdma_DmaSetup *pReadCfg, Xuint32 uVideoResolution, Xuint32 uStorageResolution, Xuint32 uMemAddr, Xuint32 uNumFrames )
{
int i;
u32 Addr;
int Status;
Xuint32 video_width, video_height;
Xuint32 storage_width, storage_height, storage_stride, storage_size, storage_offset;
// Get Video dimensions
video_height = vres_get_height( uVideoResolution ); // in lines
video_width = vres_get_width ( uVideoResolution ) << 1; // in bytes
// Get Storage dimensions
storage_height = vres_get_height( uStorageResolution ); // in lines
storage_width = vres_get_width ( uStorageResolution ) << 1; // in bytes
storage_stride = storage_width;
storage_size = storage_width * storage_height;
storage_offset = ((storage_height-video_height)>>1)*storage_width + ((storage_width-video_width)>>1);
pReadCfg->VertSizeInput = video_height;
pReadCfg->HoriSizeInput = video_width;
pReadCfg->Stride = storage_stride;
pReadCfg->FrameDelay = 0; /* This example does not test frame delay */
pReadCfg->EnableCircularBuf = 1;
pReadCfg->EnableSync = 1;
pReadCfg->PointNum = 1;
pReadCfg->EnableFrameCounter = 0; /* Endless transfers */
pReadCfg->FixedFrameStoreAddr = 0; /* We are not doing parking */
Status = XAxiVdma_DmaConfig(pAxiVdma, XAXIVDMA_READ, pReadCfg);
if (Status != XST_SUCCESS) {
xdbg_printf(XDBG_DEBUG_ERROR,
"Read channel config failed %d\n\r", Status);
return XST_FAILURE;
}
/* Initialize buffer addresses
*
* These addresses are physical addresses
*/
Addr = uMemAddr + storage_offset;
for(i = 0; i < uNumFrames; i++)
{
pReadCfg->FrameStoreStartAddr[i] = Addr;
Addr += storage_size;
}
/* Set the buffer addresses for transfer in the DMA engine
* The buffer addresses are physical addresses
*/
Status = XAxiVdma_DmaSetBufferAddr(pAxiVdma, XAXIVDMA_READ,
pReadCfg->FrameStoreStartAddr);
if (Status != XST_SUCCESS) {
xdbg_printf(XDBG_DEBUG_ERROR,
"Read channel set buffer address failed %d\n\r", Status);
return XST_FAILURE;
}
return XST_SUCCESS;
}
int vfb_rx_start(XAxiVdma *pAxiVdma)
{
int Status;
// S2MM Startup
Status = XAxiVdma_DmaStart(pAxiVdma, XAXIVDMA_WRITE);
if (Status != XST_SUCCESS)
{
xil_printf( "Start Write transfer failed %d\r\n", Status);
return XST_FAILURE;
}
return XST_SUCCESS;
}
int vfb_tx_start(XAxiVdma *pAxiVdma)
{
int Status;
// MM2S Startup
Status = XAxiVdma_DmaStart(pAxiVdma, XAXIVDMA_READ);
if (Status != XST_SUCCESS)
{
xil_printf("Start read transfer failed %d\n\r", Status);
return XST_FAILURE;
}
return XST_SUCCESS;
}
int vfb_rx_stop(XAxiVdma *pAxiVdma)
{
// S2MM Stop
XAxiVdma_DmaStop(pAxiVdma, XAXIVDMA_WRITE);
return XST_SUCCESS;
}
int vfb_tx_stop(XAxiVdma *pAxiVdma)
{
// MM2S Stop
XAxiVdma_DmaStop(pAxiVdma, XAXIVDMA_READ);
return XST_SUCCESS;
}
int vfb_dump_registers(XAxiVdma *pAxiVdma)
{
u32 uBaseAddr = pAxiVdma->BaseAddr;
// Partial Register Dump
xil_printf( "AXI_VDMA - Partial Register Dump (uBaseAddr = 0x%08X):\n\r", uBaseAddr );
xil_printf( "\t PARKPTR = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_PARKPTR_OFFSET )) );
xil_printf( "\t ----------------\n\r" );
xil_printf( "\t S2MM_DMACR = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_RX_OFFSET+XAXIVDMA_CR_OFFSET )) );
xil_printf( "\t S2MM_DMASR = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_RX_OFFSET+XAXIVDMA_SR_OFFSET )) );
xil_printf( "\t S2MM_STRD_FRMDLY = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_S2MM_ADDR_OFFSET+XAXIVDMA_STRD_FRMDLY_OFFSET)) );
xil_printf( "\t S2MM_START_ADDR0 = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_S2MM_ADDR_OFFSET+XAXIVDMA_START_ADDR_OFFSET+0)) );
xil_printf( "\t S2MM_START_ADDR1 = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_S2MM_ADDR_OFFSET+XAXIVDMA_START_ADDR_OFFSET+4)) );
xil_printf( "\t S2MM_START_ADDR2 = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_S2MM_ADDR_OFFSET+XAXIVDMA_START_ADDR_OFFSET+8)) );
xil_printf( "\t S2MM_HSIZE = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_S2MM_ADDR_OFFSET+XAXIVDMA_HSIZE_OFFSET)) );
xil_printf( "\t S2MM_VSIZE = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_S2MM_ADDR_OFFSET+XAXIVDMA_VSIZE_OFFSET)) );
xil_printf( "\t ----------------\n\r" );
xil_printf( "\t MM2S_DMACR = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_TX_OFFSET+XAXIVDMA_CR_OFFSET )) );
xil_printf( "\t MM2S_DMASR = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_TX_OFFSET+XAXIVDMA_SR_OFFSET )) );
xil_printf( "\t MM2S_STRD_FRMDLY = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_MM2S_ADDR_OFFSET+XAXIVDMA_STRD_FRMDLY_OFFSET)) );
xil_printf( "\t MM2S_START_ADDR0 = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_MM2S_ADDR_OFFSET+XAXIVDMA_START_ADDR_OFFSET+0)) );
xil_printf( "\t MM2S_START_ADDR1 = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_MM2S_ADDR_OFFSET+XAXIVDMA_START_ADDR_OFFSET+4)) );
xil_printf( "\t MM2S_START_ADDR2 = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_MM2S_ADDR_OFFSET+XAXIVDMA_START_ADDR_OFFSET+8)) );
xil_printf( "\t MM2S_HSIZE = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_MM2S_ADDR_OFFSET+XAXIVDMA_HSIZE_OFFSET)) );
xil_printf( "\t MM2S_VSIZE = 0x%08X\n\r", *((volatile int *)(uBaseAddr+XAXIVDMA_MM2S_ADDR_OFFSET+XAXIVDMA_VSIZE_OFFSET)) );
xil_printf( "\t ----------------\n\r" );
xil_printf( "\t S2MM_HSIZE_STATUS= 0x%08X\n\r", *((volatile int *)(uBaseAddr+0xF0 )) );
xil_printf( "\t S2MM_VSIZE_STATUS= 0x%08X\n\r", *((volatile int *)(uBaseAddr+0xF4 )) );
xil_printf( "\t ----------------\n\r" );
return 0;
}
int vfb_check_errors(XAxiVdma *pAxiVdma, u8 bClearErrors )
{
u32 uBaseAddr = pAxiVdma->BaseAddr;
Xuint32 inErrors;
Xuint32 outErrors;
Xuint32 Errors;
// Get Status of Error Flags
//inErrors = XAxiVdma_GetStatus(pAxiVdma, XAXIVDMA_WRITE) & XAXIVDMA_SR_ERR_ALL_MASK;
//outErrors = XAxiVdma_GetStatus(pAxiVdma, XAXIVDMA_READ) & XAXIVDMA_SR_ERR_ALL_MASK;
inErrors = *((volatile int *)(uBaseAddr+XAXIVDMA_RX_OFFSET+XAXIVDMA_SR_OFFSET )) & 0x0000CFF0;
outErrors = *((volatile int *)(uBaseAddr+XAXIVDMA_TX_OFFSET+XAXIVDMA_SR_OFFSET )) & 0x000046F0;
xil_printf( "AXI_VDMA - Checking Error Flags\n\r" );
Errors = (inErrors << 16) | (outErrors);
if ( Errors )
{
if ( inErrors & 0x00004000 )
{
xil_printf( "\tS2MM_DMASR - ErrIrq\n\r" );
}
if ( inErrors & 0x00008000 )
{
xil_printf( "\tS2MM_DMASR - EOLLateErr\n\r" );
}
if ( inErrors & 0x00000800 )
{
xil_printf( "\tS2MM_DMASR - SOFLateErr\n\r" );
}
if ( inErrors & 0x00000400 )
{
xil_printf( "\tS2MM_DMASR - SGDecErr\n\r" );
}
if ( inErrors & 0x00000200 )
{
xil_printf( "\tS2MM_DMASR - SGSlvErr\n\r" );
}
if ( inErrors & 0x00000100 )
{
xil_printf( "\tS2MM_DMASR - EOLEarlyErr\n\r" );
}
if ( inErrors & 0x00000080 )
{
xil_printf( "\tS2MM_DMASR - SOFEarlyErr\n\r" );
}
if ( inErrors & 0x00000040 )
{
xil_printf( "\tS2MM_DMASR - DMADecErr\n\r" );
}
if ( inErrors & 0x00000020 )
{
xil_printf( "\tS2MM_DMASR - DMASlvErr\n\r" );
}
if ( inErrors & 0x00000010 )
{
xil_printf( "\tS2MM_DMASR - DMAIntErr\n\r" );
}
if ( outErrors & 0x00004000 )
{
xil_printf( "\tMM2S_DMASR - ErrIrq\n\r" );
}
if ( outErrors & 0x00000400 )
{
xil_printf( "\tMM2S_DMASR - SGDecErr\n\r" );
}
if ( outErrors & 0x00000200 )
{
xil_printf( "\tMM2S_DMASR - SGSlvErr\n\r" );
}
if ( outErrors & 0x00000080 )
{
xil_printf( "\tMM2S_DMASR - SOFEarlyErr\n\r" );
}
if ( outErrors & 0x00000040 )
{
xil_printf( "\tMM2S_DMASR - DMADecErr\n\r" );
}
if ( outErrors & 0x00000020 )
{
xil_printf( "\tMM2S_DMASR - DMASlvErr\n\r" );
}
if ( outErrors & 0x00000010 )
{
xil_printf( "\tMM2S_DMASR - DMAIntErr\n\r" );
}
// Clear error flags
xil_printf( "AXI_VDMA - Clearing Error Flags\n\r" );
*((volatile int *)(uBaseAddr+XAXIVDMA_RX_OFFSET+XAXIVDMA_SR_OFFSET )) = 0x0000CFF0; //XAXIVDMA_SR_ERR_ALL_MASK;
*((volatile int *)(uBaseAddr+XAXIVDMA_TX_OFFSET+XAXIVDMA_SR_OFFSET )) = 0x000046F0; //XAXIVDMA_SR_ERR_ALL_MASK;
}
return Errors;
}