hdo  beta
/home/mrazik/Documents/web/old/hdo/at91lib/peripherals/pio/pio.c
00001 /* ----------------------------------------------------------------------------
00002  *         ATMEL Microcontroller Software Support 
00003  * ----------------------------------------------------------------------------
00004  * Copyright (c) 2008, Atmel Corporation
00005  *
00006  * All rights reserved.
00007  *
00008  * Redistribution and use in source and binary forms, with or without
00009  * modification, are permitted provided that the following conditions are met:
00010  *
00011  * - Redistributions of source code must retain the above copyright notice,
00012  * this list of conditions and the disclaimer below.
00013  *
00014  * Atmel's name may not be used to endorse or promote products derived from
00015  * this software without specific prior written permission.
00016  *
00017  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
00018  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
00019  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
00020  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
00021  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00022  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
00023  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
00024  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
00025  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
00026  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027  * ----------------------------------------------------------------------------
00028  */
00029 
00030 //------------------------------------------------------------------------------
00031 //         Headers
00032 //------------------------------------------------------------------------------
00033 
00034 #include "pio.h"
00035 #include <board.h>
00036 
00037 //------------------------------------------------------------------------------
00038 //         Local Functions
00039 //------------------------------------------------------------------------------
00040 
00041 //------------------------------------------------------------------------------
00049 //------------------------------------------------------------------------------
00050 static void PIO_SetPeripheralA(
00051     AT91S_PIO *pio,
00052     unsigned int mask,
00053     unsigned char enablePullUp)
00054 {
00055     // Disable interrupts on the pin(s)
00056     pio->PIO_IDR = mask;
00057 
00058     // Enable the pull-up(s) if necessary
00059     if (enablePullUp) {
00060 
00061         pio->PIO_PPUER = mask;
00062     }
00063     else {
00064 
00065         pio->PIO_PPUDR = mask;
00066     }
00067 
00068     // Configure pin
00069     pio->PIO_ASR = mask;
00070     pio->PIO_PDR = mask;
00071 }
00072 
00073 //------------------------------------------------------------------------------
00081 //------------------------------------------------------------------------------
00082 static void PIO_SetPeripheralB(
00083     AT91S_PIO *pio,
00084     unsigned int mask,
00085     unsigned char enablePullUp)
00086 {
00087     // Disable interrupts on the pin(s)
00088     pio->PIO_IDR = mask;
00089 
00090     // Enable the pull-up(s) if necessary
00091     if (enablePullUp) {
00092 
00093         pio->PIO_PPUER = mask;
00094     }
00095     else {
00096 
00097         pio->PIO_PPUDR = mask;
00098     }
00099 
00100     // Configure pin
00101     pio->PIO_BSR = mask;
00102     pio->PIO_PDR = mask;
00103 }
00104 
00105 //------------------------------------------------------------------------------
00113 //------------------------------------------------------------------------------
00114 static void PIO_SetInput(
00115     AT91S_PIO *pio,
00116     unsigned int mask,
00117     unsigned char enablePullUp,
00118     unsigned char enableFilter)
00119 {
00120     // Disable interrupts
00121     pio->PIO_IDR = mask;
00122 
00123     // Enable pull-up(s) if necessary
00124     if (enablePullUp) {
00125     
00126         pio->PIO_PPUER = mask;
00127     }
00128     else {
00129     
00130         pio->PIO_PPUDR = mask;
00131     }
00132 
00133     // Enable filter(s) if necessary
00134     if (enableFilter) {
00135     
00136         pio->PIO_IFER = mask;
00137     }
00138     else {
00139     
00140         pio->PIO_IFDR = mask;
00141     }
00142 
00143     // Configure pin as input
00144     pio->PIO_ODR = mask;
00145     pio->PIO_PER = mask;
00146 }
00147 
00148 //------------------------------------------------------------------------------
00158 //------------------------------------------------------------------------------
00159 static void PIO_SetOutput(
00160     AT91S_PIO *pio,
00161     unsigned int mask,
00162     unsigned char defaultValue,
00163     unsigned char enableMultiDrive,
00164     unsigned char enablePullUp)
00165 {
00166     // Disable interrupts
00167     pio->PIO_IDR = mask;
00168 
00169     // Enable pull-up(s) if necessary
00170     if (enablePullUp) {
00171     
00172         pio->PIO_PPUER = mask;
00173     }
00174     else {
00175     
00176         pio->PIO_PPUDR = mask;
00177     }
00178 
00179     // Enable multi-drive if necessary
00180     if (enableMultiDrive) {
00181     
00182         pio->PIO_MDER = mask;
00183     }
00184     else {
00185     
00186         pio->PIO_MDDR = mask;
00187     }
00188 
00189     // Set default value
00190     if (defaultValue) {
00191 
00192         pio->PIO_SODR = mask;
00193     }
00194     else {
00195 
00196         pio->PIO_CODR = mask;
00197     }
00198 
00199     // Configure pin(s) as output(s)
00200     pio->PIO_OER = mask;
00201     pio->PIO_PER = mask;
00202 }
00203 
00204 //------------------------------------------------------------------------------
00205 //         Global Functions
00206 //------------------------------------------------------------------------------
00207 
00208 //------------------------------------------------------------------------------
00216 //------------------------------------------------------------------------------
00217 unsigned char PIO_Configure(const Pin *list, unsigned int size)
00218 {
00219     // Configure pins
00220     while (size > 0) {
00221     
00222         switch (list->type) {
00223     
00224             case PIO_PERIPH_A:
00225                 PIO_SetPeripheralA(list->pio,
00226                                    list->mask,
00227                                    (list->attribute & PIO_PULLUP) ? 1 : 0);
00228                 break;
00229     
00230             case PIO_PERIPH_B:
00231                 PIO_SetPeripheralB(list->pio,
00232                                    list->mask,
00233                                    (list->attribute & PIO_PULLUP) ? 1 : 0);
00234                 break;
00235     
00236             case PIO_INPUT:
00237                 AT91C_BASE_PMC->PMC_PCER = 1 << list->id;
00238                 PIO_SetInput(list->pio,
00239                              list->mask,
00240                              (list->attribute & PIO_PULLUP) ? 1 : 0,
00241                              (list->attribute & PIO_DEGLITCH)? 1 : 0);
00242                 break;
00243     
00244             case PIO_OUTPUT_0:
00245             case PIO_OUTPUT_1:
00246                 PIO_SetOutput(list->pio,
00247                               list->mask,
00248                               (list->type == PIO_OUTPUT_1),
00249                               (list->attribute & PIO_OPENDRAIN) ? 1 : 0,
00250                               (list->attribute & PIO_PULLUP) ? 1 : 0);
00251                 break;
00252     
00253             default: return 0;
00254         }
00255 
00256         list++;
00257         size--;
00258     }
00259 
00260     return 1;
00261 }
00262 
00263 //------------------------------------------------------------------------------
00268 //------------------------------------------------------------------------------
00269 void PIO_Set(const Pin *pin)
00270 {
00271     pin->pio->PIO_SODR = pin->mask;
00272 }
00273 
00274 //------------------------------------------------------------------------------
00279 //------------------------------------------------------------------------------
00280 void PIO_Clear(const Pin *pin)
00281 {
00282     pin->pio->PIO_CODR = pin->mask;
00283 }
00284 
00285 //------------------------------------------------------------------------------
00293 //------------------------------------------------------------------------------
00294 unsigned char PIO_Get(const Pin *pin)
00295 {
00296     unsigned int reg;
00297     if ((pin->type == PIO_OUTPUT_0) || (pin->type == PIO_OUTPUT_1)) {
00298 
00299         reg = pin->pio->PIO_ODSR;
00300     }
00301     else {
00302 
00303         reg = pin->pio->PIO_PDSR;
00304     }
00305 
00306     if ((reg & pin->mask) == 0) {
00307 
00308         return 0;
00309     }
00310     else {
00311 
00312         return 1;
00313     }
00314 }
00315 
00316 
00317 //------------------------------------------------------------------------------
00324 //------------------------------------------------------------------------------
00325 unsigned char PIO_GetOutputDataStatus(const Pin *pin)
00326 {
00327     if ((pin->pio->PIO_ODSR & pin->mask) == 0) {
00328 
00329         return 0;
00330     }
00331     else {
00332 
00333         return 1;
00334     }
00335 }
00336 
00337 //------------------------------------------------------------------------------
00341 //------------------------------------------------------------------------------
00342 unsigned int PIO_GetISR(const Pin *pin)
00343 {
00344     return (pin->pio->PIO_ISR);
00345 }
00346