Port to Qt 4.1.
[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.1  2005/11/04 17:59:47  bernie
19  *#* Import into DevLib.
20  *#*
21  *#*/
22 #include <thermo_map.h>
23 #include <hw_thermo.h>
24
25 #include <drv/thermo.h>
26 #include <drv/timer.h>
27 #include <drv/ntc.h>
28
29 #include <cfg/macros.h>
30 #include <cfg/debug.h>
31
32
33 /*! Interval at which thermo control is performed. */
34 #define THERMO_INTERVAL_MS      100
35
36 /*! Number of different samples we interpolate over to get the hifi temperature. */
37 #define THERMO_HIFI_NUM_SAMPLES 10
38
39 /*! Timer for thermo-regulation. */
40 static Timer thermo_timer;
41
42 typedef struct ThermoControlDev
43 {
44         deg_t          hifi_samples[THERMO_HIFI_NUM_SAMPLES];
45         deg_t          cur_hifi_sample;
46         deg_t          target;
47         thermostatus_t status;
48         ticks_t        expire;
49 } ThermoControlDev;
50
51 /*! Array of thermo-devices. */
52 ThermoControlDev devs[THERMO_CNT];
53
54
55 /*!
56  * Return the status of the specific \a dev thermo-device.
57  */
58 thermostatus_t thermo_status(ThermoDev dev)
59 {
60         return devs[dev].status;
61 }
62
63
64 /*!
65  * Do a single thermo control for device \a dev.
66  */
67 static void thermo_do(ThermoDev index)
68 {
69         ThermoControlDev* dev = &devs[index];
70         deg_t cur_temp;
71         deg_t tolerance = thermo_hw_tolerance(index);
72
73         cur_temp = thermo_hw_read(index);
74
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;
79
80         cur_temp = thermo_read_temperature(index);
81
82         if (cur_temp == NTC_SHORT_CIRCUIT || cur_temp == NTC_OPEN_CIRCUIT)
83         {
84                 if (cur_temp == NTC_SHORT_CIRCUIT)
85                 {
86                         #ifdef _DEBUG
87                         if (!(dev->status & THERMOERRF_NTCSHORT))
88                                 kprintf("dev[%d], thermo_do: NTC_SHORT\n",index);
89                         #endif
90                         dev->status |= THERMOERRF_NTCSHORT;
91                 }
92                 else
93                 {
94                         #ifdef _DEBUG
95                         if (!(dev->status & THERMOERRF_NTCOPEN))
96                                 kprintf("dev[%d], thermo_do: NTC_OPEN\n", index);
97                         #endif
98                         dev->status |= THERMOERRF_NTCOPEN;
99                 }
100
101                 /* Reset timeout when there is an ntc error */
102                 dev->expire = thermo_hw_timeout(index) + timer_clock();
103                 thermo_hw_off(index);
104                 return;
105         }
106         dev->status &= ~(THERMOERRF_NTCOPEN | THERMOERRF_NTCSHORT);
107
108         if ((cur_temp < dev->target - tolerance) || (cur_temp > dev->target + tolerance))
109         {
110                 dev->status &= ~THERMO_TGT_REACH;
111
112                 /* Check for timeout */
113                 if (timer_clock() - dev->expire > 0)
114                 {
115                         dev->status |= THERMOERRF_TIMEOUT;
116                         kprintf("dev[%d], thermo_do: TIMEOUT\n", index);
117                 }
118         }
119         else /* In target */
120         {
121                 /* Clear errors */
122                 dev->status &= ~THERMO_ERRMASK;
123                 dev->status |= THERMO_TGT_REACH;
124
125                 /* Reset timeout in case we go out of target in the future */
126                 dev->expire = thermo_hw_timeout(index) + timer_clock();
127         }
128
129         if (cur_temp < dev->target)
130                 dev->status = (dev->status | THERMO_HEATING) & ~THERMO_FREEZING;
131         else
132                 dev->status = (dev->status & ~THERMO_HEATING) | THERMO_FREEZING;
133
134         thermo_hw_set(index, dev->target, cur_temp);
135
136 }
137
138
139 /*!
140  * Thermo soft interrupt.
141  */
142 static void thermo_softint(void)
143 {
144         int i;
145         for (i = 0; i < THERMO_CNT; ++i)
146                 if (devs[i].status & THERMO_ACTIVE)
147                         thermo_do((ThermoDev)i);
148
149         timer_add(&thermo_timer);
150 }
151
152
153 /*!
154  * Set the target temperature \a temperature for a specific \a dev thermo-device.
155  */
156 void thermo_setTarget(ThermoDev dev, deg_t temperature)
157 {
158         ASSERT(dev < THERMO_CNT);
159         devs[dev].target = temperature;
160         devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
161
162         kprintf("setTarget dev[%d], T[%d.%d]\n", dev, temperature / 10, temperature % 10);
163 }
164
165 /*!
166  * Starts a thermo-regulation for channel \a dev.
167  */
168 void thermo_start(ThermoDev dev)
169 {
170         int i;
171         deg_t temp;
172
173         ASSERT(dev < THERMO_CNT);
174
175         devs[dev].status |= THERMO_ACTIVE;
176
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;
182
183         /* Reset timeout */
184         devs[dev].expire = timer_clock() + thermo_hw_timeout(dev);
185 }
186
187 /*!
188  * Stops a thermo-regulation for channel \a dev.
189  */
190 void thermo_stop(ThermoDev dev)
191 {
192         ASSERT(dev < THERMO_CNT);
193
194         devs[dev].status &= ~THERMO_ACTIVE;
195         thermo_hw_off(dev);
196 }
197
198
199 /*!
200  * Clear errors for channel \a dev.
201  */
202 void thermo_clearErrors(ThermoDev dev)
203 {
204         ASSERT(dev < THERMO_CNT);
205         devs[dev].status &= ~(THERMO_ERRMASK);
206 }
207
208
209 /*!
210  * Read the temperature of the thermo-device \a dev using mobile mean.
211  */
212 deg_t thermo_read_temperature(ThermoDev dev)
213 {
214         int i;
215         long accum = 0;
216
217         for (i = 0; i < THERMO_HIFI_NUM_SAMPLES; i++)
218                 accum += devs[dev].hifi_samples[i];
219
220         return (deg_t)(accum / THERMO_HIFI_NUM_SAMPLES);
221 }
222
223
224 /*!
225  * Init thermo-control and associated hw.
226  */
227 void thermo_init(void)
228 {
229         THERMO_HW_INIT;
230
231         /* Set all status to off */
232         for (int i = 0; i < THERMO_CNT; i++)
233                 devs[i].status = THERMO_OFF;
234
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);
238 }