Cerbuino Bee Native Code Timer using interupts

When I say “MDK”, I’m referring to Keil uVision (their compiler is named MDK). I had suggested just starting out with a simple timer program in uVision that you would compile and upload directly to your hardware (using a JTAG adapter or whatever you’ve got laying around). In other words, no NET MF; just the native code.

However! Having said all that, I was re-reading your posts and I must have missed this the first time:

If all you’re trying to do is make a 1 MHz pulse, why not just use the PWM module with a 50% duty cycle? You can do it in C#:

PWM clockSrc = new PWM(Cpu.PWMChannel.PWM_0, 1000000, 0.5, false);

The PWM processor requires zero processing; you just configure it with the duty cycle and period, and you’re good to go.

@ jay - Yes, I missed this point as I thought that
a) PWM would not give me enough resolution i.e. would not go down to 1MHz, and
b) the thread handling and other interrupts would produce uneven timings.

So my thinking was that PWM was just not good enough - maybe I am wrong.

I will give it a try, and in the meantime will mark this as answered. I may come back with a reply later.

Many thanks for your help.

Haha, yeah – actually, PWM modules are completely automatic; the processor doesn’t do anything once you configure and enable the module. PWM modules are generated using a separate state machine that has nothing to do with the processor’s execution code.

PWM is way more accurate than a interrupt-based timer can ever be. I’ve only used interrupt-based timers to do two things:
[ul]By setting the timer to a really slow value, you can execute periodic functions, like animating an RGB LED to fade between values, or run a rudimentary task scheduler.
Bit-banging a slow protocol while you’re trying to do something else at the same time.[/ul]

If you have to bit-bang a fast protocol, you typically do blocking calls instead of interrupt-driven calls, i.e.:


for(int i=0; i<8; i++)
    writeOutput(data[i]);
    startTimer();
    while(!timerFlagCleared());
}

since you typically send the message quickly enough to not worry about getting hung up in the function for too long.

Since I last wrote in this thread, I have managed to get a PWM going in C#, and then use native code via Keil Mvision and RLPLite to do all of the bit bashing. The bit bashing is done within amicrosecond, and reads input pins and toggles output pins just as I wanted.

My task now is to trigger the PWM from within my C code that is compiled in Keil, and loaded via RLPLite.

I am using PWM5 in my C# as follows


 PWM PWMClock = new PWM(Cpu.PWMChannel.PWM_5, 500000, 0.45, false);
.
.
PWMClock.Start();

and in my C code in Kiel I would like to do as follows


// enable timer for PWM, set it running.
		  // TIM_Cmd(TIM3, ENABLE);
			TIM3->CR1 |= TIM_CR1_CEN;

but it doen’t work (surprise, surprise!).

Does anyone know what PWMClock.Start() does? It must do more than my TIM3 enable.
The problem is that it takes a long time to call the PWMClock.Start() code in C#, and then get in to my C code via RLPLite, and I miss the first data bit that I need to capture (and that then subsequently b****** up the rest of the bit stream).

I would like to trigger the PWM from an incoming data (or clock) bit from within my native code to avoid the overheads just mentioned.

Any ideas?