Test for ticket system bug #14.
[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 #include <drv/timer.h>
41
42
43 #include <cfg/macros.h>
44 #include <cpu/irq.h>
45 #include <cpu/types.h>
46 #include <cfg/compiler.h>
47
48
49 #include <hw_phase.h>
50 #include <drv/phase.h>
51
52 #include <math.h>
53
54 /** Array  of triacs */
55 static Triac triacs[TRIAC_CNT];
56
57 DB(bool phase_initialized;)
58
59 /**
60  * Zerocross interrupt, call when 220V cross zero.
61  *
62  * This function turn off all triacs that have duty < 100%
63  * and arm the triac timers for phase control.
64  * This function is frequency adaptive so can work both at 50 or 60Hz.
65  */
66 DEFINE_ZEROCROSS_ISR()
67 {
68         ticks_t period, now;
69         static ticks_t prev_time;
70         TriacDev dev;
71
72         now = timer_clock_unlocked();
73         period = now - prev_time;
74
75         for (dev = 0; dev < TRIAC_CNT; dev++)
76         {
77                 /* Only turn off triac if duty is != 100% */
78                 if (triacs[dev].duty != TRIAC_MAX_DUTY)
79                         TRIAC_OFF(dev);
80                 /* Compute delay from duty */
81                 timer_setDelay(&triacs[dev].timer, DIV_ROUND(period * (TRIAC_MAX_DUTY - triacs[dev].duty), TRIAC_MAX_DUTY));
82
83                 /* This check avoids inserting the same timer twice
84                  * in case of an intempestive zerocross or spike */
85                 if (triacs[dev].running)
86                 {
87                         timer_abort(&triacs[dev].timer);
88                         //kprintf("[%lu]\n", timer_clock());
89                 }
90
91                 triacs[dev].running = true;
92                 timer_add(&triacs[dev].timer);
93         }
94         prev_time = now;
95 }
96
97
98
99 /**
100  * Set duty of the triac channel \a dev (interrupt safe).
101  */
102 void phase_setDuty(TriacDev dev, triac_duty_t duty)
103 {
104         cpuflags_t flags;
105         IRQ_SAVE_DISABLE(flags);
106
107         phase_setDutyUnlock(dev,duty);
108
109         IRQ_RESTORE(flags);
110 }
111
112
113
114 /**
115  * Set duty of the triac channel \a dev (NOT INTERRUPT SAFE).
116  */
117 void phase_setDutyUnlock(TriacDev dev, triac_duty_t duty)
118 {
119         triacs[dev].duty = MIN(duty, (triac_duty_t)TRIAC_MAX_DUTY);
120 }
121
122
123
124 /**
125  * Set power of the triac channel \a dev (interrupt safe).
126  *
127  * This function approsimate the sine wave to a triangular wave to compute
128  * RMS power.
129  */
130 void phase_setPower(TriacDev dev, triac_power_t power)
131 {
132         bool greater_fifty = false;
133         triac_duty_t duty;
134
135         power = MIN(power, (triac_power_t)TRIAC_MAX_POWER);
136
137         if (power > TRIAC_MAX_POWER / 2)
138         {
139                 greater_fifty = true;
140                 power = TRIAC_MAX_POWER - power;
141         }
142
143         duty = TRIAC_POWER_K * sqrt(power);
144
145         if (greater_fifty)
146                 duty = TRIAC_MAX_DUTY - duty;
147         phase_setDuty(dev, duty);
148 }
149
150
151
152 /**
153  * Soft int for each \a _dev triac.
154  *
155  * The triacs are turned on at different time to achieve phase control.
156  */
157 static void phase_softint(void *_dev)
158 {
159         TriacDev dev = (TriacDev)_dev;
160
161         /* Only turn on if duty is !=0 */
162         if (triacs[dev].duty)
163                 TRIAC_ON(dev);
164         triacs[dev].running = false;
165 }
166
167
168
169 /**
170  * Initialize phase control driver
171  */
172 void phase_init(void)
173 {
174         cpuflags_t flags;
175         TriacDev dev;
176
177         /* Init timers and ensure that all triac are off */
178         for (dev = 0; dev < TRIAC_CNT; dev++)
179         {
180                 triacs[dev].duty = 0;
181                 triacs[dev].running = false;
182                 SET_TRIAC_DDR(dev);
183                 TRIAC_OFF(dev);
184                 timer_set_event_softint(&triacs[dev].timer, (Hook)phase_softint, (void *)dev);
185         }
186         IRQ_SAVE_DISABLE(flags);
187
188         /* Init zero cross interrupt */
189         PHASE_HW_INIT;
190         DB(phase_initialized = true;)
191         IRQ_RESTORE(flags);
192 }