Kévin Redon | ccbed0b | 2019-01-24 18:30:26 +0100 | [diff] [blame] | 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 _HAL_USART_ASYNC_H_INCLUDED |
| 35 | #define _HAL_USART_ASYNC_H_INCLUDED |
| 36 | |
| 37 | #include "hal_io.h" |
| 38 | #include <hpl_usart_async.h> |
| 39 | #include <utils_ringbuffer.h> |
| 40 | |
| 41 | /** |
| 42 | * \addtogroup doc_driver_hal_usart_async |
| 43 | * |
| 44 | * @{ |
| 45 | */ |
| 46 | |
| 47 | #ifdef __cplusplus |
| 48 | extern "C" { |
| 49 | #endif |
| 50 | |
| 51 | /** |
| 52 | * \brief USART descriptor |
| 53 | * |
| 54 | * The USART descriptor forward declaration. |
| 55 | */ |
| 56 | struct usart_async_descriptor; |
| 57 | |
| 58 | /** |
| 59 | * \brief USART callback type |
| 60 | */ |
| 61 | typedef void (*usart_cb_t)(const struct usart_async_descriptor *const descr); |
| 62 | |
| 63 | /** |
| 64 | * \brief USART callback types |
| 65 | */ |
| 66 | enum usart_async_callback_type { USART_ASYNC_RXC_CB, USART_ASYNC_TXC_CB, USART_ASYNC_ERROR_CB }; |
| 67 | |
| 68 | /** |
| 69 | * \brief USART callbacks |
| 70 | */ |
| 71 | struct usart_async_callbacks { |
| 72 | usart_cb_t tx_done; |
| 73 | usart_cb_t rx_done; |
| 74 | usart_cb_t error; |
| 75 | }; |
| 76 | |
| 77 | /** \brief USART status |
| 78 | * Status descriptor holds the current status of transfer. |
| 79 | */ |
| 80 | struct usart_async_status { |
| 81 | /** Status flags */ |
| 82 | uint32_t flags; |
| 83 | /** Number of characters transmitted */ |
| 84 | uint16_t txcnt; |
Kévin Redon | b40c3ee | 2019-04-17 01:23:10 +0200 | [diff] [blame] | 85 | /** Number of characters received */ |
Kévin Redon | ccbed0b | 2019-01-24 18:30:26 +0100 | [diff] [blame] | 86 | uint16_t rxcnt; |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * \brief Asynchronous USART descriptor structure |
| 91 | */ |
| 92 | struct usart_async_descriptor { |
| 93 | struct io_descriptor io; |
| 94 | struct _usart_async_device device; |
| 95 | struct usart_async_callbacks usart_cb; |
| 96 | uint32_t stat; |
| 97 | |
| 98 | struct ringbuffer rx; |
| 99 | uint16_t tx_por; |
| 100 | uint8_t * tx_buffer; |
| 101 | uint16_t tx_buffer_length; |
| 102 | }; |
| 103 | |
| 104 | /** USART write busy */ |
| 105 | #define USART_ASYNC_STATUS_BUSY 0x0001 |
| 106 | |
| 107 | /** |
| 108 | * \brief Initialize USART interface |
| 109 | * |
| 110 | * This function initializes the given I/O descriptor to be used as USART |
| 111 | * interface descriptor. |
| 112 | * It checks if the given hardware is not initialized and if the given hardware |
| 113 | * is permitted to be initialized. |
| 114 | * |
| 115 | * \param[out] descr A USART descriptor which is used to communicate via the USART |
| 116 | * \param[in] hw The pointer to the hardware instance |
| 117 | * \param[in] rx_buffer An RX buffer |
| 118 | * \param[in] rx_buffer_length The length of the buffer above |
| 119 | * \param[in] func The pointer to a set of function pointers |
| 120 | * |
| 121 | * \return Initialization status. |
| 122 | * \retval -1 Passed parameters were invalid or the interface is already |
| 123 | * initialized |
| 124 | * \retval 0 The initialization is completed successfully |
| 125 | */ |
| 126 | int32_t usart_async_init(struct usart_async_descriptor *const descr, void *const hw, uint8_t *const rx_buffer, |
| 127 | const uint16_t rx_buffer_length, void *const func); |
| 128 | |
| 129 | /** |
| 130 | * \brief Deinitialize USART interface |
| 131 | * |
| 132 | * This function deinitializes the given I/O descriptor. |
| 133 | * It checks if the given hardware is initialized and if the given hardware |
| 134 | * is permitted to be deinitialized. |
| 135 | * |
| 136 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 137 | * |
| 138 | * \return De-initialization status. |
| 139 | */ |
| 140 | int32_t usart_async_deinit(struct usart_async_descriptor *const descr); |
| 141 | |
| 142 | /** |
| 143 | * \brief Enable USART interface |
| 144 | * |
| 145 | * Enables the USART interface |
| 146 | * |
| 147 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 148 | * |
| 149 | * \return Enabling status. |
| 150 | */ |
| 151 | int32_t usart_async_enable(struct usart_async_descriptor *const descr); |
| 152 | |
| 153 | /** |
| 154 | * \brief Disable USART interface |
| 155 | * |
| 156 | * Disables the USART interface |
| 157 | * |
| 158 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 159 | * |
| 160 | * \return Disabling status. |
| 161 | */ |
| 162 | int32_t usart_async_disable(struct usart_async_descriptor *const descr); |
| 163 | |
| 164 | /** |
| 165 | * \brief Retrieve I/O descriptor |
| 166 | * |
| 167 | * This function retrieves the I/O descriptor of the given USART descriptor. |
| 168 | * |
| 169 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 170 | * \param[out] io An I/O descriptor to retrieve |
| 171 | * |
| 172 | * \return The status of I/O descriptor retrieving. |
| 173 | */ |
| 174 | int32_t usart_async_get_io_descriptor(struct usart_async_descriptor *const descr, struct io_descriptor **io); |
| 175 | |
| 176 | /** |
| 177 | * \brief Register USART callback |
| 178 | * |
| 179 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 180 | * \param[in] type Callback type |
| 181 | * \param[in] cb A callback function |
| 182 | * |
| 183 | * \return The status of callback assignment. |
| 184 | * \retval -1 Passed parameters were invalid or the interface is not initialized |
| 185 | * \retval 0 A callback is registered successfully |
| 186 | */ |
| 187 | int32_t usart_async_register_callback(struct usart_async_descriptor *const descr, |
| 188 | const enum usart_async_callback_type type, usart_cb_t cb); |
| 189 | |
| 190 | /** |
| 191 | * \brief Specify action for flow control pins |
| 192 | * |
| 193 | * This function sets action (or state) for flow control pins if |
| 194 | * the flow control is enabled. |
| 195 | * It sets state of flow control pins only if automatic support of |
| 196 | * the flow control is not supported by the hardware. |
| 197 | * |
| 198 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 199 | * \param[in] state A state to set the flow control pins |
| 200 | * |
| 201 | * \return The status of flow control action setup. |
| 202 | */ |
| 203 | int32_t usart_async_set_flow_control(struct usart_async_descriptor *const descr, |
| 204 | const union usart_flow_control_state state); |
| 205 | |
| 206 | /** |
| 207 | * \brief Set USART baud rate |
| 208 | * |
| 209 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 210 | * \param[in] baud_rate A baud rate to set |
| 211 | * |
| 212 | * \return The status of baud rate setting. |
| 213 | */ |
| 214 | int32_t usart_async_set_baud_rate(struct usart_async_descriptor *const descr, const uint32_t baud_rate); |
| 215 | |
| 216 | /** |
| 217 | * \brief Set USART data order |
| 218 | * |
| 219 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 220 | * \param[in] data_order A data order to set |
| 221 | * |
| 222 | * \return The status of data order setting. |
| 223 | */ |
| 224 | int32_t usart_async_set_data_order(struct usart_async_descriptor *const descr, const enum usart_data_order data_order); |
| 225 | |
| 226 | /** |
| 227 | * \brief Set USART mode |
| 228 | * |
| 229 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 230 | * \param[in] mode A mode to set |
| 231 | * |
| 232 | * \return The status of mode setting. |
| 233 | */ |
| 234 | int32_t usart_async_set_mode(struct usart_async_descriptor *const descr, const enum usart_mode mode); |
| 235 | |
| 236 | /** |
| 237 | * \brief Set USART parity |
| 238 | * |
| 239 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 240 | * \param[in] parity A parity to set |
| 241 | * |
| 242 | * \return The status of parity setting. |
| 243 | */ |
| 244 | int32_t usart_async_set_parity(struct usart_async_descriptor *const descr, const enum usart_parity parity); |
| 245 | |
| 246 | /** |
| 247 | * \brief Set USART stop bits |
| 248 | * |
| 249 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 250 | * \param[in] stop_bits Stop bits to set |
| 251 | * |
| 252 | * \return The status of stop bits setting. |
| 253 | */ |
| 254 | int32_t usart_async_set_stopbits(struct usart_async_descriptor *const descr, const enum usart_stop_bits stop_bits); |
| 255 | |
| 256 | /** |
| 257 | * \brief Set USART character size |
| 258 | * |
| 259 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 260 | * \param[in] size A character size to set |
| 261 | * |
| 262 | * \return The status of character size setting. |
| 263 | */ |
| 264 | int32_t usart_async_set_character_size(struct usart_async_descriptor *const descr, |
| 265 | const enum usart_character_size size); |
| 266 | |
| 267 | /** |
| 268 | * \brief Retrieve the state of flow control pins |
| 269 | * |
| 270 | * This function retrieves the flow control pins |
| 271 | * if the flow control is enabled. |
| 272 | * |
| 273 | * The function can return USART_FLOW_CONTROL_STATE_UNAVAILABLE in case |
| 274 | * if the flow control is done by the hardware |
| 275 | * and the pins state cannot be read out. |
| 276 | * |
| 277 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 278 | * \param[out] state The state of flow control pins |
| 279 | * |
| 280 | * \return The status of flow control state reading. |
| 281 | */ |
| 282 | int32_t usart_async_flow_control_status(const struct usart_async_descriptor *const descr, |
| 283 | union usart_flow_control_state *const state); |
| 284 | |
| 285 | /** |
| 286 | * \brief Check if the USART transmitter is empty |
| 287 | * |
| 288 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 289 | * |
| 290 | * \return The status of USART TX empty checking. |
| 291 | * \retval 0 The USART transmitter is not empty |
| 292 | * \retval 1 The USART transmitter is empty |
| 293 | */ |
| 294 | int32_t usart_async_is_tx_empty(const struct usart_async_descriptor *const descr); |
| 295 | |
| 296 | /** |
| 297 | * \brief Check if the USART receiver is not empty |
| 298 | * |
| 299 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 300 | * |
| 301 | * \return The status of the USART RX empty checking. |
| 302 | * \retval 1 The USART receiver is not empty |
| 303 | * \retval 0 The USART receiver is empty |
| 304 | */ |
| 305 | int32_t usart_async_is_rx_not_empty(const struct usart_async_descriptor *const descr); |
| 306 | |
| 307 | /** |
| 308 | * \brief Retrieve the current interface status |
| 309 | * |
| 310 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 311 | * \param[out] status The state of USART |
| 312 | * |
| 313 | * \return The status of USART status retrieving. |
| 314 | */ |
| 315 | int32_t usart_async_get_status(struct usart_async_descriptor *const descr, struct usart_async_status *const status); |
| 316 | |
| 317 | /** |
| 318 | * \brief flush USART ringbuf |
| 319 | * |
| 320 | * This function flush USART RX ringbuf. |
| 321 | * |
| 322 | * \param[in] descr The pointer to USART descriptor |
| 323 | * |
| 324 | * \return ERR_NONE |
| 325 | */ |
| 326 | int32_t usart_async_flush_rx_buffer(struct usart_async_descriptor *const descr); |
| 327 | |
| 328 | /** |
| 329 | * \brief Retrieve the current driver version |
| 330 | * |
| 331 | * \return Current driver version. |
| 332 | */ |
| 333 | uint32_t usart_async_get_version(void); |
| 334 | |
| 335 | #ifdef __cplusplus |
| 336 | } |
| 337 | #endif |
| 338 | /**@}*/ |
| 339 | #endif /* _HAL_USART_ASYNC_H_INCLUDED */ |