pcnt.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /******************************************************************************
  2. * Copyright (C) 2017, Huada Semiconductor Co.,Ltd All rights reserved.
  3. *
  4. * This software is owned and published by:
  5. * Huada Semiconductor Co.,Ltd ("HDSC").
  6. *
  7. * BY DOWNLOADING, INSTALLING OR USING THIS SOFTWARE, YOU AGREE TO BE BOUND
  8. * BY ALL THE TERMS AND CONDITIONS OF THIS AGREEMENT.
  9. *
  10. * This software contains source code for use with HDSC
  11. * components. This software is licensed by HDSC to be adapted only
  12. * for use in systems utilizing HDSC components. HDSC shall not be
  13. * responsible for misuse or illegal use of this software for devices not
  14. * supported herein. HDSC is providing this software "AS IS" and will
  15. * not be responsible for issues arising from incorrect user implementation
  16. * of the software.
  17. *
  18. * Disclaimer:
  19. * HDSC MAKES NO WARRANTY, EXPRESS OR IMPLIED, ARISING BY LAW OR OTHERWISE,
  20. * REGARDING THE SOFTWARE (INCLUDING ANY ACOOMPANYING WRITTEN MATERIALS),
  21. * ITS PERFORMANCE OR SUITABILITY FOR YOUR INTENDED USE, INCLUDING,
  22. * WITHOUT LIMITATION, THE IMPLIED WARRANTY OF MERCHANTABILITY, THE IMPLIED
  23. * WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR USE, AND THE IMPLIED
  24. * WARRANTY OF NONINFRINGEMENT.
  25. * HDSC SHALL HAVE NO LIABILITY (WHETHER IN CONTRACT, WARRANTY, TORT,
  26. * NEGLIGENCE OR OTHERWISE) FOR ANY DAMAGES WHATSOEVER (INCLUDING, WITHOUT
  27. * LIMITATION, DAMAGES FOR LOSS OF BUSINESS PROFITS, BUSINESS INTERRUPTION,
  28. * LOSS OF BUSINESS INFORMATION, OR OTHER PECUNIARY LOSS) ARISING FROM USE OR
  29. * INABILITY TO USE THE SOFTWARE, INCLUDING, WITHOUT LIMITATION, ANY DIRECT,
  30. * INDIRECT, INCIDENTAL, SPECIAL OR CONSEQUENTIAL DAMAGES OR LOSS OF DATA,
  31. * SAVINGS OR PROFITS,
  32. * EVEN IF Disclaimer HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  33. * YOU ASSUME ALL RESPONSIBILITIES FOR SELECTION OF THE SOFTWARE TO ACHIEVE YOUR
  34. * INTENDED RESULTS, AND FOR THE INSTALLATION OF, USE OF, AND RESULTS OBTAINED
  35. * FROM, THE SOFTWARE.
  36. *
  37. * This software may be replicated in part or whole for the licensed use,
  38. * with the restriction that this Disclaimer and Copyright notice must be
  39. * included with each copy of this software, whether used in part or whole,
  40. * at all times.
  41. */
  42. /******************************************************************************/
  43. /** \file pcnt.c
  44. **
  45. ** pcnt driver API.
  46. ** @link pcnt Group Some description @endlink
  47. **
  48. ** - 2018-04-15 Devi First Version
  49. **
  50. ******************************************************************************/
  51. /******************************************************************************
  52. * Include files
  53. ******************************************************************************/
  54. #include "pcnt.h"
  55. /**
  56. ******************************************************************************
  57. ** \addtogroup PCNTGroup
  58. ******************************************************************************/
  59. //@{
  60. /******************************************************************************
  61. * Local pre-processor symbols/macros ('#define')
  62. ******************************************************************************/
  63. #define IS_VALID_pagagain(x) ( (x) <= 7 )
  64. #define IS_VALID_channel(x) ( (OPA0 == (x)) ||\
  65. (OPA1 == (x)) ||\
  66. (OPA2 == (x)) )
  67. #define IS_VALID_STAT(x) ( (PCNT_S1E == (x)) ||\
  68. (PCNT_S0E == (x)) ||\
  69. (PCNT_BB == (x)) ||\
  70. (PCNT_FE == (x)) ||\
  71. (PCNT_DIR == (x)) ||\
  72. (PCNT_TO == (x)) ||\
  73. (PCNT_OV == (x)) ||\
  74. (PCNT_UF == (x)) )
  75. /******************************************************************************
  76. * Global variable definitions (declared in header file with 'extern')
  77. ******************************************************************************/
  78. /******************************************************************************
  79. * Local type definitions ('typedef')
  80. ******************************************************************************/
  81. /******************************************************************************
  82. * Local function prototypes ('static')
  83. ******************************************************************************/
  84. static func_ptr_t pfnPcntCallback = NULL; ///< callback function pointer for PCNT Irq
  85. /******************************************************************************
  86. * Local variable definitions ('static')
  87. ******************************************************************************/
  88. /*****************************************************************************
  89. * Function implementation - global ('extern') and local ('static')
  90. *****************************************************************************/
  91. void Pcnt_IRQHandler(void)
  92. {
  93. if(NULL != pfnPcntCallback)
  94. {
  95. pfnPcntCallback();
  96. }
  97. }
  98. /**
  99. * \brief
  100. * PCNT 初始化
  101. *
  102. * \param 无
  103. * \param 无
  104. *
  105. * \retval 无
  106. * \retval 无
  107. */
  108. en_result_t PCNT_Init(stc_pcnt_config_t* pstcPcntConfig)
  109. {
  110. M0P_SYSCTRL->PERI_CLKEN_f.PCNT = 1;
  111. M0P_PCNT->CR_f.S1P = pstcPcntConfig->bS1Sel;
  112. M0P_PCNT->CR_f.S0P = pstcPcntConfig->bS0Sel;
  113. M0P_PCNT->CR_f.DIR = pstcPcntConfig->u8Direc; //计数方式
  114. M0P_PCNT->CR_f.CLKSEL = pstcPcntConfig->u8Clk;
  115. M0P_PCNT->CR_f.MODE = pstcPcntConfig->u8Mode;
  116. M0P_PCNT->FLT_f.CLKDIV = pstcPcntConfig->u8FLTClk;
  117. if(pstcPcntConfig->bFLTEn)
  118. {
  119. if(pstcPcntConfig->u8FLTDep == 0)
  120. {
  121. M0P_PCNT->FLT_f.DEBTOP = 2;
  122. }
  123. else
  124. {
  125. M0P_PCNT->FLT_f.DEBTOP = pstcPcntConfig->u8FLTDep;
  126. }
  127. }
  128. M0P_PCNT->FLT_f.EN = pstcPcntConfig->bFLTEn;
  129. M0P_PCNT->TOCR_f.TH = pstcPcntConfig->u16TODep;
  130. M0P_PCNT->TOCR_f.EN = pstcPcntConfig->bTOEn;
  131. if (TRUE == pstcPcntConfig->bIrqEn)
  132. {
  133. M0P_PCNT->IEN = pstcPcntConfig->u8IrqStatus;
  134. EnableNvic(PCNT_IRQn,IrqLevel3,TRUE);
  135. }
  136. else
  137. {
  138. M0P_PCNT->IEN = 0x00;
  139. EnableNvic(PCNT_IRQn,IrqLevel3,FALSE);
  140. }
  141. if(NULL != pstcPcntConfig->pfnIrqCb)
  142. {
  143. pfnPcntCallback = pstcPcntConfig->pfnIrqCb;
  144. }
  145. return Ok;
  146. }
  147. /**
  148. * \brief
  149. * PCNT 去初始化
  150. *
  151. * \param 无
  152. * \param 无
  153. *
  154. * \retval 无
  155. * \retval 无
  156. */
  157. void PCNT_DeInit(void)
  158. {
  159. M0P_PCNT->CR = 0;
  160. M0P_PCNT->RUN = 0;
  161. M0P_SYSCTRL->PERI_CLKEN_f.PCNT = 0;
  162. }
  163. /**
  164. * \brief
  165. * PCNT 脉冲计数设置
  166. *
  167. * \param [in] start 开始计数设置
  168. * \param [in] end 结束计数设置
  169. *
  170. * \retval 无
  171. */
  172. en_result_t PCNT_Parameter(uint8_t start,uint8_t end)
  173. {
  174. uint32_t u32TimeOut;
  175. u32TimeOut = 1000;
  176. M0P_PCNT->BUF = end; //加载结束溢出值
  177. M0P_PCNT->CMD_f.B2T = 1;
  178. while(u32TimeOut--)
  179. {
  180. if(FALSE == M0P_PCNT->SR2_f.B2T)
  181. {
  182. break;
  183. }
  184. }
  185. if(u32TimeOut == 0)
  186. {
  187. return ErrorTimeout;
  188. }
  189. u32TimeOut = 1000;
  190. M0P_PCNT->BUF = start; //加载初始值
  191. M0P_PCNT->CMD_f.B2C = 1;
  192. while(u32TimeOut--)
  193. {
  194. if(FALSE == M0P_PCNT->SR2_f.B2C)
  195. {
  196. break;
  197. }
  198. }
  199. if(u32TimeOut == 0)
  200. {
  201. return ErrorTimeout;
  202. }
  203. return Ok;
  204. }
  205. /**
  206. * \brief
  207. * 获取PCNT计数方向
  208. * \param [in]
  209. *
  210. * \retval 无
  211. */
  212. en_pcnt_direcsel_t PCNT_Direction(void)
  213. {
  214. return (en_pcnt_direcsel_t)M0P_PCNT->SR1_f.DIR;
  215. }
  216. /**
  217. * \brief
  218. * 获取PCNT计数值
  219. * \param [in]
  220. *
  221. * \retval 无
  222. */
  223. uint16_t PCNT_Count(void)
  224. {
  225. return M0P_PCNT->CNT;
  226. }
  227. /**
  228. * \brief
  229. * 获取PCNT溢出值
  230. * \param [in]
  231. *
  232. * \retval 无
  233. */
  234. uint16_t PCNT_TopCount(void)
  235. {
  236. return M0P_PCNT->TOP;
  237. }
  238. /**
  239. * \brief
  240. * PCNT使能
  241. * \param [in]
  242. *
  243. * \retval 无
  244. */
  245. void PCNT_Run(boolean_t work)
  246. {
  247. M0P_PCNT->RUN_f.RUN = work;
  248. }
  249. /**
  250. * \brief
  251. * PCNT 读取状态
  252. * \param [in] en_pcnt_status_t PCNT状态
  253. *
  254. * \retval 无
  255. */
  256. boolean_t PCNT_GetStatus(en_pcnt_status_t enStatus)
  257. {
  258. boolean_t bFlag = FALSE;
  259. ASSERT(IS_VALID_STAT(enStatus));
  260. switch (enStatus)
  261. {
  262. case PCNT_S1E:
  263. bFlag = M0P_PCNT->IFR_f.S1E;
  264. break;
  265. case PCNT_S0E:
  266. bFlag = M0P_PCNT->IFR_f.S0E;
  267. break;
  268. case PCNT_BB:
  269. bFlag = M0P_PCNT->IFR_f.BB;
  270. break;
  271. case PCNT_FE:
  272. bFlag = M0P_PCNT->IFR_f.FE;
  273. break;
  274. case PCNT_DIR:
  275. bFlag = M0P_PCNT->IFR_f.DIR;
  276. break;
  277. case PCNT_TO:
  278. bFlag = M0P_PCNT->IFR_f.TO;
  279. break;
  280. case PCNT_OV:
  281. bFlag = M0P_PCNT->IFR_f.OV;
  282. break;
  283. case PCNT_UF:
  284. bFlag = M0P_PCNT->IFR_f.UF;
  285. break;
  286. default:
  287. break;
  288. }
  289. return bFlag;
  290. }
  291. /**
  292. * \brief
  293. * PCNT 清除状态
  294. * \param [in] en_pcnt_status_t PCNT状态
  295. *
  296. * \retval 无
  297. */
  298. void PCNT_ClrStatus(en_pcnt_status_t enStatus)
  299. {
  300. ASSERT(IS_VALID_STAT(enStatus));
  301. switch (enStatus)
  302. {
  303. case PCNT_S1E:
  304. M0P_PCNT->ICR_f.S1E = 0;
  305. break;
  306. case PCNT_S0E:
  307. M0P_PCNT->ICR_f.S0E = 0;
  308. break;
  309. case PCNT_BB:
  310. M0P_PCNT->ICR_f.BB = 0;
  311. break;
  312. case PCNT_FE:
  313. M0P_PCNT->ICR_f.FE = 0;
  314. break;
  315. case PCNT_DIR:
  316. M0P_PCNT->ICR_f.DIR = 0;
  317. break;
  318. case PCNT_TO:
  319. M0P_PCNT->ICR_f.TO = 0;
  320. break;
  321. case PCNT_OV:
  322. M0P_PCNT->ICR_f.OV = 0;
  323. break;
  324. case PCNT_UF:
  325. M0P_PCNT->ICR_f.UF = 0;
  326. break;
  327. default:
  328. break;
  329. }
  330. }
  331. /**
  332. * \brief
  333. * PCNT 中断设置
  334. * \param [in] en_pcnt_status_t PCNT状态
  335. *
  336. * \retval 无
  337. */
  338. void PCNT_SetIrqStatus(en_pcnt_status_t enStatus)
  339. {
  340. ASSERT(IS_VALID_STAT(enStatus));
  341. switch (enStatus)
  342. {
  343. case PCNT_S1E:
  344. M0P_PCNT->IEN_f.S1E = 1;
  345. break;
  346. case PCNT_S0E:
  347. M0P_PCNT->IEN_f.S0E = 1;
  348. break;
  349. case PCNT_BB:
  350. M0P_PCNT->IEN_f.BB = 1;
  351. break;
  352. case PCNT_FE:
  353. M0P_PCNT->IEN_f.FE = 1;
  354. break;
  355. case PCNT_DIR:
  356. M0P_PCNT->IEN_f.DIR = 1;
  357. break;
  358. case PCNT_TO:
  359. M0P_PCNT->IEN_f.TO = 1;
  360. break;
  361. case PCNT_OV:
  362. M0P_PCNT->IEN_f.OV = 1;
  363. break;
  364. case PCNT_UF:
  365. M0P_PCNT->IEN_f.UF = 1;
  366. break;
  367. default:
  368. break;
  369. }
  370. }
  371. //@} // OPAGroup
  372. /******************************************************************************
  373. * EOF (not truncated)
  374. ******************************************************************************/