Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

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