xref: /btstack/port/samv71-xplained-atwilc3000/ASF/common/utils/stdio/stdio_serial/stdio_serial.h (revision 1b2596b5303dd8caeea8565532c93cca8dab8cc4)
1 /**
2  *
3  * \file
4  *
5  * \brief Common Standard I/O Serial Management.
6  *
7  * This file defines a useful set of functions for the Stdio Serial interface on AVR
8  * and SAM devices.
9  *
10  * Copyright (c) 2009-2015 Atmel Corporation. All rights reserved.
11  *
12  * \asf_license_start
13  *
14  * \page License
15  *
16  * Redistribution and use in source and binary forms, with or without
17  * modification, are permitted provided that the following conditions are met:
18  *
19  * 1. Redistributions of source code must retain the above copyright notice,
20  *    this list of conditions and the following disclaimer.
21  *
22  * 2. Redistributions in binary form must reproduce the above copyright notice,
23  *    this list of conditions and the following disclaimer in the documentation
24  *    and/or other materials provided with the distribution.
25  *
26  * 3. The name of Atmel may not be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  * 4. This software may only be redistributed and used in connection with an
30  *    Atmel microcontroller product.
31  *
32  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
33  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
35  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
36  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
41  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGE.
43  *
44  * \asf_license_stop
45  *
46  ******************************************************************************/
47 /*
48  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
49  */
50 
51 
52 #ifndef _STDIO_SERIAL_H_
53 #define _STDIO_SERIAL_H_
54 
55 /**
56  * \defgroup group_common_utils_stdio_stdio_serial Standard serial I/O (stdio)
57  * \ingroup group_common_utils_stdio
58  *
59  * Common standard serial I/O management driver that
60  * implements a stdio serial interface on AVR and SAM devices.
61  *
62  * \{
63  */
64 
65 #include <stdio.h>
66 #include "compiler.h"
67 #ifndef SAMD20
68 # include "sysclk.h"
69 #endif
70 #include "serial.h"
71 
72 #if (XMEGA || MEGA_RF) && defined(__GNUC__)
73 	extern int _write (char c, int *f);
74 	extern int _read (int *f);
75 #endif
76 
77 
78 //! Pointer to the base of the USART module instance to use for stdio.
79 extern volatile void *volatile stdio_base;
80 //! Pointer to the external low level write function.
81 extern int (*ptr_put)(void volatile*, char);
82 
83 //! Pointer to the external low level read function.
84 extern void (*ptr_get)(void volatile*, char*);
85 
86 /*! \brief Initializes the stdio in Serial Mode.
87  *
88  * \param usart       Base address of the USART instance.
89  * \param opt         Options needed to set up RS232 communication (see \ref usart_options_t).
90  *
91  */
stdio_serial_init(volatile void * usart,const usart_serial_options_t * opt)92 static inline void stdio_serial_init(volatile void *usart, const usart_serial_options_t *opt)
93 {
94 	stdio_base = (void *)usart;
95 	ptr_put = (int (*)(void volatile*,char))&usart_serial_putchar;
96 	ptr_get = (void (*)(void volatile*,char*))&usart_serial_getchar;
97 # if (XMEGA || MEGA_RF)
98 	usart_serial_init((USART_t *)usart,opt);
99 # elif UC3
100 	usart_serial_init(usart,(usart_serial_options_t *)opt);
101 # elif SAM
102 	usart_serial_init((Usart *)usart,(usart_serial_options_t *)opt);
103 # else
104 #  error Unsupported chip type
105 # endif
106 
107 # if defined(__GNUC__)
108 #  if (XMEGA || MEGA_RF)
109 	// For AVR GCC libc print redirection uses fdevopen.
110 	fdevopen((int (*)(char, FILE*))(_write),(int (*)(FILE*))(_read));
111 #  endif
112 #  if UC3 || SAM
113 	// For AVR32 and SAM GCC
114 	// Specify that stdout and stdin should not be buffered.
115 	setbuf(stdout, NULL);
116 	setbuf(stdin, NULL);
117 	// Note: Already the case in IAR's Normal DLIB default configuration
118 	// and AVR GCC library:
119 	// - printf() emits one character at a time.
120 	// - getchar() requests only 1 byte to exit.
121 #  endif
122 # endif
123 }
124 
125 /**
126  * \}
127  */
128 
129 #endif  // _STDIO_SERIAL_H_
130