1 /* 2 * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, this 9 * list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * 3. Neither the name of the copyright holder nor the names of its 16 * contributors may be used to endorse or promote products derived from this 17 * software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 20 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 23 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #ifndef NRF_UART_H__ 33 #define NRF_UART_H__ 34 35 #include <nrfx.h> 36 37 #ifdef __cplusplus 38 extern "C" { 39 #endif 40 41 /** 42 * @defgroup nrf_uart_hal UART HAL 43 * @{ 44 * @ingroup nrf_uart 45 * @brief Hardware access layer for managing the UART peripheral. 46 */ 47 48 #define NRF_UART_PSEL_DISCONNECTED 0xFFFFFFFF 49 50 /** 51 * @enum nrf_uart_task_t 52 * @brief UART tasks. 53 */ 54 typedef enum 55 { 56 /*lint -save -e30 -esym(628,__INTADDR__)*/ 57 NRF_UART_TASK_STARTRX = offsetof(NRF_UART_Type, TASKS_STARTRX), /**< Task for starting reception. */ 58 NRF_UART_TASK_STOPRX = offsetof(NRF_UART_Type, TASKS_STOPRX), /**< Task for stopping reception. */ 59 NRF_UART_TASK_STARTTX = offsetof(NRF_UART_Type, TASKS_STARTTX), /**< Task for starting transmission. */ 60 NRF_UART_TASK_STOPTX = offsetof(NRF_UART_Type, TASKS_STOPTX), /**< Task for stopping transmission. */ 61 NRF_UART_TASK_SUSPEND = offsetof(NRF_UART_Type, TASKS_SUSPEND), /**< Task for suspending UART. */ 62 /*lint -restore*/ 63 } nrf_uart_task_t; 64 65 /** 66 * @enum nrf_uart_event_t 67 * @brief UART events. 68 */ 69 typedef enum 70 { 71 /*lint -save -e30*/ 72 NRF_UART_EVENT_CTS = offsetof(NRF_UART_Type, EVENTS_CTS), /**< Event from CTS line activation. */ 73 NRF_UART_EVENT_NCTS = offsetof(NRF_UART_Type, EVENTS_NCTS), /**< Event from CTS line deactivation. */ 74 NRF_UART_EVENT_RXDRDY = offsetof(NRF_UART_Type, EVENTS_RXDRDY),/**< Event from data ready in RXD. */ 75 NRF_UART_EVENT_TXDRDY = offsetof(NRF_UART_Type, EVENTS_TXDRDY),/**< Event from data sent from TXD. */ 76 NRF_UART_EVENT_ERROR = offsetof(NRF_UART_Type, EVENTS_ERROR), /**< Event from error detection. */ 77 NRF_UART_EVENT_RXTO = offsetof(NRF_UART_Type, EVENTS_RXTO) /**< Event from receiver timeout. */ 78 /*lint -restore*/ 79 } nrf_uart_event_t; 80 81 /** 82 * @enum nrf_uart_int_mask_t 83 * @brief UART interrupts. 84 */ 85 typedef enum 86 { 87 /*lint -save -e30*/ 88 NRF_UART_INT_MASK_CTS = UART_INTENCLR_CTS_Msk, /**< CTS line activation interrupt. */ 89 NRF_UART_INT_MASK_NCTS = UART_INTENCLR_NCTS_Msk, /**< CTS line deactivation interrupt. */ 90 NRF_UART_INT_MASK_RXDRDY = UART_INTENCLR_RXDRDY_Msk, /**< Data ready in RXD interrupt. */ 91 NRF_UART_INT_MASK_TXDRDY = UART_INTENCLR_TXDRDY_Msk, /**< Data sent from TXD interrupt. */ 92 NRF_UART_INT_MASK_ERROR = UART_INTENCLR_ERROR_Msk, /**< Error detection interrupt. */ 93 NRF_UART_INT_MASK_RXTO = UART_INTENCLR_RXTO_Msk /**< Receiver timeout interrupt. */ 94 /*lint -restore*/ 95 } nrf_uart_int_mask_t; 96 97 /** 98 * @enum nrf_uart_baudrate_t 99 * @brief Baudrates supported by UART. 100 */ 101 typedef enum 102 { 103 NRF_UART_BAUDRATE_1200 = UART_BAUDRATE_BAUDRATE_Baud1200, /**< 1200 baud. */ 104 NRF_UART_BAUDRATE_2400 = UART_BAUDRATE_BAUDRATE_Baud2400, /**< 2400 baud. */ 105 NRF_UART_BAUDRATE_4800 = UART_BAUDRATE_BAUDRATE_Baud4800, /**< 4800 baud. */ 106 NRF_UART_BAUDRATE_9600 = UART_BAUDRATE_BAUDRATE_Baud9600, /**< 9600 baud. */ 107 NRF_UART_BAUDRATE_14400 = UART_BAUDRATE_BAUDRATE_Baud14400, /**< 14400 baud. */ 108 NRF_UART_BAUDRATE_19200 = UART_BAUDRATE_BAUDRATE_Baud19200, /**< 19200 baud. */ 109 NRF_UART_BAUDRATE_28800 = UART_BAUDRATE_BAUDRATE_Baud28800, /**< 28800 baud. */ 110 NRF_UART_BAUDRATE_31250 = UART_BAUDRATE_BAUDRATE_Baud31250, /**< 31250 baud. */ 111 NRF_UART_BAUDRATE_38400 = UART_BAUDRATE_BAUDRATE_Baud38400, /**< 38400 baud. */ 112 NRF_UART_BAUDRATE_56000 = UART_BAUDRATE_BAUDRATE_Baud56000, /**< 56000 baud. */ 113 NRF_UART_BAUDRATE_57600 = UART_BAUDRATE_BAUDRATE_Baud57600, /**< 57600 baud. */ 114 NRF_UART_BAUDRATE_76800 = UART_BAUDRATE_BAUDRATE_Baud76800, /**< 76800 baud. */ 115 NRF_UART_BAUDRATE_115200 = UART_BAUDRATE_BAUDRATE_Baud115200, /**< 115200 baud. */ 116 NRF_UART_BAUDRATE_230400 = UART_BAUDRATE_BAUDRATE_Baud230400, /**< 230400 baud. */ 117 NRF_UART_BAUDRATE_250000 = UART_BAUDRATE_BAUDRATE_Baud250000, /**< 250000 baud. */ 118 NRF_UART_BAUDRATE_460800 = UART_BAUDRATE_BAUDRATE_Baud460800, /**< 460800 baud. */ 119 NRF_UART_BAUDRATE_921600 = UART_BAUDRATE_BAUDRATE_Baud921600, /**< 921600 baud. */ 120 NRF_UART_BAUDRATE_1000000 = UART_BAUDRATE_BAUDRATE_Baud1M, /**< 1000000 baud. */ 121 } nrf_uart_baudrate_t; 122 123 /** 124 * @enum nrf_uart_error_mask_t 125 * @brief Types of UART error masks. 126 */ 127 typedef enum 128 { 129 NRF_UART_ERROR_OVERRUN_MASK = UART_ERRORSRC_OVERRUN_Msk, /**< Overrun error. */ 130 NRF_UART_ERROR_PARITY_MASK = UART_ERRORSRC_PARITY_Msk, /**< Parity error. */ 131 NRF_UART_ERROR_FRAMING_MASK = UART_ERRORSRC_FRAMING_Msk, /**< Framing error. */ 132 NRF_UART_ERROR_BREAK_MASK = UART_ERRORSRC_BREAK_Msk, /**< Break error. */ 133 } nrf_uart_error_mask_t; 134 135 /** 136 * @enum nrf_uart_parity_t 137 * @brief Types of UART parity modes. 138 */ 139 typedef enum 140 { 141 NRF_UART_PARITY_EXCLUDED = UART_CONFIG_PARITY_Excluded << UART_CONFIG_PARITY_Pos, /**< Parity excluded. */ 142 NRF_UART_PARITY_INCLUDED = UART_CONFIG_PARITY_Included << UART_CONFIG_PARITY_Pos, /**< Parity included. */ 143 } nrf_uart_parity_t; 144 145 /** 146 * @enum nrf_uart_hwfc_t 147 * @brief Types of UART flow control modes. 148 */ 149 typedef enum 150 { 151 NRF_UART_HWFC_DISABLED = UART_CONFIG_HWFC_Disabled, /**< HW flow control disabled. */ 152 NRF_UART_HWFC_ENABLED = UART_CONFIG_HWFC_Enabled, /**< HW flow control enabled. */ 153 } nrf_uart_hwfc_t; 154 155 /** 156 * @brief Function for clearing a specific UART event. 157 * 158 * @param[in] p_reg Pointer to the peripheral registers structure. 159 * @param[in] event Event to clear. 160 */ 161 __STATIC_INLINE void nrf_uart_event_clear(NRF_UART_Type * p_reg, nrf_uart_event_t event); 162 163 /** 164 * @brief Function for checking the state of a specific UART event. 165 * 166 * @param[in] p_reg Pointer to the peripheral registers structure. 167 * @param[in] event Event to check. 168 * 169 * @retval True if event is set, False otherwise. 170 */ 171 __STATIC_INLINE bool nrf_uart_event_check(NRF_UART_Type * p_reg, nrf_uart_event_t event); 172 173 /** 174 * @brief Function for returning the address of a specific UART event register. 175 * 176 * @param[in] p_reg Pointer to the peripheral registers structure. 177 * @param[in] event Desired event. 178 * 179 * @retval Address of specified event register. 180 */ 181 __STATIC_INLINE uint32_t nrf_uart_event_address_get(NRF_UART_Type * p_reg, 182 nrf_uart_event_t event); 183 184 /** 185 * @brief Function for enabling a specific interrupt. 186 * 187 * @param p_reg Pointer to the peripheral registers structure. 188 * @param int_mask Interrupts to enable. 189 */ 190 __STATIC_INLINE void nrf_uart_int_enable(NRF_UART_Type * p_reg, uint32_t int_mask); 191 192 /** 193 * @brief Function for retrieving the state of a given interrupt. 194 * 195 * @param p_reg Pointer to the peripheral registers structure. 196 * @param int_mask Mask of interrupt to check. 197 * 198 * @retval true If the interrupt is enabled. 199 * @retval false If the interrupt is not enabled. 200 */ 201 __STATIC_INLINE bool nrf_uart_int_enable_check(NRF_UART_Type * p_reg, uint32_t int_mask); 202 203 /** 204 * @brief Function for disabling specific interrupts. 205 * 206 * @param p_reg Pointer to the peripheral registers structure. 207 * @param int_mask Interrupts to disable. 208 */ 209 __STATIC_INLINE void nrf_uart_int_disable(NRF_UART_Type * p_reg, uint32_t int_mask); 210 211 /** 212 * @brief Function for getting error source mask. Function is clearing error source flags after reading. 213 * 214 * @param p_reg Pointer to the peripheral registers structure. 215 * @return Mask with error source flags. 216 */ 217 __STATIC_INLINE uint32_t nrf_uart_errorsrc_get_and_clear(NRF_UART_Type * p_reg); 218 219 /** 220 * @brief Function for enabling UART. 221 * 222 * @param p_reg Pointer to the peripheral registers structure. 223 */ 224 __STATIC_INLINE void nrf_uart_enable(NRF_UART_Type * p_reg); 225 226 /** 227 * @brief Function for disabling UART. 228 * 229 * @param p_reg Pointer to the peripheral registers structure. 230 */ 231 __STATIC_INLINE void nrf_uart_disable(NRF_UART_Type * p_reg); 232 233 /** 234 * @brief Function for configuring TX/RX pins. 235 * 236 * @param p_reg Pointer to the peripheral registers structure. 237 * @param pseltxd TXD pin number. 238 * @param pselrxd RXD pin number. 239 */ 240 __STATIC_INLINE void nrf_uart_txrx_pins_set(NRF_UART_Type * p_reg, uint32_t pseltxd, uint32_t pselrxd); 241 242 /** 243 * @brief Function for disconnecting TX/RX pins. 244 * 245 * @param p_reg Pointer to the peripheral registers structure. 246 */ 247 __STATIC_INLINE void nrf_uart_txrx_pins_disconnect(NRF_UART_Type * p_reg); 248 249 /** 250 * @brief Function for getting TX pin. 251 * 252 * @param p_reg Pointer to the peripheral registers structure. 253 */ 254 __STATIC_INLINE uint32_t nrf_uart_tx_pin_get(NRF_UART_Type * p_reg); 255 256 /** 257 * @brief Function for getting RX pin. 258 * 259 * @param p_reg Pointer to the peripheral registers structure. 260 */ 261 __STATIC_INLINE uint32_t nrf_uart_rx_pin_get(NRF_UART_Type * p_reg); 262 263 /** 264 * @brief Function for getting RTS pin. 265 * 266 * @param p_reg Pointer to the peripheral registers structure. 267 */ 268 __STATIC_INLINE uint32_t nrf_uart_rts_pin_get(NRF_UART_Type * p_reg); 269 270 /** 271 * @brief Function for getting CTS pin. 272 * 273 * @param p_reg Pointer to the peripheral registers structure. 274 */ 275 __STATIC_INLINE uint32_t nrf_uart_cts_pin_get(NRF_UART_Type * p_reg); 276 277 278 /** 279 * @brief Function for configuring flow control pins. 280 * 281 * @param p_reg Pointer to the peripheral registers structure. 282 * @param pselrts RTS pin number. 283 * @param pselcts CTS pin number. 284 */ 285 __STATIC_INLINE void nrf_uart_hwfc_pins_set(NRF_UART_Type * p_reg, 286 uint32_t pselrts, 287 uint32_t pselcts); 288 289 /** 290 * @brief Function for disconnecting flow control pins. 291 * 292 * @param p_reg Pointer to the peripheral registers structure. 293 */ 294 __STATIC_INLINE void nrf_uart_hwfc_pins_disconnect(NRF_UART_Type * p_reg); 295 296 /** 297 * @brief Function for reading RX data. 298 * 299 * @param p_reg Pointer to the peripheral registers structure. 300 * @return Received byte. 301 */ 302 __STATIC_INLINE uint8_t nrf_uart_rxd_get(NRF_UART_Type * p_reg); 303 304 /** 305 * @brief Function for setting Tx data. 306 * 307 * @param p_reg Pointer to the peripheral registers structure. 308 * @param txd Byte. 309 */ 310 __STATIC_INLINE void nrf_uart_txd_set(NRF_UART_Type * p_reg, uint8_t txd); 311 312 /** 313 * @brief Function for starting an UART task. 314 * 315 * @param p_reg Pointer to the peripheral registers structure. 316 * @param task Task. 317 */ 318 __STATIC_INLINE void nrf_uart_task_trigger(NRF_UART_Type * p_reg, nrf_uart_task_t task); 319 320 /** 321 * @brief Function for returning the address of a specific task register. 322 * 323 * @param p_reg Pointer to the peripheral registers structure. 324 * @param task Task. 325 * 326 * @return Task address. 327 */ 328 __STATIC_INLINE uint32_t nrf_uart_task_address_get(NRF_UART_Type * p_reg, nrf_uart_task_t task); 329 330 /** 331 * @brief Function for configuring UART. 332 * 333 * @param p_reg Pointer to the peripheral registers structure. 334 * @param hwfc Hardware flow control. Enabled if true. 335 * @param parity Parity. Included if true. 336 */ 337 __STATIC_INLINE void nrf_uart_configure(NRF_UART_Type * p_reg, 338 nrf_uart_parity_t parity, 339 nrf_uart_hwfc_t hwfc); 340 341 /** 342 * @brief Function for setting UART baudrate. 343 * 344 * @param p_reg Pointer to the peripheral registers structure. 345 * @param baudrate Baudrate. 346 */ 347 __STATIC_INLINE void nrf_uart_baudrate_set(NRF_UART_Type * p_reg, nrf_uart_baudrate_t baudrate); 348 349 #ifndef SUPPRESS_INLINE_IMPLEMENTATION 350 __STATIC_INLINE void nrf_uart_event_clear(NRF_UART_Type * p_reg, nrf_uart_event_t event) 351 { 352 *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)) = 0x0UL; 353 #if __CORTEX_M == 0x04 354 volatile uint32_t dummy = *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event)); 355 (void)dummy; 356 #endif 357 358 } 359 360 __STATIC_INLINE bool nrf_uart_event_check(NRF_UART_Type * p_reg, nrf_uart_event_t event) 361 { 362 return (bool)*(volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)event); 363 } 364 365 __STATIC_INLINE uint32_t nrf_uart_event_address_get(NRF_UART_Type * p_reg, 366 nrf_uart_event_t event) 367 { 368 return (uint32_t)((uint8_t *)p_reg + (uint32_t)event); 369 } 370 371 __STATIC_INLINE void nrf_uart_int_enable(NRF_UART_Type * p_reg, uint32_t int_mask) 372 { 373 p_reg->INTENSET = int_mask; 374 } 375 376 __STATIC_INLINE bool nrf_uart_int_enable_check(NRF_UART_Type * p_reg, uint32_t int_mask) 377 { 378 return (bool)(p_reg->INTENSET & int_mask); 379 } 380 381 __STATIC_INLINE void nrf_uart_int_disable(NRF_UART_Type * p_reg, uint32_t int_mask) 382 { 383 p_reg->INTENCLR = int_mask; 384 } 385 386 __STATIC_INLINE uint32_t nrf_uart_errorsrc_get_and_clear(NRF_UART_Type * p_reg) 387 { 388 uint32_t errsrc_mask = p_reg->ERRORSRC; 389 p_reg->ERRORSRC = errsrc_mask; 390 return errsrc_mask; 391 } 392 393 __STATIC_INLINE void nrf_uart_enable(NRF_UART_Type * p_reg) 394 { 395 p_reg->ENABLE = UART_ENABLE_ENABLE_Enabled; 396 } 397 398 __STATIC_INLINE void nrf_uart_disable(NRF_UART_Type * p_reg) 399 { 400 p_reg->ENABLE = UART_ENABLE_ENABLE_Disabled; 401 } 402 403 __STATIC_INLINE void nrf_uart_txrx_pins_set(NRF_UART_Type * p_reg, uint32_t pseltxd, uint32_t pselrxd) 404 { 405 #if defined(UART_PSEL_RXD_CONNECT_Pos) 406 p_reg->PSEL.RXD = pselrxd; 407 #else 408 p_reg->PSELRXD = pselrxd; 409 #endif 410 #if defined(UART_PSEL_TXD_CONNECT_Pos) 411 p_reg->PSEL.TXD = pseltxd; 412 #else 413 p_reg->PSELTXD = pseltxd; 414 #endif 415 } 416 417 __STATIC_INLINE void nrf_uart_txrx_pins_disconnect(NRF_UART_Type * p_reg) 418 { 419 nrf_uart_txrx_pins_set(p_reg, NRF_UART_PSEL_DISCONNECTED, NRF_UART_PSEL_DISCONNECTED); 420 } 421 422 __STATIC_INLINE uint32_t nrf_uart_tx_pin_get(NRF_UART_Type * p_reg) 423 { 424 #if defined(UART_PSEL_TXD_CONNECT_Pos) 425 return p_reg->PSEL.TXD; 426 #else 427 return p_reg->PSELTXD; 428 #endif 429 } 430 431 __STATIC_INLINE uint32_t nrf_uart_rx_pin_get(NRF_UART_Type * p_reg) 432 { 433 #if defined(UART_PSEL_RXD_CONNECT_Pos) 434 return p_reg->PSEL.RXD; 435 #else 436 return p_reg->PSELRXD; 437 #endif 438 } 439 440 __STATIC_INLINE uint32_t nrf_uart_rts_pin_get(NRF_UART_Type * p_reg) 441 { 442 #if defined(UART_PSEL_RTS_CONNECT_Pos) 443 return p_reg->PSEL.RTS; 444 #else 445 return p_reg->PSELRTS; 446 #endif 447 } 448 449 __STATIC_INLINE uint32_t nrf_uart_cts_pin_get(NRF_UART_Type * p_reg) 450 { 451 #if defined(UART_PSEL_RTS_CONNECT_Pos) 452 return p_reg->PSEL.CTS; 453 #else 454 return p_reg->PSELCTS; 455 #endif 456 } 457 458 __STATIC_INLINE void nrf_uart_hwfc_pins_set(NRF_UART_Type * p_reg, uint32_t pselrts, uint32_t pselcts) 459 { 460 #if defined(UART_PSEL_RTS_CONNECT_Pos) 461 p_reg->PSEL.RTS = pselrts; 462 #else 463 p_reg->PSELRTS = pselrts; 464 #endif 465 466 #if defined(UART_PSEL_RTS_CONNECT_Pos) 467 p_reg->PSEL.CTS = pselcts; 468 #else 469 p_reg->PSELCTS = pselcts; 470 #endif 471 } 472 473 __STATIC_INLINE void nrf_uart_hwfc_pins_disconnect(NRF_UART_Type * p_reg) 474 { 475 nrf_uart_hwfc_pins_set(p_reg, NRF_UART_PSEL_DISCONNECTED, NRF_UART_PSEL_DISCONNECTED); 476 } 477 478 __STATIC_INLINE uint8_t nrf_uart_rxd_get(NRF_UART_Type * p_reg) 479 { 480 return p_reg->RXD; 481 } 482 483 __STATIC_INLINE void nrf_uart_txd_set(NRF_UART_Type * p_reg, uint8_t txd) 484 { 485 p_reg->TXD = txd; 486 } 487 488 __STATIC_INLINE void nrf_uart_task_trigger(NRF_UART_Type * p_reg, nrf_uart_task_t task) 489 { 490 *((volatile uint32_t *)((uint8_t *)p_reg + (uint32_t)task)) = 0x1UL; 491 } 492 493 __STATIC_INLINE uint32_t nrf_uart_task_address_get(NRF_UART_Type * p_reg, nrf_uart_task_t task) 494 { 495 return (uint32_t)p_reg + (uint32_t)task; 496 } 497 498 __STATIC_INLINE void nrf_uart_configure(NRF_UART_Type * p_reg, 499 nrf_uart_parity_t parity, 500 nrf_uart_hwfc_t hwfc) 501 { 502 p_reg->CONFIG = (uint32_t)parity | (uint32_t)hwfc; 503 } 504 505 __STATIC_INLINE void nrf_uart_baudrate_set(NRF_UART_Type * p_reg, nrf_uart_baudrate_t baudrate) 506 { 507 p_reg->BAUDRATE = baudrate; 508 } 509 #endif //SUPPRESS_INLINE_IMPLEMENTATION 510 511 /** @} */ 512 513 #ifdef __cplusplus 514 } 515 #endif 516 517 #endif //NRF_UART_H__ 518