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