Skip to content

Commit 3fd16e0

Browse files
committed
pinmux: some fixes after review
1 parent ba21508 commit 3fd16e0

8 files changed

Lines changed: 40 additions & 32 deletions

File tree

cores/arduino/zephyrCommon.cpp

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@
2525
*
2626
* Map deferred-init peripherals with their pinctrl configuration from devicetree.
2727
*/
28-
#define PINCTRL_NODE_DEFERRED(node_id) DT_PROP_OR(node_id, zephyr_deferred_init, 0)
28+
#define PINCTRL_NODE_DEFERRED(node_id) DT_PROP(node_id, zephyr_deferred_init)
2929

30-
#define PINCTRL_DEFINE_IF_PRESENT(node_id) \
31-
COND_CODE_1(PINCTRL_NODE_DEFERRED(node_id), (PINCTRL_DT_DEFINE(node_id);), ())
30+
#define PINCTRL_DECLARE_IF_DEFERRED(node_id) \
31+
COND_CODE_1(PINCTRL_NODE_DEFERRED(node_id), (PINCTRL_DT_DEV_CONFIG_DECLARE(node_id);), ())
3232

33-
DT_FOREACH_STATUS_OKAY_NODE(PINCTRL_DEFINE_IF_PRESENT)
33+
DT_FOREACH_STATUS_OKAY_NODE(PINCTRL_DECLARE_IF_DEFERRED)
3434

3535
struct pinctrl_map_entry {
3636
const struct device *dev;
@@ -66,11 +66,11 @@ static const struct pinctrl_dev_config *get_known_pcfg(const struct device *dev)
6666
* Switches peripheral pins back to ARDUINO pinctrl state (alternate function),
6767
* typically after a temporary transition to GPIO mode.
6868
*
69-
* @param idx Index of the pin within the device's ARDUINO pinctrl state
7069
* @param dev Pointer to the peripheral device
70+
* @param state_pin_idx Index of the pin within the device's ARDUINO pinctrl state
7171
* @return 0 on success, negative on error
7272
*/
73-
int apply_dev_channel_pinctrl(size_t state_pin_idx, const struct device *dev) {
73+
int apply_dev_channel_pinctrl(const struct device *dev, size_t state_pin_idx) {
7474

7575
const pinctrl_soc_pin_t *soc_pin = NULL;
7676
const struct pinctrl_state *state;
@@ -97,11 +97,8 @@ int apply_dev_channel_pinctrl(size_t state_pin_idx, const struct device *dev) {
9797
soc_pin = &state->pins[state_pin_idx];
9898

9999
/*
100-
* On platforms where CONFIG_PINCTRL_STORE_REG is enabled (e.g. nRF),
101-
* the peripheral's PSEL register must be written via the device base
102-
* address stored in pcfg->reg.
103-
* On platforms without CONFIG_PINCTRL_STORE_REG (e.g. STM32) the reg
104-
* argument is ignored by their pinctrl driver, so passing PINCTRL_REG_NONE is safe.
100+
* On platforms without CONFIG_PINCTRL_STORE_REG (e.g. STM32) the pcfg->reg is not present but
101+
* the argument is ignored by their pinctrl driver, so passing PINCTRL_REG_NONE is safe.
105102
*/
106103
#ifdef CONFIG_PINCTRL_STORE_REG
107104
err = pinctrl_configure_pins(soc_pin, 1, pcfg->reg);
@@ -116,12 +113,11 @@ int apply_dev_channel_pinctrl(size_t state_pin_idx, const struct device *dev) {
116113
}
117114

118115
/**
119-
* @brief Optimize peripheral transitions using pinctrl state switching.
116+
* @brief Optimize peripheral transitions applying pinctrl state PINCTRL_STATE_DEFAULT.
120117
*
121118
* @param dev Target peripheral device to acquire pin for
122-
* @param target_state Desired pinctrl state (typically PINCTRL_STATE_DEFAULT or SLEEP)
123119
*/
124-
int apply_dev_pinctrl(const struct device *dev, uint8_t target_state) {
120+
int apply_dev_pinctrl(const struct device *dev) {
125121

126122
if (dev == NULL || dev->config == NULL) {
127123
return -EINVAL;
@@ -133,7 +129,7 @@ int apply_dev_pinctrl(const struct device *dev, uint8_t target_state) {
133129
return -ENOTSUP;
134130
}
135131

136-
int ret = pinctrl_apply_state(pcfg, target_state);
132+
int ret = pinctrl_apply_state(pcfg, PINCTRL_STATE_DEFAULT);
137133

138134
if (ret < 0 && ret != -ENOENT) {
139135
return ret;
@@ -488,8 +484,8 @@ void analogWrite(pin_size_t pinNumber, int value) {
488484
}
489485
}
490486

491-
(void)apply_dev_channel_pinctrl(state_pin_index_from_spec_index(arduino_pwm, idx),
492-
arduino_pwm[idx].dev);
487+
(void)apply_dev_channel_pinctrl(arduino_pwm[idx].dev,
488+
state_pin_index_from_spec_index(arduino_pwm, idx));
493489
value = CLAMP(value, 0, maxInput);
494490

495491
const uint32_t pulse = map(value, 0, maxInput, 0, arduino_pwm[idx].period);
@@ -511,7 +507,7 @@ void analogWrite(enum dacPins dacName, int value) {
511507

512508
// TODO: add reverse map to find pin name from DAC* define
513509
// In the meantime, consider A0 == DAC0
514-
apply_dev_pinctrl(dac_dev, PINCTRL_STATE_DEFAULT);
510+
apply_dev_pinctrl(dac_dev);
515511
dac_channel_setup(dac_dev, &dac_ch_cfg[dacName]);
516512

517513
const int max_dac_value = 1U << dac_ch_cfg[dacName].resolution;
@@ -575,8 +571,8 @@ int analogRead(pin_size_t pinNumber) {
575571
* The pin is selected from the ADC device "arduino" pinctrl state.
576572
* Not checking the return value because the device might not have pinctrl (e.g. nRF SAADC).
577573
*/
578-
(void)apply_dev_channel_pinctrl(state_pin_index_from_spec_index(arduino_adc, idx),
579-
arduino_adc[idx].dev);
574+
(void)apply_dev_channel_pinctrl(arduino_adc[idx].dev,
575+
state_pin_index_from_spec_index(arduino_adc, idx));
580576

581577
err = adc_channel_setup(arduino_adc[idx].dev, &arduino_adc[idx].channel_cfg);
582578
if (err < 0) {

cores/arduino/zephyrInternal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ extern "C" {
1515
void enableInterrupt(pin_size_t);
1616
void disableInterrupt(pin_size_t);
1717

18-
int apply_dev_pinctrl(const struct device *dev, uint8_t target_state);
18+
int apply_dev_pinctrl(const struct device *dev);
1919

2020
#ifdef __cplusplus
2121
} // extern "C"

cores/arduino/zephyrSerial.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ void arduino::ZephyrSerial::begin(unsigned long baud, uint16_t conf) {
7272
/* Re-apply DEFAULT pinctrl state so shared pins
7373
* are remuxed back to Serial after temporary GPIO/PWM usage.
7474
*/
75-
(void)apply_dev_pinctrl(uart, PINCTRL_STATE_DEFAULT);
75+
(void)apply_dev_pinctrl(uart);
7676

7777
uart_configure(uart, &config);
7878
uart_irq_callback_user_data_set(uart, arduino::ZephyrSerial::IrqDispatch, this);

documentation/dynamic_pinmux.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,24 @@ Typical mapping:
4141

4242
The core builds a runtime map between devices and pinctrl configs.
4343

44-
Selection rule for nodes included in the map are node is zephyr,deferred-init OR has pinctrl state name arduino.
44+
Selection rule for nodes included in the map are node is zephyr,deferred-init.
4545

4646
The map is then used by APIs:
4747

48-
- apply_dev_pinctrl(dev, target_state)
49-
- apply_dev_channel_pinctrl(state_pin_idx, dev)
48+
- apply_dev_pinctrl(dev)
49+
- apply_dev_channel_pinctrl(dev, state_pin_idx)
5050

5151
If a device has no known pinctrl config in the map, APIs return -ENOTSUP.
5252

5353
## Dynamic Pinmux APIs
5454

55-
### apply_dev_pinctrl(dev, target_state)
55+
### apply_dev_pinctrl(dev)
5656

57-
Purpose: apply a whole pinctrl state to a device.
57+
Purpose: apply the whole pinctrl PINCTRL_STATE_DEFAULT state to a device.
5858

5959
Used for full peripheral remux, for example restore SPI/I2C/UART default state.
6060

61-
### apply_dev_channel_pinctrl(state_pin_idx, dev)
61+
### apply_dev_channel_pinctrl(dev, state_pin_idx)
6262

6363
Purpose: apply only one pin from the device arduino state.
6464

@@ -71,7 +71,7 @@ Used for channel-based peripherals (ADC/PWM) so one channel can be remuxed witho
7171
Implementation pattern:
7272

7373
- if device is not ready, call device_init(dev),
74-
- call apply_dev_pinctrl(dev, PINCTRL_STATE_DEFAULT).
74+
- call apply_dev_pinctrl(dev).
7575

7676
Effect:
7777

@@ -85,7 +85,7 @@ Implementation pattern:
8585
- if device is not ready, call device_init(dev),
8686
- resolve analog/pwm index from pin,
8787
- compute per-device ordinal index with state_pin_index_from_spec_index(arduino_adc/arduino_pwm, idx),
88-
- call apply_dev_channel_pinctrl(per_dev_idx, adc_dev),
88+
- call apply_dev_channel_pinctrl(dev, per_dev_idx),
8989

9090
Effect:
9191

libraries/SPI/SPI.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ void arduino::ZephyrSPI::begin() {
126126
/* Re-apply DEFAULT pinctrl state so shared pins
127127
* are remuxed back to SPI after temporary GPIO/PWM usage.
128128
*/
129-
(void)apply_dev_pinctrl(spi_dev, PINCTRL_STATE_DEFAULT);
129+
(void)apply_dev_pinctrl(spi_dev);
130130
}
131131

132132
void arduino::ZephyrSPI::end() {

libraries/Wire/Wire.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ void arduino::ZephyrI2C::begin() {
6969
/* Re-apply DEFAULT pinctrl state so shared pins
7070
* are remuxed back to I2C after temporary GPIO/PWM usage.
7171
*/
72-
(void)apply_dev_pinctrl(i2c_dev, PINCTRL_STATE_DEFAULT);
72+
(void)apply_dev_pinctrl(i2c_dev);
7373
}
7474

7575
void arduino::ZephyrI2C::begin(uint8_t slaveAddr) {

loader/llext_exports.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,17 @@ EXPORT_SYMBOL(k_sched_unlock);
118118
#if defined(CONFIG_PINCTRL)
119119
EXPORT_SYMBOL(pinctrl_lookup_state);
120120
EXPORT_SYMBOL(pinctrl_configure_pins);
121+
122+
/*
123+
* Export the pinctrl_dev_config objects for all deferred-init peripherals.
124+
*/
125+
#define Z_EXPORT_PINCTRL_IF_DEFERRED(node_id) \
126+
COND_CODE_1(DT_PROP(node_id, zephyr_deferred_init), \
127+
(PINCTRL_DT_DEV_CONFIG_DECLARE(node_id); \
128+
EXPORT_SYMBOL(Z_PINCTRL_DEV_CONFIG_NAME(node_id));), \
129+
())
130+
131+
DT_FOREACH_STATUS_OKAY_NODE(Z_EXPORT_PINCTRL_IF_DEFERRED)
121132
#endif
122133

123134
#if defined(CONFIG_USB_DEVICE_STACK)

loader/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ CONFIG_LLEXT_EDK_FORMAT_TAR_ZSTD=y
2727
CONFIG_GPIO=y
2828
CONFIG_GPIO_GET_DIRECTION=y
2929
CONFIG_PINCTRL=y
30+
CONFIG_PINCTRL_DYNAMIC=y
3031
CONFIG_DEVICE_DEINIT_SUPPORT=y
3132
CONFIG_I2C=y
3233
CONFIG_SPI=y

0 commit comments

Comments
 (0)