thermo.c
Go to the documentation of this file.00001
00041 #include "hw/thermo_map.h"
00042 #include "hw/hw_thermo.h"
00043
00044 #include <cfg/module.h>
00045 #include <cfg/macros.h>
00046 #include <cfg/debug.h>
00047
00048 #include <drv/thermo.h>
00049 #include <drv/timer.h>
00050 #include <drv/ntc.h>
00051
00052
00053
00055 #define THERMO_INTERVAL_MS 100
00056
00058 #define THERMO_HIFI_NUM_SAMPLES 10
00059
00061 static Timer thermo_timer;
00062
00063 typedef struct ThermoControlDev
00064 {
00065 deg_t hifi_samples[THERMO_HIFI_NUM_SAMPLES];
00066 deg_t cur_hifi_sample;
00067 deg_t target;
00068 thermostatus_t status;
00069 ticks_t expire;
00070 } ThermoControlDev;
00071
00073 ThermoControlDev devs[THERMO_CNT];
00074
00075
00079 thermostatus_t thermo_status(ThermoDev dev)
00080 {
00081 return devs[dev].status;
00082 }
00083
00084
00088 static void thermo_do(ThermoDev index)
00089 {
00090 ThermoControlDev* dev = &devs[index];
00091 deg_t cur_temp;
00092 deg_t tolerance = thermo_hw_tolerance(index);
00093
00094 cur_temp = thermo_hw_read(index);
00095
00096
00097 dev->hifi_samples[dev->cur_hifi_sample] = cur_temp;
00098 if (++dev->cur_hifi_sample == THERMO_HIFI_NUM_SAMPLES)
00099 dev->cur_hifi_sample = 0;
00100
00101 cur_temp = thermo_readTemperature(index);
00102
00103 if (cur_temp == NTC_SHORT_CIRCUIT || cur_temp == NTC_OPEN_CIRCUIT)
00104 {
00105 if (cur_temp == NTC_SHORT_CIRCUIT)
00106 {
00107 #ifdef _DEBUG
00108 if (!(dev->status & THERMOERRF_NTCSHORT))
00109 kprintf("dev[%d], thermo_do: NTC_SHORT\n",index);
00110 #endif
00111 dev->status |= THERMOERRF_NTCSHORT;
00112 }
00113 else
00114 {
00115 #ifdef _DEBUG
00116 if (!(dev->status & THERMOERRF_NTCOPEN))
00117 kprintf("dev[%d], thermo_do: NTC_OPEN\n", index);
00118 #endif
00119 dev->status |= THERMOERRF_NTCOPEN;
00120 }
00121
00122
00123 dev->expire = thermo_hw_timeout(index) + timer_clock();
00124 thermo_hw_off(index);
00125 return;
00126 }
00127 dev->status &= ~(THERMOERRF_NTCOPEN | THERMOERRF_NTCSHORT);
00128
00129 if ((cur_temp < dev->target - tolerance) || (cur_temp > dev->target + tolerance))
00130 {
00131 dev->status &= ~THERMO_TGT_REACH;
00132
00133
00134 if (timer_clock() - dev->expire > 0)
00135 {
00136 dev->status |= THERMOERRF_TIMEOUT;
00137 kprintf("dev[%d], thermo_do: TIMEOUT\n", index);
00138 }
00139 }
00140 else
00141 {
00142
00143 dev->status &= ~THERMO_ERRMASK;
00144 dev->status |= THERMO_TGT_REACH;
00145
00146
00147 dev->expire = thermo_hw_timeout(index) + timer_clock();
00148 }
00149
00150 if (cur_temp < dev->target)
00151 dev->status = (dev->status | THERMO_HEATING) & ~THERMO_FREEZING;
00152 else
00153 dev->status = (dev->status & ~THERMO_HEATING) | THERMO_FREEZING;
00154
00155 thermo_hw_set(index, dev->target, cur_temp);
00156
00157 }
00158
00159
00163 static void thermo_softint(void)
00164 {
00165 int i;
00166 for (i = 0; i < THERMO_CNT; ++i)
00167 if (devs[i].status & THERMO_ACTIVE)
00168 thermo_do((ThermoDev)i);
00169
00170 timer_add(&thermo_timer);
00171 }
00172
00173
00177 void thermo_setTarget(ThermoDev dev, deg_t temperature)
00178 {
00179 ASSERT(dev < THERMO_CNT);
00180 devs[dev].target = temperature;
00181 devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
00182
00183 kprintf("setTarget dev[%d], T[%d.%d]\n", dev, temperature / 10, temperature % 10);
00184 }
00185
00189 void thermo_start(ThermoDev dev)
00190 {
00191 int i;
00192 deg_t temp;
00193
00194 ASSERT(dev < THERMO_CNT);
00195
00196 devs[dev].status |= THERMO_ACTIVE;
00197
00198
00199 temp = thermo_hw_read(dev);
00200 for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; ++i)
00201 devs[dev].hifi_samples[i] = temp;
00202 devs[dev].cur_hifi_sample = 0;
00203
00204
00205 devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
00206 }
00207
00211 void thermo_stop(ThermoDev dev)
00212 {
00213 ASSERT(dev < THERMO_CNT);
00214
00215 devs[dev].status &= ~THERMO_ACTIVE;
00216 thermo_hw_off(dev);
00217 }
00218
00219
00223 void thermo_clearErrors(ThermoDev dev)
00224 {
00225 ASSERT(dev < THERMO_CNT);
00226 devs[dev].status &= ~(THERMO_ERRMASK);
00227 }
00228
00229
00233 deg_t thermo_readTemperature(ThermoDev dev)
00234 {
00235 int i;
00236 long accum = 0;
00237
00238 MOD_CHECK(thermo);
00239
00240 for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; i++)
00241 accum += devs[dev].hifi_samples[i];
00242
00243 return (deg_t)(accum / THERMO_HIFI_NUM_SAMPLES);
00244 }
00245
00246 MOD_DEFINE(thermo)
00247
00248
00251 void thermo_init(void)
00252 {
00253 THERMO_HW_INIT;
00254
00255
00256 for (int i = 0; i < THERMO_CNT; i++)
00257 devs[i].status = THERMO_OFF;
00258
00259 MOD_INIT(thermo);
00260
00261 timer_setDelay(&thermo_timer, ms_to_ticks(THERMO_INTERVAL_MS));
00262 timer_setSoftint(&thermo_timer, (Hook)thermo_softint, 0);
00263 timer_add(&thermo_timer);
00264 }