[vc_row][vc_column width=”1/1″][vc_column_text]External interrupt is an important mechanism to handle external event by pcDuino. pcDuino provides two external interrupt pins (pins 2 and 3). In this post, we are looking at how to use these pins to handle external interrupt.
[/vc_column_text][vc_tour][vc_tab title=”What is interrupt?” tab_id=”1384562369-1-64″][vc_column_text]Compared to the speed of CPU or even of memory, I/O devices are considered extremely slow. Printers, keyboards, mice are all very slow devices. They take a long time to respond, and furthermore, they only require occasional handling by the CPU. Even though you may feel you’re using the keyboard or mouse all the time, the CPU runs so much faster than you can, that it’s practically as if you’re not using it at all.
One way for a CPU to communicate with an I/O device is through polling. That’s discussed in a different set of class notes .
However, if the device is slow, and rarely requires servicing, external interrupts are usually a better way to go.
This way of handling I/O devices usually called interrupt driven I/O.
A CPU usually has at least one input pin devoted to interrupts. Whenever a device wants the CPU to pay attention, it sends a a signal to this pin.
The protocol usually runs like this:
I/O device sets the INT pin from 0 to 1.
The CPU completes the current instruction, and then saves the state of the program. For older ISPs, this involved copying registers to the stack. For newer ones ISPs, this usually means switching to a supervisor (operating system) set of registers.
The CPU then determines which kind interrupt has occurred. It can send a signal to the I/O device to ask for an interrupt number.
The I/O device can place the interrupt number on the data bus.
The CPU then runs an interrupt handler (a special function) for that.
Once the interrupt has been handled, the I/O device turns the INT value back to 0, and the CPU resumes running the program that was interrupted.
The detail can be found at: http://www.cs.umd.edu/class/spring2003/cmsc311/Notes/IO/extInt.html[/vc_column_text][/vc_tab][vc_tab title=”External interrupt on A10″ tab_id=”1384562369-2-15″][vc_column_text]There are 32 GPIO pins on Allwinner A10 that support EINT:
- PH0 – PH21 ( correspond to EINT0 – EINT21 )
- PI10 – PI19 ( correspond to EINT22 – EINT31 )
The relationship of the mapping between Arduino headers and A10 pins is shown below:
You can also find the mapping information in:
https://github.com/pcduino/kernel/blob/master/sunxi-boards/sys_config/a10/pcduino.fex
Because that we use GPIO5 and GPIO6 in other purposes, we cannot use them for external interruption.
We use GPIO2 and GPIO3 for external interruption when we use Arduino-ish programming.
[/vc_column_text][/vc_tab][vc_tab title=”Sample Code” tab_id=”1384562902140-2-1″][vc_column_text]
#include <core.h>
#define MAX_COUNT 30
#define INT_MODE FALLING
/* support two interrupts pin2/pin3, and default interrupt pins are pulled up*/
int led_pin0 = 18, led_pin1 = 19;
volatile int state0 = LOW;
volatile int state1 = LOW;
int bc0 = 0, bc1 = 0;
void blink0()
{
state0 = !state0;
bc0++;
printf("blink0: %d, count=%d\r\n", state0, bc0);
}
void blink1()
{
state1 = !state1;
bc1++;
printf("blink1: %d, count=%d\r\n", state1, bc1);
}
void setup()
{
pinMode(led_pin0, OUTPUT);
pinMode(led_pin1, OUTPUT);
attachInterrupt(0, blink0, INT_MODE);
attachInterrupt(1, blink1, INT_MODE);
}
void loop()
{
digitalWrite(led_pin0, state0);
digitalWrite(led_pin1, state1);
if (bc0 >= MAX_COUNT)
detachInterrupt(0);
if (bc1 >= MAX_COUNT)
detachInterrupt(1);
delay(1000);
}
[/vc_column_text][/vc_tab][vc_tab title=”Build” tab_id=”1384562936433-3-6″][vc_column_text]The above code can build and run in the Arduino IDE that comes with the pcDuino board.[/vc_column_text][/vc_tab][/vc_tour][/vc_column][/vc_row]
Leave a Reply
You must be logged in to post a comment.