Subversion Repositories Code-Repo

Rev

Rev 113 | Go to most recent revision | 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
31
    IPR2bits.TMR3IP = 0;    // Timer3 interrupt
32
    //    IPR1bits.ADIP = 0;      // ADC interupt
33
    INTCON2bits.RBIP = 0;   // Port B interrupt
34
    INTCON3bits.INT1IP = 0; // INT1 interrupt
35
 
36
    // Enable I2C interrupt
37
    PIE1bits.SSPIE = 1;
38
    // Enable Port B interrupt
39
    INTCONbits.RBIE = 1;
114 Kevin 40
#ifdef _BASE_STATION
113 Kevin 41
    // Enable interrupt for INT1
42
    INTCON3bits.INT1IE = 1;
114 Kevin 43
#endif
113 Kevin 44
}
45
 
46
void interrupt_enable() {
47
    // Peripheral interrupts can have their priority set to high or low.
48
    // Enable both high-priority interrupts and low-priority interrupts
49
    RCONbits.IPEN = 1;
50
    INTCONbits.GIEH = 1;
51
    INTCONbits.GIEL = 1;
52
}
53
 
54
int interrupt_in_high_interrupt_routine() {
55
    return (!INTCONbits.GIEH);
56
}
57
 
58
int interrupt_low_int_active() {
59
    return (!INTCONbits.GIEL);
60
}
61
 
62
int interrupt_in_low_interrupt_routine() {
63
    if (INTCONbits.GIEL == 1) {
64
        return (0);
65
    } else if (interrupt_in_high_interrupt_routine()) {
66
        return (0);
67
    } else {
68
        return (1);
69
    }
70
}
71
 
72
int interrupt_in_main_routine() {
73
    if ((!interrupt_in_low_interrupt_routine()) && (!interrupt_in_high_interrupt_routine())) {
74
        return (1);
75
    } else {
76
        return (0);
77
    }
78
}
79
 
80
// Set up the interrupt vectors
81
void InterruptHandlerHigh();
82
void InterruptHandlerLow();
83
 
84
#pragma code InterruptVectorLow = 0x18
85
 
86
void InterruptVectorLow(void) {
87
    _asm
88
    goto InterruptHandlerLow //jump to interrupt routine
89
            _endasm
90
}
91
 
92
#pragma code InterruptVectorHigh = 0x08
93
 
94
void InterruptVectorHigh(void) {
95
    _asm
96
    goto InterruptHandlerHigh //jump to interrupt routine
97
            _endasm
98
}
99
 
100
//----------------------------------------------------------------------------
101
// High priority interrupt routine
102
// this parcels out interrupts to individual handlers
103
 
104
#pragma code
105
#pragma interrupt InterruptHandlerHigh
106
 
107
void InterruptHandlerHigh() {
108
    // We need to check the interrupt flag of each enabled high-priority interrupt to
109
    //  see which device generated this interrupt.  Then we can call the correct handler.
110
 
111
    // Check to see if we have an interrupt on USART2 RX
112
    if (PIR3bits.RC2IF) {
113
        DBG_PRINT_INT("INT: UART2 RX\r\n");
114
        // Call the interrupt handler
115
        uart_recv_interrupt_handler();
116
 
117
        // Clear the interrupt flag
118
        PIR3bits.RC2IF = 0;
119
    }
120
 
121
//    // Nofity the xbee to stop sending serial data
122
//    xbee_set_RTS(1);
123
 
124
    // Check to see if we have an I2C interrupt
125
    if (PIR1bits.SSPIF) {
126
        DBG_PRINT_INT("INT: I2C\r\n");
127
        // Call the handler
128
        i2c_interrupt_handler();
129
 
130
        // Clear the interrupt flag
131
        PIR1bits.SSPIF = 0;
132
    }
133
 
134
//     //Notify xbee to resume sending serial data
135
//    xbee_set_RTS(0);
136
 
137
    // The *last* thing I do here is check to see if we can
138
    // allow the processor to go to sleep
139
    // This code *DEPENDS* on the code in messages.c being
140
    // initialized using "init_queues()" -- if you aren't using
141
    // this, then you shouldn't have this call here
142
    //    MQ_sleep_high_interrupt_if_okay();
143
}
144
 
145
//----------------------------------------------------------------------------
146
// Low priority interrupt routine
147
// this parcels out interrupts to individual handlers
148
#pragma code
149
#pragma interruptlow InterruptHandlerLow
150
// This works the same way as the "High" interrupt handler
151
 
152
void InterruptHandlerLow() {
153
    // Check to see if we have an interrupt on INT1
154
    if (INTCON3bits.INT1IF) {
155
        DBG_PRINT_INT("INT: INT1\r\n");
156
        int1_interrupt_handler();
157
 
158
        INTCON3bits.INT1IF = 0;
159
    }
160
 
161
    // Check to see if we have an interrupt on any port B inputs <4:7>
162
    if (INTCONbits.RBIF) {
163
        DBG_PRINT_INT("INT: Port B\r\n");
164
        port_b_int_interrupt_handler();
165
 
166
        INTCONbits.RBIF = 0;
167
    }
168
 
169
    // Check to see if we have an interrupt on timer 0
170
    if (INTCONbits.TMR0IF) {
171
        DBG_PRINT_INT("INT: Timer 0\r\n");
172
        // Call the handler
173
        timer0_interrupt_handler();
174
 
175
        // Clear this interrupt flag
176
        INTCONbits.TMR0IF = 0;
177
    }
178
 
179
    // Check to see if we have an interrupt on timer 1
180
    if (PIR1bits.TMR1IF) {
181
        // Call the interrupt handler
182
        timer1_interrupt_handler();
183
 
184
        // Clear the interrupt flag
185
        PIR1bits.TMR1IF = 0;
186
    }
187
 
188
    // Check to see if we have an interrupt on timer 3
189
    if (PIR2bits.TMR3IF) {
190
        DBG_PRINT_INT("INT: Timer 3\r\n");
191
        timer3_interrupt_handler();
192
 
193
        PIR2bits.TMR3IF = 0;
194
    }
195
 
196
//        // Check to see if we have an interrupt on USART1 RX
197
//        if (PIR1bits.RC1IF) {
198
//            // Call the interrupt handler
199
//            uart_recv_interrupt_handler();
200
//
201
//            // Clear the interrupt flag
202
//            PIR1bits.RC1IF = 0;
203
//        }
204
 
205
//        // Check to see if we have an interrupt on ADC
206
//        if (PIR1bits.ADIF) {
207
//            // Call the interrupt handler
208
//            adc_interrupt_handler();
209
//
210
//            // Clear the interrupt flag
211
//            PIR1bits.ADIF = 0;
212
//        }
213
}
214