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.2 2006/07/19 12:56:26 bernie
19 *#* Convert to new Doxygen style.
21 *#* Revision 1.1 2005/11/04 17:59:47 bernie
22 *#* Import into DevLib.
25 #include <thermo_map.h>
26 #include <hw_thermo.h>
28 #include <drv/thermo.h>
29 #include <drv/timer.h>
32 #include <cfg/macros.h>
33 #include <cfg/debug.h>
36 /** Interval at which thermo control is performed. */
37 #define THERMO_INTERVAL_MS 100
39 /** Number of different samples we interpolate over to get the hifi temperature. */
40 #define THERMO_HIFI_NUM_SAMPLES 10
42 /** Timer for thermo-regulation. */
43 static Timer thermo_timer;
45 typedef struct ThermoControlDev
47 deg_t hifi_samples[THERMO_HIFI_NUM_SAMPLES];
48 deg_t cur_hifi_sample;
50 thermostatus_t status;
54 /** Array of thermo-devices. */
55 ThermoControlDev devs[THERMO_CNT];
59 * Return the status of the specific \a dev thermo-device.
61 thermostatus_t thermo_status(ThermoDev dev)
63 return devs[dev].status;
68 * Do a single thermo control for device \a dev.
70 static void thermo_do(ThermoDev index)
72 ThermoControlDev* dev = &devs[index];
74 deg_t tolerance = thermo_hw_tolerance(index);
76 cur_temp = thermo_hw_read(index);
78 // Store the sample into the hifi FIFO buffer for later interpolation
79 dev->hifi_samples[dev->cur_hifi_sample] = cur_temp;
80 if (++dev->cur_hifi_sample == THERMO_HIFI_NUM_SAMPLES)
81 dev->cur_hifi_sample = 0;
83 cur_temp = thermo_read_temperature(index);
85 if (cur_temp == NTC_SHORT_CIRCUIT || cur_temp == NTC_OPEN_CIRCUIT)
87 if (cur_temp == NTC_SHORT_CIRCUIT)
90 if (!(dev->status & THERMOERRF_NTCSHORT))
91 kprintf("dev[%d], thermo_do: NTC_SHORT\n",index);
93 dev->status |= THERMOERRF_NTCSHORT;
98 if (!(dev->status & THERMOERRF_NTCOPEN))
99 kprintf("dev[%d], thermo_do: NTC_OPEN\n", index);
101 dev->status |= THERMOERRF_NTCOPEN;
104 /* Reset timeout when there is an ntc error */
105 dev->expire = thermo_hw_timeout(index) + timer_clock();
106 thermo_hw_off(index);
109 dev->status &= ~(THERMOERRF_NTCOPEN | THERMOERRF_NTCSHORT);
111 if ((cur_temp < dev->target - tolerance) || (cur_temp > dev->target + tolerance))
113 dev->status &= ~THERMO_TGT_REACH;
115 /* Check for timeout */
116 if (timer_clock() - dev->expire > 0)
118 dev->status |= THERMOERRF_TIMEOUT;
119 kprintf("dev[%d], thermo_do: TIMEOUT\n", index);
125 dev->status &= ~THERMO_ERRMASK;
126 dev->status |= THERMO_TGT_REACH;
128 /* Reset timeout in case we go out of target in the future */
129 dev->expire = thermo_hw_timeout(index) + timer_clock();
132 if (cur_temp < dev->target)
133 dev->status = (dev->status | THERMO_HEATING) & ~THERMO_FREEZING;
135 dev->status = (dev->status & ~THERMO_HEATING) | THERMO_FREEZING;
137 thermo_hw_set(index, dev->target, cur_temp);
143 * Thermo soft interrupt.
145 static void thermo_softint(void)
148 for (i = 0; i < THERMO_CNT; ++i)
149 if (devs[i].status & THERMO_ACTIVE)
150 thermo_do((ThermoDev)i);
152 timer_add(&thermo_timer);
157 * Set the target temperature \a temperature for a specific \a dev thermo-device.
159 void thermo_setTarget(ThermoDev dev, deg_t temperature)
161 ASSERT(dev < THERMO_CNT);
162 devs[dev].target = temperature;
163 devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
165 kprintf("setTarget dev[%d], T[%d.%d]\n", dev, temperature / 10, temperature % 10);
169 * Starts a thermo-regulation for channel \a dev.
171 void thermo_start(ThermoDev dev)
176 ASSERT(dev < THERMO_CNT);
178 devs[dev].status |= THERMO_ACTIVE;
180 /* Initialize the hifi FIFO with a constant value (the current temperature) */
181 temp = thermo_hw_read(dev);
182 for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; ++i)
183 devs[dev].hifi_samples[i] = temp;
184 devs[dev].cur_hifi_sample = 0;
187 devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
191 * Stops a thermo-regulation for channel \a dev.
193 void thermo_stop(ThermoDev dev)
195 ASSERT(dev < THERMO_CNT);
197 devs[dev].status &= ~THERMO_ACTIVE;
203 * Clear errors for channel \a dev.
205 void thermo_clearErrors(ThermoDev dev)
207 ASSERT(dev < THERMO_CNT);
208 devs[dev].status &= ~(THERMO_ERRMASK);
213 * Read the temperature of the thermo-device \a dev using mobile mean.
215 deg_t thermo_read_temperature(ThermoDev dev)
220 for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; i++)
221 accum += devs[dev].hifi_samples[i];
223 return (deg_t)(accum / THERMO_HIFI_NUM_SAMPLES);
228 * Init thermo-control and associated hw.
230 void thermo_init(void)
234 /* Set all status to off */
235 for (int i = 0; i < THERMO_CNT; i++)
236 devs[i].status = THERMO_OFF;
238 timer_setDelay(&thermo_timer, ms_to_ticks(THERMO_INTERVAL_MS));
239 timer_set_event_softint(&thermo_timer, (Hook)thermo_softint, 0);
240 timer_add(&thermo_timer);