Subversion Repositories Code-Repo

Rev

Rev 114 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
113 Kevin 1
#include "maindefs.h"
2
#include "timers.h"
3
#include "msg_queues.h"
4
#include "uart.h"
5
#include "i2c.h"
6
#include "adc.h"
7
#include "interrupts.h"
8
#include "xbee.h"
9
#include "pin_interrupts.h"
10
 
11
//----------------------------------------------------------------------------
12
// Note: This code for processing interrupts is configured to allow for high and
13
//       low priority interrupts.  The high priority interrupt can interrupt the
14
//       the processing of a low priority interrupt.  However, only one of each type
15
//       can be processed at the same time.  It is possible to enable nesting of low
16
//       priority interrupts, but this code is not setup for that and this nesting 
17
//       is not enabled.
18
 
19
void interrupt_init() {
20
    // Peripheral interrupts can have their priority set to high or low
21
    // Decide on the priority of the enabled peripheral interrupts (0 is low, 1 is high)
22
 
23
    // High priority interrupts
24
    IPR3bits.RC2IP = 1; // USART2 RX interrupt
25
    IPR1bits.SSPIP = 1; // I2C interrupt
26
 
27
    // Low priority interrupts
28
    //    IPR1bits.RC1IP = 0;      // USART1 RX interrupt
29
    INTCON2bits.TMR0IP = 0; // Timer0 interrupt
30
    IPR1bits.TMR1IP = 0;    // Timer1 interrupt
115 Kevin 31
    IPR1bits.TMR2IP = 0;    // Timer2 interrupt
113 Kevin 32
    IPR2bits.TMR3IP = 0;    // Timer3 interrupt
33
    //    IPR1bits.ADIP = 0;      // ADC interupt
34
    INTCON2bits.RBIP = 0;   // Port B interrupt
35
    INTCON3bits.INT1IP = 0; // INT1 interrupt
36
 
37
    // Enable I2C interrupt
38
    PIE1bits.SSPIE = 1;
39
    // Enable Port B interrupt
40
    INTCONbits.RBIE = 1;
114 Kevin 41
#ifdef _BASE_STATION
113 Kevin 42
    // Enable interrupt for INT1
43
    INTCON3bits.INT1IE = 1;
114 Kevin 44
#endif
113 Kevin 45
}
46
 
47
void interrupt_enable() {
48
    // Peripheral interrupts can have their priority set to high or low.
49
    // Enable both high-priority interrupts and low-priority interrupts
50
    RCONbits.IPEN = 1;
51
    INTCONbits.GIEH = 1;
52
    INTCONbits.GIEL = 1;
53
}
54
 
55
int interrupt_in_high_interrupt_routine() {
56
    return (!INTCONbits.GIEH);
57
}
58
 
59
int interrupt_low_int_active() {
60
    return (!INTCONbits.GIEL);
61
}
62
 
63
int interrupt_in_low_interrupt_routine() {
64
    if (INTCONbits.GIEL == 1) {
65
        return (0);
66
    } else if (interrupt_in_high_interrupt_routine()) {
67
        return (0);
68
    } else {
69
        return (1);
70
    }
71
}
72
 
73
int interrupt_in_main_routine() {
74
    if ((!interrupt_in_low_interrupt_routine()) && (!interrupt_in_high_interrupt_routine())) {
75
        return (1);
76
    } else {
77
        return (0);
78
    }
79
}
80
 
81
// Set up the interrupt vectors
82
void InterruptHandlerHigh();
83
void InterruptHandlerLow();
84
 
85
#pragma code InterruptVectorLow = 0x18
86
 
87
void InterruptVectorLow(void) {
88
    _asm
89
    goto InterruptHandlerLow //jump to interrupt routine
90
            _endasm
91
}
92
 
93
#pragma code InterruptVectorHigh = 0x08
94
 
95
void InterruptVectorHigh(void) {
96
    _asm
97
    goto InterruptHandlerHigh //jump to interrupt routine
98
            _endasm
99
}
100
 
101
//----------------------------------------------------------------------------
102
// High priority interrupt routine
103
// this parcels out interrupts to individual handlers
104
 
105
#pragma code
106
#pragma interrupt InterruptHandlerHigh
107
 
108
void InterruptHandlerHigh() {
109
    // We need to check the interrupt flag of each enabled high-priority interrupt to
110
    //  see which device generated this interrupt.  Then we can call the correct handler.
111
 
112
    // Check to see if we have an interrupt on USART2 RX
113
    if (PIR3bits.RC2IF) {
114
        DBG_PRINT_INT("INT: UART2 RX\r\n");
115
        // Call the interrupt handler
116
        uart_recv_interrupt_handler();
117
 
118
        // Clear the interrupt flag
119
        PIR3bits.RC2IF = 0;
120
    }
121
 
122
//    // Nofity the xbee to stop sending serial data
123
//    xbee_set_RTS(1);
124
 
125
    // Check to see if we have an I2C interrupt
126
    if (PIR1bits.SSPIF) {
127
        DBG_PRINT_INT("INT: I2C\r\n");
128
        // Call the handler
129
        i2c_interrupt_handler();
130
 
131
        // Clear the interrupt flag
132
        PIR1bits.SSPIF = 0;
133
    }
134
 
135
//     //Notify xbee to resume sending serial data
136
//    xbee_set_RTS(0);
137
 
138
    // The *last* thing I do here is check to see if we can
139
    // allow the processor to go to sleep
140
    // This code *DEPENDS* on the code in messages.c being
141
    // initialized using "init_queues()" -- if you aren't using
142
    // this, then you shouldn't have this call here
143
    //    MQ_sleep_high_interrupt_if_okay();
144
}
145
 
146
//----------------------------------------------------------------------------
147
// Low priority interrupt routine
148
// this parcels out interrupts to individual handlers
149
#pragma code
150
#pragma interruptlow InterruptHandlerLow
151
// This works the same way as the "High" interrupt handler
152
 
153
void InterruptHandlerLow() {
154
    // Check to see if we have an interrupt on INT1
155
    if (INTCON3bits.INT1IF) {
156
        DBG_PRINT_INT("INT: INT1\r\n");
115 Kevin 157
         // Turn off INT1 interrupts
158
        INTCON3bits.INT1IE = 0;
159
 
160
        // Call the handler
113 Kevin 161
        int1_interrupt_handler();
162
 
115 Kevin 163
        // Clear this interrupt flag
113 Kevin 164
        INTCON3bits.INT1IF = 0;
165
    }
166
 
167
    // Check to see if we have an interrupt on any port B inputs <4:7>
168
    if (INTCONbits.RBIF) {
169
        DBG_PRINT_INT("INT: Port B\r\n");
115 Kevin 170
 
171
        // Call the handler
113 Kevin 172
        port_b_int_interrupt_handler();
173
 
115 Kevin 174
        // Clear this interrupt flag
113 Kevin 175
        INTCONbits.RBIF = 0;
176
    }
177
 
178
    // Check to see if we have an interrupt on timer 0
179
    if (INTCONbits.TMR0IF) {
180
        DBG_PRINT_INT("INT: Timer 0\r\n");
181
        // Call the handler
182
        timer0_interrupt_handler();
183
 
184
        // Clear this interrupt flag
185
        INTCONbits.TMR0IF = 0;
186
    }
187
 
188
    // Check to see if we have an interrupt on timer 1
189
    if (PIR1bits.TMR1IF) {
190
        // Call the interrupt handler
191
        timer1_interrupt_handler();
192
 
193
        // Clear the interrupt flag
194
        PIR1bits.TMR1IF = 0;
195
    }
196
 
115 Kevin 197
    // Check to see if we have an interrupt on timer 2
198
    if (PIR1bits.TMR2IF) {
199
        // Call the interrupt handler
200
        timer2_interrupt_handler();
201
 
202
        // Clear the interrupt flag
203
        PIR1bits.TMR2IF = 0;
204
    }
205
 
113 Kevin 206
    // Check to see if we have an interrupt on timer 3
207
    if (PIR2bits.TMR3IF) {
208
        DBG_PRINT_INT("INT: Timer 3\r\n");
209
        timer3_interrupt_handler();
210
 
211
        PIR2bits.TMR3IF = 0;
212
    }
213
 
214
//        // Check to see if we have an interrupt on USART1 RX
215
//        if (PIR1bits.RC1IF) {
216
//            // Call the interrupt handler
217
//            uart_recv_interrupt_handler();
218
//
219
//            // Clear the interrupt flag
220
//            PIR1bits.RC1IF = 0;
221
//        }
222
 
223
//        // Check to see if we have an interrupt on ADC
224
//        if (PIR1bits.ADIF) {
225
//            // Call the interrupt handler
226
//            adc_interrupt_handler();
227
//
228
//            // Clear the interrupt flag
229
//            PIR1bits.ADIF = 0;
230
//        }
231
}
232