uart.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /**
  2. *
  3. * \file
  4. *
  5. * \brief SAM UART Driver for SAMB11
  6. *
  7. * Copyright (C) 2015-2016 Atmel Corporation. All rights reserved.
  8. *
  9. * \asf_license_start
  10. *
  11. * \page License
  12. *
  13. * Redistribution and use in source and binary forms, with or without
  14. * modification, are permitted provided that the following conditions are met:
  15. *
  16. * 1. Redistributions of source code must retain the above copyright notice,
  17. * this list of conditions and the following disclaimer.
  18. *
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. *
  23. * 3. The name of Atmel may not be used to endorse or promote products derived
  24. * from this software without specific prior written permission.
  25. *
  26. * 4. This software may only be redistributed and used in connection with an
  27. * Atmel microcontroller product.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  30. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  31. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  32. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  33. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  34. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  35. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  36. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  37. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  38. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  39. * POSSIBILITY OF SUCH DAMAGE.
  40. *
  41. * \asf_license_stop
  42. *
  43. */
  44. /*
  45. * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
  46. */
  47. #ifndef UART_H_INCLUDED
  48. #define UART_H_INCLUDED
  49. /**
  50. * \defgroup asfdoc_samb_uart_group SAM UART Driver (UART)
  51. *
  52. * This driver for Atmel&reg; | SMART SAM devices provides an interface for the
  53. * configuration and management of the device's Universal Asynchronous
  54. * Receiver/Transmitter (UART) interfaces functionality.
  55. *
  56. * The following peripherals are used by this module:
  57. * - UART (Universal Asynchronous Receiver/Transmitter)
  58. *
  59. * The following devices can use this module:
  60. * - Atmel | SMART SAM B11
  61. *
  62. * The outline of this documentation is as follows:
  63. * - \ref asfdoc_samb_uart_prerequisites
  64. * - \ref asfdoc_samb_uart_module_overview
  65. * - \ref asfdoc_samb_uart_special_considerations
  66. * - \ref asfdoc_samb_uart_extra_info
  67. * - \ref asfdoc_samb_uart_examples
  68. * - \ref asfdoc_samb_uart_api_overview
  69. *
  70. *
  71. * \section asfdoc_samb_uart_prerequisites Prerequisites
  72. *
  73. * There are no prerequisites for this module.
  74. *
  75. *
  76. * \section asfdoc_samb_uart_module_overview Module Overview
  77. *
  78. * The device UART module provides an interface between the user application
  79. * logic and hardware peripheral. This driver provides an easy-to-use interface
  80. * to transfer and receive data.
  81. *
  82. * \section asfdoc_samb_uart_special_considerations Special Considerations
  83. *
  84. * There are no special considerations for this module.
  85. *
  86. * \section asfdoc_samb_uart_extra_info Extra Information
  87. *
  88. * For extra information, see \ref asfdoc_samb_uart_extra. This includes:
  89. * - \ref asfdoc_samb_uart_extra_acronyms
  90. * - \ref asfdoc_samb_uart_extra_dependencies
  91. * - \ref asfdoc_samb_uart_extra_errata
  92. * - \ref asfdoc_samb_uart_extra_history
  93. *
  94. *
  95. * \section asfdoc_samb_uart_examples Examples
  96. *
  97. * For a list of examples related to this driver, see
  98. * \ref asfdoc_samb_uart_exqsg.
  99. *
  100. *
  101. * \section asfdoc_samb_uart_api_overview API Overview
  102. * @{
  103. */
  104. #include <compiler.h>
  105. #include <system_sam_b.h>
  106. #include <gpio.h>
  107. #ifdef __cplusplus
  108. extern "C" {
  109. #endif
  110. /** \brief UART byte bit selection
  111. *
  112. * Number of bit per byte selection for UART communication.
  113. */
  114. enum uart_number_of_bit_selection{
  115. /** 8 bit per byte*/
  116. UART_8_BITS = UART_CONFIGURATION_NUMBER_OF_BITS_0,
  117. /** 7 bit per byte*/
  118. UART_7_BITS = UART_CONFIGURATION_NUMBER_OF_BITS_1,
  119. };
  120. /** \brief UART stop bit selection
  121. *
  122. * Number of stop bit selection for UART communication.
  123. */
  124. enum uart_stop_bit_selection{
  125. /** 1 stop bit per byte*/
  126. UART_1_STOP_BIT = UART_CONFIGURATION_STOP_BITS_0,
  127. /** 2 stop bit per byte*/
  128. UART_2_STOP_BITS = UART_CONFIGURATION_STOP_BITS_1,
  129. };
  130. /** \brief UART Parity selection
  131. *
  132. * Parity type selection for UART communication.
  133. */
  134. enum uart_parity_selection{
  135. /** No parity bit */
  136. UART_NO_PARITY = 0,
  137. /** Even parity */
  138. UART_EVEN_PARITY,
  139. /** Odd parity */
  140. UART_ODD_PARITY,
  141. /** Space parity */
  142. UART_SPACE_PARITY,
  143. /** Mark parity */
  144. UART_MARK_PARITY
  145. };
  146. /**
  147. * \brief UART module instance
  148. *
  149. * Forward Declaration for the device instance.
  150. */
  151. struct uart_module;
  152. /**
  153. * \brief UART callback type
  154. *
  155. * Type of the callback functions.
  156. */
  157. typedef void (*uart_callback_t)(struct uart_module *const module);
  158. /**
  159. * \brief UART Callback enum
  160. *
  161. * Callbacks for the UART driver.
  162. */
  163. enum uart_callback {
  164. /** Callback for TX FIFO not full. */
  165. UART_TX_COMPLETE,
  166. /** Callback for CTS active. */
  167. UART_CTS_ACTIVE,
  168. /** Callback for RX FIFO overrun. */
  169. UART_RX_COMPLETE,
  170. /** Callback for RX FIFO overrun. */
  171. UART_RX_FIFO_OVERRUN,
  172. /** Number of available callbacks. */
  173. UART_CALLBACK_N,
  174. };
  175. /**
  176. * \brief Configuration structure for the UART module
  177. *
  178. * This is the configuration structure for the UART Module in SAMB11. It
  179. * is used as an argument for \ref uart_init to provide the desired
  180. * configurations for the module. The structure should be initialized using the
  181. * \ref uart_get_config_defaults .
  182. */
  183. struct uart_config{
  184. /** Baud rate */
  185. uint32_t baud_rate;
  186. /** Number of data bits */
  187. enum uart_number_of_bit_selection data_bits;
  188. /** Number of stop bits */
  189. enum uart_stop_bit_selection stop_bits;
  190. /** Parity type */
  191. enum uart_parity_selection parity;
  192. /** flow control type */
  193. bool flow_control;
  194. /** UART PAD pin number */
  195. uint32_t pin_number_pad[4];
  196. /** UART PAD pinmux selection */
  197. uint32_t pinmux_sel_pad[4];
  198. };
  199. /**
  200. * \brief UART driver software device instance structure.
  201. *
  202. * UART driver software instance structure, used to retain software
  203. * state information of an associated hardware module instance.
  204. *
  205. * \note The fields of this structure should not be altered by the user
  206. * application; they are reserved for module-internal use only.
  207. */
  208. struct uart_module {
  209. /** Pointer to the hardware instance. */
  210. Uart *hw;
  211. /** Array to store callback function pointers in. */
  212. uart_callback_t callback[UART_CALLBACK_N];
  213. /** Buffer pointer to where the next received character will be put */
  214. volatile uint8_t *rx_buffer_ptr;
  215. /** Buffer pointer to where the next character will be transmitted from
  216. **/
  217. volatile uint8_t *tx_buffer_ptr;
  218. /** Remaining characters to receive */
  219. volatile uint16_t remaining_rx_buffer_length;
  220. /** Remaining characters to transmit */
  221. volatile uint16_t remaining_tx_buffer_length;
  222. /** Bit mask for callbacks registered. */
  223. uint8_t callback_reg_mask;
  224. /** Bit mask for callbacks enabled. */
  225. uint8_t callback_enable_mask;
  226. /** Holds the status of the ongoing or last operation */
  227. volatile enum status_code status;
  228. };
  229. /** \name UART Configuration and initialization
  230. * @{
  231. */
  232. void uart_get_config_defaults(struct uart_config *const config);
  233. enum status_code uart_init(struct uart_module *const module, Uart * const hw,
  234. const struct uart_config *const config);
  235. /** @} */
  236. /** \name UART read and write functions
  237. * @{
  238. */
  239. enum status_code uart_write_wait(struct uart_module *const module,
  240. const uint8_t tx_data);
  241. enum status_code uart_read_wait(struct uart_module *const module,
  242. uint8_t *const rx_data);
  243. enum status_code uart_write_buffer_wait(struct uart_module *const module,
  244. const uint8_t *tx_data, uint32_t length);
  245. enum status_code uart_read_buffer_wait(struct uart_module *const module,
  246. uint8_t *rx_data, uint16_t length);
  247. enum status_code uart_write_buffer_job(struct uart_module *const module,
  248. uint8_t *tx_data, uint32_t length);
  249. enum status_code uart_read_buffer_job(struct uart_module *const module,
  250. uint8_t *rx_data, uint16_t length);
  251. /** @} */
  252. /** \name UART callback config
  253. * @{
  254. */
  255. void uart_register_callback(struct uart_module *const module,
  256. uart_callback_t callback_func,
  257. enum uart_callback callback_type);
  258. void uart_unregister_callback(struct uart_module *module,
  259. enum uart_callback callback_type);
  260. void uart_enable_callback(struct uart_module *const module,
  261. enum uart_callback callback_type);
  262. void uart_disable_callback(struct uart_module *const module,
  263. enum uart_callback callback_type);
  264. /** @}*/
  265. /** \name UART DAM enable/disable functions
  266. * @{
  267. */
  268. void uart_enable_transmit_dma(struct uart_module *const module);
  269. void uart_disable_transmit_dma(struct uart_module *const module);
  270. void uart_enable_receive_dma(struct uart_module *const module);
  271. void uart_disable_receive_dma(struct uart_module *const module);
  272. /** @}*/
  273. /** @}*/
  274. #ifdef __cplusplus
  275. }
  276. #endif
  277. /**
  278. * \page asfdoc_samb_uart_extra Extra Information for UART Driver
  279. *
  280. * \section asfdoc_samb_uart_extra_acronyms Acronyms
  281. * Below is a table listing the acronyms used in this module, along with their
  282. * intended meanings.
  283. *
  284. * <table>
  285. * <tr>
  286. * <th>Acronym</th>
  287. * <th>Description</th>
  288. * </tr>
  289. * <tr>
  290. * <td>UART</td>
  291. * <td>Universal Asynchronous Receiver/Transmitter</td>
  292. * </tr>
  293. * </table>
  294. *
  295. *
  296. * \section asfdoc_samb_uart_extra_dependencies Dependencies
  297. * There are no dependencies related to this driver.
  298. *
  299. *
  300. * \section asfdoc_samb_uart_extra_errata Errata
  301. * There are no errata related to this driver.
  302. *
  303. *
  304. * \section asfdoc_samb_uart_extra_history Module History
  305. * An overview of the module history is presented in the table below, with
  306. * details on the enhancements and fixes made to the module since its first
  307. * release. The current version of this corresponds to the newest version in
  308. * the table.
  309. *
  310. * <table>
  311. * <tr>
  312. * <th>Changelog</th>
  313. * </tr>
  314. * <tr>
  315. * <td>Initial Release</td>
  316. * </tr>
  317. * </table>
  318. */
  319. /**
  320. * \page asfdoc_samb_uart_exqsg Examples for UART Driver
  321. *
  322. * This is a list of the available Quick Start guides (QSGs) and example
  323. * applications for \ref asfdoc_samb_uart_group. QSGs are simple examples with
  324. * step-by-step instructions to configure and use this driver in a selection of
  325. * use cases. Note that QSGs can be compiled as a standalone application or be
  326. * added to the user application.
  327. *
  328. * - \subpage asfdoc_samb_uart_basic_use_case
  329. * - \subpage asfdoc_samb_uart_dma_use_case
  330. *
  331. * \page asfdoc_samb_uart_document_revision_history Document Revision History
  332. *
  333. * <table>
  334. * <tr>
  335. * <th>Doc. Rev.</td>
  336. * <th>Date</td>
  337. * <th>Comments</td>
  338. * </tr>
  339. * <tr>
  340. * <td>A</td>
  341. * <td>09/2015</td>
  342. * <td>Initial release</td>
  343. * </tr>
  344. * </table>
  345. */
  346. #endif