pwm.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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.h
  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. #ifndef __PWM__H__
  40. #define __PWM__H__
  41. #include "types.h"
  42. #include "gpio.h"
  43. #include "common.h"
  44. #define PWM_CH_BASE 0x4000E004UL
  45. #define PWM_CTL0_ADDR(n) (PWM_CH_BASE + n*12)
  46. #define PWM_CTL1_ADDR(n) (PWM_CH_BASE + 4 + n*12)
  47. #define PWM_ENABLE_ALL do{\
  48. *(volatile unsigned int *) 0x4000e000 |= BIT(0);\
  49. *(volatile unsigned int *) 0x4000e000 |= BIT(4);\
  50. }while(0)
  51. #define PWM_DISABLE_ALL do{\
  52. *(volatile unsigned int *) 0x4000e000 &= ~BIT(0);\
  53. *(volatile unsigned int *) 0x4000e000 &= ~BIT(4);\
  54. }while(0)
  55. #define PWM_ENABLE_CH_012 do{\
  56. *(volatile unsigned int *) 0x4000e000 |= BIT(8);\
  57. *(volatile unsigned int *) 0x4000e000 |= BIT(9);\
  58. }while(0)
  59. #define PWM_DISABLE_CH_012 do{\
  60. *(volatile unsigned int *) 0x4000e000 &= ~BIT(8);\
  61. *(volatile unsigned int *) 0x4000e000 &= ~BIT(9);\
  62. }while(0)
  63. #define PWM_ENABLE_CH_345 do{\
  64. *(volatile unsigned int *) 0x4000e000 |= BIT(10);\
  65. *(volatile unsigned int *) 0x4000e000 |= BIT(11);\
  66. }while(0)
  67. #define PWM_DISABLE_CH_345 do{\
  68. *(volatile unsigned int *) 0x4000e000 &= ~BIT(10);\
  69. *(volatile unsigned int *) 0x4000e000 &= ~BIT(11);\
  70. }while(0)
  71. #define PWM_ENABLE_CH_01 do{\
  72. *(volatile unsigned int *) 0x4000e000 |= BIT(12);\
  73. *(volatile unsigned int *) 0x4000e000 |= BIT(13);\
  74. }while(0)
  75. #define PWM_DISABLE_CH_01 do{\
  76. *(volatile unsigned int *) 0x4000e000 &= ~BIT(12);\
  77. *(volatile unsigned int *) 0x4000e000 &= ~BIT(13);\
  78. }while(0)
  79. #define PWM_ENABLE_CH_23 do{\
  80. *(volatile unsigned int *) 0x4000e000 |= BIT(14);\
  81. *(volatile unsigned int *) 0x4000e000 |= BIT(15);\
  82. }while(0)
  83. #define PWM_DISABLE_CH_23 do{\
  84. *(volatile unsigned int *) 0x4000e000 &= ~BIT(14);\
  85. *(volatile unsigned int *) 0x4000e000 &= ~BIT(15);\
  86. }while(0)
  87. #define PWM_ENABLE_CH_45 do{\
  88. *(volatile unsigned int *) 0x4000e000 |= BIT(16);\
  89. *(volatile unsigned int *) 0x4000e000 |= BIT(17);\
  90. }while(0)
  91. #define PWM_DISABLE_CH_45 do{\
  92. *(volatile unsigned int *) 0x4000e000 &= ~BIT(16);\
  93. *(volatile unsigned int *) 0x4000e000 &= ~BIT(17);\
  94. }while(0)
  95. #define PWM_INSTANT_LOAD_CH(n) subWriteReg(PWM_CTL0_ADDR(n),31,31,1)
  96. #define PWM_NO_INSTANT_LOAD_CH(n) subWriteReg(PWM_CTL0_ADDR(n),31,31,0)
  97. #define PWM_LOAD_CH(n) subWriteReg(PWM_CTL0_ADDR(n),16,16,1)
  98. #define PWM_NO_LOAD_CH(n) subWriteReg(PWM_CTL0_ADDR(n),16,16,0)
  99. #define PWM_SET_DIV(n,v) subWriteReg(PWM_CTL0_ADDR(n),14,12,v)
  100. #define PWM_SET_MODE(n,v) subWriteReg(PWM_CTL0_ADDR(n),8,8,v)
  101. #define PWM_SET_POL(n,v) subWriteReg(PWM_CTL0_ADDR(n),4,4,v)
  102. #define PWM_ENABLE_CH(n) subWriteReg(PWM_CTL0_ADDR(n),0,0,1)
  103. #define PWM_DISABLE_CH(n) subWriteReg(PWM_CTL0_ADDR(n),0,0,0)
  104. #define PWM_SET_CMP_VAL(n,v) subWriteReg(PWM_CTL1_ADDR(n),31,16,v)
  105. #define PWM_SET_TOP_VAL(n,v) subWriteReg(PWM_CTL1_ADDR(n),15,0,v)
  106. #define PWM_GET_CMP_VAL(n) ((read_reg(PWM_CTL1_ADDR(n)) & 0xFFFF0000) >> 8)
  107. #define PWM_GET_TOP_VAL(n) read_reg(PWM_CTL1_ADDR(n)) & 0x0000FFFF
  108. /*************************************************************
  109. * @brief enum variable, the number of PWM channels supported
  110. *
  111. */
  112. typedef enum{
  113. PWM_CH0 = 0,
  114. PWM_CH1 = 1,
  115. PWM_CH2 = 2,
  116. PWM_CH3 = 3,
  117. PWM_CH4 = 4,
  118. PWM_CH5 = 5
  119. }PWMN_e;
  120. /*************************************************************
  121. * @brief enum variable used for PWM clock prescaler
  122. *
  123. */
  124. typedef enum{
  125. PWM_CLK_NO_DIV = 0,
  126. PWM_CLK_DIV_2 = 1, // 80k hz
  127. PWM_CLK_DIV_4 = 2, // 40k hz
  128. PWM_CLK_DIV_8 = 3, // 20k hz
  129. PWM_CLK_DIV_16 = 4, // 10k hz
  130. PWM_CLK_DIV_32 = 5, // 5k hz
  131. PWM_CLK_DIV_64 = 6, // 2.5k hz
  132. PWM_CLK_DIV_128 = 7 // 1.25k hz
  133. }PWM_CLK_DIV_e;
  134. /*************************************************************
  135. * @brief enum variable used for PWM work mode setting
  136. *
  137. */
  138. typedef enum{
  139. PWM_CNT_UP = 0,
  140. PWM_CNT_UP_AND_DOWN = 1
  141. }PWM_CNT_MODE_e;
  142. /*************************************************************
  143. * @brief enum variable used for PWM output polarity setting
  144. *
  145. */
  146. typedef enum{
  147. PWM_POLARITY_RISING = 0,
  148. PWM_POLARITY_FALLING = 1
  149. }PWM_POLARITY_e;
  150. /**************************************************************************************
  151. * @fn hal_pwm_init
  152. *
  153. * @brief This function process for pwm initial
  154. *
  155. * input parameters
  156. *
  157. * @param PWMN_e pwmN : pwm channel
  158. * PWM_CLK_DIV_e pwmDiv : clock prescaler of PWM channel
  159. * PWM_CNT_MODE_e pwmMode : count mode of PWM channel
  160. * PWM_POLARITY_e pwmPolarity : output polarity setting of PWM channel
  161. * unsigned short cmpVal : the compare value of PWM channel
  162. * unsigned short cntTopVal : the counter top value of PWM channel
  163. *
  164. * output parameters
  165. *
  166. * @param None.
  167. *
  168. * @return None.
  169. **************************************************************************************/
  170. void hal_pwm_init(PWMN_e pwmN, PWM_CLK_DIV_e pwmDiv,
  171. PWM_CNT_MODE_e pwmMode, PWM_POLARITY_e pwmPolarity);
  172. /**************************************************************************************
  173. * @fn hal_pwm_open_channel
  174. *
  175. * @brief This function process for pwm start working
  176. *
  177. * input parameters
  178. *
  179. * @param PWMN_e pwmN : pwm channel
  180. * GPIO_Pin_e pwmPin : pwm pin number
  181. *
  182. * output parameters
  183. *
  184. * @param None.
  185. *
  186. * @return None.
  187. **************************************************************************************/
  188. void hal_pwm_open_channel(PWMN_e pwmN,GPIO_Pin_e pwmPin);
  189. /**************************************************************************************
  190. * @fn hal_pwm_close_channel
  191. *
  192. * @brief This function process for pwm stop working
  193. *
  194. * input parameters
  195. *
  196. * @param PWMN_e pwmN : pwm channel
  197. *
  198. * output parameters
  199. *
  200. * @param None.
  201. *
  202. * @return None.
  203. **************************************************************************************/
  204. void hal_pwm_close_channel(PWMN_e pwmN);
  205. /**************************************************************************************
  206. * @fn hal_pwm_destroy
  207. *
  208. * @brief This function process for pwm clear and disable
  209. *
  210. * input parameters
  211. *
  212. * @param PWMN_e pwmN : pwm channel
  213. *
  214. * output parameters
  215. *
  216. * @param None.
  217. *
  218. * @return None.
  219. **************************************************************************************/
  220. void hal_pwm_destroy(PWMN_e pwmN);
  221. /**************************************************************************************
  222. * @fn hal_pwm_set_count_val
  223. *
  224. * @brief This function process for change pwm count value
  225. *
  226. * input parameters
  227. *
  228. * @param PWMN_e pwmN : pwm channel
  229. * uint16_t cmpVal : the compare value of PWM channel
  230. * uint16_t cntTopVal : the counter top value of PWM channel
  231. *
  232. * output parameters
  233. *
  234. * @param None.
  235. *
  236. * @return None.
  237. **************************************************************************************/
  238. void hal_pwm_set_count_val(PWMN_e pwmN, uint16_t cmpVal, uint16_t cntTopVal);
  239. void hal_pwm_start(void);
  240. void hal_pwm_stop(void);
  241. //new api,make use easily
  242. typedef struct
  243. {
  244. PWMN_e pwmN;
  245. GPIO_Pin_e pwmPin;
  246. PWM_CLK_DIV_e pwmDiv;
  247. PWM_CNT_MODE_e pwmMode;
  248. PWM_POLARITY_e pwmPolarity;
  249. uint16_t cmpVal;
  250. uint16_t cntTopVal;
  251. } pwm_ch_t;
  252. /**************************************************************************************
  253. * @fn hal_pwm_module_init
  254. *
  255. * @brief init pwm global variables
  256. *
  257. * input parameters
  258. *
  259. * @param None.
  260. *
  261. * output parameters
  262. *
  263. * @param None.
  264. *
  265. * @return None.
  266. **************************************************************************************/
  267. void hal_pwm_module_init(void);
  268. /**************************************************************************************
  269. * @fn hal_pwm_module_deinit
  270. *
  271. * @brief deinit pwm global variables
  272. *
  273. * input parameters
  274. *
  275. * @param None.
  276. *
  277. * output parameters
  278. *
  279. * @param None.
  280. *
  281. * @return None.
  282. **************************************************************************************/
  283. void hal_pwm_module_deinit(void);
  284. /**************************************************************************************
  285. * @fn hal_spi_pin_init
  286. *
  287. * @brief config and make a pwm start to work
  288. *
  289. * input parameters
  290. *
  291. * @param pwm_ch_t ch: pwm channel
  292. *
  293. * output parameters
  294. *
  295. * @param None.
  296. *
  297. * @return None.
  298. **************************************************************************************/
  299. void hal_pwm_ch_start(pwm_ch_t ch);
  300. /**************************************************************************************
  301. * @fn hal_pwm_ch_stop
  302. *
  303. * @brief make a pwm stop form working
  304. *
  305. * input parameters
  306. *
  307. * @param pwm_ch_t ch: pwm channel
  308. *
  309. * output parameters
  310. *
  311. * @param None.
  312. *
  313. * @return None.
  314. **************************************************************************************/
  315. void hal_pwm_ch_stop(pwm_ch_t ch);
  316. #endif