User avatar
FADpi
Posts: 10
Joined: Tue Nov 06, 2012 10:52 pm
Contact: Website

Generating 2.4Mhz Clock on GPCLK

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

captn
Posts: 8
Joined: Thu Nov 01, 2012 11:07 am

Re: Generating 2.4Mhz Clock on GPCLK

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 :(

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5593
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Generating 2.4Mhz Clock on GPCLK

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)

captn
Posts: 8
Joined: Thu Nov 01, 2012 11:07 am

Re: Generating 2.4Mhz Clock on GPCLK

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?

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5593
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Generating 2.4Mhz Clock on GPCLK

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.

User avatar
FADpi
Posts: 10
Joined: Tue Nov 06, 2012 10:52 pm
Contact: Website

Re: Generating 2.4Mhz Clock on GPCLK

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/viewt ... 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

guzunty
Posts: 276
Joined: Mon Jan 14, 2013 10:13 am

Re: Generating 2.4Mhz Clock on GPCLK

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.
Guzunty: A fully programmable peripheral you build yourself! https://github.com/Guzunty/Pi/wiki

dom
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 5593
Joined: Wed Aug 17, 2011 7:41 pm
Location: Cambridge

Re: Generating 2.4Mhz Clock on GPCLK

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).

guzunty
Posts: 276
Joined: Mon Jan 14, 2013 10:13 am

Re: Generating 2.4Mhz Clock on GPCLK

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.
Guzunty: A fully programmable peripheral you build yourself! https://github.com/Guzunty/Pi/wiki

guzunty
Posts: 276
Joined: Mon Jan 14, 2013 10:13 am

Re: Generating 2.4Mhz Clock on GPCLK

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.
Guzunty: A fully programmable peripheral you build yourself! https://github.com/Guzunty/Pi/wiki

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Generating 2.4Mhz Clock on GPCLK

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
diereinegier
Posts: 166
Joined: Sun Dec 30, 2012 5:45 pm
Location: Bonn, Germany
Contact: Website

Re: Generating 2.4Mhz Clock on GPCLK

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. ... ransmitter
Download my repositories at https://github.com/GeorgBisseling

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Generating 2.4Mhz Clock on GPCLK

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/

resman
Posts: 10
Joined: Sat Jan 12, 2013 6:59 pm

Re: Generating 2.4Mhz Clock on GPCLK

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...

pasdesignal
Posts: 19
Joined: Sat Jan 05, 2013 8:11 am
Contact: Website

Re: Generating 2.4Mhz Clock on GPCLK

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?

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Generating 2.4Mhz Clock on GPCLK

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/

pasdesignal
Posts: 19
Joined: Sat Jan 05, 2013 8:11 am
Contact: Website

Re: Generating 2.4Mhz Clock on GPCLK

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?

Gouthamp
Posts: 8
Joined: Wed Oct 22, 2014 9:24 pm

Re: Generating 2.4Mhz Clock on GPCLK

Fri Oct 24, 2014 5:00 am

hI Gordon,
WiringPi seems to be extremely useful for me. It would be grateful if you can add the feature of tapping other clock sources to generate clock from GPIO. I need to generate exactly 10 MHz and with the oscillator of 19.2 MHz, I can take max 9.6 MHz( with a division of 2). But if you can provide access to PLL (max of 600 MHz), it will be highly useful. Please help me on this regard.

Thanks
Goutham

User avatar
gordon@drogon.net
Posts: 2022
Joined: Tue Feb 07, 2012 2:14 pm
Location: Devon, UK
Contact: Website Twitter

Re: Generating 2.4Mhz Clock on GPCLK

Fri Oct 24, 2014 6:47 am

Gouthamp wrote:hI Gordon,
WiringPi seems to be extremely useful for me. It would be grateful if you can add the feature of tapping other clock sources to generate clock from GPIO. I need to generate exactly 10 MHz and with the oscillator of 19.2 MHz, I can take max 9.6 MHz( with a division of 2). But if you can provide access to PLL (max of 600 MHz), it will be highly useful. Please help me on this regard.

Thanks
Goutham
I'll look into it.

-Gordon
--
Gordons projects: https://projects.drogon.net/

User avatar
joan
Posts: 15040
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Generating 2.4Mhz Clock on GPCLK

Fri Oct 24, 2014 7:06 am

Gouthamp wrote:hI Gordon,
WiringPi seems to be extremely useful for me. It would be grateful if you can add the feature of tapping other clock sources to generate clock from GPIO. I need to generate exactly 10 MHz and with the oscillator of 19.2 MHz, I can take max 9.6 MHz( with a division of 2). But if you can provide access to PLL (max of 600 MHz), it will be highly useful. Please help me on this regard.

Thanks
Goutham
This Minimal clock access code lets you choose any of the available clock sources for the general purpose clocks.

Gouthamp
Posts: 8
Joined: Wed Oct 22, 2014 9:24 pm

Re: Generating 2.4Mhz Clock on GPCLK

Fri Oct 24, 2014 7:52 am

Hi Joan,

The code seems to be great and hats off for the work. But can you specifically say how I can select the exact 10 MHz out of this code?
/*
gcc -o minimal_clk minimal_clk.c
sudo ./minimal_clk x.xx [options]
*/
What is the OPTIONS in it?
Also How can i use this code as part of my main program? ( my ques may be very basic,but please answer)

Thanks
Goutham

User avatar
joan
Posts: 15040
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Generating 2.4Mhz Clock on GPCLK

Fri Oct 24, 2014 9:07 am

Gouthamp wrote:Hi Joan,

The code seems to be great and hats off for the work. But can you specifically say how I can select the exact 10 MHz out of this code?
/*
gcc -o minimal_clk minimal_clk.c
sudo ./minimal_clk x.xx [options]
*/
What is the OPTIONS in it?
Also How can i use this code as part of my main program? ( my ques may be very basic,but please answer)

Thanks
Goutham
If you run the program with something like setclk -q it should show the options.

For a 10MHz clock on gpio4 (GPGLK0) you'd need

sudo ./setclk 10M

By default PLLD (500MHz) is used as the clock source.

Most of the code is a utility wrapper around the few lines you'd actually need for your task.

Gouthamp
Posts: 8
Joined: Wed Oct 22, 2014 9:24 pm

Re: Generating 2.4Mhz Clock on GPCLK

Thu Nov 06, 2014 12:56 am

Hi Joan,

How can i Autorun the above clock generation script whenever i turn the pi on.? I would like to run multiple files at same time. I need this 10 MHz clock always from my pi even though i don't run this program individually . Is there any option for that?

Thanks
Goutham

User avatar
joan
Posts: 15040
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Generating 2.4Mhz Clock on GPCLK

Sat Nov 08, 2014 10:54 am

Gouthamp wrote:Hi Joan,

How can i Autorun the above clock generation script whenever i turn the pi on.? I would like to run multiple files at same time. I need this 10 MHz clock always from my pi even though i don't run this program individually . Is there any option for that?

Thanks
Goutham
I cut down the code to (almost) the minimum required to generate a 10MHz clock.

Code: Select all


/*
10MHzClock.c
2014-10-24
Public Domain

gcc -o 10MHzClock 10MHzClock.c
sudo ./10MHzClock

*/

/*

This allows the setting of the three general purpose clocks.

The clocks are named GPCLK0, GPCLK1, and GPCLK2.

The clocks are accessible from the following gpios.

gpio4  GPCLK0 ALT0
gpio5  GPCLK1 ALT0 B+ and compute module only (reserved for system use)
gpio6  GPCLK2 ALT0 B+ and compute module only
gpio20 GPCLK0 ALT5 B+ and compute module only
gpio21 GPCLK1 ALT5 Not available on Rev.2 B (reserved for system use)

gpio32 GPCLK0 ALT0 Compute module only
gpio34 GPCLK0 ALT0 Compute module only
gpio42 GPCLK1 ALT0 Compute module only (reserved for system use)
gpio43 GPCLK2 ALT0 Compute module only
gpio44 GPCLK1 ALT0 Compute module only (reserved for system use)

Clock sources

0     0 Hz     Ground
1     19.2 MHz oscillator 
2     0 Hz     testdebug0
3     0 Hz     testdebug1
4     0 Hz     PLLA
5     1000 MHz PLLC (changes with overclock settings)
6     500 MHz  PLLD
7     216 MHz  HDMI auxiliary
8-15  0 Hz     Ground

The integer divider may be 2-4095.
The fractional divider may be 0-4095.

There is no 25MHz cap for using non-zero MASH
(multi-stage noise shaping) values.
*/

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/types.h>

#define SYST_BASE  0x20003000
#define DMA_BASE   0x20007000
#define CLK_BASE   0x20101000
#define GPIO_BASE  0x20200000
#define UART0_BASE 0x20201000
#define PCM_BASE   0x20203000
#define SPI0_BASE  0x20204000
#define I2C0_BASE  0x20205000
#define PWM_BASE   0x2020C000
#define UART1_BASE 0x20215000
#define I2C1_BASE  0x20804000
#define I2C2_BASE  0x20805000
#define DMA15_BASE 0x20E05000

#define DMA_LEN   0x1000 /* allow access to all channels */
#define CLK_LEN   0xA8
#define GPIO_LEN  0xB4
#define SYST_LEN  0x1C
#define PCM_LEN   0x24
#define PWM_LEN   0x28
#define I2C_LEN   0x1C

#define GPSET0 7
#define GPSET1 8

#define GPCLR0 10
#define GPCLR1 11

#define GPLEV0 13
#define GPLEV1 14

#define GPPUD     37
#define GPPUDCLK0 38
#define GPPUDCLK1 39

#define SYST_CS  0
#define SYST_CLO 1
#define SYST_CHI 2

#define CLK_PASSWD  (0x5A<<24)

#define CLK_CTL_MASH(x)((x)<<9)
#define CLK_CTL_BUSY    (1 <<7)
#define CLK_CTL_KILL    (1 <<5)
#define CLK_CTL_ENAB    (1 <<4)
#define CLK_CTL_SRC(x) ((x)<<0)

#define CLK_SRCS 4

#define CLK_CTL_SRC_OSC  1  /* 19.2 MHz */
#define CLK_CTL_SRC_PLLC 5  /* 1000 MHz */
#define CLK_CTL_SRC_PLLD 6  /*  500 MHz */
#define CLK_CTL_SRC_HDMI 7  /*  216 MHz */

#define CLK_DIV_DIVI(x) ((x)<<12)
#define CLK_DIV_DIVF(x) ((x)<< 0)

#define CLK_GP0_CTL 28
#define CLK_GP0_DIV 29
#define CLK_GP1_CTL 30
#define CLK_GP1_DIV 31
#define CLK_GP2_CTL 32
#define CLK_GP2_DIV 33

#define CLK_PCM_CTL 38
#define CLK_PCM_DIV 39

#define CLK_PWM_CTL 40
#define CLK_PWM_DIV 41


static volatile uint32_t  *gpioReg = MAP_FAILED;
static volatile uint32_t  *clkReg  = MAP_FAILED;

#define PI_BANK (gpio>>5)
#define PI_BIT  (1<<(gpio&0x1F))

/* gpio modes. */

#define PI_INPUT  0
#define PI_OUTPUT 1
#define PI_ALT0   4
#define PI_ALT1   5
#define PI_ALT2   6
#define PI_ALT3   7
#define PI_ALT4   3
#define PI_ALT5   2

void gpioSetMode(unsigned gpio, unsigned mode)
{
   int reg, shift;

   reg   =  gpio/10;
   shift = (gpio%10) * 3;

   gpioReg[reg] = (gpioReg[reg] & ~(7<<shift)) | (mode<<shift);
}

int gpioGetMode(unsigned gpio)
{
   int reg, shift;

   reg   =  gpio/10;
   shift = (gpio%10) * 3;

   return (*(gpioReg + reg) >> shift) & 7;
}

static int initClock(int clock, int source, int divI, int divF, int MASH)
{
   int ctl[] = {CLK_GP0_CTL, CLK_GP2_CTL};
   int div[] = {CLK_GP0_DIV, CLK_GP2_DIV};
   int src[CLK_SRCS] =
      {CLK_CTL_SRC_PLLD,
       CLK_CTL_SRC_OSC,
       CLK_CTL_SRC_HDMI,
       CLK_CTL_SRC_PLLC};

   int clkCtl, clkDiv, clkSrc;
   uint32_t setting;

   if ((clock  < 0) || (clock  > 1))    return -1;
   if ((source < 0) || (source > 3 ))   return -2;
   if ((divI   < 2) || (divI   > 4095)) return -3;
   if ((divF   < 0) || (divF   > 4095)) return -4;
   if ((MASH   < 0) || (MASH   > 3))    return -5;

   clkCtl = ctl[clock];
   clkDiv = div[clock];
   clkSrc = src[source];

   clkReg[clkCtl] = CLK_PASSWD | CLK_CTL_KILL;

   /* wait for clock to stop */

   while (clkReg[clkCtl] & CLK_CTL_BUSY)
   {
      usleep(10);
   }

   clkReg[clkDiv] =
      (CLK_PASSWD | CLK_DIV_DIVI(divI) | CLK_DIV_DIVF(divF));

   usleep(10);

   clkReg[clkCtl] =
      (CLK_PASSWD | CLK_CTL_MASH(MASH) | CLK_CTL_SRC(clkSrc));

   usleep(10);

   clkReg[clkCtl] |= (CLK_PASSWD | CLK_CTL_ENAB);
}

static int termClock(int clock)
{
   int ctl[] = {CLK_GP0_CTL, CLK_GP2_CTL};

   int clkCtl;

   if ((clock  < 0) || (clock  > 1))    return -1;

   clkCtl = ctl[clock];

   clkReg[clkCtl] = CLK_PASSWD | CLK_CTL_KILL;

   /* wait for clock to stop */

   while (clkReg[clkCtl] & CLK_CTL_BUSY)
   {
      usleep(10);
   }
}


/* Map in registers. */

static uint32_t * initMapMem(int fd, uint32_t addr, uint32_t len)
{
    return (uint32_t *) mmap(0, len,
       PROT_READ|PROT_WRITE|PROT_EXEC,
       MAP_SHARED|MAP_LOCKED,
       fd, addr);
}

int gpioInitialise(void)
{
   int fd;

   fd = open("/dev/mem", O_RDWR | O_SYNC) ;

   if (fd < 0)
   {
      fprintf(stderr,
         "This program needs root privileges.  Try using sudo\n");
      return -1;
   }

   gpioReg  = initMapMem(fd, GPIO_BASE, GPIO_LEN);
   clkReg   = initMapMem(fd, CLK_BASE,  CLK_LEN);

   close(fd);

   if ((gpioReg == MAP_FAILED) || (clkReg == MAP_FAILED))
   {
      fprintf(stderr,
         "Bad, mmap failed\n");
      return -1;
   }
   return 0;
}

/* ------------------------------------------------------------------------ */

int main(int argc, char *argv[])
{
   if (gpioInitialise() < 0) return 1;

   /* int clock, int source, int divI, int divF, int MASH */
   initClock(0, 0, 4000, 0, 0);

   gpioSetMode(4, PI_ALT0);

   return 0;
}
Compile and bung it somewhere like /usr/local/bin.

Then add the following line at the end of /etc/rc.local (just before exit 0).

Code: Select all

/usr/local/bin/10MHzClock
There is no need to use sudo as /etc/rc.local is run as root.

kjherdez
Posts: 5
Joined: Thu May 14, 2015 8:14 pm

Re: Generating 2.4Mhz Clock on GPCLK

Tue May 19, 2015 10:38 pm

Hello,

This is directed at Joan's code above to set the GPCLK output arbitrarily.

I have a brand new RPi "Raspberry Pi 2 Model B V1.1" Fresh raspian installation through "NOOBS" This is the first thing I'm trying to use it for.

If I run sudo ./setclk 10K, the program runs but there is no output on GPIO4/GPCLK0. The pin remains HI.

Is there some sort of extra setup I need to do beforehand for this to work?

Return to “C/C++”