Olimex Support Forum

OLinuXino Android / Linux boards and System On Modules => A20 => Topic started by: jhuge on September 25, 2020, 07:36:21 pm

Title: [A20 Lime2] PWM on vanilla 5.6 mainline kernel
Post by: jhuge on September 25, 2020, 07:36:21 pm
Hello,

I have a A20 OLinuXino Lime2 board running vanilla 5.6 kernel with PREEMPT_RT patch, and I'm not sure how I should go about to get hardware PWM output on a GPIO pin.

This guide (https://www.olimex.com/wiki/How_to_add_pwm) doesn't seem to apply to my case since I don't have the pwm-sunxi driver config available when compiling the kernel, and I'm not sure how to add it, and if it would work with 5.6 kernel.
I only have pwm_sun4i driver, but my /sys/class/pwm is empty. The pwm entry in the dts tree is disabled, so maybe I would need to add dtoverlay boot parameters, but I'm not sure what command to use.

I would prefer not to use software PWM because I already have hard real time tasks running.

Can anyone help me ?
Title: Re: [A20 Lime2] PWM on vanilla 5.6 mainline kernel
Post by: LubOlimex on September 28, 2020, 04:12:05 pm
In our official images we use PWM for the LCD backlight. Look at our overlays, this is olinuxino-overlay, it might give you some ideas

https://github.com/OLIMEX/olinuxino-overlays/blob/upstream/sun7i-a20/sun7i-a20-pwm.dts

https://github.com/OLIMEX/olinuxino-overlays/blob/master/sun7i-a20/sun7i-a20-pwm.dts

~#olinuxino-overlay - provides easy way to enable or disable different hardware assignments - different I2C, SPI, UART, CAN, PWM, etc;
Title: Re: [A20 Lime2] PWM on vanilla 5.6 mainline kernel
Post by: Danrancan on December 14, 2020, 12:27:41 am
Quote from: LubOlimex on September 28, 2020, 04:12:05 pmIn our official images we use PWM for the LCD backlight. Look at our overlays, this is olinuxino-overlay, it might give you some ideas

https://github.com/OLIMEX/olinuxino-overlays/blob/upstream/sun7i-a20/sun7i-a20-pwm.dts

~#olinuxino-overlay - provides easy way to enable or disable different hardware assignments - different I2C, SPI, UART, CAN, PWM, etc;

Thats a broken link like 87% of the other links on this forum and your site. Might want to remove it or fix it if you can.
Title: Re: [A20 Lime2] PWM on vanilla 5.6 mainline kernel
Post by: LubOlimex on December 14, 2020, 10:51:22 am
Hello,

Thanks for the notice. This happened because that upstream branch was merged to the master branch on October 7th 2020. Dead links are expected - the software and hardware work are in continuous development. You can find the same link now here:

https://github.com/OLIMEX/olinuxino-overlays/blob/master/sun7i-a20/sun7i-a20-pwm.dts

Also edited the original link.
Title: Re: [A20 Lime2] PWM on vanilla 5.6 mainline kernel
Post by: Danrancan on December 24, 2020, 10:05:15 am
Quote from: LubOlimex on December 14, 2020, 10:51:22 amHello,

Thanks for the notice. This happened because that upstream branch was merged to the master branch on October 7th 2020. Dead links are expected

- By whom? Professionals? I don't think dead links are "expected" on any support forum from any company that takes their product support seriously enough to consider themselves professionals, and their products/services, of high enterprise quality.

I'm not attempting to be an meanie or anything here, but I belong to over 30 different support forums for different products, and not one of them is even remotely close to being so poorly maintained and updated. I point this out not to be crude or insulting, but to give you some solid customer feedback, to let you know that it reflects very poorly on olimex, when you cannot properly maintain and update your website/support forums. I am a devops guy myself, and live amongst a community of developers, and one consistency amongst them, is that coders and developers are excellent at what they do (code), but usually they don't share dual strongpoints of being able to code, AND develop/maintain/document things properly for the end users.

So, If I may suggest, you most certainly might want to try hiring someone to keep all of your documentation, forum posts, links, etc. top-notch up to date, so it feel exceptionally professional.

I'm a huge fan of your products and you guys being one of the few open source hardware vendors around. So you have my loyalty because of that. However, I can't give any sort of strong recommendations to any of my associates about your products or your product support, while the attitude of Olimex is that "Dead Links should be expected". It is an unprofessional standard to have, and casts a stigma towards your company, as people start to unintentionally associate the lackluster website maintainence, with you products themselves. So, again, out of pure constructive criticism in good conscience to help you improve your company, I would seriously suggest hiring someone who can maintain and update your site and forum on a regular daily basis.

Quote from: LubOlimex on December 14, 2020, 10:51:22 amthe software and hardware work are in continuous development. You can find the same link now here:

https://github.com/OLIMEX/olinuxino-overlays/blob/master/sun7i-a20/sun7i-a20-pwm.dts
Also edited the original link.

After spending hours trying to gather all of the information from so many different locations, I finally DO realize, that you guys most certainly are hard at work with continuous development, and I appreciate that.

Thank you for the new link, and your update of the original link is also appreciated! Also, thank you, in general, just for your help and for your responses. They are much appreciated!

Danrancan
Title: Re: [A20 Lime2] PWM on vanilla 5.6 mainline kernel
Post by: faraz on May 17, 2022, 04:25:58 am
Hey,

I want to confirm when we enable pwm through overlay, only PB2 gets enabled?(https://github.com/OLIMEX/olinuxino-overlays/blob/master/sun7i-a20/sun7i-a20-pwm.dts)

As per https://github.com/torvalds/linux/blob/master/arch/arm/boot/dts/sun7i-a20.dtsi
I want to enable PWM on PI3 as well.

Could you please share how can that be done?
Or can I get PWM on any GPIO by modifying the dts?

regards
Faraz
Title: Re: [A20 Lime2] PWM on vanilla 5.6 mainline kernel
Post by: faraz on May 17, 2022, 07:29:57 am
Also,

could you please tell if we can use https://developer.ridgerun.com/wiki/index.php/Edit_device_tree_at_run_time this instead of building the dts?

if we can, could you please provide a quick walkthrough?

I disabled the LCD from overlay and was successfully able to use PWM0 to control a servo motor.
However, I would like to use PI3, via shield. PI3 on shield is delivered at pin 12 of GPIO5.
Title: Re: [A20 Lime2] PWM on vanilla 5.6 mainline kernel
Post by: faraz on May 19, 2022, 10:10:49 am
When PWM controller is enabled via Olinuxino-Overlays.
Only 1 PWM which is PWM0 at pin PB2 is enabled.
Below is how I enabled both PWMs. PWM0 at PB2 and PWM1 at PI3. Hope this can help someone.

First I installed device tree compiler using
sudo apt-get install device-tree-compilerThen I modified the code at https://github.com/OLIMEX/olinuxino-overlays/blob/master/sun7i-a20/sun7i-a20-pwm.dts (https://github.com/OLIMEX/olinuxino-overlays/blob/master/sun7i-a20/sun7i-a20-pwm.dts) to look like below

/dts-v1/;
/plugin/;

/ {
compatible = "allwinner,sun7i-a20";
description = "Enable PWM controller";

fragment@0 {
target = <&pio>;
__overlay__ {
pwm0_pin: pwm0-pin {
pins = "PB2";
function = "pwm";
};
};
};

        fragment@1 {
                target = <&pio>;
                __overlay__ {
                        pwm1_pin: pwm1-pin {
                                pins = "PI3";
                                function = "pwm";
                        };
                };
        };

fragment@2 {
target = <&pwm>;
__overlay__ {
pinctrl-names = "default";
pinctrl-0 = <&pwm0_pin>, <&pwm1_pin>;
status = "okay";
};
};
};

Then I compiled the .dts file into a .dtb file.
Code to compile
dtc -I dts -O dtb -o /home/sun7i-a20-pwm.dtbo /*path to the DTS file*/sun7i-a20-pwm.dts
Above code should compile the .dts file to a .dtbo file and place it in home.

Then I moved the .dtbo to /usr/lib/olinuxino-overlays/sun7i-a20/
It should replace the existing sun7i-a20-pwm.dtbo file
Then I rebooted the board and was able to generate PWM on both pins using below
From bash:
# cd /sys/class/pwm/pwmchip0/
# ls
device    export    npwm      power      subsystem  uevent    unexport
# echo 0 > export
The above enables pwm0, which should be at pin PB2
# cd pwm0
#ls
duty_cycle  enable      period      polarity    power      uevent
# echo 10000 > period
# echo 1000 > duty_cycle
# echo 1 > enable
# echo 0 > enable
# cd ../
# echo 1 > export
The above enables pwm1, which should be at pin PI3
# cd pwm1
#ls
duty_cycle  enable      period      polarity    power      uevent
# echo 10000 > period
# echo 1000 > duty_cycle
# echo 1 > enable
# echo 0 > enable

There might be typos in the code, so please double check.