Skip to content

Commit 293d125

Browse files
committed
core: Add option to reinit devices before Arduino use
Add ARDUINO_DEVICE_REINIT_ON_USE to optionally call the underlying Zephyr device init callback before Arduino peripheral operations. This keeps the current pinmux workaround opt-in while boards move toward a proper pin/function handoff model. Signed-off-by: TOKITA Hiroshi <tokita.hiroshi@gmail.com>
1 parent a8c133e commit 293d125

6 files changed

Lines changed: 27 additions & 0 deletions

File tree

Kconfig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,16 @@ config ARDUINO_MAX_TONES
4242
If set to a negative value, the maximum number will be determined from the
4343
system's digital pin configuration.
4444

45+
config ARDUINO_DEVICE_REINIT_ON_USE
46+
bool "Reinitialize devices before Arduino peripheral use"
47+
help
48+
Call the underlying Zephyr device init callback before using
49+
Arduino peripherals such as Serial, SPI, Wire, ADC, DAC, or PWM.
50+
51+
This is a temporary workaround for boards that need to re-apply
52+
peripheral pinmux settings when a sketch switches a pin between GPIO
53+
and another function.
54+
4555
module = ARDUINO_API
4656
module-str = arduino_api
4757
source "subsys/logging/Kconfig.template.log_config"

cores/arduino/zephyrCommon.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
#include <zephyr/spinlock.h>
1111

12+
#ifdef CONFIG_ARDUINO_DEVICE_REINIT_ON_USE
1213
// create an array of arduino_pins with functions to reinitialize pins if needed
1314
static const struct device *pinmux_array[DT_PROP_LEN(DT_PATH(zephyr_user), digital_pin_gpios)] = {
1415
nullptr};
@@ -21,6 +22,10 @@ void _reinit_peripheral_if_needed(pin_size_t pin, const struct device *dev) {
2122
}
2223
}
2324
}
25+
#else
26+
void _reinit_peripheral_if_needed(pin_size_t pin, const struct device *dev) {
27+
}
28+
#endif
2429

2530
static const struct gpio_dt_spec arduino_pins[] = {
2631
DT_FOREACH_PROP_ELEM_SEP(

cores/arduino/zephyrSerial.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,9 @@ void arduino::ZephyrSerial::begin(unsigned long baud, uint16_t conf) {
5858
.flow_ctrl = UART_CFG_FLOW_CTRL_NONE,
5959
};
6060

61+
#ifdef CONFIG_ARDUINO_DEVICE_REINIT_ON_USE
6162
uart->ops.init(uart);
63+
#endif
6264

6365
uart_configure(uart, &config);
6466
uart_irq_callback_user_data_set(uart, arduino::ZephyrSerial::IrqDispatch, this);

cores/arduino/zephyrSerial.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,12 @@ class ZephyrSerial : public HardwareSerial {
7676
}
7777

7878
void end() {
79+
#ifdef CONFIG_ARDUINO_DEVICE_REINIT_ON_USE
7980
#ifdef CONFIG_DEVICE_DEINIT_SUPPORT
8081
if (uart->ops.deinit) {
8182
uart->ops.deinit(uart);
8283
}
84+
#endif
8385
#endif
8486
}
8587

libraries/SPI/SPI.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,15 +118,19 @@ void arduino::ZephyrSPI::detachInterrupt() {
118118
}
119119

120120
void arduino::ZephyrSPI::begin() {
121+
#ifdef CONFIG_ARDUINO_DEVICE_REINIT_ON_USE
121122
spi_dev->ops.init(spi_dev);
123+
#endif
122124
}
123125

124126
void arduino::ZephyrSPI::end() {
127+
#ifdef CONFIG_ARDUINO_DEVICE_REINIT_ON_USE
125128
#ifdef CONFIG_DEVICE_DEINIT_SUPPORT
126129
if (spi_dev->ops.deinit) {
127130
spi_dev->ops.deinit(spi_dev);
128131
}
129132
#endif
133+
#endif
130134
}
131135

132136
#if DT_NODE_HAS_PROP(DT_PATH(zephyr_user), spis)

libraries/Wire/Wire.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,22 @@ arduino::ZephyrI2C::ZephyrI2C(const struct device *i2c) : i2c_dev(i2c) {
1515

1616
void arduino::ZephyrI2C::begin() {
1717
ring_buf_init(&rxRingBuffer.rb, sizeof(rxRingBuffer.buffer), rxRingBuffer.buffer);
18+
#ifdef CONFIG_ARDUINO_DEVICE_REINIT_ON_USE
1819
i2c_dev->ops.init(i2c_dev);
20+
#endif
1921
}
2022

2123
void arduino::ZephyrI2C::begin(uint8_t slaveAddr) {
2224
}
2325

2426
void arduino::ZephyrI2C::end() {
27+
#ifdef CONFIG_ARDUINO_DEVICE_REINIT_ON_USE
2528
#ifdef CONFIG_DEVICE_DEINIT_SUPPORT
2629
if (i2c_dev->ops.deinit) {
2730
i2c_dev->ops.deinit(i2c_dev);
2831
}
2932
#endif
33+
#endif
3034
}
3135

3236
void arduino::ZephyrI2C::setClock(uint32_t freq) {

0 commit comments

Comments
 (0)