Init sysirq module directly in timer_hw_init for at91 family.
[bertos.git] / bertos / cpu / arm / drv / pwm_at91.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 2008 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  *
34  * \brief PWM hardware-specific implementation
35  *
36  * \version $Id$
37  *
38  * \author Daniele Basile <asterix@develer.com>
39  */
40
41 #include "pwm_at91.h"
42 #include "hw/pwm_map.h"
43 #include "hw/hw_cpu.h"
44
45 #include <cfg/macros.h>
46 #include <cfg/debug.h>
47
48 #include <io/arm.h>
49
50
51 /**
52  * Register structure for pwm driver.
53  * This array content all data and register pointer
54  * to manage pwm peripheral device.
55  */
56 static PwmChannel pwm_map[PWM_CNT] =
57 {
58         {//PWM Channel 0
59                 .duty_zero = false,
60                 .pol = false,
61                 .pwm_pin = BV(PWM0),
62                 .mode_reg = &PWM_CMR0,
63                 .duty_reg = &PWM_CDTY0,
64                 .period_reg = &PWM_CPRD0,
65                 .update_reg = &PWM_CUPD0,
66         },
67         {//PWM Channel 1
68                 .duty_zero = false,
69                 .pol = false,
70                 .pwm_pin = BV(PWM1),
71                 .mode_reg = &PWM_CMR1,
72                 .duty_reg = &PWM_CDTY1,
73                 .period_reg = &PWM_CPRD1,
74                 .update_reg = &PWM_CUPD1,
75         },
76         {//PWM Channel 2
77                 .duty_zero = false,
78         .pol = false,
79                 .pwm_pin = BV(PWM2),
80                 .mode_reg = &PWM_CMR2,
81                 .duty_reg = &PWM_CDTY2,
82                 .period_reg = &PWM_CPRD2,
83                 .update_reg = &PWM_CUPD2,
84         },
85         {//PWM Channel 3
86                 .duty_zero = false,
87                 .pol = false,
88                 .pwm_pin = BV(PWM3),
89                 .mode_reg = &PWM_CMR3,
90                 .duty_reg = &PWM_CDTY3,
91                 .period_reg = &PWM_CPRD3,
92                 .update_reg = &PWM_CUPD3,
93         }
94 };
95
96
97 /**
98  * Get preiod from select channel
99  *
100  * \a dev channel
101  */
102 pwm_period_t pwm_hw_getPeriod(PwmDev dev)
103 {
104         return *pwm_map[dev].period_reg;
105 }
106
107 /**
108  * Set pwm waveform frequecy.
109  *
110  * \a freq in Hz
111  */
112 void pwm_hw_setFrequency(PwmDev dev, uint32_t freq)
113 {
114         uint32_t period = 0;
115
116         for(int i = 0; i <= PWM_HW_MAX_PRESCALER_STEP; i++)
117         {
118                 period = CLOCK_FREQ / (BV(i) * freq);
119 //              TRACEMSG("period[%ld], prescale[%d]", period, i);
120                 if ((period < PWM_HW_MAX_PERIOD) && (period != 0))
121                 {
122                         //Clean previous channel prescaler, and set new
123                         *pwm_map[dev].mode_reg &= ~PWM_CPRE_MCK_MASK;
124                         *pwm_map[dev].mode_reg |= i;
125                         //Set pwm period
126                         *pwm_map[dev].period_reg = period;
127                         break;
128                 }
129         }
130
131         TRACEMSG("PWM ch[%d] period[%ld]", dev, period);
132 }
133
134 /**
135  * Set pwm duty cycle.
136  *
137  * \a duty value 0 - 2^16
138  */
139 void pwm_hw_setDutyUnlock(PwmDev dev, uint16_t duty)
140 {
141         ASSERT(duty <= (uint16_t)*pwm_map[dev].period_reg);
142
143
144         /*
145          * WARNING: is forbidden to write 0 to duty cycle value,
146          * and so for duty = 0 we must enable PIO and clear output!
147          */
148         if (!duty)
149         {
150                 PWM_PIO_PER = pwm_map[dev].pwm_pin;
151                 pwm_map[dev].duty_zero = true;
152         }
153         else
154         {
155                 ASSERT(PWM_CCNT0);
156         /*
157          * If polarity flag is true we must invert
158          * PWM polarity.
159          */
160         if (pwm_map[dev].pol)
161         {
162             duty = (uint16_t)*pwm_map[dev].period_reg - duty;
163 //            TRACEMSG("Inverted duty[%d], pol[%d]", duty, pwm_map[dev].pol);
164         }
165
166                 PWM_PIO_PDR = pwm_map[dev].pwm_pin;
167                 *pwm_map[dev].update_reg = duty;
168                 pwm_map[dev].duty_zero = false;
169         }
170
171         PWM_ENA = BV(dev);
172
173 //      TRACEMSG("PWM ch[%d] duty[%d], period[%ld]", dev, duty, *pwm_map[dev].period_reg);
174 }
175
176
177 /**
178  * Enable select pwm channel
179  */
180 void pwm_hw_enable(PwmDev dev)
181 {
182         if (!pwm_map[dev].duty_zero)
183                 PWM_PIO_PDR = pwm_map[dev].pwm_pin;
184 }
185
186 /**
187  * Disable select pwm channel
188  */
189 void pwm_hw_disable(PwmDev dev)
190 {
191         PWM_PIO_PER = pwm_map[dev].pwm_pin;
192 }
193
194 /**
195  * Set PWM polarity to select pwm channel
196  */
197 void pwm_hw_setPolarity(PwmDev dev, bool pol)
198 {
199         pwm_map[dev].pol = pol;
200 //    TRACEMSG("Set pol[%d]", pwm_map[dev].pol);
201 }
202
203 /**
204  * Init pwm.
205  */
206 void pwm_hw_init(void)
207 {
208
209         /*
210          * Init pwm:
211          * WARNING: is forbidden to write 0 to duty cycle value,
212          * and so for duty = 0 we must enable PIO and clear output!
213          * - clear PIO outputs
214          * - enable PIO outputs
215          * - Disable PIO and enable PWM functions
216          * - Power on PWM
217          */
218         PWM_PIO_CODR = BV(PWM0) | BV(PWM1) | BV(PWM2) | BV(PWM3);
219         PWM_PIO_OER = BV(PWM0) | BV(PWM1) | BV(PWM2) | BV(PWM3);
220         PWM_PIO_PDR = BV(PWM0) | BV(PWM1) | BV(PWM2) | BV(PWM3);
221         PWM_PIO_ABSR = BV(PWM0) | BV(PWM1) | BV(PWM2) | BV(PWM3);
222         PMC_PCER |= BV(PWMC_ID);
223
224         /* Disable all channels. */
225         PWM_DIS = 0xFFFFFFFF;
226         /* Disable prescalers A and B */
227         PWM_MR = 0;
228
229         /*
230          * Set pwm mode:
231          * - set period alidned to left
232          * - set output waveform to start at high level
233          * - allow duty cycle modify at next period event
234          */
235         for (int ch = 0; ch < PWM_CNT; ch++)
236         {
237                 *pwm_map[ch].mode_reg = 0;
238                 *pwm_map[ch].mode_reg = BV(PWM_CPOL);
239         }
240
241 }
242