blob: 3f833d1ae142db8332cc978467a2ea55dbbe889a [file] [log] [blame]
Kévin Redon4cd3f7d2019-01-24 17:57:13 +01001/**
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_USART_ASYNC_H_INCLUDED
35#define _HPL_USART_ASYNC_H_INCLUDED
36
37/**
38 * \addtogroup HPL USART
39 *
40 * \section hpl_usart_rev Revision History
41 * - v1.0.0 Initial Release
42 *
43 *@{
44 */
45
46#include "hpl_usart.h"
47#include "hpl_irq.h"
48
49#ifdef __cplusplus
50extern "C" {
51#endif
52
53/**
54 * \brief USART callback types
55 */
56enum _usart_async_callback_type { USART_ASYNC_BYTE_SENT, USART_ASYNC_RX_DONE, USART_ASYNC_TX_DONE, USART_ASYNC_ERROR };
57
58/**
59 * \brief USART device structure
60 *
61 * The USART device structure forward declaration.
62 */
63struct _usart_async_device;
64
65/**
66 * \brief USART interrupt callbacks
67 */
68struct _usart_async_callbacks {
69 void (*tx_byte_sent)(struct _usart_async_device *device);
70 void (*rx_done_cb)(struct _usart_async_device *device, uint8_t data);
71 void (*tx_done_cb)(struct _usart_async_device *device);
72 void (*error_cb)(struct _usart_async_device *device);
73};
74
75/**
76 * \brief USART descriptor device structure
77 */
78struct _usart_async_device {
79 struct _usart_async_callbacks usart_cb;
80 struct _irq_descriptor irq;
81 void * hw;
82};
83/**
84 * \name HPL functions
85 */
86//@{
87/**
88 * \brief Initialize asynchronous USART
89 *
90 * This function does low level USART configuration.
91 *
92 * \param[in] device The pointer to USART device instance
93 * \param[in] hw The pointer to hardware instance
94 *
95 * \return Initialization status
96 */
97int32_t _usart_async_init(struct _usart_async_device *const device, void *const hw);
98
99/**
100 * \brief Deinitialize USART
101 *
102 * This function closes the given USART by disabling its clock.
103 *
104 * \param[in] device The pointer to USART device instance
105 */
106void _usart_async_deinit(struct _usart_async_device *const device);
107
108/**
109 * \brief Enable usart module
110 *
111 * This function will enable the usart module
112 *
113 * \param[in] device The pointer to USART device instance
114 */
115void _usart_async_enable(struct _usart_async_device *const device);
116
117/**
118 * \brief Disable usart module
119 *
120 * This function will disable the usart module
121 *
122 * \param[in] device The pointer to USART device instance
123 */
124void _usart_async_disable(struct _usart_async_device *const device);
125
126/**
127 * \brief Calculate baud rate register value
128 *
129 * \param[in] baud Required baud rate
130 * \param[in] clock_rate clock frequency
131 * \param[in] samples The number of samples
132 * \param[in] mode USART mode
133 * \param[in] fraction A fraction value
134 *
135 * \return Calculated baud rate register value
136 */
137uint16_t _usart_async_calculate_baud_rate(const uint32_t baud, const uint32_t clock_rate, const uint8_t samples,
138 const enum usart_baud_rate_mode mode, const uint8_t fraction);
139
140/**
141 * \brief Set baud rate
142 *
143 * \param[in] device The pointer to USART device instance
144 * \param[in] baud_rate A baud rate to set
145 */
146void _usart_async_set_baud_rate(struct _usart_async_device *const device, const uint32_t baud_rate);
147
148/**
149 * \brief Set data order
150 *
151 * \param[in] device The pointer to USART device instance
152 * \param[in] order A data order to set
153 */
154void _usart_async_set_data_order(struct _usart_async_device *const device, const enum usart_data_order order);
155
156/**
157 * \brief Set mode
158 *
159 * \param[in] device The pointer to USART device instance
160 * \param[in] mode A mode to set
161 */
162void _usart_async_set_mode(struct _usart_async_device *const device, const enum usart_mode mode);
163
164/**
165 * \brief Set parity
166 *
167 * \param[in] device The pointer to USART device instance
168 * \param[in] parity A parity to set
169 */
170void _usart_async_set_parity(struct _usart_async_device *const device, const enum usart_parity parity);
171
172/**
173 * \brief Set stop bits mode
174 *
175 * \param[in] device The pointer to USART device instance
176 * \param[in] stop_bits A stop bits mode to set
177 */
178void _usart_async_set_stop_bits(struct _usart_async_device *const device, const enum usart_stop_bits stop_bits);
179
180/**
181 * \brief Set character size
182 *
183 * \param[in] device The pointer to USART device instance
184 * \param[in] size A character size to set
185 */
186void _usart_async_set_character_size(struct _usart_async_device *const device, const enum usart_character_size size);
187
188/**
189 * \brief Retrieve usart status
190 *
191 * \param[in] device The pointer to USART device instance
192 */
193uint32_t _usart_async_get_status(const struct _usart_async_device *const device);
194
195/**
196 * \brief Write a byte to the given USART instance
197 *
198 * \param[in] device The pointer to USART device instance
199 * \param[in] data Data to write
200 */
201void _usart_async_write_byte(struct _usart_async_device *const device, uint8_t data);
202
203/**
204 * \brief Check if USART is ready to send next byte
205 *
206 * \param[in] device The pointer to USART device instance
207 *
208 * \return Status of the ready check.
209 * \retval true if the USART is ready to send next byte
210 * \retval false if the USART is not ready to send next byte
211 */
212bool _usart_async_is_byte_sent(const struct _usart_async_device *const device);
213
214/**
215 * \brief Set the state of flow control pins
216 *
217 * \param[in] device The pointer to USART device instance
218 * \param[in] state - A state of flow control pins to set
219 */
220void _usart_async_set_flow_control_state(struct _usart_async_device *const device,
221 const union usart_flow_control_state state);
222
223/**
224 * \brief Retrieve the state of flow control pins
225 *
226 * This function retrieves the of flow control pins.
227 *
228 * \return USART_FLOW_CONTROL_STATE_UNAVAILABLE.
229 */
230union usart_flow_control_state _usart_async_get_flow_control_state(const struct _usart_async_device *const device);
231
232/**
233 * \brief Enable data register empty interrupt
234 *
235 * \param[in] device The pointer to USART device instance
236 */
237void _usart_async_enable_byte_sent_irq(struct _usart_async_device *const device);
238
239/**
240 * \brief Enable transmission complete interrupt
241 *
242 * \param[in] device The pointer to USART device instance
243 */
244void _usart_async_enable_tx_done_irq(struct _usart_async_device *const device);
245
246/**
247 * \brief Retrieve ordinal number of the given USART hardware instance
248 *
249 * \param[in] device The pointer to USART device instance
250 *
251 * \return The ordinal number of the given USART hardware instance
252 */
253uint8_t _usart_async_get_hardware_index(const struct _usart_async_device *const device);
254
255/**
256 * \brief Enable/disable USART interrupt
257 *
258 * param[in] device The pointer to USART device instance
259 * param[in] type The type of interrupt to disable/enable if applicable
260 * param[in] state Enable or disable
261 */
262void _usart_async_set_irq_state(struct _usart_async_device *const device, const enum _usart_async_callback_type type,
263 const bool state);
264//@}
265
266#ifdef __cplusplus
267}
268#endif
269/**@}*/
270#endif /* _HPL_USART_ASYNC_H_INCLUDED */