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