1 /**
2  * \file
3  *
4  * \brief Serial Mode management
5  *
6  * Copyright (c) 2010-2015 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 #ifndef SERIAL_H_INCLUDED
47 #define SERIAL_H_INCLUDED
48 
49 #include <parts.h>
50 #include "status_codes.h"
51 
52 /**
53  * \typedef usart_if
54  *
55  * This type can be used independently to refer to USART module for the
56  * architecture used. It refers to the correct type definition for the
57  * architecture, ie. USART_t* for XMEGA or avr32_usart_t* for UC3.
58  */
59 
60 #if XMEGA
61 # include "xmega_usart/usart_serial.h"
62 #elif MEGA_RF
63 # include "megarf_usart/usart_serial.h"
64 #elif UC3
65 # include "uc3_usart/usart_serial.h"
66 #elif (SAMB)
67 #include "samb_uart/uart_serial.h"
68 #elif (SAM0)
69 #include "sam0_usart/usart_serial.h"
70 #elif SAM
71 # include "sam_uart/uart_serial.h"
72 #else
73 # error Unsupported chip type
74 #endif
75 
76 /**
77  *
78  * \defgroup serial_group Serial Interface (Serial)
79  *
80  * See \ref serial_quickstart.
81  *
82  * This is the common API for serial interface. Additional features are available
83  * in the documentation of the specific modules.
84  *
85  * \section serial_group_platform Platform Dependencies
86  *
87  * The serial API is partially chip- or platform-specific. While all
88  * platforms provide mostly the same functionality, there are some
89  * variations around how different bus types and clock tree structures
90  * are handled.
91  *
92  * The following functions are available on all platforms, but there may
93  * be variations in the function signature (i.e. parameters) and
94  * behaviour. These functions are typically called by platform-specific
95  * parts of drivers, and applications that aren't intended to be
96  * portable:
97  *   - usart_serial_init()
98  *   - usart_serial_putchar()
99  *   - usart_serial_getchar()
100  *   - usart_serial_write_packet()
101  *   - usart_serial_read_packet()
102  *
103  *
104  * @{
105  */
106 
107 //! @}
108 
109 /**
110  * \page serial_quickstart Quick start guide for Serial Interface service
111  *
112  * This is the quick start guide for the \ref serial_group "Serial Interface module", with
113  * step-by-step instructions on how to configure and use the serial in a
114  * selection of use cases.
115  *
116  * The use cases contain several code fragments. The code fragments in the
117  * steps for setup can be copied into a custom initialization function, while
118  * the steps for usage can be copied into, e.g., the main application function.
119  *
120  * \section serial_use_cases Serial use cases
121  * - \ref serial_basic_use_case
122  * - \subpage serial_use_case_1
123  *
124  * \section serial_basic_use_case Basic use case - transmit a character
125  * In this use case, the serial module is configured for:
126  * - Using USARTD0
127  * - Baudrate: 9600
128  * - Character length: 8 bit
129  * - Parity mode: Disabled
130  * - Stop bit: None
131  * - RS232 mode
132  *
133  * The use case waits for a received character on the configured USART and
134  * echoes the character back to the same USART.
135  *
136  * \section serial_basic_use_case_setup Setup steps
137  *
138  * \subsection serial_basic_use_case_setup_prereq Prerequisites
139  * -# \ref sysclk_group "System Clock Management (sysclk)"
140  *
141  * \subsection serial_basic_use_case_setup_code Example code
142  * The following configuration must be added to the project (typically to a
143  * conf_uart_serial.h file, but it can also be added to your main application file.)
144  *
145  * \note The following takes SAM3X configuration for example, other devices have similar
146  * configuration, but their parameters may be different, refer to corresponding header files.
147  *
148  * \code
149 	#define USART_SERIAL                     &USARTD0
150 	#define USART_SERIAL_BAUDRATE            9600
151 	#define USART_SERIAL_CHAR_LENGTH         US_MR_CHRL_8_BIT
152 	#define USART_SERIAL_PARITY              US_MR_PAR_NO
153 	#define USART_SERIAL_STOP_BIT            false
154 \endcode
155  *
156  * A variable for the received byte must be added:
157  * \code uint8_t received_byte; \endcode
158  *
159  * Add to application initialization:
160  * \code
161 	    sysclk_init();
162 
163 	    static usart_serial_options_t usart_options = {
164 	       .baudrate = USART_SERIAL_BAUDRATE,
165 	       .charlength = USART_SERIAL_CHAR_LENGTH,
166 	       .paritytype = USART_SERIAL_PARITY,
167 	       .stopbits = USART_SERIAL_STOP_BIT
168 	    };
169 
170 	    usart_serial_init(USART_SERIAL, &usart_options);
171 \endcode
172  *
173  * \subsection serial_basic_use_case_setup_flow Workflow
174  * -# Initialize system clock:
175  *   - \code sysclk_init(); \endcode
176  * -# Create serial USART options struct:
177  *   - \code
178 	static usart_serial_options_t usart_options = {
179 	   .baudrate = USART_SERIAL_BAUDRATE,
180 	   .charlength = USART_SERIAL_CHAR_LENGTH,
181 	   .paritytype = USART_SERIAL_PARITY,
182 	   .stopbits = USART_SERIAL_STOP_BIT
183 	};
184 \endcode
185  * -# Initialize the serial service:
186  *   - \code usart_serial_init(USART_SERIAL, &usart_options);\endcode
187  *
188  * \section serial_basic_use_case_usage Usage steps
189  *
190  * \subsection serial_basic_use_case_usage_code Example code
191  * Add to application C-file:
192  * \code
193 	usart_serial_getchar(USART_SERIAL, &received_byte);
194 	usart_serial_putchar(USART_SERIAL, received_byte);
195 \endcode
196  *
197  * \subsection serial_basic_use_case_usage_flow Workflow
198  * -# Wait for reception of a character:
199  *   - \code usart_serial_getchar(USART_SERIAL, &received_byte); \endcode
200  * -# Echo the character back:
201  *   - \code usart_serial_putchar(USART_SERIAL, received_byte); \endcode
202  */
203 
204 /**
205  * \page serial_use_case_1 Advanced use case - Send a packet of serial data
206  *
207  * In this use case, the USART module is configured for:
208  * - Using USARTD0
209  * - Baudrate: 9600
210  * - Character length: 8 bit
211  * - Parity mode: Disabled
212  * - Stop bit: None
213  * - RS232 mode
214  *
215  * The use case sends a string of text through the USART.
216  *
217  * \section serial_use_case_1_setup Setup steps
218  *
219  * \subsection serial_use_case_1_setup_prereq Prerequisites
220  * -# \ref sysclk_group "System Clock Management (sysclk)"
221  *
222  * \subsection serial_use_case_1_setup_code Example code
223  * The following configuration must be added to the project (typically to a
224  * conf_uart_serial.h file, but it can also be added to your main application file.):
225  *
226  * \note The following takes SAM3X configuration for example, other devices have similar
227  * configuration, but their parameters may be different, refer to corresponding header files.
228  *
229  * \code
230 	#define USART_SERIAL                     &USARTD0
231 	#define USART_SERIAL_BAUDRATE            9600
232 	#define USART_SERIAL_CHAR_LENGTH         US_MR_CHRL_8_BIT
233 	#define USART_SERIAL_PARITY              US_MR_PAR_NO
234 	#define USART_SERIAL_STOP_BIT            false
235 \endcode
236  *
237  * Add to application initialization:
238  * \code
239 	    sysclk_init();
240 
241 	    static usart_serial_options_t usart_options = {
242 	       .baudrate = USART_SERIAL_BAUDRATE,
243 	       .charlength = USART_SERIAL_CHAR_LENGTH,
244 	       .paritytype = USART_SERIAL_PARITY,
245 	       .stopbits = USART_SERIAL_STOP_BIT
246 	    };
247 
248 	    usart_serial_init(USART_SERIAL, &usart_options);
249 \endcode
250  *
251  * \subsection serial_use_case_1_setup_flow Workflow
252  * -# Initialize system clock:
253  *   - \code sysclk_init(); \endcode
254  * -# Create USART options struct:
255  *   - \code
256 	static usart_serial_options_t usart_options = {
257 	   .baudrate = USART_SERIAL_BAUDRATE,
258 	   .charlength = USART_SERIAL_CHAR_LENGTH,
259 	   .paritytype = USART_SERIAL_PARITY,
260 	   .stopbits = USART_SERIAL_STOP_BIT
261 	};
262 \endcode
263  * -# Initialize in RS232 mode:
264  *   - \code usart_serial_init(USART_SERIAL_EXAMPLE, &usart_options); \endcode
265  *
266  * \section serial_use_case_1_usage Usage steps
267  *
268  * \subsection serial_use_case_1_usage_code Example code
269  * Add to, e.g., main loop in application C-file:
270  * \code
271 	usart_serial_write_packet(USART_SERIAL, "Test String", strlen("Test String"));
272 \endcode
273  *
274  * \subsection serial_use_case_1_usage_flow Workflow
275  * -# Write a string of text to the USART:
276  *   - \code usart_serial_write_packet(USART_SERIAL, "Test String", strlen("Test String")); \endcode
277  */
278 
279 #endif /* SERIAL_H_INCLUDED */
280