irq.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033 #include "lib/sensors.h"
00034 #include "dev/irq.h"
00035 #include "dev/lpm.h"
00036 #include <io.h>
00037 #include <signal.h>
00038
00039 #define ADC12MCTL_NO(adcno) ((unsigned char *) ADC12MCTL0_)[adcno]
00040
00041 static int (* adc12_irq[8])(void);
00042 static int (* port1_irq[8])(void);
00043 static unsigned char adcflags;
00044
00045
00046 interrupt(PORT1_VECTOR)
00047 irq_p1(void)
00048 {
00049 int i;
00050 ENERGEST_ON(ENERGEST_TYPE_IRQ);
00051 for(i = 0; i < 8; i++) {
00052 if((P1IFG & (1 << i)) && port1_irq[i] != NULL) {
00053 if((port1_irq[i])()) {
00054 LPM4_EXIT;
00055 }
00056 }
00057 }
00058 P1IFG = 0x00;
00059 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00060 }
00061
00062 interrupt (ADC_VECTOR)
00063 irq_adc(void)
00064 {
00065 int i;
00066 ENERGEST_ON(ENERGEST_TYPE_IRQ);
00067 for(i = 0; i < 8; i++) {
00068 if(adc12_irq[i] != NULL) {
00069 if((adc12_irq[i])()) {
00070 LPM4_EXIT;
00071 }
00072 }
00073 }
00074 ENERGEST_OFF(ENERGEST_TYPE_IRQ);
00075 }
00076
00077 void
00078 irq_init(void)
00079 {
00080 int i;
00081 adcflags = 0;
00082 for(i = 0; i < 8; i++) {
00083 adc12_irq[i] = NULL;
00084 port1_irq[i] = NULL;
00085 }
00086
00087 ADC12CTL0 = REF2_5V | SHT0_10 | SHT1_10 | MSC;
00088
00089
00090
00091 ADC12CTL1 = SHP | CONSEQ_3 | ADC12DIV_7;
00092 }
00093
00094 void
00095 irq_port1_activate(unsigned char irqno, int (* irq)(void))
00096 {
00097 if(irqno < 8) {
00098 port1_irq[irqno] = irq;
00099 }
00100 }
00101
00102 void
00103 irq_port1_deactivate(unsigned char irqno)
00104 {
00105 if(irqno < 8) {
00106 port1_irq[irqno] = NULL;
00107 }
00108 }
00109
00110
00111
00112 static void
00113 sethilo(void)
00114 {
00115 int c;
00116
00117
00118 ADC12CTL1 &= ~(CSTARTADD_15);
00119
00120
00121 for(c = 0; c < 8; c++) {
00122 if(adcflags & (1 << c)) {
00123 ADC12CTL1 |= (c * CSTARTADD_1);
00124 break;
00125 }
00126 }
00127
00128
00129 ADC12IE = 0;
00130 for(c = 0; c < 8; c++) {
00131 ADC12MCTL_NO(c) &= ~EOS;
00132 }
00133
00134
00135
00136 for(c = 0; c < 8; c++) {
00137 if(adcflags & (128 >> c)) {
00138 ADC12IE |= 128 >> c;
00139 ADC12MCTL_NO(7 - c) |= EOS;
00140 break;
00141 }
00142 }
00143 }
00144
00145 void
00146 irq_adc12_activate(unsigned char adcno, unsigned char config,
00147 int (* irq)(void))
00148 {
00149 if(adcno >= 8) {
00150 return;
00151 }
00152
00153 ADC12CTL0 &= ~ENC;
00154
00155 while(ADC12CTL0 & ADC12BUSY);
00156 ADC12CTL0 &= ~(ADC12ON | REFON);
00157 ADC12IE = 0;
00158
00159
00160 ADC12IFG = 0;
00161
00162 adcflags |= (1 << adcno);
00163
00164 ADC12MCTL_NO(adcno) = config;
00165
00166 sethilo();
00167
00168 ADC12CTL0 |= ADC12ON | REFON;
00169
00170 adc12_irq[adcno] = irq;
00171
00172
00173 clock_delay(20000);
00174
00175 ADC12CTL0 |= ENC | ADC12SC;
00176 }
00177
00178 void
00179 irq_adc12_deactivate(unsigned char adcno)
00180 {
00181 if(adcno >= 8) {
00182 return;
00183 }
00184
00185 ADC12CTL0 &= ~ENC;
00186
00187 while(ADC12CTL0 & ADC12BUSY);
00188 ADC12CTL0 &= ~(ADC12ON | REFON);
00189 ADC12IE = 0;
00190
00191
00192 ADC12IFG = 0;
00193
00194 adcflags &= ~(1 << adcno);
00195
00196 ADC12MCTL_NO(adcno) = 0;
00197
00198 sethilo();
00199
00200 adc12_irq[adcno] = NULL;
00201
00202 if(adcflags) {
00203
00204 ADC12CTL0 |= (ADC12ON | REFON);
00205
00206
00207 clock_delay(20000);
00208
00209
00210 ADC12CTL0 |= ENC | ADC12SC;
00211 }
00212 }
00213
00214 int
00215 irq_adc12_active(unsigned char adcno)
00216 {
00217 return adcflags & (1 << adcno) ? 1 : 0;
00218 }
00219