Skip to content

Commit 70fb312

Browse files
ananglcarlescufi
authored andcommitted
drivers: serial: nrfx: Ensure that instances have some pins assigned
Add build assertions that will ensure that every peripheral for which a driver instance is created has some pins assigned to it. Neither pinctrl-0 nor *-pin properties can be currently marked as required in devicetree, so these assertions will help users avoid invalid configurations where it could be hard to figure out why the UART is not working. Signed-off-by: Andrzej Głąbek <[email protected]>
1 parent 6a27383 commit 70fb312

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

drivers/serial/uart_nrfx_uart.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <drivers/uart.h>
1212
#include <pm/device.h>
13+
#include <soc.h>
1314
#include <hal/nrf_uart.h>
1415

1516
#ifdef CONFIG_PINCTRL
@@ -1192,6 +1193,8 @@ static int uart_nrfx_pm_action(const struct device *dev,
11921193
PINCTRL_DT_INST_DEFINE(0);
11931194
#endif /* CONFIG_PINCTRL */
11941195

1196+
NRF_DT_ENSURE_PINS_ASSIGNED(DT_DRV_INST(0), tx_pin, rx_pin);
1197+
11951198
static const struct uart_nrfx_config uart_nrfx_uart0_config = {
11961199
#ifdef CONFIG_PINCTRL
11971200
.pcfg = PINCTRL_DT_INST_DEV_CONFIG_GET(0),

drivers/serial/uart_nrfx_uarte.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include <nrfx_timer.h>
1515
#include <sys/util.h>
1616
#include <kernel.h>
17+
#include <soc.h>
1718
#include <logging/log.h>
1819
#include <helpers/nrfx_gppi.h>
1920
LOG_MODULE_REGISTER(uart_nrfx_uarte, LOG_LEVEL_ERR);
@@ -2047,6 +2048,7 @@ static int uarte_nrfx_pm_action(const struct device *dev,
20472048
#endif /* CONFIG_PINCTRL */
20482049

20492050
#define UART_NRF_UARTE_DEVICE(idx) \
2051+
NRF_DT_ENSURE_PINS_ASSIGNED(UARTE(idx), tx_pin, rx_pin); \
20502052
UARTE_INT_DRIVEN(idx); \
20512053
UARTE_ASYNC(idx); \
20522054
IF_ENABLED(CONFIG_PINCTRL, (PINCTRL_DT_DEFINE(UARTE(idx));)) \

samples/boards/nrf/dynamic_pinctrl/boards/nrf52840dk_nrf52840.overlay

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,12 @@
1717
pinctrl-names = "default", "sleep";
1818
};
1919

20+
/* Disable uart1 so that it is not left without any pins assigned. */
21+
/* TODO: remove once all boards enable pinctrl by default */
22+
&uart1 {
23+
status = "disabled";
24+
};
25+
2026
&pinctrl {
2127
/* pin configuration for UART0 */
2228
/* TODO: move to board dts once all boards enable pinctrl by default */

soc/arm/nordic_nrf/common/soc_nrf_common.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,42 @@
179179
"NRF_DT_CHECK_GPIO_CTLR_IS_SOC: OK")))
180180
/* Note: allow a trailing ";" either way */
181181

182+
/**
183+
* @brief Helper macro for NRF_DT_ENSURE_PINS_ASSIGNED
184+
*
185+
* This macro is neeeded only because the order of parameters taken by
186+
* DT_NODE_HAS_PROP is different than that required for a macro to be
187+
* invoked by FOR_EACH_FIXED_ARG.
188+
*
189+
* @param prop lowercase-and-underscores property name
190+
* @param node_id node identifier
191+
* @return 1 if the node has the property, 0 otherwise.
192+
*/
193+
#define NRF_DT_ENSURE_NODE_HAS_PROP(prop, node_id) \
194+
DT_NODE_HAS_PROP(node_id, prop)
195+
196+
/**
197+
* Error out the build if PINCTRL is enabled and the specified node does not
198+
* have the required pinctrl-N properties defined (pinctrl-0 always, pinctrl-1
199+
* when PM_DEVICE is enabled) or if PINCTRL is not enabled and the node does
200+
* not have at least one of the specified legacy pin properties defined.
201+
*
202+
* @param node_id node identifier
203+
* @param ... list of lowercase-and-underscores legacy pin properties from
204+
* which at least one needs to be defined if PINCTRL is not enabled
205+
*/
206+
#define NRF_DT_ENSURE_PINS_ASSIGNED(node_id, ...) \
207+
BUILD_ASSERT((IS_ENABLED(CONFIG_PINCTRL) && \
208+
DT_PINCTRL_HAS_IDX(node_id, 0) && \
209+
(DT_PINCTRL_HAS_IDX(node_id, 1) || \
210+
!IS_ENABLED(CONFIG_PM_DEVICE))) \
211+
|| \
212+
(!IS_ENABLED(CONFIG_PINCTRL) && \
213+
(FOR_EACH_FIXED_ARG(NRF_DT_ENSURE_NODE_HAS_PROP, \
214+
(||), node_id, __VA_ARGS__))),\
215+
DT_NODE_PATH(node_id) \
216+
" defined without required pin configuration")
217+
182218
#endif /* !_ASMLANGUAGE */
183219

184220
#endif

0 commit comments

Comments
 (0)