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