OpenCores
URL https://opencores.org/ocsvn/usb_ft232h_avalon-mm_interface/usb_ft232h_avalon-mm_interface/trunk

Subversion Repositories usb_ft232h_avalon-mm_interface

[/] [usb_ft232h_avalon-mm_interface/] [trunk/] [testbench/] [nios/] [main.cpp] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 melman701
 
2
#include <stdio.h>
3
#include <unistd.h>
4
#include <io.h>
5
 
6
#include "system.h"
7
#include "altera_avalon_pio_regs.h"
8
#include "sys/alt_dma.h"
9
#include "altera_avalon_dma_regs.h"
10
#include "altera_avalon_sgdma.h"
11
 
12
#include "types.h"
13
 
14
 
15
alt_dma_txchan txchan;
16
alt_dma_rxchan rxchan;
17
alt_sgdma_dev *rxdma;
18
alt_sgdma_descriptor rxdesc;
19
alt_sgdma_descriptor rxdescNext;
20
 
21
volatile int transmit_done = 0;
22
volatile int receive_done = 0;
23
volatile uint32_t total_send = 0;
24
volatile uint32_t total_get = 0;
25
volatile uint32_t index = 0;
26
 
27
volatile uint8_t ubuff[40000];
28
volatile uint32_t ubuffCount = 0;
29
volatile uint32_t ubuffTail = 0;
30
volatile uint32_t ubuffHead = 0;
31
 
32
volatile alt_u8 dat = 0;
33
volatile int rxErrors = 0;
34
 
35
enum eState{
36
        IDLE,
37
        RECEIVE,
38
        TRANSMIT
39
};
40
volatile eState state = IDLE;
41
 
42
 
43
void callback_dma_tx(void* handle) {
44
        transmit_done++;
45
        total_send += (int)handle;
46
        index += (int)handle;
47
        if(index >= 32768)
48
                index = 0;
49
}
50
 
51
void callback_dma_rx(void* handle, void* data) {
52
        receive_done++;
53
        total_get += (int)handle;
54
}
55
 
56
int main(void)
57
{
58
        printf("Started...\n");
59
 
60
        uint16_t count = 0;
61
        uint16_t free = 0;
62
        int error = 0;
63
 
64
        /*for(int i=0; i<sizeof(ubuff); i++) {
65
                ubuff[i] = i & 0xFF;
66
        }*/
67
 
68
        uint8_t txbuff[32768];
69
        for(int i=0; i<sizeof(txbuff); i++) {
70
                txbuff[i] = i & 0xFF;
71
        }
72
 
73
        txchan = alt_dma_txchan_open(USB_DMA_TX_NAME);
74
        if(txchan == NULL) {
75
                puts("TXCHAN is NULL");
76
                return -2;
77
        } else {
78
                puts("txchan is OK");
79
//              IOWR_ALTERA_AVALON_DMA_CONTROL(USB_DMA_TX_BASE, (1<<ALTERA_AVALON_DMA_CONTROL_WCON_OFST));
80
                alt_dma_txchan_ioctl(txchan, ALT_DMA_TX_ONLY_ON, 0x0);
81
                alt_dma_txchan_ioctl(txchan, ALT_DMA_SET_MODE_8, NULL);
82
        }
83
 
84
        /*rxchan = alt_dma_rxchan_open(USB_DMA_RX_NAME);
85
        if(rxchan == NULL) {
86
                puts("RXCHAN is NULL");
87
                return -2;
88
        } else {
89
                puts("rxchan is OK");
90
//              IOWR_ALTERA_AVALON_DMA_CONTROL(USB_DMA_RX_BASE, (1<<ALTERA_AVALON_DMA_CONTROL_RCON_OFST));
91
                alt_dma_rxchan_ioctl(rxchan, ALT_DMA_RX_ONLY_ON, (void*)0x1);
92
                alt_dma_rxchan_ioctl(rxchan, ALT_DMA_SET_MODE_8, NULL);
93
        }*/
94
 
95
        rxdma = alt_avalon_sgdma_open(USB_SGDMA_RX_NAME);
96
        if(rxdma == NULL) {
97
                puts("Rx DMA is NULL");
98
                return - 3;
99
        } else {
100
                puts("Rx DMA is OK");
101
        }
102
 
103
        ubuffHead = 0;
104
        ubuffTail = 0;
105
        ubuffCount = 0;
106
 
107
        int divider = 0;
108
        alt_u8 led = 0;
109
 
110
        while (true)
111
        {
112
                divider++;
113
                if(divider == 100000) {
114
                        if(led & 0x1)
115
                                led = 0x0;
116
                        else
117
                                led = 0x1;
118
                        IOWR_ALTERA_AVALON_PIO_DATA(LED_BASE, led);
119
                        divider = 0;
120
                        printf("%d\/%d tx=%d rx=%d err=%d\n", total_get, total_send, transmit_done, receive_done, rxErrors);
121
                }
122
 
123
                switch(state) {
124
                case RECEIVE:
125
                        if((receive_done == 0) && (transmit_done == 0) && (total_send < 524288)) {
126
//                              state = TRANSMIT;
127
                                count = IORD_8DIRECT(FT232H_BASE, 0x2) | (IORD_8DIRECT(FT232H_BASE, 0x3) << 8);
128
                                if(count & (1<<15)) {
129
                                        free = 1024 - (count & 0x7FFF);
130
//                                      if(free > ubuffCount) free = ubuffCount;
131
                                        if(free > (32768-index)) free = 32768-index;
132
                                        if(free) {
133
                                                /*for(int i=0; i<free; i++)
134
                                                        IOWR_8DIRECT(FT232H_BASE, 0x0, txbuff[total_send+i]);
135
                                                total_send += free;*/
136
 
137
                                                if(alt_dma_txchan_send(txchan, (void*)&txbuff[index], free, callback_dma_tx, (void*)free) == 0){
138
                                                        transmit_done--;
139
//                                                      ubuffCount -= free;
140
//                                                      ubuffHead += free;
141
//                                                      state = TRANSMIT;
142
                                                }
143
                                        }
144
                                }
145
                        }
146
                        state = TRANSMIT;
147
                        break;
148
                case TRANSMIT:
149
                        if(transmit_done == 0) {
150
//                              printf("get %d send %d count %d\n", ubuffTail, ubuffHead, ubuffCount);
151
                                state = IDLE;
152
                        }
153
                        break;
154
                default:
155
                        if((receive_done == 0) && (transmit_done == 0)) {
156
                        count = IORD_8DIRECT(FT232H_BASE, 0x4) | (IORD_8DIRECT(FT232H_BASE, 0x5) << 8);
157
                        if(count & (1<<15)) {
158
                                count &= 0x7FFF;
159
                                if(count) {
160
                                        alt_avalon_sgdma_construct_mem_to_mem_desc(&rxdesc, &rxdescNext,
161
                                                        (alt_u32*)0x1, (alt_u32*)&ubuff[ubuffTail], count, 1, 0);
162
                                        error = alt_avalon_sgdma_do_sync_transfer(rxdma, &rxdesc);
163
                                        if(error == 0x0C) {
164
                                                total_get += count;
165
                                                for(int i=0; i<count; i++) {
166
                                                        if(ubuff[i] != dat) {
167
                                                                rxErrors++;
168
                                                                dat = ubuff[i];
169
                                                        } else {
170
                                                                dat++;
171
                                                        }
172
                                                }
173
                                        } else {
174
                                                puts("Get data FAILED");
175
                                        }
176
 
177
                                        /*error = alt_dma_rxchan_prepare(rxchan, (void*)&ubuff[ubuffTail], count, callback_dma_rx, (void*)count);
178
                                        if(error == 0) {
179
//                                              ubuffTail += count;
180
//                                              ubuffCount += count;
181
                                                receive_done--;
182
//                                              state = RECEIVE;
183
                                        }*/
184
                                }
185
                        }
186
                        state = RECEIVE;
187
                        }
188
                        break;
189
                }
190
        }
191
 
192
        return 0;
193
}

powered by: WebSVN 2.1.0

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