10 years, 10 months ago.

Priorities and Threads

Hello guys ,

I have currently two threads running , one with priority of normal and one with high , I thought that this way I would have two processes going on concurrently and one of them with just higher cpu time than the other . It turns out the one with normal priority is not even being processed for some reason . Any ideas ? How am I supposed to make the second process work ?

title:processUnit.cpp

#include "mbed.h"
#include "BurstSPI.h"
//#include "Threads.h"
#include "rtos.h"
Serial pc(USBTX, USBRX);
AnalogIn Vin(p18);
DigitalOut CS(p35);
DigitalOut LDR(p30);
int output=0x0000 ;
int next=1;
int i=0;
int data=0x0000;
BurstSPI device(p5, p6, p7); // mosi, miso, sclk
void inputSignal(void const *args)
{
    float data[100];
    float inpt;
    while(i<100) {
        float inpt=Vin.read();
        data[i]=inpt;
        i++;
        //pc.printf("%f \n",inpt);
    }
    i=0;
    while(i<100) {
        pc.printf("%f \n",data[i]);
        i++;
}
}
void outputSignal(void const *args)
{
    device.format(16,0);
    device.frequency(48000000);
    device.setFormat();
    LDR=1;

    while(1) {
        CS=0;

        data=output|0x3000;
        device.fastWrite(data);
        device.clearRX();

        CS=1;
        if (next==1) {
            output=output+0x000F;
        } else if (next==2) {
            output=output-0x000F;
        }
        if (output>=0x0fff) {
            //output=0x0000;
            next=2;
        } else if (output<=0x0000) {
            next=1;
        }

        i++;
        LDR=0;
        LDR=1;
    }
}



///the main method
int main()
{
    
    Thread testSignal(outputSignal,0,osPriorityHigh);
    Thread compensateSignal(inputSignal);
    while(1){}
    //outputSignal(0);
    
       
   
}

2 Answers

10 years, 10 months ago.

Try adding something into the higher priority task to let it yield the CPU (say a wait command to prove the point). I can't see anything to stop the high priority running & so let the other priority thread execute...

This needs to be within the while (1) loop.

HTH

Jez

10 years, 10 months ago.

It didn't work . Both processes are now seemingly unresponsive.

It should work, if you use an RTOS wait, not a regular wait.

But it probably is a good idea to start with checking if you really want to use RTOS. If you just want one to be done first, then the next always, it is not needed to use RTOS for it.

RTOS is very handy when you got code which runs parallel to other code, but which does have significant idle periods in between.

posted by Erik - 09 Jan 2014