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 ADC_ZERO 0 |
ADC zero value.
Referenced by run_differential_12bit_conversion_test(), run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().
#define DAC_MAX 0xfff |
Maximum value for DAC.
Referenced by run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().
#define DAC_MIN 0 |
Minimum value for DAC.
Referenced by run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_single_ended_12bit_conversion_test(), and 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().
#define NUM_CHANNELS 4 |
Number of ADC channels to use.
Referenced by run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().
|
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.
adc | Pointer to ADC to measure with. |
ch_mask | Mask for channel to measure with. |
mux_pos_inputs | Pointer to array of positive input MUX settings. |
mux_neg_inputs | Pointer to array of negative input MUX settings. |
num_inputs | Number of input MUX setting pairs. |
results | Pointer to array to store results in. |
gain | Gain to use for all measurements. |
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().
int main | ( | void | ) |
Run ADC unit tests.
Initializes the clock system, board and serial output, then sets up the ADC unit test suite and runs it.
References usart_rs232_options::baudrate, board_init(), DEFINE_TEST_ARRAY, DEFINE_TEST_CASE, DEFINE_TEST_SUITE, run_differential_12bit_conversion_test(), run_differential_12bit_with_gain_conversion_test(), run_single_ended_12bit_conversion_test(), run_single_ended_8bit_conversion_test(), sleepmgr_init(), stdio_serial_init(), sysclk_init(), test_suite_run(), and usart_serial_options.
|
static |
Test differential conversion in 12-bit mode using the DAC.
This tests output three different values on the two DAC channels:
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.
test | Current 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 |
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.
test | Current 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 |
Test single ended conversion in 12-bit mode using the DAC.
This tests output three different values on the two DAC channels:
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.
test | Current 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 |
Test single ended conversion in 8-bit mode using the DAC.
This tests output three different values on the two DAC channels:
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.
test | Current 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 |
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.
adc | Pointer to ADC to measure with. |
ch_mask | Mask for channel to measure with. |
mux_inputs | Pointer to array of input MUX settings. |
num_inputs | Number of input MUX settings. |
results | Pointer to array to store results in. |
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 |
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.
test | Pointer to current test case. |
expected | The expected ADC result. |
actual | The measured ADC result. |
adc_ch | The ADC channel used. |
mux_pos_input | The positive ADC input MUX setting used. |
mux_neg_input | The negative ADC input MUX setting used. |
lower_limit | Lower limit of accepted range, can override the limit implicitly given by the accepted deviation. |
upper_limit | Upper limit of accepted range, can override the limit implicitly given by the accepted deviation. |
gain | ADC input gain setting used. |
value_is_12bit | Specify the resolution of the ADC result and thus the accepted deviation:
|
References test_assert_true.
Referenced by run_differential_12bit_conversion_test(), and run_differential_12bit_with_gain_conversion_test().
|
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.
test | Pointer to current test case. |
expected | The expected ADC result. |
actual | The measured ADC result. |
dac_ch | The DAC channel used. |
adc_ch | The ADC channel used. |
mux_input | The ADC input MUX setting used. |
upper_limit | Upper limit of accepted range, can override the limit implicitly given by the accepted deviation. |
value_is_12bit | Specify the resolution of the ADC result and thus the accepted deviation:
|
References test_assert_true.
Referenced by run_single_ended_12bit_conversion_test(), and run_single_ended_8bit_conversion_test().