Generating 2.4Mhz Clock on GPCLK


17 posts
by FADpi » Tue Nov 06, 2012 11:27 pm
Hi all,

I want to write a C code to generate a clock on any of the General Purpose clocks pins in the GPIO GPCLK0 ~ GPCLK2. I am looking at the datasheet page 105-108 and couldn't figure out a way to do it. So basically I have an external circuit that needs a clock frequency between 1Mhz - 3.25Mhz (typical is 2.4Mhz) which will then output digital data that I need to read from one of the other GPIO ports.

I googled around and only found like two links and there were not very helpful
Anyone knows how to do it in C? (like how to select the clock source and set the divider to get the needed frequency)
Fahad
http://www.fadvisor.net/blog
User avatar
Posts: 10
Joined: Tue Nov 06, 2012 10:52 pm
by captn » Wed Nov 07, 2012 6:20 pm
AFAIK those parts of the SoC are kept in secret and I'm afraid it's without some big re efforts not possible :(
Posts: 8
Joined: Thu Nov 01, 2012 11:07 am
by dom » Wed Nov 07, 2012 8:37 pm
There is a debug feature:

osc_debug=<N>

which (may, untested) output the oscillator/N to GPCLK1 (GPIO5 or CAM_GPIO, pin 11 of S5)
Moderator
Moderator
Posts: 3864
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by captn » Wed Nov 07, 2012 9:24 pm
dom wrote:There is a debug feature:

osc_debug=<N>

which (may, untested) output the oscillator/N to GPCLK1 (GPIO5 or CAM_GPIO, pin 11 of S5)


Cool. Do you mean the clock-generator/N ? I guess the source can be set to "1-oscilator" or some of the PLLs? What is then the generator input frequency?
Posts: 8
Joined: Thu Nov 01, 2012 11:07 am
by dom » Wed Nov 07, 2012 10:14 pm
captn wrote:Cool. Do you mean the clock-generator/N ? I guess the source can be set to "1-oscilator" or some of the PLLs? What is then the generator input frequency?


Currently it comes from the oscillator (i.e. 19.2MHz).
There's no spare PLLs, so you can't generate arbitrary frequencies.*

Can you see the output?

* although if you don't use analogue audio, you may be able to persuade it's PLL to give you a multiple of the desired frequency. We still need some code to let you choose that PLL.
Moderator
Moderator
Posts: 3864
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by FADpi » Thu Nov 08, 2012 3:16 pm
Good finding dom, that is a good thing to start with.
@captn: I don't think those are kept secret, since I don't see the point in them hiding such basic capabilities of their products which people can use to build on top of. However, I do believe it is just not one of their priorities so they simply don't care.

Here is a link on this forum of a guy being able to generate 1Mhz (it seems that he os also using the oscillator as the source) http://www.raspberrypi.org/phpBB3/viewtopic.php?f=72&t=17975
However, his code is written in assembly.

I think we need a good C developers who understand working with hardware and can decipher datasheets to write a library to access the clock sources easy. WiringPi has done a good job making such a C library to interface and simplify using some of the GPIO but not the clock generation!
Fahad
http://www.fadvisor.net/blog
User avatar
Posts: 10
Joined: Tue Nov 06, 2012 10:52 pm
by guzunty » Mon Jan 14, 2013 10:26 am
Does this code do what you want?

https://github.com/Guzunty/Pi/blob/mast ... c/gz_clk.c

best,

G.
Protect your Pi for next to nothing with a Guzunty! https://github.com/Guzunty/Pi/wiki
Posts: 173
Joined: Mon Jan 14, 2013 10:13 am
by dom » Mon Jan 14, 2013 11:54 am
dom wrote:There is a debug feature:

osc_debug=<N>

which (may, untested) output the oscillator/N to GPCLK1 (GPIO5 or CAM_GPIO, pin 11 of S5)


Latest firmware has tweaked this to
osc_debug=<freq_in_hz>

which can set fraction divisors. It comes from a 2GHz PLL. (Untested)

The Gazunty code looks good too. (I'm assuming bcm2835_clk address comes from mmap hack).
Moderator
Moderator
Posts: 3864
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge
by guzunty » Mon Jan 14, 2013 3:29 pm
> I'm assuming bcm2835_clk address comes from mmap hack

The include file is here:

https://github.com/Guzunty/Pi/blob/mast ... e/gz_clk.h

It uses bcm2835.h so I think the answer is yes.
Protect your Pi for next to nothing with a Guzunty! https://github.com/Guzunty/Pi/wiki
Posts: 173
Joined: Mon Jan 14, 2013 10:13 am
by guzunty » Sat Jan 26, 2013 1:52 pm
> (I'm assuming bcm2835_clk address comes from mmap hack).

@Dom. I'm curious to understand this mmap technique better. I googled it but didn't turn anything up. Are you referring the practice of mapping the register address range used by the BCM2835 library?

One thing in particular piques my interest. The word 'hack' is often used to imply that there is a Better Way. If such exists, I'd like to know about it. TIA.

best,

G.
Protect your Pi for next to nothing with a Guzunty! https://github.com/Guzunty/Pi/wiki
Posts: 173
Joined: Mon Jan 14, 2013 10:13 am
by gordon@drogon.net » Sat Jan 26, 2013 3:27 pm
FADpi wrote:I think we need a good C developers who understand working with hardware and can decipher datasheets to write a library to access the clock sources easy. WiringPi has done a good job making such a C library to interface and simplify using some of the GPIO but not the clock generation!


Thanks for the refernce to wiringPi.. I've tended to concentrate on things that (mostly) I find useful for my own projects, but occasionally, I've added in other stuff... (ask nicely and if there appears to be demand... ;-)

I also want to try to stick to stuff that works as "standard" (at least under Raspbian, anyway), but some tweaks are needed - e.g. for the serial stuff using the on-board UART, I2C dev libraries needing to be installed...

So maybe there's some scope for looking at the clock generators, but I suspect we're really now delving into an area where only a very few people might make use of it though.

However, from what I read here & a quick look at the manual - there are 3 GPIO clocks... Each clock has a number of sources, but the likely one to use is the GPIO clock (19.2MHz if I read doms messages correctly) then apply the divider (DIVI and DIVF) to get the target frequency and enable the output on a pin via the ALT settings.

Might have a look if I have time later...

What could possibly go wrong...

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1421
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by diereinegier » Tue Jan 29, 2013 8:22 am
There is a project to use the RasPi as a radio station. They generate a fm signal between 1 MHz and 100 MHz and they even modulate a sound file onto it. They use GPIO Pin 4. Sounds pretty close to what you have in mind, doesn't it?

I tried it and it works.

http://www.icrobotics.co.uk/wiki/index.php/Turning_the_Raspberry_Pi_Into_an_FM_Transmitter
Download my repositories at https://github.com/GeorgBisseling
User avatar
Posts: 145
Joined: Sun Dec 30, 2012 5:45 pm
Location: Bonn, Germany
by gordon@drogon.net » Fri Feb 01, 2013 10:08 pm
gordon@drogon.net wrote:
What could possibly go wrong...


Well, it's there. In the latest wiringPi, there is an extension to pinMode:

pinMode (pin, GPIO_CLOCK) ;

then:

gpioClockSet (pin, frequency) ;

The pin can be 7 (BCM_GPIO 4), or on a Rev 1 Pi pin 2 (BCM_GPIO 21) can also be used.

The frequency is derived from the 19.2MHz clock, so anything up to that (within the limitation of the dividers)

so for 2.4MHz:

gpioClockSet (7, 2400000) ;

the gpio utility can also be used to set it up from the command-line:

gpio mode 7 clock
gpio clock 7 2400000

Image

And look at the deltaT on the right - showing 0.420uS which is more or less 2.4MHz (and my cheap scope isn't that accurate anyway)

Get wiringPi as usual:

cd
git clone git://git.drogon.net/wiringPi
cd wiringPi
./build

or

cd wiringPi
git pull
./build


Enjoy!

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1421
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by resman » Tue Jun 18, 2013 8:36 pm
Gordon-

Thanks for making wiringPi available. I have been looking to output a very specific frequency (1.8432MHz) but the default GPIO oscillator of 19.2 MHz didn't divide very nicely. In looking at the other clock sources available as input to the GCLK I found PLD at 500 MHz and HDMI auxiliary at 216 MHz. These sources get me much closer to my desired destination frequency, but I'm unsure as to the general availability/stability of these source frequencies. I've searched but haven't been able to locate information on these sources. Do you, or anyone else, have some insight to these clocks? Regards,

Dave...
Posts: 10
Joined: Sat Jan 12, 2013 6:59 pm
by pasdesignal » Wed Jun 19, 2013 6:48 am
resman wrote:Gordon-

Thanks for making wiringPi available. I have been looking to output a very specific frequency (1.8432MHz) but the default GPIO oscillator of 19.2 MHz didn't divide very nicely. In looking at the other clock sources available as input to the GCLK I found PLD at 500 MHz and HDMI auxiliary at 216 MHz. These sources get me much closer to my desired destination frequency, but I'm unsure as to the general availability/stability of these source frequencies. I've searched but haven't been able to locate information on these sources. Do you, or anyone else, have some insight to these clocks? Regards,

Dave...


Seconded! I was just having the same issue. The wiringPi utility is awesome (thanks Gordon! :ugeek: ), and it's really handy to be able to output a clock and be able control its frequency so easily - but not enough versatility at the moment with the limited frequencies available..... Anyone have the skills to improve on this a little?
Posts: 19
Joined: Sat Jan 05, 2013 8:11 am
by gordon@drogon.net » Wed Jun 19, 2013 7:08 am
pasdesignal wrote:
resman wrote:Gordon-

Thanks for making wiringPi available. I have been looking to output a very specific frequency (1.8432MHz) but the default GPIO oscillator of 19.2 MHz didn't divide very nicely. In looking at the other clock sources available as input to the GCLK I found PLD at 500 MHz and HDMI auxiliary at 216 MHz. These sources get me much closer to my desired destination frequency, but I'm unsure as to the general availability/stability of these source frequencies. I've searched but haven't been able to locate information on these sources. Do you, or anyone else, have some insight to these clocks? Regards,

Dave...


Seconded! I was just having the same issue. The wiringPi utility is awesome (thanks Gordon! :ugeek: ), and it's really handy to be able to output a clock and be able control its frequency so easily - but not enough versatility at the moment with the limited frequencies available..... Anyone have the skills to improve on this a little?


There are other oscillators I can tap into, so maybe i'll make those an option too.

-Gordon
--
Gordons projects: https://projects.drogon.net/
User avatar
Posts: 1421
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
by pasdesignal » Thu Jun 20, 2013 7:40 pm
I'm sure any added functionality would be well used.
Out of interest, what are the limits of the dividers on the clock circuit currently used?
Posts: 19
Joined: Sat Jan 05, 2013 8:11 am