User avatar
fulburncams
Posts: 70
Joined: Wed Nov 30, 2011 8:29 pm

CLOCK REG INFO PLEASE HELP

Fri Jun 15, 2012 12:04 pm

Hi, I am trying to set a low clock frequency for the PWM can anyone tell me where the register definitions are for the PWM0 clock, I have looked in the BCM2835 manual but cannot find anything, and from others code I have gleaned that there are some registers as follows


#define CLOCK_BASE(BCM2708_PERI_BASE + 0x101000)
#define PWMCLK_CNTL 40
#define PWMCLK_DIV 41

Thanks
Steve

User avatar
Gert van Loo
Posts: 2409
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: CLOCK REG INFO PLEASE HELP

Fri Jun 15, 2012 1:59 pm

The following code snippets are all from the Gertboard example code.
It is too big to put all here. To have a fully working PWM I suggest you get the full
example code and remove the parts you don't need.

#define PWMCLK_CNTL *(clk+40)
#define PWMCLK_DIV *(clk+41)

// Derive PWM clock direct from X-tal
// thus any system auto-slow-down-clock-to-save-power does not effect it
// The values below depends on the X-tal frequency!
PWMCLK_DIV = 0x5A000000 | (32<<12); // set pwm div to 32 (19.2MHz/32 = 600KHz)
PWMCLK_CNTL = 0x5A000011; // Source=osc and enable

So the core is the "(32<<12)". That sets your clock divider.
You can change the '32' to some other number but DON'T PLAY WITH THE OTHER VALUES!

You have to mmap the clk pointer before you can use that code.

User avatar
fulburncams
Posts: 70
Joined: Wed Nov 30, 2011 8:29 pm

Re: CLOCK REG INFO PLEASE HELP

Fri Jun 15, 2012 4:13 pm

Dear Gert

Many thanks, I tried changing it to 512<<12 but this had no effect,so tried 32<<12 and 64<<12, always the same. I guess I am doing something wrong. Is there another register I need to write to to have some effect ?

Regards
Steve

User avatar
Gert van Loo
Posts: 2409
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: CLOCK REG INFO PLEASE HELP

Fri Jun 15, 2012 4:43 pm

I am not sure what you are trying to do.
What do you expect to change? The PWM clock is just an arbitrary clock it runs off.
To control the PWM output you set a range and a value. Those two are the main control values of your PWM.
The clock only controls how long it takes for the PWM to go through its range.

User avatar
fulburncams
Posts: 70
Joined: Wed Nov 30, 2011 8:29 pm

Re: CLOCK REG INFO PLEASE HELP

Sat Jun 16, 2012 11:47 am

Gert

Thanks for the input, I am trying to drive an L298 motor drive chip which has slow rise and fall times ( of the order of 1-2 uS) and as such having a clock frequency of 600Khz means that the chip is spending a lot of time transitioning between high and low, hence I am hoping to slow the clock frequency down to something like say 25khz. I could buy a different faster chip , but it seemed like just changing one value in software would be a lot easier.

Thanks
Steve

User avatar
fulburncams
Posts: 70
Joined: Wed Nov 30, 2011 8:29 pm

Re: CLOCK REG INFO PLEASE HELP

Sat Jun 16, 2012 1:41 pm

Gert

Tried changing the PWMCLK_DIV value but it still made no difference, so instead I have changed range value instead and that seems to have worked.

Thanks
Steve

nickon314
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire

Re: CLOCK REG INFO PLEASE HELP

Wed Jul 11, 2012 2:41 pm

I would like to set the PWM pin up to 38KHz, which is used for Infra Red transmitting - so I can use the RaspPi as an IR blaster.

What register values would I need to get it to work at 38KHz ?

Regards

Nick

User avatar
Gert van Loo
Posts: 2409
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: CLOCK REG INFO PLEASE HELP

Wed Jul 11, 2012 5:10 pm

How about you try a search with "clk PWM"

nickon314
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire

Re: CLOCK REG INFO PLEASE HELP

Thu Jul 12, 2012 10:53 am

I wonder if it's the range that I need to mess with?

Regards

Nick

User avatar
Gert van Loo
Posts: 2409
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: CLOCK REG INFO PLEASE HELP

Thu Jul 12, 2012 3:57 pm

The clock you specify is the clock which is controlling the PWM, the signal data rate coming out depends greatly on the way you are planning to use the PWM to make your signal. How about you tell a bit more of what you are trying to do? What mode are you using the PWM interface in? How do you plan to make the signal?

.

nickon314
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire

Re: CLOCK REG INFO PLEASE HELP

Thu Jul 12, 2012 9:42 pm

I'm trying to port some Arduino code over to the Pi. It's an InfraRed transmitter, sending NEC format IR codes.
The PWM needs to be 38KHz.
And, the 1 and 0 are transmitted by varying the length of time that the PWM is turned on ( I think ).

So, first, I guess I need to ensure that 38KHz is actually coming out.
Then, I need to check the timing of on/off .

If I understand he wiringPi example code, it sets a range of 0 to 1023. So, If I set the duty cycle to 512 then that's 50%, which should produce a square wave with equal periods of on and off.

Regards

Nick

User avatar
Gert van Loo
Posts: 2409
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: CLOCK REG INFO PLEASE HELP

Fri Jul 13, 2012 12:34 pm

nickon314 wrote:I'm trying to port some Arduino code over to the Pi. It's an InfraRed transmitter, sending NEC format IR codes.
The PWM needs to be 38KHz.
And, the 1 and 0 are transmitted by varying the length of time that the PWM is turned on ( I think ).

So, first, I guess I need to ensure that 38KHz is actually coming out.
Then, I need to check the timing of on/off .

If I understand he wiringPi example code, it sets a range of 0 to 1023. So, If I set the duty cycle to 512 then that's 50%, which should produce a square wave with equal periods of on and off.

Regards

Nick
Thus if set-up like that your PWM output frequency will be 1/1024 th of your PWM clock frequency.

nickon314
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire

Re: CLOCK REG INFO PLEASE HELP

Fri Jul 13, 2012 1:01 pm

Waheey. I've almost got it to work.

It now produces very intermittent IR codes. The problem I have now is with the timing of the pulses. They are not regular enough, so the receiving end of the IR is only accepting about 1 in 10 of the IR bursts.

It looks like I need something more accurate than nanosleep for my purposes.

Regards

Nick

User avatar
Gert van Loo
Posts: 2409
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: CLOCK REG INFO PLEASE HELP

Fri Jul 13, 2012 5:05 pm

You can DMA into the PWM.

nickon314
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire

Re: CLOCK REG INFO PLEASE HELP

Sun Jul 15, 2012 9:00 pm

I'm not sure that's going to help me.

The IR bits are transmitted by varying the length of time between bursts of 38KHz .
The length of each burst of 38KHz is the same each time, but between each burst the PWM is turned off for a precise length of time. A zero is represented by a shorter time than a one.

So, I don't think DMA can do that for me, can it?

Regards

Nick

User avatar
Gert van Loo
Posts: 2409
Joined: Tue Aug 02, 2011 7:27 am
Contact: Website

Re: CLOCK REG INFO PLEASE HELP

Mon Jul 16, 2012 6:27 am

I think it can, but it gets rather convoluted.
Read the DMA suggestion for the panalyzer:
http://www.raspberrypi.org/phpBB3/viewt ... 96#p113732

nickon314
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire

Re: CLOCK REG INFO PLEASE HELP

Mon Jul 16, 2012 9:41 am

I notice that there is also a 64bit free running counter available.
I presume this can be mmap'ed to make it available in user space.
That might make a more accurate timer without too many changes to my code.

Do you know what the offset for it would be ?
I see that the hardware address for counters starts at 0x7E003000 . So, would that be 0x20003000 in /dev/mem ?

Regards

Nick

nickon314
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire

Re: CLOCK REG INFO PLEASE HELP

Mon Jul 16, 2012 4:59 pm

Right,
Ok, so using the free running clock is much more accurate with the timing.
Now, the IR Receiver understands the IR commands 9 times out of 10. Which is good enough for my uses.

Thanks.

Nick

honda4life
Posts: 74
Joined: Thu Mar 15, 2012 7:27 pm

Re: CLOCK REG INFO PLEASE HELP

Fri Aug 31, 2012 9:22 pm

I don't get it completely.

I think:
You divide 19,2 MHz by a prescaler 32, so you have 600 KHz "tick"
If I use a range that is 600000 I have a timebase from one second?
So if I want half second on, half second off I use 300000 as value, but I can't find what's wrong :cry:

dtbaker
Posts: 1
Joined: Fri Aug 31, 2012 11:57 am

Re: CLOCK REG INFO PLEASE HELP

Sat Sep 01, 2012 7:35 am

Hi @nickon314

Are you able to publish your IR code somewhere so we can see how you achieved a 9 out of 10 transmit success rate?

Cheers heaps!
Dave

hollmanr
Posts: 1
Joined: Tue Oct 09, 2012 9:33 pm

Re: CLOCK REG INFO PLEASE HELP

Tue Oct 09, 2012 9:38 pm

Hi @nickon314 or @dtbaker.
I too would like to understand better how I can use the PWM on the Gertboard to drive an IR. My IR receiver is 40Khz, how did you manage to setup the clock frequency on the gertboard to get a carrier frequency of 40Khz (I believe in your case it needed to be 38Khz).

Thanks !
Roland

anirban
Posts: 3
Joined: Tue Nov 27, 2012 7:48 am

Re: CLOCK REG INFO PLEASE HELP

Mon Dec 17, 2012 9:33 pm

Hi @nickon314 or @dtbaker or @hollmanr

I am trying to drive the Ir bluster by PWM output. Please share your codes or concept.

Thanks
anirban

Return to “Interfacing (DSI, CSI, I2C, etc.)”

Who is online

Users browsing this forum: Exabot [Bot], Schakal_No1 and 11 guests