Merge pull request #7590 from lebrush/fix/atmega-gpio-misc
GPIO interrupt fixes for atmega328p
This commit is contained in:
commit
c054e38156
@ -32,6 +32,24 @@ extern "C" {
|
|||||||
*/
|
*/
|
||||||
#define GPIO_PIN(x, y) ((x << 4) | y)
|
#define GPIO_PIN(x, y) ((x << 4) | y)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Override the GPIO flanks
|
||||||
|
*
|
||||||
|
* This device has an additional mode in which the interrupt is triggered
|
||||||
|
* when the pin is low.
|
||||||
|
*
|
||||||
|
* Enumeration order is important, do not modify.
|
||||||
|
* @{
|
||||||
|
*/
|
||||||
|
#define HAVE_GPIO_FLANK_T
|
||||||
|
typedef enum {
|
||||||
|
GPIO_LOW, /**< emit interrupt when pin low */
|
||||||
|
GPIO_BOTH, /**< emit interrupt on both flanks */
|
||||||
|
GPIO_FALLING, /**< emit interrupt on falling flank */
|
||||||
|
GPIO_RISING, /**< emit interrupt on rising flank */
|
||||||
|
} gpio_flank_t;
|
||||||
|
/** @} */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Use some common SPI functions
|
* @brief Use some common SPI functions
|
||||||
* @{
|
* @{
|
||||||
|
|||||||
@ -138,6 +138,9 @@ int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
|
|||||||
if ((_port_num(pin) == PORT_D && pin_num > 3)
|
if ((_port_num(pin) == PORT_D && pin_num > 3)
|
||||||
#if defined (PORTE)
|
#if defined (PORTE)
|
||||||
|| (_port_num(pin) == PORT_E && pin_num < 4)
|
|| (_port_num(pin) == PORT_E && pin_num < 4)
|
||||||
|
|| (_port_num(pin) != PORT_D && _port_num(pin) != PORT_E)
|
||||||
|
#elif defined(CPU_ATMEGA328P)
|
||||||
|
|| (pin_num < 2) || (_port_num(pin) != PORT_D)
|
||||||
#endif
|
#endif
|
||||||
|| ((mode != GPIO_IN) && (mode != GPIO_IN_PU))) {
|
|| ((mode != GPIO_IN) && (mode != GPIO_IN_PU))) {
|
||||||
return -1;
|
return -1;
|
||||||
@ -148,43 +151,26 @@ int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
|
|||||||
/* clear global interrupt flag */
|
/* clear global interrupt flag */
|
||||||
cli();
|
cli();
|
||||||
|
|
||||||
|
#if defined(CPU_ATMEGA328P)
|
||||||
|
/* INT pins start at PD2 instead of at PD0 */
|
||||||
|
pin_num -= 2;
|
||||||
|
#endif
|
||||||
|
|
||||||
EIMSK |= (1 << pin_num);
|
EIMSK |= (1 << pin_num);
|
||||||
|
|
||||||
/* configure the flank */
|
/* configure the flank */
|
||||||
switch (flank) {
|
if (flank > GPIO_RISING) {
|
||||||
case GPIO_RISING:
|
|
||||||
if (pin_num < 4) {
|
|
||||||
EICRA |= (3 << pin_num * 2);
|
|
||||||
}
|
|
||||||
#if defined(EICRB)
|
|
||||||
else {
|
|
||||||
EICRB |= (3 << (pin_num * 2) % 4);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
case GPIO_FALLING:
|
|
||||||
if (pin_num < 4) {
|
|
||||||
EICRA |= (2 << pin_num * 2);
|
|
||||||
}
|
|
||||||
#if defined(EICRB)
|
|
||||||
else {
|
|
||||||
EICRB |= (2 << (pin_num * 2) % 4);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
case GPIO_BOTH:
|
|
||||||
if (pin_num < 4) {
|
|
||||||
EICRA |= (1 << pin_num * 2);
|
|
||||||
}
|
|
||||||
#if defined(EICRB)
|
|
||||||
else {
|
|
||||||
EICRB |= (1 << (pin_num * 2) % 4);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
return -1;
|
return -1;
|
||||||
};
|
}
|
||||||
|
|
||||||
|
if (pin_num < 4) {
|
||||||
|
EICRA |= (flank << (pin_num * 2));
|
||||||
|
}
|
||||||
|
#if defined(EICRB)
|
||||||
|
else {
|
||||||
|
EICRB |= (flank << (pin_num * 2) % 4);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/* set callback */
|
/* set callback */
|
||||||
config[pin_num].cb = cb;
|
config[pin_num].cb = cb;
|
||||||
@ -198,12 +184,22 @@ int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
|
|||||||
|
|
||||||
void gpio_irq_enable(gpio_t pin)
|
void gpio_irq_enable(gpio_t pin)
|
||||||
{
|
{
|
||||||
|
#if defined(CPU_ATMEGA328P)
|
||||||
|
/* INT pins start at PD2 instead of at PD0 */
|
||||||
|
EIMSK |= (1 << (_pin_num(pin) - 2));
|
||||||
|
#else
|
||||||
EIMSK |= (1 << _pin_num(pin));
|
EIMSK |= (1 << _pin_num(pin));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void gpio_irq_disable(gpio_t pin)
|
void gpio_irq_disable(gpio_t pin)
|
||||||
{
|
{
|
||||||
|
#if defined(CPU_ATMEGA328P)
|
||||||
|
/* INT pins start at PD2 instead of at PD0 */
|
||||||
|
EIMSK &= ~(1 << (_pin_num(pin) - 2));
|
||||||
|
#else
|
||||||
EIMSK &= ~(1 << _pin_num(pin));
|
EIMSK &= ~(1 << _pin_num(pin));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
int gpio_read(gpio_t pin)
|
int gpio_read(gpio_t pin)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user