CLOCK REG INFO PLEASE HELP


22 posts
by fulburncams » 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
Posts: 70
Joined: Wed Nov 30, 2011 8:29 pm
by Gert van Loo » 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
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2078
Joined: Tue Aug 02, 2011 7:27 am
by fulburncams » 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
Posts: 70
Joined: Wed Nov 30, 2011 8:29 pm
by Gert van Loo » 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
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2078
Joined: Tue Aug 02, 2011 7:27 am
by fulburncams » 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
Posts: 70
Joined: Wed Nov 30, 2011 8:29 pm
by fulburncams » 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
User avatar
Posts: 70
Joined: Wed Nov 30, 2011 8:29 pm
by nickon314 » 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
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire
by Gert van Loo » Wed Jul 11, 2012 5:10 pm
How about you try a search with "clk PWM"
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2078
Joined: Tue Aug 02, 2011 7:27 am
by nickon314 » Thu Jul 12, 2012 10:53 am
I wonder if it's the range that I need to mess with?

Regards

Nick
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire
by Gert van Loo » 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?

.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2078
Joined: Tue Aug 02, 2011 7:27 am
by nickon314 » 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
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire
by Gert van Loo » 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.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2078
Joined: Tue Aug 02, 2011 7:27 am
by nickon314 » 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
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire
by Gert van Loo » Fri Jul 13, 2012 5:05 pm
You can DMA into the PWM.
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2078
Joined: Tue Aug 02, 2011 7:27 am
by nickon314 » 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
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire
by Gert van Loo » 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/viewtopic.php?f=37&t=7696#p113732
User avatar
Raspberry Pi Engineer & Forum Moderator
Raspberry Pi Engineer & Forum Moderator
Posts: 2078
Joined: Tue Aug 02, 2011 7:27 am
by nickon314 » 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
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire
by nickon314 » 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
Posts: 115
Joined: Tue Jul 03, 2012 12:11 pm
Location: West Yorkshire
by honda4life » 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:
Posts: 70
Joined: Thu Mar 15, 2012 7:27 pm
by dtbaker » 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
Posts: 1
Joined: Fri Aug 31, 2012 11:57 am
by hollmanr » 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
Posts: 1
Joined: Tue Oct 09, 2012 9:33 pm
by anirban » 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
Posts: 3
Joined: Tue Nov 27, 2012 7:48 am