Kévin Redon | de9fb2e | 2019-04-03 20:55:02 +0200 | [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 | * Copyright (C) 2019 sysmocom -s.f.m.c. GmbH, Author: Kevin Redon <kredon@sysmocom.de> |
| 8 | * |
| 9 | * \asf_license_start |
| 10 | * |
| 11 | * \page License |
| 12 | * |
| 13 | * Subject to your compliance with these terms, you may use Microchip |
| 14 | * software and any derivatives exclusively with Microchip products. |
| 15 | * It is your responsibility to comply with third party license terms applicable |
| 16 | * to your use of third party software (including open source software) that |
| 17 | * may accompany Microchip software. |
| 18 | * |
| 19 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, |
| 20 | * WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, |
| 21 | * INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, |
| 22 | * AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE |
| 23 | * LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL |
| 24 | * LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND WHATSOEVER RELATED TO THE |
| 25 | * SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN ADVISED OF THE |
| 26 | * POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST EXTENT |
| 27 | * ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY |
| 28 | * RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, |
| 29 | * THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. |
| 30 | * |
| 31 | * \asf_license_stop |
| 32 | * |
| 33 | */ |
| 34 | |
| 35 | #ifndef _HAL_USART_ASYNC_RINGS_H_INCLUDED |
| 36 | #define _HAL_USART_ASYNC_RINGS_H_INCLUDED |
| 37 | |
| 38 | #include "hal_io.h" |
| 39 | #include <hpl_usart_async.h> |
| 40 | #include <utils_ringbuffer.h> |
| 41 | |
| 42 | /** |
| 43 | * \addtogroup doc_driver_hal_usart_async |
| 44 | * |
| 45 | * @{ |
| 46 | */ |
| 47 | |
| 48 | #ifdef __cplusplus |
| 49 | extern "C" { |
| 50 | #endif |
| 51 | |
| 52 | /** |
| 53 | * \brief USART descriptor |
| 54 | * |
| 55 | * The USART descriptor forward declaration. |
| 56 | */ |
| 57 | struct usart_async_rings_descriptor; |
| 58 | |
| 59 | /** |
| 60 | * \brief USART callback type |
| 61 | */ |
| 62 | typedef void (*usart_rings_cb_t)(const struct usart_async_rings_descriptor *const descr); |
| 63 | |
| 64 | /** |
| 65 | * \brief USART callback types |
| 66 | */ |
| 67 | enum usart_async_rings_callback_type { USART_ASYNC_RINGS_RXC_CB, USART_ASYNC_RINGS_TXC_CB, USART_ASYNC_RINGS_ERROR_CB }; |
| 68 | |
| 69 | /** |
| 70 | * \brief USART callbacks |
| 71 | */ |
| 72 | struct usart_async_rings_callbacks { |
| 73 | usart_rings_cb_t tx_done; |
| 74 | usart_rings_cb_t rx_done; |
| 75 | usart_rings_cb_t error; |
| 76 | }; |
| 77 | |
| 78 | /** \brief USART status |
| 79 | * Status descriptor holds the current status of transfer. |
| 80 | */ |
| 81 | struct usart_async_rings_status { |
| 82 | /** Status flags */ |
| 83 | uint32_t flags; |
| 84 | /** Number of characters transmitted */ |
| 85 | uint16_t txcnt; |
| 86 | /** Number of characters receviced */ |
| 87 | uint16_t rxcnt; |
| 88 | }; |
| 89 | |
| 90 | /** |
| 91 | * \brief Asynchronous USART descriptor structure |
| 92 | */ |
| 93 | struct usart_async_rings_descriptor { |
| 94 | struct io_descriptor io; |
| 95 | struct _usart_async_device device; |
| 96 | struct usart_async_rings_callbacks usart_cb; |
| 97 | uint32_t stat; |
| 98 | |
| 99 | struct ringbuffer rx; |
| 100 | struct ringbuffer tx; |
| 101 | }; |
| 102 | |
| 103 | /** USART write busy */ |
| 104 | #define USART_ASYNC_RINGS_STATUS_BUSY 0x0001 |
| 105 | |
| 106 | /** |
| 107 | * \brief Initialize USART interface |
| 108 | * |
| 109 | * This function initializes the given I/O descriptor to be used as USART |
| 110 | * interface descriptor. |
| 111 | * It checks if the given hardware is not initialized and if the given hardware |
| 112 | * is permitted to be initialized. |
| 113 | * |
| 114 | * \param[out] descr A USART descriptor which is used to communicate via the USART |
| 115 | * \param[in] hw The pointer to the hardware instance |
| 116 | * \param[in] rx_buffer An RX buffer |
| 117 | * \param[in] rx_buffer_length The length of the buffer above |
| 118 | * \param[in] tx_buffer An TX buffer |
| 119 | * \param[in] tx_buffer_length The length of the buffer above |
| 120 | * \param[in] func The pointer to a set of function pointers |
| 121 | * |
| 122 | * \return Initialization status. |
| 123 | * \retval -1 Passed parameters were invalid or the interface is already |
| 124 | * initialized |
| 125 | * \retval 0 The initialization is completed successfully |
| 126 | */ |
| 127 | int32_t usart_async_rings_init(struct usart_async_rings_descriptor *const descr, void *const hw, uint8_t *const rx_buffer, |
| 128 | const uint16_t rx_buffer_length, uint8_t *const tx_buffer, |
| 129 | const uint16_t tx_buffer_length, void *const func); |
| 130 | |
| 131 | /** |
| 132 | * \brief Deinitialize USART interface |
| 133 | * |
| 134 | * This function deinitializes the given I/O descriptor. |
| 135 | * It checks if the given hardware is initialized and if the given hardware |
| 136 | * is permitted to be deinitialized. |
| 137 | * |
| 138 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 139 | * |
| 140 | * \return De-initialization status. |
| 141 | */ |
| 142 | int32_t usart_async_rings_deinit(struct usart_async_rings_descriptor *const descr); |
| 143 | |
| 144 | /** |
| 145 | * \brief Enable USART interface |
| 146 | * |
| 147 | * Enables the USART interface |
| 148 | * |
| 149 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 150 | * |
| 151 | * \return Enabling status. |
| 152 | */ |
| 153 | int32_t usart_async_rings_enable(struct usart_async_rings_descriptor *const descr); |
| 154 | |
| 155 | /** |
| 156 | * \brief Disable USART interface |
| 157 | * |
| 158 | * Disables the USART interface |
| 159 | * |
| 160 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 161 | * |
| 162 | * \return Disabling status. |
| 163 | */ |
| 164 | int32_t usart_async_rings_disable(struct usart_async_rings_descriptor *const descr); |
| 165 | |
| 166 | /** |
| 167 | * \brief Retrieve I/O descriptor |
| 168 | * |
| 169 | * This function retrieves the I/O descriptor of the given USART descriptor. |
| 170 | * |
| 171 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 172 | * \param[out] io An I/O descriptor to retrieve |
| 173 | * |
| 174 | * \return The status of I/O descriptor retrieving. |
| 175 | */ |
| 176 | int32_t usart_async_rings_get_io_descriptor(struct usart_async_rings_descriptor *const descr, struct io_descriptor **io); |
| 177 | |
| 178 | /** |
| 179 | * \brief Register USART callback |
| 180 | * |
| 181 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 182 | * \param[in] type Callback type |
| 183 | * \param[in] cb A callback function |
| 184 | * |
| 185 | * \return The status of callback assignment. |
| 186 | * \retval -1 Passed parameters were invalid or the interface is not initialized |
| 187 | * \retval 0 A callback is registered successfully |
| 188 | */ |
| 189 | int32_t usart_async_rings_register_callback(struct usart_async_rings_descriptor *const descr, |
| 190 | const enum usart_async_rings_callback_type type, usart_rings_cb_t cb); |
| 191 | |
| 192 | /** |
| 193 | * \brief Specify action for flow control pins |
| 194 | * |
| 195 | * This function sets action (or state) for flow control pins if |
| 196 | * the flow control is enabled. |
| 197 | * It sets state of flow control pins only if automatic support of |
| 198 | * the flow control is not supported by the hardware. |
| 199 | * |
| 200 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 201 | * \param[in] state A state to set the flow control pins |
| 202 | * |
| 203 | * \return The status of flow control action setup. |
| 204 | */ |
| 205 | int32_t usart_async_rings_set_flow_control(struct usart_async_rings_descriptor *const descr, |
| 206 | const union usart_flow_control_state state); |
| 207 | |
| 208 | /** |
| 209 | * \brief Set USART baud rate |
| 210 | * |
| 211 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 212 | * \param[in] baud_rate A baud rate to set |
| 213 | * |
| 214 | * \return The status of baud rate setting. |
| 215 | */ |
| 216 | int32_t usart_async_rings_set_baud_rate(struct usart_async_rings_descriptor *const descr, const uint32_t baud_rate); |
| 217 | |
| 218 | /** |
| 219 | * \brief Set USART data order |
| 220 | * |
| 221 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 222 | * \param[in] data_order A data order to set |
| 223 | * |
| 224 | * \return The status of data order setting. |
| 225 | */ |
| 226 | int32_t usart_async_rings_set_data_order(struct usart_async_rings_descriptor *const descr, const enum usart_data_order data_order); |
| 227 | |
| 228 | /** |
| 229 | * \brief Set USART mode |
| 230 | * |
| 231 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 232 | * \param[in] mode A mode to set |
| 233 | * |
| 234 | * \return The status of mode setting. |
| 235 | */ |
| 236 | int32_t usart_async_rings_set_mode(struct usart_async_rings_descriptor *const descr, const enum usart_mode mode); |
| 237 | |
| 238 | /** |
| 239 | * \brief Set USART parity |
| 240 | * |
| 241 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 242 | * \param[in] parity A parity to set |
| 243 | * |
| 244 | * \return The status of parity setting. |
| 245 | */ |
| 246 | int32_t usart_async_rings_set_parity(struct usart_async_rings_descriptor *const descr, const enum usart_parity parity); |
| 247 | |
| 248 | /** |
| 249 | * \brief Set USART stop bits |
| 250 | * |
| 251 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 252 | * \param[in] stop_bits Stop bits to set |
| 253 | * |
| 254 | * \return The status of stop bits setting. |
| 255 | */ |
| 256 | int32_t usart_async_rings_set_stopbits(struct usart_async_rings_descriptor *const descr, const enum usart_stop_bits stop_bits); |
| 257 | |
| 258 | /** |
| 259 | * \brief Set USART character size |
| 260 | * |
| 261 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 262 | * \param[in] size A character size to set |
| 263 | * |
| 264 | * \return The status of character size setting. |
| 265 | */ |
| 266 | int32_t usart_async_rings_set_character_size(struct usart_async_rings_descriptor *const descr, |
| 267 | const enum usart_character_size size); |
| 268 | |
| 269 | /** |
| 270 | * \brief Retrieve the state of flow control pins |
| 271 | * |
| 272 | * This function retrieves the flow control pins |
| 273 | * if the flow control is enabled. |
| 274 | * |
| 275 | * The function can return USART_FLOW_CONTROL_STATE_UNAVAILABLE in case |
| 276 | * if the flow control is done by the hardware |
| 277 | * and the pins state cannot be read out. |
| 278 | * |
| 279 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 280 | * \param[out] state The state of flow control pins |
| 281 | * |
| 282 | * \return The status of flow control state reading. |
| 283 | */ |
| 284 | int32_t usart_async_rings_flow_control_status(const struct usart_async_rings_descriptor *const descr, |
| 285 | union usart_flow_control_state *const state); |
| 286 | |
| 287 | /** |
| 288 | * \brief Check if the USART transmitter is empty |
| 289 | * |
| 290 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 291 | * |
| 292 | * \return The status of USART TX empty checking. |
| 293 | * \retval 0 The USART transmitter is not empty |
| 294 | * \retval 1 The USART transmitter is empty |
| 295 | */ |
| 296 | int32_t usart_async_rings_is_tx_empty(const struct usart_async_rings_descriptor *const descr); |
| 297 | |
| 298 | /** |
| 299 | * \brief Check if the USART receiver is not empty |
| 300 | * |
| 301 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 302 | * |
| 303 | * \return The status of the USART RX empty checking. |
| 304 | * \retval 1 The USART receiver is not empty |
| 305 | * \retval 0 The USART receiver is empty |
| 306 | */ |
| 307 | int32_t usart_async_rings_is_rx_not_empty(const struct usart_async_rings_descriptor *const descr); |
| 308 | |
| 309 | /** |
| 310 | * \brief Retrieve the current interface status |
| 311 | * |
| 312 | * \param[in] descr A USART descriptor which is used to communicate via USART |
| 313 | * \param[out] status The state of USART |
| 314 | * |
| 315 | * \return The status of USART status retrieving. |
| 316 | */ |
| 317 | int32_t usart_async_rings_get_status(struct usart_async_rings_descriptor *const descr, struct usart_async_rings_status *const status); |
| 318 | |
| 319 | /** |
| 320 | * \brief flush USART ringbuf |
| 321 | * |
| 322 | * This function flush USART RX ringbuf. |
| 323 | * |
| 324 | * \param[in] descr The pointer to USART descriptor |
| 325 | * |
| 326 | * \return ERR_NONE |
| 327 | */ |
| 328 | int32_t usart_async_rings_flush_rx_buffer(struct usart_async_rings_descriptor *const descr); |
| 329 | |
| 330 | /** |
| 331 | * \brief Retrieve the current driver version |
| 332 | * |
| 333 | * \return Current driver version. |
| 334 | */ |
| 335 | uint32_t usart_async_rings_get_version(void); |
| 336 | |
| 337 | #ifdef __cplusplus |
| 338 | } |
| 339 | #endif |
| 340 | /**@}*/ |
| 341 | #endif /* _HAL_USART_ASYNC_RINGS_H_INCLUDED */ |