Skip to content

core: Add peripheral reinit for pinmux switching#184

Open
soburi wants to merge 1 commit into
zephyrproject-rtos:nextfrom
soburi:device_dynamic_init
Open

core: Add peripheral reinit for pinmux switching#184
soburi wants to merge 1 commit into
zephyrproject-rtos:nextfrom
soburi:device_dynamic_init

Conversation

@soburi
Copy link
Copy Markdown
Member

@soburi soburi commented May 4, 2026

Allows Arduino APIs to switch a pin between GPIO and peripheral functions by re-running the selected device init path when the requested owner changes.

This is an interim pinmux handoff mechanism for boards where sketches may move a pin back and forth between serial/GPIO/PWM-style use.

In practice, this code should be excluding.
Here, for merge management purposes, it's made identical to Arduino, but
the next patch, #186, will make this switchable.

@soburi soburi marked this pull request as ready for review May 4, 2026 00:18
Copilot AI review requested due to automatic review settings May 4, 2026 00:18
@soburi soburi marked this pull request as draft May 4, 2026 00:18
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR introduces a “rough” mechanism to allow switching a given pin back and forth between peripheral functions (UART/SPI/I2C/PWM/ADC/DAC) by re-initializing/de-initializing Zephyr devices when Arduino APIs begin/end or when analog/pinMode operations occur.

Changes:

  • Add explicit device init() calls in Wire.begin(), SPI.begin(), and Serial.begin().
  • Add optional device deinit() calls in Wire.end(), SPI.end(), and Serial.end() when CONFIG_DEVICE_DEINIT_SUPPORT is enabled.
  • Add a per-pin “last device” tracker and _reinit_peripheral_if_needed() helper used by pinMode(), analogWrite(), and analogRead() to re-init peripherals when switching pinmux usage.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
libraries/Wire/Wire.cpp Re-initialize I2C device in begin(); deinit in end() behind CONFIG_DEVICE_DEINIT_SUPPORT.
libraries/SPI/SPI.cpp Re-initialize SPI device in begin(); deinit in end() behind CONFIG_DEVICE_DEINIT_SUPPORT.
cores/arduino/zephyrSerial.h Deinitialize UART device in end() behind CONFIG_DEVICE_DEINIT_SUPPORT.
cores/arduino/zephyrSerial.cpp Re-initialize UART device at start of begin().
cores/arduino/zephyrInternal.h Expose _reinit_peripheral_if_needed() as a C-linkage internal helper.
cores/arduino/zephyrCommon.cpp Implement _reinit_peripheral_if_needed() and call it from pin/analog APIs to re-init pinmux-related peripherals.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cores/arduino/zephyrCommon.cpp
Comment thread libraries/Wire/Wire.cpp
Comment thread libraries/SPI/SPI.cpp
Comment thread cores/arduino/zephyrSerial.cpp
Comment thread cores/arduino/zephyrSerial.h
Comment thread cores/arduino/zephyrCommon.cpp
Allow Arduino APIs to switch a pin between GPIO and peripheral
functions by re-running the selected device init path when
the requested owner changes.

This is an interim pinmux handoff mechanism for boards where
sketches may move a pin back and forth between
serial/GPIO/PWM-style use.

Co-Authored-by: Martino Facchin <m.facchin@arduino.cc>
Co-Authored-by: pennam <m.pennasilico@arduino.cc>
Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
@soburi soburi force-pushed the device_dynamic_init branch from eaaef01 to ad38122 Compare May 4, 2026 01:02
@soburi soburi changed the title core: pinmux: implement rough solution core: add dynamic peripheral init for pinmux switching May 4, 2026
@soburi soburi changed the title core: add dynamic peripheral init for pinmux switching core: Add peripheral reinit for pinmux switching May 4, 2026
@soburi soburi requested a review from Copilot May 4, 2026 01:48
@soburi soburi marked this pull request as ready for review May 4, 2026 01:50
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.


💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cores/arduino/zephyrInternal.h
Comment thread cores/arduino/zephyrCommon.cpp Outdated
Comment thread cores/arduino/zephyrCommon.cpp
Comment thread cores/arduino/zephyrCommon.cpp
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants