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
//    INTCON2bits.TMR0IP = 0; // Timer0 interrupt
147 Kevin 30
    IPR1bits.TMR1IP = 0;    // Timer1 interrupt
119 Kevin 31
//    IPR2bits.TMR3IP = 0; // Timer 3 interrupt
147 Kevin 32
//    IPR1bits.ADIP = 0;      // ADC interupt
119 Kevin 33
//    INTCON2bits.RBIP = 0;   // Port B interrupt
34
//    INTCON3bits.INT1IP = 0; // INT1 interrupt
35
 
36
    // Enable Port B interrupt
37
//    INTCONbits.RBIE = 1;
38
    // Enable interrupt for INT1
39
//    INTCON3bits.INT1IE = 1;
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;
47
    INTCONbits.GIEL = 1;
48
}
49
 
150 Kevin 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
//}
119 Kevin 75
 
76
// Set up the interrupt vectors
77
void InterruptHandlerHigh();
78
void InterruptHandlerLow();
79
 
80
#pragma code InterruptVectorLow = 0x18
81
 
82
void InterruptVectorLow(void) {
83
    _asm
84
    goto InterruptHandlerLow //jump to interrupt routine
85
            _endasm
86
}
87
 
88
#pragma code InterruptVectorHigh = 0x08
89
 
90
void InterruptVectorHigh(void) {
91
    _asm
92
    goto InterruptHandlerHigh //jump to interrupt routine
93
            _endasm
94
}
95
 
96
//----------------------------------------------------------------------------
97
// High priority interrupt routine
98
// this parcels out interrupts to individual handlers
99
 
100
#pragma code
101
#pragma interrupt InterruptHandlerHigh
102
 
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.
106
 
147 Kevin 107
//    // Check to see if we have an SPI2 interrupt
108
//    if (PIR3bits.SSP2IF) {
109
//        // Call the handler
110
//        SPI2_Recv_Interrupt_Handler();
111
//
112
//        // Clear the interrupt flag
113
//        PIR3bits.SSP2IF = 0;
114
//
115
//        return;
116
//    }
121 Kevin 117
 
118
    // Check to see if we have an I2C interrupt
119
    if (PIR1bits.SSPIF) {
120
        // Call the handler
121
        I2C_Interrupt_Handler();
122
 
123
        // Clear the interrupt flag
124
        PIR1bits.SSPIF = 0;
125
 
126
        return;
127
    }
128
 
119 Kevin 129
    // Check to see if we have an interrupt on USART1 RX
130
    if (PIR1bits.RC1IF) {
131
        // Call the interrupt handler
132
        UART1_Recv_Interrupt_Handler();
133
 
134
        // Clear the interrupt flag
135
        PIR1bits.RC1IF = 0;
136
 
137
        return;
138
    }
139
 
121 Kevin 140
#ifndef _DEBUG  // Disable UART1 TX interrupt for debug mode (using printf)
119 Kevin 141
    // Check to see if we have an interrupt on USART1 TX
142
    if (PIR1bits.TX1IF) {
143
        // Call the interrupt handler
144
        UART1_Send_Interrupt_Handler();
145
 
146
        // Clear the interrupt flag
147
        PIR1bits.TX1IF = 0;
148
 
149
        return;
150
    }
121 Kevin 151
#endif
119 Kevin 152
 
153
//    // Check to see if we have an interrupt on USART2 RX
154
//    if (PIR3bits.RC2IF) {
155
//        DBG_PRINT_INT("INT: UART2 RX\r\n");
156
//        // Call the interrupt handler
157
//        uart_2_recv_interrupt_handler();
158
//
159
//        // Clear the interrupt flag
160
//        PIR3bits.RC2IF = 0;
161
//    }
162
}
163
 
164
//----------------------------------------------------------------------------
165
// Low priority interrupt routine
166
// this parcels out interrupts to individual handlers
167
#pragma code
168
#pragma interruptlow InterruptHandlerLow
169
// This works the same way as the "High" interrupt handler
170
 
171
void InterruptHandlerLow() {
172
//    // Check to see if we have an interrupt on INT1
173
//    if (INTCON3bits.INT1IF) {
174
//        DBG_PRINT_INT("INT: INT1\r\n");
175
//        int1_interrupt_handler();
176
//
177
//        INTCON3bits.INT1IF = 0;
178
//    }
179
 
180
//    // Check to see if we have an interrupt on any port B inputs <4:7>
181
//    if (INTCONbits.RBIF) {
182
//        DBG_PRINT_INT("INT: Port B\r\n");
183
//        port_b_int_interrupt_handler();
184
//
185
//        INTCONbits.RBIF = 0;
186
//    }
187
 
188
//    // Check to see if we have an interrupt on timer 0
189
//    if (INTCONbits.TMR0IF) {
190
//        DBG_PRINT_INT("INT: Timer 0\r\n");
191
//        // Call the handler
192
//        timer0_interrupt_handler();
193
//
194
//        // Clear this interrupt flag
195
//        INTCONbits.TMR0IF = 0;
196
//    }
197
 
147 Kevin 198
    // Check to see if we have an interrupt on timer 1
199
    if (PIR1bits.TMR1IF) {
200
        // Call the interrupt handler
201
        Timer1_Interrupt_Handler();
119 Kevin 202
 
147 Kevin 203
        // Clear the interrupt flag
204
        PIR1bits.TMR1IF = 0;
205
    }
206
 
119 Kevin 207
//    // Check to see if we have an interrupt on timer 3
208
//    if (PIR2bits.TMR3IF) {
209
//        DBG_PRINT_INT("INT: Timer 3\r\n");
210
//        timer3_interrupt_handler();
211
//
212
//        PIR2bits.TMR3IF = 0;
213
//    }
214
 
147 Kevin 215
//    // Check to see if we have an interrupt on ADC
216
//    if (PIR1bits.ADIF) {
217
//        // Call the interrupt handler
218
//        ADC_Interrupt_Handler();
219
//
220
//        // Clear the interrupt flag
221
//        PIR1bits.ADIF = 0;
222
//    }
119 Kevin 223
}
224