void ADCInit(void) { int channel; /* Only init once */ if (ADC_Buffer) return; /* Enable clock int PMC and reset ADC */ outr(PMC_PCER, _BV(ADC_ID)); // Enable ADC clock in PMC outr(ADC_CR, ADC_SWRST); // Reset bus outr(ADC_CR, 0x00); /* Basic configuration: Disable all channels and set mode and prescaler */ outr(ADC_CHDR, ADC_CH0 | ADC_CH1 | ADC_CH2 | ADC_CH3 | ADC_CH4 | ADC_CH5 | ADC_CH6 | ADC_CH7); ADCSetMode(AT91_ADC_INITIAL_MODE); ADCSetPrescale(AT91_ADC_INITIAL_PRESCALE); /* Init adc buffers. One for every channel as we can sample all by automatic sequence */ ADC_Buffer = NutHeapAlloc(sizeof(uint16_t *) * ADC_MAX_CHANNEL); for (channel = 0; channel < ADC_MAX_CHANNEL; channel ++) { ADC_Buffer[channel] = NutHeapAlloc(sizeof(uint16_t) * AT91_ADC_BUF_SIZE + 2); ADC_Buffer[channel][_adc_buf_head] = 0; ADC_Buffer[channel][_adc_buf_tail] = 0; } if (NutRegisterIrqHandler(&sig_ADC, ADCInterrupt, NULL)) { // We do not free buffer as this would cost ROM and is not likely return; } NutIrqEnable(&sig_ADC); }
void initAnalog(void) { static uint8_t runOnce = 0; if (!runOnce) { runOnce = 1; ADCInit(); ADCSetRef(AVCC); ADCSetPrescale(ADC_PRESCALE_DIV128); } }