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.1 2005/11/04 17:59:47 bernie
19 *#* Import into DevLib.
22 #include <thermo_map.h>
23 #include <hw_thermo.h>
25 #include <drv/thermo.h>
26 #include <drv/timer.h>
29 #include <cfg/macros.h>
30 #include <cfg/debug.h>
33 /*! Interval at which thermo control is performed. */
34 #define THERMO_INTERVAL_MS 100
36 /*! Number of different samples we interpolate over to get the hifi temperature. */
37 #define THERMO_HIFI_NUM_SAMPLES 10
39 /*! Timer for thermo-regulation. */
40 static Timer thermo_timer;
42 typedef struct ThermoControlDev
44 deg_t hifi_samples[THERMO_HIFI_NUM_SAMPLES];
45 deg_t cur_hifi_sample;
47 thermostatus_t status;
51 /*! Array of thermo-devices. */
52 ThermoControlDev devs[THERMO_CNT];
56 * Return the status of the specific \a dev thermo-device.
58 thermostatus_t thermo_status(ThermoDev dev)
60 return devs[dev].status;
65 * Do a single thermo control for device \a dev.
67 static void thermo_do(ThermoDev index)
69 ThermoControlDev* dev = &devs[index];
71 deg_t tolerance = thermo_hw_tolerance(index);
73 cur_temp = thermo_hw_read(index);
75 // Store the sample into the hifi FIFO buffer for later interpolation
76 dev->hifi_samples[dev->cur_hifi_sample] = cur_temp;
77 if (++dev->cur_hifi_sample == THERMO_HIFI_NUM_SAMPLES)
78 dev->cur_hifi_sample = 0;
80 cur_temp = thermo_read_temperature(index);
82 if (cur_temp == NTC_SHORT_CIRCUIT || cur_temp == NTC_OPEN_CIRCUIT)
84 if (cur_temp == NTC_SHORT_CIRCUIT)
87 if (!(dev->status & THERMOERRF_NTCSHORT))
88 kprintf("dev[%d], thermo_do: NTC_SHORT\n",index);
90 dev->status |= THERMOERRF_NTCSHORT;
95 if (!(dev->status & THERMOERRF_NTCOPEN))
96 kprintf("dev[%d], thermo_do: NTC_OPEN\n", index);
98 dev->status |= THERMOERRF_NTCOPEN;
101 /* Reset timeout when there is an ntc error */
102 dev->expire = thermo_hw_timeout(index) + timer_clock();
103 thermo_hw_off(index);
106 dev->status &= ~(THERMOERRF_NTCOPEN | THERMOERRF_NTCSHORT);
108 if ((cur_temp < dev->target - tolerance) || (cur_temp > dev->target + tolerance))
110 dev->status &= ~THERMO_TGT_REACH;
112 /* Check for timeout */
113 if (timer_clock() - dev->expire > 0)
115 dev->status |= THERMOERRF_TIMEOUT;
116 kprintf("dev[%d], thermo_do: TIMEOUT\n", index);
122 dev->status &= ~THERMO_ERRMASK;
123 dev->status |= THERMO_TGT_REACH;
125 /* Reset timeout in case we go out of target in the future */
126 dev->expire = thermo_hw_timeout(index) + timer_clock();
129 if (cur_temp < dev->target)
130 dev->status = (dev->status | THERMO_HEATING) & ~THERMO_FREEZING;
132 dev->status = (dev->status & ~THERMO_HEATING) | THERMO_FREEZING;
134 thermo_hw_set(index, dev->target, cur_temp);
140 * Thermo soft interrupt.
142 static void thermo_softint(void)
145 for (i = 0; i < THERMO_CNT; ++i)
146 if (devs[i].status & THERMO_ACTIVE)
147 thermo_do((ThermoDev)i);
149 timer_add(&thermo_timer);
154 * Set the target temperature \a temperature for a specific \a dev thermo-device.
156 void thermo_setTarget(ThermoDev dev, deg_t temperature)
158 ASSERT(dev < THERMO_CNT);
159 devs[dev].target = temperature;
160 devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
162 kprintf("setTarget dev[%d], T[%d.%d]\n", dev, temperature / 10, temperature % 10);
166 * Starts a thermo-regulation for channel \a dev.
168 void thermo_start(ThermoDev dev)
173 ASSERT(dev < THERMO_CNT);
175 devs[dev].status |= THERMO_ACTIVE;
177 /* Initialize the hifi FIFO with a constant value (the current temperature) */
178 temp = thermo_hw_read(dev);
179 for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; ++i)
180 devs[dev].hifi_samples[i] = temp;
181 devs[dev].cur_hifi_sample = 0;
184 devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
188 * Stops a thermo-regulation for channel \a dev.
190 void thermo_stop(ThermoDev dev)
192 ASSERT(dev < THERMO_CNT);
194 devs[dev].status &= ~THERMO_ACTIVE;
200 * Clear errors for channel \a dev.
202 void thermo_clearErrors(ThermoDev dev)
204 ASSERT(dev < THERMO_CNT);
205 devs[dev].status &= ~(THERMO_ERRMASK);
210 * Read the temperature of the thermo-device \a dev using mobile mean.
212 deg_t thermo_read_temperature(ThermoDev dev)
217 for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; i++)
218 accum += devs[dev].hifi_samples[i];
220 return (deg_t)(accum / THERMO_HIFI_NUM_SAMPLES);
225 * Init thermo-control and associated hw.
227 void thermo_init(void)
231 /* Set all status to off */
232 for (int i = 0; i < THERMO_CNT; i++)
233 devs[i].status = THERMO_OFF;
235 timer_setDelay(&thermo_timer, ms_to_ticks(THERMO_INTERVAL_MS));
236 timer_set_event_softint(&thermo_timer, (Hook)thermo_softint, 0);
237 timer_add(&thermo_timer);