1 /**
2 * \file
3 *
4 * \brief USART Serial wrapper service for the SAM D/L/C/R devices.
5 *
6 * Copyright (c) 2009-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 _USART_SERIAL_H_
47 #define _USART_SERIAL_H_
48
49 #include "compiler.h"
50 #include "status_codes.h"
51 #include "usart.h"
52
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56
57 /** \name Serial Management Configuration */
58
59 typedef Sercom * usart_inst_t;
60
61 //struct usart_module usart;
62
63 /*! \brief Initializes the Usart in serial mode.
64 *
65 * \param[in,out] module Software instance of the USART to initialize.
66 * \param[in] hw Base address of the hardware USART.
67 * \param[in] config Configuration settings for the USART.
68 *
69 * \retval true if the initialization was successful
70 * \retval false if initialization failed (error in baud rate calculation)
71 */
usart_serial_init(struct usart_module * const module,usart_inst_t const hw,const struct usart_config * const config)72 static inline bool usart_serial_init(
73 struct usart_module *const module,
74 usart_inst_t const hw,
75 const struct usart_config *const config)
76 {
77 if (usart_init(module, hw, config) == STATUS_OK) {
78 return true;
79 }
80 else {
81 return false;
82 }
83 }
84
85 /** \brief Sends a character with the USART.
86 *
87 * \param[in,out] module Software instance of the USART.
88 * \param[in] c Character to write.
89 *
90 * \return Status code
91 */
usart_serial_putchar(struct usart_module * const module,uint8_t c)92 static inline enum status_code usart_serial_putchar(
93 struct usart_module *const module,
94 uint8_t c)
95 {
96 while(STATUS_OK !=usart_write_wait(module, c));
97
98 return STATUS_OK;
99 }
100
101 /** \brief Waits until a character is received, and returns it.
102 *
103 * \param[in,out] module Software instance of the USART.
104 * \param[out] c Destination for the read character.
105 */
usart_serial_getchar(struct usart_module * const module,uint8_t * c)106 static inline void usart_serial_getchar(
107 struct usart_module *const module,
108 uint8_t *c)
109 {
110 uint16_t temp = 0;
111
112 while(STATUS_OK != usart_read_wait(module, &temp));
113
114 *c = temp;
115 }
116
117 /**
118 * \brief Send a sequence of bytes to USART device
119 *
120 * \param[in,out] module Software instance of the USART.
121 * \param[in] tx_data Data buffer to read the data to write from.
122 * \param[in] length Length of data to write.
123 */
usart_serial_write_packet(struct usart_module * const module,const uint8_t * tx_data,uint16_t length)124 static inline enum status_code usart_serial_write_packet(
125 struct usart_module *const module,
126 const uint8_t *tx_data,
127 uint16_t length)
128 {
129 return usart_write_buffer_wait(module, tx_data, length);
130 }
131
132 /**
133 * \brief Receive a sequence of bytes from USART device
134 *
135 * \param[in,out] module Software instance of the USART.
136 * \param[out] rx_data Data buffer to store the read data into.
137 * \param[in] length Length of data to read.
138 */
usart_serial_read_packet(struct usart_module * const module,uint8_t * rx_data,uint16_t length)139 static inline enum status_code usart_serial_read_packet(
140 struct usart_module *const module,
141 uint8_t *rx_data,
142 uint16_t length)
143 {
144 return usart_read_buffer_wait(module, rx_data, length);
145 }
146
147 #ifdef __cplusplus
148 }
149 #endif
150
151 #endif // _USART_SERIAL_H_
152