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)
Generating 2.4Mhz Clock on GPCLK
13 posts
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
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)
osc_debug=<N>
which (may, untested) output the oscillator/N to GPCLK1 (GPIO5 or CAM_GPIO, pin 11 of S5)
- Moderator
- Posts: 3258
- Joined: Wed Aug 17, 2011 7:41 pm
- Location: Cambridge
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
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
- Posts: 3258
- Joined: Wed Aug 17, 2011 7:41 pm
- Location: Cambridge
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!
@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
http://www.fadvisor.net/blog
Protect your Pi for next to nothing with a Guzunty! https://github.com/Guzunty/Pi/wiki
- Posts: 20
- Joined: Mon Jan 14, 2013 10:13 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
- Posts: 3258
- Joined: Wed Aug 17, 2011 7:41 pm
- Location: Cambridge
> 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.
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: 20
- Joined: Mon Jan 14, 2013 10:13 am
> (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.
@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: 20
- Joined: Mon Jan 14, 2013 10:13 am
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
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
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
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

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