1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <aos/errno.h>
8 #include <aos/kernel.h>
9 #include <k_api.h>
10 #include "aos/hal/pwm.h"
11 #include "aos/cli.h"
12
pwm_test_process()13 static int32_t pwm_test_process()
14 {
15 int32_t ret = 0;
16 pwm_dev_t pwm = {0, {0.0, 25}, NULL};
17 for (int k = 0; k < 1; k++) {
18 pwm.port = k;
19 printf("\r\n=====pwm test : testing...===\r\n");
20 ret = hal_pwm_init(&pwm);
21 if (ret) {
22 printf("\r\n=====pwm test : pwm init failed===\r\n");
23 return -1;
24 }
25
26 ret = hal_pwm_start(&pwm);
27 if (ret) {
28 printf("\r\n=====pwm test : pwm start failed===\r\n");
29 return -1;
30 }
31
32 pwm_config_t para = {0.0, 25};
33 for (int j = 0; j < 2; j++) {
34 for (int i = 0; i < 10; i++) {
35 para.duty_cycle = 0.1 * i;
36 hal_pwm_para_chg(&pwm, para);
37 osDelay(100);
38 }
39 for (int i = 10; i > 0; i--) {
40 para.duty_cycle = 0.01 * i;
41 hal_pwm_para_chg(&pwm, para);
42 osDelay(100);
43 }
44 }
45 hal_pwm_stop(&pwm);
46 hal_pwm_finalize(&pwm);
47 }
48 printf("=====pwm test : Done=====\r\n");
49
50 return 0;
51
52 }
53
pwm_autotest()54 static int pwm_autotest()
55 {
56 int32_t ret = 0;
57
58 printf("\r\n\r\n");
59 printf("***************************************************************\r\n");
60 printf("*************************** PWM Test **************************\r\n");
61 printf("***************************************************************\r\n");
62 printf("***************************************************************\r\n");
63 printf("=====pwm test : Start=====\r\n");
64
65 ret = pwm_test_process();
66 if (ret) {
67 printf("\r\n=====pwm test : Failed ===\r\n");
68 goto fail;
69 }
70
71 printf("===Result : pwm test PASS !!! ===\r\n");
72 return 0;
73
74 fail:
75 printf("===Warning: pwm test FAIL !!! ===\r\n");
76 return -1;
77 }
78
handle_pwmtest_cmd(char * pwbuf,int blen,int argc,char ** argv)79 static void handle_pwmtest_cmd(char *pwbuf, int blen, int argc, char **argv)
80 {
81 pwm_autotest();
82 }
83
84 static struct cli_command pwmtest_cmd = {
85 .name = "pwmtest",
86 .help = "pwmtest",
87 .function = handle_pwmtest_cmd
88 };
89
pwm_test()90 int pwm_test()
91 {
92 aos_cli_register_command(&pwmtest_cmd);
93 return pwm_autotest();
94 }
95