Pulse-width modulation on XMOS

New to XMOS and XCore? Get started here.
User avatar
feniksa
Member++
Posts: 22
Joined: Tue Apr 03, 2012 1:12 pm

Pulse-width modulation on XMOS

Post by feniksa »

I try to make "slowly" turn on/off leds on XK-1A devkit. But i got stuck with it.

I tried to make with simple code:

Code: Select all


void flashLeds(out port leds, chanend c)
{  
  timer t;
  unsigned int time;
  int ledVal = 1;
  int isOn = 1;

  t :> time;

  leds <: ledVal;
  while (1)
  {
    
    select
    {
      case t when timerafter(time) :> void:
      {
        if (isOn)
          leds <: ledVal;
        else
          leds <: 0;
        
        isOn = !isOn;
        
        time += 1;                 // <<---- BAD IDEA HERE
        break;
      }
      
      case c :> ledVal :
      {
        break;
      }
    }
  }
}

but it have very bad performance.

Will be very pleased for some source code + comments

Thanks


User avatar
sethu_jangala
XCore Expert
Posts: 589
Joined: Wed Feb 29, 2012 10:03 am

Post by sethu_jangala »

time += 1; // <<---- BAD IDEA HERE
In your example the time delay '1' is very less. So, you are not able to see anything with the LEDs. You need to increase the delay, say something like XS1_TIMER_HZ. This make the LED toggle for every 1 second.
User avatar
feniksa
Member++
Posts: 22
Joined: Tue Apr 03, 2012 1:12 pm

Post by feniksa »

Looks like it is better to implement pulse-width modulation via clocked ports... But how?
User avatar
Ross
XCore Expert
Posts: 966
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

That time out is going to fire all the time with such a small delay!
User avatar
feniksa
Member++
Posts: 22
Joined: Tue Apr 03, 2012 1:12 pm

Post by feniksa »

Ross wrote:That time out is going to fire all the time with such a small delay!
Agree with you. What i want to do:

Target: smoothly turn on/off led on XK-A1 board
Pulse-Width modulation (as i understand):

Image

But i got stuck: How to implement it with XMOS and don't kill performance of XMOS chip.
User avatar
Ross
XCore Expert
Posts: 966
Joined: Thu Dec 10, 2009 9:20 pm
Location: Bristol, UK

Post by Ross »

What do you mean by "kill performance of the XMOS chip" please?
User avatar
feniksa
Member++
Posts: 22
Joined: Tue Apr 03, 2012 1:12 pm

Post by feniksa »

Ross wrote:What do you mean by "kill performance of the XMOS chip" please?
I mean, that most of time XMOS will wasting cycles.
User avatar
TSC
Experienced Member
Posts: 111
Joined: Sun Mar 06, 2011 11:39 pm

Post by TSC »

feniksa:

Do you know about the sc_pwm software component on GitHub?

Or are you wanting to write your own PWM code as a learning exercise or for some other reason?

BTW, the new git bbcode button which links to a specified XCore package on GitHub has a problem. It links to "githubo.com" for some reason. E.g. sc_pwm
User avatar
infiniteimprobability
XCore Legend
Posts: 1126
Joined: Thu May 27, 2010 10:08 am

Post by infiniteimprobability »

Producing PWM on the xcore can be done in many different ways..

It all depends on what you want to achieve. Things to consider are:

- Frequency (PWM period)
- Bits resolution needed
- number of channels
- Synchronicity (edge, centre, asynch..)
- Modulation depth required (do you need to go right to 100% and 0%)
- Buffering and duty reload scheme
- Jitter tolerance
- API (function call, channel end, shared memory, interfaces - new XC feature in tools 13)
- Available resources. Does it need to be squeezed in with other stuff (Again - tools 13 helps here with combinable functions).

Stuff on the chip you have at your disposal are:

- Ports (standard I/O)
- Port timers (to time output events)
- General timers
- Port buffering (useful for serialising and playing bit streams)
- Clock blocks (for generating precise periods/count timing)
- Lots of logical cores for doing the work

As TSC says, sc_pwm has lots of examples of varying type and complexity.

I guess what would be interesting to know is what's your goal? Is it learning, LED dimming or something more sophisticated like spinning a motor?

If it's just LED dimming/learning then using a timer in a select is good...however ensure that you generate an event at the start of frame, and another at the transition point for the on and off. You can use two timers for this - in Tools 13 timers get re-used in each core (Ie. declaring multiple timers results in just one bing used by default) so don't hold back on using this useful resource.