• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *    http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 
18 #include <unistd.h>
19 
20 #include "cmsis_os2.h"
21 #include "iot_gpio.h"
22 #include "iot_gpio_ex.h"
23 #include "iot_pwm.h"
24 #include "ohos_init.h"
25 
26 #define LED_GPIO 2
27 #define THREAD_STACK_SIZE (1024 * 4)
28 #define THREAD_PRIO 25
29 
30 #define PWM_CHANGE_TIMES 100
31 #define PWM_FREQ 40000
32 #define PWM_DELAY_10US 10
33 
34 /**
35  * @brief pwm task output PWM with different duty cycle
36  *
37  */
PwmTask(void)38 static void PwmTask(void)
39 {
40     unsigned int i;
41 
42     // init gpio of LED
43     IoTGpioInit(LED_GPIO);
44 
45     // set the GPIO_2 multiplexing function to PWM
46     IoTGpioSetFunc(LED_GPIO, IOT_GPIO_FUNC_GPIO_2_PWM2_OUT);
47 
48     // set GPIO_2 is output mode
49     IoTGpioSetDir(LED_GPIO, IOT_GPIO_DIR_OUT);
50 
51     // init PWM2
52     IoTPwmInit(LED_GPIO);
53 
54     while (1) {
55         for (i = 0; i < PWM_CHANGE_TIMES; i++) {
56             // output PWM with different duty cycle
57             IoTPwmStart(LED_GPIO, i, PWM_FREQ);
58             usleep(PWM_DELAY_10US);
59         }
60         i = 0;
61     }
62 }
63 
64 /**
65  * @brief Main Entry of the Pwm Example
66  *
67  */
PwmExampleEntry(void)68 static void PwmExampleEntry(void)
69 {
70     osThreadAttr_t attr;
71 
72     attr.name = "PwmTask";
73     attr.attr_bits = 0U;
74     attr.cb_mem = NULL;
75     attr.cb_size = 0U;
76     attr.stack_mem = NULL;
77     attr.stack_size = THREAD_STACK_SIZE;
78     attr.priority = THREAD_PRIO;
79 
80     if (osThreadNew((osThreadFunc_t)PwmTask, NULL, &attr) == NULL) {
81         printf("Failed to create PwmTask!\n");
82     }
83 }
84 
85 APP_FEATURE_INIT(PwmExampleEntry);