1 |
2 |
ZTEX |
/*%
|
2 |
|
|
fx3demo -- Demonstrates common features of the FX3
|
3 |
|
|
Copyright (C) 2009-2017 ZTEX GmbH.
|
4 |
|
|
http://www.ztex.de
|
5 |
|
|
|
6 |
|
|
Licensed under the Apache License, Version 2.0 (the "License");
|
7 |
|
|
you may not use this file except in compliance with the License.
|
8 |
|
|
You may obtain a copy of the License at
|
9 |
|
|
|
10 |
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
11 |
|
|
|
12 |
|
|
Unless required by applicable law or agreed to in writing, software
|
13 |
|
|
distributed under the License is distributed on an "AS IS" BASIS,
|
14 |
|
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
15 |
|
|
See the License for the specific language governing permissions and
|
16 |
|
|
limitations under the License.
|
17 |
|
|
%*/
|
18 |
|
|
#include "cyu3system.h"
|
19 |
|
|
#include "cyu3os.h"
|
20 |
|
|
#include "cyu3dma.h"
|
21 |
|
|
#include "math.h"
|
22 |
|
|
|
23 |
|
|
// loads default configuration macros
|
24 |
|
|
#include "ztex-conf.c"
|
25 |
|
|
|
26 |
|
|
CyU3PDmaChannel dma_out_handle, dma_in_handle, dma_st_out_handle;
|
27 |
|
|
|
28 |
|
|
/*
|
29 |
|
|
Define endpoints 1, 2 and 4 which belong to interface 0 (in/out are seen from the host)
|
30 |
|
|
EP 1 is used for the speed test, EP 2 and 4 for the uppercase conversion.
|
31 |
|
|
Bulk size is 1 for EP 2 and 4 and 16 for EP 1.
|
32 |
|
|
DMA buffer size if 2x1K for EP 2 and 4 and 2x32K for EP 1
|
33 |
|
|
*/
|
34 |
|
|
#undef EP_SETUP
|
35 |
|
|
#define EP_SETUP \
|
36 |
|
|
INTERFACE(0, \
|
37 |
|
|
EP_BULK(4, OUT, 1, /* direction as seen from the host */ \
|
38 |
|
|
DMA(dma_in_handle, CY_U3P_DMA_TYPE_MANUAL_IN, 1, 2, CY_U3P_CPU_SOCKET_CONS, /* direction as seen from device */ \
|
39 |
|
|
CB(0,0) \
|
40 |
|
|
) \
|
41 |
|
|
) \
|
42 |
|
|
EP_BULK(2, IN, 1, \
|
43 |
|
|
DMA(dma_out_handle, CY_U3P_DMA_TYPE_MANUAL_OUT, 1, 2, CY_U3P_CPU_SOCKET_PROD, ) \
|
44 |
|
|
) \
|
45 |
|
|
EP_BULK(1, IN, 16, \
|
46 |
|
|
DMA(dma_st_out_handle, CY_U3P_DMA_TYPE_MANUAL_OUT, 32, 2, CY_U3P_CPU_SOCKET_PROD, ) \
|
47 |
|
|
) \
|
48 |
|
|
)
|
49 |
|
|
|
50 |
|
|
#undef ZTEX_PRODUCT_STRING
|
51 |
|
|
#define ZTEX_PRODUCT_STRING "Demo for FX3 Boards"
|
52 |
|
|
|
53 |
|
|
#include "ztex.c"
|
54 |
|
|
|
55 |
|
|
void usb_start() {
|
56 |
|
|
// start USB transfers as soon cable is connected
|
57 |
|
|
ZTEX_REC(CyU3PDmaChannelSetXfer (&dma_in_handle, 0));
|
58 |
|
|
ZTEX_REC(CyU3PDmaChannelSetXfer (&dma_out_handle, 0));
|
59 |
|
|
ZTEX_REC(CyU3PDmaChannelSetXfer (&dma_st_out_handle, 0));
|
60 |
|
|
}
|
61 |
|
|
|
62 |
|
|
void usb_stop() {
|
63 |
|
|
// nothing required here
|
64 |
|
|
}
|
65 |
|
|
|
66 |
|
|
void run () {
|
67 |
|
|
CyU3PDmaBuffer_t inbuf, outbuf;
|
68 |
|
|
CyU3PReturnStatus_t status;
|
69 |
|
|
uint32_t i;
|
70 |
|
|
|
71 |
|
|
ztex_log ( "Starting FX3 Demo" );
|
72 |
|
|
|
73 |
|
|
while (1) {
|
74 |
|
|
if (ztex_usb_is_connected) {
|
75 |
|
|
/* Check for free buffer on the speed test EP. The call will fail if there was
|
76 |
|
|
* an error or if the USB connection was reset / disconnected. In case of error,
|
77 |
|
|
* invoke the error handler and in case of reset / disconnection, continue to
|
78 |
|
|
* beginning of the loop. If no free buffer is available timeout error occurs
|
79 |
|
|
* and firmware continues with the upper case conversion test */
|
80 |
|
|
status = CyU3PDmaChannelGetBuffer (&dma_st_out_handle, &outbuf, 50);
|
81 |
|
|
if ( !ztex_usb_is_connected ) continue;
|
82 |
|
|
if ( status != CY_U3P_ERROR_TIMEOUT ) {
|
83 |
|
|
ZTEX_REC_CONT(status);
|
84 |
|
|
// Commit the buffer to the speed test EP
|
85 |
|
|
status = CyU3PDmaChannelCommitBuffer (&dma_st_out_handle, outbuf.size, 0);
|
86 |
|
|
//ZTEX_LOG("EC=%d, ST: %d bytes", status, outbuf.size);
|
87 |
|
|
if (!ztex_usb_is_connected) continue;
|
88 |
|
|
ZTEX_REC(status);
|
89 |
|
|
}
|
90 |
|
|
else {
|
91 |
|
|
/* Uppercase conversion test: Wait for receiving a buffer from the producer socket
|
92 |
|
|
* (OUT endpoint). */
|
93 |
|
|
status = CyU3PDmaChannelGetBuffer (&dma_in_handle, &inbuf, CYU3P_NO_WAIT );
|
94 |
|
|
// ZTEX_LOG("EC=%d, Read %d bytes", status, inbuf.count);
|
95 |
|
|
if (!ztex_usb_is_connected || status==CY_U3P_ERROR_TIMEOUT) continue;
|
96 |
|
|
ZTEX_REC_CONT(status);
|
97 |
|
|
|
98 |
|
|
/* Wait for a free buffer to transmit the received data. */
|
99 |
|
|
status = CyU3PDmaChannelGetBuffer (&dma_out_handle, &outbuf, CYU3P_WAIT_FOREVER);
|
100 |
|
|
if (!ztex_usb_is_connected || status==CY_U3P_ERROR_TIMEOUT) continue;
|
101 |
|
|
ZTEX_REC(status);
|
102 |
|
|
|
103 |
|
|
/* Convert the data from the producer channel to the consumer channel.
|
104 |
|
|
* The inbuf.count holds the amount of valid data received. */
|
105 |
|
|
CyU3PMemCopy (outbuf.buffer, inbuf.buffer, inbuf.count);
|
106 |
|
|
for (i=0; i<inbuf.count; i++ )
|
107 |
|
|
outbuf.buffer[i] = inbuf.buffer[i]>='a' && inbuf.buffer[i]<='z' ? inbuf.buffer[i] - 32 : inbuf.buffer[i];
|
108 |
|
|
i = round(sqrt(inbuf.count)*10.0);
|
109 |
|
|
ZTEX_LOG("Info: Converted %d.%d^2 bytes", i/10, i%10);
|
110 |
|
|
|
111 |
|
|
/* Now discard the data from the producer channel so that the buffer is made available
|
112 |
|
|
* to receive more data. */
|
113 |
|
|
status = CyU3PDmaChannelDiscardBuffer (&dma_in_handle);
|
114 |
|
|
if (!ztex_usb_is_connected) continue;
|
115 |
|
|
ZTEX_REC(status);
|
116 |
|
|
|
117 |
|
|
/* Commit the received data to the consumer pipe so that the data can be
|
118 |
|
|
* transmitted back to the USB host. Since the same data is sent back, the
|
119 |
|
|
* count shall be same as received and the status field of the call shall
|
120 |
|
|
* be 0 for default use case. */
|
121 |
|
|
status = CyU3PDmaChannelCommitBuffer (&dma_out_handle, inbuf.count, 0);
|
122 |
|
|
// ZTEX_LOG("EC=%d, Sent %d bytes", status, outbuf.status);
|
123 |
|
|
if (!ztex_usb_is_connected) continue;
|
124 |
|
|
ZTEX_REC(status);
|
125 |
|
|
}
|
126 |
|
|
}
|
127 |
|
|
else {
|
128 |
|
|
/* No active data transfer. Sleep for a small amount of time. */
|
129 |
|
|
CyU3PThreadSleep (100);
|
130 |
|
|
}
|
131 |
|
|
}
|
132 |
|
|
|
133 |
|
|
}
|
134 |
|
|
|
135 |
|
|
/*
|
136 |
|
|
* Main function
|
137 |
|
|
*/
|
138 |
|
|
int main (void)
|
139 |
|
|
{
|
140 |
|
|
// global configuration
|
141 |
|
|
// ztex_app_thread_stack = 0x2000; // stack size of application thread, default: 0x1000
|
142 |
|
|
// ztex_app_thread_prio = 7; // priority of application thread, should be 7..15, default: 8
|
143 |
|
|
// ztex_allow_lpm = CyFalse; // do not allow transition into low power mode, default: allowed
|
144 |
|
|
|
145 |
|
|
ztex_app_thread_run = run;
|
146 |
|
|
ztex_usb_start = usb_start;
|
147 |
|
|
ztex_usb_stop = usb_stop;
|
148 |
|
|
|
149 |
|
|
ztex_main(); // starts the OS and never returns
|
150 |
|
|
return 0; // makes the compiler happy
|
151 |
|
|
}
|