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