test_adc2.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*
  2. Tests for the adc2 device driver
  3. */
  4. #include "esp_system.h"
  5. #include "driver/adc.h"
  6. #include "driver/dac.h"
  7. #include "unity.h"
  8. #include "esp_system.h"
  9. #include "esp_event_loop.h"
  10. #include "esp_wifi.h"
  11. #include "esp_log.h"
  12. #include "nvs_flash.h"
  13. static const char* TAG = "test_adc2";
  14. #define DEFAULT_SSID "TEST_SSID"
  15. #define DEFAULT_PWD "TEST_PASS"
  16. static esp_err_t event_handler(void *ctx, system_event_t *event)
  17. {
  18. printf("ev_handle_called.\n");
  19. switch(event->event_id) {
  20. case SYSTEM_EVENT_STA_START:
  21. ESP_LOGI(TAG, "SYSTEM_EVENT_STA_START");
  22. //do not actually connect in test case
  23. //;
  24. break;
  25. case SYSTEM_EVENT_STA_GOT_IP:
  26. ESP_LOGI(TAG, "SYSTEM_EVENT_STA_GOT_IP");
  27. ESP_LOGI(TAG, "got ip:%s\n",
  28. ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
  29. break;
  30. case SYSTEM_EVENT_STA_DISCONNECTED:
  31. ESP_LOGI(TAG, "SYSTEM_EVENT_STA_DISCONNECTED");
  32. TEST_ESP_OK(esp_wifi_connect());
  33. break;
  34. default:
  35. break;
  36. }
  37. return ESP_OK;
  38. }
  39. TEST_CASE("adc2 work with wifi","[adc]")
  40. {
  41. int read_raw;
  42. int target_value;
  43. //adc and dac init
  44. TEST_ESP_OK( dac_output_enable( DAC_CHANNEL_1 ));
  45. TEST_ESP_OK( dac_output_enable( DAC_CHANNEL_2 ));
  46. TEST_ESP_OK( dac_output_voltage( DAC_CHANNEL_1, 30 ));
  47. TEST_ESP_OK( dac_output_voltage( DAC_CHANNEL_2, 60 ));
  48. TEST_ESP_OK( adc2_config_channel_atten( ADC2_CHANNEL_8, ADC_ATTEN_0db ));
  49. TEST_ESP_OK( adc2_config_channel_atten( ADC2_CHANNEL_9, ADC_ATTEN_0db ));
  50. //init wifi
  51. printf("nvs init\n");
  52. esp_err_t r = nvs_flash_init();
  53. if (r == ESP_ERR_NVS_NO_FREE_PAGES) {
  54. printf("no free pages, erase..\n");
  55. TEST_ESP_OK(nvs_flash_erase());
  56. r = nvs_flash_init();
  57. }
  58. TEST_ESP_OK( r);
  59. tcpip_adapter_init();
  60. TEST_ESP_OK(esp_event_loop_init(event_handler, NULL));
  61. wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
  62. TEST_ESP_OK(esp_wifi_init(&cfg));
  63. wifi_config_t wifi_config = {
  64. .sta = {
  65. .ssid = DEFAULT_SSID,
  66. .password = DEFAULT_PWD
  67. },
  68. };
  69. TEST_ESP_OK(esp_wifi_set_mode(WIFI_MODE_STA));
  70. TEST_ESP_OK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
  71. //test read value
  72. TEST_ESP_OK( adc2_get_raw( ADC2_CHANNEL_8, ADC_WIDTH_12Bit, &read_raw ));
  73. target_value = 30*4096*3/256; //3 = 3.3/1.1
  74. printf("dac set: %d, adc read: %d (target_value: %d)\n", 30, read_raw, target_value );
  75. TEST_ASSERT_INT_WITHIN( 600, target_value, read_raw );
  76. TEST_ESP_OK( adc2_get_raw( ADC2_CHANNEL_9, ADC_WIDTH_12Bit, &read_raw ));
  77. target_value = 60*4096*3/256;
  78. printf("dac set: %d, adc read: %d (target_value: %d)\n", 60, read_raw, target_value );
  79. TEST_ASSERT_INT_WITHIN( 600, target_value, read_raw );
  80. //now start wifi
  81. printf("wifi start...\n");
  82. TEST_ESP_OK(esp_wifi_start());
  83. //test reading during wifi on
  84. TEST_ASSERT_EQUAL( adc2_get_raw( ADC2_CHANNEL_8, ADC_WIDTH_12Bit, &read_raw ), ESP_ERR_TIMEOUT );
  85. TEST_ASSERT_EQUAL( adc2_get_raw( ADC2_CHANNEL_9, ADC_WIDTH_12Bit, &read_raw ), ESP_ERR_TIMEOUT );
  86. //wifi stop again
  87. printf("wifi stop...\n");
  88. TEST_ESP_OK( esp_wifi_stop() );
  89. TEST_ESP_OK(esp_wifi_deinit());
  90. nvs_flash_deinit();
  91. //test read value
  92. TEST_ESP_OK( adc2_get_raw( ADC2_CHANNEL_8, ADC_WIDTH_12Bit, &read_raw ));
  93. target_value = 30*4096*3/256; //3 = 3.3/1.1
  94. printf("dac set: %d, adc read: %d (target_value: %d)\n", 30, read_raw, target_value );
  95. TEST_ASSERT_INT_WITHIN( 600, target_value, read_raw );
  96. TEST_ESP_OK( adc2_get_raw( ADC2_CHANNEL_9, ADC_WIDTH_12Bit, &read_raw ));
  97. target_value = 60*4096*3/256;
  98. printf("dac set: %d, adc read: %d (target_value: %d)\n", 60, read_raw, target_value );
  99. TEST_ASSERT_INT_WITHIN( 600, target_value, read_raw );
  100. printf("test passed...\n");
  101. TEST_IGNORE_MESSAGE("this test case is ignored due to the critical memory leak of tcpip_adapter and event_loop.");
  102. }