frasersdev / librpip

A Library for using Raspberry Pi Peripherals in user space.
http://librpip.frasersdev.net/
GNU General Public License v2.0
3 stars 3 forks source link

Period too small #12

Open icare-petibles opened 7 years ago

icare-petibles commented 7 years ago

Hi, I use RPi3 witch librpip (Raspbian Jessie + Pixel). With :


echo 20000 > /sys/class/pwm/pwmchip0/pwm0/period
echo 5000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 >  /sys/class/pwm/pwmchip0/pwm0/enable

```I have a frequency of 500 Hz and not 50 kHz.
Same problem with a RPi Zero v1.3 (Raspbian Jessie lite)
Where is my mistake?
Thanks
frasersdev commented 7 years ago

Hi,

the driver expects the period to be in nano seconds (ns) or 10 -9 so 500hz has a period of: = 1/500s = 0.002s = 2ms = 2,000us = 2,000,000ns

icare-petibles commented 7 years ago

Hi, Thank you for the answer I have no problem with the parameters of period and duty. When I enter the following data:

echo 2000000 > /sys/class/pwm/pwmchip0/pwm0/period
echo 500000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 >  /sys/class/pwm/pwmchip0/pwm0/enable

II have the following results:

period = 165 ms (6.07 Hz)
duty = 40 ms

With the following data:

echo 20000 > /sys/class/pwm/pwmchip0/pwm0/period
echo 5000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 >  /sys/class/pwm/pwmchip0/pwm0/enable

II have the following results:

period = 1.65 ms (606 Hz)
duty = 0.4 ms

If I want a period of 2 ms (500 Hz), I must enter the following data:

echo 24000 > /sys/class/pwm/pwmchip0/pwm0/period
echo 6000 > /sys/class/pwm/pwmchip0/pwm0/duty_cycle
echo 1 >  /sys/class/pwm/pwmchip0/pwm0/enable

II have the following results:

period = 2 ms (500 Hz)
duty = 0.5 ms

The hard works normally because with another tutorial (Jumpnow) I have the right values. But I want to use your library (without going through sudo) Thank you for helping me and a hello from France

frasersdev commented 7 years ago

Ok, I can reproduce the problem on a PI2B (running 4.4.9-v7+ #884 SMP)

$ cd /sys/class/pwm/pwmchip0/pwm0 
$ echo "100000000" > period
$ echo "50000000" > duty_cycle
$ echo "1" > enable

gives a period of ~8s (~80x to high, same as your results).

How are you starting the PWM clock? What version of raspbian are you using?

The driver for the PWM hardware is maintained here: https://github.com/raspberrypi/linux/blob/rpi-4.9.y/drivers/pwm/pwm-bcm2835.c You'll notice the period and duty cycle are scaled based on the clock rate (lines 84/85). This is what I suspect is failing.

icare-petibles commented 7 years ago

Hi, Ok.

How are you starting the PWM clock?

Yes, via librpip-pwm-init

What version of raspbian are you using?

Raspbian 4.4.50-v7 #970SMP

How, i can solve my problem:

Thanks and Greetings

frasersdev commented 7 years ago

The problem is either the way the pwm clock is being started (librpip issue) or the way the driver is calculating the period (linux kernel issue) - in either case its going to take a little bit of time to research, test, update etc.

So I would suggest the best thing to do right now is to apply a correction factor.

I'll update this issue with my findings of the librpip side of things.

frasersdev commented 7 years ago

it does look like the problem is with the librpip 'pwmclk' utility

you can start the PWM clock another way to work around the issue. edit the /usr/local/bin/librpip-util/librpip-pwm-init file so that it matches this:

#!/bin/sh

#start the PWM clock with our binary
#/usr/local/bin/librpip-util/pwmclk

#start the PWM clock by sending some audio :-/
speaker-test -Dhw:CARD=ALSA,DEV=0 -f300 -c1 -p100 -P2 -l1 > /dev/null 2>&1

reboot and your timings should be ok.

icare-petibles commented 7 years ago

Hi, Thanks for the answers. I started the pwm clock with analog audio output in librpip-pwm-init. And in raspi-config -> Advanced Options -> Audio -> Force 3.5 mm ('headphone') jack. And it works correctly. With Advanced Options -> Audio -> Auto or Force HDMI, it does not work. I will be able to continue my pwm tests. How can I be notified if there is an update of the library? Thanks again for this help.