1 |
608 |
jeremybenn |
/* ----------------------------------------------------------------------------
|
2 |
|
|
* ATMEL Microcontroller Software Support
|
3 |
|
|
* ----------------------------------------------------------------------------
|
4 |
|
|
* Copyright (c) 2008, Atmel Corporation
|
5 |
|
|
*
|
6 |
|
|
* All rights reserved.
|
7 |
|
|
*
|
8 |
|
|
* Redistribution and use in source and binary forms, with or without
|
9 |
|
|
* modification, are permitted provided that the following conditions are met:
|
10 |
|
|
*
|
11 |
|
|
* - Redistributions of source code must retain the above copyright notice,
|
12 |
|
|
* this list of conditions and the disclaimer below.
|
13 |
|
|
*
|
14 |
|
|
* Atmel's name may not be used to endorse or promote products derived from
|
15 |
|
|
* this software without specific prior written permission.
|
16 |
|
|
*
|
17 |
|
|
* DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
|
18 |
|
|
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
19 |
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
|
20 |
|
|
* DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
|
21 |
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
22 |
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
|
23 |
|
|
* OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
24 |
|
|
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
25 |
|
|
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
|
26 |
|
|
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
27 |
|
|
* ----------------------------------------------------------------------------
|
28 |
|
|
*/
|
29 |
|
|
|
30 |
|
|
//------------------------------------------------------------------------------
|
31 |
|
|
// Headers
|
32 |
|
|
//------------------------------------------------------------------------------
|
33 |
|
|
|
34 |
|
|
#include "pwmc.h"
|
35 |
|
|
#include <board.h>
|
36 |
|
|
#include <utility/assert.h>
|
37 |
|
|
#include <utility/trace.h>
|
38 |
|
|
|
39 |
|
|
//------------------------------------------------------------------------------
|
40 |
|
|
// Local functions
|
41 |
|
|
//------------------------------------------------------------------------------
|
42 |
|
|
|
43 |
|
|
//------------------------------------------------------------------------------
|
44 |
|
|
/// Finds a prescaler/divisor couple to generate the desired frequency from
|
45 |
|
|
/// MCK.
|
46 |
|
|
/// Returns the value to enter in PWMC_MR or 0 if the configuration cannot be
|
47 |
|
|
/// met.
|
48 |
|
|
/// \param frequency Desired frequency in Hz.
|
49 |
|
|
/// \param mck Master clock frequency in Hz.
|
50 |
|
|
//------------------------------------------------------------------------------
|
51 |
|
|
static unsigned short FindClockConfiguration(
|
52 |
|
|
unsigned int frequency,
|
53 |
|
|
unsigned int mck)
|
54 |
|
|
{
|
55 |
|
|
unsigned int divisors[11] = {1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024};
|
56 |
|
|
unsigned char divisor = 0;
|
57 |
|
|
unsigned int prescaler;
|
58 |
|
|
|
59 |
|
|
SANITY_CHECK(frequency < mck);
|
60 |
|
|
|
61 |
|
|
// Find prescaler and divisor values
|
62 |
|
|
prescaler = (mck / divisors[divisor]) / frequency;
|
63 |
|
|
while ((prescaler > 255) && (divisor < 11)) {
|
64 |
|
|
|
65 |
|
|
divisor++;
|
66 |
|
|
prescaler = (mck / divisors[divisor]) / frequency;
|
67 |
|
|
}
|
68 |
|
|
|
69 |
|
|
// Return result
|
70 |
|
|
if (divisor < 11) {
|
71 |
|
|
|
72 |
|
|
trace_LOG(trace_DEBUG, "-D- Found divisor=%u and prescaler=%u for freq=%uHz\n\r",
|
73 |
|
|
divisors[divisor], prescaler, frequency);
|
74 |
|
|
return prescaler | (divisor << 8);
|
75 |
|
|
}
|
76 |
|
|
else {
|
77 |
|
|
|
78 |
|
|
return 0;
|
79 |
|
|
}
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
//------------------------------------------------------------------------------
|
83 |
|
|
// Global functions
|
84 |
|
|
//------------------------------------------------------------------------------
|
85 |
|
|
|
86 |
|
|
//------------------------------------------------------------------------------
|
87 |
|
|
/// Configures PWM a channel with the given parameters.
|
88 |
|
|
/// The PWM controller must have been clocked in the PMC prior to calling this
|
89 |
|
|
/// function.
|
90 |
|
|
/// \param channel Channel number.
|
91 |
|
|
/// \param prescaler Channel prescaler.
|
92 |
|
|
/// \param alignment Channel alignment.
|
93 |
|
|
/// \param polarity Channel polarity.
|
94 |
|
|
//------------------------------------------------------------------------------
|
95 |
|
|
void PWMC_ConfigureChannel(
|
96 |
|
|
unsigned char channel,
|
97 |
|
|
unsigned int prescaler,
|
98 |
|
|
unsigned int alignment,
|
99 |
|
|
unsigned int polarity)
|
100 |
|
|
{
|
101 |
|
|
SANITY_CHECK(prescaler < AT91C_PWMC_CPRE_MCKB);
|
102 |
|
|
SANITY_CHECK((alignment & ~AT91C_PWMC_CALG) == 0);
|
103 |
|
|
SANITY_CHECK((polarity & ~AT91C_PWMC_CPOL) == 0);
|
104 |
|
|
|
105 |
|
|
// Disable channel
|
106 |
|
|
AT91C_BASE_PWMC->PWMC_DIS = 1 << channel;
|
107 |
|
|
|
108 |
|
|
// Configure channel
|
109 |
|
|
AT91C_BASE_PWMC->PWMC_CH[channel].PWMC_CMR = prescaler | alignment | polarity;
|
110 |
|
|
}
|
111 |
|
|
|
112 |
|
|
//------------------------------------------------------------------------------
|
113 |
|
|
/// Configures PWM clocks A & B to run at the given frequencies. This function
|
114 |
|
|
/// finds the best MCK divisor and prescaler values automatically.
|
115 |
|
|
/// \param clka Desired clock A frequency (0 if not used).
|
116 |
|
|
/// \param clkb Desired clock B frequency (0 if not used).
|
117 |
|
|
/// \param mck Master clock frequency.
|
118 |
|
|
//------------------------------------------------------------------------------
|
119 |
|
|
void PWMC_ConfigureClocks(unsigned int clka, unsigned int clkb, unsigned int mck)
|
120 |
|
|
{
|
121 |
|
|
unsigned int mode = 0;
|
122 |
|
|
unsigned int result;
|
123 |
|
|
|
124 |
|
|
// Clock A
|
125 |
|
|
if (clka != 0) {
|
126 |
|
|
|
127 |
|
|
result = FindClockConfiguration(clka, mck);
|
128 |
|
|
ASSERT(result != 0, "-F- Could not generate the desired PWM frequency (%uHz)\n\r", clka);
|
129 |
|
|
mode |= result;
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
// Clock B
|
133 |
|
|
if (clkb != 0) {
|
134 |
|
|
|
135 |
|
|
result = FindClockConfiguration(clkb, mck);
|
136 |
|
|
ASSERT(result != 0, "-F- Could not generate the desired PWM frequency (%uHz)\n\r", clkb);
|
137 |
|
|
mode |= (result << 16);
|
138 |
|
|
}
|
139 |
|
|
|
140 |
|
|
// Configure clocks
|
141 |
|
|
trace_LOG(trace_DEBUG, "-D- Setting PWMC_MR = 0x%08X\n\r", mode);
|
142 |
|
|
AT91C_BASE_PWMC->PWMC_MR = mode;
|
143 |
|
|
}
|
144 |
|
|
|
145 |
|
|
//------------------------------------------------------------------------------
|
146 |
|
|
/// Sets the period value used by a PWM channel. This function writes directly
|
147 |
|
|
/// to the CPRD register if the channel is disabled; otherwise, it uses the
|
148 |
|
|
/// update register CUPD.
|
149 |
|
|
/// \param channel Channel number.
|
150 |
|
|
/// \param period Period value.
|
151 |
|
|
//------------------------------------------------------------------------------
|
152 |
|
|
void PWMC_SetPeriod(unsigned char channel, unsigned short period)
|
153 |
|
|
{
|
154 |
|
|
// If channel is disabled, write to CPRD
|
155 |
|
|
if ((AT91C_BASE_PWMC->PWMC_SR & (1 << channel)) == 0) {
|
156 |
|
|
|
157 |
|
|
AT91C_BASE_PWMC->PWMC_CH[channel].PWMC_CPRDR = period;
|
158 |
|
|
}
|
159 |
|
|
// Otherwise use update register
|
160 |
|
|
else {
|
161 |
|
|
|
162 |
|
|
AT91C_BASE_PWMC->PWMC_CH[channel].PWMC_CMR |= AT91C_PWMC_CPD;
|
163 |
|
|
AT91C_BASE_PWMC->PWMC_CH[channel].PWMC_CUPDR = period;
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
//------------------------------------------------------------------------------
|
168 |
|
|
/// Sets the duty cycle used by a PWM channel. This function writes directly to
|
169 |
|
|
/// the CDTY register if the channel is disabled; otherwise it uses the
|
170 |
|
|
/// update register CUPD.
|
171 |
|
|
/// Note that the duty cycle must always be inferior or equal to the channel
|
172 |
|
|
/// period.
|
173 |
|
|
/// \param channel Channel number.
|
174 |
|
|
/// \param duty Duty cycle value.
|
175 |
|
|
//------------------------------------------------------------------------------
|
176 |
|
|
void PWMC_SetDutyCycle(unsigned char channel, unsigned short duty)
|
177 |
|
|
{
|
178 |
|
|
SANITY_CHECK(duty <= AT91C_BASE_PWMC->PWMC_CH[channel].PWMC_CPRDR);
|
179 |
|
|
|
180 |
|
|
// SAM7S errata
|
181 |
|
|
#if defined(at91sam7s16) || defined(at91sam7s161) || defined(at91sam7s32) \
|
182 |
|
|
|| defined(at91sam7s321) || defined(at91sam7s64) || defined(at91sam7s128) \
|
183 |
|
|
|| defined(at91sam7s256) || defined(at91sam7s512)
|
184 |
|
|
ASSERT(duty > 0, "-F- Duty cycle value 0 is not permitted on SAM7S chips.\n\r");
|
185 |
|
|
ASSERT((duty > 1) || (AT91C_BASE_PWMC->PWMC_CH[channel].PWMC_CMR & AT91C_PWMC_CALG),
|
186 |
|
|
"-F- Duty cycle value 1 is not permitted in left-aligned mode on SAM7S chips.\n\r");
|
187 |
|
|
#endif
|
188 |
|
|
|
189 |
|
|
// If channel is disabled, write to CDTY
|
190 |
|
|
if ((AT91C_BASE_PWMC->PWMC_SR & (1 << channel)) == 0) {
|
191 |
|
|
|
192 |
|
|
AT91C_BASE_PWMC->PWMC_CH[channel].PWMC_CDTYR = duty;
|
193 |
|
|
}
|
194 |
|
|
// Otherwise use update register
|
195 |
|
|
else {
|
196 |
|
|
|
197 |
|
|
AT91C_BASE_PWMC->PWMC_CH[channel].PWMC_CMR &= ~AT91C_PWMC_CPD;
|
198 |
|
|
AT91C_BASE_PWMC->PWMC_CH[channel].PWMC_CUPDR = duty;
|
199 |
|
|
}
|
200 |
|
|
}
|
201 |
|
|
|
202 |
|
|
//------------------------------------------------------------------------------
|
203 |
|
|
/// Enables the given PWM channel. This does NOT enable the corresponding pin;
|
204 |
|
|
/// this must be done in the user code.
|
205 |
|
|
/// \param channel Channel number.
|
206 |
|
|
//------------------------------------------------------------------------------
|
207 |
|
|
void PWMC_EnableChannel(unsigned char channel)
|
208 |
|
|
{
|
209 |
|
|
AT91C_BASE_PWMC->PWMC_ENA = 1 << channel;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
//------------------------------------------------------------------------------
|
213 |
|
|
/// Disables the given PWM channel.
|
214 |
|
|
/// \param channel Channel number.
|
215 |
|
|
//------------------------------------------------------------------------------
|
216 |
|
|
void PWMC_DisableChannel(unsigned char channel)
|
217 |
|
|
{
|
218 |
|
|
AT91C_BASE_PWMC->PWMC_DIS = 1 << channel;
|
219 |
|
|
}
|
220 |
|
|
|
221 |
|
|
//------------------------------------------------------------------------------
|
222 |
|
|
/// Enables the period interrupt for the given PWM channel.
|
223 |
|
|
/// \param channel Channel number.
|
224 |
|
|
//------------------------------------------------------------------------------
|
225 |
|
|
void PWMC_EnableChannelIt(unsigned char channel)
|
226 |
|
|
{
|
227 |
|
|
AT91C_BASE_PWMC->PWMC_IER = 1 << channel;
|
228 |
|
|
}
|
229 |
|
|
|
230 |
|
|
//------------------------------------------------------------------------------
|
231 |
|
|
/// Disables the period interrupt for the given PWM channel.
|
232 |
|
|
/// \param channel Channel number.
|
233 |
|
|
//------------------------------------------------------------------------------
|
234 |
|
|
void PWMC_DisableChannelIt(unsigned char channel)
|
235 |
|
|
{
|
236 |
|
|
AT91C_BASE_PWMC->PWMC_IDR = 1 << channel;
|
237 |
|
|
}
|
238 |
|
|
|