Skip to content

feat(pwmout/gpioout/pca9685): add possibility to configure pwm-channels as gpios - #29409

Open
marinauterion wants to merge 3 commits into
PX4:mainfrom
marinauterion:pr-add-pwm-as-plain-gpios-config
Open

marinauterion wants to merge 3 commits into
PX4:mainfrom
marinauterion:pr-add-pwm-as-plain-gpios-config

Conversation

@marinauterion

@marinauterion marinauterion commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Solved Problem

Implement the ability to control PWM outputs as GPIO (ON/OFF) within PX4. This is required to enable toggling peripherals, such as the airspeed sensor, on and off.
Currently, these pins are directly driven by timers (TIMs) and lack the option for direct GPIO control.

Solution

  • New driver gpio_out drives the FMU channels selected in PWM_MAIN_GPIO_EN / PWM_AUX_GPIO_EN as GPIOs: HIGH if output > (MIN + MAX) / 2, else LOW.
  • pwm_out and dshot skip these channels; gpio_out starts last, so existing actuator_outputs instances are unchanged.
  • Not supported on DShot timers (rejected by gpio_out, hidden in QGC).
  • pca9685_pwm_out: same feature via PCA9685_GPIO_EN.

Tests

While PCA can be changed on the fly, changes for PWM_MAIN need a reboot

PWM MAIN
pwm_as_gpio_2
PCA9685 OUTPUT
pwm_as_gpio
Testing
2026-10-15-13.32.27.423262484.mp4

@github-actions github-actions Bot added kind:feature Request or change that adds new functionality. scope:drivers Device drivers and hardware interfaces. labels Oct 23, 2026
@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

🔎 Flash and RAM Analysis

Target Flash Δ Flash Total Static RAM Δ Static RAM Total
px4_fmu-v2 (F4) 🟡 +200 B (+0.02%) 97.75% +0 B (+0.00%) 10.29%
px4_fmu-v5x (F7) 🟡 +304 B (+0.02%) 96.14% +0 B (+0.00%) 21.95%
px4_fmu-v6x (H7) 🟡 +312 B (+0.02%) 97.63% +0 B (+0.00%) 10.63%
px4_io-v2 (F1) +4 B (+0.01%) 70.79% +4 B (+0.09%) 54.44%

px4_io-v2 firmware image, which an FMU flashes to its IO at boot: 🔴 changed.

px4_fmu-v2 — Bloaty section/symbol breakdown
     VM SIZE    
 -------------- 
  +0.0%    +200    .text
    +0.2%    +144    g_cromfs_image
     +15%     +48    PWMOut::update_pwm_out_state()
    +0.0%     +12    [section .text]
   -30.8%      -4    g_nullstring
  +0.0%    +200    TOTAL

px4_fmu-v5x — Bloaty section/symbol breakdown
     VM SIZE    
 -------------- 
  +0.0%    +304    .text
    +0.1%    +144    g_cromfs_image
     +26%     +48    DShot::initialize_dshot()
     +15%     +48    PWMOut::update_pwm_out_state()
     +29%     +30    CameraInterfaceGPIO::setup()
    [NEW]     +24    CSWTCH.139
    +5.3%     +16    DShot::handle_configure_actuator()
     +39%     +14    DShot::motor_index_from_output()
    [NEW]      +9    CSWTCH.140
    +6.2%      +4    OutputModuleInterface::~OutputModuleInterface()
    +5.6%      +2    CameraInterfaceGPIO::trigger()
    -1.0%      -2    DShot::mixerChanged()
    [DEL]      -9    CSWTCH.138
    [DEL]     -24    CSWTCH.137
  +0.0%    +304    TOTAL

px4_fmu-v6x — Bloaty section/symbol breakdown
     VM SIZE    
 -------------- 
  +0.0%    +312    .text
    +0.1%    +144    g_cromfs_image
     +26%     +48    DShot::initialize_dshot()
     +15%     +48    PWMOut::update_pwm_out_state()
     +29%     +30    CameraInterfaceGPIO::setup()
    [NEW]     +24    CSWTCH.139
    +5.3%     +16    DShot::handle_configure_actuator()
     +39%     +14    DShot::motor_index_from_output()
    [NEW]      +9    CSWTCH.140
    +0.0%      +8    [section .text]
    +6.2%      +4    OutputModuleInterface::~OutputModuleInterface()
    +5.6%      +2    CameraInterfaceGPIO::trigger()
    +2.9%      +2    up_saveusercontext
    -1.0%      -2    DShot::mixerChanged()
    -0.9%      -2    exec_builtin
    [DEL]      -9    CSWTCH.138
    [DEL]     -24    CSWTCH.137
  +0.0%    +312    TOTAL

px4_io-v2 — Bloaty section/symbol breakdown
     VM SIZE    
 -------------- 
  +0.4%      +4    .data
    +100%      +2    _sumd
    +7.7%      +2    channel_allocations
  +0.0%      +4    TOTAL

Updated: 2026-11-26T02:20:12

@github-actions

github-actions Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

No broken links found in changed files.

@marinauterion
marinauterion force-pushed the pr-add-pwm-as-plain-gpios-config branch from 7591dd5 to 580fbf4 Compare October 23, 2026 16:16
@dakejahl

dakejahl commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Claude review on behalf of @dakejahl

Merge after fixes: with any GPIO_EN bit set, gpio_out takes actuator_outputs instance 0 and pushes every existing output driver up one instance, which changes what telemetry and logs report as the vehicle's outputs.

Must-fix

  • ROMFS/px4fmu_common/init.d/rcS:559, src/drivers/gpio_out/GpioOut.hpp:78: gpio_out's MixingOutput advertises actuator_outputs in its constructor (mixer_module.cpp:99) before dshot and pwm_out start, so on fmu-v6s a PWM-only setup shows the GPIO channels instead of the servos in ACTUATOR_OUTPUT_STATUS and SERVO_OUTPUT_RAW_0, DShot plus PWM moves pwm_out to instance 2, which SERVO_OUTPUT_RAW doesn't stream, and adding pca9685 to that moves pwm_out to instance 3, which the logger doesn't record (logged_topics.cpp:171); masking the <prefix>_GPIO_EN channels out of pwm_out's and dshot's channel masks and starting gpio_out after them keeps the existing instances.

Optional

  • src/drivers/gpio_out/GpioOut.cpp:74: bits at or above DIRECT_PWM_OUTPUT_CHANNELS (10 on fmu-v6s) keep _gpio_mask non-zero, so PWM_MAIN_GPIO_EN=1024 leaves the driver running with no pin claimed and no warning; mask the param with (1u << DIRECT_PWM_OUTPUT_CHANNELS) - 1.
  • Tools/module_config/output_groups_from_timer_config.py:204: QGC hides DIS/MIN/MAX on a DShot timer, but gpio_out uses them for its threshold and disarmed level, so a GPIO channel on a DShot timer can only be configured from the parameter list; exempt GPIO channels from that show_if or document it.
  • src/drivers/gpio_out/GpioOut.cpp:109: gpio_out's actuator_outputs carries the disarmed value on every non-GPIO channel, so logs show outputs nothing drives; zero them the way PWMOut::updateOutputs does for unset functions.
  • src/drivers/pwm_out/PWMOut.cpp:78: the PARAM_INVALID guard doesn't change behaviour, because param_get on an invalid handle already left tim_config at 0 and removed the timer's channels; drop it from this PR.
  • src/drivers/gpio_out/Kconfig:1: add depends on DRIVERS_PWM_OUT, because the FUNC/MIN/MAX/DIS params gpio_out reads come from pwm_out's module config and without them the driver silently does nothing.
  • platforms/nuttx/src/px4/rpi/rpi_common/include/px4_arch/io_timer.h:80, platforms/nuttx/src/px4/espressif/esp32/include/px4_arch/io_timer.h:76: gpio_out can't link on RP2040 (no io_timer_allocate_channel/io_timer_unallocate_channel) or ESP32 (declared, never defined), so these enum additions are dead.
  • PR title: the scope names dshot, which this PR doesn't change, and the title becomes the squash commit subject.

@marinauterion marinauterion changed the title feat(pwmout/dshot/pca9685): add possibility to configure pwm-channels as gpios feat(pwmout/gpioout/pca9685): add possibility to configure pwm-channels as gpios Oct 26, 2026
@marinauterion
marinauterion force-pushed the pr-add-pwm-as-plain-gpios-config branch 4 times, most recently from 1bec3f6 to 3a3ced1 Compare October 28, 2026 15:18
@marinauterion
marinauterion force-pushed the pr-add-pwm-as-plain-gpios-config branch from 3a3ced1 to 671ca4e Compare November 2, 2026 15:58
…ls as plain gpios

feat(Tools/module_config/generate_actuators_metadata): implement TODO to skip custom params that are notdefined by any module

Signed-off-by: Marin Doetterer <marin@auterion.com>
- start gpio_out after dshot/pwm_out to keep the existing actuator_outputs instances
- pwm_out and dshot mask out <prefix>_GPIO_EN channels instead of relying on claim order
- reject GPIO channels on DShot timers and bits beyond the board's channel count
- hide the GPIO column in QGC for DShot timers (same show_if as DIS/MIN/MAX)
- report 0 on non-GPIO channels in gpio_out's actuator_outputs
- gpio_out depends on DRIVERS_PWM_OUT
- drop the unused GPIO channel mode on RP2040/ESP32 and the PARAM_INVALID guard in pwm_out
- docs: DShot limitation and boot behaviour

Signed-off-by: Marin Doetterer <marin@auterion.com>
@marinauterion
marinauterion force-pushed the pr-add-pwm-as-plain-gpios-config branch from 671ca4e to cafc5bb Compare November 2, 2026 16:07
@alexcekay

Copy link
Copy Markdown
Member

@hamishwillee: Could you please check the docs part of the change? Thanks a lot.

Review follow-up for the new GPIO Outputs section: add a version
badge, state that dshot does not drive a DShot-timer output selected
as GPIO, say that most firmware lacks gpio_out and how to enable it,
note that PCA9685 changes need no reboot, and link the new parameters
and modules. Also clarify that PWM MAIN holds the FMU outputs on boards
without an I/O board, and use consistent output/checkbox wording.

Assisted-by: Claude:claude-opus-5-5
Signed-off-by: Hamish Willee <hamishwillee@gmail.com>

@hamishwillee hamishwillee left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

@marinauterion @alexcekay Apologies for the delay. I'm swamped and I missed this. I've improved my tracking which is how I found this, but please ping me again if you don't get a response.
If something has to go in urgently and I am the blocker, send me a message in slack.

Firstly, despite all the comments, this is great. Most of it is spotted by my Claude instance, which is set up to check things no one would think of, such as "Does the yaml long descrption repeat the short description and how can I minimise it". So most of this is minor consistency fixes.

I pushed f3dd207 with docs tidy-ups to the GPIO Outputs section:

  • version badge
  • "output"/"checkbox" wording
  • info box rewrite: gpio_out must be in firmware and how to enable it, and dshot doesn't drive a DShot output selected as GPIO
  • PCA9685 changes need no reboot
  • links to the new parameters and modules
  • PWM MAIN tab description

Inline, there's a question on the pin state before gpio_out starts, and suggestions for the parameter descriptions.

Outside this PR's lines:

  • The PCA9685_DUTY_EN long description has grammar errors ("Other standard params follows", "12bits").
  • docs/en/flight_modes_fw/hold.md L64 links boards/auterion/fmu-v6s/default.px4board#L46 for CONFIG_FIGURE_OF_EIGHT. That setting is at L50 after this PR (L49 before it).

Comment on lines +427 to +428
The output keeps its reset level until `gpio_out` starts, which happens after the other output drivers during boot.
It is then set to the level of its disarmed value.

@hamishwillee hamishwillee Nov 26, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What is the pin state before gpio_out starts: floating, or set by the board? If it depends on the board.

Claude suggests this. Push back if it makes no sense on any of this.

Suggested change
The output keeps its reset level until `gpio_out` starts, which happens after the other output drivers during boot.
It is then set to the level of its disarmed value.
Until `gpio_out` starts, which happens after the other output drivers during boot, the pin is in its power-on state (which depends on the board).
It is then set to the level of its disarmed value.

Comment on lines +9 to +10
long: |
Selecting which channels are driven as plain digital (on/off) outputs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Say how the level is chosen and that DShot timers aren't supported; don't duplicate the short description in the long description.

Suggested change
long: |
Selecting which channels are driven as plain digital (on/off) outputs.
long: High if the output is above the midpoint of PWM_MAIN_MINn and PWM_MAIN_MAXn, otherwise low. Not supported on DShot timers.

Comment on lines +9 to +10
long: |
Selecting which channels are driven as plain digital (on/off) outputs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Say how the level is chosen and that DShot timers aren't supported; don't duplicate the short description in the long description.

Suggested change
long: |
Selecting which channels are driven as plain digital (on/off) outputs.
long: High if the output is above the midpoint of PWM_AUX_MINn and PWM_AUX_MAXn, otherwise low. Not supported on DShot timers.

Comment on lines +99 to +100
long: |
Selecting which channels are driven as plain digital (on/off) outputs.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Say how the level is chosen; don't duplicate the short description in the long description.

Suggested change
long: |
Selecting which channels are driven as plain digital (on/off) outputs.
long: On if the output is above the midpoint of PCA9685_MINn and PCA9685_MAXn, otherwise off. Overrides PCA9685_DUTY_EN.

Comment on lines +103 to +118
0: Put CH1 into GPIO mode
1: Put CH2 into GPIO mode
2: Put CH3 into GPIO mode
3: Put CH4 into GPIO mode
4: Put CH5 into GPIO mode
5: Put CH6 into GPIO mode
6: Put CH7 into GPIO mode
7: Put CH8 into GPIO mode
8: Put CH9 into GPIO mode
9: Put CH10 into GPIO mode
10: Put CH11 into GPIO mode
11: Put CH12 into GPIO mode
12: Put CH13 into GPIO mode
13: Put CH14 into GPIO mode
14: Put CH15 into GPIO mode
15: Put CH16 into GPIO mode

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Match the gpio_out bit labels ("in GPIO mode").

Suggested change
0: Put CH1 into GPIO mode
1: Put CH2 into GPIO mode
2: Put CH3 into GPIO mode
3: Put CH4 into GPIO mode
4: Put CH5 into GPIO mode
5: Put CH6 into GPIO mode
6: Put CH7 into GPIO mode
7: Put CH8 into GPIO mode
8: Put CH9 into GPIO mode
9: Put CH10 into GPIO mode
10: Put CH11 into GPIO mode
11: Put CH12 into GPIO mode
12: Put CH13 into GPIO mode
13: Put CH14 into GPIO mode
14: Put CH15 into GPIO mode
15: Put CH16 into GPIO mode
0: Put CH1 in GPIO mode
1: Put CH2 in GPIO mode
2: Put CH3 in GPIO mode
3: Put CH4 in GPIO mode
4: Put CH5 in GPIO mode
5: Put CH6 in GPIO mode
6: Put CH7 in GPIO mode
7: Put CH8 in GPIO mode
8: Put CH9 in GPIO mode
9: Put CH10 in GPIO mode
10: Put CH11 in GPIO mode
11: Put CH12 in GPIO mode
12: Put CH13 in GPIO mode
13: Put CH14 in GPIO mode
14: Put CH15 in GPIO mode
15: Put CH16 in GPIO mode

show_as: bitset
advanced: true
- name: 'GPIO_EN'
label: "GPIO\n Mode"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this match the PWM tab's "GPIO Output" label? (If so, the docs line and screenshot need updating too.)

@hamishwillee

Copy link
Copy Markdown
Contributor

Also, could you please add a release note update in https://docs.px4.io/main/en/releases/main.

I'd love to link this from other places to increase visibility. Perhaps a comment in the intro https://docs.px4.io/main/en/getting_started/px4_basic_concepts#outputs-motors-servos-actuators that links to your new section?

This is "at your discretion" - but IMO it certainly wouldn't hurt.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kind:feature Request or change that adds new functionality. scope:drivers Device drivers and hardware interfaces.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants