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