ciu32f003_std_lptim(1).h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /************************************************************************************************/
  2. /**
  3. * @file ciu32f003_std_lptim.h
  4. * @author MCU Ecosystem Development Team
  5. * @brief LPTIM STD库驱动头文件。
  6. * 提供LPTIM相关的STD库操作函数声明、数据类型以及常量的定义。
  7. *
  8. *
  9. **************************************************************************************************
  10. * @attention
  11. * Copyright (c) CEC Huada Electronic Design Co.,Ltd. All rights reserved.
  12. *
  13. **************************************************************************************************
  14. */
  15. /*避免头文件重复引用*/
  16. #ifndef CIU32F003_STD_LPTIM_H
  17. #define CIU32F003_STD_LPTIM_H
  18. /************************************************************************************************/
  19. /**
  20. * @addtogroup CIU32F003_STD_Driver
  21. * @{
  22. */
  23. /**
  24. * @defgroup LPTIM LPTIM
  25. * @brief 低功耗定时器的STD库驱动
  26. * @{
  27. */
  28. /************************************************************************************************/
  29. #ifdef __cplusplus
  30. extern "C" {
  31. #endif
  32. /*------------------------------------------includes--------------------------------------------*/
  33. #include "ciu32f003_std_common.h"
  34. /*--------------------------------------------define--------------------------------------------*/
  35. /************************************************************************************************/
  36. /**
  37. * @defgroup LPTIM_Constants LPTIM Constants
  38. * @brief LPTIM常量定义及宏定义
  39. * @{
  40. *
  41. */
  42. /************************************************************************************************/
  43. /* 计数器计数模式 */
  44. #define LPTIM_COUNT_CONTINUOUS LPTIM_CR_CNTSTRT /**< 连续计数模式 */
  45. #define LPTIM_COUNT_SINGLE LPTIM_CR_SNGSTRT /**< 单次计数模式 */
  46. /* LPTIM PSC预分频器分频系数选择 */
  47. #define LPTIM_PRESCALER_DIV1 LPTIM_CFG_PRESC_1 /**< PSC预分频器1分频 */
  48. #define LPTIM_PRESCALER_DIV2 LPTIM_CFG_PRESC_2 /**< PSC预分频器2分频 */
  49. #define LPTIM_PRESCALER_DIV4 LPTIM_CFG_PRESC_4 /**< PSC预分频器4分频 */
  50. #define LPTIM_PRESCALER_DIV8 LPTIM_CFG_PRESC_8 /**< PSC预分频器8分频 */
  51. #define LPTIM_PRESCALER_DIV16 LPTIM_CFG_PRESC_16 /**< PSC预分频器16分频 */
  52. #define LPTIM_PRESCALER_DIV32 LPTIM_CFG_PRESC_32 /**< PSC预分频器32分频 */
  53. #define LPTIM_PRESCALER_DIV64 LPTIM_CFG_PRESC_64 /**< PSC预分频器64分频 */
  54. #define LPTIM_PRESCALER_DIV128 LPTIM_CFG_PRESC_128 /**< PSC预分频器128分频 */
  55. /* LPTIM中断源 */
  56. #define LPTIM_INTERRUPT_ARRM LPTIM_IER_ARRM_IE /**< 自动重载匹配中断使能 */
  57. #define LPTIM_INTERRUPT_ITRF LPTIM_IER_ITRF_IE /**< 级联触发中断使能 */
  58. /* LPTIM中断状态标志 */
  59. #define LPTIM_FLAG_ARRM LPTIM_ISR_ARRM /**< 自动重载匹配标志 */
  60. #define LPTIM_FLAG_ITRF LPTIM_ISR_ITRF /**< 级联触发标志 */
  61. /* LPTIM中断清除标志 */
  62. #define LPTIM_CLEAR_ARRM LPTIM_ICR_ARRM_CF /**< 自动重载匹配标志清零 */
  63. #define LPTIM_CLEAR_ITRF LPTIM_ICR_ITRF_CF /**< 级联触发标志清零 */
  64. /**
  65. * @}
  66. */
  67. /*-------------------------------------------functions------------------------------------------*/
  68. /************************************************************************************************/
  69. /**
  70. * @defgroup LPTIM_External_Functions LPTIM External Functions
  71. * @brief LPTIM对外函数
  72. * @{
  73. *
  74. */
  75. /************************************************************************************************/
  76. /**
  77. * @brief 使能LPTIM
  78. * @note 在LPTIM使能位置位后,需要两个计数器周期后才能生效
  79. * @retval 无
  80. */
  81. __STATIC_INLINE void std_lptim_enable(void)
  82. {
  83. LPTIM1->CR |= LPTIM_CR_ENABLE;
  84. }
  85. /**
  86. * @brief 禁止LPTIM
  87. * @retval 无
  88. */
  89. __STATIC_INLINE void std_lptim_disable(void)
  90. {
  91. LPTIM1->CR &= (~LPTIM_CR_ENABLE);
  92. }
  93. /**
  94. * @brief 配置LPTIM计数器按照预期模式开始计数
  95. * @param operate_mode 计数模式选择
  96. * @arg LPTIM_COUNT_CONTINUOUS: 连续计数模式
  97. * @arg LPTIM_COUNT_SINGLE: 单次计数模式
  98. * @note 必须使能LPTIM后才能启动计数器计数
  99. * @retval 无
  100. */
  101. __STATIC_INLINE void std_lptim_start_counter(uint32_t operate_mode)
  102. {
  103. MODIFY_REG(LPTIM1->CR, (LPTIM_CR_CNTSTRT | LPTIM_CR_SNGSTRT), operate_mode);
  104. }
  105. /**
  106. * @brief 设置LPTIM自动重装载值
  107. * @param auto_reload 自动重装载值(该变量的范围为0x0~0xFFFF)
  108. * @retval 无
  109. */
  110. __STATIC_INLINE void std_lptim_set_auto_reload(uint32_t auto_reload)
  111. {
  112. LPTIM1->ARR = auto_reload;
  113. }
  114. /**
  115. * @brief 获取LPTIM自动重装载值
  116. * @retval uint32_t 自动重装载值(该变量的范围为0x0~0xFFFF)
  117. */
  118. __STATIC_INLINE uint32_t std_lptim_get_auto_reload(void)
  119. {
  120. return (LPTIM1->ARR);
  121. }
  122. /**
  123. * @brief 获取LPTIM计数值
  124. * @note 当LPTIM为异步时钟计数时,为确保获取正确的计数值,需要确保两次读取的计数值一致。
  125. * @retval uint32_t LPTIM计数值(该变量的范围为0x0~0xFFFF)
  126. */
  127. __STATIC_INLINE uint32_t std_lptim_get_count(void)
  128. {
  129. return (LPTIM1->CNT);
  130. }
  131. /**
  132. * @brief 设置LPTIM预分频器分频系数
  133. * @param prescaler 预分频系数选择
  134. * @arg LPTIM_PRESCALER_DIV1
  135. * @arg LPTIM_PRESCALER_DIV2
  136. * @arg LPTIM_PRESCALER_DIV4
  137. * @arg ...
  138. * @arg LPTIM_PRESCALER_DIV128
  139. * @retval 无
  140. */
  141. __STATIC_INLINE void std_lptim_set_prescaler(uint32_t prescaler)
  142. {
  143. MODIFY_REG(LPTIM1->CFG, LPTIM_CFG_PRESC, prescaler);
  144. }
  145. /**
  146. * @brief 获取LPTIM预分频器分频系数
  147. * @retval uint32_t 预分频系数选择
  148. * @arg LPTIM_PRESCALER_DIV1
  149. * @arg LPTIM_PRESCALER_DIV2
  150. * @arg LPTIM_PRESCALER_DIV4
  151. * @arg ...
  152. * @arg LPTIM_PRESCALER_DIV128
  153. */
  154. __STATIC_INLINE uint32_t std_lptim_get_prescaler(void)
  155. {
  156. return (LPTIM1->CFG & LPTIM_CFG_PRESC);
  157. }
  158. /**
  159. * @brief 使能LPTIM级联触发
  160. * @retval 无
  161. */
  162. __STATIC_INLINE void std_lptim_internal_trigger_enable(void)
  163. {
  164. LPTIM1->CFG |= LPTIM_CFG_ITREN;
  165. }
  166. /**
  167. * @brief 禁止LPTIM级联触发
  168. * @retval 无
  169. */
  170. __STATIC_INLINE void std_lptim_internal_trigger_disable(void)
  171. {
  172. LPTIM1->CFG &= ~LPTIM_CFG_ITREN;
  173. }
  174. /**
  175. * @brief 使能LPTIM中断
  176. * @param interrupt LPTIM中断源
  177. * @arg LPTIM_INTERRUPT_ARRM: 自动重载匹配中断
  178. * @arg LPTIM_INTERRUPT_ITRF: 级联触发中断使能
  179. * @retval 无
  180. */
  181. __STATIC_INLINE void std_lptim_interrupt_enable(uint32_t interrupt)
  182. {
  183. LPTIM1->IER |= interrupt;
  184. }
  185. /**
  186. * @brief 禁止LPTIM中断
  187. * @param interrupt LPTIM中断源
  188. * @arg LPTIM_INTERRUPT_ARRM: 自动重载匹配中断
  189. * @arg LPTIM_INTERRUPT_ITRF: 级联触发中断使能
  190. * @retval 无
  191. */
  192. __STATIC_INLINE void std_lptim_interrupt_disable(uint32_t interrupt)
  193. {
  194. LPTIM1->IER &= (~interrupt);
  195. }
  196. /**
  197. * @brief 获取LPTIM中断状态
  198. * @param interrupt LPTIM中断源
  199. * @arg LPTIM_INTERRUPT_ARRM: 自动重载匹配中断
  200. * @arg LPTIM_INTERRUPT_ITRF: 级联触发中断使能
  201. * @retval uint32_t LPTIM中断源使能状态
  202. * @arg 非0: 使能
  203. * @arg 0: 禁止
  204. */
  205. __STATIC_INLINE uint32_t std_lptim_get_interrupt_status(uint32_t interrupt)
  206. {
  207. return (LPTIM1->IER & interrupt);
  208. }
  209. /**
  210. * @brief 获取LPTIM状态标志位
  211. * @param flag LPTIM状态标志位
  212. * @arg LPTIM_FLAG_ARRM: 自动重载匹配标志
  213. * @arg LPTIM_FLAG_ITRF: 级联触发标志
  214. * @retval uint32_t LPTIM标志位的状态
  215. * @arg 非0: 标志位置位
  216. * @arg 0: 标志位清除
  217. */
  218. __STATIC_INLINE uint32_t std_lptim_get_flag(uint32_t flag)
  219. {
  220. return (LPTIM1->ISR & flag);
  221. }
  222. /**
  223. * @brief 清除LPTIM状态标志位
  224. * @param flag LPTIM状态标志位
  225. * @arg LPTIM_CLEAR_ARRM: 自动重载匹配标志
  226. * @arg LPTIM_CLEAR_ITRF: 级联触发标志清零
  227. * @retval 无
  228. */
  229. __STATIC_INLINE void std_lptim_clear_flag(uint32_t flag)
  230. {
  231. LPTIM1->ICR = flag;
  232. }
  233. /* LPTIM去初始化函数 */
  234. void std_lptim_deinit(void);
  235. /**
  236. * @}
  237. */
  238. #ifdef __cplusplus
  239. }
  240. #endif
  241. /**
  242. * @}
  243. */
  244. /**
  245. * @}
  246. */
  247. #endif /* CIU32F003_STD_LPTIM_H */