• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 Copyright (C) 2024 HiHope Open Source Organization .
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 #include "ohos_init.h"
18 #include "cmsis_os2.h"
19 
20 #include "iot_gpio.h"
21 #include "iot_gpio_ex.h"
22 #include "iot_pwm.h"
23 
24 #define PWM_GPIO IOT_IO_NAME_GPIO_10    // PWM对应的GPIO引脚
25 #define PWM_PORT 2                      // PWM端口
26 #define Frequency 1230   //pwm接口不能输出低于1230hz的pwm,只能通过IO模拟的方式输出
27 
PwmTask(void * arg)28 static void PwmTask(void *arg)
29 {
30     (void) arg;
31 
32     // 初始化GPIO模块
33     IoTGpioInit(PWM_GPIO);
34     // 设置GPIO10为PWM模式
35     uint32_t ret = IoSetFunc(PWM_GPIO, 1);
36     printf("[PwmExample] IoSetFunc ret: %d\r\n", ret);
37     // 初始化PWM模块
38     ret = IoTPwmInit(PWM_PORT);
39     printf("[PwmExample] IoTPwmInit ret: %d\r\n", ret);
40     uint32_t counter  = 0;
41     uint32_t running = 1;
42 
43     while (running) {
44         for (int i = 0; i < 100; i++) {
45             IoTPwmStart(PWM_PORT, i, Frequency);
46             osDelay(5);
47             uapi_pwm_clear_group(PWM_PORT);
48         }
49         counter++;
50         if (counter >= 100) {
51             running = 0;
52         }
53     }
54 }
55 
56 // 入口函数
PwmEntry(void)57 static void PwmEntry(void)
58 {
59     // 定义线程属性
60     osThreadAttr_t attr;
61     attr.name = "PwmTask";
62     attr.stack_size = 2048;
63     attr.priority = osPriorityNormal;
64     attr.attr_bits = 0U;
65     attr.cb_mem = NULL;
66     attr.cb_size = 0U;
67     attr.stack_mem = NULL;
68 
69     // 创建线程
70     if (osThreadNew(PwmTask, NULL, &attr) == NULL) {
71         printf("[PwmExample] Falied to create PwmTask!\n");
72     }
73 }
74 
75 // 运行入口函数
76 SYS_RUN(PwmEntry);