cf376e98a36f192945624683d7761b4abca49704
3 Copyright (C) Dean Camera, 2011.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2011 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
32 * \brief Standard avr-libc character stream driver for the USART.
34 * Serial stream driver for the USART subsystem on supported USB AVRs. This makes use of the functions in the
35 * regular USART driver (see \ref Group_Serial), but allows the avr-libc standard stream functions (\c printf,
36 * \c puts, etc.) to work with the
40 /** \ingroup Group_PeripheralDrivers
41 * @defgroup Group_SerialStream Serial Stream Driver - LUFA/Drivers/Peripheral/SerialStream.h
43 * \section Sec_Dependencies Module Source Dependencies
44 * The following files must be built with any user project that uses this module:
45 * - LUFA/Drivers/Peripheral/SerialStream.c <i>(Makefile source module name: LUFA_SRC_SERIALSTREAM)</i>
47 * \section Sec_ModDescription Module Description
48 * Serial stream driver for the USART subsystem on supported USB AVRs. This makes use of the functions in the
49 * regular USART driver (see \ref Group_Serial), but allows the avr-libc standard stream functions (\c printf,
50 * \c puts, etc.) to work with the USART. Upon configuration, this will redirect the \c stdin standard input
51 * and \c stdout output streams to the USART.
53 * \section Sec_ExampleUsage Example Usage
54 * The following snippet is an example of how this module may be used within a typical
58 * // Initialise the Serial Stream driver before first use, with 9600 baud (and no double-speed mode)
59 * SerialStream_Init(9600, false);
61 * // Write a string to the USART via the implicit stdout stream
62 * printf("Test String using stdout\r\n");
64 * // Write a string to the USART via the explicit USART stream
65 * fprintf(&USARTStream, "Test String using explicit stream handle\r\n");
67 * // Read in an integer from the USART using the implicit stdin stream
69 * scanf("%d", &TestValue);
75 #ifndef __SERIAL_STREAM_H__
76 #define __SERIAL_STREAM_H__
84 /* Enable C linkage for C++ Compilers: */
85 #if defined(__cplusplus)
89 /* Private Interface - For use in library only: */
90 #if !defined(__DOXYGEN__)
91 /* Function Prototypes: */
92 #if defined(__INCLUDE_FROM_SERIALSTREAM_C)
93 static int SerialStream_TxByte(char DataByte
,
94 FILE *Stream
) ATTR_NON_NULL_PTR_ARG(2);
95 static int SerialStream_RxByte(FILE *Stream
) ATTR_NON_NULL_PTR_ARG(1);
99 /* Public Interface - May be used in end-application: */
100 /* External Variables: */
101 /** Named stream for the USART, once \ref SerialStream_Init() has been called. This may be used with the
102 * file based stream functions (fprintf, fscanf, etc.) that require a handle to the stream rather than
103 * using the stdin and stdout named streams.
105 extern FILE USARTStream
;
107 /* Inline Functions: */
108 /** Initialises the serial stream (and regular USART driver) so that both the stream and regular
109 * USART driver functions can be used. Must be called before any stream or regular USART functions.
111 * \param[in] BaudRate Baud rate to configure the USART to.
112 * \param[in] DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate.
114 static inline void SerialStream_Init(const uint32_t BaudRate
,
115 const bool DoubleSpeed
)
117 Serial_Init(BaudRate
, DoubleSpeed
);
119 stdout
= &USARTStream
;
120 stdin
= &USARTStream
;
123 /** Turns off the serial stream (and regular USART driver), disabling and returning used hardware to
124 * their default configuration.
126 static inline void SerialStream_ShutDown(void)
131 /* Disable C linkage for C++ Compilers: */
132 #if defined(__cplusplus)