1 /**
2  * \file
3  *
4  * \brief USART related functionality declaration.
5  *
6  * Copyright (c) 2014-2018 Microchip Technology Inc. and its subsidiaries.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Subject to your compliance with these terms, you may use Microchip
13  * software and any derivatives exclusively with Microchip products.
14  * It is your responsibility to comply with third party license terms applicable
15  * to your use of third party software (including open source software) that
16  * may accompany Microchip software.
17  *
18  * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES,
19  * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE,
20  * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY,
21  * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE
22  * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL
23  * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE
24  * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE
25  * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE.  TO THE FULLEST EXTENT
26  * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY
27  * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY,
28  * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE.
29  *
30  * \asf_license_stop
31  *
32  */
33 
34 #ifndef _HPL_SYNC_USART_H_INCLUDED
35 #define _HPL_SYNC_USART_H_INCLUDED
36 
37 /**
38  * \addtogroup HPL USART SYNC
39  *
40  * \section hpl_usart_sync_rev Revision History
41  * - v1.0.0 Initial Release
42  *
43  *@{
44  */
45 
46 #include <hpl_usart.h>
47 
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51 
52 /**
53  * \brief USART descriptor device structure
54  */
55 struct _usart_sync_device {
56 	void *hw;
57 };
58 
59 /**
60  * \name HPL functions
61  */
62 //@{
63 /**
64  * \brief Initialize synchronous USART
65  *
66  * This function does low level USART configuration.
67  *
68  * \param[in] device The pointer to USART device instance
69  * \param[in] hw The pointer to hardware instance
70  *
71  * \return Initialization status
72  */
73 int32_t _usart_sync_init(struct _usart_sync_device *const device, void *const hw);
74 
75 /**
76  * \brief Deinitialize USART
77  *
78  * This function closes the given USART by disabling its clock.
79  *
80  * \param[in] device The pointer to USART device instance
81  */
82 void _usart_sync_deinit(struct _usart_sync_device *const device);
83 
84 /**
85  * \brief Enable usart module
86  *
87  * This function will enable the usart module
88  *
89  * \param[in] device The pointer to USART device instance
90  */
91 void _usart_sync_enable(struct _usart_sync_device *const device);
92 
93 /**
94  * \brief Disable usart module
95  *
96  * This function will disable the usart module
97  *
98  * \param[in] device The pointer to USART device instance
99  */
100 void _usart_sync_disable(struct _usart_sync_device *const device);
101 
102 /**
103  * \brief Calculate baud rate register value
104  *
105  * \param[in] baud Required baud rate
106  * \param[in] clock_rate clock frequency
107  * \param[in] samples The number of samples
108  * \param[in] mode USART mode
109  * \param[in] fraction A fraction value
110  *
111  * \return Calculated baud rate register value
112  */
113 uint16_t _usart_sync_calculate_baud_rate(const uint32_t baud, const uint32_t clock_rate, const uint8_t samples,
114                                          const enum usart_baud_rate_mode mode, const uint8_t fraction);
115 
116 /**
117  * \brief Set baud rate
118  *
119  * \param[in] device The pointer to USART device instance
120  * \param[in] baud_rate A baud rate to set
121  */
122 void _usart_sync_set_baud_rate(struct _usart_sync_device *const device, const uint32_t baud_rate);
123 
124 /**
125  * \brief Set data order
126  *
127  * \param[in] device The pointer to USART device instance
128  * \param[in] order A data order to set
129  */
130 void _usart_sync_set_data_order(struct _usart_sync_device *const device, const enum usart_data_order order);
131 
132 /**
133  * \brief Set mode
134  *
135  * \param[in] device The pointer to USART device instance
136  * \param[in] mode A mode to set
137  */
138 void _usart_sync_set_mode(struct _usart_sync_device *const device, const enum usart_mode mode);
139 
140 /**
141  * \brief Set parity
142  *
143  * \param[in] device The pointer to USART device instance
144  * \param[in] parity A parity to set
145  */
146 void _usart_sync_set_parity(struct _usart_sync_device *const device, const enum usart_parity parity);
147 
148 /**
149  * \brief Set stop bits mode
150  *
151  * \param[in] device The pointer to USART device instance
152  * \param[in] stop_bits A stop bits mode to set
153  */
154 void _usart_sync_set_stop_bits(struct _usart_sync_device *const device, const enum usart_stop_bits stop_bits);
155 
156 /**
157  * \brief Set character size
158  *
159  * \param[in] device The pointer to USART device instance
160  * \param[in] size A character size to set
161  */
162 void _usart_sync_set_character_size(struct _usart_sync_device *const device, const enum usart_character_size size);
163 
164 /**
165  * \brief Retrieve usart status
166  *
167  * \param[in] device The pointer to USART device instance
168  */
169 uint32_t _usart_sync_get_status(const struct _usart_sync_device *const device);
170 
171 /**
172  * \brief Write a byte to the given USART instance
173  *
174  * \param[in] device The pointer to USART device instance
175  * \param[in] data Data to write
176  */
177 void _usart_sync_write_byte(struct _usart_sync_device *const device, uint8_t data);
178 
179 /**
180  * \brief Read a byte from the given USART instance
181  *
182  * \param[in] device The pointer to USART device instance
183  * \param[in] data Data to write
184  *
185  * \return Data received via USART interface.
186  */
187 uint8_t _usart_sync_read_byte(const struct _usart_sync_device *const device);
188 
189 /**
190  * \brief Check if USART is ready to send next byte
191  *
192  * \param[in] device The pointer to USART device instance
193  *
194  * \return Status of the ready check.
195  * \retval true if the USART is ready to send next byte
196  * \retval false if the USART is not ready to send next byte
197  */
198 bool _usart_sync_is_ready_to_send(const struct _usart_sync_device *const device);
199 
200 /**
201  * \brief Check if USART transmitter has sent the byte
202  *
203  * \param[in] device The pointer to USART device instance
204  *
205  * \return Status of the ready check.
206  * \retval true if the USART transmitter has sent the byte
207  * \retval false if the USART transmitter has not send the byte
208  */
209 bool _usart_sync_is_transmit_done(const struct _usart_sync_device *const device);
210 
211 /**
212  * \brief Check if there is data received by USART
213  *
214  * \param[in] device The pointer to USART device instance
215  *
216  * \return Status of the data received check.
217  * \retval true if the USART has received a byte
218  * \retval false if the USART has not received a byte
219  */
220 bool _usart_sync_is_byte_received(const struct _usart_sync_device *const device);
221 
222 /**
223  * \brief Set the state of flow control pins
224  *
225  * \param[in] device The pointer to USART device instance
226  * \param[in] state - A state of flow control pins to set
227  */
228 void _usart_sync_set_flow_control_state(struct _usart_sync_device *const     device,
229                                         const union usart_flow_control_state state);
230 
231 /**
232  * \brief Retrieve the state of flow control pins
233  *
234  * This function retrieves the of flow control pins.
235  *
236  * \return USART_FLOW_CONTROL_STATE_UNAVAILABLE.
237  */
238 union usart_flow_control_state _usart_sync_get_flow_control_state(const struct _usart_sync_device *const device);
239 
240 /**
241  * \brief Retrieve ordinal number of the given USART hardware instance
242  *
243  * \param[in] device The pointer to USART device instance
244  *
245  * \return The ordinal number of the given USART hardware instance
246  */
247 uint8_t _usart_sync_get_hardware_index(const struct _usart_sync_device *const device);
248 //@}
249 
250 #ifdef __cplusplus
251 }
252 #endif
253 /**@}*/
254 #endif /* _HPL_SYNC_USART_H_INCLUDED */
255