How to use PWM pin as GPIO

Hello everyone,

I am having some trouble using the PWM1 pin as a GPIO. I can use the PWM1 on that pin and I have no problem exporting the pin itself as a GPIO. However no GPIO changes seems to affect the pin. I have tried to disable the PWM1 (a.k.a pwm2) on the Device Tree and the problem still persists (also tried to disable all PWM devices just to make sure).

Have you also set a pinmux for thab pin? Please check this article.

Hello @alex.tx,

Yes, this is exactly what I was missing!

In the future, it will be necessary for me to change between PWM and GPIO on that pin during runtime. I know that this is possible because I can do this on the GPIO Tool software that comes with the Linux image provided by Toradex. However, I could not figure out a way of doing that. Could you also direct me on how to do that?

hi @farina

The Gpio Tool uses a hack to change the Pinmuxing which should not be used for production. In future using DeviceTree Overlays could be a possible solution.

For the current Bsp i suggest you define the Pin as PWM and set 0 duty cycle for Off and 100% duty cycle for On.

Best regards,
Jaski

Hello @jaski.tx,

In my case changing the duty cycle from 0 to 100% do not solve my issue, I have already tested this and there is a considerable time difference between it and the GPIO. Also, I will have to switch some other pins between GPIO and SPI. This is something that I can not change because it is a product constraint. It uses one SPI_2 pins obtain some of the device’s configurations and, also, to multiplex a SPI_1 transmission. Is there a way to do this on Linux in runtime?

Dear @farina

Unfortunately currently there is no “proper” way to change the pinmuxing under Linux. However, what you can do is to use the debugfs to change the muxing. This can be done in:

/sys/kernel/debug/pinctrl/iomuxc/pinconf-config

It should work somehow like that but it didn’t work for me (maybe you have more luck):

echo 'modify config_pin 5d030000.pwm default SC_P_GPT0_COMPARE 0000020' > pinconf-config

As an alternative you can try to directly modify the pin mux values via “/dev/mem”. There is a simple program called devmem2 available where you can directly access memory:
https://bootlin.com/pub/mirror/devmem2.c

Regards,
Stefan

Thanks @stefan_e.tx

I will look into it!

You are welcome. Let us know your results.