Use configuration file instead appconfig. Reformat. Clean up. Remove CVS logs.
[bertos.git] / bertos / drv / thermo.c
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief Thermo-control driver
34  *
35  * \version $Id$
36  *
37  * \author Giovanni Bajo <rasky@develer.com>
38  * \author Francesco Sacchi <batt@develer.com>
39  */
40
41 #include "thermo_map.h"
42 #include "hw_thermo.h"
43
44 #include <cfg/module.h>
45 #include <cfg/macros.h>
46 #include <cfg/debug.h>
47
48 #include <drv/thermo.h>
49 #include <drv/timer.h>
50 #include <drv/ntc.h>
51
52
53
54 /** Interval at which thermo control is performed. */
55 #define THERMO_INTERVAL_MS      100
56
57 /** Number of different samples we interpolate over to get the hifi temperature. */
58 #define THERMO_HIFI_NUM_SAMPLES 10
59
60 /** Timer for thermo-regulation. */
61 static Timer thermo_timer;
62
63 typedef struct ThermoControlDev
64 {
65         deg_t          hifi_samples[THERMO_HIFI_NUM_SAMPLES];
66         deg_t          cur_hifi_sample;
67         deg_t          target;
68         thermostatus_t status;
69         ticks_t        expire;
70 } ThermoControlDev;
71
72 /** Array of thermo-devices. */
73 ThermoControlDev devs[THERMO_CNT];
74
75
76 /**
77  * Return the status of the specific \a dev thermo-device.
78  */
79 thermostatus_t thermo_status(ThermoDev dev)
80 {
81         return devs[dev].status;
82 }
83
84
85 /**
86  * Do a single thermo control for device \a dev.
87  */
88 static void thermo_do(ThermoDev index)
89 {
90         ThermoControlDev* dev = &devs[index];
91         deg_t cur_temp;
92         deg_t tolerance = thermo_hw_tolerance(index);
93
94         cur_temp = thermo_hw_read(index);
95
96         // Store the sample into the hifi FIFO buffer for later interpolation
97         dev->hifi_samples[dev->cur_hifi_sample] = cur_temp;
98         if (++dev->cur_hifi_sample == THERMO_HIFI_NUM_SAMPLES)
99                 dev->cur_hifi_sample = 0;
100
101         cur_temp = thermo_readTemperature(index);
102
103         if (cur_temp == NTC_SHORT_CIRCUIT || cur_temp == NTC_OPEN_CIRCUIT)
104         {
105                 if (cur_temp == NTC_SHORT_CIRCUIT)
106                 {
107                         #ifdef _DEBUG
108                         if (!(dev->status & THERMOERRF_NTCSHORT))
109                                 kprintf("dev[%d], thermo_do: NTC_SHORT\n",index);
110                         #endif
111                         dev->status |= THERMOERRF_NTCSHORT;
112                 }
113                 else
114                 {
115                         #ifdef _DEBUG
116                         if (!(dev->status & THERMOERRF_NTCOPEN))
117                                 kprintf("dev[%d], thermo_do: NTC_OPEN\n", index);
118                         #endif
119                         dev->status |= THERMOERRF_NTCOPEN;
120                 }
121
122                 /* Reset timeout when there is an ntc error */
123                 dev->expire = thermo_hw_timeout(index) + timer_clock();
124                 thermo_hw_off(index);
125                 return;
126         }
127         dev->status &= ~(THERMOERRF_NTCOPEN | THERMOERRF_NTCSHORT);
128
129         if ((cur_temp < dev->target - tolerance) || (cur_temp > dev->target + tolerance))
130         {
131                 dev->status &= ~THERMO_TGT_REACH;
132
133                 /* Check for timeout */
134                 if (timer_clock() - dev->expire > 0)
135                 {
136                         dev->status |= THERMOERRF_TIMEOUT;
137                         kprintf("dev[%d], thermo_do: TIMEOUT\n", index);
138                 }
139         }
140         else /* In target */
141         {
142                 /* Clear errors */
143                 dev->status &= ~THERMO_ERRMASK;
144                 dev->status |= THERMO_TGT_REACH;
145
146                 /* Reset timeout in case we go out of target in the future */
147                 dev->expire = thermo_hw_timeout(index) + timer_clock();
148         }
149
150         if (cur_temp < dev->target)
151                 dev->status = (dev->status | THERMO_HEATING) & ~THERMO_FREEZING;
152         else
153                 dev->status = (dev->status & ~THERMO_HEATING) | THERMO_FREEZING;
154
155         thermo_hw_set(index, dev->target, cur_temp);
156
157 }
158
159
160 /**
161  * Thermo soft interrupt.
162  */
163 static void thermo_softint(void)
164 {
165         int i;
166         for (i = 0; i < THERMO_CNT; ++i)
167                 if (devs[i].status & THERMO_ACTIVE)
168                         thermo_do((ThermoDev)i);
169
170         timer_add(&thermo_timer);
171 }
172
173
174 /**
175  * Set the target temperature \a temperature for a specific \a dev thermo-device.
176  */
177 void thermo_setTarget(ThermoDev dev, deg_t temperature)
178 {
179         ASSERT(dev < THERMO_CNT);
180         devs[dev].target = temperature;
181         devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
182
183         kprintf("setTarget dev[%d], T[%d.%d]\n", dev, temperature / 10, temperature % 10);
184 }
185
186 /**
187  * Starts a thermo-regulation for channel \a dev.
188  */
189 void thermo_start(ThermoDev dev)
190 {
191         int i;
192         deg_t temp;
193
194         ASSERT(dev < THERMO_CNT);
195
196         devs[dev].status |= THERMO_ACTIVE;
197
198         /* Initialize the hifi FIFO with a constant value (the current temperature) */
199         temp = thermo_hw_read(dev);
200         for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; ++i)
201                 devs[dev].hifi_samples[i] = temp;
202         devs[dev].cur_hifi_sample = 0;
203
204         /* Reset timeout */
205         devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
206 }
207
208 /**
209  * Stops a thermo-regulation for channel \a dev.
210  */
211 void thermo_stop(ThermoDev dev)
212 {
213         ASSERT(dev < THERMO_CNT);
214
215         devs[dev].status &= ~THERMO_ACTIVE;
216         thermo_hw_off(dev);
217 }
218
219
220 /**
221  * Clear errors for channel \a dev.
222  */
223 void thermo_clearErrors(ThermoDev dev)
224 {
225         ASSERT(dev < THERMO_CNT);
226         devs[dev].status &= ~(THERMO_ERRMASK);
227 }
228
229
230 /**
231  * Read the temperature of the thermo-device \a dev using mobile mean.
232  */
233 deg_t thermo_readTemperature(ThermoDev dev)
234 {
235         int i;
236         long accum = 0;
237
238         MOD_CHECK(thermo);
239
240         for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; i++)
241                 accum += devs[dev].hifi_samples[i];
242
243         return (deg_t)(accum / THERMO_HIFI_NUM_SAMPLES);
244 }
245
246 MOD_DEFINE(thermo)
247
248 /**
249  * Init thermo-control and associated hw.
250  */
251 void thermo_init(void)
252 {
253         THERMO_HW_INIT;
254
255         /* Set all status to off */
256         for (int i = 0; i < THERMO_CNT; i++)
257                 devs[i].status = THERMO_OFF;
258
259         MOD_INIT(thermo);
260
261         timer_setDelay(&thermo_timer, ms_to_ticks(THERMO_INTERVAL_MS));
262         timer_set_event_softint(&thermo_timer, (Hook)thermo_softint, 0);
263         timer_add(&thermo_timer);
264 }