]> git.kernelconcepts.de Git - karo-tx-redboot.git/blob - packages/io/usb/serial/slave/v2_0/tests/usbserial_echo.c
Initial revision
[karo-tx-redboot.git] / packages / io / usb / serial / slave / v2_0 / tests / usbserial_echo.c
1 //==========================================================================
2 //
3 //      usb_serial_echo.c
4 //
5 //      Example application for the USB serial layer in eCos.
6 //
7 //==========================================================================
8 //####ECOSGPLCOPYRIGHTBEGIN####
9 // -------------------------------------------
10 // This file is part of eCos, the Embedded Configurable Operating System.
11 //
12 // eCos is free software; you can redistribute it and/or modify it under
13 // the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 2 or (at your option) any later version.
15 //
16 // eCos is distributed in the hope that it will be useful, but WITHOUT ANY
17 // WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 // FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
19 // for more details.
20 //
21 // You should have received a copy of the GNU General Public License along
22 // with eCos; if not, write to the Free Software Foundation, Inc.,
23 // 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24 //
25 // As a special exception, if other files instantiate templates or use macros
26 // or inline functions from this file, or you compile this file and link it
27 // with other works to produce a work based on this file, this file does not
28 // by itself cause the resulting work to be covered by the GNU General Public
29 // License. However the source code for this file must still be made available
30 // in accordance with section (3) of the GNU General Public License.
31 //
32 // This exception does not invalidate any other reasons why a work based on
33 // this file might be covered by the GNU General Public License.
34 //
35 // -------------------------------------------
36 //####ECOSGPLCOPYRIGHTEND####
37 //===========================================================================
38 //#####DESCRIPTIONBEGIN####
39 //
40 // Author(s):    Frank M. Pagliughi (fmp), SoRo Systems, Inc.
41 // Contributors: 
42 // Date:         2008-06-02
43 // Description:  USB serial example application.
44 //
45 //####DESCRIPTIONEND####
46 //===========================================================================
47
48 #include <cyg/kernel/kapi.h>
49 #include <cyg/hal/hal_arch.h>
50 #include <cyg/infra/diag.h>
51 #include <pkgconf/kernel.h>
52 #include <cyg/io/usb/usbs_at91.h>
53 #include <cyg/io/usb/usbs_serial.h>
54
55 #include <stdio.h>
56 #include <stdlib.h>
57
58 #define DEBUG_OUTPUT
59
60 #if defined(DEBUG_OUTPUT)
61 #define DBG diag_printf
62 #else
63 #define DBG (1) ? (void)0 : diag_printf
64 #endif
65
66 typedef unsigned char byte;
67
68 // --------------------------------------------------------------------------
69 //                                                         Data & Callback(s)
70 // --------------------------------------------------------------------------
71
72 #define BUF_SIZE        4096
73
74 static byte     rx_buf[2][BUF_SIZE+1], tx_buf[BUF_SIZE+1];
75
76 // --------------------------------------------------------------------------
77 //                                                              Main Routine
78 // --------------------------------------------------------------------------
79
80 int main(void)
81 {
82   int             n;
83   unsigned        ibuf, next_buf;
84   
85   // ----- Start USB subsystem -----
86   
87   usbs_serial_start();
88   
89   // ----- Get data from host and send it back -----
90   
91   while (1) {
92     ibuf = 0;
93     
94     // ----- Wait for the host to configure -----
95     
96     usbs_serial_wait_until_configured();
97     cyg_thread_delay((cyg_tick_count_t) 10);
98     
99     // ----- While configured read data & print to screen -----
100     
101     usbs_serial_start_rx(&usbs_ser0, rx_buf[ibuf], BUF_SIZE);
102     
103     while (usbs_serial_is_configured()) {
104       
105       n = usbs_serial_wait_for_rx(&usbs_ser0);
106       next_buf = ibuf ^ 1;
107       
108       usbs_serial_start_rx(&usbs_ser0, rx_buf[next_buf], BUF_SIZE);
109       
110       if (n < 0) {
111         DBG("*** I/O Error: %d ***\n", n);
112       }
113       else {
114         memcpy(tx_buf, rx_buf[ibuf], n);
115         usbs_serial_tx(&usbs_ser0, tx_buf, n);
116         rx_buf[ibuf][n] = '\0';
117         DBG("%s", rx_buf[ibuf]);
118       }
119       
120       ibuf = next_buf;
121     }
122   }
123   
124   return 0;
125 }
126