Added basic driver example use code to the library documentation.
[pub/USBasp.git] / LUFA / Drivers / Peripheral / SerialStream.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2010.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2010 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 /** \file
32 * \brief Standard avr-libc character stream driver for the USART.
33 *
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 (printf,
36 * puts, etc.) to work with the
37 * USART.
38 */
39
40 /** \ingroup Group_PeripheralDrivers
41 * @defgroup Group_SerialStream Serial Stream Driver - LUFA/Drivers/Peripheral/SerialStream.h
42 *
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>
46 *
47 * \section 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 (printf,
50 * puts, etc.) to work with the USART. Upon configuration, this will redirect the standard input and output
51 * streams to the USART.
52 *
53 * <b>Example Usage:</b>
54 * \code
55 * // Initialise the Serial Stream driver before first use, with 9600 baud (and no double-speed mode)
56 * SerialStream_Init(9600, false);
57 *
58 * // Write a string to the USART via the implicit stdout stream
59 * printf("Test String using stdout\r\n");
60 *
61 * // Write a string to the USART via the explicit USART stream
62 * fprintf(&USARTStream, "Test String using explicit stream handle\r\n");
63 *
64 * // Read in an integer from the USART using the implicit stdin stream
65 * uint16_t TestValue;
66 * scanf("%d", &TestValue);
67 * \endcode
68 *
69 * @{
70 */
71
72 #ifndef __SERIAL_STREAM_H__
73 #define __SERIAL_STREAM_H__
74
75 /* Includes: */
76 #include <avr/io.h>
77 #include <stdio.h>
78
79 #include "Serial.h"
80
81 /* Enable C linkage for C++ Compilers: */
82 #if defined(__cplusplus)
83 extern "C" {
84 #endif
85
86 /* Private Interface - For use in library only: */
87 #if !defined(__DOXYGEN__)
88 /* Function Prototypes: */
89 #if defined(__INCLUDE_FROM_SERIALSTREAM_C)
90 static int SerialStream_TxByte(char DataByte,
91 FILE *Stream) ATTR_NON_NULL_PTR_ARG(2);
92 static int SerialStream_RxByte(FILE *Stream) ATTR_NON_NULL_PTR_ARG(1);
93 #endif
94 #endif
95
96 /* Public Interface - May be used in end-application: */
97 /* Inline Functions: */
98 /** Initialises the serial stream (and regular USART driver) so that both the stream and regular
99 * USART driver functions can be used. Must be called before any stream or regular USART functions.
100 *
101 * \param[in] BaudRate Baud rate to configure the USART to.
102 * \param[in] DoubleSpeed Enables double speed mode when set, halving the sample time to double the baud rate.
103 */
104 static inline void SerialStream_Init(const uint32_t BaudRate,
105 const bool DoubleSpeed)
106 {
107 Serial_Init(BaudRate, DoubleSpeed);
108
109 stdout = &USARTStream;
110 stdin = &USARTStream;
111 }
112
113 /** Turns off the serial stream (and regular USART driver), disabling and returning used hardware to
114 * their default configuration.
115 */
116 static inline void SerialStream_ShutDown(void)
117 {
118 Serial_ShutDown();
119 }
120
121 /* External Variables: */
122 /** Named stream for the USART, once \ref SerialStream_Init() has been called. This may be used with the
123 * file based stream functions (fprintf, fscanf, etc.) that require a handle to the stream rather than
124 * using the stdin and stdout named streams.
125 */
126 extern FILE USARTStream;
127
128 /* Disable C linkage for C++ Compilers: */
129 #if defined(__cplusplus)
130 }
131 #endif
132
133 #endif
134
135 /** @} */
136