wm_pwm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882
  1. /**
  2. * @file wm_pwm.c
  3. *
  4. * @brief pwm driver module
  5. *
  6. * @author dave
  7. *
  8. * Copyright (c) 2014 Winner Microelectronics Co., Ltd.
  9. */
  10. #include <string.h>
  11. #include "wm_debug.h"
  12. #include "wm_regs.h"
  13. #include "wm_irq.h"
  14. #include "wm_pwm.h"
  15. #include "wm_gpio.h"
  16. #include "wm_cpu.h"
  17. #include "tls_common.h"
  18. typedef void (*pwm_irq_callback)(void);
  19. static pwm_irq_callback pwm_callback;
  20. ATTRIBUTE_ISR void PWM_IRQHandler(void)
  21. {
  22. csi_kernel_intrpt_enter();
  23. if (pwm_callback)
  24. pwm_callback();
  25. csi_kernel_intrpt_exit();
  26. }
  27. /**
  28. * @brief This function is used to register the pwm interrupt callback function
  29. *
  30. * @param[in] callback the pwm interrupt callback function
  31. *
  32. * @return None
  33. *
  34. * @note None
  35. */
  36. void tls_pwm_isr_register(void (*callback)(void))
  37. {
  38. pwm_callback = callback;
  39. tls_irq_enable(PWM_IRQn);
  40. }
  41. /**
  42. * @brief This function is used to set duty ratio
  43. *
  44. * @param[in] channel pwm channel NO.,range form 0 to 4
  45. * @param[in] duty Number of active levels
  46. *
  47. * @retval WM_SUCCESS success
  48. * @retval WM_FAILED failed
  49. *
  50. * @note None
  51. */
  52. int tls_pwm_duty_config(u8 channel, u8 duty)
  53. {
  54. u32 temp = 0;
  55. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  56. {
  57. TLS_DBGPRT_ERR("duty param err\n");
  58. return WM_FAILED;
  59. }
  60. if (duty == 0)
  61. {
  62. tls_pwm_stop(channel);
  63. return WM_SUCCESS;
  64. }
  65. if (4 == channel)
  66. {
  67. temp = tls_reg_read32(HR_PWM_CH4_REG2) & ~0x0000FF00;
  68. temp |= (duty << 8);
  69. tls_reg_write32(HR_PWM_CH4_REG2, temp); /* duty radio */
  70. }
  71. else
  72. {
  73. temp = tls_reg_read32(HR_PWM_CMPDAT) & ~(0xFF << channel * 8);
  74. temp |= (duty << (channel * 8));
  75. tls_reg_write32(HR_PWM_CMPDAT, temp); /* duty radio */
  76. }
  77. return WM_SUCCESS;
  78. }
  79. /**
  80. * @brief This function is used to set frequency
  81. *
  82. * @param[in] channel pwm channel NO., range form 0 to 4
  83. * @param[in] clkdiv clock divider, range 0 to 65535
  84. * @param[in] period the number of the counting clock cycle
  85. *
  86. * @retval WM_SUCCESS success
  87. * @retval WM_FAILED failed
  88. *
  89. * @note None
  90. */
  91. int tls_pwm_freq_config(u8 channel, u16 clkdiv, u8 period)
  92. {
  93. u32 temp = 0;
  94. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  95. {
  96. TLS_DBGPRT_ERR("freq param err\n");
  97. return WM_FAILED;
  98. }
  99. if (4 == channel)
  100. {
  101. temp = tls_reg_read32(HR_PWM_CH4_REG1) & ~0xFFFF0000;
  102. temp |= (clkdiv << 16);
  103. tls_reg_write32(HR_PWM_CH4_REG1, temp);/* clock divider */
  104. temp = tls_reg_read32(HR_PWM_CH4_REG1) & ~0x0000FF00;
  105. temp |= (period << 8);
  106. tls_reg_write32(HR_PWM_CH4_REG1, temp); /* the number of the counting clock cycle */
  107. }
  108. else
  109. {
  110. temp = tls_reg_read32(HR_PWM_CLKDIV01 + (channel / 2) * 4) & ~(0xFFFF << ((channel % 2) * 16));
  111. temp |= (clkdiv << ((channel % 2) * 16));
  112. tls_reg_write32(HR_PWM_CLKDIV01 + (channel / 2) * 4, temp);/* clock divider */
  113. temp = tls_reg_read32(HR_PWM_PERIOD) & ~(0xFF << channel * 8);
  114. temp |= (period << (channel * 8));
  115. tls_reg_write32(HR_PWM_PERIOD, temp);/* the number of the counting clock cycle */
  116. }
  117. return WM_SUCCESS;
  118. }
  119. /**
  120. * @brief This function is used to set the output mode
  121. *
  122. * @param[in] channel pwm channel NO.,range form 0 to 4
  123. * @param[in] mode pwm work mode for signal generate
  124. *
  125. * @retval WM_SUCCESS success
  126. * @retval WM_FAILED failed
  127. *
  128. * @note None
  129. */
  130. int tls_pwm_out_mode_config(u8 channel, enum tls_pwm_out_mode mode)
  131. {
  132. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  133. return WM_FAILED;
  134. if (WM_PWM_OUT_MODE_BRAKE == mode)
  135. tls_reg_write32(HR_PWM_BRKCTL, tls_reg_read32(HR_PWM_BRKCTL) | BIT(11 + channel));/* the brake mode */
  136. else if (WM_PWM_OUT_MODE_ALLSYC == mode)
  137. {
  138. if (channel != 0)
  139. return WM_FAILED;
  140. tls_reg_write32(HR_PWM_BRKCTL, tls_reg_read32(HR_PWM_BRKCTL) & ~0xF800); /* disable the brake mode */
  141. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(6)); /* enable the all synchronous mode mode */
  142. }
  143. else if (WM_PWM_OUT_MODE_2SYC == mode)
  144. {
  145. if (channel != 0 && channel != 2)
  146. return WM_FAILED;
  147. tls_reg_write32(HR_PWM_BRKCTL, tls_reg_read32(HR_PWM_BRKCTL) & ~(0x1800<<channel)); /* disable the brake mode */
  148. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & ~BIT(6)); /* disable the all synchronous mode mode */
  149. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(14 + channel / 2)); /* enable the two channel synchronous mode */
  150. }
  151. else if (WM_PWM_OUT_MODE_MC == mode)
  152. {
  153. if (channel != 0 && channel != 2)
  154. return WM_FAILED;
  155. tls_reg_write32(HR_PWM_BRKCTL, tls_reg_read32(HR_PWM_BRKCTL) & ~(0x1800<<channel));/* disable the brake mode */
  156. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & ~BIT(6)); /* disable the all synchronous mode mode */
  157. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & ~BIT(14 + channel / 2)); /* disable the two channel synchronous mode */
  158. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(0 + channel / 2)); /* enable the complementary mode */
  159. }
  160. else if(WM_PWM_OUT_MODE_INDPT == mode)
  161. {
  162. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(6)));
  163. if (channel != 4 )
  164. {
  165. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(0 + channel / 2)));
  166. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(14 + channel / 2)));
  167. }
  168. tls_reg_write32(HR_PWM_BRKCTL, tls_reg_read32(HR_PWM_BRKCTL) & (~BIT(11 + channel))); /* enable the independent mode */
  169. }
  170. else
  171. return WM_FAILED;
  172. return WM_SUCCESS;
  173. }
  174. /**
  175. * @brief This function is used to set the counting mode
  176. *
  177. * @param[in] channel pwm channel NO.,range form 0 to 4
  178. * @param[in] cnt_type counting mode
  179. *
  180. * @retval WM_SUCCESS success
  181. * @retval WM_FAILED failed
  182. *
  183. * @note None
  184. */
  185. int tls_pwm_cnt_type_config(u8 channel, enum tls_pwm_cnt_type cnt_type)
  186. {
  187. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  188. return WM_FAILED;
  189. if (4 == channel)
  190. {
  191. if (WM_PWM_CNT_TYPE_EDGE_ALLGN_CAP == cnt_type)
  192. {
  193. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) & (~BIT(4)));
  194. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) & (~BIT(3)));
  195. }
  196. if (WM_PWM_CNT_TYPE_EDGE_ALIGN_OUT == cnt_type)
  197. {
  198. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) & (~BIT(4)));
  199. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) | BIT(3));
  200. }
  201. else if (WM_PWM_CNT_TYPE_CENTER_ALIGN == cnt_type)
  202. {
  203. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) | BIT(4));
  204. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) & (~BIT(3)));
  205. }
  206. }
  207. else
  208. {
  209. if (WM_PWM_CNT_TYPE_EDGE_ALLGN_CAP == cnt_type && channel == 0)
  210. {
  211. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(17)));
  212. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(16)));
  213. }
  214. if (WM_PWM_CNT_TYPE_EDGE_ALIGN_OUT == cnt_type)
  215. {
  216. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(17 + channel * 2)));
  217. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(16 + channel * 2));
  218. }
  219. else if (WM_PWM_CNT_TYPE_CENTER_ALIGN == cnt_type)
  220. {
  221. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(17 + channel * 2));
  222. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(16 + channel * 2)));
  223. }
  224. else
  225. return WM_FAILED;
  226. }
  227. return WM_SUCCESS;
  228. }
  229. /**
  230. * @brief This function is used to set whether to loop
  231. *
  232. * @param[in] channel pwm channel NO.,range form 0 to 4
  233. * @param[in] loop_mode whether to loop
  234. *
  235. * @retval WM_SUCCESS success
  236. * @retval WM_FAILED failed
  237. *
  238. * @note None
  239. */
  240. int tls_pwm_loop_mode_config(u8 channel, enum tls_pwm_loop_type loop_mode)
  241. {
  242. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  243. return WM_FAILED;
  244. if (4 == channel)
  245. {
  246. if (WM_PWM_LOOP_TYPE_LOOP == loop_mode)
  247. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) | BIT(1));
  248. else
  249. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) & (~BIT(1)));
  250. }
  251. else
  252. {
  253. if (WM_PWM_LOOP_TYPE_LOOP == loop_mode)
  254. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(8 + channel));
  255. else
  256. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(8 + channel)));
  257. }
  258. return WM_SUCCESS;
  259. }
  260. /**
  261. * @brief This function is used to set whether to inverse the output
  262. *
  263. * @param[in] channel pwm channel NO.,range form 0 to 4
  264. * @param[in] en ENABLE or DISABLE
  265. *
  266. * @retval WM_SUCCESS success
  267. * @retval WM_FAILED failed
  268. *
  269. * @note None
  270. */
  271. int tls_pwm_out_inverse_cmd(u8 channel, bool en)
  272. {
  273. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  274. return WM_FAILED;
  275. if (4 == channel)
  276. {
  277. if (ENABLE == en)
  278. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) | BIT(0));
  279. else
  280. tls_reg_write32(HR_PWM_CH4_REG2, tls_reg_read32(HR_PWM_CH4_REG2) & (~BIT(0)));
  281. }
  282. else
  283. {
  284. if (ENABLE == en)
  285. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(2 + channel));
  286. else
  287. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(2 + channel)));
  288. }
  289. return WM_SUCCESS;
  290. }
  291. /**
  292. * @brief This function is used to set the number of period to be generated
  293. *
  294. * @param[in] channel pwm channel NO.,range form 0 to 4
  295. * @param[in] pnum the number of period to be generated,range from 0 to 255
  296. *
  297. * @retval WM_SUCCESS success
  298. * @retval WM_FAILED failed
  299. *
  300. * @note None
  301. */
  302. int tls_pwm_stoptime_by_period_config(u8 channel, u8 pnum)
  303. {
  304. u32 temp = 0;
  305. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  306. return WM_FAILED;
  307. if (4 == channel)
  308. {
  309. temp = tls_reg_read32(HR_PWM_CH4_REG1) & ~0x000000FF;
  310. temp |= pnum;
  311. tls_reg_write32(HR_PWM_CH4_REG1, temp);
  312. }
  313. else
  314. {
  315. temp = tls_reg_read32(HR_PWM_PNUM) & ~(0xFF << channel * 8);
  316. temp |= (pnum << (channel * 8));
  317. tls_reg_write32(HR_PWM_PNUM, temp);
  318. }
  319. return WM_SUCCESS;
  320. }
  321. /**
  322. * @brief This function is used to set output enable
  323. *
  324. * @param[in] channel pwm channel NO.,channel 0 or channel 4
  325. * @param[in] en ENABLE or DISABLE
  326. *
  327. * @retval WM_SUCCESS success
  328. * @retval WM_FAILED failed
  329. *
  330. * @note None
  331. */
  332. int tls_pwm_output_en_cmd(u8 channel, bool en)
  333. {
  334. if(channel != 0 && channel != 4)
  335. return WM_FAILED;
  336. if (4 == channel)
  337. {
  338. if (ENABLE == en)
  339. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) & (~BIT(2)));
  340. else
  341. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) | BIT(2));
  342. }
  343. else
  344. {
  345. if (ENABLE == en)
  346. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(12)));
  347. else
  348. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(12));
  349. }
  350. return WM_SUCCESS;
  351. }
  352. /**
  353. * @brief This function is used to set the dead time
  354. *
  355. * @param[in] channel pwm channel NO.,channel 0 or channel 2
  356. * @param[in] dten whether enalbe the deat time, ENABLE or DISABLE
  357. * @param[in] dtclkdiv dead zone clock divider, range 0 to 3
  358. * @param[in] dtcnt the number of the counting clock cycle, range 0 to 255
  359. *
  360. * @retval WM_SUCCESS success
  361. * @retval WM_FAILED failed
  362. *
  363. * @note None
  364. */
  365. int tls_pwm_deadzone_config(u8 channel, bool dten, u8 dtclkdiv, u8 dtcnt)
  366. {
  367. u32 temp = 0;
  368. if ((channel !=0 && channel != 2) || dtclkdiv > 3)
  369. return WM_FAILED;
  370. if(ENABLE == dten)
  371. {
  372. temp = tls_reg_read32(HR_PWM_DTCTL) & ~0x00030000;
  373. temp |= (dtclkdiv<<16);
  374. tls_reg_write32(HR_PWM_DTCTL, temp);/* dead zone clock divider */
  375. if (channel == 0 || channel == 1)
  376. {
  377. temp = tls_reg_read32(HR_PWM_DTCTL) & ~0x000000FF;
  378. temp |= dtcnt;
  379. tls_reg_write32(HR_PWM_DTCTL, temp);/* the number of the counting clock cycle */
  380. tls_reg_write32(HR_PWM_DTCTL, tls_reg_read32(HR_PWM_CTL) | BIT(20)); /* whether enalbe the deat time */
  381. }
  382. else if (channel == 2 || channel == 3)
  383. {
  384. temp = tls_reg_read32(HR_PWM_DTCTL) & ~0x0000FF00;
  385. temp |= (dtcnt<<8);
  386. tls_reg_write32(HR_PWM_DTCTL, temp);/* the number of the counting clock cycle */
  387. tls_reg_write32(HR_PWM_DTCTL, tls_reg_read32(HR_PWM_CTL) | BIT(21)); /* whether enalbe the deat time */
  388. }
  389. }
  390. else
  391. {
  392. if (channel == 0 || channel == 1)
  393. {
  394. tls_reg_write32(HR_PWM_DTCTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(20))); /* whether enalbe the deat time */
  395. }
  396. else if (channel == 2 || channel == 3)
  397. {
  398. tls_reg_write32(HR_PWM_DTCTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(21))); /* whether enalbe the deat time */
  399. }
  400. }
  401. return WM_SUCCESS;
  402. }
  403. /**
  404. * @brief This function is used to set whether to inverse the capture input
  405. *
  406. * @param[in] channel pwm channel NO.,channel 0 or channel 4
  407. * @param[in] en ENABLE or DISABLE
  408. *
  409. * @retval WM_SUCCESS success
  410. * @retval WM_FAILED failed
  411. *
  412. * @note None
  413. */
  414. int tls_pwm_capture_inverse_cmd(u8 channel, bool en)
  415. {
  416. if (channel != 0 && channel != 4)
  417. return WM_FAILED;
  418. if (channel == 0)
  419. {
  420. if (ENABLE == en)
  421. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(25));
  422. else
  423. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(25)));
  424. }
  425. else
  426. {
  427. if (ENABLE == en)
  428. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) | BIT(0));
  429. else
  430. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) & (~BIT(0)));
  431. }
  432. return WM_SUCCESS;
  433. }
  434. /**
  435. * @brief This function is used to set break mode
  436. *
  437. * @param[in] channel pwm channel NO.,channel 0 or channel 4
  438. * @param[in] en whether enable the break mode,ENABLE or DISABLE
  439. * @param[in] brok when break
  440. *
  441. * @retval WM_SUCCESS success
  442. * @retval WM_FAILED failed
  443. *
  444. * @note None
  445. */
  446. int tls_pwm_brake_mode_config(u8 channel, bool en, enum tls_pwm_brake_out_level brok)
  447. {
  448. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  449. return WM_FAILED;
  450. if (ENABLE == en)
  451. {
  452. if (WM_PWM_BRAKE_OUT_HIGH == brok)
  453. tls_reg_write32(HR_PWM_BRKCTL, tls_reg_read32(HR_PWM_BRKCTL) | BIT(3+channel));
  454. else
  455. tls_reg_write32(HR_PWM_BRKCTL, tls_reg_read32(HR_PWM_BRKCTL) & (~BIT(3+channel)));
  456. tls_reg_write32(HR_PWM_BRKCTL, tls_reg_read32(HR_PWM_BRKCTL) | BIT(11+channel));
  457. }
  458. else
  459. {
  460. tls_reg_write32(HR_PWM_BRKCTL, tls_reg_read32(HR_PWM_BRKCTL) & (~BIT(11+channel)));
  461. }
  462. return WM_SUCCESS;
  463. }
  464. /**
  465. * @brief This function is used to enable the capture mode
  466. *
  467. * @param[in] channel pwm channel NO.,channel 0 or channel 4
  468. *
  469. * @retval WM_SUCCESS success
  470. * @retval WM_FAILED failed
  471. *
  472. * @note None
  473. */
  474. int tls_pwm_capture_mode_config(u8 channel)
  475. {
  476. if (channel != 0 && channel != 4)
  477. return WM_FAILED;
  478. if (channel == 0)
  479. {
  480. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(24));
  481. }
  482. else
  483. {
  484. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) | BIT(1));
  485. }
  486. return WM_SUCCESS;
  487. }
  488. /**
  489. * @brief This function is used to set the interrupt about the number of period
  490. *
  491. * @param[in] channel pwm channel,range from 0 to 4
  492. * @param[in] en enble or disable
  493. *
  494. * @retval WM_SUCCESS success
  495. * @retval WM_FAILED failed
  496. *
  497. * @note None
  498. */
  499. int tls_pwm_stoptime_irq_cmd(u8 channel, bool en)
  500. {
  501. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  502. return WM_FAILED;
  503. if (4 == channel)
  504. {
  505. if (en)
  506. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) | BIT(4));
  507. else
  508. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) & (~BIT(4)));
  509. }
  510. else
  511. {
  512. if (en)
  513. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) | BIT(channel));
  514. else
  515. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) & (~BIT(channel)));
  516. }
  517. return WM_SUCCESS;
  518. }
  519. /**
  520. * @brief This function is used to set the interrupt about the
  521. capture
  522. *
  523. * @param[in] channel pwm channel,channel 0 or channel 4
  524. * @param[in] int_type interrupt type
  525. *
  526. * @retval WM_SUCCESS success
  527. * @retval WM_FAILED failed
  528. *
  529. * @note None
  530. */
  531. int tls_pwm_capture_irq_type_config(u8 channel, enum tls_pwm_cap_int_type int_type)
  532. {
  533. if (channel != 0 && channel != 4)
  534. return WM_FAILED;
  535. if (0 == channel)
  536. {
  537. if (WM_PWM_CAP_RISING_FALLING_EDGE_INT == int_type)
  538. {
  539. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) | BIT(5));
  540. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) | BIT(6));
  541. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) & (~BIT(7)));
  542. }
  543. else if (WM_PWM_CAP_RISING_EDGE_INT == int_type)
  544. {
  545. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) | BIT(5));
  546. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) & (~BIT(6)));
  547. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) & (~BIT(7)));
  548. }
  549. else if (WM_PWM_CAP_FALLING_EDGE_INT == int_type)
  550. {
  551. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) | BIT(6));
  552. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) & (~BIT(5)));
  553. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) & (~BIT(7)));
  554. }
  555. else if(WM_PWM_CAP_DMA_INT == int_type)
  556. {
  557. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) | BIT(7));
  558. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) & (~BIT(5)));
  559. tls_reg_write32(HR_PWM_INTEN, tls_reg_read32(HR_PWM_INTEN) & (~BIT(6)));
  560. }
  561. }
  562. else if (4 == channel)
  563. {
  564. if (WM_PWM_CAP_RISING_FALLING_EDGE_INT == int_type)
  565. {
  566. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) | BIT(8));
  567. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) | BIT(9));
  568. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) & (~BIT(10)));
  569. }
  570. else if (WM_PWM_CAP_RISING_EDGE_INT == int_type)
  571. {
  572. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) | BIT(8));
  573. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) & (~BIT(9)));
  574. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) & (~BIT(10)));
  575. }
  576. else if (WM_PWM_CAP_FALLING_EDGE_INT == int_type)
  577. {
  578. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) | BIT(9));
  579. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) & (~BIT(8)));
  580. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) & (~BIT(10)));
  581. }
  582. else if(WM_PWM_CAP_DMA_INT == int_type)
  583. {
  584. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) | BIT(10));
  585. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) & (~BIT(8)));
  586. tls_reg_write32(HR_PWM_CAP2CTL, tls_reg_read32(HR_PWM_CAP2CTL) & (~BIT(9)));
  587. }
  588. }
  589. return WM_SUCCESS;
  590. }
  591. /**
  592. * @brief This function is used to initial pwm(out mode)
  593. *
  594. * @param[in] pwm_param structure containing the initialization parameters
  595. *
  596. * @retval WM_SUCCESS success
  597. * @retval WM_FAILED failed
  598. *
  599. * @note None
  600. */
  601. int tls_pwm_out_init(pwm_init_param * pwm_param)
  602. {
  603. int ret=0;
  604. if (pwm_param->channel > (PWM_CHANNEL_MAX_NUM - 1))
  605. return WM_FAILED;
  606. /* set output mode */
  607. ret = tls_pwm_out_mode_config(pwm_param->channel, pwm_param->mode);
  608. if (ret!=WM_SUCCESS)
  609. return WM_FAILED;
  610. if (WM_PWM_OUT_MODE_MC == pwm_param->mode)
  611. {
  612. /* set dead time */
  613. ret = tls_pwm_deadzone_config(pwm_param->channel, pwm_param->dten, pwm_param->dtclkdiv, pwm_param->dtcnt);
  614. if (ret!=WM_SUCCESS)
  615. return WM_FAILED;
  616. }
  617. /* set count type */
  618. tls_pwm_cnt_type_config(pwm_param->channel, pwm_param->cnt_type);
  619. /* set period value and duty radio */
  620. tls_pwm_freq_config(pwm_param->channel, pwm_param->clkdiv, pwm_param->period);
  621. tls_pwm_duty_config(pwm_param->channel, pwm_param->duty);
  622. /* set cycle type */
  623. tls_pwm_loop_mode_config(pwm_param->channel, pwm_param->loop_type);
  624. /* set output whether is inverse */
  625. tls_pwm_out_inverse_cmd(pwm_param->channel, pwm_param->inverse_en);
  626. /* set period number of generating */
  627. tls_pwm_stoptime_by_period_config(pwm_param->channel, pwm_param->pnum);
  628. /* set interrupt of period number whether is enable */
  629. tls_pwm_stoptime_irq_cmd(pwm_param->channel, pwm_param->pnum_int);
  630. /* set output status */
  631. if (pwm_param->channel == 0 || pwm_param->channel == 4)
  632. tls_pwm_output_en_cmd(pwm_param->channel, WM_PWM_OUT_EN_STATE_OUT);
  633. if (pwm_param->mode == WM_PWM_OUT_MODE_ALLSYC && pwm_param->channel == 0)
  634. tls_pwm_output_en_cmd(4, WM_PWM_OUT_EN_STATE_OUT);
  635. return WM_SUCCESS;
  636. }
  637. /**
  638. * @brief This function is used to initial pwm(capture mode)
  639. *
  640. * @param[in] channel pwm channel, channel 0 or channel 4
  641. * @param[in] clkdiv clock divider, range 0 to 65535
  642. * @param[in] inverse_en whether the input signal is reversed
  643. * @param[in] int_type interrupt type
  644. *
  645. * @retval WM_SUCCESS success
  646. * @retval WM_FAILED failed
  647. *
  648. * @note None
  649. */
  650. int tls_pwm_cap_init(u8 channel, u16 clkdiv, bool inverse_en, enum tls_pwm_cap_int_type int_type)
  651. {
  652. if (channel != 0 && channel != 4)
  653. return WM_FAILED;
  654. /* set clock divider and period value */
  655. tls_pwm_freq_config(channel, clkdiv, 0xFF);
  656. /* set input of capture mode whether is inverse */
  657. tls_pwm_capture_inverse_cmd(channel, inverse_en);
  658. /* set the capture mode */
  659. tls_pwm_capture_mode_config(channel);
  660. /* set count type (only edge alignment in the capture mode) */
  661. tls_pwm_cnt_type_config(channel, WM_PWM_CNT_TYPE_EDGE_ALLGN_CAP);
  662. /* set output status */
  663. if(channel == 0)
  664. tls_pwm_output_en_cmd(channel, WM_PWM_OUT_EN_STATE_TRI);
  665. /* set cycle mode (must be set int the capture mode) */
  666. tls_pwm_loop_mode_config(channel, WM_PWM_LOOP_TYPE_LOOP);
  667. /* set interrupt type */
  668. tls_pwm_capture_irq_type_config(channel, int_type);
  669. return WM_SUCCESS;
  670. }
  671. /**
  672. * @brief This function is used to start pwm
  673. *
  674. * @param[in] channel pwm channel, range from 0 to 4
  675. *
  676. * @retval WM_SUCCESS success
  677. * @retval WM_FAILED failed
  678. *
  679. * @note None
  680. */
  681. int tls_pwm_start(u8 channel)
  682. {
  683. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  684. return WM_FAILED;
  685. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) | BIT(27 + channel)); /* start counter */
  686. return WM_SUCCESS;
  687. }
  688. /**
  689. * @brief This function is used to stop pwm
  690. *
  691. * @param[in] channel pwm channel, range from 0 to 4
  692. *
  693. * @retval WM_SUCCESS success
  694. * @retval WM_FAILED failed
  695. *
  696. * @note None
  697. */
  698. int tls_pwm_stop(u8 channel)
  699. {
  700. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  701. return WM_FAILED;
  702. tls_reg_write32(HR_PWM_CTL, tls_reg_read32(HR_PWM_CTL) & (~BIT(27 + channel)));/* stop counter */
  703. return WM_SUCCESS;
  704. }
  705. /**
  706. * @brief This function is used to stop pwm
  707. *
  708. * @param[in] channel pwm channel no, range form 0 to 4
  709. * @param[in] freq frequency, range from 1 to 156250
  710. *
  711. * @return None
  712. *
  713. * @note None
  714. */
  715. void tls_pwm_freq_set(u8 channel, u32 freq)
  716. {
  717. u16 clkdiv=0;
  718. tls_sys_clk sysclk;
  719. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  720. return;
  721. tls_sys_clk_get(&sysclk);
  722. clkdiv = sysclk.apbclk*UNIT_MHZ/256/freq;
  723. tls_pwm_stop(channel);
  724. tls_pwm_freq_config(channel, clkdiv, 255);
  725. tls_pwm_start(channel);
  726. }
  727. /**
  728. * @brief This function is used to set duty radio
  729. *
  730. * @param[in] channel pwm channel NO., range form 0 to 4
  731. * @param[in] duty duty radio, range from 0 to 255
  732. *
  733. * @return None
  734. *
  735. * @note None
  736. */
  737. void tls_pwm_duty_set(u8 channel, u8 duty)
  738. {
  739. if(channel > (PWM_CHANNEL_MAX_NUM - 1))
  740. return;
  741. if (duty == 0)
  742. {
  743. tls_pwm_stop(channel);
  744. }
  745. else
  746. {
  747. tls_pwm_duty_config(channel, duty);
  748. tls_pwm_start(channel);
  749. }
  750. }
  751. /**
  752. * @brief This function is used to initial pwm
  753. *
  754. * @param[in] channel pwm channel, range from 0 to 4
  755. * @param[in] freq freq range from 1 to 156250
  756. * @param[in] duty duty range from 0 to 255
  757. * @param[in] pnum period num,range from 0 to 255
  758. *
  759. * @retval WM_SUCCESS success
  760. * @retval WM_FAILED failed
  761. *
  762. * @note None
  763. */
  764. int tls_pwm_init(u8 channel,u32 freq, u8 duty, u8 pnum)
  765. {
  766. pwm_init_param pwm_param;
  767. int ret=-1;
  768. tls_sys_clk sysclk;
  769. tls_sys_clk_get(&sysclk);
  770. memset(&pwm_param, 0, sizeof(pwm_init_param));
  771. pwm_param.period = 255;
  772. pwm_param.cnt_type = WM_PWM_CNT_TYPE_EDGE_ALIGN_OUT;
  773. pwm_param.loop_type = WM_PWM_LOOP_TYPE_LOOP;
  774. pwm_param.mode = WM_PWM_OUT_MODE_INDPT;
  775. pwm_param.inverse_en = DISABLE;
  776. pwm_param.pnum = pnum;
  777. pwm_param.pnum_int = DISABLE;
  778. pwm_param.duty = duty;
  779. pwm_param.channel = channel;
  780. pwm_param.clkdiv = sysclk.apbclk*UNIT_MHZ/256/freq;
  781. // printf("clkdiv:%d\n", pwm_param.clkdiv);
  782. ret = tls_pwm_out_init(&pwm_param);
  783. // tls_pwm_start(channel);
  784. return ret;
  785. }