73a5b451a6fb9fc4bc0e9631e51633debc3177da
[bertos.git] / drv / thermo.c
1 /**
2  * \file
3  * <!--
4  * Copyright 2005 Develer S.r.l. (http://www.develer.com/)
5  * This file is part of DevLib - See README.devlib for information.
6  * -->
7  *
8  * \brief Thermo-control driver
9  *
10  * \version $Id$
11  *
12  * \author Giovanni Bajo <rasky@develer.com>
13  * \author Francesco Sacchi <batt@develer.com>
14  */
15
16 /*#*
17  *#* $Log$
18  *#* Revision 1.3  2006/09/20 20:12:41  marco
19  *#* Names convention, MOD_* macros.
20  *#*
21  *#* Revision 1.2  2006/07/19 12:56:26  bernie
22  *#* Convert to new Doxygen style.
23  *#*
24  *#* Revision 1.1  2005/11/04 17:59:47  bernie
25  *#* Import into DevLib.
26  *#*
27  *#*/
28 #include <thermo_map.h>
29 #include <hw_thermo.h>
30
31 #include <drv/thermo.h>
32 #include <drv/timer.h>
33 #include <drv/ntc.h>
34
35 #include <cfg/macros.h>
36 #include <cfg/debug.h>
37
38
39 /** Interval at which thermo control is performed. */
40 #define THERMO_INTERVAL_MS      100
41
42 /** Number of different samples we interpolate over to get the hifi temperature. */
43 #define THERMO_HIFI_NUM_SAMPLES 10
44
45 /** Timer for thermo-regulation. */
46 static Timer thermo_timer;
47
48 typedef struct ThermoControlDev
49 {
50         deg_t          hifi_samples[THERMO_HIFI_NUM_SAMPLES];
51         deg_t          cur_hifi_sample;
52         deg_t          target;
53         thermostatus_t status;
54         ticks_t        expire;
55 } ThermoControlDev;
56
57 /** Array of thermo-devices. */
58 ThermoControlDev devs[THERMO_CNT];
59
60
61 /**
62  * Return the status of the specific \a dev thermo-device.
63  */
64 thermostatus_t thermo_status(ThermoDev dev)
65 {
66         return devs[dev].status;
67 }
68
69
70 /**
71  * Do a single thermo control for device \a dev.
72  */
73 static void thermo_do(ThermoDev index)
74 {
75         ThermoControlDev* dev = &devs[index];
76         deg_t cur_temp;
77         deg_t tolerance = thermo_hw_tolerance(index);
78
79         cur_temp = thermo_hw_read(index);
80
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;
85
86         cur_temp = thermo_readTemperature(index);
87
88         if (cur_temp == NTC_SHORT_CIRCUIT || cur_temp == NTC_OPEN_CIRCUIT)
89         {
90                 if (cur_temp == NTC_SHORT_CIRCUIT)
91                 {
92                         #ifdef _DEBUG
93                         if (!(dev->status & THERMOERRF_NTCSHORT))
94                                 kprintf("dev[%d], thermo_do: NTC_SHORT\n",index);
95                         #endif
96                         dev->status |= THERMOERRF_NTCSHORT;
97                 }
98                 else
99                 {
100                         #ifdef _DEBUG
101                         if (!(dev->status & THERMOERRF_NTCOPEN))
102                                 kprintf("dev[%d], thermo_do: NTC_OPEN\n", index);
103                         #endif
104                         dev->status |= THERMOERRF_NTCOPEN;
105                 }
106
107                 /* Reset timeout when there is an ntc error */
108                 dev->expire = thermo_hw_timeout(index) + timer_clock();
109                 thermo_hw_off(index);
110                 return;
111         }
112         dev->status &= ~(THERMOERRF_NTCOPEN | THERMOERRF_NTCSHORT);
113
114         if ((cur_temp < dev->target - tolerance) || (cur_temp > dev->target + tolerance))
115         {
116                 dev->status &= ~THERMO_TGT_REACH;
117
118                 /* Check for timeout */
119                 if (timer_clock() - dev->expire > 0)
120                 {
121                         dev->status |= THERMOERRF_TIMEOUT;
122                         kprintf("dev[%d], thermo_do: TIMEOUT\n", index);
123                 }
124         }
125         else /* In target */
126         {
127                 /* Clear errors */
128                 dev->status &= ~THERMO_ERRMASK;
129                 dev->status |= THERMO_TGT_REACH;
130
131                 /* Reset timeout in case we go out of target in the future */
132                 dev->expire = thermo_hw_timeout(index) + timer_clock();
133         }
134
135         if (cur_temp < dev->target)
136                 dev->status = (dev->status | THERMO_HEATING) & ~THERMO_FREEZING;
137         else
138                 dev->status = (dev->status & ~THERMO_HEATING) | THERMO_FREEZING;
139
140         thermo_hw_set(index, dev->target, cur_temp);
141
142 }
143
144
145 /**
146  * Thermo soft interrupt.
147  */
148 static void thermo_softint(void)
149 {
150         int i;
151         for (i = 0; i < THERMO_CNT; ++i)
152                 if (devs[i].status & THERMO_ACTIVE)
153                         thermo_do((ThermoDev)i);
154
155         timer_add(&thermo_timer);
156 }
157
158
159 /**
160  * Set the target temperature \a temperature for a specific \a dev thermo-device.
161  */
162 void thermo_setTarget(ThermoDev dev, deg_t temperature)
163 {
164         ASSERT(dev < THERMO_CNT);
165         devs[dev].target = temperature;
166         devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
167
168         kprintf("setTarget dev[%d], T[%d.%d]\n", dev, temperature / 10, temperature % 10);
169 }
170
171 /**
172  * Starts a thermo-regulation for channel \a dev.
173  */
174 void thermo_start(ThermoDev dev)
175 {
176         int i;
177         deg_t temp;
178
179         ASSERT(dev < THERMO_CNT);
180
181         devs[dev].status |= THERMO_ACTIVE;
182
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;
188
189         /* Reset timeout */
190         devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
191 }
192
193 /**
194  * Stops a thermo-regulation for channel \a dev.
195  */
196 void thermo_stop(ThermoDev dev)
197 {
198         ASSERT(dev < THERMO_CNT);
199
200         devs[dev].status &= ~THERMO_ACTIVE;
201         thermo_hw_off(dev);
202 }
203
204
205 /**
206  * Clear errors for channel \a dev.
207  */
208 void thermo_clearErrors(ThermoDev dev)
209 {
210         ASSERT(dev < THERMO_CNT);
211         devs[dev].status &= ~(THERMO_ERRMASK);
212 }
213
214
215 /**
216  * Read the temperature of the thermo-device \a dev using mobile mean.
217  */
218 deg_t thermo_readTemperature(ThermoDev dev)
219 {
220         int i;
221         long accum = 0;
222
223         MOD_CHECK(thermo);
224
225         for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; i++)
226                 accum += devs[dev].hifi_samples[i];
227
228         return (deg_t)(accum / THERMO_HIFI_NUM_SAMPLES);
229 }
230
231 MOD_DEFINE(thermo)
232
233 /**
234  * Init thermo-control and associated hw.
235  */
236 void thermo_init(void)
237 {
238         THERMO_HW_INIT;
239
240         /* Set all status to off */
241         for (int i = 0; i < THERMO_CNT; i++)
242                 devs[i].status = THERMO_OFF;
243
244         MOD_INIT(thermo);
245
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);
249 }