1 /*
2 * Copyright (c) 2022 HiSilicon (Shanghai) Technologies CO., LIMITED.
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 <unistd.h>
18
19 #include "ohos_init.h"
20 #include "cmsis_os2.h"
21 #include "iot_gpio.h"
22 #include "iot_gpio_ex.h"
23 #include "iot_watchdog.h"
24 #include "iot_pwm.h"
25
26 #define LED_INTERVAL_TIME_US 300000
27 #define LED_TASK_STACK_SIZE 512
28 #define LED_TASK_PRIO 25
29
30 static int g_beepState = 1;
31 static int g_iState = 0;
32 #define IOT_PWM_PORT_PWM0 0
33 #define IOT_PWM_BEEP 9
34 #define IOT_GPIO_KEY 8
35
PWMBeepTask(const char *arg)36 static void *PWMBeepTask(const char *arg)
37 {
38 while (1) {
39 if (g_beepState) {
40 IoTPwmStart(IOT_PWM_PORT_PWM0, 50, 4000); /* 占空比50 / 4000,频率4000 */
41 } else {
42 IoTPwmStop(IOT_PWM_PORT_PWM0);
43 }
44 if (g_iState == 0xffff) {
45 g_iState = 0;
46 break;
47 }
48 }
49 }
50
OnButtonPressed(const char *arg)51 static void OnButtonPressed(const char *arg)
52 {
53 (void) arg;
54 g_beepState = !g_beepState;
55 printf("PRESS_KEY!\n");
56 g_iState++;
57 }
58
StartPWMBeepTask(void)59 static void StartPWMBeepTask(void)
60 {
61 osThreadAttr_t attr;
62
63 IoTGpioInit(IOT_GPIO_KEY);
64 IoSetFunc(IOT_GPIO_KEY, 0);
65 IoTGpioSetDir(IOT_GPIO_KEY, IOT_GPIO_DIR_IN);
66 IoSetPull(IOT_GPIO_KEY, IOT_IO_PULL_UP);
67 IoTGpioRegisterIsrFunc(IOT_GPIO_KEY, IOT_INT_TYPE_EDGE, IOT_GPIO_EDGE_FALL_LEVEL_LOW, OnButtonPressed, NULL);
68 IoTGpioInit(IOT_PWM_BEEP);
69 IoSetFunc(IOT_PWM_BEEP, 5); /* 设置IO5的功能 */
70 IoTGpioSetDir(IOT_PWM_BEEP, IOT_GPIO_DIR_OUT);
71 IoTPwmInit(IOT_PWM_PORT_PWM0);
72 IoTWatchDogDisable();
73
74 attr.name = "PWMBeepTask";
75 attr.attr_bits = 0U;
76 attr.cb_mem = NULL;
77 attr.cb_size = 0U;
78 attr.stack_mem = NULL;
79 attr.stack_size = 1024; /* 堆栈大小为1024 */
80 attr.priority = osPriorityNormal;
81
82 if (osThreadNew((osThreadFunc_t)PWMBeepTask, NULL, &attr) == NULL) {
83 printf("[StartPWMBeepTask] Failed to create PWMBeepTask!\n");
84 }
85 }
86
87 APP_FEATURE_INIT(StartPWMBeepTask);