00001
00038 #ifndef GPIO_PORT_H_INCLUDED
00039 #define GPIO_PORT_H_INCLUDED
00040
00041 #include <compiler.h>
00042 #include <types.h>
00043 #include <regs/xmega_port.h>
00044
00061 typedef uint8_t pin_mask_t;
00062
00068 typedef uint16_t gpio_pin_t;
00069
00076 typedef uint16_t port_pin_flags_t;
00077
00079
00080 #define PORT_DIR_INPUT (0 << 8) //!< Pin is Input
00081 #define PORT_DIR_OUTPUT (1 << 8) //!< Pin is Output
00082
00083
00085
00086 #define PORT_INIT_LOW (0 << 9) //!< Initial Ouptput State is High
00087 #define PORT_INIT_HIGH (1 << 9) //!< Initial Ouptput State is Low
00088
00089
00091
00092 #define PORT_BOTHEDGES (0 << 0) //!< Sense Both Edges
00093 #define PORT_RISING (1 << 0) //!< Sense Risign Edge
00094 #define PORT_FALLING (2 << 0) //!< Sense Falling Edge
00095 #define PORT_LEVEL (3 << 0) //!< Sense Low Level
00096 #define PORT_INPUT_DISABLE (7 << 0) //!< Input Buffer Disabled
00097
00098
00100
00101 #define PORT_TOTEM (0 << 3) //!< Normal push/pull output
00102 #define PORT_BUSKEEPER (1 << 3) //!< Bus Keeper
00103 #define PORT_PULL_DOWN (2 << 3) //!< Pull-Down (when input)
00104 #define PORT_PULL_UP (3 << 3) //!< Pull-Up (when input)
00105 #define PORT_WIRED_OR (4 << 3) //!< Wired OR
00106 #define PORT_WIRED_AND (5 << 3) //!< Wired AND
00107 #define PORT_WIRED_OR_PULL_DOWN (6 << 3) //!< Wired OR and Pull-Down
00108 #define PORT_WIRED_AND_PULL_UP (7 << 3) //!< Wired AND and Pull-Up
00109
00110
00112
00113 #define PORT_INV_ENABLED (1 << 6) //!< I/O is Inverted
00114 #define PORT_INV_DISABLE (0 << 6) //!< I/O is Not Inverted
00115
00116
00118
00119 #define PORT_SRL_ENABLED (1 << 7) //!< Slew Rate Limit Enabled
00120 #define PORT_SRL_DISABLED (0 << 7) //!< Slew Rate Limit Disabled
00121
00122
00123
00133 #define CREATE_GPIO_PIN(port, pin) ((PORT_##port) + (pin))
00134
00146 static inline void *gpio_pin_to_port(gpio_pin_t pin)
00147 {
00148
00149 return (void *)(PORTA_BASE + (pin >> 3) * 0x20);
00150 }
00151
00158 static inline pin_mask_t gpio_pin_to_mask(gpio_pin_t pin)
00159 {
00160 return 1U << (pin & 0x7);
00161 }
00162
00171 extern void port_select_gpio(void *port, pin_mask_t pin_mask,
00172 port_pin_flags_t flags);
00173
00181 static inline void port_select_gpio_pin(gpio_pin_t pin, port_pin_flags_t flags)
00182 {
00183 port_select_gpio(gpio_pin_to_port(pin), gpio_pin_to_mask(pin), flags);
00184 }
00185
00186
00203 #define PORT_PORTA (0 * 8)
00204 #define PORT_PORTB (1 * 8)
00205 #define PORT_PORTC (2 * 8)
00206 #define PORT_PORTD (3 * 8)
00207 #define PORT_PORTE (4 * 8)
00208 #define PORT_PORTF (5 * 8)
00209 #define PORT_PORTG (6 * 8)
00210 #define PORT_PORTH (7 * 8)
00211 #define PORT_PORTJ (8 * 8)
00212 #define PORT_PORTK (9 * 8)
00213 #define PORT_PORTL (10 * 8)
00214 #define PORT_PORTM (11 * 8)
00215 #define PORT_PORTN (12 * 8)
00216 #define PORT_PORTP (13 * 8)
00217 #define PORT_PORTQ (14 * 8)
00218 #define PORT_PORTR (15 * 8)
00219
00220
00221 __always_inline static void gpio_set_value_inline(gpio_pin_t pin, bool value)
00222 {
00223 pin_mask_t pin_mask = gpio_pin_to_mask(pin);
00224 void *port = gpio_pin_to_port(pin);
00225
00226 if (value)
00227 port_write_reg(port, OUTSET, pin_mask);
00228 else
00229 port_write_reg(port, OUTCLR, pin_mask);
00230 }
00231
00232 __always_inline static bool gpio_get_value_inline(gpio_pin_t pin)
00233 {
00234 return (port_read_reg(gpio_pin_to_port(pin), IN) >> (pin & 0x7)) & 1;
00235 }
00236
00237 extern void gpio_set_value_noninline(gpio_pin_t pin, bool value);
00238 extern bool gpio_get_value_noninline(gpio_pin_t pin);
00240
00241
00253 __always_inline static void gpio_set_value(gpio_pin_t pin, bool value)
00254 {
00255 if (is_constant(pin))
00256 gpio_set_value_inline(pin, value);
00257 else
00258 gpio_set_value_noninline(pin, value);
00259 }
00260
00268 __always_inline static bool gpio_get_value(gpio_pin_t pin)
00269 {
00270 if (is_constant(pin))
00271 return gpio_get_value_inline(pin);
00272 else
00273 return gpio_get_value_noninline(pin);
00274 }
00275
00281 __always_inline static void gpio_toggle_value(gpio_pin_t pin)
00282 {
00283 pin_mask_t pin_mask = gpio_pin_to_mask(pin);
00284 void *port = gpio_pin_to_port(pin);
00285
00286 port_write_reg(port, OUTTGL, pin_mask);
00287 }
00289
00290
00291 #endif