summaryrefslogtreecommitdiffstats
path: root/lib/msp430/WInterrupts.c
blob: e0fba177eab66ef1d08d3640a12e7aef69d1f42b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
  ************************************************************************
  *	WInterrupts.c
  *
  *	Arduino core files for MSP430
  *		Copyright (c) 2012 Robert Wessels. All right reserved.
  *
  *
  ***********************************************************************
  Derived from:
 
  WInterrupts.c Part of the Wiring project - http://wiring.uniandes.edu.co

  Copyright (c) 2004-05 Hernando Barragan

  This library is free software; you can redistribute it and/or
  modify it under the terms of the GNU Lesser General Public
  License as published by the Free Software Foundation; either
  version 2.1 of the License, or (at your option) any later version.

  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  Lesser General Public License for more details.

  You should have received a copy of the GNU Lesser General
  Public License along with this library; if not, write to the
  Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  Boston, MA  02111-1307  USA
  
  Modified 24 November 2006 by David A. Mellis
  Modified 1 August 2010 by Mark Sproul
*/

#include <inttypes.h>
#include <stdio.h>

#include "wiring_private.h"

#ifndef BV
#define BV(x) (1 << (x))
#endif

#define bit_pos(A) ((A) == 1u << 0 ? 0 \
: (A) == 1u << 1 ? 1 \
: (A) == 1u << 2 ? 2 \
: (A) == 1u << 3 ? 3 \
: (A) == 1u << 4 ? 4 \
: (A) == 1u << 5 ? 5 \
: (A) == 1u << 6 ? 6 \
: (A) == 1u << 7 ? 7 \
: 0)

#define NUM_INTS_PER_PORT 8
static volatile voidFuncPtr intFuncP1[NUM_INTS_PER_PORT];
#if defined(__MSP430_HAS_PORT2_R__)
static volatile voidFuncPtr intFuncP2[NUM_INTS_PER_PORT];
#endif

void attachInterrupt(uint8_t interruptNum, void (*userFunc)(void), int mode) {
 	uint8_t bit = digitalPinToBitMask(interruptNum);
	uint8_t port = digitalPinToPort(interruptNum);
        	
        
        if ((port == NOT_A_PIN) || !((mode == FALLING) || (mode == RISING))) return;
        
        __dint();
        
        switch(port) {
                case P1:
        		P1IE |= bit;
	        	P1IFG &= ~bit;
		        P1IES = mode ? P1IES | bit : P1IES & ~bit;
                        intFuncP1[bit_pos(bit)] = userFunc;
                        break;
                #if defined(__MSP430_HAS_PORT2_R__)
                case P2:
        		P2IE |= bit;
	        	P2IFG &= ~bit;
		        P2IES = mode ? P2IES | bit : P2IES & ~bit;
	                intFuncP2[bit_pos(bit)] = userFunc;
                        break;
                #endif
                default:
                        break;
        }
       
        __eint();
}

void detachInterrupt(uint8_t interruptNum) {
 	uint8_t bit = digitalPinToBitMask(interruptNum);
	uint8_t port = digitalPinToPort(interruptNum);
	
        if (port == NOT_A_PIN) return;
        
        switch(port) {
                case P1:
        		P1IE &= ~bit;
		        intFuncP1[bit_pos(bit)] = 0;
                        break;
                #if defined(__MSP430_HAS_PORT2_R__)
                case P2:
        		P2IE &= ~bit;
		        intFuncP2[bit_pos(bit)] = 0;
                        break;
                #endif
                default:
                        break;
        } 
}


__attribute__((interrupt(PORT1_VECTOR)))
void Port_1(void)
{
	uint8_t i;
	for(i = 0; i < 8; i++) {
		if((P1IFG & BV(i)) && intFuncP1[i]) {
			intFuncP1[i]();
			P1IFG &= ~BV(i);
		}
	}
}

#if defined(__MSP430_HAS_PORT2_R__)
__attribute__((interrupt(PORT2_VECTOR)))
void Port_2(void)
{
	uint8_t i;
	for(i = 0; i < 8; i++) {
		if((P2IFG & BV(i)) && intFuncP2[i]) {
			intFuncP2[i]();
			P2IFG &= ~BV(i);
		}
	}
}
#endif