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

Subversion Repositories usb_fpga_2_14

[/] [usb_fpga_2_14/] [trunk/] [fx3/] [ztex-ufm-2_02.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ZTEX
/*%
2
   ZTEX Firmware Kit for EZ-USB FX3 Microcontrollers
3
   Copyright (C) 2009-2017 ZTEX GmbH.
4
   http://www.ztex.de
5
 
6
   This Source Code Form is subject to the terms of the Mozilla Public
7
   License, v. 2.0. If a copy of the MPL was not distributed with this file,
8
   You can obtain one at http://mozilla.org/MPL/2.0/.
9
 
10
   Alternatively, the contents of this file may be used under the terms
11
   of the GNU General Public License Version 3, as described below:
12
 
13
   This program is free software; you can redistribute it and/or modify
14
   it under the terms of the GNU General Public License version 3 as
15
   published by the Free Software Foundation.
16
 
17
   This program is distributed in the hope that it will be useful, but
18
   WITHOUT ANY WARRANTY; without even the implied warranty of
19
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20
   General Public License for more details.
21
 
22
   You should have received a copy of the GNU General Public License
23
   along with this program; if not, see http://www.gnu.org/licenses/.
24
%*/
25
/*
26
   Board specific functions for prototype board ZTEX-USB FPGA Module 2.02
27
*/
28
 
29
#ifndef _ZTEX_CONF_UFM_2_02_C1_
30
#define _ZTEX_CONF_UFM_2_02_C1_
31
 
32
#include "cyu3pib.h"
33
 
34
/*
35
   This macro defines the Product string. Limited to 31 characters.
36
*/
37
#undef ZTEX_PRODUCT_STRING
38
#define ZTEX_PRODUCT_STRING "ZTEX USB-FPGA Module 2.02b"
39
 
40
// GPIO's
41
#define ZTEX_GPIO_MODE0 46
42
#define ZTEX_GPIO_MODE1 48
43
 
44
#define ZTEX_GPIO_LED   47
45
 
46
#define ZTEX_GPIO_FPGA_RESET 41
47
#define ZTEX_GPIO_FPGA_DONE 45
48
#define ZTEX_GPIO_FPGA_INIT_B 43
49
#define ZTEX_GPIO_FPGA_RDWR_B 44
50
 
51
#undef ZTEX_GPIO_SIMPLE_BITMAP0
52
#undef ZTEX_GPIO_SIMPLE_BITMAP1
53
#define ZTEX_GPIO_SIMPLE_BITMAP0 0
54
#define ZTEX_GPIO_SIMPLE_BITMAP1 ( 1 << (ZTEX_GPIO_MODE0-32) | 1 << (ZTEX_GPIO_MODE1-32) | 1 << (ZTEX_GPIO_LED-32) \
55
        | 1 << (ZTEX_GPIO_FPGA_RESET-32) | 1 << (ZTEX_GPIO_FPGA_DONE-32) | 1 << (ZTEX_GPIO_FPGA_INIT_B-32) | 1 << (ZTEX_GPIO_FPGA_RDWR_B-32) \
56
    )
57
 
58
#define ZTEX_FPGA_CONFIGURED ( ztex_gpio_get(ZTEX_GPIO_FPGA_DONE) )
59
 
60
#define _ZTEX_BOARD_
61
#define _ZTEX_FPGA_
62
 
63
void ztex_disable_flash();
64
 
65
#endif // _ZTEX_CONF_UFM_2_02_C1_
66
 
67
#ifdef _ZTEX_INCLUDE_2_
68
#ifndef _ZTEX_CONF_UFM_2_02_C2_
69
#define _ZTEX_CONF_UFM_2_02_C2_
70
 
71
#include "ztex-fpgaconf1.c"
72
 
73
void (*ztex_fpga_config_start_app)() = 0;                // called before FPGA configuration
74
void (*ztex_fpga_config_done_app)() = 0;         // called after FPGA configuration
75
 
76
uint8_t ztex_fpga_cs = 0;                                // check sum
77
uint32_t ztex_fpga_bytes = 0;                            // transferred bytes
78
uint8_t ztex_fpga_init_b = 0;                            // init b 
79
 
80
uint8_t ztex_fpga_config_started = 0;
81
 
82
void ztex_disable_flash() {
83
    ztex_gpio_set(ZTEX_GPIO_MODE1, CyFalse);
84
    ztex_gpio_set(ZTEX_GPIO_MODE0, CyTrue);
85
    ztex_gpio_set(ZTEX_GPIO_MODE1, CyTrue);
86
    ztex_gpio_set(ZTEX_GPIO_MODE0, CyFalse);
87
}
88
 
89
void ztex_enable_flash() {
90
    ztex_gpio_set(ZTEX_GPIO_MODE1, CyFalse);
91
    ztex_gpio_set(ZTEX_GPIO_MODE0, CyFalse);
92
}
93
 
94
CyBool_t ztex_fpga_configured() {
95
    return ztex_gpio_get(ZTEX_GPIO_FPGA_DONE);
96
}
97
 
98
void ztex_fpga_reset() {
99
    if ( ztex_fpga_config_start_app != 0 ) ztex_fpga_config_start_app();
100
 
101
    ztex_gpio_set(ZTEX_GPIO_FPGA_RESET, CyFalse);
102
 
103
    ztex_gpio_set_input(ZTEX_GPIO_FPGA_INIT_B);         CyU3PGpioSetIoMode(ZTEX_GPIO_FPGA_INIT_B, CY_U3P_GPIO_IO_MODE_WPU);
104
    ztex_gpio_set_output(ZTEX_GPIO_FPGA_RDWR_B, CyFalse);
105
 
106
    CyU3PThreadSleep (20);
107
    ztex_gpio_set(ZTEX_GPIO_FPGA_RESET, CyTrue);
108
}
109
 
110
void ztex_fpga_config_start() {
111
    if ( ztex_fpga_config_started ) return;
112
    ztex_fpga_config_started = 1;
113
 
114
    ztex_gpio_set(ZTEX_GPIO_FPGA_RESET, CyFalse);
115
 
116
    // start gpif
117
    ztex_fpgaconf1_start();
118
 
119
    ztex_fpga_reset();
120
 
121
    uint8_t i = 0;
122
    while ( (!ztex_gpio_get(ZTEX_GPIO_FPGA_INIT_B)) && i<255 ) {
123
        CyU3PThreadSleep (1);
124
        i++;
125
    }
126
 
127
    ztex_fpga_init_b = ztex_gpio_get(ZTEX_GPIO_FPGA_INIT_B) ? 200 : 100;
128
    ztex_fpga_cs = 0;
129
    ztex_fpga_bytes = 0;
130
}
131
 
132
void ztex_fpga_config_done() {
133
    ztex_fpga_init_b += ztex_gpio_get(ZTEX_GPIO_FPGA_INIT_B) ? 22 : 11;
134
 
135
    ztex_fpgaconf1_send(ztex_ep0buf,16);
136
 
137
    if ( ZTEX_FPGA_CONFIGURED ) {
138
        CyU3PGpioSetIoMode(ZTEX_GPIO_FPGA_INIT_B, CY_U3P_GPIO_IO_MODE_NONE);
139
        ztex_gpio_set_input(ZTEX_GPIO_FPGA_RDWR_B);
140
        if ( ztex_fpga_config_done_app != 0 ) ztex_fpga_config_done_app();
141
    }
142
 
143
    // stop gpif
144
    ztex_fpgaconf1_stop();
145
    ztex_gpio_set(ZTEX_GPIO_LED, !ZTEX_FPGA_CONFIGURED);
146
    ztex_fpga_config_started = 0;
147
}
148
 
149
 
150
// VR 0x30
151
uint8_t vr_fpga_info(uint16_t value, uint16_t index, uint16_t length ) {
152
    ztex_ep0buf[0] = ZTEX_FPGA_CONFIGURED ? 0 : 1;
153
    ztex_ep0buf[1] = ztex_fpga_cs;
154
    ztex_ep0buf[2] = ztex_fpga_bytes;
155
    ztex_ep0buf[3] = ztex_fpga_bytes >> 8;
156
    ztex_ep0buf[4] = ztex_fpga_bytes >> 16;
157
    ztex_ep0buf[5] = ztex_fpga_bytes >> 24;
158
    ztex_ep0buf[6] = ztex_fpga_init_b;
159
    ztex_ep0buf[7] = 0;         // flash configuration result
160
    ztex_ep0buf[8] = 0;          // bit order = not swapped
161
    ZTEX_REC_RET( CyU3PUsbSendEP0Data( 9, ztex_ep0buf ) );
162
    return 0;
163
}
164
 
165
// VC 0x31
166
uint8_t vc_fpga_reset(uint16_t value, uint16_t index, uint16_t length ) {
167
    ztex_fpga_reset();
168
    CyU3PUsbAckSetup();
169
    return 0;
170
}
171
 
172
// VC 0x32
173
uint8_t vc_fpga_send(uint16_t value, uint16_t index, uint16_t length ) {
174
    ztex_fpga_config_start();
175
 
176
    if ( length > 0 ) {
177
        ZTEX_REC_RET ( CyU3PUsbGetEP0Data (length, ztex_ep0buf, NULL) );
178
        for (uint16_t i = 0;  i<length; i++)
179
            ztex_fpga_cs += ztex_ep0buf[i];
180
        ztex_fpga_bytes += length;
181
        ztex_fpgaconf1_send(ztex_ep0buf,length);
182
    }
183
    else {
184
        CyU3PUsbAckSetup();
185
    }
186
 
187
    if ( length == 0 || ((length & 63) != 0)  ) ztex_fpga_config_done();
188
    return 0;
189
}
190
 
191
void ztex_board_init() {
192
    ztex_disable_flash_boot = ztex_disable_flash;
193
    ztex_fpga_config_started = 0;
194
 
195
    ztex_gpio_set_output(ZTEX_GPIO_LED, CyTrue);
196
    ztex_gpio_set_output(ZTEX_GPIO_MODE0, CyFalse);
197
    ztex_gpio_set_output(ZTEX_GPIO_MODE1, CyFalse);
198
 
199
    ztex_gpio_set_input(ZTEX_GPIO_FPGA_DONE);
200
    ztex_gpio_set_output(ZTEX_GPIO_FPGA_RESET, CyTrue);
201
 
202
    ztex_register_vendor_req(0x30, vr_fpga_info);
203
    ztex_register_vendor_cmd(0x31, vc_fpga_reset);
204
    ztex_register_vendor_cmd(0x32, vc_fpga_send);
205
 
206
    ztex_gpio_set(ZTEX_GPIO_LED, !ZTEX_FPGA_CONFIGURED);
207
}
208
 
209
 
210
void ztex_board_stop() {
211
    if ( ztex_fpga_config_started ) {   // USB is stopped during configuration
212
        ztex_fpgaconf1_stop();
213
        ztex_fpga_reset();
214
        ztex_fpga_config_started = 0;
215
    }
216
}
217
 
218
#endif // _ZTEX_CONF_UFM_2_02_C2_
219
#endif // _ZTEX_INCLUDE_2_

powered by: WebSVN 2.1.0

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