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