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 
13 TEST_CASE("SigmaDelta config test", "[sigma_delta]")
14 {
15     sigmadelta_config_t sigmadelta_cfg = {
16         .sigmadelta_prescale = 80,
17         .sigmadelta_duty = 45,
18         .sigmadelta_gpio = 4,
19     };
20     for (int i = 0; i < SOC_SIGMADELTA_CHANNEL_NUM; i++) {
21         sigmadelta_cfg.channel = i;
22         TEST_ESP_OK(sigmadelta_config(&sigmadelta_cfg));
23     }
24 
25     sigmadelta_cfg.channel = SOC_SIGMADELTA_CHANNEL_NUM;
26     TEST_ASSERT_EQUAL_MESSAGE(ESP_ERR_INVALID_ARG, sigmadelta_config(&sigmadelta_cfg), "wrong channel number should be inspected");
27 }
28 
29 // connect GPIO4 with LED positive pin, and the GND pin connect LED negative pin
30 // logic analyzer help also to see the wave form(more standard and accurate)
31 TEST_CASE("SigmaDelta pin, duty, prescale set", "[sigma_delta][ignore]")
32 {
33     sigmadelta_config_t sigmadelta_cfg = {
34         .channel = 0,
35         .sigmadelta_prescale = 80,
36         .sigmadelta_duty = 0,
37         .sigmadelta_gpio = 4,
38     };
39     TEST_ESP_OK(sigmadelta_config(&sigmadelta_cfg));
40 
41     int8_t duty = 0;
42     int inc = 1;
43     for (int i = 0; i < 1000; i++) {
44         sigmadelta_set_duty(sigmadelta_cfg.channel, duty);
45         vTaskDelay(10 / portTICK_PERIOD_MS);
46 
47         duty += inc;
48         if (duty == 127 || duty == -127) {
49             inc = (-1) * inc;
50         }
51     }
52 
53     TEST_ESP_OK(sigmadelta_set_prescale(0, 200));
54     for (int i = 0; i < 1000; i++) {
55         sigmadelta_set_duty(sigmadelta_cfg.channel, duty);
56         vTaskDelay(10 / portTICK_PERIOD_MS);
57 
58         duty += inc;
59         if (duty == 127 || duty == -127) {
60             inc = (-1) * inc;
61         }
62     }
63 
64     TEST_ESP_OK(sigmadelta_set_pin(sigmadelta_cfg.channel, 5));
65     vTaskDelay(3000 / portTICK_PERIOD_MS);
66 }
67