Microchip® Advanced Software Framework

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

Unit tests for DAC driver using ADC.

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

DAC 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 DAC_8BIT_MAX   0xff
 Maximum value for DAC in 8-bit. 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 DAC unit tests. More...
 
DAC unit test helper functions
static uint16_t left_adjust_8bit_value (uint16_t value)
 Shift 8 bit value to be used in left adjusted mode. More...
 
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 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...
 
DAC unit test functions
static void run_dual_channel_12bit_conversion_test (const struct test_case *test)
 Test dual channel 12 bit conversion. More...
 
static void run_dual_channel_8bit_conversion_test (const struct test_case *test)
 Test dual channel conversion in 8-bit mode using the DAC. More...
 
static void run_single_channel_12bit_conversion_test (const struct test_case *test)
 Test single channel conversion in 12-bit mode using the DAC. More...
 
static void run_single_channel_8bit_conversion_test (const struct test_case *test)
 Test single channel conversion in 8-bit mode using the DAC. More...
 

#define ADC_SIGNED_12BIT_MAX   0x7ff

Maximum value for ADC in signed 12-bit.

#define ADC_SIGNED_12BIT_MIN   -0x800

Minimum value for ADC in signed 12-bit.

#define ADC_UNSIGNED_12BIT_MAX   0xfff
#define ADC_UNSIGNED_8BIT_MAX   0xff

Maximum value for ADC in unsigned 8-bit.

#define DAC_8BIT_MAX   0xff

Maximum value for DAC in 8-bit.

Referenced by run_dual_channel_8bit_conversion_test(), and run_single_channel_8bit_conversion_test().

#define DAC_MAX   0xfff
#define NUM_AVERAGE_SAMPLES   4

Number of samples to average over.

Referenced by single_ended_unsigned_average().

static uint16_t left_adjust_8bit_value ( uint16_t  value)
static

Shift 8 bit value to be used in left adjusted mode.

Parameters
valuevalue to left adjust
Returns
value left adjusted 16 bit value to write to dac_set_channel_value

Referenced by run_dual_channel_8bit_conversion_test(), and run_single_channel_8bit_conversion_test().

static void run_dual_channel_12bit_conversion_test ( const struct test_case test)
static

Test dual channel 12 bit conversion.

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 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_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_dual_channel_8bit_conversion_test ( const struct test_case test)
static

Test dual channel 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_8BIT_MAX Half of the maximum value of the DAC in 8 bit mode
  • DAC_8BIT_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 channels, 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_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_8BIT_MAX, DAC_ADJ_LEFT, DAC_CH0, DAC_CH1, dac_enable(), 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(), left_adjust_8bit_value(), NUM_CHANNELS, single_ended_unsigned_average(), and verify_unsigned_result().

Referenced by main().

static void run_single_channel_12bit_conversion_test ( const struct test_case test)
static

Test single channel 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, 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_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_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_channel_8bit_conversion_test ( const struct test_case test)
static

Test single channel 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_8BIT_MAX Half of the maximum value of the DAC in 8 bit mode
  • DAC_8BIT_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_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_8BIT_MAX, DAC_ADJ_LEFT, DAC_CH0, dac_enable(), 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(), left_adjust_8bit_value(), 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.

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_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), and run_single_channel_8bit_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_dual_channel_12bit_conversion_test(), run_dual_channel_8bit_conversion_test(), run_single_channel_12bit_conversion_test(), and run_single_channel_8bit_conversion_test().