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