• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2020 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  * Description: PWM Sample Source. \n
16  *
17  * History: \n
18  * 2023-06-27, Create file. \n
19  */
20 #include "pinctrl.h"
21 #include "pwm.h"
22 #include "tcxo.h"
23 #include "soc_osal.h"
24 #include "app_init.h"
25 
26 #define TEST_TCXO_DELAY_1000MS     1000
27 
28 #define PWM_TASK_PRIO              24
29 #define PWM_TASK_STACK_SIZE        0x1000
30 
pwm_sample_callback(uint8_t channel)31 static errcode_t pwm_sample_callback(uint8_t channel)
32 {
33     osal_printk("PWM %d, cycle done. \r\n", channel);
34     return ERRCODE_SUCC;
35 }
36 
pwm_task(const char * arg)37 static void *pwm_task(const char *arg)
38 {
39     UNUSED(arg);
40     pwm_config_t cfg_no_repeat = {
41         100,
42         100,
43         0,
44         0xFF,
45         false
46     };
47 
48     uapi_pin_set_mode(CONFIG_PWM_PIN, CONFIG_PWM_PIN_MODE);
49     uapi_pwm_deinit();
50     uapi_pwm_init();
51     uapi_pwm_open(CONFIG_PWM_CHANNEL, &cfg_no_repeat);
52 
53     uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
54     uapi_pwm_unregister_interrupt(CONFIG_PWM_CHANNEL);
55     uapi_pwm_register_interrupt(CONFIG_PWM_CHANNEL, pwm_sample_callback);
56 #ifdef CONFIG_PWM_USING_V151
57     uint8_t channel_id = CONFIG_PWM_CHANNEL;
58     /* channel_id can also choose to configure multiple channels, and the third parameter also needs to be adjusted
59         accordingly. */
60     uapi_pwm_set_group(CONFIG_PWM_GROUP_ID, &channel_id, 1);
61     /* Here you can also call the uapi_pwm_start interface to open each channel individually. */
62     uapi_pwm_start_group(CONFIG_PWM_GROUP_ID);
63 #else
64     uapi_pwm_start(CONFIG_PWM_CHANNEL);
65 #endif
66 
67     uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
68 #ifdef CONFIG_PWM_USING_V151
69     uapi_pwm_close(CONFIG_PWM_GROUP_ID);
70 #else
71     uapi_pwm_close(CONFIG_PWM_CHANNEL);
72 #endif
73 
74     uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS);
75     uapi_pwm_deinit();
76     return NULL;
77 }
78 
pwm_entry(void)79 static void pwm_entry(void)
80 {
81     osal_task *task_handle = NULL;
82     osal_kthread_lock();
83     task_handle = osal_kthread_create((osal_kthread_handler)pwm_task, 0, "PwmTask", PWM_TASK_STACK_SIZE);
84     if (task_handle != NULL) {
85         osal_kthread_set_priority(task_handle, PWM_TASK_PRIO);
86     }
87     osal_kthread_unlock();
88 }
89 
90 /* Run the pwm_entry. */
91 app_run(pwm_entry);