Pixhawk - Enabling GPIO output

Dear All

I have searched the forum and elsewhere but haven’t found a clear answer on how to use a PWM pin ( or other?) as a digital output ( would be curious about enabling input too, but not needed at this time)

Would anyone be able to point me to some documentation or send me an example code that does this?.

Is this something that can be done via the configuration files in init.d or is it something that needs to be hardcoded and compiled?

Thanks !
Eduardo

I don’t think there are any user configurable GPIO pins. Look at px4fmu_led.c to see how to write code which uses outputs pins, and any board_config.h (in src/drivers/boards) to see how the GPIOs are configured.

Thanks Mark
If I wanted to use an AUX OUT pin, would it be enough to not enable PMW_AUX in the init.d/ startup script and then develop an application ( I mean such as https://dev.px4.io/tutorial-hello-sky.html) that subscribes to whatever channel is meant to drive the digital output?

Or are PWM_AUX pins always controlled by the main PX4 application, i.e. not available for other use?

Is any of this Pixhawk/Ardupilot info applicable in the PX4? ( Scroll to the very bottom of the page)
http://ardupilot.org/copter/docs/common-pixhawk-overview.html

Thanks

look in fmu.cpp to see how the AUX pwm output pins are handled. You can probably get away with disabling all AUX pwm outputs, but note that there are other things like camera trigger which can be set up to use those pins also.

Would I disable the AUX pwm outputs by not calling
set PWM_AUX in the init script?

Or would I have to modify fmu.cpp so that it calls up_pwm_servo_deinit() ?
/* make sure servos are off */
up_pwm_servo_deinit();

thanks

You can probably just edit rcS to start fmu.cpp with no (or less than 6) PWM outputs enabled. Look at the mode_pwm options. That’s assuming you’re using fmu-v2, v3 or v4

Hello together,

I´m also struggeling with this AUX Pin enabling for digital Input&Output on the Pixhawk.
I currently try to enable it with stm32_gpio…

But I did not find which Port and which Pin they belong to. According to the pixhawk guide (see also link above), those are virtual Pins 50 to 55.

I would be very happy to get even a hint how to manage this.
I work with Matlab SFunction (= c-code)

PS: I used stm32_… comands to set all ports to output and 0 and another time to one. But couldn’t measure any difference at the output pins…

Thanks ahead =)

@dagar helped me get this working, it turns out it’s quite simple

in the rCS script insert these two lines
set FMU_MODE pwm4 set AUX_MODE pwm4
after the camera part, i.e. in line 472

These calls reserve the first 4 pins in FMU ( AUX PWM) to be used as pwm and the two remaining to be used as GPIO

And then if you wanted to toggle AUX6 you would first call:
px4_arch_configgpio(GPIO_GPIO5_OUTPUT)

to configure the pin as Output (or GPIO_GPIO5_INPUT to configure as input.)

The GPIO0-GPIO5 correspond to AUX1-AUX6.
https://github.com/PX4/Firmware/blob/master/src/drivers/boards/px4fmu-v2/board_config.h#L178

You would place this code in the constructor of a class you’re using (for example) as you only want to configure once.

And then call this where needed in your code
px4_arch_gpiowrite(GPIO_GPIO5_OUTPUT,value);
or for an input
px4_arch_gpioread(GPIO_GPIO5_INPUT);

Hope this helps

3 Likes

Hi and thanks for the answer.

But I did not make it yet to enable it.
I Think I also have a problem with including the right headers.

#include <math.h>
# ifndef MATLAB_MEX_FILE
	#include <stdio.h>
	#include <fcntl.h>
	#include <sys/types.h>
	#include <sys/stat.h>
	#include "stm32_gpio.h"
	#include "drivers/drv_gpio.h"
	//#include "drivers/boards/px4fmu-v2/board_config.h"
# endif

I could not manage to include the last boar_config file, due to some error while building my function.
I tried to use the definition from drv_gpio.h
# define GPIO_SERVO_6 (1<<5) /**< servo 6 output */
but I think it is probably the wrong one.

I also inserted the set FMU and Set AUX_Mode, but it did not work.
(It also threw me an error. I think pmw4 was not know. - and I did not have a camera part in this referred code??..)
But regarding this, as far as I understand the webpage, I should be able to use the aux pins 5 &6 anyway due to default virtual pin setup.

Also the commands px_arch_gpio don’t exist for me - why? Is there a way to determine which board I exactly have oir what am I missing? I used:

    stm32_gpioinit();
    stm32_unconfiggpio(GPIO_SERVO_6);
    stm32_configgpio(GPIO_SERVO_6);
    stm32_gpiowrite(GPIO_SERVO_6,1);

Well… thanks ahead again and I wrote so late because I tried … but yeah. Did not work

That sounds odd… just to be sure, what board are you using?

This one…

That is the same board I’m using, the Pixhawk.

I didn’t have to include any header files, the default ones worked fine.

Strange also that
set FMU_MODE pwm4
would throw an error and that you can’t find the camera section

Are you using a recent version of source code ?
Are you building with make make px4fmu-v2_default ?

Sorry typo, should be
make px4fmu-v2_default

Just for reference in case someone comes across this thread there’s now parameter configurable GPIO usage e.g. for camera triggering: https://docs.px4.io/master/en/peripherals/camera.html#trigger-hardware-configuration-hardware-setup

1 Like

For anybody coming across this thread years later like me and need to enable GPIO output, this is what worked for me:

Add these lines to the appropriate board_config.h file (e.g. boards/px4/fmu-v5/board_config.h) and modify them for the pins you want to use.

#define MAIN_OUT_6            /* PD14 */ (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|GPIO_OUTPUT_SET|GPIO_PORTD|GPIO_PIN14)
#define MAIN_OUT_7            /* PH6  */ (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_50MHz|GPIO_OUTPUT_SET|GPIO_PORTH|GPIO_PIN6)

This defines the output modes of the GPIO pins you are interested in.
These lines specifically enable GPIO push-pull output on the MAIN_OUT pins 6 and 7 of the Pixhawk 4 mini.
According to: Pixhawk4Mini_pinout, these pins correspond to FMU_CH6 and FMU_CH7,
which corresponds to “Port D, Pin 14” and “Port H, Pin 6” according to: FMUv5_stm32_pinout

Now, in your own app like px4_simple_app.c from Hello Sky Tutorial, add the following code to configure the pins first:

px4_arch_configgpio(MAIN_OUT_6);
px4_arch_configgpio(MAIN_OUT_7);

Use this line to make the pin HIGH (3.3V):

px4_arch_gpiowrite(MAIN_OUT_6, 1);

Use this line to make the pin LOW (0V):

px4_arch_gpiowrite(MAIN_OUT_6, 0);
4 Likes

@Murray_Louw Im actually trying your method now for Pixhawk 4. I think I am configuring it right but not able to control the gpios… hmmm. Im trying to make ti blink

On the board_config.h

And on my App

EDIT…
This was actualy correct I was plugging my LED IO PWM OUT instead of the FMU PWM OUT… My Bad

Do you have the excel sheet of pinout for pixhawk4 holybro ?

For anyone trying to reproduce this. The correct GPIO pin and port can be found inside
PX4-Autopilot/boards/../../src/timer_config.cpp

e.g. Using the auxiliary pins of the durandal-v1 flight controller:
from PX4-Autopilot/boards/holybro/durandal-v1/src/timer_config.cpp

constexpr timer_io_channels_t timer_io_channels[MAX_TIMER_IO_CHANNELS] = {
	initIOTimerChannel(io_timers, {Timer::Timer1, Timer::Channel4}, {GPIO::PortE, GPIO::Pin14}),
	initIOTimerChannel(io_timers, {Timer::Timer1, Timer::Channel3}, {GPIO::PortA, GPIO::Pin10}),
	initIOTimerChannel(io_timers, {Timer::Timer1, Timer::Channel2}, {GPIO::PortE, GPIO::Pin11}),
	initIOTimerChannel(io_timers, {Timer::Timer1, Timer::Channel1}, {GPIO::PortE, GPIO::Pin9}),
	initIOTimerChannel(io_timers, {Timer::Timer4, Timer::Channel2}, {GPIO::PortD, GPIO::Pin13}),

now define the desired pins inside board_config.h

#define AUX_OUT_1 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_100MHz|GPIO_OUTPUT_SET|GPIO_PORTE|GPIO_PIN14)
#define AUX_OUT_2 (GPIO_OUTPUT|GPIO_PUSHPULL|GPIO_SPEED_100MHz|GPIO_OUTPUT_SET|GPIO_PORTA|GPIO_PIN10)

Hello,

I’ve been able to control all FMU GPIO pins successfully, but I was wondering if its possible to control the GPIO pins for the IO processor (px4io). I’m using V6C hardware and I’m getting an error when I’m trying to use these pins.

Here’s what I’ve done:

I’ve added this definitions in boards/px4/io-v2/src/board_config.h to make it more readable:

#define IO_CH1 GPIO_PWM1
#define IO_CH2 GPIO_PWM2
#define IO_CH3 GPIO_PWM3
#define IO_CH4 GPIO_PWM4
#define IO_CH5 GPIO_PWM5
#define IO_CH6 GPIO_PWM6
#define IO_CH7 GPIO_PWM7
#define IO_CH8 GPIO_PWM8

I tried to include board_config for the io processor in the iridium module:

#include <boards/px4/io-v2/src/board_config.h>

This is where I’m getting the error when I’m trying to build:

I’ve added the configgpio in main:

	px4_arch_configgpio(FMU_CH1);
	px4_arch_configgpio(FMU_CH2);
	px4_arch_configgpio(IO_CH1);

Then since I’m playing with the iridium driver I added a toggle like feature in one of the function:

        px4_arch_gpiowrite(FMU_CH1, 1);
	px4_usleep(2000000);
	px4_arch_gpiowrite(FMU_CH1, 0);

	px4_arch_gpiowrite(FMU_CH2, 1);
	px4_usleep(1000000);
	px4_arch_gpiowrite(FMU_CH2, 0);

	px4_arch_gpiowrite(IO_CH1, 1);
	px4_usleep(10000);
	px4_arch_gpiowrite(IO_CH1, 0);

You can see my code for FMU_CH1 and CH2 that was working quite well:

image

1 Like

This is firmware that runs at IO MCU:

And this is how FMU controls it:

1 Like