• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* Copyright 2022 Unionman Technology Co., Ltd.
2  *
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 
17 #include <cstdio>
18 #include <cstdlib>
19 #include <pthread.h>
20 #include <csignal>
21 #include <semaphore.h>
22 
23 #include "pwm_if.h"
24 #include "hdf_log.h"
25 #include "osal_irq.h"
26 #include "osal_time.h"
27 
28 #ifdef HDF_LOG_ON
29 #define LOGD(format, ...)                                     \
30     do {                                                      \
31             HDF_LOGD("[%{public}s:%{public}d] " format "\n",  \
32                 __FUNCTION__, __LINE__,                       \
33                 ##__VA_ARGS__);                               \
34     } while (0)
35 
36 #define LOGI(format, ...)                                                             \
37     do {                                                                              \
38             HDF_LOGI("[%{public}s:%{public}d] " format "\n", __FUNCTION__, __LINE__,  \
39                 ##__VA_ARGS__);                                                       \
40     } while (0)
41 
42 #define LOGW(format, ...)                                                         \
43     do {                                                                          \
44         HDF_LOGW("[%{public}s:%{public}d] " format "\n", __FUNCTION__, __LINE__,  \
45             ##__VA_ARGS__);                                                       \
46     } while (0)
47 
48 #define LOGE(format, ...)                                   \
49     do {                                                    \
50         HDF_LOGE(                                           \
51             "\033[0;32;31m"                                 \
52             "[%{public}s:%{public}d] " format "\033[m"      \
53             "\n",                                           \
54             __FUNCTION__,  __LINE__, ##__VA_ARGS__);        \
55     } while (0)
56 #else
57 #define LOGD(format, ...)                                   \
58     do {                                                    \
59             printf("[%s:%d] " format "\n",                  \
60                 __FUNCTION__, __LINE__,                     \
61                 ##__VA_ARGS__);                             \
62     } while (0)
63 
64 #define LOGI(format, ...)                                       \
65     do {                                                        \
66         printf("[%s:%d] " format "\n", __FUNCTION__, __LINE__,  \
67             ##__VA_ARGS__);                                     \
68     } while (0)
69 
70 #define LOGW(format, ...)                                       \
71     do {                                                        \
72         printf("[%s:%d] " format "\n", __FUNCTION__, __LINE__,  \
73             ##__VA_ARGS__);                                     \
74     } while (0)
75 
76 #define LOGE(format, ...)                             \
77     do {                                              \
78         printf("\033[0;32;31m"                        \
79             "[%s:%d] " format "\033[m"                \
80             "\n",                                     \
81             __FUNCTION__,  __LINE__, ##__VA_ARGS__);  \
82     } while (0)
83 #endif
84 
85 #define PWM_NUM_0 (0)
86 #define PWM_NUM_2 (2)
87 
88 static DevHandle g_pwmHandle = nullptr;
89 
SignalHandler(int32_t sig)90 static void SignalHandler(int32_t sig)
91 {
92     if (sig == SIGINT) {
93         if (g_pwmHandle != nullptr) {
94             (void)PwmDisable(g_pwmHandle);
95             PwmClose(g_pwmHandle);
96         }
97 
98         LOGI("\nCatch Ctrl + C, waitting for exit.\n");
99     }
100 
101     return ;
102 }
103 
main(int argc,char * argv[])104 int32_t main(int argc, char *argv[])
105 {
106     int32_t ret;
107     uint32_t num;
108     struct PwmConfig pcfg;
109     struct sigaction sigAction;
110 
111     memset(&sigAction, 0x0, sizeof(struct sigaction));
112     sigAction.sa_handler = SignalHandler;
113     sigemptyset(&sigAction.sa_mask);
114     sigAction.sa_flags = 0;
115     sigaction(SIGINT, &sigAction, nullptr);
116     sigAction.sa_handler = SIG_IGN;
117     sigaction(SIGPIPE, &sigAction, nullptr);
118 
119     if (argc > 1) {
120         num = atoi(argv[1]);
121         if ((num != PWM_NUM_0) && (num != PWM_NUM_2)) {
122             LOGE("Only support pwm num 0 and 2!\n");
123             return HDF_FAILURE;
124         }
125     } else {
126         num = 0; /* 0 - PWM_1, 2 - PWM_2 */
127     }
128 
129     LOGD("PwmOpen PWM_%d (for Unionpi Tiger)\n", (num == 0) ? 1 : 2);
130 
131     g_pwmHandle = PwmOpen(num);
132     if (g_pwmHandle == NULL) {
133         LOGE("PwmOpen: failed!\n");
134         return HDF_FAILURE;
135     }
136 
137     ret = PwmSetPeriod(g_pwmHandle, 50000000);
138     if (ret != HDF_SUCCESS) {
139         HDF_LOGE("PwmSetPeriod: failed, ret %d\n", ret);
140         PwmClose(g_pwmHandle);
141         return HDF_FAILURE;
142     }
143 
144     ret = PwmSetDuty(g_pwmHandle, 25000000);
145     if (ret != HDF_SUCCESS) {
146         LOGE("PwmSetDuty: failed, ret %d\n", ret);
147         PwmClose(g_pwmHandle);
148         return HDF_FAILURE;
149     }
150 
151     ret = PwmSetPolarity(g_pwmHandle, PWM_INVERTED_POLARITY);
152     if (ret != HDF_SUCCESS) {
153         LOGE("PwmSetPolarity: failed, ret %d\n", ret);
154         PwmClose(g_pwmHandle);
155         return HDF_FAILURE;
156     }
157 
158     ret = PwmEnable(g_pwmHandle);
159     if (ret != HDF_SUCCESS) {
160         LOGE("PwmEnable: failed, ret %d\n", ret);
161         PwmClose(g_pwmHandle);
162         return HDF_FAILURE;
163     }
164 
165     pcfg.duty = 10000000; /* 占空时间为10000000纳秒 */
166     pcfg.period = 40000000; /* 周期为40000000纳秒 */
167     pcfg.polarity = PWM_NORMAL_POLARITY; /* 极性为正 */
168     pcfg.status = PWM_ENABLE_STATUS; /* 运行状态为启用 */
169     ret = PwmSetConfig(g_pwmHandle, &pcfg);
170     if (ret != HDF_SUCCESS) {
171         LOGE("PwmSetConfig: failed, ret %d\n", ret);
172         PwmClose(g_pwmHandle);
173         return HDF_FAILURE;
174     }
175 
176     LOGD("Watting <Enter> to finish ...\n");
177     getchar();
178 
179     ret = PwmDisable(g_pwmHandle);
180     if (ret != HDF_SUCCESS) {
181         LOGE("PwmDisable: failed, ret %d\n", ret);
182         PwmClose(g_pwmHandle);
183         return HDF_FAILURE;
184     }
185 
186     PwmClose(g_pwmHandle);
187 
188     return HDF_SUCCESS;
189 }