pwm.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /**
  2. * @file pwm.h
  3. * @author chipsea
  4. * @brief
  5. * @version 0.1
  6. * @date 2020-11-30
  7. * @copyright Copyright (c) 2020, CHIPSEA Co., Ltd.
  8. * @note
  9. */
  10. /*******************************************************************************
  11. * @file pwm.h
  12. * @brief Contains all functions support for pwm driver
  13. * @version 0.0
  14. * @date 30. Oct. 2017
  15. * @author Ding
  16. *
  17. *
  18. *******************************************************************************/
  19. #ifndef __PWM__H__
  20. #define __PWM__H__
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. #include "types.h"
  25. #include "gpio.h"
  26. #define PWM_ENABLE_ALL do{\
  27. AP_PWM->pwmen |= BIT(0);\
  28. AP_PWM->pwmen |= BIT(4);\
  29. }while(0)
  30. #define PWM_DISABLE_ALL do{\
  31. AP_PWM->pwmen &= ~BIT(0);\
  32. AP_PWM->pwmen &= ~BIT(4);\
  33. }while(0)
  34. #define PWM_ENABLE_CH_012 do{\
  35. AP_PWM->pwmen |= BIT(8);\
  36. AP_PWM->pwmen |= BIT(9);\
  37. }while(0)
  38. #define PWM_DISABLE_CH_012 do{\
  39. AP_PWM->pwmen &= ~BIT(8);\
  40. AP_PWM->pwmen &= ~BIT(9);\
  41. }while(0)
  42. #define PWM_ENABLE_CH_345 do{\
  43. AP_PWM->pwmen |= BIT(10);\
  44. AP_PWM->pwmen |= BIT(11);\
  45. }while(0)
  46. #define PWM_DISABLE_CH_345 do{\
  47. AP_PWM->pwmen &= ~BIT(10);\
  48. AP_PWM->pwmen &= ~BIT(11);\
  49. }while(0)
  50. #define PWM_ENABLE_CH_01 do{\
  51. AP_PWM->pwmen |= BIT(12);\
  52. AP_PWM->pwmen |= BIT(13);\
  53. }while(0)
  54. #define PWM_DISABLE_CH_01 do{\
  55. AP_PWM->pwmen &= ~BIT(12);\
  56. AP_PWM->pwmen &= ~BIT(13);\
  57. }while(0)
  58. #define PWM_ENABLE_CH_23 do{\
  59. AP_PWM->pwmen |= BIT(14);\
  60. AP_PWM->pwmen |= BIT(15);\
  61. }while(0)
  62. #define PWM_DISABLE_CH_23 do{\
  63. AP_PWM->pwmen &= ~BIT(14);\
  64. AP_PWM->pwmen &= ~BIT(15);\
  65. }while(0)
  66. #define PWM_ENABLE_CH_45 do{\
  67. AP_PWM->pwmen |= BIT(16);\
  68. AP_PWM->pwmen |= BIT(17);\
  69. }while(0)
  70. #define PWM_DISABLE_CH_45 do{\
  71. AP_PWM->pwmen &= ~BIT(16);\
  72. AP_PWM->pwmen &= ~BIT(17);\
  73. }while(0)
  74. #define PWM_INSTANT_LOAD_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),31,31,1)
  75. #define PWM_NO_INSTANT_LOAD_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),31,31,0)
  76. #define PWM_LOAD_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),16,16,1)
  77. #define PWM_NO_LOAD_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),16,16,0)
  78. #define PWM_SET_DIV(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),14,12,v)
  79. #define PWM_SET_MODE(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),8,8,v)
  80. #define PWM_SET_POL(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),4,4,v)
  81. #define PWM_ENABLE_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),0,0,1)
  82. #define PWM_DISABLE_CH(n) subWriteReg(&(AP_PWM_CTRL(n)->ctrl0),0,0,0)
  83. #define PWM_SET_CMP_VAL(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl1),31,16,v)
  84. #define PWM_SET_TOP_VAL(n,v) subWriteReg(&(AP_PWM_CTRL(n)->ctrl1),15,0,v)
  85. #define PWM_GET_CMP_VAL(n) ((AP_PWM_CTRL(n)->ctrl1 & 0xFFFF0000) >> 8)
  86. #define PWM_GET_TOP_VAL(n) AP_PWM_CTRL(n)->ctrl1 & 0x0000FFFF
  87. /*************************************************************
  88. * @brief enum variable, the number of PWM channels supported
  89. *
  90. */
  91. typedef enum{
  92. PWM_CH0 = 0,
  93. PWM_CH1 = 1,
  94. PWM_CH2 = 2,
  95. PWM_CH3 = 3,
  96. PWM_CH4 = 4,
  97. PWM_CH5 = 5
  98. }PwmCh_t;
  99. /*************************************************************
  100. * @brief enum variable used for PWM clock prescaler
  101. *
  102. */
  103. typedef enum{
  104. PWM_CLK_NO_DIV = 0,
  105. PWM_CLK_DIV_2 = 1,
  106. PWM_CLK_DIV_4 = 2,
  107. PWM_CLK_DIV_8 = 3,
  108. PWM_CLK_DIV_16 = 4,
  109. PWM_CLK_DIV_32 = 5,
  110. PWM_CLK_DIV_64 = 6,
  111. PWM_CLK_DIV_128 = 7
  112. }PwmClkDiv_t;
  113. /*************************************************************
  114. * @brief enum variable used for PWM work mode setting
  115. *
  116. */
  117. typedef enum{
  118. PWM_CNT_UP = 0, //!<!< pwm mode up
  119. PWM_CNT_UP_AND_DOWN = 1 //!< pwm mode up and down
  120. }PwmMode_t;
  121. /*************************************************************
  122. * @brief enum variable used for PWM output polarity setting
  123. *
  124. */
  125. typedef enum{
  126. PWM_POLARITY_RISING = 0, //!< pwm polarity rising
  127. PWM_POLARITY_FALLING = 1 //!< pwm polarity falling
  128. }PwmPolarity_t;
  129. /**************************************************************************************
  130. * @fn HalPwmInit
  131. *
  132. * @brief This function process for pwm initial
  133. *
  134. * input parameters
  135. *
  136. * @param PwmCh_t pwmN : pwm channel
  137. * PwmClkDiv_t pwmDiv : clock prescaler of PWM channel
  138. * PwmMode_t pwmMode : count mode of PWM channel
  139. * PwmPolarity_t pwmPolarity : output polarity setting of PWM channel
  140. * unsigned short cmpVal : the compare value of PWM channel
  141. * unsigned short cntTopVal : the counter top value of PWM channel
  142. *
  143. * output parameters
  144. *
  145. * @param None.
  146. *
  147. * @return None.
  148. **************************************************************************************/
  149. void HalPwmInit(PwmCh_t pwmN, PwmClkDiv_t pwmDiv,
  150. PwmMode_t pwmMode, PwmPolarity_t pwmPolarity);
  151. /**************************************************************************************
  152. * @fn HalPwmOpenChannel
  153. *
  154. * @brief This function process for pwm start working
  155. *
  156. * input parameters
  157. *
  158. * @param PwmCh_t pwmN : pwm channel
  159. * GpioPin_t pwmPin : pwm pin number
  160. *
  161. * output parameters
  162. *
  163. * @param None.
  164. *
  165. * @return None.
  166. **************************************************************************************/
  167. void HalPwmOpenChannel(PwmCh_t pwmN,GpioPin_t pwmPin);
  168. /**************************************************************************************
  169. * @fn HalPwmCloseChannel
  170. *
  171. * @brief This function process for pwm stop working
  172. *
  173. * input parameters
  174. *
  175. * @param PwmCh_t pwmN : pwm channel
  176. *
  177. * output parameters
  178. *
  179. * @param None.
  180. *
  181. * @return None.
  182. **************************************************************************************/
  183. void HalPwmCloseChannel(PwmCh_t pwmN);
  184. /**************************************************************************************
  185. * @fn HalPwmDestroy
  186. *
  187. * @brief This function process for pwm clear and disable
  188. *
  189. * input parameters
  190. *
  191. * @param PwmCh_t pwmN : pwm channel
  192. *
  193. * output parameters
  194. *
  195. * @param None.
  196. *
  197. * @return None.
  198. **************************************************************************************/
  199. void HalPwmDestroy(PwmCh_t pwmN);
  200. /**************************************************************************************
  201. * @fn HalPwmSetCountVal
  202. *
  203. * @brief This function process for change pwm count value
  204. *
  205. * input parameters
  206. *
  207. * @param PwmCh_t pwmN : pwm channel
  208. * uint16_t cmpVal : the compare value of PWM channel
  209. * uint16_t cntTopVal : the counter top value of PWM channel
  210. *
  211. * output parameters
  212. *
  213. * @param None.
  214. *
  215. * @return None.
  216. **************************************************************************************/
  217. void HalPwmSetCountVal(PwmCh_t pwmN, uint16_t cmpVal, uint16_t cntTopVal);
  218. /**************************************************************************************
  219. * @fn HalPwmStart
  220. *
  221. * @brief pwm start
  222. *
  223. * input parameters
  224. *
  225. * @param None.
  226. *
  227. * output parameters
  228. *
  229. * @param None.
  230. *
  231. * @return None.
  232. **************************************************************************************/
  233. void HalPwmStart(void);
  234. /**************************************************************************************
  235. * @fn HalPwmStop
  236. *
  237. * @brief pwm stop
  238. *
  239. * input parameters
  240. *
  241. * @param None.
  242. *
  243. * output parameters
  244. *
  245. * @param None.
  246. *
  247. * @return None.
  248. **************************************************************************************/
  249. void HalPwmStop(void);
  250. //new api,make use easily
  251. typedef struct
  252. {
  253. PwmCh_t pwmN;
  254. GpioPin_t pwmPin;
  255. PwmClkDiv_t pwmDiv;
  256. PwmMode_t pwmMode;
  257. PwmPolarity_t pwmPolarity;
  258. uint16_t cmpVal;
  259. uint16_t cntTopVal;
  260. } PwmCfg_t;
  261. /**************************************************************************************
  262. * @fn HalPwmModuleInit
  263. *
  264. * @brief init pwm global variables
  265. *
  266. * input parameters
  267. *
  268. * @param None.
  269. *
  270. * output parameters
  271. *
  272. * @param None.
  273. *
  274. * @return None.
  275. **************************************************************************************/
  276. void HalPwmModuleInit(void);
  277. /**************************************************************************************
  278. * @fn HalPwmModuleDeinit
  279. *
  280. * @brief deinit pwm global variables
  281. *
  282. * input parameters
  283. *
  284. * @param None.
  285. *
  286. * output parameters
  287. *
  288. * @param None.
  289. *
  290. * @return None.
  291. **************************************************************************************/
  292. void HalPwmModuleDeinit(void);
  293. /**************************************************************************************
  294. * @fn HalPwmChannelStart
  295. *
  296. * @brief config and make a pwm start to work
  297. *
  298. * input parameters
  299. *
  300. * @param PwmCfg_t ch: pwm channel
  301. *
  302. * output parameters
  303. *
  304. * @param None.
  305. *
  306. * @return None.
  307. **************************************************************************************/
  308. void HalPwmChannelStart(PwmCfg_t ch);
  309. /**************************************************************************************
  310. * @fn HalPwmChannelStop
  311. *
  312. * @brief make a pwm stop form working
  313. *
  314. * input parameters
  315. *
  316. * @param PwmCfg_t ch: pwm channel
  317. *
  318. * output parameters
  319. *
  320. * @param None.
  321. *
  322. * @return None.
  323. **************************************************************************************/
  324. void HalPwmChannelStop(PwmCh_t pwmN);
  325. #ifdef __cplusplus
  326. }
  327. #endif
  328. #endif