OpenCores
URL https://opencores.org/ocsvn/vspi/vspi/trunk

Subversion Repositories vspi

[/] [vspi/] [trunk/] [projnav/] [xps/] [SDK/] [SDK_Workspace/] [demo/] [src/] [main.cc] - Blame information for rev 14

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 14 mjlyons
/*
2
 * Empty C++ Application
3
 */
4
 
5
#include <stdio.h>
6
 
7
#include "xil_types.h"
8
#include "xparameters.h"
9
#include "xdmacentral.h"
10
 
11
#define DMA_DEVICE_ID                   XPAR_DMACENTRAL_0_DEVICE_ID
12
 
13
#define DMA_BUFFER_BYTE_SIZE    4096
14
 
15
static XDmaCentral Dma;
16
 
17
u32 * pSpiifcBase = (u32 *)0x85000000;
18
u32 * pMosiBase   = (u32 *)0x85010000;
19
u32 * pMisoBase   = (u32 *)0x85011000;
20
 
21
//
22
// Initializes DMA controller
23
//
24
int InitDma();
25
 
26
//
27
// Copies a block of data between two burst-mode enabled memory regions. Uses
28
// XPS Central DMA controller. Function spins while waiting for copy to complete.
29
//
30
int DmaCopy(void * pSrc, void * pDest, size_t byteCount);
31
 
32
//
33
// Writes to the three memmap regions in the Spiifc peripheral
34
// (regs, mosi/miso buffers). Verifies the writes stuck using a read.
35
//
36
void SpiifcPioTest();
37
 
38
//
39
// DMA copies from mosi to miso buffers and verifies result using PIO.
40
//
41
void SpiifcDmaTest();
42
 
43
//
44
// InitDma - Initializes DMA controller
45
//
46
int InitDma()
47
{
48
        XDmaCentral_Config *pDmaCfg;
49
        int status;
50
 
51
        // Configure DMA controller
52
        pDmaCfg = XDmaCentral_LookupConfig(DMA_DEVICE_ID);
53
        if (NULL == pDmaCfg) { return XST_FAILURE; }
54
        status = XDmaCentral_CfgInitialize(&Dma, pDmaCfg, pDmaCfg->BaseAddress);
55
        if (XST_SUCCESS != status) { return status; }
56
 
57
        // Reset DMAC
58
        XDmaCentral_Reset(&Dma);
59
 
60
        // Setup DMAC control register to increment src & dest addr
61
        XDmaCentral_SetControl(
62
                        &Dma,
63
                        XDMC_DMACR_SOURCE_INCR_MASK | XDMC_DMACR_DEST_INCR_MASK);
64
 
65
        // DMAC does not raise interrupts (when transfer completes)
66
        XDmaCentral_InterruptEnableSet(&Dma, 0);
67
 
68
        return XST_SUCCESS;
69
}
70
 
71
int DmaCopy(void * pSrc, void * pDest, size_t byteCount)
72
{
73
        u32 regValue;
74
        XDmaCentral_Transfer(&Dma, pSrc, pDest, byteCount);
75
        do {    // Wait for DMA transfer to complete
76
                regValue = XDmaCentral_GetStatus(&Dma);
77
        } while ((regValue & XDMC_DMASR_BUSY_MASK) == XDMC_DMASR_BUSY_MASK);
78
        if (regValue & XDMC_DMASR_BUS_ERROR_MASK) {
79
                xil_printf("DMA_BUS_ERROR\n");
80
                return XST_FAILURE;
81
        }
82
 
83
        return XST_SUCCESS;
84
}
85
 
86
int main()
87
{
88
        int status, i;
89
 
90
        // Initialize system
91
        if (XST_SUCCESS != (status = InitDma())) {
92
                xil_printf("[FAIL] InitDma() failed. Exiting.\n");
93
                return status;
94
        }
95
 
96
        // Perform Programmed IO tests
97
        SpiifcPioTest();
98
 
99
        // Test DMA
100
        SpiifcDmaTest();
101
 
102
        // Spiifc loopback: anything sent to spiifc is sent back
103
        while (1) {
104
 
105
//              //
106
//              // Print values of all SPI registers
107
//              //
108
//              for (i = 0; i < 16; i++) {
109
//                      xil_printf("Reg%d=0x%08x\n", i, pSpiifcBase[i]);
110
//              }
111
 
112
                // Wait for M->S transfer to complete
113
                //xil_printf("Waiting for M->S transfer to complete..\n");
114
                while(0 == (pSpiifcBase[0] & 0x1)) { ; }
115
                pSpiifcBase[0] &= (~0x1);                // Handling MOSI transfer completion
116
 
117
                // Initiating DMA transfer
118
                //xil_printf("DMA MOSI buffer to MISO buffer\n");
119
                DmaCopy(pMosiBase, pMisoBase, 4096);
120
 
121
                // DMA done, set flag for master
122
                //xil_printf("DMA completed\n");
123
                pSpiifcBase[0] |= 0x2;
124
 
125
        }
126
 
127
        /*
128
        int i = 0;
129
        for (i = 0; i < 40000000; i++) { ; }
130
        for (i = 0; i < 1024; i++) {
131
                xil_printf("pMOSI[i] = 0x%08X\n", pMosiBase[i]);
132
        }
133
        */
134
}
135
 
136
void SpiifcPioTest()
137
{
138
        int i = 0;
139
 
140
        xil_printf("Testing Spiifc PIO...\n");
141
 
142
        // PIO Write to Spiifc memmap regions
143
        pSpiifcBase[0] = 0x87654321;
144
    pMosiBase[0] = 0x12345678;
145
    pMisoBase[0] = 0xFEEDFACE;
146
    pMisoBase[1] = 0xBEEFBABE;
147
    pMisoBase[2] = 0xBEEFBEEF;
148
 
149
        xil_printf("Reg0 @ 0x%08X: verifying PIO... ", pSpiifcBase);
150
        if (0x87654321 == *pSpiifcBase) {
151
                xil_printf("[PASS]\n");
152
        } else {
153
                xil_printf("[FAIL] (actual=0x%08X)\n", *pSpiifcBase);
154
        }
155
 
156
        xil_printf("MOSI @ 0x%08X: verifying PIO... ", pMosiBase);
157
    if (0x12345678 == *pMosiBase) {
158
        xil_printf("[PASS]\n");
159
    } else {
160
        xil_printf("[FAIL] (actual=0x%08X)\n", *pMosiBase);
161
    }
162
 
163
    xil_printf("MISO @ 0x%08X: verifying PIO... ", pMisoBase);
164
    if (0xFEEDFACE == *pMisoBase) {
165
        xil_printf("[PASS]\n");
166
    } else {
167
        xil_printf("[FAIL] (actual=0x%08X)\n", *pMisoBase);
168
    }
169
 
170
        for (i = 0; i < 16; i++) {
171
                pSpiifcBase[i] = (i << 24) | (i << 16) | (i << 8) | i;
172
        }
173
        for (i = 0; i < 16; i++) {
174
                xil_printf("Reg%d=0x%08x\n", i, pSpiifcBase[i]);
175
        }
176
 
177
    xil_printf("\n");
178
}
179
 
180
void SpiifcDmaTest()
181
{
182
        int status;
183
        u32 i;
184
 
185
        // Pattern DMA memory buffer
186
        for(i = 0; i < DMA_BUFFER_BYTE_SIZE/4; i++) {
187
 
188
                pMosiBase[i] = ((i*4+3) & 0xFF) << 24 |
189
                                       ((i*4+2) & 0xFF) << 16 |
190
                                       ((i*4+1) & 0xFF) << 8  |
191
                                       ((i*4+0) & 0xFF);
192
 
193
                //pMosiBase[i] = 0xAABBCCDD;
194
                //xil_printf("0x%08X\n", pMosiBase[i]);
195
        }
196
 
197
        // DMA buffer to Spiifc.MISO buffer
198
        if (XST_SUCCESS != (status =
199
                        DmaCopy(pMosiBase, pMisoBase, DMA_BUFFER_BYTE_SIZE))) {
200
                xil_printf("[FAIL] DmaCopy() failed. Exiting.\n");
201
        }
202
 
203
        // Check DMAC copied Spiifc.MOSI --> Spiifc.MISO buffer
204
        u32 expectedDmaWord = 0;
205
        int failWords = 0;
206
        for (i = 0; i < (DMA_BUFFER_BYTE_SIZE/4); i++) {
207
                expectedDmaWord = ((i*4+3) & 0xFF) << 24 |
208
                                          ((i*4+2) & 0xFF) << 16 |
209
                                          ((i*4+1) & 0xFF) << 8  |
210
                                          ((i*4+0) & 0xFF) << 0;
211
 
212
                if (pMisoBase[i] != expectedDmaWord) {
213
                        xil_printf(
214
                                        "[FAIL] DMA mem word [i]: expected=0x%08X, actual=0x%08X\n",
215
                                        expectedDmaWord, pMisoBase[i]);
216
                }
217
        }
218
        if (0 == failWords) {
219
                xil_printf("[PASS] DMA transfer from MOSI to MISO memory\n");
220
        }
221
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.