3 \author Scott Livingston
5 \brief Simple A/D sampling of a GoodFET pin. Currently assumes
6 x2274 chip, on a GoodFET31 board.
15 //! Handle an ADC10 command; currently assumes x2274, on a GoodFET31 board.
16 void adc_handle_fn( uint8_t const app,
20 // define the adc app's app_t
21 app_t const adc_app = {
33 "\tThe ADC app adds simple A/D sampling of a GoodFET pin.\n"
34 "\tCurrently assumes x2274 chip, on a GoodFET31 board.\n"
39 ADC10CTL0 = ADC10DISABLE;
40 ADC10CTL0 = REFON | REF2_5V | ADC10ON; // internal reference, at 2.5V
41 ADC10CTL1 = INCH_5 | ADC10SSEL_2; // input A5; using MCLK as clock source.
42 ADC10AE0 = 1<<5; // enable channel A5
43 ADC10SA = 0x200; // Start at bottom of RAM (i.e. lowest address).
48 ADC10CTL0 = ADC10DISABLE;
49 ADC10CTL0 = ADC10CTL1 = 0x0000;
53 //! Handle an ADC10 command; currently assumes x2274, on a GoodFET31 board.
54 void adc_handle_fn( uint8_t const app,
73 case ADC10_1SAMPLE: //! Capture and return a single sample.
74 sample = sample_adc10();
75 *cmddata = sample & 0xff;
76 *(cmddata+1) = sample >> 8;
80 case ADC10_NSAMPLE: //! Capture and return a sequence, with constanst Sps.
82 *cmddata = 0xff; // Max length
84 actual_N = nsamples_adc10( 0xff, 3, 8 );
85 } else if (len == 1) {
86 actual_N = nsamples_adc10( *cmddata, 3, 8 );
88 actual_N = nsamples_adc10( *cmddata, *(cmddata+1), *(cmddata+2) );
90 txdata( app, verb, actual_N*2 );
94 debugstr( "Verb unimplemented in ADC10 application." );
104 // We assume the ADC10 module has already been initialized.
106 u16 ctl0_start = ADC10CTL0;
108 ADC10CTL0 |= ADC10SHT_3; /* Switch to longest sample-and-hold time
109 (i.e. 64 ticks), to increase likelihood
110 of success given we are only taking a
111 single sample here. */
112 ADC10CTL0 |= 0x0003; // ENC | ADC10SC
113 while (!(ADC10CTL0 & ADC10IFG)) ;
116 ADC10CTL0 = ctl0_start; /* Return ADC10 control register to original
117 state, at calling of this function. */
122 u16 nsamples_adc10( u8 N_count, u8 t_sample, u8 clock_div )
127 //while (ADC10BUSY) ; // Wait till any pending operation completes.
129 ctl0_start = ADC10CTL0;
130 ctl1_start = ADC10CTL1; // Save control registers states.
132 if (N_count > (CMDDATALEN-4)/2) { // Bound number of samples to be obtained here.
133 N_count = (CMDDATALEN-4)/2;
135 clock_div--; // Place in form appropriate for control register
137 // Additional preparations of ADC10
138 ADC10CTL0 |= (t_sample&0x3)<<11 | MSC;
139 ADC10CTL1 |= (clock_div&0x7)<<5
140 | ADC10SSEL_2 // source from MCLK (should be 16 MHz),
141 | CONSEQ_2; // repeat-single-channel mode.
143 // Setup DTC (to make acquisition of block of samples easy/reliable)
148 ADC10CTL0 |= 0x0003; // ENC | ADC10SC
150 while (!(ADC10CTL0 & ADC10IFG)) ;
152 ADC10CTL0 = ctl0_start; // Restore control registers
153 ADC10CTL1 = ctl1_start;
154 ADC10DTC1 = 0x0000; // ...and disable DTC