Subversion Repositories Code-Repo

Rev

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

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