Subversion Repositories Code-Repo

Rev

Details | Last modification | View Log | RSS feed

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