1 /* 2 * Copyright (c) 2022 Winner Microelectronics Co., Ltd. All rights reserved. 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 * @file wm_pwm.h 18 * 19 * @brief pwm driver module 20 * 21 * @author dave 22 * 23 * Copyright (c) 2014 Winner Microelectronics Co., Ltd. 24 */ 25 #ifndef WM_PWM_H 26 #define WM_PWM_H 27 28 #include "wm_type_def.h" 29 30 /** pwm channel's maximum number */ 31 #define PWM_CHANNEL_MAX_NUM 5 32 33 /** pwm work mode for signal generate */ 34 enum tls_pwm_out_mode { 35 WM_PWM_OUT_MODE_BRAKE = 0, /**< brake mode */ 36 WM_PWM_OUT_MODE_ALLSYC, /**< all synchronous mode */ 37 WM_PWM_OUT_MODE_2SYC, /**< two channel synchronous mode */ 38 WM_PWM_OUT_MODE_MC, /**< complementary mode */ 39 WM_PWM_OUT_MODE_INDPT /**< independent mode */ 40 }; 41 42 /** interrupt type for capture mode */ 43 enum tls_pwm_cap_int_type { 44 WM_PWM_CAP_RISING_EDGE_INT, /**< rising edge arises the interrupt */ 45 WM_PWM_CAP_FALLING_EDGE_INT, /**< falling edge arises the interrupt */ 46 WM_PWM_CAP_RISING_FALLING_EDGE_INT, /**< both rising edge and falling edge arise the interrupt */ 47 WM_PWM_CAP_DMA_INT /**< dma request */ 48 }; 49 50 /** pwm output status */ 51 enum tls_pwm_out_en_state { 52 WM_PWM_OUT_EN_STATE_TRI, /**< set tristate status */ 53 WM_PWM_OUT_EN_STATE_OUT /**< set output status */ 54 }; 55 56 /** pwm count mode */ 57 enum tls_pwm_cnt_type { 58 WM_PWM_CNT_TYPE_EDGE_ALLGN_CAP, /**< edge alignment(only capture mode) */ 59 WM_PWM_CNT_TYPE_EDGE_ALIGN_OUT, /**< edge alignment(only output mode) */ 60 WM_PWM_CNT_TYPE_CENTER_ALIGN /**< central alignment */ 61 }; 62 63 /** pwm cycle type */ 64 enum tls_pwm_loop_type { 65 WM_PWM_LOOP_TYPE_SINGLE, /**< single mode */ 66 WM_PWM_LOOP_TYPE_LOOP /**< auto load */ 67 }; 68 69 /** pwm waveform inversion mode */ 70 enum tls_pwm_waveform_inversion { 71 WM_PWM_WAVEFORM_NOINVERSION, /**< not inverse */ 72 WM_PWM_WAVEFORM_INVERSION /**< inversion */ 73 }; 74 75 /** pwm output level in the brake mode */ 76 enum tls_pwm_brake_out_level { 77 WM_PWM_BRAKE_OUT_HIGH, /**< output high level */ 78 WM_PWM_BRAKE_OUT_LOW /**< output low level */ 79 }; 80 81 /** pwm initial parameters */ 82 typedef struct _pwm_init_param { 83 enum tls_pwm_out_mode mode; /**< work mode */ 84 u8 channel; /**< channel id 0~4 */ 85 u16 clkdiv; /**< clock divided value */ 86 u8 period; /**< period value(output frequency F = CLK/CLK_DIV/PERIOD) */ 87 u8 duty; /**< duty radio (range 0~255, high level or low level by out_inversion decided */ 88 bool dten; /**< enable dead zone time (ENABLE or DISABLE) */ 89 u8 dtclkdiv; /**< dead zone clock divided value (0~3) */ 90 u8 dtcnt; /**< period number of dead zone time (0~255) */ 91 enum tls_pwm_cnt_type cnt_type; /**< count type */ 92 enum tls_pwm_loop_type loop_type; /**< cycle type */ 93 bool inverse_en; /**< output is inverse */ 94 u8 pnum; /**< generate interrupt after pnum period */ 95 bool pnum_int; /**< period interrupt is enable */ 96 }pwm_init_param; 97 98 /** 99 * @defgroup Driver_APIs Driver APIs 100 * @brief Driver APIs 101 */ 102 103 /** 104 * @addtogroup Driver_APIs 105 * @{ 106 */ 107 108 /** 109 * @defgroup PWM_Driver_APIs PWM Driver APIs 110 * @brief PWM driver APIs 111 */ 112 113 /** 114 * @addtogroup PWM_Driver_APIs 115 * @{ 116 */ 117 118 /** 119 * @brief This function is used to register the pwm interrupt callback function 120 * 121 * @param[in] callback the pwm interrupt callback function 122 * 123 * @return None 124 * 125 * @note None 126 */ 127 void tls_pwm_isr_register(void (*callback)(void)); 128 129 /** 130 * @brief This function is used to set duty radio 131 * 132 * @param[in] channel pwm channel NO.,range form 0 to 4 133 * @param[in] duty Number of active levels 134 * 135 * @retval WM_SUCCESS success 136 * @retval WM_FAILED failed 137 * 138 * @note None 139 */ 140 int tls_pwm_duty_config(u8 channel, u8 duty); 141 142 /** 143 * @brief This function is used to set frequency 144 * 145 * @param[in] channel pwm channel NO., range form 0 to 4 146 * @param[in] clkdiv clock divider, range 0 to 65535 147 * @param[in] period the number of the counting clock cycle 148 * 149 * @retval WM_SUCCESS success 150 * @retval WM_FAILED failed 151 * 152 * @note None 153 */ 154 int tls_pwm_freq_config(u8 channel, u16 clkdiv, u8 period); 155 156 /** 157 * @brief This function is used to set the output mode 158 * 159 * @param[in] channel pwm channel NO., range form 0 to 4 160 * @param[in] mode pwm work mode for signal generate 161 * 162 * @retval WM_SUCCESS success 163 * @retval WM_FAILED failed 164 * 165 * @note None 166 */ 167 int tls_pwm_out_mode_config(u8 channel, enum tls_pwm_out_mode mode); 168 169 /** 170 * @brief This function is used to set the counting mode 171 * 172 * @param[in] channel pwm channel NO.,range form 0 to 4 173 * @param[in] cnt_type counting mode 174 * 175 * @retval WM_SUCCESS success 176 * @retval WM_FAILED failed 177 * 178 * @note None 179 */ 180 int tls_pwm_cnt_type_config(u8 channel, enum tls_pwm_cnt_type cnt_type); 181 182 /** 183 * @brief This function is used to set whether to loop 184 * 185 * @param[in] channel pwm channel NO.,range form 0 to 4 186 * @param[in] loop_mode whether to loop 187 * 188 * @retval WM_SUCCESS success 189 * @retval WM_FAILED failed 190 * 191 * @note None 192 */ 193 int tls_pwm_loop_mode_config(u8 channel, enum tls_pwm_loop_type loop_mode); 194 195 /** 196 * @brief This function is used to set whether to inverse the output 197 198 * 199 * @param[in] channel pwm channel NO.,range form 0 to 4 200 * @param[in] en ENABLE or DISABLE 201 * 202 * @retval WM_SUCCESS success 203 * @retval WM_FAILED failed 204 * 205 * @note None 206 */ 207 int tls_pwm_out_inverse_cmd(u8 channel, bool en); 208 209 /** 210 * @brief This function is used to set the number of period to be generated 211 * 212 * @param[in] channel pwm channel NO.,range form 0 to 4 213 * @param[in] pnum the number of period to be generated,range from 0 to 255 214 * 215 * @retval WM_SUCCESS success 216 * @retval WM_FAILED failed 217 * 218 * @note None 219 */ 220 int tls_pwm_stoptime_by_period_config(u8 channel, u8 pnum); 221 222 /** 223 * @brief This function is used to set output enable 224 * 225 * @param[in] channel pwm channel NO.,channel 0 or channel 4 226 * @param[in] en ENABLE or DISABLE 227 * 228 * @retval WM_SUCCESS success 229 * @retval WM_FAILED failed 230 * 231 * @note None 232 */ 233 int tls_pwm_output_en_cmd(u8 channel, bool en); 234 235 /** 236 * @brief This function is used to set the dead time 237 * 238 * @param[in] channel pwm channel NO.,channel 0 or channel 2 239 * @param[in] dten whether enalbe the deat time, ENABLE or DISABLE 240 * @param[in] dtclkdiv dead zone clock divider, range 0 to 3 241 * @param[in] dtcnt the number of the counting clock cycle, range 0 to 255 242 * 243 * @retval WM_SUCCESS success 244 * @retval WM_FAILED failed 245 * 246 * @note None 247 */ 248 int tls_pwm_deadzone_config(u8 channel, bool dten, u8 dtclkdiv, u8 dtcnt); 249 250 /** 251 * @brief This function is used to set whether to inverse the capture input 252 * 253 * @param[in] channel pwm channel NO.,channel 0 or channel 4 254 * @param[in] en ENABLE or DISABLE 255 * 256 * @retval WM_SUCCESS success 257 * @retval WM_FAILED failed 258 * 259 * @note None 260 */ 261 int tls_pwm_capture_inverse_cmd(u8 channel, bool en); 262 263 /** 264 * @brief This function is used to set break mode 265 * 266 * @param[in] channel pwm channel NO.,channel 0 or channel 4 267 * @param[in] en whether enable the break mode,ENABLE or DISABLE 268 * @param[in] brok when break 269 * 270 * @retval WM_SUCCESS success 271 * @retval WM_FAILED failed 272 * 273 * @note None 274 */ 275 int tls_pwm_brake_mode_config(u8 channel, bool en, enum tls_pwm_brake_out_level brok); 276 277 /** 278 * @brief This function is used to enable the capture mode 279 * 280 * @param[in] channel pwm channel NO.,channel 0 or channel 4 281 * 282 * @retval WM_SUCCESS success 283 * @retval WM_FAILED failed 284 * 285 * @note None 286 */ 287 int tls_pwm_capture_mode_config(u8 channel); 288 289 /** 290 * @brief This function is used to set the interrupt about the number of period 291 * 292 * @param[in] channel pwm channel,range from 0 to 4 293 * @param[in] en enble or disable 294 * 295 * @retval WM_SUCCESS success 296 * @retval WM_FAILED failed 297 * 298 * @note None 299 */ 300 int tls_pwm_stoptime_irq_cmd(u8 channel, bool en); 301 302 /** 303 * @brief This function is used to set the interrupt about the 304 capture 305 * 306 * @param[in] channel pwm channel,channel 0 or channel 4 307 * @param[in] int_type interrupt type 308 * 309 * @retval WM_SUCCESS success 310 * @retval WM_FAILED failed 311 * 312 * @note None 313 */ 314 int tls_pwm_capture_irq_type_config(u8 channel, enum tls_pwm_cap_int_type int_type); 315 316 /** 317 * @brief This function is used to initial pwm(out mode) 318 * 319 * @param[in] pwm_param structure containing the initialization parameters 320 * 321 * @retval WM_SUCCESS success 322 * @retval WM_FAILED failed 323 * 324 * @note None 325 */ 326 int tls_pwm_out_init(pwm_init_param *pwm_param); 327 328 /** 329 * @brief This function is used to initial pwm(capture mode) 330 * 331 * @param[in] channel pwm channel, channel 0 or channel 4 332 * @param[in] clkdiv clock divider, range 0 to 65535 333 * @param[in] inverse_en whether the input signal is reversed 334 * @param[in] int_type interrupt type 335 * 336 * @retval WM_SUCCESS success 337 * @retval WM_FAILED failed 338 * 339 * @note None 340 */ 341 int tls_pwm_cap_init(u8 channel, u16 clkdiv, bool inverse_en, enum tls_pwm_cap_int_type int_type); 342 343 /** 344 * @brief This function is used to start pwm 345 * 346 * @param[in] channel pwm channel, range from 0 to 4 347 * 348 * @retval WM_SUCCESS success 349 * @retval WM_FAILED failed 350 * 351 * @note None 352 */ 353 int tls_pwm_start(u8 channel); 354 355 /** 356 * @brief This function is used to stop pwm 357 * 358 * @param[in] channel pwm channel no, range form 0 to 4 359 * @param[in] freq frequency, range from 1 to 156250 360 * 361 * @return None 362 * 363 * @note None 364 */ 365 void tls_pwm_freq_set(u8 channel, u32 freq); 366 367 /** 368 * @brief This function is used to set duty radio 369 * 370 * @param[in] channel pwm channel NO., range form 0 to 4 371 * @param[in] duty duty radio, range from 0 to 255 372 * 373 * @return None 374 * 375 * @note None 376 */ 377 void tls_pwm_duty_set(u8 channel, u8 duty); 378 379 /** 380 * @brief This function is used to initial pwm 381 * 382 * @param[in] channel pwm channel, range from 0 to 4 383 * @param[in] freq is a pointer to frequency, freq range from 1 to 156250 384 * @param[in] duty is a pointer to duty radio, duty range from 0 to 255 385 * 386 * @retval WM_SUCCESS success 387 * @retval WM_FAILED failed 388 * 389 * @note None 390 */ 391 int tls_pwm_init(u8 channel, u32 freq, u8 duty, u8 pnum); 392 393 /** 394 * @brief This function is used to stop pwm 395 * 396 * @param[in] channel pwm channel, range from 0 to 4 397 * 398 * @retval WM_SUCCESS success 399 * @retval WM_FAILED failed 400 * 401 * @note None 402 */ 403 int tls_pwm_stop(u8 channel); 404 405 /** 406 * @} 407 */ 408 409 /** 410 * @} 411 */ 412 413 #endif /* WM_PWM_H */ 414