2017-09-13 06:33:05 -04:00
|
|
|
/*
|
|
|
|
Tests for the adc2 device driver
|
|
|
|
*/
|
|
|
|
#include "esp_system.h"
|
|
|
|
#include "driver/adc.h"
|
|
|
|
#include "unity.h"
|
|
|
|
#include "esp_system.h"
|
2019-07-05 04:58:04 -04:00
|
|
|
#include "esp_event.h"
|
2017-09-13 06:33:05 -04:00
|
|
|
#include "esp_wifi.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
#include "nvs_flash.h"
|
2018-09-25 20:17:46 -04:00
|
|
|
#include "test_utils.h"
|
2017-09-13 06:33:05 -04:00
|
|
|
|
2020-11-26 03:56:13 -05:00
|
|
|
#if !TEMPORARY_DISABLED_FOR_TARGETS(ESP32S3, ESP32C3)
|
|
|
|
#include "driver/dac.h"
|
2020-08-20 00:22:36 -04:00
|
|
|
|
2017-09-13 06:33:05 -04:00
|
|
|
static const char* TAG = "test_adc2";
|
|
|
|
|
2020-02-25 09:19:48 -05:00
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32
|
|
|
|
#define ADC_TEST_WIDTH ADC_WIDTH_BIT_12
|
|
|
|
#define ADC_TEST_RESOLUTION (4096)
|
|
|
|
#define ADC_TEST_DAC_RANGE (256)
|
|
|
|
#define ADC_TEST_CH1 ADC2_CHANNEL_8
|
|
|
|
#define ADC_TEST_CH2 ADC2_CHANNEL_9
|
|
|
|
#define ADC_TEST_ERROR (600)
|
|
|
|
#elif defined CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
#define ADC_TEST_WIDTH ADC_WIDTH_BIT_13 //ESP32S2 only support 13 bit width
|
|
|
|
#define ADC_TEST_RESOLUTION (8192)
|
|
|
|
#define ADC_TEST_DAC_RANGE (210)
|
|
|
|
#define ADC_TEST_CH1 ADC2_CHANNEL_6
|
|
|
|
#define ADC_TEST_CH2 ADC2_CHANNEL_7
|
|
|
|
#define ADC_TEST_ERROR (1500)
|
|
|
|
#endif
|
2017-09-13 06:33:05 -04:00
|
|
|
#define DEFAULT_SSID "TEST_SSID"
|
|
|
|
#define DEFAULT_PWD "TEST_PASS"
|
|
|
|
|
2019-07-05 04:58:04 -04:00
|
|
|
static void wifi_event_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
2017-09-13 06:33:05 -04:00
|
|
|
{
|
|
|
|
printf("ev_handle_called.\n");
|
2019-07-05 04:58:04 -04:00
|
|
|
switch(event_id) {
|
|
|
|
case WIFI_EVENT_STA_START:
|
|
|
|
ESP_LOGI(TAG, "WIFI_EVENT_STA_START");
|
2017-09-13 06:33:05 -04:00
|
|
|
//do not actually connect in test case
|
|
|
|
//;
|
|
|
|
break;
|
2019-07-05 04:58:04 -04:00
|
|
|
case WIFI_EVENT_STA_DISCONNECTED:
|
|
|
|
ESP_LOGI(TAG, "WIFI_EVENT_STA_DISCONNECTED");
|
2017-09-13 06:33:05 -04:00
|
|
|
TEST_ESP_OK(esp_wifi_connect());
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2019-07-05 04:58:04 -04:00
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ip_event_handler(void* arg, esp_event_base_t event_base,
|
|
|
|
int32_t event_id, void* event_data)
|
|
|
|
{
|
|
|
|
ip_event_got_ip_t *event;
|
|
|
|
printf("ev_handle_called.\n");
|
|
|
|
switch(event_id) {
|
|
|
|
case IP_EVENT_STA_GOT_IP:
|
|
|
|
event = (ip_event_got_ip_t*)event_data;
|
|
|
|
ESP_LOGI(TAG, "IP_EVENT_STA_GOT_IP");
|
2019-08-20 08:40:34 -04:00
|
|
|
ESP_LOGI(TAG, "got ip:" IPSTR "\n", IP2STR(&event->ip_info.ip));
|
2019-07-05 04:58:04 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int event_init(void)
|
|
|
|
{
|
|
|
|
TEST_ESP_OK(esp_event_loop_create_default());
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_register(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler, NULL));
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int event_deinit(void)
|
|
|
|
{
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_unregister(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler));
|
|
|
|
ESP_ERROR_CHECK(esp_event_handler_unregister(IP_EVENT, ESP_EVENT_ANY_ID, &ip_event_handler));
|
2017-09-13 06:33:05 -04:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-02-08 00:48:25 -05:00
|
|
|
TEST_CASE("adc2 work with wifi","[adc]")
|
2017-09-13 06:33:05 -04:00
|
|
|
{
|
|
|
|
int read_raw;
|
|
|
|
int target_value;
|
2018-09-25 20:17:46 -04:00
|
|
|
|
|
|
|
test_case_uses_tcpip();
|
|
|
|
|
2017-09-13 06:33:05 -04:00
|
|
|
//adc and dac init
|
|
|
|
TEST_ESP_OK( dac_output_enable( DAC_CHANNEL_1 ));
|
|
|
|
TEST_ESP_OK( dac_output_enable( DAC_CHANNEL_2 ));
|
|
|
|
TEST_ESP_OK( dac_output_voltage( DAC_CHANNEL_1, 30 ));
|
|
|
|
TEST_ESP_OK( dac_output_voltage( DAC_CHANNEL_2, 60 ));
|
2020-02-25 09:19:48 -05:00
|
|
|
TEST_ESP_OK( adc2_config_channel_atten( ADC_TEST_CH1, ADC_ATTEN_0db ));
|
|
|
|
TEST_ESP_OK( adc2_config_channel_atten( ADC_TEST_CH2, ADC_ATTEN_0db ));
|
2017-09-13 06:33:05 -04:00
|
|
|
//init wifi
|
|
|
|
printf("nvs init\n");
|
|
|
|
esp_err_t r = nvs_flash_init();
|
2018-07-25 11:11:09 -04:00
|
|
|
if (r == ESP_ERR_NVS_NO_FREE_PAGES || r == ESP_ERR_NVS_NEW_VERSION_FOUND) {
|
|
|
|
printf("no free pages or nvs version mismatch, erase..\n");
|
2017-09-13 06:33:05 -04:00
|
|
|
TEST_ESP_OK(nvs_flash_erase());
|
|
|
|
r = nvs_flash_init();
|
2020-01-02 01:25:33 -05:00
|
|
|
}
|
2017-09-13 06:33:05 -04:00
|
|
|
TEST_ESP_OK( r);
|
2019-09-01 12:25:23 -04:00
|
|
|
esp_netif_init();
|
2019-07-05 04:58:04 -04:00
|
|
|
event_init();
|
2019-09-01 12:25:23 -04:00
|
|
|
esp_netif_create_default_wifi_sta();
|
|
|
|
|
2017-09-13 06:33:05 -04:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
|
|
|
TEST_ESP_OK(esp_wifi_init(&cfg));
|
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
.sta = {
|
|
|
|
.ssid = DEFAULT_SSID,
|
|
|
|
.password = DEFAULT_PWD
|
|
|
|
},
|
|
|
|
};
|
|
|
|
TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_STA));
|
|
|
|
TEST_ESP_OK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
|
2020-01-02 01:25:33 -05:00
|
|
|
|
2017-09-13 06:33:05 -04:00
|
|
|
//test read value
|
2020-02-25 09:19:48 -05:00
|
|
|
TEST_ESP_OK( adc2_get_raw( ADC_TEST_CH1, ADC_TEST_WIDTH, &read_raw ));
|
|
|
|
target_value = 30*ADC_TEST_RESOLUTION*3/ADC_TEST_DAC_RANGE; //3 = 3.3/1.1
|
2017-09-13 06:33:05 -04:00
|
|
|
printf("dac set: %d, adc read: %d (target_value: %d)\n", 30, read_raw, target_value );
|
2020-02-25 09:19:48 -05:00
|
|
|
TEST_ASSERT_INT_WITHIN( ADC_TEST_ERROR, target_value, read_raw );
|
|
|
|
TEST_ESP_OK( adc2_get_raw( ADC_TEST_CH2, ADC_TEST_WIDTH, &read_raw ));
|
|
|
|
target_value = 60*ADC_TEST_RESOLUTION*3/ADC_TEST_DAC_RANGE;
|
2017-09-13 06:33:05 -04:00
|
|
|
printf("dac set: %d, adc read: %d (target_value: %d)\n", 60, read_raw, target_value );
|
2020-02-25 09:19:48 -05:00
|
|
|
TEST_ASSERT_INT_WITHIN( ADC_TEST_ERROR, target_value, read_raw );
|
2017-09-13 06:33:05 -04:00
|
|
|
|
|
|
|
//now start wifi
|
|
|
|
printf("wifi start...\n");
|
|
|
|
TEST_ESP_OK(esp_wifi_start());
|
|
|
|
//test reading during wifi on
|
2020-02-25 09:19:48 -05:00
|
|
|
#ifdef CONFIG_IDF_TARGET_ESP32
|
|
|
|
TEST_ASSERT_EQUAL( adc2_get_raw( ADC_TEST_CH1, ADC_TEST_WIDTH, &read_raw ), ESP_ERR_TIMEOUT );
|
|
|
|
TEST_ASSERT_EQUAL( adc2_get_raw( ADC_TEST_CH2, ADC_TEST_WIDTH, &read_raw ), ESP_ERR_TIMEOUT );
|
|
|
|
#elif defined CONFIG_IDF_TARGET_ESP32S2
|
|
|
|
TEST_ASSERT_EQUAL( adc2_get_raw( ADC_TEST_CH1, ADC_TEST_WIDTH, &read_raw ), ESP_OK );
|
|
|
|
TEST_ASSERT_EQUAL( adc2_get_raw( ADC_TEST_CH2, ADC_TEST_WIDTH, &read_raw ), ESP_OK );
|
|
|
|
#endif
|
2017-09-13 06:33:05 -04:00
|
|
|
//wifi stop again
|
|
|
|
printf("wifi stop...\n");
|
|
|
|
TEST_ESP_OK( esp_wifi_stop() );
|
|
|
|
TEST_ESP_OK(esp_wifi_deinit());
|
2019-07-05 04:58:04 -04:00
|
|
|
event_deinit();
|
2017-09-13 06:33:05 -04:00
|
|
|
nvs_flash_deinit();
|
|
|
|
|
|
|
|
//test read value
|
2020-02-25 09:19:48 -05:00
|
|
|
TEST_ESP_OK( adc2_get_raw( ADC_TEST_CH1, ADC_TEST_WIDTH, &read_raw ));
|
|
|
|
target_value = 30*ADC_TEST_RESOLUTION*3/ADC_TEST_DAC_RANGE; //3 = 3.3/1.1
|
2017-09-13 06:33:05 -04:00
|
|
|
printf("dac set: %d, adc read: %d (target_value: %d)\n", 30, read_raw, target_value );
|
2020-02-25 09:19:48 -05:00
|
|
|
TEST_ASSERT_INT_WITHIN( ADC_TEST_ERROR, target_value, read_raw );
|
|
|
|
TEST_ESP_OK( adc2_get_raw( ADC_TEST_CH2, ADC_TEST_WIDTH, &read_raw ));
|
|
|
|
target_value = 60*ADC_TEST_RESOLUTION*3/ADC_TEST_DAC_RANGE;
|
2017-09-13 06:33:05 -04:00
|
|
|
printf("dac set: %d, adc read: %d (target_value: %d)\n", 60, read_raw, target_value );
|
2020-02-25 09:19:48 -05:00
|
|
|
TEST_ASSERT_INT_WITHIN( ADC_TEST_ERROR, target_value, read_raw );
|
2017-09-13 06:33:05 -04:00
|
|
|
|
|
|
|
printf("test passed...\n");
|
|
|
|
|
2019-09-01 12:25:23 -04:00
|
|
|
TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of esp_netif and event_loop.");
|
2017-09-13 06:33:05 -04:00
|
|
|
}
|
2020-08-20 00:22:36 -04:00
|
|
|
|
|
|
|
#endif
|