00001
00038 #ifndef GPIO_GPIO_MEGA_H_INCLUDED
00039 #define GPIO_GPIO_MEGA_H_INCLUDED
00040
00041 #include <assert.h>
00042
00055 typedef uint8_t gpio_pin_t;
00056
00063 static inline port_t gpio_pin_to_port(gpio_pin_t pin)
00064 {
00065 return (pin >> 3);
00066 }
00067
00073 static inline uint8_t gpio_pin_to_mask(gpio_pin_t pin)
00074 {
00075 return (1U << (pin & 0x07));
00076 }
00077
00088 #define CREATE_GPIO_PIN(port, pin) (GPIO_##port * 8 + (pin))
00089
00092 #define GPIO_DIR_INPUT (0 << 0) //!< Set pin as input
00093 #define GPIO_DIR_OUTPUT (1 << 0) //!< Set pin as output
00094 #define GPIO_PULL_UP (1 << 1) //!< Enable pull-up
00095 #define GPIO_INIT_HIGH (1 << 1) //!< Set initial pin state to high
00096 #define GPIO_INIT_LOW (0 << 1) //!< Set initial pin state to low
00097
00098
00106 static inline void gpio_set_gpio_pin(gpio_pin_t pin)
00107 {
00108 uint8_t portx;
00109
00110 switch (gpio_pin_to_port(pin))
00111 {
00112 #ifdef CONFIG_HAVE_GPIO_PORTA
00113 case GPIO_PORTA:
00114 portx = avr_read_reg8(PORTA);
00115 portx |= gpio_pin_to_mask(pin);
00116 avr_write_reg8(PORTA, portx);
00117 break;
00118 #endif
00119 #ifdef CONFIG_HAVE_GPIO_PORTB
00120 case GPIO_PORTB:
00121 portx = avr_read_reg8(PORTB);
00122 portx |= gpio_pin_to_mask(pin);
00123 avr_write_reg8(PORTB, portx);
00124 break;
00125 #endif
00126 #ifdef CONFIG_HAVE_GPIO_PORTC
00127 case GPIO_PORTC:
00128 portx = avr_read_reg8(PORTC);
00129 portx |= gpio_pin_to_mask(pin);
00130 avr_write_reg8(PORTC, portx);
00131 break;
00132 #endif
00133 #ifdef CONFIG_HAVE_GPIO_PORTD
00134 case GPIO_PORTD:
00135 portx = avr_read_reg8(PORTD);
00136 portx |= gpio_pin_to_mask(pin);
00137 avr_write_reg8(PORTD, portx);
00138 break;
00139 #endif
00140 #ifdef CONFIG_HAVE_GPIO_PORTE
00141 case GPIO_PORTE:
00142 portx = avr_read_reg8(PORTE);
00143 portx |= gpio_pin_to_mask(pin);
00144 avr_write_reg8(PORTE, portx);
00145 break;
00146 #endif
00147 #ifdef CONFIG_HAVE_GPIO_PORTF
00148 case GPIO_PORTF:
00149 portx = avr_read_reg8(PORTF);
00150 portx |= gpio_pin_to_mask(pin);
00151 avr_write_reg8(PORTF, portx);
00152 break;
00153 #endif
00154 }
00155 }
00156
00163 static inline void gpio_clear_gpio_pin(gpio_pin_t pin)
00164 {
00165 uint8_t portx;
00166
00167 switch (gpio_pin_to_port(pin))
00168 {
00169 #ifdef CONFIG_HAVE_GPIO_PORTA
00170 case GPIO_PORTA:
00171 portx = avr_read_reg8(PORTA);
00172 portx &= ~gpio_pin_to_mask(pin);
00173 avr_write_reg8(PORTA, portx);
00174 break;
00175 #endif
00176 #ifdef CONFIG_HAVE_GPIO_PORTB
00177 case GPIO_PORTB:
00178 portx = avr_read_reg8(PORTB);
00179 portx &= ~gpio_pin_to_mask(pin);
00180 avr_write_reg8(PORTB, portx);
00181 break;
00182 #endif
00183 #ifdef CONFIG_HAVE_GPIO_PORTC
00184 case GPIO_PORTC:
00185 portx = avr_read_reg8(PORTC);
00186 portx &= ~gpio_pin_to_mask(pin);
00187 avr_write_reg8(PORTC, portx);
00188 break;
00189 #endif
00190 #ifdef CONFIG_HAVE_GPIO_PORTD
00191 case GPIO_PORTD:
00192 portx = avr_read_reg8(PORTD);
00193 portx &= ~gpio_pin_to_mask(pin);
00194 avr_write_reg8(PORTD, portx);
00195 break;
00196 #endif
00197 #ifdef CONFIG_HAVE_GPIO_PORTE
00198 case GPIO_PORTE:
00199 portx = avr_read_reg8(PORTE);
00200 portx &= ~gpio_pin_to_mask(pin);
00201 avr_write_reg8(PORTE, portx);
00202 break;
00203 #endif
00204 #ifdef CONFIG_HAVE_GPIO_PORTF
00205 case GPIO_PORTF:
00206 portx = avr_read_reg8(PORTF);
00207 portx &= ~gpio_pin_to_mask(pin);
00208 avr_write_reg8(PORTF, portx);
00209 break;
00210 #endif
00211 }
00212 }
00213
00222 static inline void gpio_set_value(gpio_pin_t pin, bool value)
00223 {
00224 if (value)
00225 gpio_set_gpio_pin(pin);
00226 else
00227 gpio_clear_gpio_pin(pin);
00228 }
00229
00240 static inline bool gpio_get_value(gpio_pin_t pin)
00241 {
00242 uint8_t pin_state = 0;
00243
00244 switch (gpio_pin_to_port(pin))
00245 {
00246 #ifdef CONFIG_HAVE_GPIO_PORTA
00247 case GPIO_PORTA:
00248 pin_state = avr_read_reg8(PINA);
00249 break;
00250 #endif
00251 #ifdef CONFIG_HAVE_GPIO_PORTB
00252 case GPIO_PORTB:
00253 pin_state = avr_read_reg8(PINB);
00254 break;
00255 #endif
00256 #ifdef CONFIG_HAVE_GPIO_PORTC
00257 case GPIO_PORTC:
00258 pin_state = avr_read_reg8(PINC);
00259 break;
00260 #endif
00261 #ifdef CONFIG_HAVE_GPIO_PORTD
00262 case GPIO_PORTD:
00263 pin_state = avr_read_reg8(PIND);
00264 break;
00265 #endif
00266 #ifdef CONFIG_HAVE_GPIO_PORTE
00267 case GPIO_PORTE:
00268 pin_state = avr_read_reg8(PINE);
00269 break;
00270 #endif
00271 #ifdef CONFIG_HAVE_GPIO_PORTF
00272 case GPIO_PORTF:
00273 pin_state = avr_read_reg8(PINF);
00274 break;
00275 #endif
00276 default:
00277 unhandled_case(gpio_pin_to_port(pin));
00278 break;
00279 }
00280
00281 return pin_state & gpio_pin_to_mask(pin);
00282 }
00283
00296 __always_inline static void port_select_gpio_pin(gpio_pin_t pin, uint8_t flags)
00297 {
00298 uint8_t ddrx;
00299 uint8_t portx;
00300
00301 switch (gpio_pin_to_port(pin))
00302 {
00303 #ifdef CONFIG_HAVE_GPIO_PORTA
00304 case GPIO_PORTA:
00305 ddrx = avr_read_reg8(DDRA);
00306 portx = avr_read_reg8(PORTA);
00307
00308 if (flags & GPIO_DIR_OUTPUT)
00309 ddrx |= gpio_pin_to_mask(pin);
00310 else
00311 ddrx &= ~gpio_pin_to_mask(pin);
00312
00313 if (flags & (GPIO_INIT_HIGH | GPIO_PULL_UP))
00314 portx |= gpio_pin_to_mask(pin);
00315 else
00316 portx &= ~gpio_pin_to_mask(pin);
00317
00318 avr_write_reg8(PORTA, portx);
00319 avr_write_reg8(DDRA, ddrx);
00320 break;
00321 #endif
00322 #ifdef CONFIG_HAVE_GPIO_PORTB
00323 case GPIO_PORTB:
00324 ddrx = avr_read_reg8(DDRB);
00325 portx = avr_read_reg8(PORTB);
00326
00327 if (flags & GPIO_DIR_OUTPUT)
00328 ddrx |= gpio_pin_to_mask(pin);
00329 else
00330 ddrx &= ~gpio_pin_to_mask(pin);
00331
00332 if (flags & (GPIO_INIT_HIGH | GPIO_PULL_UP))
00333 portx |= gpio_pin_to_mask(pin);
00334 else
00335 portx &= ~gpio_pin_to_mask(pin);
00336
00337 avr_write_reg8(PORTB, portx);
00338 avr_write_reg8(DDRB, ddrx);
00339 break;
00340 #endif
00341 #ifdef CONFIG_HAVE_GPIO_PORTC
00342 case GPIO_PORTC:
00343 ddrx = avr_read_reg8(DDRC);
00344 portx = avr_read_reg8(PORTC);
00345
00346 if (flags & GPIO_DIR_OUTPUT)
00347 ddrx |= gpio_pin_to_mask(pin);
00348 else
00349 ddrx &= ~gpio_pin_to_mask(pin);
00350
00351 if (flags & (GPIO_INIT_HIGH | GPIO_PULL_UP))
00352 portx |= gpio_pin_to_mask(pin);
00353 else
00354 portx &= ~gpio_pin_to_mask(pin);
00355
00356 avr_write_reg8(PORTC, portx);
00357 avr_write_reg8(DDRC, ddrx);
00358 break;
00359 #endif
00360 #ifdef CONFIG_HAVE_GPIO_PORTD
00361 case GPIO_PORTD:
00362 ddrx = avr_read_reg8(DDRD);
00363 portx = avr_read_reg8(PORTD);
00364
00365 if (flags & GPIO_DIR_OUTPUT)
00366 ddrx |= gpio_pin_to_mask(pin);
00367 else
00368 ddrx &= ~gpio_pin_to_mask(pin);
00369 if (flags & (GPIO_INIT_HIGH |GPIO_PULL_UP))
00370 portx |= gpio_pin_to_mask(pin);
00371 else
00372 portx &= ~gpio_pin_to_mask(pin);
00373
00374 avr_write_reg8(PORTD, portx);
00375 avr_write_reg8(DDRD, ddrx);
00376 break;
00377 #endif
00378 #ifdef CONFIG_HAVE_GPIO_PORTE
00379 case GPIO_PORTE:
00380 ddrx = avr_read_reg8(DDRE);
00381 portx = avr_read_reg8(PORTE);
00382
00383 if (flags & GPIO_DIR_OUTPUT)
00384 ddrx |= gpio_pin_to_mask(pin);
00385 else
00386 ddrx &= ~gpio_pin_to_mask(pin);
00387 if (flags & (GPIO_INIT_HIGH |GPIO_PULL_UP))
00388 portx |= gpio_pin_to_mask(pin);
00389 else
00390 portx &= ~gpio_pin_to_mask(pin);
00391
00392 avr_write_reg8(PORTE, portx);
00393 avr_write_reg8(DDRE, ddrx);
00394 break;
00395 #endif
00396 #ifdef CONFIG_HAVE_GPIO_PORTF
00397 case GPIO_PORTF:
00398 ddrx = avr_read_reg8(DDRF);
00399 portx = avr_read_reg8(PORTF);
00400
00401 if (flags & GPIO_DIR_OUTPUT)
00402 ddrx |= gpio_pin_to_mask(pin);
00403 else
00404 ddrx &= ~gpio_pin_to_mask(pin);
00405 if (flags & (GPIO_INIT_HIGH |GPIO_PULL_UP))
00406 portx |= gpio_pin_to_mask(pin);
00407 else
00408 portx &= ~gpio_pin_to_mask(pin);
00409
00410 avr_write_reg8(PORTF, portx);
00411 avr_write_reg8(DDRF, ddrx);
00412 break;
00413 #endif
00414 }
00415 }
00416 #endif