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