lpt.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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. /** \file lpt.c
  43. **
  44. ** Common API of Low Power timer.
  45. ** @link lptGroup Some description @endlink
  46. **
  47. ** - 2018-04-16 Husj First version
  48. **
  49. ******************************************************************************/
  50. /*******************************************************************************
  51. * Include files
  52. ******************************************************************************/
  53. #include "lpt.h"
  54. /**
  55. *******************************************************************************
  56. ** \addtogroup LptGroup
  57. ******************************************************************************/
  58. //@{
  59. /*******************************************************************************
  60. * Local pre-processor symbols/macros ('#define')
  61. ******************************************************************************/
  62. /*******************************************************************************
  63. * Global variable definitions (declared in header file with 'extern')
  64. ******************************************************************************/
  65. /*******************************************************************************
  66. * Local type definitions ('typedef')
  67. ******************************************************************************/
  68. /*******************************************************************************
  69. * Local variable definitions ('static')
  70. ******************************************************************************/
  71. /*******************************************************************************
  72. * Local function prototypes ('static')
  73. ******************************************************************************/
  74. static func_ptr_t pfnLpTimCallback = NULL;
  75. /*******************************************************************************
  76. * Function implementation - global ('extern') and local ('static')
  77. ******************************************************************************/
  78. /**
  79. *****************************************************************************
  80. ** \brief Low Power Timer 中断标志获取
  81. **
  82. **
  83. **
  84. **
  85. ** \retval TRUE or FALSE
  86. *****************************************************************************/
  87. boolean_t Lpt_GetIntFlag(void)
  88. {
  89. boolean_t bRetVal = FALSE;
  90. bRetVal = M0P_LPTIMER->IFR_f.TF ? TRUE : FALSE;
  91. return bRetVal;
  92. }
  93. /**
  94. *****************************************************************************
  95. ** \brief Low Power Timer 中断标志清除
  96. **
  97. **
  98. **
  99. **
  100. ** \retval Ok or Error
  101. *****************************************************************************/
  102. en_result_t Lpt_ClearIntFlag(void)
  103. {
  104. en_result_t enResult = Error;
  105. M0P_LPTIMER->ICLR_f.TFC = FALSE;
  106. enResult = Ok;
  107. return enResult;
  108. }
  109. /**
  110. *****************************************************************************
  111. ** \brief Low Power Timer 中断服务函数
  112. **
  113. **
  114. ** \param [in] u8Param == 0
  115. **
  116. *****************************************************************************/
  117. void LpTim_IRQHandler(uint8_t u8Param)
  118. {
  119. if(NULL != pfnLpTimCallback)
  120. {
  121. pfnLpTimCallback();
  122. }
  123. }
  124. /**
  125. *****************************************************************************
  126. ** \brief Low Power Timer 中断使能
  127. **
  128. **
  129. **
  130. **
  131. ** \retval Ok or Error
  132. *****************************************************************************/
  133. en_result_t Lpt_EnableIrq (void)
  134. {
  135. en_result_t enResult = Error;
  136. M0P_LPTIMER->CR_f.IE = TRUE;
  137. enResult = Ok;
  138. return enResult;
  139. }
  140. /**
  141. *****************************************************************************
  142. ** \brief Low Power Timer 中断禁止
  143. **
  144. **
  145. **
  146. **
  147. ** \retval Ok or Error
  148. *****************************************************************************/
  149. en_result_t Lpt_DisableIrq(void)
  150. {
  151. en_result_t enResult = Error;
  152. M0P_LPTIMER->CR_f.IE = FALSE;
  153. enResult = Ok;
  154. return enResult;
  155. }
  156. /**
  157. *****************************************************************************
  158. ** \brief Low Power Timer 初始化配置
  159. **
  160. **
  161. ** \param [in] pstcConfig 初始化配置结构体指针
  162. **
  163. ** \retval Ok or Error
  164. *****************************************************************************/
  165. en_result_t Lpt_Init(stc_lpt_config_t* pstcConfig)
  166. {
  167. en_result_t enResult = Error;
  168. M0P_LPTIMER->CR_f.GATE_P = pstcConfig->enGateP;
  169. M0P_LPTIMER->CR_f.GATE = pstcConfig->enGate;
  170. M0P_LPTIMER->CR_f.TCK_SEL = pstcConfig->enTckSel;
  171. M0P_LPTIMER->CR_f.TOG_EN = pstcConfig->enTog;
  172. M0P_LPTIMER->CR_f.CT = pstcConfig->enCT;
  173. M0P_LPTIMER->CR_f.MD = pstcConfig->enMD;
  174. pfnLpTimCallback = pstcConfig->pfnLpTimCb;
  175. enResult = Ok;
  176. return enResult;
  177. }
  178. /**
  179. *****************************************************************************
  180. ** \brief Low Power Timer 启动运行
  181. **
  182. **
  183. **
  184. ** \retval Ok or Error
  185. *****************************************************************************/
  186. en_result_t Lpt_Run(void)
  187. {
  188. en_result_t enResult = Error;
  189. M0P_LPTIMER->CR_f.TR = TRUE;
  190. enResult = Ok;
  191. return enResult;
  192. }
  193. /**
  194. *****************************************************************************
  195. ** \brief Low Power Timer 停止运行
  196. **
  197. **
  198. **
  199. ** \retval Ok or Error
  200. *****************************************************************************/
  201. en_result_t Lpt_Stop(void)
  202. {
  203. en_result_t enResult = Error;
  204. M0P_LPTIMER->CR_f.TR = FALSE;
  205. enResult = Ok;
  206. return enResult;
  207. }
  208. /**
  209. *****************************************************************************
  210. ** \brief Low Power Timer 重载值设置
  211. **
  212. **
  213. ** \param [in] u16Data 16bits重载值
  214. **
  215. ** \retval Ok or Error
  216. *****************************************************************************/
  217. en_result_t Lpt_ARRSet(uint16_t u16Data)
  218. {
  219. en_result_t enResult = Error;
  220. boolean_t bRetVal = FALSE;
  221. bRetVal = M0P_LPTIMER->CR_f.WT_FLAG ? TRUE : FALSE;
  222. if(TRUE == bRetVal)
  223. {
  224. M0P_LPTIMER->ARR_f.ARR = u16Data;
  225. enResult = Ok;
  226. }
  227. else
  228. {
  229. enResult = Error;
  230. }
  231. return enResult;
  232. }
  233. /**
  234. *****************************************************************************
  235. ** \brief Low Power Timer 16位计数值获取
  236. **
  237. **
  238. **
  239. ** \retval 16bits计数值
  240. *****************************************************************************/
  241. uint16_t Lpt_Cnt16Get(void)
  242. {
  243. uint16_t u16CntData = 0;
  244. u16CntData = M0P_LPTIMER->CNT_f.CNT;
  245. return u16CntData;
  246. }
  247. //@} // LptGroup
  248. /*******************************************************************************
  249. * EOF (not truncated)
  250. ******************************************************************************/