thermo.c

Go to the documentation of this file.
00001 
00041 /*#*
00042  *#* $Log$
00043  *#* Revision 1.3  2006/09/20 20:12:41  marco
00044  *#* Names convention, MOD_* macros.
00045  *#*
00046  *#* Revision 1.2  2006/07/19 12:56:26  bernie
00047  *#* Convert to new Doxygen style.
00048  *#*
00049  *#* Revision 1.1  2005/11/04 17:59:47  bernie
00050  *#* Import into DevLib.
00051  *#*
00052  *#*/
00053 #include <thermo_map.h>
00054 #include <hw_thermo.h>
00055 
00056 #include <drv/thermo.h>
00057 #include <drv/timer.h>
00058 #include <drv/ntc.h>
00059 
00060 #include <cfg/macros.h>
00061 #include <cfg/debug.h>
00062 
00063 
00065 #define THERMO_INTERVAL_MS      100
00066 
00068 #define THERMO_HIFI_NUM_SAMPLES 10
00069 
00071 static Timer thermo_timer;
00072 
00073 typedef struct ThermoControlDev
00074 {
00075     deg_t          hifi_samples[THERMO_HIFI_NUM_SAMPLES];
00076     deg_t          cur_hifi_sample;
00077     deg_t          target;
00078     thermostatus_t status;
00079     ticks_t        expire;
00080 } ThermoControlDev;
00081 
00083 ThermoControlDev devs[THERMO_CNT];
00084 
00085 
00089 thermostatus_t thermo_status(ThermoDev dev)
00090 {
00091     return devs[dev].status;
00092 }
00093 
00094 
00098 static void thermo_do(ThermoDev index)
00099 {
00100     ThermoControlDev* dev = &devs[index];
00101     deg_t cur_temp;
00102     deg_t tolerance = thermo_hw_tolerance(index);
00103 
00104     cur_temp = thermo_hw_read(index);
00105 
00106     // Store the sample into the hifi FIFO buffer for later interpolation
00107     dev->hifi_samples[dev->cur_hifi_sample] = cur_temp;
00108     if (++dev->cur_hifi_sample == THERMO_HIFI_NUM_SAMPLES)
00109         dev->cur_hifi_sample = 0;
00110 
00111     cur_temp = thermo_readTemperature(index);
00112 
00113     if (cur_temp == NTC_SHORT_CIRCUIT || cur_temp == NTC_OPEN_CIRCUIT)
00114     {
00115         if (cur_temp == NTC_SHORT_CIRCUIT)
00116         {
00117             #ifdef _DEBUG
00118             if (!(dev->status & THERMOERRF_NTCSHORT))
00119                 kprintf("dev[%d], thermo_do: NTC_SHORT\n",index);
00120             #endif
00121             dev->status |= THERMOERRF_NTCSHORT;
00122         }
00123         else
00124         {
00125             #ifdef _DEBUG
00126             if (!(dev->status & THERMOERRF_NTCOPEN))
00127                 kprintf("dev[%d], thermo_do: NTC_OPEN\n", index);
00128             #endif
00129             dev->status |= THERMOERRF_NTCOPEN;
00130         }
00131 
00132         /* Reset timeout when there is an ntc error */
00133         dev->expire = thermo_hw_timeout(index) + timer_clock();
00134         thermo_hw_off(index);
00135         return;
00136     }
00137     dev->status &= ~(THERMOERRF_NTCOPEN | THERMOERRF_NTCSHORT);
00138 
00139     if ((cur_temp < dev->target - tolerance) || (cur_temp > dev->target + tolerance))
00140     {
00141         dev->status &= ~THERMO_TGT_REACH;
00142 
00143         /* Check for timeout */
00144         if (timer_clock() - dev->expire > 0)
00145         {
00146             dev->status |= THERMOERRF_TIMEOUT;
00147             kprintf("dev[%d], thermo_do: TIMEOUT\n", index);
00148         }
00149     }
00150     else /* In target */
00151     {
00152         /* Clear errors */
00153         dev->status &= ~THERMO_ERRMASK;
00154         dev->status |= THERMO_TGT_REACH;
00155 
00156         /* Reset timeout in case we go out of target in the future */
00157         dev->expire = thermo_hw_timeout(index) + timer_clock();
00158     }
00159 
00160     if (cur_temp < dev->target)
00161         dev->status = (dev->status | THERMO_HEATING) & ~THERMO_FREEZING;
00162     else
00163         dev->status = (dev->status & ~THERMO_HEATING) | THERMO_FREEZING;
00164 
00165     thermo_hw_set(index, dev->target, cur_temp);
00166 
00167 }
00168 
00169 
00173 static void thermo_softint(void)
00174 {
00175     int i;
00176     for (i = 0; i < THERMO_CNT; ++i)
00177         if (devs[i].status & THERMO_ACTIVE)
00178             thermo_do((ThermoDev)i);
00179 
00180     timer_add(&thermo_timer);
00181 }
00182 
00183 
00187 void thermo_setTarget(ThermoDev dev, deg_t temperature)
00188 {
00189     ASSERT(dev < THERMO_CNT);
00190     devs[dev].target = temperature;
00191     devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
00192 
00193     kprintf("setTarget dev[%d], T[%d.%d]\n", dev, temperature / 10, temperature % 10);
00194 }
00195 
00199 void thermo_start(ThermoDev dev)
00200 {
00201     int i;
00202     deg_t temp;
00203 
00204     ASSERT(dev < THERMO_CNT);
00205 
00206     devs[dev].status |= THERMO_ACTIVE;
00207 
00208     /* Initialize the hifi FIFO with a constant value (the current temperature) */
00209     temp = thermo_hw_read(dev);
00210     for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; ++i)
00211         devs[dev].hifi_samples[i] = temp;
00212     devs[dev].cur_hifi_sample = 0;
00213 
00214     /* Reset timeout */
00215     devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
00216 }
00217 
00221 void thermo_stop(ThermoDev dev)
00222 {
00223     ASSERT(dev < THERMO_CNT);
00224 
00225     devs[dev].status &= ~THERMO_ACTIVE;
00226     thermo_hw_off(dev);
00227 }
00228 
00229 
00233 void thermo_clearErrors(ThermoDev dev)
00234 {
00235     ASSERT(dev < THERMO_CNT);
00236     devs[dev].status &= ~(THERMO_ERRMASK);
00237 }
00238 
00239 
00243 deg_t thermo_readTemperature(ThermoDev dev)
00244 {
00245     int i;
00246     long accum = 0;
00247 
00248     MOD_CHECK(thermo);
00249 
00250     for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; i++)
00251         accum += devs[dev].hifi_samples[i];
00252 
00253     return (deg_t)(accum / THERMO_HIFI_NUM_SAMPLES);
00254 }
00255 
00256 MOD_DEFINE(thermo)
00257 
00258 
00261 void thermo_init(void)
00262 {
00263     THERMO_HW_INIT;
00264 
00265     /* Set all status to off */
00266     for (int i = 0; i < THERMO_CNT; i++)
00267         devs[i].status = THERMO_OFF;
00268 
00269     MOD_INIT(thermo);
00270 
00271     timer_setDelay(&thermo_timer, ms_to_ticks(THERMO_INTERVAL_MS));
00272     timer_set_event_softint(&thermo_timer, (Hook)thermo_softint, 0);
00273     timer_add(&thermo_timer);
00274 }