wm_pwm.h 11 KB

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