Library to access LPC17xx peripherals. It uses static inline functions, constant propagation and dead code elimination to be as fast as possible.

Dependents:   Chua-VGA Wolfram-1D-VGA WolframRnd-1D-VGA Basin-VGA ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers gpio.h Source File

gpio.h

00001 /* Copyright (C) 2010, 2011 by Ivo van Poorten <ivop@euronet.nl>
00002  * This file is licensed under the terms of the GNU Lesser
00003  * General Public License, version 3.
00004  */
00005 
00006 #ifndef FASTLIB_GPIO_H
00007 #define FASTLIB_GPIO_H
00008 
00009 #include "fastlib/common.h"
00010 
00011 static inline volatile uint32_t *fl_gpio_get_base(const int port) {
00012     switch (port) {
00013     case 0:  return (volatile uint32_t *) 0x2009c000;
00014     case 1:  return (volatile uint32_t *) 0x2009c020;
00015     case 2:  return (volatile uint32_t *) 0x2009c040;
00016     case 3:  return (volatile uint32_t *) 0x2009c060;
00017     case 4:  return (volatile uint32_t *) 0x2009c080;
00018     default: return (volatile uint32_t *) 0;
00019     }
00020 }
00021 
00022 #define FL_GPIO_OVERALL_INT_STATUS      ((volatile uint32_t *) 0x40028080)
00023 
00024 #define FL_GPIO_PORT0_RISE_INT_STATUS   ((volatile uint32_t *) 0x40028084)
00025 #define FL_GPIO_PORT0_FALL_INT_STATUS   ((volatile uint32_t *) 0x40028088)
00026 #define FL_GPIO_PORT0_INT_CLEAR         ((volatile uint32_t *) 0x4002808C)
00027 #define FL_GPIO_PORT0_RISE_INT          ((volatile uint32_t *) 0x40028090)
00028 #define FL_GPIO_PORT0_FALL_INT          ((volatile uint32_t *) 0x40028094)
00029 
00030 #define FL_GPIO_PORT2_RISE_INT_STATUS   ((volatile uint32_t *) 0x400280A4)
00031 #define FL_GPIO_PORT2_FALL_INT_STATUS   ((volatile uint32_t *) 0x400280A8)
00032 #define FL_GPIO_PORT2_INT_CLEAR         ((volatile uint32_t *) 0x400280AC)
00033 #define FL_GPIO_PORT2_RISE_INT          ((volatile uint32_t *) 0x400280B0)
00034 #define FL_GPIO_PORT2_FALL_INT          ((volatile uint32_t *) 0x400280B4)
00035 
00036 /* dir: 0=input, 1=output */
00037 static inline void fl_gpio_set_direction(const unsigned port, const unsigned int bitmask, const unsigned dir) {
00038     if (dir) *(fl_gpio_get_base(port)) |=  bitmask;
00039     else     *(fl_gpio_get_base(port)) &= ~bitmask;
00040 }
00041 
00042 static inline void fl_gpio_set_value(const unsigned port, const unsigned bitmask) {
00043     *(fl_gpio_get_base(port) + 6) = bitmask;
00044 }
00045 
00046 static inline void fl_gpio_clear_value(const unsigned port, const unsigned bitmask) {
00047     *(fl_gpio_get_base(port) + 7) = bitmask;
00048 }
00049 
00050 static inline uint32_t fl_gpio_get_value(const unsigned port, const unsigned bitmask) {
00051     return *(fl_gpio_get_base(port) + 5);
00052 }
00053 
00054 static inline void fl_gpio_set_mask(const unsigned port, const unsigned bitmask) {
00055     *(fl_gpio_get_base(port) + 4) |= bitmask;
00056 }
00057 
00058 static inline void fl_gpio_clear_mask(const unsigned port, const unsigned bitmask) {
00059     *(fl_gpio_get_base(port) + 4) &= ~bitmask;
00060 }
00061 
00062 /* interrupts only on port 0 or 2 */
00063 
00064 /* return: 0 = none, !0 = at least one */
00065 static inline int fl_gpio_port_interrupt_status(const unsigned port) {
00066     return *FL_GPIO_OVERALL_INT_STATUS & (1U<<port);
00067 }
00068 
00069 static inline void fl_gpio_interrupt_enable(const unsigned port, const unsigned bitmask, const unsigned edge) {
00070     if (port == 0) {
00071         if (edge == FL_RISE) *FL_GPIO_PORT0_RISE_INT |= bitmask;
00072         else                 *FL_GPIO_PORT0_FALL_INT |= bitmask;
00073     } else {
00074         if (edge == FL_RISE) *FL_GPIO_PORT2_RISE_INT |= bitmask;
00075         else                 *FL_GPIO_PORT2_FALL_INT |= bitmask;
00076     }
00077 }
00078 
00079 static inline void fl_gpio_interrupt_disable(const unsigned port, const unsigned bitmask, const unsigned edge) {
00080     if (port == 0) {
00081         if (edge == FL_RISE) *FL_GPIO_PORT0_RISE_INT &= ~bitmask;
00082         else                 *FL_GPIO_PORT0_FALL_INT &= ~bitmask;
00083     } else {
00084         if (edge == FL_RISE) *FL_GPIO_PORT2_RISE_INT &= ~bitmask;
00085         else                 *FL_GPIO_PORT2_FALL_INT &= ~bitmask;
00086     }
00087 }
00088 
00089 static inline uint32_t fl_gpio_interrupt_status(const unsigned port, const unsigned bitmask, const unsigned edge) {
00090     if (port == 0) {
00091         if (edge == FL_RISE) return *FL_GPIO_PORT0_RISE_INT_STATUS & bitmask;
00092         else                 return *FL_GPIO_PORT0_FALL_INT_STATUS & bitmask;
00093     } else {
00094         if (edge == FL_RISE) return *FL_GPIO_PORT2_RISE_INT_STATUS & bitmask;
00095         else                 return *FL_GPIO_PORT2_FALL_INT_STATUS & bitmask;
00096     }
00097 }
00098 
00099 static inline void fl_gpio_interrupt_clear(const unsigned port, const unsigned bitmask) {
00100     if (port == 0) *FL_GPIO_PORT0_INT_CLEAR = bitmask;
00101     else           *FL_GPIO_PORT2_INT_CLEAR = bitmask;
00102 }
00103 
00104 #endif