Add pin de-multiplexing to STM32 EXTI handling

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@3753 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2011-07-08 12:46:56 +00:00
parent ab6f9522f0
commit 5ff36317c4

View File

@ -91,10 +91,7 @@ static const char g_portchar[8] =
}; };
#endif #endif
static xcpt_t stm32_exti_callbacks[7] = static xcpt_t stm32_exti_callbacks[16];
{
NULL, NULL, NULL, NULL, NULL, NULL, NULL
};
/**************************************************************************** /****************************************************************************
* Private Functions * Private Functions
@ -255,16 +252,20 @@ int stm32_gpio_configlock(uint32_t cfgset, bool altlock)
return OK; return OK;
} }
/**************************************************************************** /****************************************************************************
* Interrupt Service Routines - Dispatchers * Interrupt Service Routines - Dispatchers
****************************************************************************/ ****************************************************************************/
int stm32_exti0_isr(int irq, void *context) static int stm32_exti0_isr(int irq, void *context)
{ {
int ret = OK; int ret = OK;
/* Clear the pending interrupt */
putreg32(0x0001, STM32_EXTI_PR); putreg32(0x0001, STM32_EXTI_PR);
/* And dispatch the interrupt to the handler */
if (stm32_exti_callbacks[0]) if (stm32_exti_callbacks[0])
{ {
ret = stm32_exti_callbacks[0](irq, context); ret = stm32_exti_callbacks[0](irq, context);
@ -272,11 +273,16 @@ int stm32_exti0_isr(int irq, void *context)
return ret; return ret;
} }
int stm32_exti1_isr(int irq, void *context) static int stm32_exti1_isr(int irq, void *context)
{ {
int ret = OK; int ret = OK;
/* Clear the pending interrupt */
putreg32(0x0002, STM32_EXTI_PR); putreg32(0x0002, STM32_EXTI_PR);
/* And dispatch the interrupt to the handler */
if (stm32_exti_callbacks[1]) if (stm32_exti_callbacks[1])
{ {
ret = stm32_exti_callbacks[1](irq, context); ret = stm32_exti_callbacks[1](irq, context);
@ -284,11 +290,16 @@ int stm32_exti1_isr(int irq, void *context)
return ret; return ret;
} }
int stm32_exti2_isr(int irq, void *context) static int stm32_exti2_isr(int irq, void *context)
{ {
int ret = OK; int ret = OK;
/* Clear the pending interrupt */
putreg32(0x0004, STM32_EXTI_PR); putreg32(0x0004, STM32_EXTI_PR);
/* And dispatch the interrupt to the handler */
if (stm32_exti_callbacks[2]) if (stm32_exti_callbacks[2])
{ {
ret = stm32_exti_callbacks[2](irq, context); ret = stm32_exti_callbacks[2](irq, context);
@ -296,11 +307,16 @@ int stm32_exti2_isr(int irq, void *context)
return ret; return ret;
} }
int stm32_exti3_isr(int irq, void *context) static int stm32_exti3_isr(int irq, void *context)
{ {
int ret = OK; int ret = OK;
/* Clear the pending interrupt */
putreg32(0x0008, STM32_EXTI_PR); putreg32(0x0008, STM32_EXTI_PR);
/* And dispatch the interrupt to the handler */
if (stm32_exti_callbacks[3]) if (stm32_exti_callbacks[3])
{ {
ret = stm32_exti_callbacks[3](irq, context); ret = stm32_exti_callbacks[3](irq, context);
@ -308,11 +324,16 @@ int stm32_exti3_isr(int irq, void *context)
return ret; return ret;
} }
int stm32_exti4_isr(int irq, void *context) static int stm32_exti4_isr(int irq, void *context)
{ {
int ret = OK; int ret = OK;
/* Clear the pending interrupt */
putreg32(0x0010, STM32_EXTI_PR); putreg32(0x0010, STM32_EXTI_PR);
/* And dispatch the interrupt to the handler */
if (stm32_exti_callbacks[4]) if (stm32_exti_callbacks[4])
{ {
ret = stm32_exti_callbacks[4](irq, context); ret = stm32_exti_callbacks[4](irq, context);
@ -320,28 +341,52 @@ int stm32_exti4_isr(int irq, void *context)
return ret; return ret;
} }
int stm32_exti95_isr(int irq, void *context) static int stm32_exti_multiisr(int irq, void *context, int first, int last)
{ {
uint32_t pr;
int pin;
int ret = OK; int ret = OK;
putreg32(0x03E0, STM32_EXTI_PR); /* ACK all pins, since we support just one */ /* Examine the state of each pin in the group */
if (stm32_exti_callbacks[5])
pr = getreg32(STM32_EXTI_PR);
/* And dispatch the interrupt to the handler */
for (pin = first; pin <= last; pin++)
{ {
ret = stm32_exti_callbacks[5](irq, context); /* Is an interrupt pending on this pin? */
uint32_t mask = (1 << pin);
if ((pr & mask) != 0)
{
/* Clear the pending interrupt */
putreg32(mask, STM32_EXTI_PR);
/* And dispatch the interrupt to the handler */
if (stm32_exti_callbacks[pin])
{
int tmp = stm32_exti_callbacks[pin](irq, context);
if (tmp != OK)
{
ret = tmp;
}
}
}
} }
return ret; return ret;
} }
int stm32_exti1510_isr(int irq, void *context) static int stm32_exti95_isr(int irq, void *context)
{ {
int ret = OK; return stm32_exti_multiisr(irq, context, 5, 9);
}
putreg32(0xFC00, STM32_EXTI_PR); /* ACK all pins, since we support just one */ static int stm32_exti1510_isr(int irq, void *context)
if (stm32_exti_callbacks[6]) {
{ return stm32_exti_multiisr(irq, context, 10, 15);
ret = stm32_exti_callbacks[6](irq, context);
}
return ret;
} }
/**************************************************************************** /****************************************************************************
@ -582,66 +627,82 @@ bool stm32_gpioread(uint32_t pinset)
xcpt_t stm32_gpiosetevent(uint32_t pinset, bool risingedge, bool fallingedge, xcpt_t stm32_gpiosetevent(uint32_t pinset, bool risingedge, bool fallingedge,
bool event, xcpt_t func) bool event, xcpt_t func)
{ {
uint32_t exti_isr = pinset & GPIO_PIN_MASK; uint32_t pin = pinset & GPIO_PIN_MASK;
uint32_t exti_bit = STM32_EXTI_BIT( exti_isr ); uint32_t exti = STM32_EXTI_BIT(pin);
int intno; int irq;
int (*exti_hnd)(int irq, void *context); xcpt_t handler;
xcpt_t oldhandler = NULL; xcpt_t oldhandler = NULL;
/* Set callback, single callback at the moment, but we could extend /* Select the interrupt handler for this EXTI pin */
* that easily
*/
if (exti_isr < 5) { if (pin < 5)
intno = exti_isr + STM32_IRQ_EXTI0; {
switch(exti_isr) { irq = pin + STM32_IRQ_EXTI0;
case 0: exti_hnd = stm32_exti0_isr; break; switch (pin)
case 1: exti_hnd = stm32_exti1_isr; break; {
case 2: exti_hnd = stm32_exti2_isr; break; case 0:
case 3: exti_hnd = stm32_exti3_isr; break; handler = stm32_exti0_isr;
default:exti_hnd = stm32_exti4_isr; break; break;
case 1:
handler = stm32_exti1_isr;
break;
case 2:
handler = stm32_exti2_isr;
break;
case 3:
handler = stm32_exti3_isr;
break;
default:
handler = stm32_exti4_isr;
break;
} }
} }
else if (exti_isr < 10) { else if (pin < 10)
exti_isr = 5; {
exti_hnd = stm32_exti95_isr; irq = STM32_IRQ_EXTI95;
intno = STM32_IRQ_EXTI95; handler = stm32_exti95_isr;
} }
else { else
exti_isr = 6; {
exti_hnd = stm32_exti1510_isr; irq = STM32_IRQ_EXTI1510;
intno = STM32_IRQ_EXTI1510; handler = stm32_exti1510_isr;
} }
/* Get the previous GPIO IRQ handler; Save the new IRQ handler. */ /* Get the previous GPIO IRQ handler; Save the new IRQ handler. */
oldhandler = stm32_exti_callbacks[exti_isr]; oldhandler = stm32_exti_callbacks[pin];
stm32_exti_callbacks[exti_isr] = func; stm32_exti_callbacks[pin] = func;
/* Install external interrupt handlers */ /* Install external interrupt handlers */
if (func) { if (func)
irq_attach(intno, exti_hnd); {
up_enable_irq(intno); irq_attach(irq, handler);
up_enable_irq(irq);
}
else
{
up_disable_irq(irq);
} }
else up_disable_irq(intno);
/* Configure GPIO, enable EXTI line enabled if event or interrupt is enabled */ /* Configure GPIO, enable EXTI line enabled if event or interrupt is enabled */
if (event || func) if (event || func)
{
pinset |= GPIO_EXTI; pinset |= GPIO_EXTI;
}
stm32_configgpio( pinset ); stm32_configgpio(pinset);
/* Configure rising/falling edges */ /* Configure rising/falling edges */
modifyreg32(STM32_EXTI_RTSR, risingedge ? 0 : exti_bit, risingedge ? exti_bit : 0); modifyreg32(STM32_EXTI_RTSR, risingedge ? 0 : exti, risingedge ? exti : 0);
modifyreg32(STM32_EXTI_FTSR, fallingedge ? 0 : exti_bit, fallingedge ? exti_bit : 0); modifyreg32(STM32_EXTI_FTSR, fallingedge ? 0 : exti, fallingedge ? exti : 0);
/* Enable Events and Interrupts */ /* Enable Events and Interrupts */
modifyreg32(STM32_EXTI_EMR, event ? 0 : exti_bit, event ? exti_bit : 0); modifyreg32(STM32_EXTI_EMR, event ? 0 : exti, event ? exti : 0);
modifyreg32(STM32_EXTI_IMR, func ? 0 : exti_bit, func ? exti_bit : 0); modifyreg32(STM32_EXTI_IMR, func ? 0 : exti, func ? exti : 0);
/* Return the old IRQ handler */ /* Return the old IRQ handler */