serial.h 9.3 KB

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