Microchip® Advanced Software Framework

xmega/drivers/adc/unit_tests/unit_tests.c File Reference

Unit tests for ADC driver using DAC.

Copyright (c) 2011-2018 Microchip Technology Inc. and its subsidiaries.

#include <stdint.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <asf.h>
#include <conf_test.h>

Macros

ADC unit test definitions
#define NUM_AVERAGE_SAMPLES   4
 Number of samples to average over. More...
 
#define NUM_CHANNELS   4
 Number of ADC channels to use. More...
 
#define DAC_MIN   0
 Minimum value for DAC. More...
 
#define DAC_MAX   0xfff
 Maximum value for DAC. More...
 
#define ADC_ZERO   0
 ADC zero value. More...
 
#define ADC_UNSIGNED_8BIT_MAX   0xff
 Maximum value for ADC in unsigned 8-bit. More...
 
#define ADC_UNSIGNED_12BIT_MAX   0xfff
 Maximum value for ADC in unsigned 12-bit. More...
 
#define ADC_SIGNED_12BIT_MAX   0x7ff
 Maximum value for ADC in signed 12-bit. More...
 
#define ADC_SIGNED_12BIT_MIN   -0x800
 Minimum value for ADC in signed 12-bit. More...
 

Functions

int main (void)
 Run ADC unit tests. More...
 
ADC unit test helper functions
static void single_ended_unsigned_average (ADC_t *adc, uint8_t ch_mask, const uint8_t *mux_inputs, uint8_t num_inputs, uint16_t *results)
 Measure single-ended input MUX combinations on channel. More...
 
static void differential_signed_average (ADC_t *adc, uint8_t ch_mask, const uint8_t *mux_pos_inputs, const uint8_t *mux_neg_inputs, uint8_t num_inputs, int16_t *results, uint8_t gain)
 Measure differential input MUX combinations on channel. More...
 
static void verify_unsigned_result (const struct test_case *test, uint16_t expected, uint16_t actual, uint8_t dac_ch, uint8_t adc_ch, uint8_t mux_input, uint16_t upper_limit, bool value_is_12bit)
 Verify that unsigned ADC result is in acceptable range. More...
 
static void verify_signed_result (const struct test_case *test, int16_t expected, int16_t actual, uint8_t adc_ch, uint8_t mux_pos_input, uint8_t mux_neg_input, int16_t lower_limit, int16_t upper_limit, uint8_t gain, bool value_is_12bit)
 Verify that unsigned ADC result is in acceptable range. More...
 
ADC unit test functions
static void run_single_ended_12bit_conversion_test (const struct test_case *test)
 Test single ended conversion in 12-bit mode using the DAC. More...
 
static void run_single_ended_8bit_conversion_test (const struct test_case *test)
 Test single ended conversion in 8-bit mode using the DAC. More...
 
static void run_differential_12bit_conversion_test (const struct test_case *test)
 Test differential conversion in 12-bit mode using the DAC. More...
 
static void run_differential_12bit_with_gain_conversion_test (const struct test_case *test)
 Test differential conversion with gain in 12-bit mode using the DAC. More...
 

#define ADC_SIGNED_12BIT_MAX   0x7ff

Maximum value for ADC in signed 12-bit.

Referenced by run_differential_12bit_conversion_test(), and run_differential_12bit_with_gain_conversion_test().

#define ADC_SIGNED_12BIT_MIN   -0x800

Minimum value for ADC in signed 12-bit.

Referenced by run_differential_12bit_conversion_test(), and run_differential_12bit_with_gain_conversion_test().

#define ADC_UNSIGNED_12BIT_MAX   0xfff

Maximum value for ADC in unsigned 12-bit.

Referenced by run_single_ended_12bit_conversion_test().

#define ADC_UNSIGNED_8BIT_MAX   0xff

Maximum value for ADC in unsigned 8-bit.

Referenced by run_single_ended_8bit_conversion_test().

#define NUM_AVERAGE_SAMPLES   4

Number of samples to average over.

Referenced by differential_signed_average(), and single_ended_unsigned_average().

static void differential_signed_average ( ADC_t *  adc,
uint8_t  ch_mask,
const uint8_t *  mux_pos_inputs,
const uint8_t *  mux_neg_inputs,
uint8_t  num_inputs,
int16_t *  results,
uint8_t  gain 
)
static

Measure differential input MUX combinations on channel.

Measures a set of input MUX combinations on a single channel, using averaging of the number of samples specified with NUM_AVERAGE_SAMPLES.

Precondition
This function does not configure the ADC, only the ADC channel, and therefore the specified ADC needs to be configured before this function is run.
Parameters
adcPointer to ADC to measure with.
ch_maskMask for channel to measure with.
mux_pos_inputsPointer to array of positive input MUX settings.
mux_neg_inputsPointer to array of negative input MUX settings.
num_inputsNumber of input MUX setting pairs.
resultsPointer to array to store results in.
gainGain to use for all measurements.
Note
The array which results points to must have at least num_inputs elements.

References adc_disable(), adc_enable(), adc_get_result, adc_start_conversion(), adc_wait_for_interrupt_flag(), adcch_disable_interrupt(), ADCCH_MODE_COMPLETE, adcch_set_input(), adcch_set_interrupt_mode(), adcch_write_configuration(), and NUM_AVERAGE_SAMPLES.

Referenced by run_differential_12bit_conversion_test(), and run_differential_12bit_with_gain_conversion_test().

static void run_differential_12bit_conversion_test ( const struct test_case test)
static

Test differential conversion in 12-bit mode using the DAC.

This tests output three different values on the two DAC channels:

  • 1/2 * DAC_MAX on both outputs to get a differential zero
  • DAC_MAX on positive and DAC_MIN on negative to get max positive result
  • DAC_MIN on positive and DAC_MAX on negative to get max negative result

These values are then measured using the ADC on the pins that are connected to the DAC channel, and the results are compared and checked to see if they are within the acceptable range of values that passes the test.

Parameters
testCurrent test case.

References adc_read_configuration(), ADC_REF_BANDGAP, ADC_RES_12, adc_set_clock_rate(), adc_set_conversion_parameters(), adc_set_conversion_trigger(), ADC_SIGN_ON, ADC_SIGNED_12BIT_MAX, ADC_SIGNED_12BIT_MIN, ADC_TRIG_MANUAL, adc_write_configuration(), ADC_ZERO, DAC_ADJ_RIGHT, DAC_CH0, DAC_CH1, dac_enable(), DAC_MAX, DAC_MIN, dac_read_configuration(), DAC_REF_BANDGAP, dac_set_active_channel(), dac_set_channel_value(), dac_set_conversion_interval(), dac_set_conversion_parameters(), dac_set_conversion_trigger(), dac_set_refresh_interval(), dac_wait_for_channel_ready(), dac_write_configuration(), differential_signed_average(), NUM_CHANNELS, and verify_signed_result().

Referenced by main().

static void run_differential_12bit_with_gain_conversion_test ( const struct test_case test)
static

Test differential conversion with gain in 12-bit mode using the DAC.

This test outputs a gain compensated level on DAC output that should result in a value of half maximum positive value on the ADC.

These values are then measured using the ADC on the pins that are connected to the DAC channel, and the results are compared and checked to see if they are within the acceptable range of values that passes the test.

Parameters
testCurrent test case.

References adc_read_configuration(), ADC_REF_BANDGAP, ADC_RES_12, adc_set_clock_rate(), adc_set_conversion_parameters(), adc_set_conversion_trigger(), ADC_SIGN_ON, ADC_SIGNED_12BIT_MAX, ADC_SIGNED_12BIT_MIN, ADC_TRIG_MANUAL, adc_write_configuration(), DAC_ADJ_RIGHT, DAC_CH0, DAC_CH1, dac_enable(), DAC_MAX, DAC_MIN, dac_read_configuration(), DAC_REF_BANDGAP, dac_set_active_channel(), dac_set_channel_value(), dac_set_conversion_interval(), dac_set_conversion_parameters(), dac_set_conversion_trigger(), dac_set_refresh_interval(), dac_wait_for_channel_ready(), dac_write_configuration(), differential_signed_average(), NUM_CHANNELS, and verify_signed_result().

Referenced by main().

static void run_single_ended_12bit_conversion_test ( const struct test_case test)
static

Test single ended conversion in 12-bit mode using the DAC.

This tests output three different values on the two DAC channels:

  • 0 (output analog value is greater than 0, as the DAC cannot go that low)
  • 1/2 * DAC_MAX Half of the maximum value of the DAC
  • DAC_MAX The maximum value (VREF) of the DAC.

These values are then measured using the ADC on the pins that are connected to the DAC channel, and the results are compared and checked to see if they are within the acceptable range of values that passes the test.

Parameters
testCurrent test case.

References adc_read_configuration(), ADC_REF_BANDGAP, ADC_RES_12, adc_set_clock_rate(), adc_set_conversion_parameters(), adc_set_conversion_trigger(), ADC_SIGN_OFF, ADC_TRIG_MANUAL, ADC_UNSIGNED_12BIT_MAX, adc_write_configuration(), ADC_ZERO, DAC_ADJ_RIGHT, DAC_CH0, DAC_CH1, dac_enable(), DAC_MAX, DAC_MIN, dac_read_configuration(), DAC_REF_BANDGAP, dac_set_active_channel(), dac_set_channel_value(), dac_set_conversion_interval(), dac_set_conversion_parameters(), dac_set_conversion_trigger(), dac_set_refresh_interval(), dac_wait_for_channel_ready(), dac_write_configuration(), NUM_CHANNELS, single_ended_unsigned_average(), and verify_unsigned_result().

Referenced by main().

static void run_single_ended_8bit_conversion_test ( const struct test_case test)
static

Test single ended conversion in 8-bit mode using the DAC.

This tests output three different values on the two DAC channels:

  • 0 (output analog value is greater than 0, as the DAC cannot go that low)
  • 1/2 * DAC_MAX Half of the maximum value of the DAC
  • DAC_MAX The maximum value (VREF) of the DAC.

These values are then measured using the ADC on the pins that are connected to the DAC channel, using all available ADC channels and the results are compared and checked to see if they are within the acceptable range of values that passes the test.

Parameters
testCurrent test case.

References adc_read_configuration(), ADC_REF_BANDGAP, ADC_RES_8, adc_set_clock_rate(), adc_set_conversion_parameters(), adc_set_conversion_trigger(), ADC_SIGN_OFF, ADC_TRIG_MANUAL, ADC_UNSIGNED_8BIT_MAX, adc_write_configuration(), ADC_ZERO, DAC_ADJ_RIGHT, DAC_CH0, DAC_CH1, dac_enable(), DAC_MAX, DAC_MIN, dac_read_configuration(), DAC_REF_BANDGAP, dac_set_active_channel(), dac_set_channel_value(), dac_set_conversion_interval(), dac_set_conversion_parameters(), dac_set_conversion_trigger(), dac_set_refresh_interval(), dac_wait_for_channel_ready(), dac_write_configuration(), NUM_CHANNELS, single_ended_unsigned_average(), and verify_unsigned_result().

Referenced by main().

static void single_ended_unsigned_average ( ADC_t *  adc,
uint8_t  ch_mask,
const uint8_t *  mux_inputs,
uint8_t  num_inputs,
uint16_t *  results 
)
static

Measure single-ended input MUX combinations on channel.

Measures a set of input MUX combinations on a single channel, using averaging of the number of samples specified with NUM_AVERAGE_SAMPLES.

Precondition
This function does not configure the ADC, only the ADC channel, and therefore the specified ADC needs to be configured before this function is run.
Parameters
adcPointer to ADC to measure with.
ch_maskMask for channel to measure with.
mux_inputsPointer to array of input MUX settings.
num_inputsNumber of input MUX settings.
resultsPointer to array to store results in.
Note
The array which results points to must have at least num_inputs elements.

References adc_disable(), adc_enable(), adc_get_result, adc_start_conversion(), adc_wait_for_interrupt_flag(), adcch_disable_interrupt(), ADCCH_MODE_COMPLETE, ADCCH_NEG_NONE, adcch_set_input(), adcch_set_interrupt_mode(), adcch_write_configuration(), and NUM_AVERAGE_SAMPLES.

Referenced by run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().

static void verify_signed_result ( const struct test_case test,
int16_t  expected,
int16_t  actual,
uint8_t  adc_ch,
uint8_t  mux_pos_input,
uint8_t  mux_neg_input,
int16_t  lower_limit,
int16_t  upper_limit,
uint8_t  gain,
bool  value_is_12bit 
)
static

Verify that unsigned ADC result is in acceptable range.

Checks that measurement is within the expected range, and outputs a clear error message to the unit test framework if it is not.

Parameters
testPointer to current test case.
expectedThe expected ADC result.
actualThe measured ADC result.
adc_chThe ADC channel used.
mux_pos_inputThe positive ADC input MUX setting used.
mux_neg_inputThe negative ADC input MUX setting used.
lower_limitLower limit of accepted range, can override the limit implicitly given by the accepted deviation.
upper_limitUpper limit of accepted range, can override the limit implicitly given by the accepted deviation.
gainADC input gain setting used.
value_is_12bitSpecify the resolution of the ADC result and thus the accepted deviation:
  • true for +/- CONF_TEST_ACCEPT_DELTA_12BIT
  • false for +/- CONF_TEST_ACCEPT_DELTA_8BIT

References test_assert_true.

Referenced by run_differential_12bit_conversion_test(), and run_differential_12bit_with_gain_conversion_test().

static void verify_unsigned_result ( const struct test_case test,
uint16_t  expected,
uint16_t  actual,
uint8_t  dac_ch,
uint8_t  adc_ch,
uint8_t  mux_input,
uint16_t  upper_limit,
bool  value_is_12bit 
)
static

Verify that unsigned ADC result is in acceptable range.

Checks that measurement is within the expected range, and outputs a clear error message to the unit test framework if it is not.

Parameters
testPointer to current test case.
expectedThe expected ADC result.
actualThe measured ADC result.
dac_chThe DAC channel used.
adc_chThe ADC channel used.
mux_inputThe ADC input MUX setting used.
upper_limitUpper limit of accepted range, can override the limit implicitly given by the accepted deviation.
value_is_12bitSpecify the resolution of the ADC result and thus the accepted deviation:
  • true for +/- CONF_TEST_ACCEPT_DELTA_12BIT
  • false for +/- CONF_TEST_ACCEPT_DELTA_8BIT

References test_assert_true.

Referenced by run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().