Simvik

ADC timing from the electrical characteristics

Pick the ADC prescaler and sampling time from the datasheet's limits, and work out the conversion time. A hard embedded C problem (Reading datasheets and…

hard · Reading datasheets and schematics · datasheets · adc · clocks · timing

The datasheet's ADC section says:

  • f<sub>ADC</sub> must not exceed 14 MHz. The ADC clock is the bus clock divided by the prescaler (ADC_DIV, provided).
  • t<sub>S</sub> (sampling time) is chosen per channel from the SMP table. With the sensor's source impedance, the electrical characteristics table requires t<sub>S</sub> of at least min_sample_ns.
  • t<sub>CONV</sub> = t<sub>S</sub> + 12.5 ADC clock cycles.

Write adc_plan(pclk_hz, min_sample_ns, out):

  1. Pick the smallest prescaler that keeps f<sub>ADC</sub> at or below 14 MHz (a faster ADC clock gives faster conversions).
  2. With that clock, pick the shortest sampling time that is at least min_sample_ns.
  3. Fill out with both codes and t<sub>CONV</sub> in nanoseconds, rounded up, and return 0. If either choice is impossible, return -1.

Do the timing comparisons exactly in integers: the half-cycle table avoids the .5s, and 64-bit products avoid overflow.

Starter code

int adc_plan(uint32_t pclk_hz, uint32_t min_sample_ns, adc_plan_t *out) {
  (void)min_sample_ns;
  out->adcpre = 0;                                     // the fastest clock
  out->smp = 0;                                        // the shortest sampling time
  out->conv_ns = 14000u / (pclk_hz / 2000000u);        // 14 cycles
  return 0;
}

Example tests

72 MHz bus, a 1 µs sampling requirement

show(72000000, 1000);

Expected output:

72 MHz, sample >= 1000 ns: /6, SMP 2 (13.5 cycles), 2167 ns

Two more boards

show(48000000, 0);
show(56000000, 5000);

Expected output:

48 MHz, sample >= 0 ns: /4, SMP 0 (1.5 cycles), 1167 ns
56 MHz, sample >= 5000 ns: /4, SMP 6 (71.5 cycles), 6000 ns

Learn it first

More Reading datasheets and schematics problems