Topic: PWM directly from Chumby board

I would like to drive some led's directly from the Chumby. Is it possible to get a PWM signal directly from the Chumby board? In this topic pins 26 & 27 are used to trigger a binary output, so theoretically this could be used with a script which can do this in a frequency of 500 Hz.

Did anybody try this before? Or are there any example scripts in the Chumby source code which can help me to achieve this? It would be cool to do this directly from the chumby, without the need of additional hardware in between.

Re: PWM directly from Chumby board

Which chumby do you have?  The pins I mentioned in that post (26 and 27) are the Tx and Rx pins of the serial port on the chumby One.  They can easily be turned into GPIOs using the script I mentioned.

Conveniently enough, they're also PWM0 and PWM1, both of which can be configured to output a 500 Hz signal with very little effort.

Re: PWM directly from Chumby board

Sorry, forget to mention I have a Chumby one. This means I can use regutil for the necessary shell scripts. Also, I will be running the Flash widget locally, so I could also use ASnative() calls.

Do you have an example script in which the Tx and Rx are used for a PWM signal? Or could you point me into the right direction for finding these?

Thanks!

Re: PWM directly from Chumby board

The first thing you'll need to do is configure the pins to be PWM.  The documentation for HW_PINCTRL_MUXSEL3 (page 1444 of my reference manual) says that we need to set pins 26 and 27 to 0 to select PWM0 and PWM1 functionality:

# Set bank 1, pins 26 and 27 to be PWM outputs
regutil -w HW_PINCTRL_MUXSEL3_CLR=0x00f00000

Then, you'll need to calculate your period.  It's all based on the 24 MHz crystal.  So if you want a 500 Hz signal, you'll want to divide the signal by 64 to get a 375 kHz signal, which will divide nicely.  375000/500 gives you a period of 750 clock cycles total.  If you want a 50% duty, you'll have the signal high for 375 cycles and low for 375 cycles.  Putting that all together, you get regutil writes of:

# Set the crystal to divide by 64, and have a period of 750 cycles.
regutil -w HW_PWM_PERIOD0=0x005b02ee

# Set an "active" and an "inactive" period of 375 cycles each.
regutil -w HW_PWM_ACTIVE0=0x01770177

Then, finally you'll want to turn on the PWM:

#Turn on PWM0.
regutil -w HW_PWM_CTRL_SET=0x00000001

Re: PWM directly from Chumby board

ChumbyLurker, thanks for your help. I understand the general idea of the code you gave, and I think it works. After executing the script the LED on the RX port is 50% less bright then the LED on the TX port.

But some things which are still unclear:
- when I reboot the Chumby, the port is still in the same state, does the chip remember that it is in PWM mode?
- I tried to change the periods to full brightness by changing the HW_PWM_ACTIVE0=0x01770177 to  HW_PWM_ACTIVE0=0x000002ee. This doesn't seem to have an effect.
- The line where you set the period you use hexadecimal 0x005b02ee. I am new to the hexadecimal conversion, but when I convert this number to decimals I get the values 91 and 750. Shouldn't these be 64 and 750?
- When I want to change the duty cycles, can I just change the HW_PWM_ACTIVE0 register value?

Also, can I find the reference manual you are referring to in your post somewhere online? The reference manual in the wiki doesn't have a page 1444.

Hope you can shine some light on these issues.

Many thanks!!

Re: PWM directly from Chumby board

The document I'm referring to is available from Freescale, and is the Applications Processor Reference Manual.

Page 24-8 (actually, 1102) says the format of HW_PWM_PERIOD0 is:

[BITS] PURPOSE
-------------------
[31:25] RSVD2
[24] MATT_SEL
[23] MATT
[22:20] CDIV
[19:18] INACTIVE_STATE
[17:16] ACTIVE_STATE
[15:0] PERIOD

So the period is set to 0x2ee, which equals 750.  The 0xb just means that the first half of the period is high, and the second half is low.  You can swap them, or set it so that one of the halves is a high-impedance.  The 0x5 means it gets divided by 64.

The shape of the pulse is defined in HW_PWM_ACTIVE0, which looks like:

[BITS] PURPOSE
-------------------
[31:16] INACTIVE
[15:0] ACTIVE

Where those values are the number of clock cycles.

Re: PWM directly from Chumby board

Thanks, ChumbyLurker. It makes sense now. I got everything working!

One addition to the code you gave me. The order of setting the period and setting the active/inactive cycles should be reversed. The manual says: The HW_PWM_ACTIVEn register must be written first, followed by HW_PWM_PERIODn. With this adaptation it works great.

I have one more question regarding the power of the PWM signal. Currently this is a 2V signal, but I expected this to be a 3V signal (because the other ports on the chumby board are also 3V). Is there a register I can change to make it a 3V signal? This would be perfect for driving LEDs.

Re: PWM directly from Chumby board

Is it 2V on average, or are you looking at it under a scope?  At 50% duty cycle, it will probably average out on a multimeter at 2V or so.