Microchip® Advanced Software Framework

drivers/adc/unit_test/unit_test.c File Reference

SAM Analog to Digital Converter (ADC) Unit test.

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

#include <asf.h>
#include <stdio_serial.h>
#include <string.h>
#include "conf_test.h"

Macros

#define ADC_OFFSET   150
 
#define ADC_SAMPLES   128
 
#define ADC_VAL_DAC_FULL_OUTPUT   4095
 
#define ADC_VAL_DAC_HALF_OUTPUT   2047
 
#define DAC_VAL_HALF_VOLT   512
 
#define DAC_VAL_ONE_VOLT   1023
 

Functions

static void adc_user_callback (struct adc_module *const module)
 ADC callback function. More...
 
static void cdc_uart_init (void)
 Initialize the USART for unit test. More...
 
static void cleanup_adc_callback_mode_test (const struct test_case *test)
 Cleanup Function: ADC callback mode test. More...
 
static void cleanup_adc_window_mode_test (const struct test_case *test)
 Cleanup Function: ADC window mode test. More...
 
int main (void)
 Run ADC unit tests. More...
 
static void run_adc_average_mode_test (const struct test_case *test)
 ADC average mode test function. More...
 
static void run_adc_callback_mode_test (const struct test_case *test)
 ADC callback mode test function. More...
 
static void run_adc_init_test (const struct test_case *test)
 Test for ADC initialization. More...
 
static void run_adc_polled_mode_test (const struct test_case *test)
 Test for ADC conversion in polled mode. More...
 
static void run_adc_window_mode_test (const struct test_case *test)
 ADC window mode test function. More...
 
static void setup_adc_average_mode_test (const struct test_case *test)
 Setup Function: ADC average mode test. More...
 
static void setup_adc_callback_mode_test (const struct test_case *test)
 Setup Function: ADC callback mode test. More...
 
static void setup_adc_window_mode_test (const struct test_case *test)
 Setup Function: ADC window mode test. More...
 
static void test_dac_init (void)
 Initialize the DAC for unit test. More...
 

Variables

uint16_t adc_buf [ADC_SAMPLES]
 
volatile bool adc_init_success = false
 
struct adc_module adc_inst
 
struct usart_module cdc_uart_module
 
struct dac_module dac_inst
 
volatile bool interrupt_flag = false
 

#define ADC_VAL_DAC_FULL_OUTPUT   4095
#define DAC_VAL_ONE_VOLT   1023

static void adc_user_callback ( struct adc_module *const  module)
static

ADC callback function.

Called by ADC driver on interrupt detection.

Parameters
modulePointer to the ADC module (not used)

References interrupt_flag.

Referenced by setup_adc_callback_mode_test(), and setup_adc_window_mode_test().

static void cdc_uart_init ( void  )
static
static void cleanup_adc_callback_mode_test ( const struct test_case test)
static

Cleanup Function: ADC callback mode test.

This function clears the ADC result buffer and unregisters & disables callback for the ADC buffer read.

Parameters
testCurrent test case.

References adc_buf, adc_disable_callback(), adc_inst, ADC_SAMPLES, adc_unregister_callback(), and i.

Referenced by main().

static void cleanup_adc_window_mode_test ( const struct test_case test)
static

Cleanup Function: ADC window mode test.

This function unregisters & disables callback for window detection.

Parameters
testCurrent test case.

References adc_disable_callback(), adc_inst, and adc_unregister_callback().

Referenced by main().

static void run_adc_average_mode_test ( const struct test_case test)
static

ADC average mode test function.

This test performs the ADC averaging by starting a conversion. 0.5V is applied as input from DAC. Converted result is verified for expected results.

Parameters
testCurrent test case.

References adc_inst, ADC_OFFSET, adc_read(), adc_start_conversion(), ADC_VAL_DAC_HALF_OUTPUT, dac_chan_write(), DAC_CHANNEL_0, dac_inst, DAC_VAL_HALF_VOLT, delay_ms, STATUS_OK, and test_assert_true.

Referenced by main().

static void run_adc_callback_mode_test ( const struct test_case test)
static

ADC callback mode test function.

This test checks the callback functionality of the ADC driver. ADC callback for buffered conversion is enabled. Converted results are verified for expected results.

Parameters
testCurrent test case.

References adc_buf, adc_inst, ADC_OFFSET, adc_read_buffer_job(), ADC_SAMPLES, ADC_VAL_DAC_HALF_OUTPUT, dac_chan_write(), DAC_CHANNEL_0, dac_inst, DAC_VAL_HALF_VOLT, delay_ms, i, interrupt_flag, and test_assert_true.

Referenced by main().

static void run_adc_init_test ( const struct test_case test)
static

Test for ADC initialization.

This test initializes the ADC module and checks whether the initialization is successful or not.

If this test fails no other tests will run.

Parameters
testCurrent test case.

References adc_enable(), adc_get_config_defaults(), adc_init(), adc_init_success, adc_inst, GCLK_GENERATOR_3, status, STATUS_ERR_IO, STATUS_OK, and test_assert_true.

Referenced by main().

static void run_adc_polled_mode_test ( const struct test_case test)
static

Test for ADC conversion in polled mode.

This test checks the polled mode functionality of the ADC. 0.5V and 1V inputs are applied to the ADC via DAC and the ADC outputs are verified for expected results.

Parameters
testCurrent test case.

References adc_flush(), adc_init_success, adc_inst, ADC_OFFSET, adc_read(), adc_start_conversion(), ADC_VAL_DAC_FULL_OUTPUT, ADC_VAL_DAC_HALF_OUTPUT, dac_chan_write(), DAC_CHANNEL_0, dac_inst, DAC_VAL_HALF_VOLT, DAC_VAL_ONE_VOLT, delay_ms, i, STATUS_OK, and test_assert_true.

Referenced by main().

static void run_adc_window_mode_test ( const struct test_case test)
static

ADC window mode test function.

This test gives an input voltage outside the window and checks whether the callback is triggered or not.

Parameters
testCurrent test case.

References dac_chan_write(), DAC_CHANNEL_0, dac_inst, DAC_VAL_ONE_VOLT, delay_ms, interrupt_flag, and test_assert_true.

Referenced by main().

static void setup_adc_average_mode_test ( const struct test_case test)
static

Setup Function: ADC average mode test.

This function initializes the ADC in averaging mode.

Parameters
testCurrent test case.

References adc_disable(), adc_enable(), adc_get_config_defaults(), adc_init(), adc_init_success, adc_inst, GCLK_GENERATOR_3, status, STATUS_ERR_IO, STATUS_OK, and test_assert_true.

Referenced by main().

static void setup_adc_callback_mode_test ( const struct test_case test)
static

Setup Function: ADC callback mode test.

This function initializes the ADC result buffer and registers & enables callback for the ADC buffer read.

Parameters
testCurrent test case.

References adc_buf, adc_enable_callback(), adc_init_success, adc_inst, adc_register_callback(), ADC_SAMPLES, adc_user_callback(), i, interrupt_flag, and test_assert_true.

Referenced by main().

static void setup_adc_window_mode_test ( const struct test_case test)
static

Setup Function: ADC window mode test.

This function initializes the ADC in window mode. Upper and lower threshold values are provided. It also registers & enables callback for window detection.

Parameters
testCurrent test case.

References adc_disable(), adc_enable(), adc_enable_callback(), adc_get_config_defaults(), adc_init(), adc_init_success, adc_inst, ADC_OFFSET, adc_register_callback(), adc_start_conversion(), adc_user_callback(), ADC_VAL_DAC_HALF_OUTPUT, dac_chan_write(), DAC_CHANNEL_0, dac_inst, DAC_VAL_HALF_VOLT, delay_ms, GCLK_GENERATOR_3, interrupt_flag, status, STATUS_ERR_IO, STATUS_OK, and test_assert_true.

Referenced by main().

static void test_dac_init ( void  )
static

Initialize the DAC for unit test.

Initializes the DAC module used for sending the analog values to the ADC during test.

References dac_config::clock_source, dac_chan_enable(), dac_chan_get_config_defaults(), dac_chan_set_config(), DAC_CHANNEL_0, dac_enable(), dac_get_config_defaults(), dac_init(), dac_inst, DAC_REFERENCE_INT1V, GCLK_GENERATOR_3, and dac_config::reference.

Referenced by main().

struct usart_module cdc_uart_module
struct dac_module dac_inst
volatile bool interrupt_flag = false