Skip to content

Commit 19b0051

Browse files
committed
samples: drivers: pinctrl: add generic sample to show dynamic features
Add a new sample that shows how to change a UART device pins at runtime. Signed-off-by: Gerard Marull-Paretas <[email protected]>
1 parent 72a1eb6 commit 19b0051

File tree

6 files changed

+147
-0
lines changed

6 files changed

+147
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(pinctrl_dynamic)
7+
8+
target_sources(app PRIVATE src/main.c)
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/ {
7+
aliases {
8+
uart = &uart1;
9+
};
10+
11+
zephyr,user {
12+
uart_alt_default = <&uart1_alt_default>;
13+
};
14+
};
15+
16+
&pinctrl {
17+
/* Alternative pin configuration for UART1 */
18+
uart1_alt_default: uart1_alt_default {
19+
group1 {
20+
psels = <NRF_PSEL(UART_TX, 1, 5)>;
21+
};
22+
group2 {
23+
psels = <NRF_PSEL(UART_RX, 1, 7)>;
24+
bias-pull-up;
25+
};
26+
};
27+
};
28+
29+
&uart1 {
30+
status = "okay";
31+
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
/ {
7+
aliases {
8+
uart = &usart1;
9+
};
10+
11+
zephyr,user {
12+
uart_alt_default = <&usart1_tx_pb6 &usart1_rx_pb7>;
13+
uart_alt_reset = <&analog_pa9 &analog_pa10>;
14+
};
15+
};
16+
17+
&usart1 {
18+
pinctrl-0 = <&usart1_tx_pa9 &usart1_rx_pa10>;
19+
pinctrl-1 = <&analog_pa9 &analog_pa10>;
20+
pinctrl-names = "default", "reset";
21+
current-speed = <115200>;
22+
status = "okay";
23+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CONFIG_PINCTRL=y
2+
CONFIG_PINCTRL_DYNAMIC=y
3+
CONFIG_SERIAL=y
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
sample:
2+
description: A dynamic pin control sample
3+
name: pinctrl_dynamic
4+
tests:
5+
sample.drivers.pinctrl.dynamic:
6+
build_only: true
7+
platform_allow: nrf52840dk/nrf52840
8+
integration_platforms:
9+
- nrf52840dk/nrf52840
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2025 Nordic Semiconductor ASA
3+
* SPDX-License-Identifier: Apache-2.0
4+
*/
5+
6+
#include <stdio.h>
7+
#include <string.h>
8+
9+
#include <zephyr/device.h>
10+
#include <zephyr/drivers/pinctrl.h>
11+
#include <zephyr/drivers/uart.h>
12+
13+
#define UART_NODE DT_ALIAS(uart)
14+
15+
/* obtain reference to pinctrl config (owned by device) */
16+
PINCTRL_DT_DEV_CONFIG_DECLARE(UART_NODE);
17+
struct pinctrl_dev_config *uart_config = PINCTRL_DT_DEV_CONFIG_GET(UART_NODE);
18+
19+
/* define alternate states and build alternate state */
20+
PINCTRL_DT_STATE_PINS_DEFINE(DT_PATH(zephyr_user), uart_alt_default);
21+
#if DT_PINCTRL_HAS_NAME(UART_NODE, reset)
22+
PINCTRL_DT_STATE_PINS_DEFINE(DT_PATH(zephyr_user), uart_alt_reset);
23+
#endif
24+
25+
static const struct pinctrl_state uart_alt[] = {
26+
PINCTRL_DT_STATE_INIT(uart_alt_default, PINCTRL_STATE_DEFAULT),
27+
#if DT_PINCTRL_HAS_NAME(UART_NODE, reset)
28+
PINCTRL_DT_STATE_INIT(uart_alt_reset, PINCTRL_STATE_RESET),
29+
#endif
30+
};
31+
32+
static const struct device *const uart = DEVICE_DT_GET(UART_NODE);
33+
34+
static const char *const msg_a = "Hello World (A)!\r\n";
35+
static const char *const msg_b = "Hello World (B)!\r\n";
36+
37+
int main(void)
38+
{
39+
int ret;
40+
41+
if (!device_is_ready(uart)) {
42+
printf("UART device not ready\n");
43+
return 0;
44+
}
45+
46+
for (size_t i = 0U; i < strlen(msg_a); i++) {
47+
uart_poll_out(uart, msg_a[i]);
48+
}
49+
50+
ret = device_deinit(uart);
51+
if (ret < 0) {
52+
printf("Failed to de-initialize UART device (%d)\n", ret);
53+
return 0;
54+
}
55+
56+
ret = pinctrl_update_states(uart_config, uart_alt, ARRAY_SIZE(uart_alt));
57+
if (ret < 0) {
58+
printf("Failed to update pinctrl states\n");
59+
return 0;
60+
}
61+
62+
ret = device_init(uart);
63+
if (ret < 0) {
64+
printf("Failed to initialize UART device (%d)\n", ret);
65+
return 0;
66+
}
67+
68+
for (size_t i = 0U; i < strlen(msg_b); i++) {
69+
uart_poll_out(uart, msg_b[i]);
70+
}
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)