Generate clock on timer7 (P8_8)pin of beaglebone.

316 views
Skip to first unread message

sayalik...@gmail.com

unread,
Jan 28, 2019, 7:52:45 AM1/28/19
to BeagleBoard
Hello..
     I am first time work on beaglebone.I want to generate Clock of 3Khz on timer7 (P8_8)pin og beaglebone.
here is my code.

import Adafruit_BBIO.GPIO as GPIO
import time
outpin="P8_8"
GPIO.setup(outpin,GPIO.OUT)
GPIO.setclock(outpin,3000)
while True:
      GPIO.output(outpin,GPIO.HIGH)
      time.sleep(2)
      GPIO.output(outpin,GPIO.LOW)
      time.sleep(2)
-------------------------------------------------------------------------------------
It gives error..
GPIO.setclock(outpin,3000)
AttributeError: 'module' object has no attribute 'setclock'

Please correct me..

Dennis Lee Bieber

unread,
Jan 28, 2019, 1:41:43 PM1/28/19
to beagl...@googlegroups.com
On Mon, 28 Jan 2019 01:27:39 -0800 (PST),
sayalik...@gmail.com declaimed the
following:
Well, where did you find any documentation about a .setclock()
function? It doesn't exist in
https://adafruit-beaglebone-io-python.readthedocs.io/en/latest/GPIO.html so
take it out.

Furthermore, your while loop takes four seconds to execute -- that
equates to a frequency of 0.25Hz. Generating a 3kHz square wave would
require time.sleep(0.000166666666) -- presuming the OS clock allows for
sleeps that small (note: you will also be affected by any OS pre-emption of
your program). [Total loop time 0.000333333]

Perhaps you really want to be using the PWM function
https://adafruit-beaglebone-io-python.readthedocs.io/en/latest/PWM.html


--
Wulfraed Dennis Lee Bieber AF6VN
wlf...@ix.netcom.com HTTP://wlfraed.home.netcom.com/

Robert Nelson

unread,
Jan 28, 2019, 2:17:45 PM1/28/19
to Beagle Board, sayalik...@gmail.com
Just use the BB-PWM-TIMER-P8.08.dts overlay:

https://github.com/beagleboard/bb.org-overlays/blob/master/src/arm/BB-PWM-TIMER-P8.08.dts

PS, to hit your clock, make sure to properly set teh prescaler:

https://github.com/beagleboard/bb.org-overlays/blob/master/src/arm/BB-PWM-TIMER-P8.08.dts#L47

Regards,

--
Robert Nelson
https://rcn-ee.com/

sayali kutwal

unread,
Jan 29, 2019, 8:03:22 AM1/29/19
to beagl...@googlegroups.com
How to set  the function setclock for timer.


--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/znMvwYwvrgY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/mjiu4epofo8eth2jdimfid58mtv34gffnf%404ax.com.
For more options, visit https://groups.google.com/d/optout.

sayali kutwal

unread,
Jan 29, 2019, 8:03:27 AM1/29/19
to beagl...@googlegroups.com
Hello.
   I want the step by step process to generate clock on P8_8 pin of beaglebone .
and require python code to for the same .The step  by step process to set prescaler.

On Tue, Jan 29, 2019 at 12:11 AM Dennis Lee Bieber <wlf...@ix.netcom.com> wrote:

Dennis Lee Bieber

unread,
Jan 29, 2019, 12:53:05 PM1/29/19
to beagl...@googlegroups.com
On Tue, 29 Jan 2019 11:46:28 +0530, sayali kutwal
<sayalik...@gmail.com> declaimed the
following:

>How to set the function setclock for timer.
>

There is NO setclock function in Adafruit_BBIO. A Google search doesn't
find ANY setclock function for BBB.

The closest I've found is for setting SPI
https://groups.google.com/forum/#!msg/beagleboard/x6VjN_q00c4/UXRFmrYyITQJ

Timers appear to be set up during boot using the device tree
https://www.element14.com/community/thread/34045/l/enabling-timer4-output-on-beaglebone-black?displayFullThread=true

The possible alternative (I've not dug up the Technical Reference
Manual) might require mmap'ing to the timer control registers, and writing
the needed values to it. That assumes mmap can be set to go to hardware
addresses and not virtual addresses.

Dennis Lee Bieber

unread,
Jan 29, 2019, 1:01:35 PM1/29/19
to beagl...@googlegroups.com
On Tue, 29 Jan 2019 11:50:23 +0530, sayali kutwal
<sayalik...@gmail.com> declaimed the
following:

>Hello.
> I want the step by step process to generate clock on P8_8 pin of
>beaglebone .
>and require python code to for the same .The step by step process to set
>prescaler.
>

Chapter 20 of
https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=2ahUKEwjEwdvoyJPgAhUKUa0KHSqvCSsQFjAAegQIChAC&url=https%3A%2F%2Fwww.ti.com%2Flit%2Fug%2Fspruh73p%2Fspruh73p.pdf&usg=AOvVaw1YMRpIKtzhhNT5QoOwIAZ2

Mark Lazarewicz

unread,
Jan 29, 2019, 1:56:06 PM1/29/19
to beagl...@googlegroups.com
Dennis
I think he wants you to code his college assignment for him and he doesn't wanna read the TRM.
They used to give code examples in TRM today's future engineers I'm getting scared😊


--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard+unsub...@googlegroups.com.

Dennis Lee Bieber

unread,
Jan 29, 2019, 2:16:41 PM1/29/19
to beagl...@googlegroups.com
On Tue, 29 Jan 2019 18:55:49 +0000 (UTC), "'Mark Lazarewicz' via
BeagleBoard" <beagl...@googlegroups.com>
declaimed the following:

>DennisI think he wants you to code his college assignment for him and he doesn't wanna read the TRM.They used to give code examples in TRM today's future engineers I'm getting scared?
>

No doubt -- but that I don't provide (I haven't read enough of the TRM
to even attempt this using C).

My normal response is "how would you do that in C?" and then maybe
assist in porting the C to Python.

Mark Lazarewicz

unread,
Jan 29, 2019, 5:41:22 PM1/29/19
to beagl...@googlegroups.com
Sorry Dennis I guess I'm not very tactful. I'll try and rephrase.
Doesn't programming any internal microprocessor registers start with the TRM programming in any language? I can't comprehend not reading the TRM then using a bunch of library functions in some code I found without understanding what the code is doing. I guess if it works and you don't care about how it works that's fine. I've always been fascinated with the details of the registers.these newer chips are quite complex and reading the relavent chapter takes some time and can be daunting but I always figured that was how you really understood the low level
--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to the Google Groups "BeagleBoard" group.
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard+unsub...@googlegroups.com.

sayali kutwal

unread,
Jan 30, 2019, 8:29:22 AM1/30/19
to beagl...@googlegroups.com
Hello.
     It's not my collage assignment. I just want to learn the beaglebone for curiosity. So i want to generate the clock on pin P8_8 (timer7) beaglebone.
For this i write python code...

import Adafruit_BBIO.GPIO as GPIO
import time
outpin="P8_8"
GPIO.setup(outpin,GPIO.OUT)
GPIO.setclock(outpin,3000)
while True:
GPIO.output(outpin,GPIO.HIGH)
time.sleep(2)
GPIO.output(outpin,GPIO.LOW)

time.sleep(2)
----------------------------------------------
But  there is not any library for timer.so i am not getting anything.
after searching i find the term DMTimer to generate clock for beaglebone but i dont know how to do it.
so please guide me step by step.

Thanking you.

You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/znMvwYwvrgY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.

Mark Lazarewicz

unread,
Jan 30, 2019, 9:57:16 AM1/30/19
to beagl...@googlegroups.com
Hello Sayali

My one experience with Python was some HWIL I was modifying for NASA Satellite to test my bootloader written in C. I'm a C programmer I'm not familiar with this library where did you get this 
time.sleep(2) statement? 

Maybe you are not importing the correct objects. If SLEEP exists you need to find how to pull it in if it doesn't exist write your own. I'm sure you can see this  sleep is used to generate a square wave of a certain frequency you can see on the the o scope on the pin.Sleep is going to somehow convert the 2 into seconds or milliseconds etc and put into a processor register of a timer for that so you might need to study TRM.
If I was a professor teaching microprocessors Id leave that to the student to complete to see if he's got the ambition to dig deep and understand the chip.
Getting some code to compile on a target sounds more like some programming exercise. If you  want to learn embedded software engineer not be a programmer learning to write such code will be helpful and require read TRM.

Good Luck
To unsubscribe from this group and stop receiving emails from it, send an email to beagleboard...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/beagleboard/CAC6i%3DDLPik5vQzeoJ3VOcGCqRMuuxiebtv700-iysR%3D%3DyL%3Do9Q%40mail.gmail.com.

Dennis Lee Bieber

unread,
Jan 30, 2019, 5:48:18 PM1/30/19
to beagl...@googlegroups.com
On Wed, 30 Jan 2019 10:04:21 +0530, sayali kutwal
<sayalik...@gmail.com> declaimed the
following:

>Hello.
> It's not my collage assignment. I just want to learn the beaglebone
>for curiosity. So i want to generate the clock on pin P8_8 (timer7)
>beaglebone.
>For this i write python code...
>
>import Adafruit_BBIO.GPIO as GPIO
>import time
>outpin="P8_8"
>GPIO.setup(outpin,GPIO.OUT)
>GPIO.setclock(outpin,3000)
>while True:
>GPIO.output(outpin,GPIO.HIGH)
>time.sleep(2)
>GPIO.output(outpin,GPIO.LOW)
>time.sleep(2)
>----------------------------------------------
>But there is not any library for timer.so i am not getting anything.
>after searching i find the term DMTimer to generate clock for beaglebone
>but i dont know how to do it.
>so please guide me step by step.
>

STEP ONE: READ the TRM section on timers

Step Two: determine how you would access the settings using a low-level
language (C, assembler)

Step Three: port that code to Python -- which might mean writing a
C-language extension library that you can import (read the Extending Python
manual) as direct hardware access may not be possible in pure Python.



Since no one has provided a quick solution, the odds are very good that
no one reading this thread has ever done what you are asking... And that
means ANY OF US would have to do the above three steps to answer you. And
since none of us (except you) has the interest, that leaves it on you to do
the needed research.

Using GPIO control only:
-=-=-=-
debian@beaglebone:~$ cat sqwave.py
"""
Generation of a square wave using GPIO

Dennis L Bieber January 30, 2019
"""

import Adafruit_BBIO.GPIO as GPIO
import time
import sys

OUTPIN = "P8_8"
#FREQUENCY = 1000.0 #in Hz

FREQUENCY = float(sys.argv[1])

INV_FREQ = 1.0 / FREQUENCY
DELAY = INV_FREQ / 2.0


print("Frequency: %s\tInverse Frequency: %s\tDelay: %s"
% (FREQUENCY, INV_FREQ, DELAY))

GPIO.setup(OUTPIN, GPIO.OUT)

state = GPIO.HIGH

try:
while True:
GPIO.output(OUTPIN, state)
time.sleep(DELAY)
state = GPIO.LOW if state == GPIO.HIGH else GPIO.HIGH
except:
GPIO.output(OUTPIN, GPIO.LOW)


debian@beaglebone:~$ python sqwave.py 100.0
Frequency: 100.0 Inverse Frequency: 0.01 Delay: 0.005
^Cdebian@beaglebone:~$ python sqwave.py 500.0
Frequency: 500.0 Inverse Frequency: 0.002 Delay: 0.001
^Cdebian@beaglebone:~$ python sqwave.py 750.0
Frequency: 750.0 Inverse Frequency: 0.00133333333333 Delay:
0.000666666666667
^Cdebian@beaglebone:~$ python sqwave.py 1000.0
Frequency: 1000.0 Inverse Frequency: 0.001 Delay: 0.0005
^Cdebian@beaglebone:~$ python sqwave.py 3000.0
Frequency: 3000.0 Inverse Frequency: 0.000333333333333 Delay:
0.000166666666667
^Cdebian@beaglebone:~$ python sqwave.py 10000.0
Frequency: 10000.0 Inverse Frequency: 0.0001 Delay: 5e-05
^Cdebian@beaglebone:~$

-=-=-=-

NOTE: between Python overhead, and the OS overhead of accessing GPIO,
the effective frequency is much less than requested! I had an oscilloscope
attached and it reported (it varies with OS overhead):

Input Freq Measured Freq
100.0 93
500.0 374
750.0 500
1000.0 600
3000.0 970
10000.0 1300

Off by 33% at 750Hz, and can't even manage 1kHz when "set" for 3kHz.


Using a different pin, to access the PWM system...

-=-=-=-
debian@beaglebone:~$ cat pwm.py
"""
Generation of a square wave using PWM

Dennis L Bieber January 30, 2019
"""

import Adafruit_BBIO.PWM as PWM
import time
import sys

OUTPIN = "P8_13"
DUTY = 50.0

FREQUENCY = float(sys.argv[1])

PWM.start(OUTPIN, DUTY, FREQUENCY)

jnk = raw_input("Press <enter> to exit")

PWM.stop(OUTPIN)
PWM.cleanup()

debian@beaglebone:~$ python pwm.py 100.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 500.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 750.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 1000.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 3000.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 10000.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 50000.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 100000.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 250000.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 500000.0
Press <enter> to exit
debian@beaglebone:~$ python pwm.py 1000000.0
Press <enter> to exit
debian@beaglebone:~$

-=-=-=-

Input Freq Measured Freq
100.0 100
500.0 500
750.0 750
1000.0 1000
3000.0 2994
10000.0 10000
50000.0 50000
100000.0 99900
250000.0 249400
500000.0 497500
1000000.0 990100

Only off by about 10% at 1MHz


However, using a hardware timer rather than the PWM subsystem will
require much study, and I (for one) have no need at the present time to
spend that much time on it. I've also never coded for the PRUs, and those
would be the next place to hit to generate a square wave.

TJF

unread,
Jan 31, 2019, 11:13:42 AM1/31/19
to BeagleBoard
Hi sayalik!

Your task gets very simple when you drop Adafruit library and use libpruio instead. The code

#!/usr/bin/python
## \file
# \brief Example: set 3 kHz timer on P8_08.
#
# This file contains an example on how to use libpruio to set a 3kHz
# timer on pin 10 on header P8.
#
# Licence: GPLv3, Copyright 2019 by Thomas{dot)Freiherr( at ]gmx[dot}net

from __future__ import print_function
from libpruio import *

## The header pin to set
PIN
= P8_08

## Create a ctypes pointer to the pruio structure
io
= pruio_new(PRUIO_DEF_ACTIVE, 4, 0x98, 0)
try:
 
## The pointer dereferencing, using contents member
  IO
= io.contents
 
if IO.Errr: raise AssertionError("pruio_new failed (%s)" % IO.Errr)
 
if pruio_tim_setValue(PIN, 1/3, 0, 0): #         settimer output 3 kHz
   
raise AssertionError("setting timer failed (%s)" % IO.Errr)
 
if pruio_config(io, 1, 0x1FE, 0, 4): #  upload settings, start IO mode
   
raise AssertionError("config failed (%s)" % IO.Errr)
 
##      IO mode is running, you should see 3 kHz timer output on P8_08
 
print("Press <Crtl>-C to stop")
 
try:
   
while True:
 
except KeyboardInterrupt:
   
print("") #                                           clear terminal
finally:
  pruio_destroy
(io)

It sets timer7 at 3 kHz until you stop by pressing <Crtl>-C. Then it restores the system to its original state.

In order to run that code you'll have to install libpruio. Here's the command sequence for kernel 4.14 (it may differ for other kernels)

echo deb http://beagle.tuks.nl/debian jessie/ > /etc/apt/sources.list.d/tuks.list
echo deb
-src http://beagle.tuks.nl/debian jessie/ >> /etc/apt/sources.list.d/tuks.list
wget
-qO - http://beagle.tuks.nl/debian/pubring.gpg | sudo apt-key add -
apt
-get update
apt
-get install python-pruio libpruio-klm libpruio-doc
adduser
<YourUserID> pruio
rm
/boot/dtbs/`uname -r`/am335x-boneblack-uboot-univ.dtb

Execute all commands with root privilegues. (The last one kills the trigger file for cape-universal, instead rename it if you want to use ist later.)

Regards
Message has been deleted

TJF

unread,
Jan 31, 2019, 11:20:38 AM1/31/19
to BeagleBoard
I forgot to mention that you need the language binding in you project folder. Befor running the code also execute

cd <your_project_folder>
cp -r /usr/share/doc/python-pruio/examples/libpruio .

Find further details in the on-line docs.

TJF

unread,
Jan 31, 2019, 11:28:33 AM1/31/19
to BeagleBoard
One more notice:

After installing libpruio (code sequence in my first post) you have to reboot in order to get ride of the cape-universal stuff.

sayali kutwal

unread,
Feb 1, 2019, 8:43:20 AM2/1/19
to beagl...@googlegroups.com
Hello.
    Thank you for your great support.Its help me lot.

On Thu, Jan 31, 2019 at 9:58 PM TJF <jeli.f...@gmail.com> wrote:
One more notice:

After installing libpruio (code sequence in my first post) you have to reboot in order to get ride of the cape-universal stuff.

--
For more options, visit http://beagleboard.org/discuss
---
You received this message because you are subscribed to a topic in the Google Groups "BeagleBoard" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/beagleboard/znMvwYwvrgY/unsubscribe.
To unsubscribe from this group and all its topics, send an email to beagleboard...@googlegroups.com.
Reply all
Reply to author
Forward
Message has been deleted
0 new messages