Nut/OS  4.10.3
API Reference
porttran.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008 by egnite GmbH
00003  *
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the copyright holders nor the names of
00016  *    contributors may be used to endorse or promote products derived
00017  *    from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  *
00034  */
00035 
00036 /*
00037  * $Id: porttran.h 2560 2009-03-18 04:00:06Z thiagocorrea $
00038  */
00039 
00040 #include <cfg/arch.h>
00041 
00046 
00117 /*
00118  * Remove any previously defined register names.
00119  */
00120 #undef  GPIO_PE_REG
00121 #undef  GPIO_PD_REG
00122 #undef  GPIO_PS_REG
00123 
00124 #undef  GPIO_OE_REG
00125 #undef  GPIO_OD_REG
00126 #undef  GPIO_OS_REG
00127 
00128 #undef  GPIO_SOD_REG
00129 #undef  GPIO_COD_REG
00130 #undef  GPIO_ODS_REG
00131 
00132 #undef  GPIO_PDS_REG
00133 
00134 #undef  GPIO_PUE_REG
00135 #undef  GPIO_PUD_REG
00136 #undef  GPIO_PUS_REG
00137 
00138 #undef  GPIO_MDE_REG
00139 #undef  GPIO_MDD_REG
00140 #undef  GPIO_MDS_REG
00141 
00142 #undef  GPIO_IFE_REG
00143 #undef  GPIO_IFD_REG
00144 #undef  GPIO_IFS_REG
00145 
00146 #if defined(__AVR__)
00147 /*
00148  * Determine AVR port names.
00149  */
00150 #include <cfg/arch/avr.h>
00151 
00152 #if GPIO_ID == PIOB_ID
00153 #define GPIO_PDS_REG    PINB
00154 #define GPIO_SOD_REG    PORTB
00155 #define GPIO_OE_REG     DDRB
00156 #define GPIO_PUE_REG    PORTB
00157 
00158 #elif  GPIO_ID == PIOC_ID
00159 #define GPIO_PDS_REG    PINC
00160 #define GPIO_SOD_REG    PORTC
00161 #define GPIO_OE_REG     DDRC
00162 #define GPIO_PUE_REG    PORTC
00163 
00164 #elif  GPIO_ID == PIOD_ID
00165 #define GPIO_PDS_REG    PIND
00166 #define GPIO_SOD_REG    PORTD
00167 #define GPIO_OE_REG     DDRD
00168 #define GPIO_PUE_REG    PORTD
00169 
00170 #elif  GPIO_ID == PIOE_ID
00171 #define GPIO_PDS_REG    PINE
00172 #define GPIO_SOD_REG    PORTE
00173 #define GPIO_OE_REG     DDRE
00174 #define GPIO_PUE_REG    PORTE
00175 
00176 #elif  GPIO_ID == PIOF_ID
00177 #define GPIO_PDS_REG    PINF
00178 #define GPIO_SOD_REG    PORTF
00179 #define GPIO_OE_REG     DDRF
00180 #define GPIO_PUE_REG    PORTF
00181 
00182 #elif  GPIO_ID == PIOG_ID
00183 #define GPIO_PDS_REG    PING
00184 #define GPIO_SOD_REG    PORTG
00185 #define GPIO_OE_REG     DDRG
00186 #define GPIO_PUE_REG    PORTG
00187 
00188 #elif  GPIO_ID == PIOH_ID
00189 #define GPIO_PDS_REG    PINH
00190 #define GPIO_SOD_REG    PORTH
00191 #define GPIO_OE_REG     DDRH
00192 #define GPIO_PUE_REG    PORTH
00193 
00194 #elif  GPIO_ID == PIOI_ID
00195 #define GPIO_PDS_REG    PINI
00196 #define GPIO_SOD_REG    PORTI
00197 #define GPIO_OE_REG     DDRI
00198 #define GPIO_PUE_REG    PORTI
00199 
00200 #elif  GPIO_ID == PIOJ_ID
00201 #define GPIO_PDS_REG    PINJ
00202 #define GPIO_SOD_REG    PORTJ
00203 #define GPIO_OE_REG     DDRJ
00204 #define GPIO_PUE_REG    PORTJ
00205 
00206 #elif  GPIO_ID == PIOK_ID
00207 #define GPIO_PDS_REG    PINK
00208 #define GPIO_SOD_REG    PORTK
00209 #define GPIO_OE_REG     DDRK
00210 #define GPIO_PUE_REG    PORTK
00211 
00212 #elif  GPIO_ID == PIOL_ID
00213 #define GPIO_PDS_REG    PINL
00214 #define GPIO_SOD_REG    PORTL
00215 #define GPIO_OE_REG     DDRL
00216 #define GPIO_PUE_REG    PORTL
00217 
00218 #else
00219 #define GPIO_PDS_REG    PINA
00220 #define GPIO_SOD_REG    PORTA
00221 #define GPIO_OE_REG     DDRA
00222 #define GPIO_PUE_REG    PORTA
00223 #endif
00224 
00225 #elif defined(MCU_AT91)
00226 /*
00227  * Determine AT91 port names.
00228  */
00229 #include <arch/arm/at91.h>
00230 
00231 #if GPIO_ID == PIOA_ID
00232 #define  GPIO_PE_REG    PIOA_PER
00233 #define  GPIO_PD_REG    PIOA_PDR
00234 #define  GPIO_PS_REG    PIOA_PSR
00235 #define  GPIO_OE_REG    PIOA_OER
00236 #define  GPIO_OD_REG    PIOA_ODR
00237 #define  GPIO_OS_REG    PIOA_OSR
00238 #define  GPIO_SOD_REG   PIOA_SODR
00239 #define  GPIO_COD_REG   PIOA_CODR
00240 #define  GPIO_ODS_REG   PIOA_ODSR
00241 #define  GPIO_PDS_REG   PIOA_PDSR
00242 #if defined(PIOA_PUER)
00243 #define  GPIO_PUE_REG   PIOA_PUER
00244 #if defined(PIOA_PUDR)
00245 #define  GPIO_PUD_REG   PIOA_PUDR
00246 #define  GPIO_PUS_REG   PIOA_PUSR
00247 #endif /* PIOA_PUDR */
00248 #endif /* PIOA_PUER */
00249 #if defined(PIOA_MDER)
00250 #define  GPIO_MDE_REG   PIOA_MDER
00251 #if defined(PIOA_MDDR)
00252 #define  GPIO_MDD_REG   PIOA_MDDR
00253 #define  GPIO_MDS_REG   PIOA_MDSR
00254 #endif /* PIOA_MDDR */
00255 #endif /* PIOA_MDER */
00256 #if defined(PIOA_IFER)
00257 #define  GPIO_IFE_REG   PIOA_IFER
00258 #if defined(PIOA_IFDR)
00259 #define  GPIO_IFD_REG   PIOA_IFDR
00260 #define  GPIO_IFS_REG   PIOA_IFSR
00261 #endif /* PIOA_IFDR */
00262 #endif /* PIOA_IFER */
00263 
00264 #elif GPIO_ID == PIOB_ID
00265 #define  GPIO_PE_REG    PIOB_PER
00266 #define  GPIO_PD_REG    PIOB_PDR
00267 #define  GPIO_PS_REG    PIOB_PSR
00268 #define  GPIO_OE_REG    PIOB_OER
00269 #define  GPIO_OD_REG    PIOB_ODR
00270 #define  GPIO_OS_REG    PIOB_OSR
00271 #define  GPIO_SOD_REG   PIOB_SODR
00272 #define  GPIO_COD_REG   PIOB_CODR
00273 #define  GPIO_ODS_REG   PIOB_ODSR
00274 #define  GPIO_PDS_REG   PIOB_PDSR
00275 #if defined(PIOB_PUER)
00276 #define  GPIO_PUE_REG   PIOB_PUER
00277 #if defined(PIOB_PUDR)
00278 #define  GPIO_PUD_REG   PIOB_PUDR
00279 #define  GPIO_PUS_REG   PIOB_PUSR
00280 #endif /* PIOB_PUDR */
00281 #endif /* PIOB_PUER */
00282 #if defined(PIOB_MDER)
00283 #define  GPIO_MDE_REG   PIOB_MDER
00284 #if defined(PIOB_MDDR)
00285 #define  GPIO_MDD_REG   PIOB_MDDR
00286 #define  GPIO_MDS_REG   PIOB_MDSR
00287 #endif /* PIOB_MDDR */
00288 #endif /* PIOB_MDER */
00289 #if defined(PIOB_IFER)
00290 #define  GPIO_IFE_REG   PIOB_IFER
00291 #if defined(PIOB_IFDR)
00292 #define  GPIO_IFD_REG   PIOB_IFDR
00293 #define  GPIO_IFS_REG   PIOB_IFSR
00294 #endif /* PIOB_IFDR */
00295 #endif /* PIOB_IFER */
00296 
00297 #elif GPIO_ID == PIOC_ID
00298 #define  GPIO_PE_REG    PIOC_PER
00299 #define  GPIO_PD_REG    PIOC_PDR
00300 #define  GPIO_PS_REG    PIOC_PSR
00301 #define  GPIO_OE_REG    PIOC_OER
00302 #define  GPIO_OD_REG    PIOC_ODR
00303 #define  GPIO_OS_REG    PIOC_OSR
00304 #define  GPIO_SOD_REG   PIOC_SODR
00305 #define  GPIO_COD_REG   PIOC_CODR
00306 #define  GPIO_ODS_REG   PIOC_ODSR
00307 #define  GPIO_PDS_REG   PIOC_PDSR
00308 #if defined(PIOC_PUER)
00309 #define  GPIO_PUE_REG   PIOC_PUER
00310 #if defined(PIOC_PUDR)
00311 #define  GPIO_PUD_REG   PIOC_PUDR
00312 #define  GPIO_PUS_REG   PIOC_PUSR
00313 #endif /* PIOC_PUDR */
00314 #endif /* PIOC_PUER */
00315 #if defined(PIOC_MDER)
00316 #define  GPIO_MDE_REG   PIOC_MDER
00317 #if defined(PIOC_MDDR)
00318 #define  GPIO_MDD_REG   PIOC_MDDR
00319 #define  GPIO_MDS_REG   PIOC_MDSR
00320 #endif /* PIOC_MDDR */
00321 #endif /* PIOC_MDER */
00322 #if defined(PIOC_IFER)
00323 #define  GPIO_IFE_REG   PIOC_IFER
00324 #if defined(PIOC_IFDR)
00325 #define  GPIO_IFD_REG   PIOC_IFDR
00326 #define  GPIO_IFS_REG   PIOC_IFSR
00327 #endif /* PIOC_IFDR */
00328 #endif /* PIOC_IFER */
00329 
00330 #else /* GPIO_ID */
00331 #define  GPIO_PE_REG    PIO_PER
00332 #define  GPIO_PD_REG    PIO_PDR
00333 #define  GPIO_PS_REG    PIO_PSR
00334 #define  GPIO_OE_REG    PIO_OER
00335 #define  GPIO_OD_REG    PIO_ODR
00336 #define  GPIO_OS_REG    PIO_OSR
00337 #define  GPIO_SOD_REG   PIO_SODR
00338 #define  GPIO_COD_REG   PIO_CODR
00339 #define  GPIO_ODS_REG   PIO_ODSR
00340 #define  GPIO_PDS_REG   PIO_PDSR
00341 #if defined(PIO_PUER)
00342 #define  GPIO_PUE_REG   PIO_PUER
00343 #if defined(PIO_PUDR)
00344 #define  GPIO_PUD_REG   PIO_PUDR
00345 #define  GPIO_PUS_REG   PIO_PUSR
00346 #endif /* PIO_PUDR */
00347 #endif /* PIO_PUER */
00348 #if defined(PIO_MDER)
00349 #define  GPIO_MDE_REG   PIO_MDER
00350 #if defined(PIO_MDDR)
00351 #define  GPIO_MDD_REG   PIO_MDDR
00352 #define  GPIO_MDS_REG   PIO_MDSR
00353 #endif /* PIO_MDDR */
00354 #endif /* PIO_MDER */
00355 #if defined(PIO_IFER)
00356 #define  GPIO_IFE_REG   PIO_IFER
00357 #if defined(PIO_IFDR)
00358 #define  GPIO_IFD_REG   PIO_IFDR
00359 #define  GPIO_IFS_REG   PIO_IFSR
00360 #endif /* PIO_IFDR */
00361 #endif /* PIO_IFER */
00362 
00363 #endif /* GPIO_ID */
00364 
00365 #elif defined(__AVR32__)
00366 
00367 #include <avr32/io.h>
00368 
00369 #define  GPIO_PE_REG    &AVR32_GPIO.port[GPIO_ID].gpers
00370 #define  GPIO_PD_REG    &AVR32_GPIO.port[GPIO_ID].pderc
00371 #define  GPIO_PS_REG    &AVR32_GPIO.port[GPIO_ID].pder
00372 #define  GPIO_OE_REG    &AVR32_GPIO.port[GPIO_ID].oders
00373 #define  GPIO_OD_REG    &AVR32_GPIO.port[GPIO_ID].oderc
00374 #define  GPIO_OS_REG    &AVR32_GPIO.port[GPIO_ID].oder
00375 #define  GPIO_SOD_REG   &AVR32_GPIO.port[GPIO_ID].ovrs
00376 #define  GPIO_COD_REG   &AVR32_GPIO.port[GPIO_ID].ovrc
00377 #define  GPIO_ODS_REG   &AVR32_GPIO.port[GPIO_ID].ovr
00378 #define  GPIO_PDS_REG   &AVR32_GPIO.port[GPIO_ID].pvr
00379 #define  GPIO_PUE_REG   &AVR32_GPIO.port[GPIO_ID].puers
00380 #define  GPIO_PUD_REG   &AVR32_GPIO.port[GPIO_ID].puerc
00381 #define  GPIO_PUS_REG   &AVR32_GPIO.port[GPIO_ID].puer
00382 #define  GPIO_MDE_REG   &AVR32_GPIO.port[GPIO_ID].odmerc
00383 #define  GPIO_MDD_REG   &AVR32_GPIO.port[GPIO_ID].odmers
00384 #define  GPIO_MDS_REG   &AVR32_GPIO.port[GPIO_ID].odmer
00385 #define  GPIO_IFE_REG   &AVR32_GPIO.port[GPIO_ID].gfers
00386 #define  GPIO_IFD_REG   &AVR32_GPIO.port[GPIO_ID].gferc
00387 #define  GPIO_IFS_REG   &AVR32_GPIO.port[GPIO_ID].gfer
00388 
00389 /* Additional targets can be added here. */
00390 
00391 #endif /* MCU */
00392 
00393 
00394 /*
00395  * Remove any previously defined port macros.
00396  */
00397 #undef GPIO_SET_LO
00398 #undef GPIO_SET_HI
00399 #undef GPIO_IS_HI
00400 #undef GPIO_GET
00401 #undef GPIO_ENABLE
00402 #undef GPIO_OUTPUT
00403 #undef GPIO_INPUT
00404 #undef GPIO_PULLUP_ON
00405 #undef GPIO_PULLUP_OFF
00406 #undef GPIO_OPENDRAIN
00407 #undef GPIO_PUSHPULL
00408 
00409 #if defined(GPIO_COD_REG)
00410 #define GPIO_SET_LO(b)      outr(GPIO_COD_REG, _BV(b))
00411 #define GPIO_SET_HI(b)      outr(GPIO_SOD_REG, _BV(b))
00412 #elif defined(GPIO_SOD_REG)
00413 #define GPIO_SET_LO(b)      cbi(GPIO_SOD_REG, b)
00414 #define GPIO_SET_HI(b)      sbi(GPIO_SOD_REG, b)
00415 #else
00416 #define GPIO_SET_LO(b)
00417 #define GPIO_SET_HI(b)
00418 #endif
00419 
00420 #if defined(GPIO_ODS_REG)
00421 #define GPIO_IS_HI(b)       ((inr(GPIO_ODS_REG) & _BV(b)) == _BV(b))
00422 #elif defined(GPIO_SOD_REG)
00423 #define GPIO_IS_HI(b)       ((inr(GPIO_SOD_REG) & _BV(b)) == _BV(b))
00424 #else
00425 #define GPIO_IS_HI(b)
00426 #endif
00427 
00428 #if defined(GPIO_PDS_REG)
00429 #define GPIO_GET(b)         ((inr(GPIO_PDS_REG) & _BV(b)) == _BV(b))
00430 #else
00431 #define GPIO_GET(b)
00432 #endif
00433 
00434 /*
00435 ** PIO enable and disable macros.
00436 */
00437 #if defined(GPIO_PE_REG) && defined(GPIO_PD_REG)
00438 /* Direct write, if PIO enable and disable registers are available. */
00439 #define GPIO_ENABLE(b)      outr(GPIO_PE_REG, _BV(b))
00440 #define GPIO_DISABLE(b)     outr(GPIO_PD_REG, _BV(b))
00441 #elif defined(GPIO_PE_REG)
00442 /* Modify bit, if PIO enable register only. */
00443 #define GPIO_ENABLE(b)      sbi(GPIO_PE_REG, _BV(b))
00444 #define GPIO_DISABLE(b)     cbi(GPIO_PE_REG, _BV(b))
00445 #elif defined(GPIO_PD_REG)
00446 /* Modify bit, if PIO disable register only. */
00447 #define GPIO_ENABLE(b)      cbi(GPIO_PD_REG, _BV(b))
00448 #define GPIO_DISABLE(b)     sbi(GPIO_PD_REG, _BV(b))
00449 #else
00450 /* None, PIO may be always enabled or not available. */
00451 #define GPIO_ENABLE(b)
00452 #endif
00453 
00454 #if defined(GPIO_OD_REG)
00455 #define GPIO_OUTPUT(b)      outr(GPIO_OE_REG, _BV(b))
00456 #elif defined(GPIO_OE_REG)
00457 #define GPIO_OUTPUT(b)      sbi(GPIO_OE_REG, b)
00458 #else
00459 #define GPIO_OUTPUT(b)
00460 #endif
00461 
00462 #if defined(GPIO_OD_REG)
00463 #define GPIO_INPUT(b)       outr(GPIO_OD_REG, _BV(b))
00464 #elif defined(GPIO_OE_REG)
00465 #define GPIO_INPUT(b)       cbi(GPIO_OE_REG, b)
00466 #else
00467 #define GPIO_INPUT(b)
00468 #endif
00469 
00470 #if defined(GPIO_PUD_REG)
00471 #define GPIO_PULLUP_ON(b)   outr(GPIO_PUE_REG, _BV(b))
00472 #elif defined(GPIO_PUE_REG)
00473 #define GPIO_PULLUP_ON(b)   sbi(GPIO_PUE_REG, b)
00474 #else
00475 #define GPIO_PULLUP_ON(b)
00476 #endif
00477 
00478 #if defined(GPIO_PUD_REG)
00479 #define GPIO_PULLUP_OFF(b)  outr(GPIO_PUD_REG, _BV(b))
00480 #elif defined(GPIO_PUE_REG)
00481 #define GPIO_PULLUP_OFF(b)  cbi(GPIO_PUE_REG, b)
00482 #else
00483 #define GPIO_PULLUP_OFF(b)
00484 #endif
00485 
00486 #if defined(GPIO_IFD_REG)
00487 #define GPIO_FILTER_ON(b)   outr(GPIO_IFE_REG, _BV(b))
00488 #elif defined(GPIO_IFE_REG)
00489 #define GPIO_FILTER_ON(b)   sbi(GPIO_IFE_REG, b)
00490 #else
00491 #define GPIO_FILTER_ON(b)
00492 #endif
00493 
00494 #if defined(GPIO_IFD_REG)
00495 #define GPIO_FILTER_OFF(b)  outr(GPIO_IFD_REG, _BV(b))
00496 #elif defined(GPIO_IFE_REG)
00497 #define GPIO_FILTER_OFF(b)  cbi(GPIO_IFE_REG, b)
00498 #else
00499 #define GPIO_FILTER_OFF(b)
00500 #endif
00501 
00502 #if defined(GPIO_MDD_REG)
00503 #define GPIO_OPENDRAIN(b)   outr(GPIO_MDE_REG, _BV(b))
00504 #elif defined(GPIO_MDE_REG)
00505 #define GPIO_OPENDRAIN(b)   sbi(GPIO_MDE_REG, b)
00506 #else
00507 #define GPIO_OPENDRAIN(b)
00508 #endif
00509 
00510 #if defined(GPIO_MDD_REG)
00511 #define GPIO_PUSHPULL(b)  outr(GPIO_MDD_REG, _BV(b))
00512 #elif defined(GPIO_MDE_REG)
00513 #define GPIO_PUSHPULL(b)  cbi(GPIO_MDE_REG, b)
00514 #else
00515 #define GPIO_PUSHPULL(b)
00516 #endif
00517