4 * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5 * This file is part of DevLib - See README.devlib for information.
8 * \brief Thermo-control driver
12 * \author Giovanni Bajo <rasky@develer.com>
13 * \author Francesco Sacchi <batt@develer.com>
18 *#* Revision 1.3 2006/09/20 20:12:41 marco
19 *#* Names convention, MOD_* macros.
21 *#* Revision 1.2 2006/07/19 12:56:26 bernie
22 *#* Convert to new Doxygen style.
24 *#* Revision 1.1 2005/11/04 17:59:47 bernie
25 *#* Import into DevLib.
28 #include <thermo_map.h>
29 #include <hw_thermo.h>
31 #include <drv/thermo.h>
32 #include <drv/timer.h>
35 #include <cfg/macros.h>
36 #include <cfg/debug.h>
39 /** Interval at which thermo control is performed. */
40 #define THERMO_INTERVAL_MS 100
42 /** Number of different samples we interpolate over to get the hifi temperature. */
43 #define THERMO_HIFI_NUM_SAMPLES 10
45 /** Timer for thermo-regulation. */
46 static Timer thermo_timer;
48 typedef struct ThermoControlDev
50 deg_t hifi_samples[THERMO_HIFI_NUM_SAMPLES];
51 deg_t cur_hifi_sample;
53 thermostatus_t status;
57 /** Array of thermo-devices. */
58 ThermoControlDev devs[THERMO_CNT];
62 * Return the status of the specific \a dev thermo-device.
64 thermostatus_t thermo_status(ThermoDev dev)
66 return devs[dev].status;
71 * Do a single thermo control for device \a dev.
73 static void thermo_do(ThermoDev index)
75 ThermoControlDev* dev = &devs[index];
77 deg_t tolerance = thermo_hw_tolerance(index);
79 cur_temp = thermo_hw_read(index);
81 // Store the sample into the hifi FIFO buffer for later interpolation
82 dev->hifi_samples[dev->cur_hifi_sample] = cur_temp;
83 if (++dev->cur_hifi_sample == THERMO_HIFI_NUM_SAMPLES)
84 dev->cur_hifi_sample = 0;
86 cur_temp = thermo_readTemperature(index);
88 if (cur_temp == NTC_SHORT_CIRCUIT || cur_temp == NTC_OPEN_CIRCUIT)
90 if (cur_temp == NTC_SHORT_CIRCUIT)
93 if (!(dev->status & THERMOERRF_NTCSHORT))
94 kprintf("dev[%d], thermo_do: NTC_SHORT\n",index);
96 dev->status |= THERMOERRF_NTCSHORT;
101 if (!(dev->status & THERMOERRF_NTCOPEN))
102 kprintf("dev[%d], thermo_do: NTC_OPEN\n", index);
104 dev->status |= THERMOERRF_NTCOPEN;
107 /* Reset timeout when there is an ntc error */
108 dev->expire = thermo_hw_timeout(index) + timer_clock();
109 thermo_hw_off(index);
112 dev->status &= ~(THERMOERRF_NTCOPEN | THERMOERRF_NTCSHORT);
114 if ((cur_temp < dev->target - tolerance) || (cur_temp > dev->target + tolerance))
116 dev->status &= ~THERMO_TGT_REACH;
118 /* Check for timeout */
119 if (timer_clock() - dev->expire > 0)
121 dev->status |= THERMOERRF_TIMEOUT;
122 kprintf("dev[%d], thermo_do: TIMEOUT\n", index);
128 dev->status &= ~THERMO_ERRMASK;
129 dev->status |= THERMO_TGT_REACH;
131 /* Reset timeout in case we go out of target in the future */
132 dev->expire = thermo_hw_timeout(index) + timer_clock();
135 if (cur_temp < dev->target)
136 dev->status = (dev->status | THERMO_HEATING) & ~THERMO_FREEZING;
138 dev->status = (dev->status & ~THERMO_HEATING) | THERMO_FREEZING;
140 thermo_hw_set(index, dev->target, cur_temp);
146 * Thermo soft interrupt.
148 static void thermo_softint(void)
151 for (i = 0; i < THERMO_CNT; ++i)
152 if (devs[i].status & THERMO_ACTIVE)
153 thermo_do((ThermoDev)i);
155 timer_add(&thermo_timer);
160 * Set the target temperature \a temperature for a specific \a dev thermo-device.
162 void thermo_setTarget(ThermoDev dev, deg_t temperature)
164 ASSERT(dev < THERMO_CNT);
165 devs[dev].target = temperature;
166 devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
168 kprintf("setTarget dev[%d], T[%d.%d]\n", dev, temperature / 10, temperature % 10);
172 * Starts a thermo-regulation for channel \a dev.
174 void thermo_start(ThermoDev dev)
179 ASSERT(dev < THERMO_CNT);
181 devs[dev].status |= THERMO_ACTIVE;
183 /* Initialize the hifi FIFO with a constant value (the current temperature) */
184 temp = thermo_hw_read(dev);
185 for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; ++i)
186 devs[dev].hifi_samples[i] = temp;
187 devs[dev].cur_hifi_sample = 0;
190 devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
194 * Stops a thermo-regulation for channel \a dev.
196 void thermo_stop(ThermoDev dev)
198 ASSERT(dev < THERMO_CNT);
200 devs[dev].status &= ~THERMO_ACTIVE;
206 * Clear errors for channel \a dev.
208 void thermo_clearErrors(ThermoDev dev)
210 ASSERT(dev < THERMO_CNT);
211 devs[dev].status &= ~(THERMO_ERRMASK);
216 * Read the temperature of the thermo-device \a dev using mobile mean.
218 deg_t thermo_readTemperature(ThermoDev dev)
225 for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; i++)
226 accum += devs[dev].hifi_samples[i];
228 return (deg_t)(accum / THERMO_HIFI_NUM_SAMPLES);
234 * Init thermo-control and associated hw.
236 void thermo_init(void)
240 /* Set all status to off */
241 for (int i = 0; i < THERMO_CNT; i++)
242 devs[i].status = THERMO_OFF;
246 timer_setDelay(&thermo_timer, ms_to_ticks(THERMO_INTERVAL_MS));
247 timer_set_event_softint(&thermo_timer, (Hook)thermo_softint, 0);
248 timer_add(&thermo_timer);