Easy Mode GPIO PWM on U3

Moderators: mdrjr, odroid

Easy Mode GPIO PWM on U3

Unread postby mdrjr » Fri May 16, 2014 7:32 am

Starting for today (20140515) I'm including on the kernel software pwm support for GPIO's.
So, you need to update your kernel to take advantage of this.

As explained here: viewtopic.php?f=80&t=4894

You can choose one of the 3 GPIO's or if you have the IO Shield any of the 16 GPIO's on the shield for that.

Its simple as cake..

Code: Select all
root@odroid:~# cd /sys/class/soft_pwm/
root@odroid:/sys/class/soft_pwm# echo 204 > export
root@odroid:/sys/class/soft_pwm# cd pwm204
root@odroid:/sys/class/soft_pwm/pwm204# ls
counter  period  power  pulse  pulses  subsystem  uevent
root@odroid:/sys/class/soft_pwm/pwm204# echo 1000 > period
root@odroid:/sys/class/soft_pwm/pwm204# echo 100 > pulse
root@odroid:/sys/class/soft_pwm/pwm204#


See? now your gpio is a PWM output. And you can get ~20kHz on it.
All values of period/pulse are in uSeconds (microseconds).

u3_soft_pwm_scope.jpg
u3_soft_pwm_scope.jpg (109.73 KiB) Viewed 8866 times
mdrjr
Site Admin
 
Posts: 11640
Joined: Fri Feb 22, 2013 11:34 pm
Location: Brazil
languages_spoken: english, portuguese
ODROIDs: -

Re: Easy Mode GPIO PWM on U3

Unread postby arcltd » Mon Jun 09, 2014 4:14 am

Hi

I'm hoping to get my U3 and IO shield board this week and I want to be able to read voltages through the ADC, I've looked through the magazine February issue and a few other posts but not seen any direct examples of how to read the ADC, can you offer any advice before my stuff turns up.

Thanks
Andy
arcltd
 
Posts: 7
Joined: Mon Jun 09, 2014 1:16 am
Location: Bristol, UK
languages_spoken: english
ODROIDs: U3 + U3 sheild

Re: Easy Mode GPIO PWM on U3

Unread postby mdrjr » Mon Jun 09, 2014 7:15 am

arcltd wrote:Hi

I'm hoping to get my U3 and IO shield board this week and I want to be able to read voltages through the ADC, I've looked through the magazine February issue and a few other posts but not seen any direct examples of how to read the ADC, can you offer any advice before my stuff turns up.

Thanks
Andy


Please create your own thread and we'll take from there :9
mdrjr
Site Admin
 
Posts: 11640
Joined: Fri Feb 22, 2013 11:34 pm
Location: Brazil
languages_spoken: english, portuguese
ODROIDs: -

Re: Easy Mode GPIO PWM on U3

Unread postby Respectech » Mon Jul 07, 2014 12:27 pm

I was having trouble remembering what the port numbers were for the GPIO pins on the I/O Board. I found them in the February 2014 issue of ODROID Magazine, pg 33:

"16 Ports are mapped to GPIO #289~#304 (I/O Shield pin number P00~P07, P10~P17)"
Respectech
 
Posts: 279
Joined: Wed Nov 13, 2013 9:20 am
Location: California
languages_spoken: English, German, some Ukrainian, some Spanish
ODROIDs: All the ODROIDs!!

Well, the ones released since the U2.

Re: Easy Mode GPIO PWM on U3

Unread postby Respectech » Tue Jul 08, 2014 7:07 am

Just a note that it is important to not push it much faster than 1000/100 microseconds for the period/pulse as it can lock up the terminal and cause other weird behavior.

1,000,000 microseconds = 1 second
100,000 microseconds = 0.1 second

1000 microseconds = 0.001 seconds = 1KHz
50 microseconds = 0.000050 seconds = 20KHz

I wasn't able to get it to work properly with even a period/pulse of 1000/100 (10KHz), but greater values seemed to work fine.
Respectech
 
Posts: 279
Joined: Wed Nov 13, 2013 9:20 am
Location: California
languages_spoken: English, German, some Ukrainian, some Spanish
ODROIDs: All the ODROIDs!!

Well, the ones released since the U2.

Re: Easy Mode GPIO PWM on U3

Unread postby alben » Sun Jul 27, 2014 4:18 pm

Hi,

thank you for this new easy approach.

To push GPIO even further, is there any solution to use GPIO entries as Interrupt ?
We only have
root@odroid:/sys/class/soft_pwm/pwm204# ls
counter period power pulse pulses subsystem uevent
but 'edge' that is generally used for interrupts does not appear.

Is there another solution ?
The idea would be to add sensors (PIR sensors for example) that would start applications on alarms.

Thank you for any hint.
Alex
alben
 
Posts: 9
Joined: Fri Jul 18, 2014 6:02 am
languages_spoken: english
ODROIDs: U3

Re: Easy Mode GPIO PWM on U3

Unread postby Digimaster » Sun Jul 27, 2014 4:23 pm

At least it can be emulated by software.
Use pthreads lib to read gpio status in separate thread, or just a fork with pipe to signal main application.
User avatar
Digimaster
 
Posts: 208
Joined: Tue Feb 26, 2013 4:16 pm
Location: Moscow, Russia
languages_spoken: english, russian
ODROIDs: U2, X2, U3, C1, C2, XU4 × 300+

Re: Easy Mode GPIO PWM on U3

Unread postby alben » Sun Jul 27, 2014 8:36 pm

Digimaster wrote:At least it can be emulated by software.
Use pthreads lib to read gpio status in separate thread, or just a fork with pipe to signal main application.


Hi, thank you for the hint.
But is it a battery saving approach ? Interrupts do not imply threads waiting for an even.

There is another possible approach : using a gpio counter that regularly run callback that would checks the gpio pin state... is it possible on this plateforme ?

Which solution is the most battery saving ?
Thanks
alben
 
Posts: 9
Joined: Fri Jul 18, 2014 6:02 am
languages_spoken: english
ODROIDs: U3

Re: Easy Mode GPIO PWM on U3

Unread postby Digimaster » Mon Jul 28, 2014 7:30 pm

Sure, timers can be used as well. Btw threads is good enough for power saving.
It can check gpio from time to time going sleep in between, so it can be 99,9% in sleeping state.
User avatar
Digimaster
 
Posts: 208
Joined: Tue Feb 26, 2013 4:16 pm
Location: Moscow, Russia
languages_spoken: english, russian
ODROIDs: U2, X2, U3, C1, C2, XU4 × 300+

Re: Easy Mode GPIO PWM on U3

Unread postby alben » Tue Jul 29, 2014 4:14 pm

Digimaster wrote:Sure, timers can be used as well. Btw threads is good enough for power saving.
It can check gpio from time to time going sleep in between, so it can be 99,9% in sleeping state.


Yep, this is a very good argument, i will play with this solution, thanks.

One last question : any news about the ioshield support (gpio access, etc.) from Android ?

Thanks
alben
 
Posts: 9
Joined: Fri Jul 18, 2014 6:02 am
languages_spoken: english
ODROIDs: U3

Re: Easy Mode GPIO PWM on U3

Unread postby odroid » Tue Jul 29, 2014 4:50 pm

alben wrote:One last question : any news about the ioshield support (gpio access, etc.) from Android ?


Refer this link.
viewtopic.php?f=51&t=5846
User avatar
odroid
Site Admin
 
Posts: 25312
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID

Re: Easy Mode GPIO PWM on U3

Unread postby chens » Wed Apr 29, 2015 1:55 am

is there any example(c/c++) code ? i need do it in my code.
chens
 
Posts: 20
Joined: Thu Jul 24, 2014 3:51 pm
languages_spoken: english
ODROIDs: odroid xu,odroid xu3,odroid u3,odroid xu_lite

Re: Easy Mode GPIO PWM on U3

Unread postby mdrjr » Tue Jun 16, 2015 6:15 am

use fopen() and fwrite() on the files above.
mdrjr
Site Admin
 
Posts: 11640
Joined: Fri Feb 22, 2013 11:34 pm
Location: Brazil
languages_spoken: english, portuguese
ODROIDs: -

Re: Easy Mode GPIO PWM on U3

Unread postby netstv » Sat Jun 27, 2015 2:46 am

I have set everything up with the latest ODRIOD U3 and rev 0.5 of the io shield board.

I have a setup script file:
Code: Select all
#!/bin/bash

# The i2c-4 is not loaded by default.  So need to load it.
modprobe i2c_gpio_custom bus0=4,200,199

# now load the gpio module
sudo modprobe gpio-pca953x

# Set the I2C address of the module
echo tca6416 0x20 > /sys/devices/platform/i2c-gpio.4/i2c-4/new_device

cd /sys/class/soft_pwm
echo 289 > export


Then I try to do:

echo 1000 > period and I get a crash.
Code: Select all
root@odroid:/sys/class/soft_pwm/pwm289# echo 1000 > period
root@odroid:/sys/class/soft_pwm/pwm289#[  200.766884] ------------[ cut here ]------------
[  200.771293] WARNING: at kernel/mutex.c:198 __mutex_lock_slowpath+0x2c0/0x344()
[  200.778485] Modules linked in: gpio_pca953x i2c_gpio_custom ipv6 rfcomm bnep bluetooth smsc95xx usbnet gpio_keys fuse vfat fat
[  200.789852] [<c0014f78>] (unwind_backtrace+0x0/0xf8) from [<c0024b64>] (warn_slowpath_common+0x4c/0x64)
[  200.799217] [<c0024b64>] (warn_slowpath_common+0x4c/0x64) from [<c0024b98>] (warn_slowpath_null+0x1c/0x24)
[  200.808844] [<c0024b98>] (warn_slowpath_null+0x1c/0x24) from [<c0428c9c>] (__mutex_lock_slowpath+0x2c0/0x344)
[  200.818732] [<c0428c9c>] (__mutex_lock_slowpath+0x2c0/0x344) from [<c0428d2c>] (mutex_lock+0xc/0x24)
[  200.827840] [<c0428d2c>] (mutex_lock+0xc/0x24) from [<bf0fc1c4>] (pca953x_gpio_set_value+0x1c/0x70 [gpio_pca953x])
[  200.838165] [<bf0fc1c4>] (pca953x_gpio_set_value+0x1c/0x70 [gpio_pca953x]) from [<c01ea750>] (__gpio_set_value+0x60/0xb0)
[  200.849086] [<c01ea750>] (__gpio_set_value+0x60/0xb0) from [<c01ed310>] (soft_pwm_hrtimer_callback+0xcc/0x19c)
[  200.859060] [<c01ed310>] (soft_pwm_hrtimer_callback+0xcc/0x19c) from [<c0044220>] (__run_hrtimer.isra.20+0x44/0xd0)
[  200.869467] [<c0044220>] (__run_hrtimer.isra.20+0x44/0xd0) from [<c0044980>] (hrtimer_interrupt+0x108/0x2b4)
[  200.879267] [<c0044980>] (hrtimer_interrupt+0x108/0x2b4) from [<c0044b90>] (hrtimer_peek_ahead_timers+0x10/0x18)
[  200.889414] [<c0044b90>] (hrtimer_peek_ahead_timers+0x10/0x18) from [<c002bc1c>] (__do_softirq+0xc8/0x188)
[  200.899040] [<c002bc1c>] (__do_softirq+0xc8/0x188) from [<c002bd08>] (run_ksoftirqd+0x2c/0x44)
[  200.907627] [<c002bd08>] (run_ksoftirqd+0x2c/0x44) from [<c00485f8>] (smpboot_thread_fn+0x164/0x238)
[  200.916734] [<c00485f8>] (smpboot_thread_fn+0x164/0x238) from [<c004043c>] (kthread+0xa4/0xb0)
[  200.925319] [<c004043c>] (kthread+0xa4/0xb0) from [<c000e398>] (ret_from_fork+0x14/0x3c)
[  200.933381] ---[ end trace 9d951ee46edea562 ]---



I know the gpio is setup properly to my device because if I just set the GPIO,
Code: Select all
for gpio_n in `seq 289 304`
do
echo $gpio_n > /sys/class/gpio/export
done



If I call the above for loop setting up the /sys/class/gpio/gpio289
and then try to export the echo 289 > /sys/class/soft_pwm/export

I get
Code: Select all
echo: write error: Device or resource busy


I haven't looked at the code yet, going to do that shortly, but I'm thinking I'm setting something up incorrectly...

Is there something I'm missing where I need to setup the GPIO as an output?

Thanks for any help you can give me.

-stv
netstv
 
Posts: 12
Joined: Fri Feb 20, 2015 9:13 am
languages_spoken: english
ODROIDs: ODROID-U3

Re: Easy Mode GPIO PWM on U3

Unread postby mdrjr » Sat Jun 27, 2015 7:16 am

If you don't use the softpwm and poke the gpio's manually it works?
mdrjr
Site Admin
 
Posts: 11640
Joined: Fri Feb 22, 2013 11:34 pm
Location: Brazil
languages_spoken: english, portuguese
ODROIDs: -

Re: Easy Mode GPIO PWM on U3

Unread postby netstv » Mon Jun 29, 2015 2:52 am

Yes that is correct.

So I know the GPIO is setup properly and that everything at that point works.
netstv
 
Posts: 12
Joined: Fri Feb 20, 2015 9:13 am
languages_spoken: english
ODROIDs: ODROID-U3

Re: Easy Mode GPIO PWM on U3

Unread postby mdrjr » Mon Jun 29, 2015 7:29 am

I need to test that.. when I wrote that guide I didn't tested the softpwm using the IO expansion board.
mdrjr
Site Admin
 
Posts: 11640
Joined: Fri Feb 22, 2013 11:34 pm
Location: Brazil
languages_spoken: english, portuguese
ODROIDs: -

Re: Easy Mode GPIO PWM on U3

Unread postby netstv » Tue Jul 28, 2015 6:20 am

Hi Mdrjr --

Have you tried to load the tca6416 with the latest 3.8.13.30 kernel?

I'm having problems with it and I want to debug and figure out the PWM issue. Being that I only work on this in my spare time, I thought I'd ask you while I'm trying to figure out what is going on.... Once I get it loading, I'm sure I'll figure out what is going on with the PWM.

I have setup the tca6416 as was described in the magazine article (I think Feb '14). So I have created an /etc/modules/tca6416.conf which calls a script /usr/sbin/tca6416init.sh file that I created.

Works great with the 3.8.13.28 zImage but when I change it to the 3.8.13.30 zImage it fails.

I'm still hunting down what the actual error is, but there has to be something that I need to do to the kernel config (just a guess).

BTW: I do have the proper /lib/modules loaded just an FYI.

Thanks in advance..

-stv
netstv
 
Posts: 12
Joined: Fri Feb 20, 2015 9:13 am
languages_spoken: english
ODROIDs: ODROID-U3

Re: Easy Mode GPIO PWM on U3

Unread postby mdrjr » Tue Jul 28, 2015 6:48 am

Hello netstv,

This is how I set it up here:

Code: Select all
modprobe i2c-dev
modprobe i2c_gpio_custom bus0=4,200,199
echo tca6416 0x20 > /sys/devices/platform/i2c-gpio.4/i2c-4/new_device
mdrjr
Site Admin
 
Posts: 11640
Joined: Fri Feb 22, 2013 11:34 pm
Location: Brazil
languages_spoken: english, portuguese
ODROIDs: -

Re: Easy Mode GPIO PWM on U3

Unread postby netstv » Tue Jul 28, 2015 6:54 am

Yup...

and then I do:

Code: Select all
root@odroid: cd /sys/class/soft_pwm
root@odroid:/sys/class/soft_pwm# echo 289 > export
-bash: echo: write error: Unknown error 517


now I'm trying to find out what in the heck that error 517 is.
netstv
 
Posts: 12
Joined: Fri Feb 20, 2015 9:13 am
languages_spoken: english
ODROIDs: ODROID-U3

Re: Easy Mode GPIO PWM on U3

Unread postby mdrjr » Tue Jul 28, 2015 7:01 am

GPIO's from the shield are base 340.

GPIO1 on the shield is 341
GPIO2 is 342... and so on..
mdrjr
Site Admin
 
Posts: 11640
Joined: Fri Feb 22, 2013 11:34 pm
Location: Brazil
languages_spoken: english, portuguese
ODROIDs: -

Re: Easy Mode GPIO PWM on U3

Unread postby netstv » Wed Jul 29, 2015 2:25 am

Oh duh! Your right.. .but now I screwed up my zImage and it doesn't boot...

I'll keep you up to date on my progress :ugeek:
netstv
 
Posts: 12
Joined: Fri Feb 20, 2015 9:13 am
languages_spoken: english
ODROIDs: ODROID-U3

Re: Easy Mode GPIO PWM on U3

Unread postby odroid » Wed Jul 29, 2015 10:35 am

User avatar
odroid
Site Admin
 
Posts: 25312
Joined: Fri Feb 22, 2013 11:14 pm
languages_spoken: English
ODROIDs: ODROID

Re: Easy Mode GPIO PWM on U3

Unread postby netstv » Wed Jul 29, 2015 1:00 pm

Yeah I know.. if you see from my earlier posts I have it right.. I was just ... well being stupid.
netstv
 
Posts: 12
Joined: Fri Feb 20, 2015 9:13 am
languages_spoken: english
ODROIDs: ODROID-U3

Re: Easy Mode GPIO PWM on U3

Unread postby vbextreme » Thu Sep 10, 2015 11:40 pm

max precision pwm at 10khz?
Easy framework framework for c language
EasyGCC Color output of gcc
MagixCubic cli puzzle game
Bigt Google translate for bash with notify
vbextreme
 
Posts: 80
Joined: Sat Jul 19, 2014 6:43 pm
languages_spoken: english,italiano
ODROIDs: U3,IO shields,Wi-Fi


Return to Hardware and peripherals

Who is online

Users browsing this forum: No registered users and 1 guest