blob: abc6aec994fcdf7f774f186ce6d75d298d96bd3a [file] [log] [blame]
Gordon Henderson3fbc5642013-03-24 20:04:07 +00001/*
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
Gordon Henderson3fbc5642013-03-24 20:04:07 +000041// globalCounter:
42// Global variable to count interrupts
43// Should be declared volatile to make sure the compiler doesn't cache it.
44
Phil Howard26c7fe32016-02-27 16:03:10 +000045static volatile int globalCounter [8] ;
Gordon Henderson3fbc5642013-03-24 20:04:07 +000046
47
48/*
49 * myInterrupt:
50 *********************************************************************************
51 */
52
Phil Howard26c7fe32016-02-27 16:03:10 +000053void myInterrupt0 (void) { ++globalCounter [0] ; }
54void myInterrupt1 (void) { ++globalCounter [1] ; }
55void myInterrupt2 (void) { ++globalCounter [2] ; }
56void myInterrupt3 (void) { ++globalCounter [3] ; }
57void myInterrupt4 (void) { ++globalCounter [4] ; }
58void myInterrupt5 (void) { ++globalCounter [5] ; }
59void myInterrupt6 (void) { ++globalCounter [6] ; }
60void myInterrupt7 (void) { ++globalCounter [7] ; }
Gordon Henderson3fbc5642013-03-24 20:04:07 +000061
62
63/*
64 *********************************************************************************
65 * main
66 *********************************************************************************
67 */
68
69int main (void)
70{
Phil Howard26c7fe32016-02-27 16:03:10 +000071 int gotOne, pin ;
72 int myCounter [8] ;
Gordon Henderson3fbc5642013-03-24 20:04:07 +000073
Phil Howard26c7fe32016-02-27 16:03:10 +000074 for (pin = 0 ; pin < 8 ; ++pin)
75 globalCounter [pin] = myCounter [pin] = 0 ;
Gordon Henderson3fbc5642013-03-24 20:04:07 +000076
Phil Howard26c7fe32016-02-27 16:03:10 +000077 wiringPiSetup () ;
Gordon Henderson3fbc5642013-03-24 20:04:07 +000078
Phil Howard26c7fe32016-02-27 16:03:10 +000079 wiringPiISR (0, INT_EDGE_FALLING, &myInterrupt0) ;
80 wiringPiISR (1, INT_EDGE_FALLING, &myInterrupt1) ;
81 wiringPiISR (2, INT_EDGE_FALLING, &myInterrupt2) ;
82 wiringPiISR (3, INT_EDGE_FALLING, &myInterrupt3) ;
83 wiringPiISR (4, INT_EDGE_FALLING, &myInterrupt4) ;
84 wiringPiISR (5, INT_EDGE_FALLING, &myInterrupt5) ;
85 wiringPiISR (6, INT_EDGE_FALLING, &myInterrupt6) ;
86 wiringPiISR (7, INT_EDGE_FALLING, &myInterrupt7) ;
Gordon Henderson3fbc5642013-03-24 20:04:07 +000087
88 for (;;)
89 {
Phil Howard26c7fe32016-02-27 16:03:10 +000090 gotOne = 0 ;
Gordon Henderson3fbc5642013-03-24 20:04:07 +000091 printf ("Waiting ... ") ; fflush (stdout) ;
92
Phil Howard26c7fe32016-02-27 16:03:10 +000093 for (;;)
94 {
95 for (pin = 0 ; pin < 8 ; ++pin)
96 {
97 if (globalCounter [pin] != myCounter [pin])
98 {
99 printf (" Int on pin %d: Counter: %5d\n", pin, globalCounter [pin]) ;
100 myCounter [pin] = globalCounter [pin] ;
101 ++gotOne ;
102 }
103 }
104 if (gotOne != 0)
105 break ;
106 }
Gordon Henderson3fbc5642013-03-24 20:04:07 +0000107 }
108
109 return 0 ;
110}