9bba617d1c8285cb69095e29fe24f1fbd215a7d2
[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_cpu.h"
43
44 #include "appconfig.h"
45
46 #include <cfg/macros.h>
47 #include <cfg/debug.h>
48
49 #include <io/arm.h>
50
51
52 /**
53  * Register structure for pwm driver.
54  * This array content all data and register pointer
55  * to manage pwm peripheral device.
56  */
57 static PwmChannel pwm_map[PWM_CNT] =
58 {
59         {//PWM Channel 0
60                 .duty_zero = 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                 .pwm_pin = BV(PWM1),
70                 .mode_reg = &PWM_CMR1,
71                 .duty_reg = &PWM_CDTY1,
72                 .period_reg = &PWM_CPRD1,
73                 .update_reg = &PWM_CUPD1,
74         },
75         {//PWM Channel 2
76                 .duty_zero = false,
77                 .pwm_pin = BV(PWM2),
78                 .mode_reg = &PWM_CMR2,
79                 .duty_reg = &PWM_CDTY2,
80                 .period_reg = &PWM_CPRD2,
81                 .update_reg = &PWM_CUPD2,
82         },
83         {//PWM Channel 3
84                 .duty_zero = false,
85                 .pwm_pin = BV(PWM3),
86                 .mode_reg = &PWM_CMR3,
87                 .duty_reg = &PWM_CDTY3,
88                 .period_reg = &PWM_CPRD3,
89                 .update_reg = &PWM_CUPD3,
90         }
91 };
92
93
94 /**
95  * Get preiod from select channel
96  *
97  * \a dev channel
98  */
99 pwm_period_t pwm_hw_getPeriod(PwmDev dev)
100 {
101         return *pwm_map[dev].period_reg;
102 }
103
104 /**
105  * Set pwm waveform frequecy.
106  *
107  * \a freq in Hz
108  */
109 void pwm_hw_setFrequency(PwmDev dev, uint32_t freq)
110 {
111         uint32_t period = 0;
112
113         for(int i = 0; i <= PWM_HW_MAX_PRESCALER_STEP; i++)
114         {
115                 period = CLOCK_FREQ / (BV(i) * freq);
116 //              TRACEMSG("period[%d], prescale[%d]", period, i);
117                 if ((period < PWM_HW_MAX_PERIOD) && (period != 0))
118                 {
119                         //Clean previous channel prescaler, and set new
120                         *pwm_map[dev].mode_reg &= ~PWM_CPRE_MCK_MASK;
121                         *pwm_map[dev].mode_reg |= i;
122                         //Set pwm period
123                         *pwm_map[dev].period_reg = period;
124                         break;
125                 }
126         }
127
128         PWM_ENA = BV(dev);
129
130 //      TRACEMSG("PWM ch[%d] period[%d]", dev, period);
131 }
132
133 /**
134  * Set pwm duty cycle.
135  *
136  * \a duty value 0 - 2^16
137  */
138 void pwm_hw_setDutyUnlock(PwmDev dev, uint16_t duty)
139 {
140         ASSERT(duty <= (uint16_t)*pwm_map[dev].period_reg);
141
142
143         /*
144          * WARNING: is forbidden to write 0 to duty cycle value,
145          * and so for duty = 0 we must enable PIO and clear output!
146          */
147         if (!duty)
148         {
149                 PWM_PIO_PER = pwm_map[dev].pwm_pin;
150                 pwm_map[dev].duty_zero = true;
151         }
152         else
153         {
154                 ASSERT(PWM_CCNT0);
155                 PWM_PIO_PDR = pwm_map[dev].pwm_pin;
156                 *pwm_map[dev].update_reg = duty;
157                 pwm_map[dev].duty_zero = false;
158         }
159
160 //      TRACEMSG("PWM ch[%d] duty[%d], period[%ld]", dev, duty, *pwm_map[dev].period_reg);
161 }
162
163
164 /**
165  * Enable select pwm channel
166  */
167 void pwm_hw_enable(PwmDev dev)
168 {
169         if (!pwm_map[dev].duty_zero)
170                 PWM_PIO_PDR = pwm_map[dev].pwm_pin;
171 }
172
173 /**
174  * Disable select pwm channel
175  */
176 void pwm_hw_disable(PwmDev dev)
177 {
178         PWM_PIO_PER = pwm_map[dev].pwm_pin;
179 }
180
181
182 /**
183  * Init pwm.
184  */
185 void pwm_hw_init(void)
186 {
187
188         /*
189          * Init pwm:
190          * WARNING: is forbidden to write 0 to duty cycle value,
191          * and so for duty = 0 we must enable PIO and clear output!
192          * - clear PIO outputs
193          * - enable PIO outputs
194          * - Disable PIO and enable PWM functions
195          * - Power on PWM
196          */
197         PWM_PIO_CODR = BV(PWM0) | BV(PWM1) | BV(PWM2) | BV(PWM3);
198         PWM_PIO_OER = BV(PWM0) | BV(PWM1) | BV(PWM2) | BV(PWM3);
199         PWM_PIO_PDR = BV(PWM0) | BV(PWM1) | BV(PWM2) | BV(PWM3);
200         PWM_PIO_ABSR = BV(PWM0) | BV(PWM1) | BV(PWM2) | BV(PWM3);
201         PMC_PCER |= BV(PWMC_ID);
202
203         /* Disable all channels. */
204         PWM_DIS = 0xFFFFFFFF;
205         /* Disable prescalers A and B */
206         PWM_MR = 0;
207
208         /*
209          * Set pwm mode:
210          * - set period alidned to left
211          * - set output waveform to low level
212          * - allow duty cycle modify at next period event
213          */
214         for (int ch = 0; ch < PWM_CNT; ch++)
215                 *pwm_map[ch].mode_reg = 0;
216 }
217