Subversion Repositories Code-Repo

Rev

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