lpc_timer.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**********************************************************************
  2. * $Id$ lpc_timer.h 2011-06-02
  3. *//**
  4. * @file lpc_timer.h
  5. * @brief Contains all macro definitions and function prototypes
  6. * support for Timer firmware library on LPC
  7. * @version 1.0
  8. * @date 02. June. 2011
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2011, NXP Semiconductor
  12. * All rights reserved.
  13. *
  14. ***********************************************************************
  15. * Software that is described herein is for illustrative purposes only
  16. * which provides customers with programming information regarding the
  17. * products. This software is supplied "AS IS" without any warranties.
  18. * NXP Semiconductors assumes no responsibility or liability for the
  19. * use of the software, conveys no license or title under any patent,
  20. * copyright, or mask work right to the product. NXP Semiconductors
  21. * reserves the right to make changes in the software without
  22. * notification. NXP Semiconductors also make no representation or
  23. * warranty that such application will be suitable for the specified
  24. * use without further testing or modification.
  25. * Permission to use, copy, modify, and distribute this software and its
  26. * documentation is hereby granted, under NXP Semiconductors'
  27. * relevant copyright in the software, without fee, provided that it
  28. * is used in conjunction with NXP Semiconductors microcontrollers. This
  29. * copyright, permission, and disclaimer notice must appear in all copies of
  30. * this code.
  31. **********************************************************************/
  32. /* Peripheral group ----------------------------------------------------------- */
  33. /** @defgroup TIMER TIM (Timer)
  34. * @ingroup LPC_CMSIS_FwLib_Drivers
  35. * @{
  36. */
  37. #ifndef __LPC_TIMER_H_
  38. #define __LPC_TIMER_H_
  39. /* Includes ------------------------------------------------------------------- */
  40. #include "LPC407x_8x_177x_8x.h"
  41. #include "lpc_types.h"
  42. #ifdef __cplusplus
  43. extern "C"
  44. {
  45. #endif
  46. /* Private Macros ------------------------------------------------------------- */
  47. /** @defgroup TIM_Private_Macros Timer Private Macros
  48. * @{
  49. */
  50. /* --------------------- BIT DEFINITIONS -------------------------------------- */
  51. /**********************************************************************
  52. ** Interrupt information
  53. **********************************************************************/
  54. /** Macro to clean interrupt pending */
  55. #define TIM_IR_CLR(n) _BIT(n)
  56. /**********************************************************************
  57. ** Timer interrupt register definitions
  58. **********************************************************************/
  59. /** Macro for getting a timer match interrupt bit */
  60. #define TIM_MATCH_INT(n) (_BIT(n & 0x0F))
  61. /** Macro for getting a capture event interrupt bit */
  62. #define TIM_CAP_INT(n) (_BIT(((n & 0x0F) + 4)))
  63. /**********************************************************************
  64. * Timer control register definitions
  65. **********************************************************************/
  66. /** Timer/counter enable bit */
  67. #define TIM_ENABLE ((uint32_t)(1<<0))
  68. /** Timer/counter reset bit */
  69. #define TIM_RESET ((uint32_t)(1<<1))
  70. /** Timer control bit mask */
  71. #define TIM_TCR_MASKBIT ((uint32_t)(3))
  72. /**********************************************************************
  73. * Timer match control register definitions
  74. **********************************************************************/
  75. /** Bit location for interrupt on MRx match, n = 0 to 3 */
  76. #define TIM_INT_ON_MATCH(n) (_BIT((n * 3)))
  77. /** Bit location for reset on MRx match, n = 0 to 3 */
  78. #define TIM_RESET_ON_MATCH(n) (_BIT(((n * 3) + 1)))
  79. /** Bit location for stop on MRx match, n = 0 to 3 */
  80. #define TIM_STOP_ON_MATCH(n) (_BIT(((n * 3) + 2)))
  81. /** Timer Match control bit mask */
  82. #define TIM_MCR_MASKBIT ((uint32_t)(0x0FFF))
  83. /** Timer Match control bit mask for specific channel*/
  84. #define TIM_MCR_CHANNEL_MASKBIT(n) ((uint32_t)(7<<(n*3)))
  85. /**********************************************************************
  86. * Timer capture control register definitions
  87. **********************************************************************/
  88. /** Bit location for CAP.n on CRx rising edge, n = 0 to 3 */
  89. #define TIM_CAP_RISING(n) (_BIT((n * 3)))
  90. /** Bit location for CAP.n on CRx falling edge, n = 0 to 3 */
  91. #define TIM_CAP_FALLING(n) (_BIT(((n * 3) + 1)))
  92. /** Bit location for CAP.n on CRx interrupt enable, n = 0 to 3 */
  93. #define TIM_INT_ON_CAP(n) (_BIT(((n * 3) + 2)))
  94. /** Mask bit for rising and falling edge bit */
  95. #define TIM_EDGE_MASK(n) (_SBF((n * 3), 0x03))
  96. /** Timer capture control bit mask */
  97. #define TIM_CCR_MASKBIT ((uint32_t)(0x3F))
  98. /** Timer Capture control bit mask for specific channel*/
  99. #define TIM_CCR_CHANNEL_MASKBIT(n) ((uint32_t)(7<<(n*3)))
  100. /**********************************************************************
  101. * Timer external match register definitions
  102. **********************************************************************/
  103. /** Bit location for output state change of MAT.n when external match
  104. happens, n = 0 to 3 */
  105. #define TIM_EM(n) _BIT(n)
  106. /** Output state change of MAT.n when external match happens: no change */
  107. #define TIM_EM_NOTHING ((uint8_t)(0x0))
  108. /** Output state change of MAT.n when external match happens: low */
  109. #define TIM_EM_LOW ((uint8_t)(0x1))
  110. /** Output state change of MAT.n when external match happens: high */
  111. #define TIM_EM_HIGH ((uint8_t)(0x2))
  112. /** Output state change of MAT.n when external match happens: toggle */
  113. #define TIM_EM_TOGGLE ((uint8_t)(0x3))
  114. /** Macro for setting for the MAT.n change state bits */
  115. #define TIM_EM_SET(n,s) (_SBF(((n << 1) + 4), (s & 0x03)))
  116. /** Mask for the MAT.n change state bits */
  117. #define TIM_EM_MASK(n) (_SBF(((n << 1) + 4), 0x03))
  118. /** Timer external match bit mask */
  119. #define TIM_EMR_MASKBIT 0x0FFF
  120. /**********************************************************************
  121. * Timer Count Control Register definitions
  122. **********************************************************************/
  123. /** Mask to get the Counter/timer mode bits */
  124. #define TIM_CTCR_MODE_MASK 0x3
  125. /** Mask to get the count input select bits */
  126. #define TIM_CTCR_INPUT_MASK 0xC
  127. /** Timer Count control bit mask */
  128. #define TIM_CTCR_MASKBIT 0xF
  129. #define TIM_COUNTER_MODE ((uint8_t)(1))
  130. /**
  131. * @}
  132. */
  133. /* Public Types --------------------------------------------------------------- */
  134. /** @defgroup TIM_Public_Types Timer Public Types
  135. * @{
  136. */
  137. /***********************************************************************
  138. * Timer device enumeration
  139. **********************************************************************/
  140. /** @brief interrupt type */
  141. typedef enum
  142. {
  143. TIM_MR0_INT =0, /*!< interrupt for Match channel 0*/
  144. TIM_MR1_INT =1, /*!< interrupt for Match channel 1*/
  145. TIM_MR2_INT =2, /*!< interrupt for Match channel 2*/
  146. TIM_MR3_INT =3, /*!< interrupt for Match channel 3*/
  147. TIM_CR0_INT =4, /*!< interrupt for Capture channel 0*/
  148. TIM_CR1_INT =5, /*!< interrupt for Capture channel 1*/
  149. }TIM_INT_TYPE;
  150. /** @brief Timer/counter operating mode */
  151. typedef enum
  152. {
  153. TIM_TIMER_MODE = 0, /*!< Timer mode */
  154. TIM_COUNTER_RISING_MODE, /*!< Counter rising mode */
  155. TIM_COUNTER_FALLING_MODE, /*!< Counter falling mode */
  156. TIM_COUNTER_ANY_MODE /*!< Counter on both edges */
  157. } TIM_MODE_OPT;
  158. /** @brief Timer/Counter prescale option */
  159. typedef enum
  160. {
  161. TIM_PRESCALE_TICKVAL = 0, /*!< Prescale in absolute value */
  162. TIM_PRESCALE_USVAL /*!< Prescale in microsecond value */
  163. } TIM_PRESCALE_OPT;
  164. /** @brief Counter input option */
  165. typedef enum
  166. {
  167. TIM_COUNTER_INCAP0 = 0, /*!< CAPn.0 input pin for TIMERn */
  168. TIM_COUNTER_INCAP1, /*!< CAPn.1 input pin for TIMERn */
  169. } TIM_COUNTER_INPUT_OPT;
  170. /** @brief Timer/Counter external match option */
  171. typedef enum
  172. {
  173. TIM_EXTMATCH_NOTHING = 0, /*!< Do nothing for external output pin if match */
  174. TIM_EXTMATCH_LOW, /*!< Force external output pin to low if match */
  175. TIM_EXTMATCH_HIGH, /*!< Force external output pin to high if match */
  176. TIM_EXTMATCH_TOGGLE /*!< Toggle external output pin if match */
  177. }TIM_EXTMATCH_OPT;
  178. /** @brief Timer/counter capture mode options */
  179. typedef enum {
  180. TIM_CAPTURE_NONE = 0, /*!< No Capture */
  181. TIM_CAPTURE_RISING, /*!< Rising capture mode */
  182. TIM_CAPTURE_FALLING, /*!< Falling capture mode */
  183. TIM_CAPTURE_ANY /*!< On both edges */
  184. } TIM_CAP_MODE_OPT;
  185. /** @brief Configuration structure in TIMER mode */
  186. typedef struct
  187. {
  188. uint8_t PrescaleOption; /**< Timer Prescale option, should be:
  189. - TIM_PRESCALE_TICKVAL: Prescale in absolute value
  190. - TIM_PRESCALE_USVAL: Prescale in microsecond value
  191. */
  192. uint8_t Reserved[3]; /**< Reserved */
  193. uint32_t PrescaleValue; /**< Prescale value */
  194. } TIM_TIMERCFG_Type;
  195. /** @brief Configuration structure in COUNTER mode */
  196. typedef struct {
  197. uint8_t CounterOption; /**< Counter Option, should be:
  198. - TIM_COUNTER_INCAP0: CAPn.0 input pin for TIMERn
  199. - TIM_COUNTER_INCAP1: CAPn.1 input pin for TIMERn
  200. */
  201. uint8_t CountInputSelect;
  202. uint8_t Reserved[2];
  203. } TIM_COUNTERCFG_Type;
  204. /** @brief Match channel configuration structure */
  205. typedef struct {
  206. uint8_t MatchChannel; /**< Match channel, should be in range
  207. from 0..3 */
  208. uint8_t IntOnMatch; /**< Interrupt On match, should be:
  209. - ENABLE: Enable this function.
  210. - DISABLE: Disable this function.
  211. */
  212. uint8_t StopOnMatch; /**< Stop On match, should be:
  213. - ENABLE: Enable this function.
  214. - DISABLE: Disable this function.
  215. */
  216. uint8_t ResetOnMatch; /**< Reset On match, should be:
  217. - ENABLE: Enable this function.
  218. - DISABLE: Disable this function.
  219. */
  220. uint8_t ExtMatchOutputType; /**< External Match Output type, should be:
  221. - TIM_EXTMATCH_NOTHING: Do nothing for external output pin if match
  222. - TIM_EXTMATCH_LOW: Force external output pin to low if match
  223. - TIM_EXTMATCH_HIGH: Force external output pin to high if match
  224. - TIM_EXTMATCH_TOGGLE: Toggle external output pin if match.
  225. */
  226. uint8_t Reserved[3]; /** Reserved */
  227. uint32_t MatchValue; /** Match value */
  228. } TIM_MATCHCFG_Type;
  229. /** @brief Capture Input configuration structure */
  230. typedef struct {
  231. uint8_t CaptureChannel; /**< Capture channel, should be in range
  232. from 0..1 */
  233. uint8_t RisingEdge; /**< caption rising edge, should be:
  234. - ENABLE: Enable rising edge.
  235. - DISABLE: Disable this function.
  236. */
  237. uint8_t FallingEdge; /**< caption falling edge, should be:
  238. - ENABLE: Enable falling edge.
  239. - DISABLE: Disable this function.
  240. */
  241. uint8_t IntOnCaption; /**< Interrupt On caption, should be:
  242. - ENABLE: Enable interrupt function.
  243. - DISABLE: Disable this function.
  244. */
  245. } TIM_CAPTURECFG_Type;
  246. /**
  247. * @}
  248. */
  249. /* Public Functions ----------------------------------------------------------- */
  250. /** @defgroup TIM_Public_Functions Timer Public Functions
  251. * @{
  252. */
  253. /* Init/DeInit TIM functions -----------*/
  254. void TIM_Init(LPC_TIM_TypeDef *TIMx, TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct);
  255. void TIM_DeInit(LPC_TIM_TypeDef *TIMx);
  256. /* TIM interrupt functions -------------*/
  257. void TIM_ClearIntPending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
  258. void TIM_ClearIntCapturePending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
  259. FlagStatus TIM_GetIntStatus(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
  260. FlagStatus TIM_GetIntCaptureStatus(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
  261. /* TIM configuration functions --------*/
  262. void TIM_ConfigStructInit(TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct);
  263. void TIM_ConfigMatch(LPC_TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct);
  264. void TIM_UpdateMatchValue(LPC_TIM_TypeDef *TIMx,uint8_t MatchChannel, uint32_t MatchValue);
  265. void TIM_ConfigCapture(LPC_TIM_TypeDef *TIMx, TIM_CAPTURECFG_Type *TIM_CaptureConfigStruct);
  266. void TIM_Cmd(LPC_TIM_TypeDef *TIMx, FunctionalState NewState);
  267. uint32_t TIM_GetCaptureValue(LPC_TIM_TypeDef *TIMx, TIM_COUNTER_INPUT_OPT CaptureChannel);
  268. void TIM_ResetCounter(LPC_TIM_TypeDef *TIMx);
  269. void TIM_Waitus(uint32_t time);
  270. void TIM_Waitms(uint32_t time);
  271. /**
  272. * @}
  273. */
  274. #ifdef __cplusplus
  275. }
  276. #endif
  277. #endif /* __LPC_TIMER_H_ */
  278. /**
  279. * @}
  280. */
  281. /* --------------------------------- End Of File ------------------------------ */