Subversion Repositories Code-Repo

Rev

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