test_sigmadelta.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /*
  2. * SPDX-FileCopyrightText: 2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/task.h"
  8. #include "unity.h"
  9. #include "test_utils.h"
  10. #include "soc/soc_caps.h"
  11. #include "driver/sigmadelta.h"
  12. TEST_CASE("SigmaDelta config test", "[sigma_delta]")
  13. {
  14. sigmadelta_config_t sigmadelta_cfg = {
  15. .sigmadelta_prescale = 80,
  16. .sigmadelta_duty = 45,
  17. .sigmadelta_gpio = 4,
  18. };
  19. for (int i = 0; i < SOC_SIGMADELTA_CHANNEL_NUM; i++) {
  20. sigmadelta_cfg.channel = i;
  21. TEST_ESP_OK(sigmadelta_config(&sigmadelta_cfg));
  22. }
  23. sigmadelta_cfg.channel = SOC_SIGMADELTA_CHANNEL_NUM;
  24. TEST_ASSERT_EQUAL_MESSAGE(ESP_ERR_INVALID_ARG, sigmadelta_config(&sigmadelta_cfg), "wrong channel number should be inspected");
  25. }
  26. // connect GPIO4 with LED positive pin, and the GND pin connect LED negative pin
  27. // logic analyzer help also to see the wave form(more standard and accurate)
  28. TEST_CASE("SigmaDelta pin, duty, prescale set", "[sigma_delta][ignore]")
  29. {
  30. sigmadelta_config_t sigmadelta_cfg = {
  31. .channel = 0,
  32. .sigmadelta_prescale = 80,
  33. .sigmadelta_duty = 0,
  34. .sigmadelta_gpio = 4,
  35. };
  36. TEST_ESP_OK(sigmadelta_config(&sigmadelta_cfg));
  37. int8_t duty = 0;
  38. int inc = 1;
  39. for (int i = 0; i < 1000; i++) {
  40. sigmadelta_set_duty(sigmadelta_cfg.channel, duty);
  41. vTaskDelay(10 / portTICK_PERIOD_MS);
  42. duty += inc;
  43. if (duty == 127 || duty == -127) {
  44. inc = (-1) * inc;
  45. }
  46. }
  47. TEST_ESP_OK(sigmadelta_set_prescale(0, 200));
  48. for (int i = 0; i < 1000; i++) {
  49. sigmadelta_set_duty(sigmadelta_cfg.channel, duty);
  50. vTaskDelay(10 / portTICK_PERIOD_MS);
  51. duty += inc;
  52. if (duty == 127 || duty == -127) {
  53. inc = (-1) * inc;
  54. }
  55. }
  56. TEST_ESP_OK(sigmadelta_set_pin(sigmadelta_cfg.channel, 5));
  57. vTaskDelay(3000 / portTICK_PERIOD_MS);
  58. }