1 /* ----------------------------------------------------------------------------
\r
2 * SAM Software Package License
\r
3 * ----------------------------------------------------------------------------
\r
4 * Copyright (c) 2014, 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
32 /*----------------------------------------------------------------------------
\r
34 *----------------------------------------------------------------------------*/
\r
37 /*----------------------------------------------------------------------------
\r
39 *----------------------------------------------------------------------------*/
\r
42 * \brief 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 enabled.
\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
50 static void PIO_SetPeripheralA(
\r
53 unsigned char enablePullUp)
\r
55 unsigned int abcdsr;
\r
56 /* Disable interrupts on the pin(s) */
\r
57 pio->PIO_IDR = mask;
\r
59 /* Enable the pull-up(s) if necessary */
\r
61 pio->PIO_PUER = mask;
\r
63 pio->PIO_PUDR = mask;
\r
66 abcdsr = pio->PIO_ABCDSR[0];
\r
67 pio->PIO_ABCDSR[0] &= (~mask & abcdsr);
\r
68 abcdsr = pio->PIO_ABCDSR[1];
\r
69 pio->PIO_ABCDSR[1] &= (~mask & abcdsr);
\r
70 pio->PIO_PDR = mask;
\r
74 * \brief Configures one or more pin(s) of a PIO controller as being controlled
\r
75 * by peripheral B. Optionally, the corresponding internal pull-up(s) can be
\r
78 * \param pio Pointer to a PIO controller.
\r
79 * \param mask Bitmask of one or more pin(s) to configure.
\r
80 * \param enablePullUp Indicates if the pin(s) internal pull-up shall be
\r
83 static void PIO_SetPeripheralB(
\r
86 unsigned char enablePullUp)
\r
88 unsigned int abcdsr;
\r
89 /* Disable interrupts on the pin(s) */
\r
90 pio->PIO_IDR = mask;
\r
92 /* Enable the pull-up(s) if necessary */
\r
94 pio->PIO_PUER = mask;
\r
96 pio->PIO_PUDR = mask;
\r
99 abcdsr = pio->PIO_ABCDSR[0];
\r
100 pio->PIO_ABCDSR[0] = (mask | abcdsr);
\r
101 abcdsr = pio->PIO_ABCDSR[1];
\r
102 pio->PIO_ABCDSR[1] &= (~mask & abcdsr);
\r
104 pio->PIO_PDR = mask;
\r
108 * \brief Configures one or more pin(s) of a PIO controller as being controlled
\r
109 * by peripheral C. Optionally, the corresponding internal pull-up(s) can be
\r
112 * \param pio Pointer to a PIO controller.
\r
113 * \param mask Bitmask of one or more pin(s) to configure.
\r
114 * \param enablePullUp Indicates if the pin(s) internal pull-up shall be
\r
117 static void PIO_SetPeripheralC(
\r
120 unsigned char enablePullUp)
\r
122 unsigned int abcdsr;
\r
123 /* Disable interrupts on the pin(s) */
\r
124 pio->PIO_IDR = mask;
\r
126 /* Enable the pull-up(s) if necessary */
\r
127 if (enablePullUp) {
\r
128 pio->PIO_PUER = mask;
\r
130 pio->PIO_PUDR = mask;
\r
133 abcdsr = pio->PIO_ABCDSR[0];
\r
134 pio->PIO_ABCDSR[0] &= (~mask & abcdsr);
\r
135 abcdsr = pio->PIO_ABCDSR[1];
\r
136 pio->PIO_ABCDSR[1] = (mask | abcdsr);
\r
138 pio->PIO_PDR = mask;
\r
142 * \brief Configures one or more pin(s) of a PIO controller as being controlled
\r
143 * by peripheral D. Optionally, the corresponding internal pull-up(s) can be
\r
146 * \param pio Pointer to a PIO controller.
\r
147 * \param mask Bitmask of one or more pin(s) to configure.
\r
148 * \param enablePullUp Indicates if the pin(s) internal pull-up shall be
\r
151 static void PIO_SetPeripheralD(
\r
154 unsigned char enablePullUp)
\r
156 unsigned int abcdsr;
\r
157 /* Disable interrupts on the pin(s) */
\r
158 pio->PIO_IDR = mask;
\r
160 /* Enable the pull-up(s) if necessary */
\r
161 if (enablePullUp) {
\r
162 pio->PIO_PUER = mask;
\r
164 pio->PIO_PUDR = mask;
\r
167 abcdsr = pio->PIO_ABCDSR[0];
\r
168 pio->PIO_ABCDSR[0] = (mask | abcdsr);
\r
169 abcdsr = pio->PIO_ABCDSR[1];
\r
170 pio->PIO_ABCDSR[1] = (mask | abcdsr);
\r
172 pio->PIO_PDR = mask;
\r
176 * \brief Configures one or more pin(s) or a PIO controller as inputs. Optionally,
\r
177 * the corresponding internal pull-up(s) and glitch filter(s) can be enabled.
\r
179 * \param pio Pointer to a PIO controller.
\r
180 * \param mask Bitmask indicating which pin(s) to configure as input(s).
\r
181 * \param enablePullUp Indicates if the internal pull-up(s) must be enabled.
\r
182 * \param enableFilter Indicates if the glitch filter(s) must be enabled.
\r
184 static void PIO_SetInput(
\r
187 unsigned char attribute)
\r
189 /* Disable interrupts */
\r
190 pio->PIO_IDR = mask;
\r
192 /* Enable pull-up(s) if necessary */
\r
193 if (attribute & PIO_PULLUP)
\r
194 pio->PIO_PUER = mask;
\r
196 pio->PIO_PUDR = mask;
\r
198 /* Enable Input Filter if necessary */
\r
199 if (attribute & (PIO_DEGLITCH | PIO_DEBOUNCE))
\r
200 pio->PIO_IFER = mask;
\r
202 pio->PIO_IFDR = mask;
\r
204 /* Enable de-glitch or de-bounce if necessary */
\r
205 if (attribute & PIO_DEGLITCH) {
\r
206 pio->PIO_IFSCDR = mask;
\r
208 if (attribute & PIO_DEBOUNCE) {
\r
209 pio->PIO_IFSCER = mask;
\r
212 /* Configure pin as input */
\r
213 pio->PIO_ODR = mask;
\r
214 pio->PIO_PER = mask;
\r
218 * \brief Configures one or more pin(s) of a PIO controller as outputs, with the
\r
219 * given default value. Optionally, the multi-drive feature can be enabled
\r
222 * \param pio Pointer to a PIO controller.
\r
223 * \param mask Bitmask indicating which pin(s) to configure.
\r
224 * \param defaultValue Default level on the pin(s).
\r
225 * \param enableMultiDrive Indicates if the pin(s) shall be configured as
\r
227 * \param enablePullUp Indicates if the pin shall have its pull-up activated.
\r
229 static void PIO_SetOutput(
\r
232 unsigned char defaultValue,
\r
233 unsigned char enableMultiDrive,
\r
234 unsigned char enablePullUp)
\r
236 /* Disable interrupts */
\r
237 pio->PIO_IDR = mask;
\r
239 /* Enable pull-up(s) if necessary */
\r
240 if (enablePullUp) {
\r
241 pio->PIO_PUER = mask;
\r
243 pio->PIO_PUDR = mask;
\r
246 /* Enable multi-drive if necessary */
\r
247 if (enableMultiDrive) {
\r
248 pio->PIO_MDER = mask;
\r
250 pio->PIO_MDDR = mask;
\r
253 /* Set default value */
\r
254 if (defaultValue) {
\r
255 pio->PIO_SODR = mask;
\r
257 pio->PIO_CODR = mask;
\r
259 /* Configure pin(s) as output(s) */
\r
260 pio->PIO_OER = mask;
\r
261 pio->PIO_PER = mask;
\r
264 /*----------------------------------------------------------------------------
\r
266 *----------------------------------------------------------------------------*/
\r
269 * \brief Configures a list of Pin instances, each of which can either hold a
\r
270 * single pin or a group of pins, depending on the mask value; all pins are
\r
271 * configured by this function. The size of the array must also be provided and
\r
272 * is easily computed using PIO_LISTSIZE whenever its length is not known in
\r
275 * \param list Pointer to a list of Pin instances.
\r
276 * \param size Size of the Pin list (calculated using PIO_LISTSIZE).
\r
278 * \return 1 if the pins have been configured properly; otherwise 0.
\r
280 uint8_t PIO_Configure( const Pin *list, uint32_t size )
\r
282 /* Configure pins */
\r
283 while ( size > 0 ) {
\r
284 switch ( list->type ) {
\r
286 PIO_SetPeripheralA(list->pio,
\r
288 (list->attribute & PIO_PULLUP) ? 1 : 0);
\r
292 PIO_SetPeripheralB(list->pio,
\r
294 (list->attribute & PIO_PULLUP) ? 1 : 0);
\r
298 PIO_SetPeripheralC(list->pio,
\r
300 (list->attribute & PIO_PULLUP) ? 1 : 0);
\r
304 PIO_SetPeripheralD(list->pio,
\r
306 (list->attribute & PIO_PULLUP) ? 1 : 0);
\r
310 PMC_EnablePeripheral(list->id);
\r
312 PIO_SetInput(list->pio,
\r
319 PIO_SetOutput(list->pio,
\r
321 (list->type == PIO_OUTPUT_1),
\r
322 (list->attribute & PIO_OPENDRAIN) ? 1 : 0,
\r
323 (list->attribute & PIO_PULLUP) ? 1 : 0);
\r
335 * \brief Sets a high output level on all the PIOs defined in the given Pin
\r
337 * This has no immediate effects on PIOs that are not output, but the PIO
\r
338 * controller will memorize the value they are changed to outputs.
\r
340 * \param pin Pointer to a Pin instance describing one or more pins.
\r
342 void PIO_Set(const Pin *pin)
\r
344 pin->pio->PIO_SODR = pin->mask;
\r
348 * \brief Sets a low output level on all the PIOs defined in the given Pin
\r
350 * This has no immediate effects on PIOs that are not output, but the PIO
\r
351 * controller will memorize the value they are changed to outputs.
\r
353 * \param pin Pointer to a Pin instance describing one or more pins.
\r
355 void PIO_Clear(const Pin *pin)
\r
357 pin->pio->PIO_CODR = pin->mask;
\r
361 * \brief Returns 1 if one or more PIO of the given Pin instance currently have
\r
362 * a high level; otherwise returns 0. This method returns the actual value that
\r
363 * is being read on the pin. To return the supposed output value of a pin, use
\r
364 * PIO_GetOutputDataStatus() instead.
\r
366 * \param pin Pointer to a Pin instance describing one or more pins.
\r
368 * \return 1 if the Pin instance contains at least one PIO that currently has
\r
369 * a high level; otherwise 0.
\r
371 unsigned char PIO_Get( const Pin *pin )
\r
375 if ( (pin->type == PIO_OUTPUT_0) || (pin->type == PIO_OUTPUT_1) ) {
\r
376 reg = pin->pio->PIO_ODSR ;
\r
378 reg = pin->pio->PIO_PDSR ;
\r
381 if ( (reg & pin->mask) == 0 ) {
\r
389 * \brief Returns 1 if one or more PIO of the given Pin are configured to output
\r
390 * a high level (even if they are not output).
\r
391 * To get the actual value of the pin, use PIO_Get() instead.
\r
393 * \param pin Pointer to a Pin instance describing one or more pins.
\r
395 * \return 1 if the Pin instance contains at least one PIO that is configured
\r
396 * to output a high level; otherwise 0.
\r
398 unsigned char PIO_GetOutputDataStatus(const Pin *pin)
\r
400 if ((pin->pio->PIO_ODSR & pin->mask) == 0) {
\r
408 * \brief Configures Glitch or Denouncing filter for input.
\r
410 * \param pin Pointer to a Pin instance describing one or more pins.
\r
411 * \param cuttoff Cut off frequency for denounce filter.
\r
413 void PIO_SetDebounceFilter( const Pin *pin, uint32_t cuttoff )
\r
415 Pio *pio = pin->pio;
\r
417 pio->PIO_IFSCER = pin->mask; /* set Denouncing, 0 bit field no effect */
\r
418 pio->PIO_SCDR = ((32678/(2*(cuttoff))) - 1) & 0x3FFF;
\r
419 /* the lowest 14 bits work */
\r
423 * \brief Enable write protect.
\r
425 * \param pin Pointer to a Pin instance describing one or more pins.
\r
427 void PIO_EnableWriteProtect( const Pin *pin )
\r
429 Pio *pio = pin->pio;
\r
431 pio->PIO_WPMR = ( PIO_WPMR_WPKEY_VALID | PIO_WPMR_WPEN_EN );
\r
435 * \brief Disable write protect.
\r
437 * \param pin Pointer to a Pin instance describing one or more pins.
\r
440 void PIO_DisableWriteProtect( const Pin *pin )
\r
442 Pio *pio = pin->pio;
\r
444 pio->PIO_WPMR = ( PIO_WPMR_WPKEY_VALID | PIO_WPMR_WPEN_DIS );
\r
448 * \brief Get write protect violation information.
\r
450 * \param pin Pointer to a Pin instance describing one or more pins.
\r
452 uint32_t PIO_GetWriteProtectViolationInfo( const Pin * pin )
\r
454 Pio *pio = pin->pio;
\r
455 return (pio->PIO_WPSR);
\r
459 * \brief Set pin type
\r
460 * the pin is controlled by the corresponding peripheral (A, B, C, D,E)
\r
461 * \param pin Pointer to a Pin instance describing one or more pins.
\r
462 * \param pinType PIO_PERIPH_A, PIO_PERIPH_B, ...
\r
464 void PIO_SetPinType( Pin * pin, uint8_t pinType)
\r
466 pin->type = pinType;
\r