1 /* ----------------------------------------------------------------------------
\r
2 * ATMEL Microcontroller Software Support
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2008, Atmel Corporation
\r
6 * All rights reserved.
\r
8 * Redistribution and use in source and binary forms, with or without
\r
9 * modification, are permitted provided that the following conditions are met:
\r
11 * - Redistributions of source code must retain the above copyright notice,
\r
12 * this list of conditions and the disclaimer below.
\r
14 * Atmel's name may not be used to endorse or promote products derived from
\r
15 * this software without specific prior written permission.
\r
17 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
\r
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
\r
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
\r
20 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
\r
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
\r
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
\r
23 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
\r
24 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
\r
25 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
\r
26 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
\r
27 * ----------------------------------------------------------------------------
\r
30 //------------------------------------------------------------------------------
\r
32 //------------------------------------------------------------------------------
\r
37 //------------------------------------------------------------------------------
\r
39 //------------------------------------------------------------------------------
\r
41 //------------------------------------------------------------------------------
\r
42 /// Configures one or more pin(s) of a PIO controller as being controlled by
\r
43 /// peripheral A. Optionally, the corresponding internal pull-up(s) can be
\r
45 /// \param pio Pointer to a PIO controller.
\r
46 /// \param mask Bitmask of one or more pin(s) to configure.
\r
47 /// \param enablePullUp Indicates if the pin(s) internal pull-up shall be
\r
49 //------------------------------------------------------------------------------
\r
50 static void PIO_SetPeripheralA(
\r
53 unsigned char enablePullUp)
\r
55 #if !defined(AT91C_PIOA_ASR)
\r
59 // Disable interrupts on the pin(s)
\r
60 pio->PIO_IDR = mask;
\r
62 // Enable the pull-up(s) if necessary
\r
65 pio->PIO_PPUER = mask;
\r
69 pio->PIO_PPUDR = mask;
\r
73 #if defined(AT91C_PIOA_ASR)
\r
74 pio->PIO_ASR = mask;
\r
76 abmr = pio->PIO_ABSR;
\r
77 pio->PIO_ABSR &= (~mask & abmr);
\r
79 pio->PIO_PDR = mask;
\r
82 //------------------------------------------------------------------------------
\r
83 /// Configures one or more pin(s) of a PIO controller as being controlled by
\r
84 /// peripheral B. Optionally, the corresponding internal pull-up(s) can be
\r
86 /// \param pio Pointer to a PIO controller.
\r
87 /// \param mask Bitmask of one or more pin(s) to configure.
\r
88 /// \param enablePullUp Indicates if the pin(s) internal pull-up shall be
\r
90 //------------------------------------------------------------------------------
\r
91 static void PIO_SetPeripheralB(
\r
94 unsigned char enablePullUp)
\r
96 #if !defined(AT91C_PIOA_BSR)
\r
100 // Disable interrupts on the pin(s)
\r
101 pio->PIO_IDR = mask;
\r
103 // Enable the pull-up(s) if necessary
\r
104 if (enablePullUp) {
\r
106 pio->PIO_PPUER = mask;
\r
110 pio->PIO_PPUDR = mask;
\r
114 #if defined(AT91C_PIOA_BSR)
\r
115 pio->PIO_BSR = mask;
\r
117 abmr = pio->PIO_ABSR;
\r
118 pio->PIO_ABSR = mask | abmr;
\r
120 pio->PIO_PDR = mask;
\r
123 #if defined(AT91C_PIOA_IFDGSR) //Glitch or Debouncing filter selection supported
\r
124 //------------------------------------------------------------------------------
\r
125 /// Configures Glitch or Debouncing filter for input
\r
126 /// \param pio Pointer to a PIO controller.
\r
127 /// \param mask Bitmask for filter selection.
\r
128 /// each of 32 bit field, 0 is Glitch, 1 is Debouncing
\r
129 /// \param clkDiv Clock divider if Debouncing select, using the lowest 14 bits
\r
130 /// common for all PIO line of selecting deboucing filter
\r
131 //------------------------------------------------------------------------------
\r
132 static void PIO_SetFilter(
\r
134 unsigned int filterSel,
\r
135 unsigned int clkDiv)
\r
137 pio->PIO_DIFSR = filterSel;//set Debouncing, 0 bit field no effect
\r
138 pio->PIO_SCIFSR = ~filterSel;//set Glitch, 0 bit field no effect
\r
140 pio->PIO_SCDR = clkDiv & 0x3FFF;//the lowest 14 bits work
\r
144 //------------------------------------------------------------------------------
\r
145 /// Configures one or more pin(s) or a PIO controller as inputs. Optionally,
\r
146 /// the corresponding internal pull-up(s) and glitch filter(s) can be
\r
148 /// \param pio Pointer to a PIO controller.
\r
149 /// \param mask Bitmask indicating which pin(s) to configure as input(s).
\r
150 /// \param enablePullUp Indicates if the internal pull-up(s) must be enabled.
\r
151 /// \param enableFilter Indicates if the glitch filter(s) must be enabled.
\r
152 //------------------------------------------------------------------------------
\r
153 static void PIO_SetInput(
\r
156 unsigned char enablePullUp,
\r
157 unsigned char enableFilter)
\r
159 // Disable interrupts
\r
160 pio->PIO_IDR = mask;
\r
162 // Enable pull-up(s) if necessary
\r
163 if (enablePullUp) {
\r
165 pio->PIO_PPUER = mask;
\r
169 pio->PIO_PPUDR = mask;
\r
172 // Enable filter(s) if necessary
\r
173 if (enableFilter) {
\r
175 pio->PIO_IFER = mask;
\r
179 pio->PIO_IFDR = mask;
\r
182 // Configure pin as input
\r
183 pio->PIO_ODR = mask;
\r
184 pio->PIO_PER = mask;
\r
187 //------------------------------------------------------------------------------
\r
188 /// Configures one or more pin(s) of a PIO controller as outputs, with the
\r
189 /// given default value. Optionally, the multi-drive feature can be enabled
\r
191 /// \param pio Pointer to a PIO controller.
\r
192 /// \param mask Bitmask indicating which pin(s) to configure.
\r
193 /// \param defaultValue Default level on the pin(s).
\r
194 /// \param enableMultiDrive Indicates if the pin(s) shall be configured as
\r
196 /// \param enablePullUp Indicates if the pin shall have its pull-up activated.
\r
197 //------------------------------------------------------------------------------
\r
198 static void PIO_SetOutput(
\r
201 unsigned char defaultValue,
\r
202 unsigned char enableMultiDrive,
\r
203 unsigned char enablePullUp)
\r
205 // Disable interrupts
\r
206 pio->PIO_IDR = mask;
\r
208 // Enable pull-up(s) if necessary
\r
209 if (enablePullUp) {
\r
211 pio->PIO_PPUER = mask;
\r
215 pio->PIO_PPUDR = mask;
\r
218 // Enable multi-drive if necessary
\r
219 if (enableMultiDrive) {
\r
221 pio->PIO_MDER = mask;
\r
225 pio->PIO_MDDR = mask;
\r
228 // Set default value
\r
229 if (defaultValue) {
\r
231 pio->PIO_SODR = mask;
\r
235 pio->PIO_CODR = mask;
\r
238 // Configure pin(s) as output(s)
\r
239 pio->PIO_OER = mask;
\r
240 pio->PIO_PER = mask;
\r
243 //------------------------------------------------------------------------------
\r
244 // Global Functions
\r
245 //------------------------------------------------------------------------------
\r
247 //------------------------------------------------------------------------------
\r
248 /// Configures a list of Pin instances, each of which can either hold a single
\r
249 /// pin or a group of pins, depending on the mask value; all pins are configured
\r
250 /// by this function. The size of the array must also be provided and is easily
\r
251 /// computed using PIO_LISTSIZE whenever its length is not known in advance.
\r
252 /// \param list Pointer to a list of Pin instances.
\r
253 /// \param size Size of the Pin list (calculated using PIO_LISTSIZE).
\r
254 /// \return 1 if the pins have been configured properly; otherwise 0.
\r
255 //------------------------------------------------------------------------------
\r
256 unsigned char PIO_Configure(const Pin *list, unsigned int size)
\r
261 switch (list->type) {
\r
264 PIO_SetPeripheralA(list->pio,
\r
266 (list->attribute & PIO_PULLUP) ? 1 : 0);
\r
270 PIO_SetPeripheralB(list->pio,
\r
272 (list->attribute & PIO_PULLUP) ? 1 : 0);
\r
276 AT91C_BASE_PMC->PMC_PCER = 1 << list->id;
\r
277 PIO_SetInput(list->pio,
\r
279 (list->attribute & PIO_PULLUP) ? 1 : 0,
\r
280 (list->attribute & PIO_DEGLITCH)? 1 : 0);
\r
282 #if defined(AT91C_PIOA_IFDGSR) //PIO3 with Glitch or Debouncing selection
\r
283 //if glitch input filter enabled, set it
\r
284 if(list->attribute & PIO_DEGLITCH)//Glitch input filter enabled
\r
285 PIO_SetFilter(list->pio,
\r
286 list->inFilter.filterSel,
\r
287 list->inFilter.clkDivider);
\r
293 PIO_SetOutput(list->pio,
\r
295 (list->type == PIO_OUTPUT_1),
\r
296 (list->attribute & PIO_OPENDRAIN) ? 1 : 0,
\r
297 (list->attribute & PIO_PULLUP) ? 1 : 0);
\r
310 //------------------------------------------------------------------------------
\r
311 /// Sets a high output level on all the PIOs defined in the given Pin instance.
\r
312 /// This has no immediate effects on PIOs that are not output, but the PIO
\r
313 /// controller will memorize the value they are changed to outputs.
\r
314 /// \param pin Pointer to a Pin instance describing one or more pins.
\r
315 //------------------------------------------------------------------------------
\r
316 void PIO_Set(const Pin *pin)
\r
318 pin->pio->PIO_SODR = pin->mask;
\r
321 //------------------------------------------------------------------------------
\r
322 /// Sets a low output level on all the PIOs defined in the given Pin instance.
\r
323 /// This has no immediate effects on PIOs that are not output, but the PIO
\r
324 /// controller will memorize the value they are changed to outputs.
\r
325 /// \param pin Pointer to a Pin instance describing one or more pins.
\r
326 //------------------------------------------------------------------------------
\r
327 void PIO_Clear(const Pin *pin)
\r
329 pin->pio->PIO_CODR = pin->mask;
\r
332 //------------------------------------------------------------------------------
\r
333 /// Returns 1 if one or more PIO of the given Pin instance currently have a high
\r
334 /// level; otherwise returns 0. This method returns the actual value that is
\r
335 /// being read on the pin. To return the supposed output value of a pin, use
\r
336 /// PIO_GetOutputDataStatus() instead.
\r
337 /// \param pin Pointer to a Pin instance describing one or more pins.
\r
338 /// \return 1 if the Pin instance contains at least one PIO that currently has
\r
339 /// a high level; otherwise 0.
\r
340 //------------------------------------------------------------------------------
\r
341 unsigned char PIO_Get(const Pin *pin)
\r
344 if ((pin->type == PIO_OUTPUT_0) || (pin->type == PIO_OUTPUT_1)) {
\r
346 reg = pin->pio->PIO_ODSR;
\r
350 reg = pin->pio->PIO_PDSR;
\r
353 if ((reg & pin->mask) == 0) {
\r
364 //------------------------------------------------------------------------------
\r
365 /// Returns 1 if one or more PIO of the given Pin are configured to output a
\r
366 /// high level (even if they are not output).
\r
367 /// To get the actual value of the pin, use PIO_Get() instead.
\r
368 /// \param pin Pointer to a Pin instance describing one or more pins.
\r
369 /// \return 1 if the Pin instance contains at least one PIO that is configured
\r
370 /// to output a high level; otherwise 0.
\r
371 //------------------------------------------------------------------------------
\r
372 unsigned char PIO_GetOutputDataStatus(const Pin *pin)
\r
374 if ((pin->pio->PIO_ODSR & pin->mask) == 0) {
\r