00001
00041 #ifndef GPIO_PORTMUX_GPIO_H_INCLUDED
00042 #define GPIO_PORTMUX_GPIO_H_INCLUDED
00043
00044 #include <gpio/portmux_gpio_regs.h>
00045
00053 typedef uint32_t pin_mask_t;
00054
00060 typedef unsigned int gpio_pin_t;
00061
00073 #define PORTMUX_GPIO_VER(major, minor) (((major) << 8) | (minor))
00074
00075
00076
00077
00078
00079
00080 enum portmux_function {
00081 PORTMUX_FUNC_A,
00082 PORTMUX_FUNC_B,
00083 PORTMUX_FUNC_C,
00084 PORTMUX_FUNC_D,
00085 };
00086
00087 #define PORTMUX_DIR_INPUT (0 << 0)
00088 #define PORTMUX_DIR_OUTPUT (1 << 0)
00089 #define PORTMUX_INIT_LOW (0 << 1)
00090 #define PORTMUX_INIT_HIGH (1 << 1)
00091 #define PORTMUX_PULL_UP (1 << 2)
00092 #define PORTMUX_PULL_DOWN (2 << 2)
00093 #define PORTMUX_BUSKEEPER (3 << 2)
00094 #define PORTMUX_DRIVE_MIN (0 << 4)
00095 #define PORTMUX_DRIVE_LOW (1 << 4)
00096 #define PORTMUX_DRIVE_HIGH (2 << 4)
00097 #define PORTMUX_DRIVE_MAX (3 << 4)
00098 #define PORTMUX_OPEN_DRAIN (1 << 6)
00099
00100 extern void portmux_select_peripheral(void *port, pin_mask_t pin_mask,
00101 enum portmux_function func, unsigned long flags);
00102 extern void portmux_select_gpio(void *port, pin_mask_t pin_mask,
00103 unsigned long flags);
00104
00105
00106 static inline void *gpio_pin_to_port(gpio_pin_t pin)
00107 {
00108 return (void *)(GPIO_BASE + (pin >> 5) * 0x100);
00109 }
00110
00111 static inline pin_mask_t gpio_pin_to_mask(gpio_pin_t pin)
00112 {
00113 return 1U << (pin & 0x1f);
00114 }
00115
00123 static inline void portmux_select_gpio_pin(gpio_pin_t pin, unsigned long flags)
00124 {
00125 portmux_select_gpio(gpio_pin_to_port(pin), gpio_pin_to_mask(pin), flags);
00126 }
00127
00128
00129
00130 static inline void gpio_set_value_inline(gpio_pin_t pin, bool value)
00131 {
00132 pin_mask_t pin_mask = gpio_pin_to_mask(pin);
00133 void *port = gpio_pin_to_port(pin);
00134
00135 if (value)
00136 gpio_write_reg(port, OVRS, pin_mask);
00137 else
00138 gpio_write_reg(port, OVRC, pin_mask);
00139 }
00140
00141 static inline bool gpio_get_value_inline(gpio_pin_t pin)
00142 {
00143 return (gpio_read_reg(gpio_pin_to_port(pin), PVR) >> (pin & 0x1f)) & 1;
00144 }
00145
00146 extern void gpio_set_value_noninline(gpio_pin_t pin, bool value);
00147 extern bool gpio_get_value_noninline(gpio_pin_t pin);
00148
00149
00150
00162 __always_inline static void gpio_set_value(gpio_pin_t pin, bool value)
00163 {
00164 if (is_constant(pin))
00165 gpio_set_value_inline(pin, value);
00166 else
00167 gpio_set_value_noninline(pin, value);
00168 }
00169
00178 __always_inline static bool gpio_get_value(gpio_pin_t pin)
00179 {
00180 if (is_constant(pin))
00181 return gpio_get_value_inline(pin);
00182 else
00183 return gpio_get_value_noninline(pin);
00184 }
00185
00194 static inline uint32_t gpio_get_int_flag(gpio_pin_t pin)
00195 {
00196 void *port = gpio_pin_to_port(pin);
00197
00198 return gpio_read_reg(port, IFR) & gpio_pin_to_mask(pin);
00199 }
00200
00211 static inline uint32_t gpio_int_is_pending(gpio_pin_t pin)
00212 {
00213 void *port = gpio_pin_to_port(pin);
00214 pin_mask_t pin_mask = gpio_pin_to_mask(pin);
00215
00216 return gpio_read_reg(port, IER) & gpio_read_reg(port, IFR) & pin_mask;
00217 }
00218
00224 static inline void gpio_clear_int_flag(gpio_pin_t pin)
00225 {
00226 gpio_write_reg(gpio_pin_to_port(pin), IFRC, gpio_pin_to_mask(pin));
00227 }
00228
00234 static inline bool gpio_get_int_mask(gpio_pin_t pin)
00235 {
00236 void *port = gpio_pin_to_port(pin);
00237
00238 return gpio_read_reg(port, IER) & gpio_pin_to_mask(pin);
00239 }
00240
00246 static inline void gpio_enable_interrupt(gpio_pin_t pin)
00247 {
00248 gpio_write_reg(gpio_pin_to_port(pin), IERS, gpio_pin_to_mask(pin));
00249 }
00250
00256 static inline void gpio_disable_interrupt(gpio_pin_t pin)
00257 {
00258 gpio_write_reg(gpio_pin_to_port(pin), IERC, gpio_pin_to_mask(pin));
00259 }
00260
00261
00262
00263
00264
00265
00266 extern void __portmux_error_bad_id(const char *periph_type, unsigned int id);
00267
00268
00269
00270
00271
00272 extern void portmux_error_bad_constant_id(const char *, unsigned int);
00273
00274 static inline void portmux_error_bad_id(const char *periph_type,
00275 unsigned int id)
00276 {
00277 if (is_constant(id))
00278 portmux_error_bad_constant_id(periph_type, id);
00279 else
00280 __portmux_error_bad_id(periph_type, id);
00281 }
00282
00283 #endif