1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <stdio.h>
6 #include <stdint.h>
7 #include <string.h>
8 #include <stdlib.h>
9 
10 #include "aos/kernel.h"
11 #include "aos/hal/pwm.h"
12 #include "pwm_test.h"
13 
14 #define PWM_TESTCASE_NUM 10
15 
16 pwm_dev_t    pwm_dev;
17 pwm_config_t config;
18 
19 pwm_config_t testsace_config[PWM_TESTCASE_NUM] = {{.freq = 1000, .duty_cycle = 0.5f},
20                                                   {.freq = 1000, .duty_cycle = 0.0f},
21                                                   {.freq = 1000, .duty_cycle = 0.3f},
22                                                   {.freq = 1000, .duty_cycle = 0.7f},
23                                                   {.freq = 1000, .duty_cycle = 1.0f},
24                                                   {.freq = 2000, .duty_cycle = 0.5f},
25                                                   {.freq = 2000, .duty_cycle = 0.0f},
26                                                   {.freq = 2000, .duty_cycle = 0.3f},
27                                                   {.freq = 2000, .duty_cycle = 0.7f},
28                                                   {.freq = 2000, .duty_cycle = 1.0f}};
29 
hal_pwm_test_init(void)30 void hal_pwm_test_init(void)
31 {
32     int i = 0;
33 
34     printf("*********** pwm test start ! ***********\n");
35 
36     printf("enter the testcase you want to test: \n");
37     printf("for example: enter \"pwm_testcase1\" to test frequence 1000hz duty_cycle 50%%\n");
38 
39     for (i = 0; i < PWM_TESTCASE_NUM; ++i) {
40         printf("pwm_testcase%d [frequence:%dhz  duty_cycle:%d%% ]\n",(i + 1) ,testsace_config[i].freq, (int)(testsace_config[i].duty_cycle * 100));
41     }
42 }
43 
hal_pwm_test_output(char * msg)44 void hal_pwm_test_output(char *msg)
45 {
46     int ret = -1;
47     int num = 0;
48 
49     num = atoi(msg);
50 
51     if (num > PWM_TESTCASE_NUM) {
52         printf("testcase number error !\n");
53         return;
54     }
55 
56     printf("pwm output frequence: %dhz duty_cycle: %d%%\n", testsace_config[num - 1].freq, (int)(testsace_config[num - 1].duty_cycle * 100));
57 
58     hal_pwm_stop(&pwm_dev);
59 
60     pwm_dev.port = PORT_PWM_TEST;
61     pwm_dev.config.freq = testsace_config[num - 1].freq;
62     pwm_dev.config.duty_cycle = testsace_config[num - 1].duty_cycle;
63     ret = hal_pwm_init(&pwm_dev);
64     if (ret != 0) {
65         printf("hal_pwm_init error !\n");
66         return;
67     }
68 
69     hal_pwm_start(&pwm_dev);
70 
71     printf("please observe the output waveform through the oscilloscope to judge whether it is correct !\n");
72 }
73 
hal_pwm_test_finalize(void)74 void hal_pwm_test_finalize(void)
75 {
76     int ret = -1;
77 
78     ret = hal_pwm_finalize(&pwm_dev);
79     if (ret != 0) {
80         printf("hal_pwm_finalize error !\n");
81         return;
82     }
83 
84     printf("pwm test result: succeed !\n");
85 
86     printf("*********** pwm test end ! ***********\n");
87 }
88