Simvik

Apply the errata sheet's workarounds

An ADC driver that follows the reference manual still reads wrong values. The errata sheet explains why. A medium embedded C problem (Reading datasheets and…

medium · Reading datasheets and schematics · datasheets · errata · adc · drivers

To save power, the ADC is kept off between readings. Following the reference manual, adc_read(ch) enables the ADC, selects the channel, starts a conversion, waits while BUSY is set, reads DR and disables the ADC again. The first reading after each enable is often wrong by a few hundred counts.

The chip's errata sheet lists two problems with this silicon revision:

2.4.1 First conversion after enabling the ADC is inaccurate. The result of the first conversion after EN is set can be off by up to ±200 LSB. Workaround: after enabling the ADC, perform one conversion and discard its result. 2.4.3 Writes to CHSEL are ignored while a conversion is in progress. Workaround: write CHSEL only while BUSY is clear.

Fix adc_read so that it returns correct results, and leaves the ADC disabled.

Starter code

uint16_t adc_read(int ch) {
  adc_enable();
  adc_select(ch);
  adc_start();
  while (adc_busy()) { }
  uint16_t v = adc_data();
  adc_disable();
  return v;
}

Example tests

Each channel once

int chs[] = { 0, 1, 2, 3 };
read_all(chs, 4);

Expected output:

AIN0: 1000
AIN1: 2048
AIN2: 3071
AIN3: 4000
ADC off

Learn it first

More Reading datasheets and schematics problems