Page 1 of 1

Code halting immediately upon run after adding a task

PostPosted: Tue Nov 08, 2011 1:13 pm
by etran42
Hello,

I've recently started using Salvo. I was able to set up the hardware to the point where I could compile and run a sample code through an ICD2 debugger. The sample code has 3 tasks (1 blinks an LED, and two others deal with an SD card). I was trying to use this as a baseline and wanted to add a new task to it. I want the new task to utilize the AD converter. I wrote the following task:

Code: Select all
/******************************************************************************

task_adc()

Takes a sensor reading from AN5

******************************************************************************/
#include "main.h"
#include "init.h"
#include "task_adc.h"

// Pumpkin CubeSat Kit headers
#include "csk_led.h"
#include "csk_uart.h"
#include "csk_adc.h"

// Pumpkin Salvo headers
#include "salvo.h"


/******************************************************************************
****                                                                       ****
**                                                                           **
task_adc()

Task that takes a sensor reading and sends it out of UART1

**                                                                           **
****                                                                       ****
******************************************************************************/
void task_adc(void) {
   
   initADC();
     int c;
     while (1) {
   
    c = readADC();
    OS_Delay(100);
//    uart1_puts(c);
    OS_Delay(100);   

  } /* while */

} /* task_adc() */


Code: Select all
/*******************************

ADC Initialization Task

*******************************/

#include "salvo.h"
#include "main.h"
#include "init.h"
#include "events.h"
#include <p33FJ256GP710.h>



void initADC(void) {
   /* ADC Initialization */
   
   AD1CON1bits.AD12B = 0;      //Select 10-bit mode
   AD1PCFGLbits.PCFG0 = 0;         //Set AN5 as analog input
   AD1PCFGLbits.PCFG1 = 0;
   AD1PCFGLbits.PCFG2 = 0;
   AD1PCFGLbits.PCFG3 = 0;
   AD1PCFGLbits.PCFG4 = 0;
   AD1PCFGLbits.PCFG5 = 0;
   
   AD1CON2bits.VCFG = 0;         //Use AVdd+ and AVss-
   AD1CON3 = 0x8C00;         //Set sample time/clock source
   AD1CON2bits.CHPS = 0;         //Convert CH0
   AD1CON1bits.SSRC = 7;         //Select auto-convert
   AD1CON1bits.FORM = 0;         //Set output as unsigned int
   AD1CHS0 = 0x0085;         //Set AN5 as CH0 for scan
   AD1CSSL = 0x0010;         //Select AN5 for input scan

}
/* ADC Sampling */

int readADC(void) {
   
   int ADCValue=0;
   
   AD1CON1bits.ADON = 1;         //Turns ADC module on
   AD1CON1bits.SAMP = 1;         //Start sampling...
   while(!AD1CON1bits.DONE)      //Is the conversion done?
      {
      ADCValue = ADC1BUF0;      //Yes, store the value
      }
      
   AD1CON1bits.ADON = 0;         //Turn ADC module off
   
   /* Close ADC */
   return ADCValue;            //ADCValue is the output
}


I tried to mimic the style of the sample tasks, to minimize errors on my part. I was able to compile the project. However, when I tried to run the program through the debugger, it would immediately halt. There were no errors, no comments about the code, or anything of that sort. I uncommented the line in the main code:

Code: Select all
  OSCreateTask(task_adc,      TASK_ADC_P,          5);


to see if it was this new task that was causing this issue, and it was. When this line is uncommented, the code runs as usual and the LED blinks as it's supposed to. Once I add this task back to the scheduler, the code halts immediately upon running without any explanation. What am I doing wrong?

Re: Code halting immediately upon run after adding a task

PostPosted: Fri Nov 11, 2011 5:34 pm
by Andrew
1. Please don't declare local variables after function calls inside a function.

2. You need to make 'c' static, otherwise it will not maintain its value across Salvo context switches.

3. Did you increase OSTASKS in your salvocfg.h?

Re: Code halting immediately upon run after adding a task

PostPosted: Mon Nov 14, 2011 12:24 pm
by etran42
Thanks for the help! I updated the OSTASKS value in salvocfg.h and now the code is running as expected. The LED task indicates that the scheduler is functioning, at least. I'm still having trouble getting my ADC task to function correctly, though.

You mentioned that I should declare a "static int c" so that the value of the ADC value would be retained between context switches. I assume that the declaration should go in the task itself?

Re: Code halting immediately upon run after adding a task

PostPosted: Tue Nov 15, 2011 8:16 am
by Andrew
Yes.

Salvo gets by with so little RAM by having no task stacks. The downside of that is that local variables (which are normally stored on a stack) are potentially obliterated between context switches (because the RAM where they lived may be used as a stack by another task in the interim, etc.). Therefore, if you want a local (i.e., declared inside of a task) variable to persist across context switches, declare it as static.

Whjether you do function- or file-scope static declarations, the net result of where it goes in RAM is the same ... but the visibility will differ.