621f3baaa5f0118add62a7dd803ae3c64be7b008
[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 "hw/thermo_map.h"
42 #include "hw/hw_thermo.h"
43
44 #include "cfg/cfg_thermo.h"
45
46 #include <cfg/module.h>
47 #include <cfg/macros.h>
48 #include <cfg/debug.h>
49
50 #include <drv/thermo.h>
51 #include <drv/timer.h>
52 #include <drv/ntc.h>
53
54
55
56
57 /** Timer for thermo-regulation. */
58 static Timer thermo_timer;
59
60 typedef struct ThermoControlDev
61 {
62         deg_t          hifi_samples[THERMO_HIFI_NUM_SAMPLES];
63         deg_t          cur_hifi_sample;
64         deg_t          target;
65         thermostatus_t status;
66         ticks_t        expire;
67 } ThermoControlDev;
68
69 /** Array of thermo-devices. */
70 ThermoControlDev devs[THERMO_CNT];
71
72
73 /**
74  * Return the status of the specific \a dev thermo-device.
75  */
76 thermostatus_t thermo_status(ThermoDev dev)
77 {
78         return devs[dev].status;
79 }
80
81
82 /**
83  * Do a single thermo control for device \a dev.
84  */
85 static void thermo_do(ThermoDev index)
86 {
87         ThermoControlDev* dev = &devs[index];
88         deg_t cur_temp;
89         deg_t tolerance = thermo_hw_tolerance(index);
90
91         cur_temp = thermo_hw_read(index);
92
93         // Store the sample into the hifi FIFO buffer for later interpolation
94         dev->hifi_samples[dev->cur_hifi_sample] = cur_temp;
95         if (++dev->cur_hifi_sample == THERMO_HIFI_NUM_SAMPLES)
96                 dev->cur_hifi_sample = 0;
97
98         cur_temp = thermo_readTemperature(index);
99
100         if (cur_temp == NTC_SHORT_CIRCUIT || cur_temp == NTC_OPEN_CIRCUIT)
101         {
102                 if (cur_temp == NTC_SHORT_CIRCUIT)
103                 {
104                         #ifdef _DEBUG
105                         if (!(dev->status & THERMOERRF_NTCSHORT))
106                                 kprintf("dev[%d], thermo_do: NTC_SHORT\n",index);
107                         #endif
108                         dev->status |= THERMOERRF_NTCSHORT;
109                 }
110                 else
111                 {
112                         #ifdef _DEBUG
113                         if (!(dev->status & THERMOERRF_NTCOPEN))
114                                 kprintf("dev[%d], thermo_do: NTC_OPEN\n", index);
115                         #endif
116                         dev->status |= THERMOERRF_NTCOPEN;
117                 }
118
119                 /* Reset timeout when there is an ntc error */
120                 dev->expire = thermo_hw_timeout(index) + timer_clock();
121                 thermo_hw_off(index);
122                 return;
123         }
124         dev->status &= ~(THERMOERRF_NTCOPEN | THERMOERRF_NTCSHORT);
125
126         if ((cur_temp < dev->target - tolerance) || (cur_temp > dev->target + tolerance))
127         {
128                 dev->status &= ~THERMO_TGT_REACH;
129
130                 /* Check for timeout */
131                 if (timer_clock() - dev->expire > 0)
132                 {
133                         dev->status |= THERMOERRF_TIMEOUT;
134                         kprintf("dev[%d], thermo_do: TIMEOUT\n", index);
135                 }
136         }
137         else /* In target */
138         {
139                 /* Clear errors */
140                 dev->status &= ~THERMO_ERRMASK;
141                 dev->status |= THERMO_TGT_REACH;
142
143                 /* Reset timeout in case we go out of target in the future */
144                 dev->expire = thermo_hw_timeout(index) + timer_clock();
145         }
146
147         if (cur_temp < dev->target)
148                 dev->status = (dev->status | THERMO_HEATING) & ~THERMO_FREEZING;
149         else
150                 dev->status = (dev->status & ~THERMO_HEATING) | THERMO_FREEZING;
151
152         thermo_hw_set(index, dev->target, cur_temp);
153
154 }
155
156
157 /**
158  * Thermo soft interrupt.
159  */
160 static void thermo_softint(void)
161 {
162         int i;
163         for (i = 0; i < THERMO_CNT; ++i)
164                 if (devs[i].status & THERMO_ACTIVE)
165                         thermo_do((ThermoDev)i);
166
167         timer_add(&thermo_timer);
168 }
169
170
171 /**
172  * Set the target temperature \a temperature for a specific \a dev thermo-device.
173  */
174 void thermo_setTarget(ThermoDev dev, deg_t temperature)
175 {
176         ASSERT(dev < THERMO_CNT);
177         devs[dev].target = temperature;
178         devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
179
180         kprintf("setTarget dev[%d], T[%d.%d]\n", dev, temperature / 10, temperature % 10);
181 }
182
183 /**
184  * Starts a thermo-regulation for channel \a dev.
185  */
186 void thermo_start(ThermoDev dev)
187 {
188         int i;
189         deg_t temp;
190
191         ASSERT(dev < THERMO_CNT);
192
193         devs[dev].status |= THERMO_ACTIVE;
194
195         /* Initialize the hifi FIFO with a constant value (the current temperature) */
196         temp = thermo_hw_read(dev);
197         for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; ++i)
198                 devs[dev].hifi_samples[i] = temp;
199         devs[dev].cur_hifi_sample = 0;
200
201         /* Reset timeout */
202         devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
203 }
204
205 /**
206  * Stops a thermo-regulation for channel \a dev.
207  */
208 void thermo_stop(ThermoDev dev)
209 {
210         ASSERT(dev < THERMO_CNT);
211
212         devs[dev].status &= ~THERMO_ACTIVE;
213         thermo_hw_off(dev);
214 }
215
216
217 /**
218  * Clear errors for channel \a dev.
219  */
220 void thermo_clearErrors(ThermoDev dev)
221 {
222         ASSERT(dev < THERMO_CNT);
223         devs[dev].status &= ~(THERMO_ERRMASK);
224 }
225
226
227 /**
228  * Read the temperature of the thermo-device \a dev using mobile mean.
229  */
230 deg_t thermo_readTemperature(ThermoDev dev)
231 {
232         int i;
233         long accum = 0;
234
235         MOD_CHECK(thermo);
236
237         for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; i++)
238                 accum += devs[dev].hifi_samples[i];
239
240         return (deg_t)(accum / THERMO_HIFI_NUM_SAMPLES);
241 }
242
243 MOD_DEFINE(thermo)
244
245 /**
246  * Init thermo-control and associated hw.
247  */
248 void thermo_init(void)
249 {
250         THERMO_HW_INIT;
251
252         /* Set all status to off */
253         for (int i = 0; i < THERMO_CNT; i++)
254                 devs[i].status = THERMO_OFF;
255
256         MOD_INIT(thermo);
257
258         timer_setDelay(&thermo_timer, ms_to_ticks(THERMO_INTERVAL_MS));
259         timer_setSoftint(&thermo_timer, (Hook)thermo_softint, 0);
260         timer_add(&thermo_timer);
261 }