Gordon Henderson | 3fbc564 | 2013-03-24 20:04:07 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * isr.c: |
| 3 | * Wait for Interrupt test program - ISR method |
| 4 | * |
| 5 | * How to test: |
| 6 | * Use the SoC's pull-up and pull down resistors that are avalable |
| 7 | * on input pins. So compile & run this program (via sudo), then |
| 8 | * in another terminal: |
| 9 | * gpio mode 0 up |
| 10 | * gpio mode 0 down |
| 11 | * at which point it should trigger an interrupt. Toggle the pin |
| 12 | * up/down to generate more interrupts to test. |
| 13 | * |
| 14 | * Copyright (c) 2013 Gordon Henderson. |
| 15 | *********************************************************************** |
| 16 | * This file is part of wiringPi: |
| 17 | * https://projects.drogon.net/raspberry-pi/wiringpi/ |
| 18 | * |
| 19 | * wiringPi is free software: you can redistribute it and/or modify |
| 20 | * it under the terms of the GNU Lesser General Public License as published by |
| 21 | * the Free Software Foundation, either version 3 of the License, or |
| 22 | * (at your option) any later version. |
| 23 | * |
| 24 | * wiringPi is distributed in the hope that it will be useful, |
| 25 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 26 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 27 | * GNU Lesser General Public License for more details. |
| 28 | * |
| 29 | * You should have received a copy of the GNU Lesser General Public License |
| 30 | * along with wiringPi. If not, see <http://www.gnu.org/licenses/>. |
| 31 | *********************************************************************** |
| 32 | */ |
| 33 | |
| 34 | #include <stdio.h> |
| 35 | #include <string.h> |
| 36 | #include <errno.h> |
| 37 | #include <stdlib.h> |
| 38 | #include <wiringPi.h> |
| 39 | |
| 40 | |
| 41 | // What GPIO input are we using? |
| 42 | // This is a wiringPi pin number |
| 43 | |
| 44 | #define BUTTON_PIN 0 |
| 45 | |
| 46 | // globalCounter: |
| 47 | // Global variable to count interrupts |
| 48 | // Should be declared volatile to make sure the compiler doesn't cache it. |
| 49 | |
| 50 | static volatile int globalCounter = 0 ; |
| 51 | |
| 52 | |
| 53 | /* |
| 54 | * myInterrupt: |
| 55 | ********************************************************************************* |
| 56 | */ |
| 57 | |
| 58 | void myInterrupt (void) |
| 59 | { |
| 60 | ++globalCounter ; |
| 61 | } |
| 62 | |
| 63 | |
| 64 | /* |
| 65 | ********************************************************************************* |
| 66 | * main |
| 67 | ********************************************************************************* |
| 68 | */ |
| 69 | |
| 70 | int main (void) |
| 71 | { |
| 72 | int myCounter = 0 ; |
| 73 | |
| 74 | if (wiringPiSetup () < 0) |
| 75 | { |
| 76 | fprintf (stderr, "Unable to setup wiringPi: %s\n", strerror (errno)) ; |
| 77 | return 1 ; |
| 78 | } |
| 79 | |
| 80 | if (wiringPiISR (BUTTON_PIN, INT_EDGE_FALLING, &myInterrupt) < 0) |
| 81 | { |
| 82 | fprintf (stderr, "Unable to setup ISR: %s\n", strerror (errno)) ; |
| 83 | return 1 ; |
| 84 | } |
| 85 | |
| 86 | |
| 87 | for (;;) |
| 88 | { |
| 89 | printf ("Waiting ... ") ; fflush (stdout) ; |
| 90 | |
| 91 | while (myCounter == globalCounter) |
| 92 | delay (100) ; |
| 93 | |
| 94 | printf (" Done. counter: %5d\n", globalCounter) ; |
| 95 | myCounter = globalCounter ; |
| 96 | } |
| 97 | |
| 98 | return 0 ; |
| 99 | } |