/* Set lowest ADC to be start in sequence and highest to be interrupt enabled and set end-of-sequence on the highest active ADC */ static void sethilo(void) { int c; /* Clear start of sequence */ ADC12CTL1 &= ~(CSTARTADD_15); /* Set new start of sequence to lowest active memory holder */ for(c = 0; c < 8; c++) { if(adcflags & (1 << c)) { ADC12CTL1 |= (c * CSTARTADD_1); break; } } /* Clear all interrupts and end-of-sequences */ ADC12IE = 0; for(c = 0; c < 8; c++) { ADC12MCTL_NO(c) &= ~EOS; } /* Set highest interrupt and end-of-sequence. This will generate one interrupt for each sequence of conversions. */ for(c = 0; c < 8; c++) { if(adcflags & (128 >> c)) { ADC12IE |= 128 >> c; ADC12MCTL_NO(7 - c) |= EOS; break; } } }
/*---------------------------------------------------------------------------*/ void irq_adc12_deactivate(const struct sensors_sensor *sensor, unsigned char adcno) { /* stop converting */ ADC12CTL0 &= ~ENC; /* wait for conversion to stop */ while(ADC12CTL0 & ADC12BUSY); ADC12CTL0 &= ~(ADC12ON | REFON); ADC12IE = 0; /* clear any pending interrupts */ ADC12IFG = 0; adcflags &= ~(1 << adcno); ADC12MCTL_NO(adcno) = 0; sethilo(); sensors_remove_irq(sensor, IRQ_ADC); if(adcflags) { /* Turn on the ADC12 */ ADC12CTL0 |= (ADC12ON | REFON); /* Delay */ clock_delay(20000); /* Still active. Turn on the conversion. */ ADC12CTL0 |= ENC | ADC12SC; } }
/*---------------------------------------------------------------------------*/ void irq_adc12_activate(const struct sensors_sensor *sensor, unsigned char adcno, unsigned char config) { /* stop converting */ ADC12CTL0 &= ~ENC; /* wait for conversion to stop */ while(ADC12CTL0 & ADC12BUSY); ADC12CTL0 &= ~(ADC12ON | REFON); ADC12IE = 0; /* clear any pending interrupts */ ADC12IFG = 0; adcflags |= (1 << adcno); ADC12MCTL_NO(adcno) = config; sethilo(); ADC12CTL0 |= ADC12ON | REFON; sensors_add_irq(sensor, IRQ_ADC); /* Delay */ clock_delay(20000); ADC12CTL0 |= ENC | ADC12SC; }
/*---------------------------------------------------------------------------*/ static CC_INLINE void start(void) { uint16_t c, last; /* Set up the ADC. */ P6DIR = 0xff; P6OUT = 0x00; /* Setup ADC12, ref., sampling time */ /* XXX Note according to the specification a minimum of 17 ms should be allowed after turn on of the internal reference generator. */ ADC12CTL0 = REF2_5V + SHT0_6 + SHT1_6 + MSC + REFON; /* Use sampling timer, repeat-sequence-of-channels */ ADC12CTL1 = SHP + CONSEQ_3; last = 15; for(c = 0; c < 16; c++) { /* Clear all end-of-sequences */ ADC12MCTL_NO(c) &= ~EOS; if(adc_on & (1 << c)) { if(last == 15) { /* Set new start of sequence to lowest active memory holder */ ADC12CTL1 |= (c * CSTARTADD_1); } last = c; } } /* Set highest end-of-sequence. */ ADC12MCTL_NO(last) |= EOS; ADC12CTL0 |= ADC12ON; ADC12CTL0 |= ENC; /* enable conversion */ ADC12CTL0 |= ADC12SC; /* sample & convert */ }
/*---------------------------------------------------------------------------*/ int adc_sensors_configure(uint16_t input, uint8_t ref, int type, int value) { uint16_t c; if(type == SENSORS_ACTIVE) { stop(); if(value) { adc_on |= input; P6SEL |= input & 0xff; /* Set ADC config */ for(c = 0; c < 16; c++) { if(input & (1 << c)) { ADC12MCTL_NO(c) = (c * INCH_1) | ref; } } } else { adc_on &= ~input; ready &= ~input; P6SEL &= ~(input & 0xff); } if(adc_on == 0) { P6DIR = 0x00; P6SEL = 0x00; /* Turn off ADC and internal reference generator */ ADC12CTL0 = 0; ADC12CTL1 = 0; } else { start(); } return 1; } return 0; }