1 |
580 |
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 "pio.h"
|
35 |
|
|
#include <board.h>
|
36 |
|
|
|
37 |
|
|
//------------------------------------------------------------------------------
|
38 |
|
|
// Local Functions
|
39 |
|
|
//------------------------------------------------------------------------------
|
40 |
|
|
|
41 |
|
|
//------------------------------------------------------------------------------
|
42 |
|
|
/// Configures one or more pin(s) of a PIO controller as being controlled by
|
43 |
|
|
/// peripheral A. Optionally, the corresponding internal pull-up(s) can be
|
44 |
|
|
/// enabled.
|
45 |
|
|
/// \param pio Pointer to a PIO controller.
|
46 |
|
|
/// \param mask Bitmask of one or more pin(s) to configure.
|
47 |
|
|
/// \param enablePullUp Indicates if the pin(s) internal pull-up shall be
|
48 |
|
|
/// configured.
|
49 |
|
|
//------------------------------------------------------------------------------
|
50 |
|
|
static void PIO_SetPeripheralA(
|
51 |
|
|
AT91S_PIO *pio,
|
52 |
|
|
unsigned int mask,
|
53 |
|
|
unsigned char enablePullUp)
|
54 |
|
|
{
|
55 |
|
|
#if !defined(AT91C_PIOA_ASR)
|
56 |
|
|
unsigned int abmr;
|
57 |
|
|
#endif
|
58 |
|
|
|
59 |
|
|
// Disable interrupts on the pin(s)
|
60 |
|
|
pio->PIO_IDR = mask;
|
61 |
|
|
|
62 |
|
|
// Enable the pull-up(s) if necessary
|
63 |
|
|
if (enablePullUp) {
|
64 |
|
|
|
65 |
|
|
pio->PIO_PPUER = mask;
|
66 |
|
|
}
|
67 |
|
|
else {
|
68 |
|
|
|
69 |
|
|
pio->PIO_PPUDR = mask;
|
70 |
|
|
}
|
71 |
|
|
|
72 |
|
|
// Configure pin
|
73 |
|
|
#if defined(AT91C_PIOA_ASR)
|
74 |
|
|
pio->PIO_ASR = mask;
|
75 |
|
|
#else
|
76 |
|
|
abmr = pio->PIO_ABSR;
|
77 |
|
|
pio->PIO_ABSR &= (~mask & abmr);
|
78 |
|
|
#endif
|
79 |
|
|
pio->PIO_PDR = mask;
|
80 |
|
|
}
|
81 |
|
|
|
82 |
|
|
//------------------------------------------------------------------------------
|
83 |
|
|
/// Configures one or more pin(s) of a PIO controller as being controlled by
|
84 |
|
|
/// peripheral B. Optionally, the corresponding internal pull-up(s) can be
|
85 |
|
|
/// enabled.
|
86 |
|
|
/// \param pio Pointer to a PIO controller.
|
87 |
|
|
/// \param mask Bitmask of one or more pin(s) to configure.
|
88 |
|
|
/// \param enablePullUp Indicates if the pin(s) internal pull-up shall be
|
89 |
|
|
/// configured.
|
90 |
|
|
//------------------------------------------------------------------------------
|
91 |
|
|
static void PIO_SetPeripheralB(
|
92 |
|
|
AT91S_PIO *pio,
|
93 |
|
|
unsigned int mask,
|
94 |
|
|
unsigned char enablePullUp)
|
95 |
|
|
{
|
96 |
|
|
#if !defined(AT91C_PIOA_BSR)
|
97 |
|
|
unsigned int abmr;
|
98 |
|
|
#endif
|
99 |
|
|
|
100 |
|
|
// Disable interrupts on the pin(s)
|
101 |
|
|
pio->PIO_IDR = mask;
|
102 |
|
|
|
103 |
|
|
// Enable the pull-up(s) if necessary
|
104 |
|
|
if (enablePullUp) {
|
105 |
|
|
|
106 |
|
|
pio->PIO_PPUER = mask;
|
107 |
|
|
}
|
108 |
|
|
else {
|
109 |
|
|
|
110 |
|
|
pio->PIO_PPUDR = mask;
|
111 |
|
|
}
|
112 |
|
|
|
113 |
|
|
// Configure pin
|
114 |
|
|
#if defined(AT91C_PIOA_BSR)
|
115 |
|
|
pio->PIO_BSR = mask;
|
116 |
|
|
#else
|
117 |
|
|
abmr = pio->PIO_ABSR;
|
118 |
|
|
pio->PIO_ABSR = mask | abmr;
|
119 |
|
|
#endif
|
120 |
|
|
pio->PIO_PDR = mask;
|
121 |
|
|
}
|
122 |
|
|
|
123 |
|
|
#if defined(AT91C_PIOA_IFDGSR) //Glitch or Debouncing filter selection supported
|
124 |
|
|
//------------------------------------------------------------------------------
|
125 |
|
|
/// Configures Glitch or Debouncing filter for input
|
126 |
|
|
/// \param pio Pointer to a PIO controller.
|
127 |
|
|
/// \param mask Bitmask for filter selection.
|
128 |
|
|
/// each of 32 bit field, 0 is Glitch, 1 is Debouncing
|
129 |
|
|
/// \param clkDiv Clock divider if Debouncing select, using the lowest 14 bits
|
130 |
|
|
/// common for all PIO line of selecting deboucing filter
|
131 |
|
|
//------------------------------------------------------------------------------
|
132 |
|
|
static void PIO_SetFilter(
|
133 |
|
|
AT91S_PIO *pio,
|
134 |
|
|
unsigned int filterSel,
|
135 |
|
|
unsigned int clkDiv)
|
136 |
|
|
{
|
137 |
|
|
pio->PIO_DIFSR = filterSel;//set Debouncing, 0 bit field no effect
|
138 |
|
|
pio->PIO_SCIFSR = ~filterSel;//set Glitch, 0 bit field no effect
|
139 |
|
|
|
140 |
|
|
pio->PIO_SCDR = clkDiv & 0x3FFF;//the lowest 14 bits work
|
141 |
|
|
}
|
142 |
|
|
#endif
|
143 |
|
|
|
144 |
|
|
//------------------------------------------------------------------------------
|
145 |
|
|
/// Configures one or more pin(s) or a PIO controller as inputs. Optionally,
|
146 |
|
|
/// the corresponding internal pull-up(s) and glitch filter(s) can be
|
147 |
|
|
/// enabled.
|
148 |
|
|
/// \param pio Pointer to a PIO controller.
|
149 |
|
|
/// \param mask Bitmask indicating which pin(s) to configure as input(s).
|
150 |
|
|
/// \param enablePullUp Indicates if the internal pull-up(s) must be enabled.
|
151 |
|
|
/// \param enableFilter Indicates if the glitch filter(s) must be enabled.
|
152 |
|
|
//------------------------------------------------------------------------------
|
153 |
|
|
static void PIO_SetInput(
|
154 |
|
|
AT91S_PIO *pio,
|
155 |
|
|
unsigned int mask,
|
156 |
|
|
unsigned char enablePullUp,
|
157 |
|
|
unsigned char enableFilter)
|
158 |
|
|
{
|
159 |
|
|
// Disable interrupts
|
160 |
|
|
pio->PIO_IDR = mask;
|
161 |
|
|
|
162 |
|
|
// Enable pull-up(s) if necessary
|
163 |
|
|
if (enablePullUp) {
|
164 |
|
|
|
165 |
|
|
pio->PIO_PPUER = mask;
|
166 |
|
|
}
|
167 |
|
|
else {
|
168 |
|
|
|
169 |
|
|
pio->PIO_PPUDR = mask;
|
170 |
|
|
}
|
171 |
|
|
|
172 |
|
|
// Enable filter(s) if necessary
|
173 |
|
|
if (enableFilter) {
|
174 |
|
|
|
175 |
|
|
pio->PIO_IFER = mask;
|
176 |
|
|
}
|
177 |
|
|
else {
|
178 |
|
|
|
179 |
|
|
pio->PIO_IFDR = mask;
|
180 |
|
|
}
|
181 |
|
|
|
182 |
|
|
// Configure pin as input
|
183 |
|
|
pio->PIO_ODR = mask;
|
184 |
|
|
pio->PIO_PER = mask;
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
//------------------------------------------------------------------------------
|
188 |
|
|
/// Configures one or more pin(s) of a PIO controller as outputs, with the
|
189 |
|
|
/// given default value. Optionally, the multi-drive feature can be enabled
|
190 |
|
|
/// on the pin(s).
|
191 |
|
|
/// \param pio Pointer to a PIO controller.
|
192 |
|
|
/// \param mask Bitmask indicating which pin(s) to configure.
|
193 |
|
|
/// \param defaultValue Default level on the pin(s).
|
194 |
|
|
/// \param enableMultiDrive Indicates if the pin(s) shall be configured as
|
195 |
|
|
/// open-drain.
|
196 |
|
|
/// \param enablePullUp Indicates if the pin shall have its pull-up activated.
|
197 |
|
|
//------------------------------------------------------------------------------
|
198 |
|
|
static void PIO_SetOutput(
|
199 |
|
|
AT91S_PIO *pio,
|
200 |
|
|
unsigned int mask,
|
201 |
|
|
unsigned char defaultValue,
|
202 |
|
|
unsigned char enableMultiDrive,
|
203 |
|
|
unsigned char enablePullUp)
|
204 |
|
|
{
|
205 |
|
|
// Disable interrupts
|
206 |
|
|
pio->PIO_IDR = mask;
|
207 |
|
|
|
208 |
|
|
// Enable pull-up(s) if necessary
|
209 |
|
|
if (enablePullUp) {
|
210 |
|
|
|
211 |
|
|
pio->PIO_PPUER = mask;
|
212 |
|
|
}
|
213 |
|
|
else {
|
214 |
|
|
|
215 |
|
|
pio->PIO_PPUDR = mask;
|
216 |
|
|
}
|
217 |
|
|
|
218 |
|
|
// Enable multi-drive if necessary
|
219 |
|
|
if (enableMultiDrive) {
|
220 |
|
|
|
221 |
|
|
pio->PIO_MDER = mask;
|
222 |
|
|
}
|
223 |
|
|
else {
|
224 |
|
|
|
225 |
|
|
pio->PIO_MDDR = mask;
|
226 |
|
|
}
|
227 |
|
|
|
228 |
|
|
// Set default value
|
229 |
|
|
if (defaultValue) {
|
230 |
|
|
|
231 |
|
|
pio->PIO_SODR = mask;
|
232 |
|
|
}
|
233 |
|
|
else {
|
234 |
|
|
|
235 |
|
|
pio->PIO_CODR = mask;
|
236 |
|
|
}
|
237 |
|
|
|
238 |
|
|
// Configure pin(s) as output(s)
|
239 |
|
|
pio->PIO_OER = mask;
|
240 |
|
|
pio->PIO_PER = mask;
|
241 |
|
|
}
|
242 |
|
|
|
243 |
|
|
//------------------------------------------------------------------------------
|
244 |
|
|
// Global Functions
|
245 |
|
|
//------------------------------------------------------------------------------
|
246 |
|
|
|
247 |
|
|
//------------------------------------------------------------------------------
|
248 |
|
|
/// Configures a list of Pin instances, each of which can either hold a single
|
249 |
|
|
/// pin or a group of pins, depending on the mask value; all pins are configured
|
250 |
|
|
/// by this function. The size of the array must also be provided and is easily
|
251 |
|
|
/// computed using PIO_LISTSIZE whenever its length is not known in advance.
|
252 |
|
|
/// \param list Pointer to a list of Pin instances.
|
253 |
|
|
/// \param size Size of the Pin list (calculated using PIO_LISTSIZE).
|
254 |
|
|
/// \return 1 if the pins have been configured properly; otherwise 0.
|
255 |
|
|
//------------------------------------------------------------------------------
|
256 |
|
|
unsigned char PIO_Configure(const Pin *list, unsigned int size)
|
257 |
|
|
{
|
258 |
|
|
// Configure pins
|
259 |
|
|
while (size > 0) {
|
260 |
|
|
|
261 |
|
|
switch (list->type) {
|
262 |
|
|
|
263 |
|
|
case PIO_PERIPH_A:
|
264 |
|
|
PIO_SetPeripheralA(list->pio,
|
265 |
|
|
list->mask,
|
266 |
|
|
(list->attribute & PIO_PULLUP) ? 1 : 0);
|
267 |
|
|
break;
|
268 |
|
|
|
269 |
|
|
case PIO_PERIPH_B:
|
270 |
|
|
PIO_SetPeripheralB(list->pio,
|
271 |
|
|
list->mask,
|
272 |
|
|
(list->attribute & PIO_PULLUP) ? 1 : 0);
|
273 |
|
|
break;
|
274 |
|
|
|
275 |
|
|
case PIO_INPUT:
|
276 |
|
|
AT91C_BASE_PMC->PMC_PCER = 1 << list->id;
|
277 |
|
|
PIO_SetInput(list->pio,
|
278 |
|
|
list->mask,
|
279 |
|
|
(list->attribute & PIO_PULLUP) ? 1 : 0,
|
280 |
|
|
(list->attribute & PIO_DEGLITCH)? 1 : 0);
|
281 |
|
|
|
282 |
|
|
#if defined(AT91C_PIOA_IFDGSR) //PIO3 with Glitch or Debouncing selection
|
283 |
|
|
//if glitch input filter enabled, set it
|
284 |
|
|
if(list->attribute & PIO_DEGLITCH)//Glitch input filter enabled
|
285 |
|
|
PIO_SetFilter(list->pio,
|
286 |
|
|
list->inFilter.filterSel,
|
287 |
|
|
list->inFilter.clkDivider);
|
288 |
|
|
#endif
|
289 |
|
|
break;
|
290 |
|
|
|
291 |
|
|
case PIO_OUTPUT_0:
|
292 |
|
|
case PIO_OUTPUT_1:
|
293 |
|
|
PIO_SetOutput(list->pio,
|
294 |
|
|
list->mask,
|
295 |
|
|
(list->type == PIO_OUTPUT_1),
|
296 |
|
|
(list->attribute & PIO_OPENDRAIN) ? 1 : 0,
|
297 |
|
|
(list->attribute & PIO_PULLUP) ? 1 : 0);
|
298 |
|
|
break;
|
299 |
|
|
|
300 |
|
|
default: return 0;
|
301 |
|
|
}
|
302 |
|
|
|
303 |
|
|
list++;
|
304 |
|
|
size--;
|
305 |
|
|
}
|
306 |
|
|
|
307 |
|
|
return 1;
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
//------------------------------------------------------------------------------
|
311 |
|
|
/// Sets a high output level on all the PIOs defined in the given Pin instance.
|
312 |
|
|
/// This has no immediate effects on PIOs that are not output, but the PIO
|
313 |
|
|
/// controller will memorize the value they are changed to outputs.
|
314 |
|
|
/// \param pin Pointer to a Pin instance describing one or more pins.
|
315 |
|
|
//------------------------------------------------------------------------------
|
316 |
|
|
void PIO_Set(const Pin *pin)
|
317 |
|
|
{
|
318 |
|
|
pin->pio->PIO_SODR = pin->mask;
|
319 |
|
|
}
|
320 |
|
|
|
321 |
|
|
//------------------------------------------------------------------------------
|
322 |
|
|
/// Sets a low output level on all the PIOs defined in the given Pin instance.
|
323 |
|
|
/// This has no immediate effects on PIOs that are not output, but the PIO
|
324 |
|
|
/// controller will memorize the value they are changed to outputs.
|
325 |
|
|
/// \param pin Pointer to a Pin instance describing one or more pins.
|
326 |
|
|
//------------------------------------------------------------------------------
|
327 |
|
|
void PIO_Clear(const Pin *pin)
|
328 |
|
|
{
|
329 |
|
|
pin->pio->PIO_CODR = pin->mask;
|
330 |
|
|
}
|
331 |
|
|
|
332 |
|
|
//------------------------------------------------------------------------------
|
333 |
|
|
/// Returns 1 if one or more PIO of the given Pin instance currently have a high
|
334 |
|
|
/// level; otherwise returns 0. This method returns the actual value that is
|
335 |
|
|
/// being read on the pin. To return the supposed output value of a pin, use
|
336 |
|
|
/// PIO_GetOutputDataStatus() instead.
|
337 |
|
|
/// \param pin Pointer to a Pin instance describing one or more pins.
|
338 |
|
|
/// \return 1 if the Pin instance contains at least one PIO that currently has
|
339 |
|
|
/// a high level; otherwise 0.
|
340 |
|
|
//------------------------------------------------------------------------------
|
341 |
|
|
unsigned char PIO_Get(const Pin *pin)
|
342 |
|
|
{
|
343 |
|
|
unsigned int reg;
|
344 |
|
|
if ((pin->type == PIO_OUTPUT_0) || (pin->type == PIO_OUTPUT_1)) {
|
345 |
|
|
|
346 |
|
|
reg = pin->pio->PIO_ODSR;
|
347 |
|
|
}
|
348 |
|
|
else {
|
349 |
|
|
|
350 |
|
|
reg = pin->pio->PIO_PDSR;
|
351 |
|
|
}
|
352 |
|
|
|
353 |
|
|
if ((reg & pin->mask) == 0) {
|
354 |
|
|
|
355 |
|
|
return 0;
|
356 |
|
|
}
|
357 |
|
|
else {
|
358 |
|
|
|
359 |
|
|
return 1;
|
360 |
|
|
}
|
361 |
|
|
}
|
362 |
|
|
|
363 |
|
|
|
364 |
|
|
//------------------------------------------------------------------------------
|
365 |
|
|
/// Returns 1 if one or more PIO of the given Pin are configured to output a
|
366 |
|
|
/// high level (even if they are not output).
|
367 |
|
|
/// To get the actual value of the pin, use PIO_Get() instead.
|
368 |
|
|
/// \param pin Pointer to a Pin instance describing one or more pins.
|
369 |
|
|
/// \return 1 if the Pin instance contains at least one PIO that is configured
|
370 |
|
|
/// to output a high level; otherwise 0.
|
371 |
|
|
//------------------------------------------------------------------------------
|
372 |
|
|
unsigned char PIO_GetOutputDataStatus(const Pin *pin)
|
373 |
|
|
{
|
374 |
|
|
if ((pin->pio->PIO_ODSR & pin->mask) == 0) {
|
375 |
|
|
|
376 |
|
|
return 0;
|
377 |
|
|
}
|
378 |
|
|
else {
|
379 |
|
|
|
380 |
|
|
return 1;
|
381 |
|
|
}
|
382 |
|
|
}
|