Go Down

Topic: DuinOS: small and simple rtos (Read 51 times) previous topic - next topic

Mark G.

Julian,

Thanks for taking a look!

Here's what I have:

Code: [Select]

#include <DuinOS.h>
 #include <DuinOS/queue.h>
 #include <DuinOS/semphr.h>


 
 #define PIR_SENSOR_0  B00000001
 #define PIR_SENSOR_1  B00000010  
 #define STACK_SIZE 250
 
 #define pinLED  13
 
 #define TESTBED
 
volatile byte PIR_state = 0;

xSemaphoreHandle sem_PIR = NULL;


 //****************************************
 // Name: PRI_sensor_task()
 // Description:
 //   This is the task that processes the sensor reading done by the ISR
 // Creation Date:  2009/12/13
 // History:
 //    2009/12/13 - Created.
 //****************************************
  taskLoop(PIR_sensor_task)
  {
      byte  local_PIR_state, sensor_state;
       
     vSemaphoreCreateBinary(sem_PIR);

     if( sem_PIR == NULL )
     {
        // set an LED to indicate error
     }


      for( ;; )
      {
          // Here we'll take the sensor state and send it along to the MCP
          if (xSemaphoreTake(sem_PIR, portMAX_DELAY) == pdTRUE)
          {
            // enter a critical section and grab the state of the PIR sensors
            taskENTER_CRITICAL();
            local_PIR_state = PIR_state;
            taskEXIT_CRITICAL();
 
            // Send the sensor status
#ifdef TESTBED
             sensor_state = (local_PIR_state & B00000001);
             local_PIR_state = local_PIR_state >> 1;
             if (sensor_state == 0) {
                Serial.println("  - Sensor is OFF - ");
                digitalWrite(pinLED, LOW);
             }
             else {
                Serial.println("  - Sensor is ON  - ");
                digitalWrite(pinLED, HIGH);
             }
#endif
            //Serial.println(PIR_state, HEX);
          }  
          else
          {
            Serial.println("*****UNBLOCKED*****");
          }      
      }
  }
 

 //****************************************
 // Name: PRI0_interrupt_CHANGE()
 // Description:
 //  ISR to handle changes in sensor reading.
 // Creation Date:  2009/12/13
 // History:
 //    2009/12/13 - Created.
 //****************************************
 void PIR0_interrupt_CHANGE()
 {
   
  static signed portBASE_TYPE xHigherPriorityTaskWoken;
 
   // Here we handle the PIR sensor notification on falling
   xHigherPriorityTaskWoken = pdFALSE;
 
   PIR_state = PIR_state ^ PIR_SENSOR_0;

   xSemaphoreGiveFromISR(sem_PIR, &xHigherPriorityTaskWoken);
 }
 
 
//****************************************
 // Name: setup()
 // Description:
 //  standard setup function.
 // Creation Date:  2009/12/13
 // History:
 //    2009/12/13 - Created.
 //****************************************
 void setup()
 {
   pinMode(pinLED, OUTPUT);
   digitalWrite(pinLED, LOW);
   
   // Attach the interrupts for the first PIR sensor.  We want to know when it's on and off.
   attachInterrupt(0, PIR0_interrupt_CHANGE, CHANGE);

   createTaskLoop(PIR_sensor_task, NORMAL_PRIORITY);
   
   // Set up serial connection with our MCP
   Serial.begin(19200);

 }
 
 
//****************************************
 // Name: loop()
 // Description:
 //  standard loop function.
 // Creation Date:  2009/12/13
 // History:
 //    2009/12/13 - Created.
 //****************************************
 void loop()
 {
     Serial.println("Looping...");
     delay(1000);
   
 }


I was able to get it working as is but notice the println() in the PRI_sensor_task() has been commented out.  Uncommenting that will crash the uC.

juliandasilva

Hi Mark, I think the problem is not with the printl(), we tested the following code, and it worked ok (with a Seeeduino Mega):

Code: [Select]

#define pinLED  13

volatile byte PIR_state = 0;

taskLoop(PIR_sensor_task)
{
     Serial.println("Task...");
     Serial.println(PIR_state+1, HEX);
     delay(1000);
}
 
 
void setup()
 {
   pinMode(pinLED, OUTPUT);
   digitalWrite(pinLED, LOW);
   
   // Attach the interrupts for the first PIR sensor.  We want to know when it's on and off.
   //attachInterrupt(0, PIR0_interrupt_CHANGE, CHANGE);

   createTaskLoop(PIR_sensor_task,LOW_PRIORITY);
   
   // Set up serial connection with our MCP
   Serial.begin(19200);

 }
 
 void loop()
 {
     Serial.println("Looping...");
     Serial.println(PIR_state, HEX);
     delay(1000);
 }


Does your sample hang the mcu at the begining, or it works for a while and then hangs?

Regards,
Julián



Mark G.

#92
Dec 31, 2009, 06:25 am Last Edit: Dec 31, 2009, 06:26 am by mcgski Reason: 1
Julian,

Try this.  I think it has something to do with the semaphore or maybe the digitalWrite()?!?

Looks like the main loop() never gets back from the println() as the LED never goes to LOW.

Code: [Select]

#include <DuinOS.h>
#include <DuinOS/queue.h>
#include <DuinOS/semphr.h>


#define pinLED  13

volatile byte PIR_state = 0;
xSemaphoreHandle sem_pintest = NULL;

taskLoop(PIR_sensor_task)
{
     vSemaphoreCreateBinary(sem_pintest);

     if( sem_pintest == NULL )
     {
        Serial1.println("Semaphonre not created in task()");
     }
     
     for(;;) {
       if (xSemaphoreTake(sem_pintest, portMAX_DELAY) == pdTRUE) {
     
         Serial.println("Task...");
         Serial.println(PIR_state+1, HEX);
         digitalWrite(pinLED, HIGH);
         delay(1000);
       }
     }
}
 
 
void setup()
 {
   pinMode(pinLED, OUTPUT);
   digitalWrite(pinLED, LOW);
   
   // Attach the interrupts for the first PIR sensor.  We want to know when it's on and off.
   //attachInterrupt(0, PIR0_interrupt_CHANGE, CHANGE);

   createTaskLoop(PIR_sensor_task,LOW_PRIORITY);
   
   // Set up serial connection with our MCP
   Serial.begin(19200);

 }
 
 void loop()
 {
     Serial.println("Looping...");
     Serial.println(PIR_state, HEX);
     delay(1000);
     digitalWrite(pinLED, LOW);
     xSemaphoreGive(sem_pintest);

 }




Chumbud

One thing you should be aware of which may or may not be the problem here is that the macro that replaces delay in DuinOS assumes the task scheduler has already been started and the task scheduler is not started until after setup is called.  If any code calls the delay routine in Setup your app will freeze.

Mark G.

Thanks Chumbud.  Didn't know that but it doesn't seem to be the problem.  My setup() is clean.  The only delays are in the task and in loop().

Go Up