[sdiy] RTOS (was Re: sine wave floating point conversion issue)
Tom Wiltshire
tom at electricdruid.net
Sat Feb 25 22:23:30 CET 2012
What advantages do you find with a full RTOS, Matthew?
I generally use a tick-based scheduling algorithm without priority and find that sufficient. I'd begin to wonder if I was doing the right thing if it wasn't enough for the task(s) at hand, to be honest. I'd probably try and rethink the problem into a simpler solution rather than use a more powerful structure.
It looks like this:
//=============================================================
// Process Timing
//=============================================================
// Process Timing bitmasks
#define SCAN_KNOBS 128 // Process analog inputs
#define CHECK_SERIAL 64 // Check for received serial messages
#define CHECK_THING1 32 // Do something
#define CHECK_THING2 16 // Do something
// Set up an array to define the timing
static int process_timing[] = { 0b10100000,
0b01100000,
0b10100000,
0b01110000};
// For what each bit means, see above - I'm not using all the available width, obviously.
// Set up the Process Timing Clock
int clock = 0;
// Loop forever
while(1)
{
// Has the Timer counted the process timing period?
if (IFS0bits.T1IF == 1) {
IFS0bits.T1IF = 0; // Clear period match flag
// Move onto the next tick of the Process Timing Clock
clock++;
clock &= 0x3; // Wrap around in 2 bits (4 tick cycle)
// Get the flags from the Process Timing array
int timing = process_timing[clock];
// Now we use the flags to determine which processes should run
//=============================================================
// PROCESS: DO SOMETHING
//=============================================================
if ((timing & CHECK_THING1) > 0) {
// So do it
}
//=============================================================
// PROCESS: DO SOMETHING ELSE
//=============================================================
if ((timing & CHECK_THING2) > 0) {
// So do it
}
// Include as many processes as you need.
}
}
I have a timer running to provide the timebase for this, and I can set up certain events to occur more frequently than others in the process_timing array. In the example above, for instance, the SCAN_KNOBS and CHECK_SERIAL alternate, CHECK_THING1 happens every time, and CHECK_THING2 only happens every fourth time.
I've mostly used a 1ms timebase, but sometimes used a 0.5ms timebase, with the majority of tasks happening every second cycle.
A structure like this seems to me to provide most of the benefits whilst being extremely simple - unless I'm missing something important? Hence the question.
Thanks,
Tom
On 25 Feb 2012, at 19:52, Matthew Smith wrote:
> I guess, for the sake of completeness, I should ask if you REALLY want to be calculating sines at all, unless you have a way overpowered system with more spare CPU cycles than you know what to do with.
>
> Don't know what frequency range you will be working over, but if you were to set up an array of 4096 integers, rather than go through the heartache of doing infinite series to get your sines, you could do FAR less math just figuring out when to grab the next value from the array. And, as you go up in frequency, what your increment value is - because you sure won't need every single one of 4k samples up the top end of the keyboard.
>
> I tend to regard working out sines on the fly to be a case of re-calculating the wheel, but then I was brought up on Godfrey & Siddon's Four Figure Tables ;-)
>
> By the way, Dan, have you considered using an RTOS for this type of work? Whilst I have yet to implement one, my reading suggests that they are far less scary than I had thought - and really wish I'd started using them before I had started any of my current projects. If you're still using AVRs, FreeRTOS is know to work on the ATMega128 (just enough RAM.)
>
> Cheers
>
> M
>
> --
> Matthew Smith
>
> Business: http://www.smiffytech.com
> Blog: http://www.smiffysplace.com
> Linkedin: http://www.linkedin.com/in/smiffy
> Flickr: http://www.flickr.com/photos/msmiffy
> Twitter: http://twitter.com/smiffy
>
> ABN 16 391 203 815
> _______________________________________________
> Synth-diy mailing list
> Synth-diy at dropmix.xs4all.nl
> http://dropmix.xs4all.nl/mailman/listinfo/synth-diy
More information about the Synth-diy
mailing list