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 (SAM0) 67 #include "sam0_usart/usart_serial.h" 68 #elif SAM 69 # include "sam_uart/uart_serial.h" 70 #else 71 # error Unsupported chip type 72 #endif 73 74 /** 75 * 76 * \defgroup serial_group Serial Interface (Serial) 77 * 78 * See \ref serial_quickstart. 79 * 80 * This is the common API for serial interface. Additional features are available 81 * in the documentation of the specific modules. 82 * 83 * \section serial_group_platform Platform Dependencies 84 * 85 * The serial API is partially chip- or platform-specific. While all 86 * platforms provide mostly the same functionality, there are some 87 * variations around how different bus types and clock tree structures 88 * are handled. 89 * 90 * The following functions are available on all platforms, but there may 91 * be variations in the function signature (i.e. parameters) and 92 * behaviour. These functions are typically called by platform-specific 93 * parts of drivers, and applications that aren't intended to be 94 * portable: 95 * - usart_serial_init() 96 * - usart_serial_putchar() 97 * - usart_serial_getchar() 98 * - usart_serial_write_packet() 99 * - usart_serial_read_packet() 100 * 101 * 102 * @{ 103 */ 104 105 //! @} 106 107 /** 108 * \page serial_quickstart Quick start guide for Serial Interface service 109 * 110 * This is the quick start guide for the \ref serial_group "Serial Interface module", with 111 * step-by-step instructions on how to configure and use the serial in a 112 * selection of use cases. 113 * 114 * The use cases contain several code fragments. The code fragments in the 115 * steps for setup can be copied into a custom initialization function, while 116 * the steps for usage can be copied into, e.g., the main application function. 117 * 118 * \section serial_use_cases Serial use cases 119 * - \ref serial_basic_use_case 120 * - \subpage serial_use_case_1 121 * 122 * \section serial_basic_use_case Basic use case - transmit a character 123 * In this use case, the serial module is configured for: 124 * - Using USARTD0 125 * - Baudrate: 9600 126 * - Character length: 8 bit 127 * - Parity mode: Disabled 128 * - Stop bit: None 129 * - RS232 mode 130 * 131 * The use case waits for a received character on the configured USART and 132 * echoes the character back to the same USART. 133 * 134 * \section serial_basic_use_case_setup Setup steps 135 * 136 * \subsection serial_basic_use_case_setup_prereq Prerequisites 137 * -# \ref sysclk_group "System Clock Management (sysclk)" 138 * 139 * \subsection serial_basic_use_case_setup_code Example code 140 * The following configuration must be added to the project (typically to a 141 * conf_uart_serial.h file, but it can also be added to your main application file.) 142 * 143 * \note The following takes SAM3X configuration for example, other devices have similar 144 * configuration, but their parameters may be different, refer to corresponding header files. 145 * 146 * \code 147 #define USART_SERIAL &USARTD0 148 #define USART_SERIAL_BAUDRATE 9600 149 #define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT 150 #define USART_SERIAL_PARITY US_MR_PAR_NO 151 #define USART_SERIAL_STOP_BIT false 152 \endcode 153 * 154 * A variable for the received byte must be added: 155 * \code uint8_t received_byte; \endcode 156 * 157 * Add to application initialization: 158 * \code 159 sysclk_init(); 160 161 static usart_serial_options_t usart_options = { 162 .baudrate = USART_SERIAL_BAUDRATE, 163 .charlength = USART_SERIAL_CHAR_LENGTH, 164 .paritytype = USART_SERIAL_PARITY, 165 .stopbits = USART_SERIAL_STOP_BIT 166 }; 167 168 usart_serial_init(USART_SERIAL, &usart_options); 169 \endcode 170 * 171 * \subsection serial_basic_use_case_setup_flow Workflow 172 * -# Initialize system clock: 173 * - \code sysclk_init(); \endcode 174 * -# Create serial USART options struct: 175 * - \code 176 static usart_serial_options_t usart_options = { 177 .baudrate = USART_SERIAL_BAUDRATE, 178 .charlength = USART_SERIAL_CHAR_LENGTH, 179 .paritytype = USART_SERIAL_PARITY, 180 .stopbits = USART_SERIAL_STOP_BIT 181 }; 182 \endcode 183 * -# Initialize the serial service: 184 * - \code usart_serial_init(USART_SERIAL, &usart_options);\endcode 185 * 186 * \section serial_basic_use_case_usage Usage steps 187 * 188 * \subsection serial_basic_use_case_usage_code Example code 189 * Add to application C-file: 190 * \code 191 usart_serial_getchar(USART_SERIAL, &received_byte); 192 usart_serial_putchar(USART_SERIAL, received_byte); 193 \endcode 194 * 195 * \subsection serial_basic_use_case_usage_flow Workflow 196 * -# Wait for reception of a character: 197 * - \code usart_serial_getchar(USART_SERIAL, &received_byte); \endcode 198 * -# Echo the character back: 199 * - \code usart_serial_putchar(USART_SERIAL, received_byte); \endcode 200 */ 201 202 /** 203 * \page serial_use_case_1 Advanced use case - Send a packet of serial data 204 * 205 * In this use case, the USART module is configured for: 206 * - Using USARTD0 207 * - Baudrate: 9600 208 * - Character length: 8 bit 209 * - Parity mode: Disabled 210 * - Stop bit: None 211 * - RS232 mode 212 * 213 * The use case sends a string of text through the USART. 214 * 215 * \section serial_use_case_1_setup Setup steps 216 * 217 * \subsection serial_use_case_1_setup_prereq Prerequisites 218 * -# \ref sysclk_group "System Clock Management (sysclk)" 219 * 220 * \subsection serial_use_case_1_setup_code Example code 221 * The following configuration must be added to the project (typically to a 222 * conf_uart_serial.h file, but it can also be added to your main application file.): 223 * 224 * \note The following takes SAM3X configuration for example, other devices have similar 225 * configuration, but their parameters may be different, refer to corresponding header files. 226 * 227 * \code 228 #define USART_SERIAL &USARTD0 229 #define USART_SERIAL_BAUDRATE 9600 230 #define USART_SERIAL_CHAR_LENGTH US_MR_CHRL_8_BIT 231 #define USART_SERIAL_PARITY US_MR_PAR_NO 232 #define USART_SERIAL_STOP_BIT false 233 \endcode 234 * 235 * Add to application initialization: 236 * \code 237 sysclk_init(); 238 239 static usart_serial_options_t usart_options = { 240 .baudrate = USART_SERIAL_BAUDRATE, 241 .charlength = USART_SERIAL_CHAR_LENGTH, 242 .paritytype = USART_SERIAL_PARITY, 243 .stopbits = USART_SERIAL_STOP_BIT 244 }; 245 246 usart_serial_init(USART_SERIAL, &usart_options); 247 \endcode 248 * 249 * \subsection serial_use_case_1_setup_flow Workflow 250 * -# Initialize system clock: 251 * - \code sysclk_init(); \endcode 252 * -# Create USART options struct: 253 * - \code 254 static usart_serial_options_t usart_options = { 255 .baudrate = USART_SERIAL_BAUDRATE, 256 .charlength = USART_SERIAL_CHAR_LENGTH, 257 .paritytype = USART_SERIAL_PARITY, 258 .stopbits = USART_SERIAL_STOP_BIT 259 }; 260 \endcode 261 * -# Initialize in RS232 mode: 262 * - \code usart_serial_init(USART_SERIAL_EXAMPLE, &usart_options); \endcode 263 * 264 * \section serial_use_case_1_usage Usage steps 265 * 266 * \subsection serial_use_case_1_usage_code Example code 267 * Add to, e.g., main loop in application C-file: 268 * \code 269 usart_serial_write_packet(USART_SERIAL, "Test String", strlen("Test String")); 270 \endcode 271 * 272 * \subsection serial_use_case_1_usage_flow Workflow 273 * -# Write a string of text to the USART: 274 * - \code usart_serial_write_packet(USART_SERIAL, "Test String", strlen("Test String")); \endcode 275 */ 276 277 #endif /* SERIAL_H_INCLUDED */ 278