ald_lptim.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /**
  2. *********************************************************************************
  3. *
  4. * @file ald_lptim.c
  5. * @brief LPTIM module driver.
  6. * This is the common part of the LPTIM initialization
  7. *
  8. * @version V1.0
  9. * @date 09 Nov 2017
  10. * @author AE Team
  11. * @note
  12. *
  13. * Copyright (C) Shanghai Eastsoft Microelectronics Co. Ltd. All rights reserved.
  14. *
  15. *********************************************************************************
  16. */
  17. #ifndef __ALD_LPTIM_H__
  18. #define __ALD_LPTIM_H__
  19. #ifdef __cplusplus
  20. extern "C" {
  21. #endif
  22. #include "utils.h"
  23. #include "ald_cmu.h"
  24. /** @addtogroup ES32FXXX_ALD
  25. * @{
  26. */
  27. /** @addtogroup LPTIM
  28. * @{
  29. */
  30. /** @defgroup LPTIM_Public_Types LPTIM Public Types
  31. * @{
  32. */
  33. /**
  34. * @brief LPTIM clock select
  35. */
  36. typedef enum {
  37. LPTIM_CKSEL_INTERNAL = 0, /**< Select internal clock */
  38. LPTIM_CKSEL_EXTERNAL = 1, /**< Select external clock */
  39. } lptim_cksel_t;
  40. /**
  41. * @brief LPTIM clock pol
  42. */
  43. typedef enum {
  44. LPTIM_CKPOL_RISING = 0, /**< using rising edge */
  45. LPTIM_CKPOL_FALLING = 1, /**< using falling edge */
  46. } lptim_ckpol_t;
  47. /**
  48. * @brief LPTIM clock fliter
  49. */
  50. typedef enum {
  51. LPTIM_CKFLT_0 = 0, /**< not clock filter */
  52. LPTIM_CKFLT_2 = 1, /**< 2 cycle filter */
  53. LPTIM_CKFLT_4 = 2, /**< 4 cycle filter */
  54. LPTIM_CKFLT_8 = 3, /**< 8 cycle filter */
  55. } lptim_ckflt_t;
  56. /**
  57. * @brief LPTIM trigger fliter
  58. */
  59. typedef enum {
  60. LPTIM_TRGFLT_0 = 0, /**< not clock filter */
  61. LPTIM_TRGFLT_2 = 1, /**< 2 cycle filter */
  62. LPTIM_TRGFLT_4 = 2, /**< 4 cycle filter */
  63. LPTIM_TRGFLT_8 = 3, /**< 8 cycle filter */
  64. } lptim_trgflt_t;
  65. /**
  66. * @brief LPTIM prescaler
  67. */
  68. typedef enum {
  69. LPTIM_PRESC_1 = 0, /**< No prescaler is used */
  70. LPTIM_PRESC_2 = 1, /**< Clock is divided by 2 */
  71. LPTIM_PRESC_4 = 2, /**< Clock is divided by 4 */
  72. LPTIM_PRESC_8 = 3, /**< Clock is divided by 8 */
  73. LPTIM_PRESC_16 = 4, /**< Clock is divided by 16 */
  74. LPTIM_PRESC_32 = 5, /**< Clock is divided by 32 */
  75. LPTIM_PRESC_64 = 6, /**< Clock is divided by 64 */
  76. LPTIM_PRESC_128 = 7, /**< Clock is divided by 128 */
  77. } lptim_presc_t;
  78. /**
  79. * @brief LPTIM trig select
  80. */
  81. typedef enum {
  82. LPTIM_TRIGSEL_EXT0 = 0, /**< Trigger select external channel 0 */
  83. LPTIM_TRIGSEL_EXT1 = 1, /**< Trigger select external channel 1 */
  84. LPTIM_TRIGSEL_EXT2 = 2, /**< Trigger select external channel 2 */
  85. LPTIM_TRIGSEL_EXT3 = 3, /**< Trigger select external channel 3 */
  86. LPTIM_TRIGSEL_EXT4 = 4, /**< Trigger select external channel 4 */
  87. LPTIM_TRIGSEL_EXT5 = 5, /**< Trigger select external channel 5 */
  88. LPTIM_TRIGSEL_EXT6 = 6, /**< Trigger select external channel 6 */
  89. LPTIM_TRIGSEL_EXT7 = 7, /**< Trigger select external channel 7 */
  90. } lptim_trigsel_t;
  91. /**
  92. * @brief LPTIM start mode select
  93. */
  94. typedef enum {
  95. LPTIM_MODE_SINGLE = 0, /**< Start single mode */
  96. LPTIM_MODE_CONTINUOUS = 1, /**< Start continuous mode */
  97. } lptim_mode_t;
  98. /**
  99. * @brief LPTIM trig en
  100. */
  101. typedef enum {
  102. LPTIM_TRIGEN_SW = 0, /**< software trigger */
  103. LPTIM_TRIGEN_RISING = 1, /**< rising edge trigger */
  104. LPTIM_TRIGEN_FALLING = 2, /**< falling edge trigger */
  105. LPTIM_TRIGEN_BOTH = 3, /**< rising and falling edge trigger */
  106. } lptim_trigen_t;
  107. /**
  108. * @brief LPTIM wave
  109. */
  110. typedef enum {
  111. LPTIM_WAVE_NONE = 0, /**< Output close */
  112. LPTIM_WAVE_TOGGLE = 1, /**< Output toggle */
  113. LPTIM_WAVE_PULSE = 2, /**< Output pulse */
  114. LPTIM_WAVE_PWM = 3, /**< Output PWM */
  115. } lptim_wave_t;
  116. /**
  117. * @brief LPTIM interrupt
  118. */
  119. typedef enum {
  120. LPTIM_IT_CMPMAT = 1, /**< Compare interrupt bit */
  121. LPTIM_IT_ARRMAT = 2, /**< Update interrupt bit */
  122. LPTIM_IT_EXTTRIG = 4, /**< external trigger interrupt bit */
  123. } lptim_it_t;
  124. /**
  125. * @brief LPTIM Interrupt flag
  126. */
  127. typedef enum {
  128. LPTIM_FLAG_CMPMAT = 1, /**< Compare interrupt flag */
  129. LPTIM_FLAG_ARRMAT = 2, /**< Update interrupt flag */
  130. LPTIM_FLAG_EXTTRIG = 4, /**< Update interrupt flag */
  131. } lptim_flag_t;
  132. /**
  133. * @brief LPTIM state structures definition
  134. */
  135. typedef enum {
  136. LPTIM_STATE_RESET = 0x00, /**< Peripheral not yet initialized or disabled */
  137. LPTIM_STATE_READY = 0x01, /**< Peripheral Initialized and ready for use */
  138. LPTIM_STATE_BUSY = 0x02, /**< An internal process is ongoing */
  139. LPTIM_STATE_TIMEOUT = 0x03, /**< Timeout state */
  140. LPTIM_STATE_ERROR = 0x04, /**< Reception process is ongoing */
  141. } lptim_state_t;
  142. /**
  143. * @brief LPTIM Init Structure definition
  144. */
  145. typedef struct {
  146. lptim_presc_t psc; /**< Specifies the prescaler value */
  147. uint16_t arr; /**< Specifies the update value */
  148. uint16_t cmp; /**< Specifies the compare value */
  149. cmu_lp_perh_clock_sel_t clock; /**< Specifies the clock choose */
  150. lptim_mode_t mode; /**< Specifies the start mode */
  151. } lptim_init_t;
  152. /**
  153. * @brief LPTIM trigger Structure definition
  154. */
  155. typedef struct {
  156. lptim_trigen_t mode; /**< Specifies the trigger mode */
  157. lptim_trigsel_t sel; /**< Specifies the trigger source select */
  158. } lptim_trigger_init_t;
  159. /**
  160. * @brief LPTIM trigger Structure definition
  161. */
  162. typedef struct {
  163. lptim_cksel_t sel; /**< Specifies the clock select */
  164. lptim_ckpol_t polarity; /**< Specifies the clock polarity */
  165. } lptim_clock_source_init_t;
  166. /**
  167. * @brief LPTIM Handle Structure definition
  168. */
  169. typedef struct lptim_handle_s {
  170. LPTIM_TypeDef *perh; /**< Register base address */
  171. lptim_init_t init; /**< LPTIM Time required parameters */
  172. lock_state_t lock; /**< Locking object */
  173. lptim_state_t state; /**< LPTIM operation state */
  174. void (*trig_cbk)(struct lptim_handle_s *arg); /**< Trigger callback */
  175. void (*update_cbk)(struct lptim_handle_s *arg); /**< Update callback */
  176. void (*cmp_cbk)(struct lptim_handle_s *arg); /**< Compare callback */
  177. } lptim_handle_t;
  178. /**
  179. * @}
  180. */
  181. /** @defgroup LPTIM_Public_Macros LPTIM Public Macros
  182. * @{
  183. */
  184. #define LPTIM_ENABLE(x) (SET_BIT((x)->perh->CON1, LP16T_CON1_ENABLE_MSK))
  185. #define LPTIM_DISABLE(x) (CLEAR_BIT((x)->perh->CON1, LP16T_CON1_ENABLE_MSK))
  186. #define LPTIM_CNTSTART(x) (SET_BIT((x)->perh->CON1, LP16T_CON1_CNTSTRT_MSK))
  187. #define LPTIM_SNGSTART(x) (SET_BIT((x)->perh->CON1, LP16T_CON1_SNGSTRT_MSK))
  188. #define LPTIM_UPDATE_ENABLE(x) (SET_BIT((x)->perh->UPDATE, LP16T_UPDATE_UDIS_MSK))
  189. #define LPTIM_UPDATE_DISABLE(x) (CLEAR_BIT((x)->perh->UPDATE, LP16T_UPDATE_UDIS_MSK))
  190. #define LPTIM_PRELOAD_IMM(x) (SET_BIT((x)->perh->CR0, LP16T_CON0_PRELOAD_MSK))
  191. #define LPTIM_PRELOAD_WAIT(x) (CLEAR_BIT((x)->perh->CR0, LP16T_CON0_PRELOAD_MSK))
  192. #define LPTIM_WAVEPOL_NORMAL(x) (MODIFY_REG((x)->perh->CR0, LP16T_CON0_WAVE_MSK, 0 << LP16T_CON0_WAVE_POSS))
  193. #define LPTIM_WAVEPOL_INVERSE(x) (MODIFY_REG((x)->perh->CR0, LP16T_CON0_WAVE_MSK, 1 << LP16T_CON0_WAVE_POSS))
  194. /**
  195. * @}
  196. */
  197. /** @defgroup LPTIM_Private_Macros LPTIM Private Macros
  198. * @{
  199. */
  200. #define IS_LPTIM(x) ((x) == LPTIM0)
  201. #define IS_LPTIM_CKSEL(x) (((x) == LPTIM_CKSEL_INTERNAL) || \
  202. ((x) == LPTIM_CKSEL_EXTERNAL))
  203. #define IS_LPTIM_CKPOL(x) (((x) == LPTIM_CKPOL_RISING) || \
  204. ((x) == LPTIM_CKPOL_FALLING))
  205. #define IS_LPTIM_MODE(x) (((x) == LPTIM_MODE_SINGLE) || \
  206. ((x) == LPTIM_MODE_CONTINUOUS))
  207. #define IS_LPTIM_CKFLT(x) (((x) == LPTIM_CKFLT_0) || \
  208. ((x) == LPTIM_CKFLT_2) || \
  209. ((x) == LPTIM_CKFLT_4) || \
  210. ((x) == LPTIM_CKFLT_8))
  211. #define IS_LPTIM_TRGFLT(x) (((x) == LPTIM_TRGFLT_0) || \
  212. ((x) == LPTIM_TRGFLT_2) || \
  213. ((x) == LPTIM_TRGFLT_4) || \
  214. ((x) == LPTIM_TRGFLT_8))
  215. #define IS_LPTIM_PRESC(x) (((x) == LPTIM_PRESC_1) || \
  216. ((x) == LPTIM_PRESC_2) || \
  217. ((x) == LPTIM_PRESC_4) || \
  218. ((x) == LPTIM_PRESC_8) || \
  219. ((x) == LPTIM_PRESC_16) || \
  220. ((x) == LPTIM_PRESC_32) || \
  221. ((x) == LPTIM_PRESC_64) || \
  222. ((x) == LPTIM_PRESC_128))
  223. #define IS_LPTIM_TRIGSEL(x) (((x) == LPTIM_TRIGSEL_EXT0) || \
  224. ((x) == LPTIM_TRIGSEL_EXT1) || \
  225. ((x) == LPTIM_TRIGSEL_EXT2) || \
  226. ((x) == LPTIM_TRIGSEL_EXT3) || \
  227. ((x) == LPTIM_TRIGSEL_EXT4) || \
  228. ((x) == LPTIM_TRIGSEL_EXT5) || \
  229. ((x) == LPTIM_TRIGSEL_EXT6) || \
  230. ((x) == LPTIM_TRIGSEL_EXT7))
  231. #define IS_LPTIM_TRIGEN(x) (((x) == LPTIM_TRIGEN_SW) || \
  232. ((x) == LPTIM_TRIGEN_RISING) || \
  233. ((x) == LPTIM_TRIGEN_FALLING) || \
  234. ((x) == LPTIM_TRIGEN_BOTH))
  235. #define IS_LPTIM_IT(x) (((x) == LPTIM_IT_CMPMAT) || \
  236. ((x) == LPTIM_IT_ARRMAT) || \
  237. ((x) == LPTIM_IT_EXTTRIG))
  238. #define IS_LPTIM_FLAG(x) (((x) == LPTIM_FLAG_CMPMAT) || \
  239. ((x) == LPTIM_FLAG_ARRMAT) || \
  240. ((x) == LPTIM_FLAG_EXTTRIG))
  241. /**
  242. * @}
  243. */
  244. /** @addtogroup LPTIM_Public_Functions
  245. * @{
  246. */
  247. /** @addtogroup LPTIM_Public_Functions_Group1
  248. * @{
  249. */
  250. void lptim_reset(lptim_handle_t *hperh);
  251. void lptim_trigger_config(lptim_handle_t *hperh, lptim_trigger_init_t *config);
  252. void lptim_clock_source_config(lptim_handle_t *hperh, lptim_clock_source_init_t *config);
  253. void lptim_trigger_filter_config(lptim_handle_t *hperh, lptim_trgflt_t flt);
  254. void lptim_clock_filter_config(lptim_handle_t *hperh, lptim_ckflt_t flt);
  255. /**
  256. * @}
  257. */
  258. /** @addtogroup LPTIM_Public_Functions_Group2
  259. * @{
  260. */
  261. ald_status_t lptim_toggle_init(lptim_handle_t *hperh);
  262. void lptim_toggle_start(lptim_handle_t *hperh);
  263. void lptim_toggle_stop(lptim_handle_t *hperh);
  264. void lptim_toggle_start_by_it(lptim_handle_t *hperh);
  265. void lptim_toggle_stop_by_it(lptim_handle_t *hperh);
  266. /**
  267. * @}
  268. */
  269. /** @addtogroup LPTIM_Public_Functions_Group3
  270. * @{
  271. */
  272. ald_status_t lptim_pulse_init(lptim_handle_t *hperh);
  273. void lptim_pulse_start(lptim_handle_t *hperh);
  274. void lptim_pulse_stop(lptim_handle_t *hperh);
  275. void lptim_pulse_start_by_it(lptim_handle_t *hperh);
  276. void lptim_pulse_stop_by_it(lptim_handle_t *hperh);
  277. /**
  278. * @}
  279. */
  280. /** @addtogroup LPTIM_Public_Functions_Group4
  281. * @{
  282. */
  283. ald_status_t lptim_pwm_init(lptim_handle_t *hperh);
  284. void lptim_pwm_start(lptim_handle_t *hperh);
  285. void lptim_pwm_stop(lptim_handle_t *hperh);
  286. void lptim_pwm_start_by_it(lptim_handle_t *hperh);
  287. void lptim_pwm_stop_by_it(lptim_handle_t *hperh);
  288. /**
  289. * @}
  290. */
  291. /** @addtogroup LPTIM_Public_Functions_Group5
  292. * @{
  293. */
  294. void lptim_irq_handle(lptim_handle_t *hperh);
  295. void lptim_interrupt_config(lptim_handle_t *hperh, lptim_it_t it, type_func_t state);
  296. it_status_t lptim_get_it_status(lptim_handle_t *hperh, lptim_it_t it);
  297. flag_status_t lptim_get_flag_status(lptim_handle_t *hperh, lptim_flag_t flag);
  298. void lptim_clear_flag_status(lptim_handle_t *hperh, lptim_flag_t flag);
  299. /**
  300. * @}
  301. */
  302. /** @addtogroup LPTIM_Public_Functions_Group6
  303. * @{
  304. */
  305. lptim_state_t lptim_get_state(lptim_handle_t *hperh);
  306. /**
  307. * @}
  308. */
  309. /**
  310. * @}
  311. */
  312. /**
  313. * @}
  314. */
  315. /**
  316. * @}
  317. */
  318. #ifdef __cplusplus
  319. }
  320. #endif
  321. #endif /* __ALD_LPTIM_H__ */