Show Posts
Pages: 1 [2]
16  Forum 2005-2010 (read only) / News / Re: DuinOS: small and simple rtos on: December 31, 2009, 12:18:52 pm
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().
17  Forum 2005-2010 (read only) / News / Re: DuinOS: small and simple rtos on: December 31, 2009, 12:25:15 am
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:
#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);

  }
 

18  Forum 2005-2010 (read only) / News / Re: DuinOS: small and simple rtos on: December 30, 2009, 12:03:57 am
Julian,

Thanks for taking a look!

Here's what I have:

Code:
#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.
19  Forum 2005-2010 (read only) / News / Re: DuinOS: small and simple rtos on: December 29, 2009, 02:03:05 am
Julian,

I'm having trouble with Serial.println() locking up my Seeeduino Mega.  I have a simple PIR on the pin 3 interrupt that toggles a state value when changed.  It gives a binary semaphore that the task loop is waiting on.  The Loop() routine is just spitting out text to let me know it's alive.

The task prints out a string followed by a byte hex value (eg. Serial.print(PIR_state, HEX) ).  Seems if I just spit out text is fine, but any value stops things up good.

To simplify things, I had the loop() function give the semaphore and the same thing happened - text worked, values did not.

I added the #ifdef statements you suggested for SerialHardware.cpp and .h.  Any thoughts?

Let me know if you'd like me to post on the bug tracker.
20  Forum 2005-2010 (read only) / News / Re: DuinOS: small and simple rtos on: December 13, 2009, 12:46:55 am
Julian,

Good stuff.  Thanks for doing this.  I'm an old embedded systems programmer who's been away from it for about 20+ years.  I cut my teeth with preemptive multitasking systems so it's good to have something familiar in the Arduino environment.

I've been reading on the FreeRTOS site and others and it looks like the taskENTER_CRITICAL() just disables interrupts.  Help me connect the stale synapses in my brain on the matter.  If I am updating data in an interrupt routine then I'm pretty safe but in tasks I need the critical sections to ensure nothing gets changed by an interrupt.  

What about println() functions?  Are they thread safe?  I got the impression they are not.

Anyway, keep up the good work.  Can't wait until your next rev.  Let us know if there's an area you'd like us to work on.
Pages: 1 [2]