Update cpu.h dir.
[bertos.git] / drv / phase.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  * \version $Id$
33  *
34  * \brief Phase control driver (implementation)
35  *
36  * \version $Id$
37  * \author Francesco Sacchi <batt@develer.com>
38  */
39
40 /*#*
41  *#* $Log$
42  *#* Revision 1.2  2006/07/19 12:56:26  bernie
43  *#* Convert to new Doxygen style.
44  *#*
45  *#* Revision 1.1  2005/11/04 18:06:44  bernie
46  *#* Import into DevLib.
47  *#*
48  *#* Revision 1.1  2005/05/24 09:17:58  batt
49  *#* Move drivers to top-level.
50  *#*
51  *#* Revision 1.14  2005/05/20 12:21:05  batt
52  *#* Reformat.
53  *#*
54  *#* Revision 1.13  2005/05/09 16:34:14  batt
55  *#* Change some function names to accomplish coding standard; Add debug phase_initialized; Change duty_t and power_t to uint16_t.
56  *#*
57  *#* Revision 1.12  2005/05/04 17:22:30  batt
58  *#* Workaround a Doxygen parsing problem.
59  *#*
60  *#* Revision 1.11  2005/05/02 09:05:03  batt
61  *#* Rename duty_t and power_t in triac_duty_t and triac_power_t
62  *#*
63  *#* Revision 1.10  2005/05/02 08:48:55  batt
64  *#* Disable interrupt only when necessary.
65  *#*
66  *#* Revision 1.9  2005/04/29 11:52:51  batt
67  *#* Remove debug printf; Add a comment.
68  *#*
69  *#* Revision 1.8  2005/04/29 10:22:56  batt
70  *#* Avoid retriggering TRIAC on low duty-cycle.
71  *#*
72  *#* Revision 1.7  2005/04/29 09:54:36  batt
73  *#* Convert to new timer.
74  *#*
75  *#* Revision 1.6  2005/04/28 17:11:53  batt
76  *#* Expand abbreviation.
77  *#*
78  *#* Revision 1.5  2005/04/28 15:10:11  batt
79  *#* Use timer API to add and set events.
80  *#*
81  *#* Revision 1.4  2005/04/28 12:04:46  batt
82  *#* Add some comments.
83  *#*
84  *#* Revision 1.3  2005/04/28 10:35:45  batt
85  *#* Complete phase_setpower.
86  *#*
87  *#* Revision 1.2  2005/04/27 19:22:49  batt
88  *#* Add duty_t, power_t, MAX_DUTY and MAX_POWER
89  *#*
90  *#* Revision 1.1  2005/04/27 17:13:56  batt
91  *#* Add triac phase control driver.
92  *#*
93  *#*/
94
95 #include <drv/timer.h>
96
97
98 #include <cfg/macros.h>
99 #include <cpu/cpu.h>
100 #include <cfg/compiler.h>
101
102
103 #include <hw_phase.h>
104 #include <drv/phase.h>
105
106 #include <math.h>
107
108 /** Array  of triacs */
109 static Triac triacs[TRIAC_CNT];
110
111 DB(bool phase_initialized;)
112
113 /**
114  * Zerocross interrupt, call when 220V cross zero.
115  *
116  * This function turn off all triacs that have duty < 100%
117  * and arm the triac timers for phase control.
118  * This function is frequency adaptive so can work both at 50 or 60Hz.
119  */
120 DEFINE_ZEROCROSS_ISR()
121 {
122         ticks_t period, now;
123         static ticks_t prev_time;
124         TriacDev dev;
125
126         now = timer_clock_unlocked();
127         period = now - prev_time;
128
129         for (dev = 0; dev < TRIAC_CNT; dev++)
130         {
131                 /* Only turn off triac if duty is != 100% */
132                 if (triacs[dev].duty != TRIAC_MAX_DUTY)
133                         TRIAC_OFF(dev);
134                 /* Compute delay from duty */
135                 timer_setDelay(&triacs[dev].timer, ((period * (TRIAC_MAX_DUTY - triacs[dev].duty) + TRIAC_MAX_DUTY / 2) / TRIAC_MAX_DUTY));
136
137                 /* This check avoids inserting the same timer twice
138                  * in case of an intempestive zerocross or spike */
139                 if (triacs[dev].running)
140                 {
141                         timer_abort(&triacs[dev].timer);
142                         //kprintf("[%lu]\n", timer_clock());
143                 }
144
145                 triacs[dev].running = true;
146                 timer_add(&triacs[dev].timer);
147         }
148         prev_time = now;
149 }
150
151
152
153 /**
154  * Set duty of the triac channel \a dev (interrupt safe).
155  */
156 void phase_setDuty(TriacDev dev, triac_duty_t duty)
157 {
158         cpuflags_t flags;
159         IRQ_SAVE_DISABLE(flags);
160
161         phase_setDutyUnlock(dev,duty);
162
163         IRQ_RESTORE(flags);
164 }
165
166
167
168 /**
169  * Set duty of the triac channel \a dev (NOT INTERRUPT SAFE).
170  */
171 void phase_setDutyUnlock(TriacDev dev, triac_duty_t duty)
172 {
173         triacs[dev].duty = MIN(duty, (triac_duty_t)TRIAC_MAX_DUTY);
174 }
175
176
177
178 /**
179  * Set power of the triac channel \a dev (interrupt safe).
180  *
181  * This function approsimate the sine wave to a triangular wave to compute
182  * RMS power.
183  */
184 void phase_setPower(TriacDev dev, triac_power_t power)
185 {
186         bool greater_fifty = false;
187         triac_duty_t duty;
188
189         power = MIN(power, (triac_power_t)TRIAC_MAX_POWER);
190
191         if (power > TRIAC_MAX_POWER / 2)
192         {
193                 greater_fifty = true;
194                 power = TRIAC_MAX_POWER - power;
195         }
196
197         duty = TRIAC_POWER_K * sqrt(power);
198
199         if (greater_fifty)
200                 duty = TRIAC_MAX_DUTY - duty;
201         phase_setDuty(dev, duty);
202 }
203
204
205
206 /**
207  * Soft int for each \a _dev triac.
208  *
209  * The triacs are turned on at different time to achieve phase control.
210  */
211 static void phase_softint(void *_dev)
212 {
213         TriacDev dev = (TriacDev)_dev;
214
215         /* Only turn on if duty is !=0 */
216         if (triacs[dev].duty)
217                 TRIAC_ON(dev);
218         triacs[dev].running = false;
219 }
220
221
222
223 /**
224  * Initialize phase control driver
225  */
226 void phase_init(void)
227 {
228         cpuflags_t flags;
229         TriacDev dev;
230
231         /* Init timers and ensure that all triac are off */
232         for (dev = 0; dev < TRIAC_CNT; dev++)
233         {
234                 triacs[dev].duty = 0;
235                 triacs[dev].running = false;
236                 SET_TRIAC_DDR(dev);
237                 TRIAC_OFF(dev);
238                 timer_set_event_softint(&triacs[dev].timer, (Hook)phase_softint, (void *)dev);
239         }
240         IRQ_SAVE_DISABLE(flags);
241
242         /* Init zero cross interrupt */
243         PHASE_HW_INIT;
244         DB(phase_initialized = true;)
245         IRQ_RESTORE(flags);
246 }