temperature.c File Reference


Detailed Description

AVR32 UC3 Control Panel temperature sensor module.

Author:
Atmel Corporation: http://www.atmel.com
Support and FAQ: http://support.atmel.no/

Definition in file temperature.c.

#include <string.h>
#include "compiler.h"
#include "FreeRTOS.h"
#include "datalog.h"
#include "shell.h"
#include "board.h"
#include "gpio.h"
#include "adc.h"
#include "sensor.h"
#include "config_file.h"
#include "fsaccess.h"
#include "BasicSMTP.h"

Go to the source code of this file.

Defines

#define SENSOR_TEMP_CONFIG_FILE   "A:/CFG/TEMP.TXT"
#define TEMPERATURE_GETCONF_MAXLEN   64

Functions

Bool b_temperature_get_value (xLogDef *pxLog)
 Get the current temperature value.
Bool b_temperature_init (void)
 Init the temperature channel.
eExecStatus e_temperature_get_config (signed portCHAR **ppcStringReply)
 Get the temperature sensor config.
eExecStatus e_temperature_set_config (signed portCHAR **ppcStringReply, int ac, signed portCHAR *av[])
 Set the sensor config.
int sprintf (char *out, const char *format,...)

Variables

static volatile avr32_adc_t * adc = (volatile avr32_adc_t *) &AVR32_ADC
static Bool b_temp_alarm = pdFALSE
static Bool b_temp_alarm_max = pdFALSE
static Bool b_temp_alarm_min = pdFALSE
static int l_temp_max = 100
static int l_temp_min = -50
const unsigned short temperature_code []
 The following table give the correspondance between the ADC code and the temperature Each elements of the table corresponds to an ADC code value.
unsigned int ul_temp_lograte = 5
xSemaphoreHandle xCFGMutex
 The CFG system mutex.


Define Documentation

#define SENSOR_TEMP_CONFIG_FILE   "A:/CFG/TEMP.TXT"

Path for the config file

Definition at line 68 of file temperature.c.

Referenced by b_temperature_init(), and e_temperature_set_config().

#define TEMPERATURE_GETCONF_MAXLEN   64

Max string length of a get config.

Definition at line 65 of file temperature.c.

Referenced by e_temperature_get_config().


Function Documentation

Bool b_temperature_get_value ( xLogDef pxLog  ) 

Get the current temperature value.

Parameters:
pxLog a Log structure.
Returns:
TRUE upon success, FALSE if error.

Definition at line 283 of file temperature.c.

References adc, b_temp_alarm, b_temp_alarm_max, b_temp_alarm_min, l_temp_max, l_temp_min, LogDef::pcStringLog, LogDef::pfFreeStringLog, sprintf(), temperature_code, and v_SMTP_Post().

Referenced by b_sensor_get_value().

00284 {
00285    int i_current_val, value, index = 0;
00286 
00287 
00288    /* enable channel for sensor */
00289    adc_enable( adc, ADC_TEMPERATURE_CHANNEL );
00290    // start conversion
00291    adc_start( adc );
00292    // get value for sensor
00293    value = adc_get_value( adc, ADC_TEMPERATURE_CHANNEL );
00294    /* Disable channel for sensor */
00295    adc_disable( adc, ADC_TEMPERATURE_CHANNEL );
00296    
00297    if(value > temperature_code[0])
00298    {
00299         i_current_val = -20;
00300    }
00301    else
00302    {
00303       while(temperature_code[index++] > value);
00304       i_current_val = (index - 1 - 20);
00305    }
00306 
00307    // Alloc memory for the log string.
00308    pxLog->pcStringLog = pvPortMalloc( 12*sizeof( char ) );
00309    if( NULL == pxLog->pcStringLog )
00310    {
00311       return( FALSE );
00312    }
00313    pxLog->pfFreeStringLog = vPortFree; // Because pvPortMalloc() was used to
00314                                        // alloc the log string.
00315 
00316    // Build the log string.
00317    if( i_current_val <= l_temp_min )
00318    {
00319       sprintf( pxLog->pcStringLog, "%3dC | min", i_current_val );
00320       // if alarms have to be checked and no alarm for min was pending
00321       if (( b_temp_alarm == pdTRUE ) && ( b_temp_alarm_min == pdFALSE ))
00322       {
00323         // alarm has been taken into account, 
00324         // don't reenter this test before leaving min area
00325         b_temp_alarm_min = pdTRUE;
00326         // allow alarm if max is reached
00327         b_temp_alarm_max = pdFALSE;
00328         // post alarm to SMTP task
00329         v_SMTP_Post("Min Temp Alarm", NULL);
00330       }
00331    }
00332    else if( i_current_val >= l_temp_max )
00333    {
00334       sprintf( pxLog->pcStringLog, "%3dC | max", i_current_val );
00335       // if alarms have to be checked and no alarm for max was pending
00336       if (( b_temp_alarm == pdTRUE ) && ( b_temp_alarm_max == pdFALSE ))
00337       {
00338         // alarm has been taken into account, 
00339         // don't reenter this test before leaving max area
00340         b_temp_alarm_max = pdTRUE;
00341         // allow alarm if min is reached
00342         b_temp_alarm_min = pdFALSE;
00343         // post alarm to SMTP task
00344         v_SMTP_Post("Max Temp Alarm", NULL);
00345       }
00346    }
00347    else
00348    {
00349       sprintf( pxLog->pcStringLog, "%3dC", i_current_val );
00350       // if alarms have to be checked
00351       if ( b_temp_alarm == pdTRUE )
00352       {
00353         // no alarm is pending 
00354         b_temp_alarm_max = pdFALSE;
00355         b_temp_alarm_min = pdFALSE;
00356       }        
00357    }
00358 
00359    return( TRUE );
00360 }

Bool b_temperature_init ( void   ) 

Init the temperature channel.

Returns:
TRUE upon success, FALSE if error.

Definition at line 122 of file temperature.c.

References b_temp_alarm, config_file_get_value(), l_temp_max, l_temp_min, SENSOR_TEMP_CONFIG_FILE, ul_temp_lograte, x_supervisor_SemaphoreGive(), x_supervisor_SemaphoreTake(), and xCFGMutex.

00123 {
00124 portCHAR token[6];
00125 portCHAR * unit;
00126 
00127    // Get the xCFGMutex.
00128    if( pdTRUE == x_supervisor_SemaphoreTake( xCFGMutex, 0 ) )
00129    {
00130        // get the field
00131        if (config_file_get_value(SENSOR_TEMP_CONFIG_FILE, "alarm" , token) >= 0)
00132        {
00133          // update value
00134          if (!strcmp(token, "on"))
00135          {
00136            b_temp_alarm = pdTRUE;
00137          }
00138        }
00139        if (config_file_get_value(SENSOR_TEMP_CONFIG_FILE, "min" , token) >= 0)
00140        {
00141          unit = strpbrk(token , "C");
00142          if (unit != NULL)
00143          {
00144            *unit = '\0';
00145          }
00146          l_temp_min = atol(token);
00147        }
00148        if (config_file_get_value(SENSOR_TEMP_CONFIG_FILE, "max" , token) >= 0)
00149        {
00150          unit = strpbrk(token , "C");
00151          if (unit != NULL)
00152          {
00153            *unit = '\0';
00154          }
00155          l_temp_max = atol(token);
00156        }
00157        if (config_file_get_value(SENSOR_TEMP_CONFIG_FILE, "lograte" , token) >= 0)
00158        {
00159          ul_temp_lograte = atoi(token);
00160        }
00161      // Release the xCFGMutex.
00162      x_supervisor_SemaphoreGive( xCFGMutex );
00163    }
00164    /* enable pin for sensor */
00165    gpio_enable_module_pin( ADC_TEMPERATURE_PIN , ADC_TEMPERATURE_FUNCTION );
00166 
00167    return (TRUE);
00168 }

eExecStatus e_temperature_get_config ( signed portCHAR **  ppcStringReply  ) 

Get the temperature sensor config.

Parameters:
ppcStringReply Input/Output. The response string. NEVER NULL AS INPUT. A malloc for the response string is performed here; the caller must free this string.
Returns:
the status of the command execution.

Definition at line 179 of file temperature.c.

References b_temp_alarm, l_temp_max, l_temp_min, SHELL_ERRMSG_MEMALLOC, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK, sprintf(), TEMPERATURE_GETCONF_MAXLEN, and ul_temp_lograte.

00180 {
00181    // Alloc space for the reply.
00182    *ppcStringReply = (signed portCHAR *)pvPortMalloc( TEMPERATURE_GETCONF_MAXLEN );
00183    if( NULL == *ppcStringReply )
00184    {
00185       *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_MEMALLOC;
00186       return( SHELL_EXECSTATUS_KO );
00187    }
00188    // Build the string.
00189    sprintf( (char *)*ppcStringReply, "lograte=%d\r\n""min=%dC\r\n""max=%dC\r\n""alarm=%s\r\n",
00190             ul_temp_lograte, l_temp_min, l_temp_max, ((b_temp_alarm == pdTRUE) ? "on" : "off") );
00191    return( SHELL_EXECSTATUS_OK );
00192 }

eExecStatus e_temperature_set_config ( signed portCHAR **  ppcStringReply,
int  ac,
signed portCHAR *  av[] 
)

Set the sensor config.

Parameters:
ppcStringReply Input/Output. The response string. NEVER NULL AS INPUT.
ac Input. Number of args
av Input. pointer to args
Returns:
the status of the command execution.

Definition at line 203 of file temperature.c.

References b_temp_alarm, config_file_set_value(), l_temp_max, l_temp_min, SENSOR_MSG_ALARM_OFF, SENSOR_MSG_ALARM_ON, SENSOR_MSG_CONFIG_SET, SENSOR_TEMP_CONFIG_FILE, SHELL_ERRMSG_CONFIGERROR, SHELL_EXECSTATUS_KO, SHELL_EXECSTATUS_OK_NO_FREE, and ul_temp_lograte.

00204 {
00205 portCHAR * token;
00206 
00207 
00208     if (config_file_set_value(SENSOR_TEMP_CONFIG_FILE, ac, av) != 0)
00209     {
00210       *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00211       // return error
00212       return( SHELL_EXECSTATUS_KO );
00213     }
00214 
00215   // alarm field
00216   if (!strcmp((char *)av[0] , "alarm"))
00217   {
00218     if (!strcmp((char *)av[1] , "on"))
00219     {
00220       b_temp_alarm = pdTRUE;
00221       *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_ON;
00222       return( SHELL_EXECSTATUS_OK_NO_FREE );
00223     }
00224     else if (!strcmp( (char *)av[1], "off"))
00225     {
00226       b_temp_alarm = pdFALSE;
00227       *ppcStringReply = (signed portCHAR *)SENSOR_MSG_ALARM_OFF;
00228       return( SHELL_EXECSTATUS_OK_NO_FREE );
00229     }
00230     else
00231     {
00232       *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00233       return( SHELL_EXECSTATUS_KO );
00234     }
00235   }
00236   // lograte field
00237   else if (!strcmp((char *)av[0] , "lograte"))
00238   {
00239     ul_temp_lograte = atoi((char *)av[1]);
00240     *ppcStringReply = (signed portCHAR *)SENSOR_MSG_CONFIG_SET;
00241     return( SHELL_EXECSTATUS_OK_NO_FREE );
00242   }
00243   // min field
00244   else if (!strcmp((char *)av[0] , "min"))
00245   {
00246     token = strpbrk((char *)av[1] , "C");
00247     if (token != NULL)
00248     {
00249       *token = '\0';
00250     }
00251     l_temp_min = atoi((char *)av[1]);
00252     *ppcStringReply = (signed portCHAR *)SENSOR_MSG_CONFIG_SET;
00253     return( SHELL_EXECSTATUS_OK_NO_FREE );
00254   }
00255   // max field
00256   else if (!strcmp((char *)av[0] , "max"))
00257   {
00258     token = strpbrk((char *)av[1] , "C");
00259     if (token != NULL)
00260     {
00261       *token = '\0';
00262     }
00263     l_temp_max = atoi((char *)av[1]);
00264     *ppcStringReply = (signed portCHAR *)SENSOR_MSG_CONFIG_SET;
00265     return( SHELL_EXECSTATUS_OK_NO_FREE );
00266   }
00267   // unknown field : error
00268   else
00269 
00270   {
00271     *ppcStringReply = (signed portCHAR *)SHELL_ERRMSG_CONFIGERROR;
00272     return( SHELL_EXECSTATUS_KO );
00273   }
00274 }

int sprintf ( char *  out,
const char *  format,
  ... 
)

Definition at line 293 of file printf-stdarg.c.

00294 {
00295         va_list args;
00296         
00297         va_start( args, format );
00298         return print( &out, format, args );
00299 }


Variable Documentation

volatile avr32_adc_t* adc = (volatile avr32_adc_t *) &AVR32_ADC [static]

ADC cell.

Definition at line 108 of file temperature.c.

Bool b_temp_alarm = pdFALSE [static]

Bool b_temp_alarm_max = pdFALSE [static]

Alarm for max is pending ?

Definition at line 103 of file temperature.c.

Referenced by b_temperature_get_value().

Bool b_temp_alarm_min = pdFALSE [static]

Alarm for min is pending ?

Definition at line 105 of file temperature.c.

Referenced by b_temperature_get_value().

int l_temp_max = 100 [static]

Max Value for alarm.

Definition at line 98 of file temperature.c.

Referenced by b_temperature_get_value(), b_temperature_init(), e_temperature_get_config(), and e_temperature_set_config().

int l_temp_min = -50 [static]

Min value for alarm.

Definition at line 95 of file temperature.c.

Referenced by b_temperature_get_value(), b_temperature_init(), e_temperature_get_config(), and e_temperature_set_config().

const unsigned short temperature_code[]

Initial value:

{
   0x3B4,0x3B0,0x3AB,0x3A6,0x3A0,0x39A,0x394,0x38E,0x388,0x381,0x37A,0x373,
   0x36B,0x363,0x35B,0x353,0x34A,0x341,0x338,0x32F,0x325,0x31B,0x311,0x307,
   0x2FC,0x2F1,0x2E6,0x2DB,0x2D0,0x2C4,0x2B8,0x2AC,0x2A0,0x294,0x288,0x27C,
   0x26F,0x263,0x256,0x24A,0x23D,0x231,0x225,0x218,0x20C,0x200,0x1F3,0x1E7,
   0x1DB,0x1CF,0x1C4,0x1B8,0x1AC,0x1A1,0x196,0x18B,0x180,0x176,0x16B,0x161,
   0x157,0x14D,0x144,0x13A,0x131,0x128,0x11F,0x117,0x10F,0x106,0xFE,0xF7,
   0xEF,0xE8,0xE1,0xDA,0xD3,0xCD,0xC7,0xC0,0xBA,0xB5,0xAF,0xAA,0xA4,0x9F,
   0x9A,0x96,0x91,0x8C,0x88,0x84,0x80,0x7C,0x78,0x74,0x71,0x6D,0x6A,0x67,
   0x64,0x61,0x5E,0x5B,0x58,0x55,0x53,0x50,0x4E,0x4C,0x49,0x47,0x45,0x43,
   0x41,0x3F,0x3D,0x3C,0x3A,0x38}
The following table give the correspondance between the ADC code and the temperature Each elements of the table corresponds to an ADC code value.

The index in the table give the corresponding temperature (inC) with the following formula : Tempeature=index-20. table[O] corresponds to -20C temperature code The following table gives the ADC code for VCC=3.3V and Aref=AVcc

Definition at line 79 of file temperature.c.

Referenced by b_temperature_get_value().

unsigned int ul_temp_lograte = 5

Value for the log rate.

Definition at line 92 of file temperature.c.

Referenced by b_temperature_init(), e_temperature_get_config(), e_temperature_set_config(), and portTASK_FUNCTION().

xSemaphoreHandle xCFGMutex


Generated on Fri Feb 19 02:22:48 2010 for AVR32 - Control Panel demonstration. by  doxygen 1.5.5