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