1 /**
2 * \file
3 *
4 * \brief USB CDC Standard I/O Serial Management.
5 *
6 * This module defines support routines for a stdio serial interface to the
7 * Atmel Software Framework (ASF) common USB CDC service.
8 *
9 * Copyright (c) 2011-2015 Atmel Corporation. All rights reserved.
10 *
11 * \asf_license_start
12 *
13 * \page License
14 *
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions are met:
17 *
18 * 1. Redistributions of source code must retain the above copyright notice,
19 * this list of conditions and the following disclaimer.
20 *
21 * 2. Redistributions in binary form must reproduce the above copyright notice,
22 * this list of conditions and the following disclaimer in the documentation
23 * and/or other materials provided with the distribution.
24 *
25 * 3. The name of Atmel may not be used to endorse or promote products derived
26 * from this software without specific prior written permission.
27 *
28 * 4. This software may only be redistributed and used in connection with an
29 * Atmel microcontroller product.
30 *
31 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
32 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
34 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
35 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGE.
42 *
43 * \asf_license_stop
44 *
45 */
46 /*
47 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
48 */
49
50
51 #include "stdio_usb.h"
52
53 static bool stdio_usb_interface_enable = false;
54
stdio_usb_putchar(volatile void * unused,char data)55 int stdio_usb_putchar (volatile void * unused, char data)
56 {
57 /* A negative return value should be used to indicate that data
58 * was not written, but this doesn't seem to work with GCC libc.
59 */
60 if (!stdio_usb_interface_enable) {
61 return 0; // -1
62 }
63
64 return udi_cdc_putc(data) ? 0 : -1;
65 }
66
stdio_usb_getchar(void volatile * unused,char * data)67 void stdio_usb_getchar (void volatile * unused, char *data)
68 {
69 /* A negative return value should be used to indicate that data
70 * was not read, but this doesn't seem to work with GCC libc.
71 */
72 if (!stdio_usb_interface_enable) {
73 *data = 0; // -1
74 return;
75 }
76
77 *data = (char)udi_cdc_getc();
78 }
79
stdio_usb_enable(void)80 bool stdio_usb_enable(void)
81 {
82 stdio_usb_interface_enable = true;
83 return true;
84 }
85
stdio_usb_disable(void)86 void stdio_usb_disable(void)
87 {
88 stdio_usb_interface_enable = false;
89 }
90
stdio_usb_init(void)91 void stdio_usb_init(void)
92 {
93 stdio_base = NULL;
94 ptr_put = stdio_usb_putchar;
95 ptr_get = stdio_usb_getchar;
96
97 /*
98 * Start and attach USB CDC device interface for devices with
99 * integrated USB interfaces. Assume the VBUS is present if
100 * VBUS monitoring is not available.
101 */
102 udc_start ();
103
104 #if defined(__GNUC__)
105 # if XMEGA
106 // For AVR GCC libc print redirection uses fdevopen.
107 fdevopen((int (*)(char, FILE*))(_write),(int (*)(FILE*))(_read));
108 # endif
109 # if UC3 || SAM
110 // For AVR32 and SAM GCC
111 // Specify that stdout and stdin should not be buffered.
112 setbuf(stdout, NULL);
113 setbuf(stdin, NULL);
114 // Note: Already the case in IAR's Normal DLIB default configuration
115 // and AVR GCC library:
116 // - printf() emits one character at a time.
117 // - getchar() requests only 1 byte to exit.
118 # endif
119 #endif
120 }
121
122