pwm.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. /**************************************************************************************************
  2. Phyplus Microelectronics Limited confidential and proprietary.
  3. All rights reserved.
  4. IMPORTANT: All rights of this software belong to Phyplus Microelectronics
  5. Limited ("Phyplus"). Your use of this Software is limited to those
  6. specific rights granted under the terms of the business contract, the
  7. confidential agreement, the non-disclosure agreement and any other forms
  8. of agreements as a customer or a partner of Phyplus. You may not use this
  9. Software unless you agree to abide by the terms of these agreements.
  10. You acknowledge that the Software may not be modified, copied,
  11. distributed or disclosed unless embedded on a Phyplus Bluetooth Low Energy
  12. (BLE) integrated circuit, either as a product or is integrated into your
  13. products. Other than for the aforementioned purposes, you may not use,
  14. reproduce, copy, prepare derivative works of, modify, distribute, perform,
  15. display or sell this Software and/or its documentation for any purposes.
  16. YOU FURTHER ACKNOWLEDGE AND AGREE THAT THE SOFTWARE AND DOCUMENTATION ARE
  17. PROVIDED AS IS WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS OR IMPLIED,
  18. INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF MERCHANTABILITY, TITLE,
  19. NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL
  20. PHYPLUS OR ITS SUBSIDIARIES BE LIABLE OR OBLIGATED UNDER CONTRACT,
  21. NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR OTHER
  22. LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
  23. INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE
  24. OR CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT
  25. OF SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
  26. (INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
  27. **************************************************************************************************/
  28. /*******************************************************************************
  29. * @file pwm.c
  30. * @brief Contains all functions support for pwm driver
  31. * @version 0.0
  32. * @date 30. Oct. 2017
  33. * @author Ding
  34. *
  35. * Copyright(C) 2016, PhyPlus Semiconductor
  36. * All rights reserved.
  37. *
  38. *******************************************************************************/
  39. #include "ap_cp.h"
  40. #include "clock.h"
  41. #include "pwm.h"
  42. #include "gpio.h"
  43. #include "pwrmgr.h"
  44. #include "common.h"
  45. /**************************************************************************************
  46. * @fn hal_pwm_init
  47. *
  48. * @brief This function process for pwm initial
  49. *
  50. * input parameters
  51. *
  52. * @param PWMN_e pwmN : pwm channel
  53. * PWM_CLK_DIV_e pwmDiv : clock prescaler of PWM channel
  54. * PWM_CNT_MODE_e pwmMode : count mode of PWM channel
  55. * PWM_POLARITY_e pwmPolarity : output polarity setting of PWM channel
  56. * unsigned short cmpVal : the compare value of PWM channel
  57. * unsigned short cntTopVal : the counter top value of PWM channel
  58. *
  59. * output parameters
  60. *
  61. * @param None.
  62. *
  63. * @return None.
  64. **************************************************************************************/
  65. void hal_pwm_init(
  66. PWMN_e pwmN,
  67. PWM_CLK_DIV_e pwmDiv,
  68. PWM_CNT_MODE_e pwmMode,
  69. PWM_POLARITY_e pwmPolarity)
  70. {
  71. clk_gate_enable(MOD_PWM);
  72. PWM_DISABLE_CH(pwmN);
  73. PWM_SET_DIV(pwmN, pwmDiv);
  74. PWM_SET_MODE(pwmN, pwmMode);
  75. PWM_SET_POL(pwmN, pwmPolarity);
  76. //PWM_INSTANT_LOAD_CH(pwmN);
  77. hal_pwrmgr_register(MOD_PWM, NULL, NULL);
  78. }
  79. /**************************************************************************************
  80. * @fn hal_pwm_open_channel
  81. *
  82. * @brief This function process for pwm start working
  83. *
  84. * input parameters
  85. *
  86. * @param PWMN_e pwmN : pwm channel
  87. * GPIO_Pin_e pwmPin : pwm pin number
  88. *
  89. * output parameters
  90. *
  91. * @param None.
  92. *
  93. * @return None.
  94. **************************************************************************************/
  95. void hal_pwm_open_channel(PWMN_e pwmN,GPIO_Pin_e pwmPin){
  96. hal_gpio_fmux_set(pwmPin, (Fmux_Type_e)(PWM0 + pwmN));
  97. PWM_ENABLE_CH(pwmN);
  98. }
  99. /**************************************************************************************
  100. * @fn hal_pwm_close_channel
  101. *
  102. * @brief This function process for pwm stop working
  103. *
  104. * input parameters
  105. *
  106. * @param PWMN_e pwmN : pwm channel
  107. *
  108. * output parameters
  109. *
  110. * @param None.
  111. *
  112. * @return None.
  113. **************************************************************************************/
  114. void hal_pwm_close_channel(PWMN_e pwmN){
  115. PWM_DISABLE_CH(pwmN);
  116. //clk_gate_disable(MOD_PWM);
  117. }
  118. /**************************************************************************************
  119. * @fn hal_pwm_destroy
  120. *
  121. * @brief This function process for pwm clear and disable
  122. *
  123. * input parameters
  124. *
  125. * @param PWMN_e pwmN : pwm channel
  126. *
  127. * output parameters
  128. *
  129. * @param None.
  130. *
  131. * @return None.
  132. **************************************************************************************/
  133. void hal_pwm_destroy(PWMN_e pwmN){
  134. PWM_DISABLE_CH(pwmN);
  135. PWM_NO_LOAD_CH(pwmN);
  136. PWM_NO_INSTANT_LOAD_CH(pwmN);
  137. PWM_SET_DIV(pwmN, 0);
  138. PWM_SET_MODE(pwmN, 0);
  139. PWM_SET_POL(pwmN, 0);
  140. PWM_SET_TOP_VAL(pwmN, 0);
  141. PWM_SET_CMP_VAL(pwmN, 0);
  142. }
  143. /**************************************************************************************
  144. * @fn hal_pwm_set_count_val
  145. *
  146. * @brief This function process for change pwm count value
  147. *
  148. * input parameters
  149. *
  150. * @param PWMN_e pwmN : pwm channel
  151. * uint16_t cmpVal : the compare value of PWM channel
  152. * uint16_t cntTopVal : the counter top value of PWM channel
  153. *
  154. * output parameters
  155. *
  156. * @param None.
  157. *
  158. * @return None.
  159. **************************************************************************************/
  160. void hal_pwm_set_count_val(PWMN_e pwmN, uint16_t cmpVal, uint16_t cntTopVal){
  161. PWM_NO_LOAD_CH(pwmN);
  162. PWM_SET_CMP_VAL(pwmN, cmpVal);
  163. PWM_SET_TOP_VAL(pwmN, cntTopVal);
  164. PWM_LOAD_CH(pwmN);
  165. }
  166. static unsigned int pwm_en = 0;
  167. void hal_pwm_start(void)
  168. {
  169. if(pwm_en == 0)
  170. {
  171. hal_pwrmgr_lock(MOD_PWM);
  172. PWM_ENABLE_ALL;
  173. pwm_en = 1;
  174. }
  175. }
  176. void hal_pwm_stop(void)
  177. {
  178. if(pwm_en == 1)
  179. {
  180. hal_pwrmgr_unlock(MOD_PWM);
  181. PWM_DISABLE_ALL;
  182. pwm_en = 0;
  183. }
  184. }
  185. //------------------------------------------------------------
  186. //new api,make use easily
  187. typedef struct {
  188. bool enable;
  189. bool ch_en[6];
  190. pwm_ch_t ch[6];
  191. }pwm_Ctx_t;
  192. static pwm_Ctx_t pwmCtx =
  193. {
  194. .enable = FALSE,
  195. .ch_en = {FALSE,FALSE,FALSE,FALSE,FALSE,FALSE},
  196. };
  197. void hal_pwm_module_init(void)
  198. {
  199. int i = 0;
  200. if(pwmCtx.enable == TRUE)
  201. return;
  202. pwmCtx.enable = TRUE;
  203. for(i = 0;i < 6;i++)
  204. {
  205. pwmCtx.ch_en[i] = FALSE;
  206. pwmCtx.ch[i].pwmN = (PWMN_e)i;
  207. pwmCtx.ch[i].pwmPin = GPIO_DUMMY;
  208. pwmCtx.ch[i].pwmDiv = PWM_CLK_NO_DIV;
  209. pwmCtx.ch[i].pwmMode = PWM_CNT_UP;
  210. pwmCtx.ch[i].pwmPolarity = PWM_POLARITY_RISING;
  211. pwmCtx.ch[i].cmpVal = 0;
  212. pwmCtx.ch[i].cntTopVal = 0;
  213. }
  214. hal_pwm_stop();
  215. }
  216. void hal_pwm_module_deinit(void)
  217. {
  218. int i = 0;
  219. if(pwmCtx.enable == FALSE)
  220. return;
  221. pwmCtx.enable = FALSE;
  222. for(i = 0;i < 6;i++)
  223. {
  224. pwmCtx.ch_en[i] = FALSE;
  225. pwmCtx.ch[i].pwmN = (PWMN_e)i;
  226. pwmCtx.ch[i].pwmPin = GPIO_DUMMY;
  227. pwmCtx.ch[i].pwmDiv = PWM_CLK_NO_DIV;
  228. pwmCtx.ch[i].pwmMode = PWM_CNT_UP;
  229. pwmCtx.ch[i].pwmPolarity = PWM_POLARITY_RISING;
  230. pwmCtx.ch[i].cmpVal = 0;
  231. pwmCtx.ch[i].cntTopVal = 0;
  232. hal_pwm_close_channel((PWMN_e)i);
  233. hal_pwm_destroy((PWMN_e)i);
  234. }
  235. hal_pwm_stop();
  236. }
  237. void hal_pwm_ch_start(pwm_ch_t ch)
  238. {
  239. if(pwmCtx.enable == FALSE)
  240. return;
  241. if(pwmCtx.ch_en[ch.pwmN] == TRUE)
  242. {
  243. hal_pwm_set_count_val(ch.pwmN,ch.cmpVal,ch.cntTopVal);
  244. }
  245. else
  246. {
  247. hal_pwm_init(ch.pwmN,ch.pwmDiv,ch.pwmMode,ch.pwmPolarity);
  248. hal_pwm_set_count_val(ch.pwmN,ch.cmpVal,ch.cntTopVal);
  249. hal_pwm_open_channel(ch.pwmN,ch.pwmPin);
  250. pwmCtx.ch_en[ch.pwmN] = TRUE;
  251. hal_pwm_start();
  252. }
  253. }
  254. void hal_pwm_ch_stop(pwm_ch_t ch)
  255. {
  256. if(pwmCtx.ch_en[ch.pwmN] == FALSE)
  257. return;
  258. else
  259. {
  260. hal_pwm_stop();
  261. pwmCtx.ch_en[ch.pwmN] = FALSE;
  262. hal_pwm_destroy(ch.pwmN);
  263. hal_pwm_close_channel(ch.pwmN);
  264. }
  265. }
  266. bool hal_pwm_ch_enable(PWMN_e pwmN)
  267. {
  268. return pwmCtx.ch_en[pwmN];
  269. }
  270. pwm_ch_t hal_pwm_ch_reg(PWMN_e pwmN)
  271. {
  272. return pwmCtx.ch[pwmN];
  273. }