PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/src/libs/LPC17xx/LPC17xxLib/inc/lpc17xx_timer.h

https://github.com/Smoothieware/Smoothieware
C Header | 342 lines | 133 code | 47 blank | 162 comment | 20 complexity | 9955bd4a087feb9edc4d5e83f42c9b1c MD5 | raw file
  1. /**********************************************************************
  2. * $Id$ lpc17xx_timer.h 2010-05-21
  3. *//**
  4. * @file lpc17xx_timer.h
  5. * @brief Contains all macro definitions and function prototypes
  6. * support for Timer firmware library on LPC17xx
  7. * @version 2.0
  8. * @date 21. May. 2010
  9. * @author NXP MCU SW Application Team
  10. *
  11. * Copyright(C) 2010, 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. **********************************************************************/
  26. /* Peripheral group ----------------------------------------------------------- */
  27. /** @defgroup TIM TIM (Timer)
  28. * @ingroup LPC1700CMSIS_FwLib_Drivers
  29. * @{
  30. */
  31. #ifndef __LPC17XX_TIMER_H_
  32. #define __LPC17XX_TIMER_H_
  33. /* Includes ------------------------------------------------------------------- */
  34. #include "LPC17xx.h"
  35. #include "lpc_types.h"
  36. #ifdef __cplusplus
  37. extern "C"
  38. {
  39. #endif
  40. /* Private Macros ------------------------------------------------------------- */
  41. /** @defgroup TIM_Private_Macros TIM Private Macros
  42. * @{
  43. */
  44. /* --------------------- BIT DEFINITIONS -------------------------------------- */
  45. /**********************************************************************
  46. ** Interrupt information
  47. **********************************************************************/
  48. /** Macro to clean interrupt pending */
  49. #define TIM_IR_CLR(n) _BIT(n)
  50. /**********************************************************************
  51. ** Timer interrupt register definitions
  52. **********************************************************************/
  53. /** Macro for getting a timer match interrupt bit */
  54. #define TIM_MATCH_INT(n) (_BIT(n & 0x0F))
  55. /** Macro for getting a capture event interrupt bit */
  56. #define TIM_CAP_INT(n) (_BIT(((n & 0x0F) + 4)))
  57. /**********************************************************************
  58. * Timer control register definitions
  59. **********************************************************************/
  60. /** Timer/counter enable bit */
  61. #define TIM_ENABLE ((uint32_t)(1<<0))
  62. /** Timer/counter reset bit */
  63. #define TIM_RESET ((uint32_t)(1<<1))
  64. /** Timer control bit mask */
  65. #define TIM_TCR_MASKBIT ((uint32_t)(3))
  66. /**********************************************************************
  67. * Timer match control register definitions
  68. **********************************************************************/
  69. /** Bit location for interrupt on MRx match, n = 0 to 3 */
  70. #define TIM_INT_ON_MATCH(n) (_BIT((n * 3)))
  71. /** Bit location for reset on MRx match, n = 0 to 3 */
  72. #define TIM_RESET_ON_MATCH(n) (_BIT(((n * 3) + 1)))
  73. /** Bit location for stop on MRx match, n = 0 to 3 */
  74. #define TIM_STOP_ON_MATCH(n) (_BIT(((n * 3) + 2)))
  75. /** Timer Match control bit mask */
  76. #define TIM_MCR_MASKBIT ((uint32_t)(0x0FFF))
  77. /** Timer Match control bit mask for specific channel*/
  78. #define TIM_MCR_CHANNEL_MASKBIT(n) ((uint32_t)(7<<(n*3)))
  79. /**********************************************************************
  80. * Timer capture control register definitions
  81. **********************************************************************/
  82. /** Bit location for CAP.n on CRx rising edge, n = 0 to 3 */
  83. #define TIM_CAP_RISING(n) (_BIT((n * 3)))
  84. /** Bit location for CAP.n on CRx falling edge, n = 0 to 3 */
  85. #define TIM_CAP_FALLING(n) (_BIT(((n * 3) + 1)))
  86. /** Bit location for CAP.n on CRx interrupt enable, n = 0 to 3 */
  87. #define TIM_INT_ON_CAP(n) (_BIT(((n * 3) + 2)))
  88. /** Mask bit for rising and falling edge bit */
  89. #define TIM_EDGE_MASK(n) (_SBF((n * 3), 0x03))
  90. /** Timer capture control bit mask */
  91. #define TIM_CCR_MASKBIT ((uint32_t)(0x3F))
  92. /** Timer Capture control bit mask for specific channel*/
  93. #define TIM_CCR_CHANNEL_MASKBIT(n) ((uint32_t)(7<<(n*3)))
  94. /**********************************************************************
  95. * Timer external match register definitions
  96. **********************************************************************/
  97. /** Bit location for output state change of MAT.n when external match
  98. happens, n = 0 to 3 */
  99. #define TIM_EM(n) _BIT(n)
  100. /** Output state change of MAT.n when external match happens: no change */
  101. #define TIM_EM_NOTHING ((uint8_t)(0x0))
  102. /** Output state change of MAT.n when external match happens: low */
  103. #define TIM_EM_LOW ((uint8_t)(0x1))
  104. /** Output state change of MAT.n when external match happens: high */
  105. #define TIM_EM_HIGH ((uint8_t)(0x2))
  106. /** Output state change of MAT.n when external match happens: toggle */
  107. #define TIM_EM_TOGGLE ((uint8_t)(0x3))
  108. /** Macro for setting for the MAT.n change state bits */
  109. #define TIM_EM_SET(n,s) (_SBF(((n << 1) + 4), (s & 0x03)))
  110. /** Mask for the MAT.n change state bits */
  111. #define TIM_EM_MASK(n) (_SBF(((n << 1) + 4), 0x03))
  112. /** Timer external match bit mask */
  113. #define TIM_EMR_MASKBIT 0x0FFF
  114. /**********************************************************************
  115. * Timer Count Control Register definitions
  116. **********************************************************************/
  117. /** Mask to get the Counter/timer mode bits */
  118. #define TIM_CTCR_MODE_MASK 0x3
  119. /** Mask to get the count input select bits */
  120. #define TIM_CTCR_INPUT_MASK 0xC
  121. /** Timer Count control bit mask */
  122. #define TIM_CTCR_MASKBIT 0xF
  123. #define TIM_COUNTER_MODE ((uint8_t)(1))
  124. /* ---------------- CHECK PARAMETER DEFINITIONS ---------------------------- */
  125. /** Macro to determine if it is valid TIMER peripheral */
  126. #define PARAM_TIMx(n) ((((uint32_t *)n)==((uint32_t *)LPC_TIM0)) || (((uint32_t *)n)==((uint32_t *)LPC_TIM1)) \
  127. || (((uint32_t *)n)==((uint32_t *)LPC_TIM2)) || (((uint32_t *)n)==((uint32_t *)LPC_TIM3)))
  128. /* Macro check interrupt type */
  129. #define PARAM_TIM_INT_TYPE(TYPE) ((TYPE ==TIM_MR0_INT)||(TYPE ==TIM_MR1_INT)\
  130. ||(TYPE ==TIM_MR2_INT)||(TYPE ==TIM_MR3_INT)\
  131. ||(TYPE ==TIM_CR0_INT)||(TYPE ==TIM_CR1_INT))
  132. /* Macro check TIMER mode */
  133. #define PARAM_TIM_MODE_OPT(MODE) ((MODE == TIM_TIMER_MODE)||(MODE == TIM_COUNTER_RISING_MODE)\
  134. || (MODE == TIM_COUNTER_RISING_MODE)||(MODE == TIM_COUNTER_RISING_MODE))
  135. /* Macro check TIMER prescale value */
  136. #define PARAM_TIM_PRESCALE_OPT(OPT) ((OPT == TIM_PRESCALE_TICKVAL)||(OPT == TIM_PRESCALE_USVAL))
  137. /* Macro check TIMER counter intput mode */
  138. #define PARAM_TIM_COUNTER_INPUT_OPT(OPT) ((OPT == TIM_COUNTER_INCAP0)||(OPT == TIM_COUNTER_INCAP1))
  139. /* Macro check TIMER external match mode */
  140. #define PARAM_TIM_EXTMATCH_OPT(OPT) ((OPT == TIM_EXTMATCH_NOTHING)||(OPT == TIM_EXTMATCH_LOW)\
  141. ||(OPT == TIM_EXTMATCH_HIGH)||(OPT == TIM_EXTMATCH_TOGGLE))
  142. /* Macro check TIMER external match mode */
  143. #define PARAM_TIM_CAP_MODE_OPT(OPT) ((OPT == TIM_CAPTURE_NONE)||(OPT == TIM_CAPTURE_RISING) \
  144. ||(OPT == TIM_CAPTURE_FALLING)||(OPT == TIM_CAPTURE_ANY))
  145. /**
  146. * @}
  147. */
  148. /* Public Types --------------------------------------------------------------- */
  149. /** @defgroup TIM_Public_Types TIM Public Types
  150. * @{
  151. */
  152. /***********************************************************************
  153. * Timer device enumeration
  154. **********************************************************************/
  155. /** @brief interrupt type */
  156. typedef enum
  157. {
  158. TIM_MR0_INT =0, /*!< interrupt for Match channel 0*/
  159. TIM_MR1_INT =1, /*!< interrupt for Match channel 1*/
  160. TIM_MR2_INT =2, /*!< interrupt for Match channel 2*/
  161. TIM_MR3_INT =3, /*!< interrupt for Match channel 3*/
  162. TIM_CR0_INT =4, /*!< interrupt for Capture channel 0*/
  163. TIM_CR1_INT =5 /*!< interrupt for Capture channel 1*/
  164. }TIM_INT_TYPE;
  165. /** @brief Timer/counter operating mode */
  166. typedef enum
  167. {
  168. TIM_TIMER_MODE = 0, /*!< Timer mode */
  169. TIM_COUNTER_RISING_MODE, /*!< Counter rising mode */
  170. TIM_COUNTER_FALLING_MODE, /*!< Counter falling mode */
  171. TIM_COUNTER_ANY_MODE /*!< Counter on both edges */
  172. } TIM_MODE_OPT;
  173. /** @brief Timer/Counter prescale option */
  174. typedef enum
  175. {
  176. TIM_PRESCALE_TICKVAL = 0, /*!< Prescale in absolute value */
  177. TIM_PRESCALE_USVAL /*!< Prescale in microsecond value */
  178. } TIM_PRESCALE_OPT;
  179. /** @brief Counter input option */
  180. typedef enum
  181. {
  182. TIM_COUNTER_INCAP0 = 0, /*!< CAPn.0 input pin for TIMERn */
  183. TIM_COUNTER_INCAP1, /*!< CAPn.1 input pin for TIMERn */
  184. } TIM_COUNTER_INPUT_OPT;
  185. /** @brief Timer/Counter external match option */
  186. typedef enum
  187. {
  188. TIM_EXTMATCH_NOTHING = 0, /*!< Do nothing for external output pin if match */
  189. TIM_EXTMATCH_LOW, /*!< Force external output pin to low if match */
  190. TIM_EXTMATCH_HIGH, /*!< Force external output pin to high if match */
  191. TIM_EXTMATCH_TOGGLE /*!< Toggle external output pin if match */
  192. }TIM_EXTMATCH_OPT;
  193. /** @brief Timer/counter capture mode options */
  194. typedef enum {
  195. TIM_CAPTURE_NONE = 0, /*!< No Capture */
  196. TIM_CAPTURE_RISING, /*!< Rising capture mode */
  197. TIM_CAPTURE_FALLING, /*!< Falling capture mode */
  198. TIM_CAPTURE_ANY /*!< On both edges */
  199. } TIM_CAP_MODE_OPT;
  200. /** @brief Configuration structure in TIMER mode */
  201. typedef struct
  202. {
  203. uint8_t PrescaleOption; /**< Timer Prescale option, should be:
  204. - TIM_PRESCALE_TICKVAL: Prescale in absolute value
  205. - TIM_PRESCALE_USVAL: Prescale in microsecond value
  206. */
  207. uint8_t Reserved[3]; /**< Reserved */
  208. uint32_t PrescaleValue; /**< Prescale value */
  209. } TIM_TIMERCFG_Type;
  210. /** @brief Configuration structure in COUNTER mode */
  211. typedef struct {
  212. uint8_t CounterOption; /**< Counter Option, should be:
  213. - TIM_COUNTER_INCAP0: CAPn.0 input pin for TIMERn
  214. - TIM_COUNTER_INCAP1: CAPn.1 input pin for TIMERn
  215. */
  216. uint8_t CountInputSelect;
  217. uint8_t Reserved[2];
  218. } TIM_COUNTERCFG_Type;
  219. /** @brief Match channel configuration structure */
  220. typedef struct {
  221. uint8_t MatchChannel; /**< Match channel, should be in range
  222. from 0..3 */
  223. uint8_t IntOnMatch; /**< Interrupt On match, should be:
  224. - ENABLE: Enable this function.
  225. - DISABLE: Disable this function.
  226. */
  227. uint8_t StopOnMatch; /**< Stop On match, should be:
  228. - ENABLE: Enable this function.
  229. - DISABLE: Disable this function.
  230. */
  231. uint8_t ResetOnMatch; /**< Reset On match, should be:
  232. - ENABLE: Enable this function.
  233. - DISABLE: Disable this function.
  234. */
  235. uint8_t ExtMatchOutputType; /**< External Match Output type, should be:
  236. - TIM_EXTMATCH_NOTHING: Do nothing for external output pin if match
  237. - TIM_EXTMATCH_LOW: Force external output pin to low if match
  238. - TIM_EXTMATCH_HIGH: Force external output pin to high if match
  239. - TIM_EXTMATCH_TOGGLE: Toggle external output pin if match.
  240. */
  241. uint8_t Reserved[3]; /** Reserved */
  242. uint32_t MatchValue; /** Match value */
  243. } TIM_MATCHCFG_Type;
  244. /** @brief Capture Input configuration structure */
  245. typedef struct {
  246. uint8_t CaptureChannel; /**< Capture channel, should be in range
  247. from 0..1 */
  248. uint8_t RisingEdge; /**< caption rising edge, should be:
  249. - ENABLE: Enable rising edge.
  250. - DISABLE: Disable this function.
  251. */
  252. uint8_t FallingEdge; /**< caption falling edge, should be:
  253. - ENABLE: Enable falling edge.
  254. - DISABLE: Disable this function.
  255. */
  256. uint8_t IntOnCaption; /**< Interrupt On caption, should be:
  257. - ENABLE: Enable interrupt function.
  258. - DISABLE: Disable this function.
  259. */
  260. } TIM_CAPTURECFG_Type;
  261. /**
  262. * @}
  263. */
  264. /* Public Functions ----------------------------------------------------------- */
  265. /** @defgroup TIM_Public_Functions TIM Public Functions
  266. * @{
  267. */
  268. /* Init/DeInit TIM functions -----------*/
  269. void TIM_Init(LPC_TIM_TypeDef *TIMx, TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct);
  270. void TIM_DeInit(LPC_TIM_TypeDef *TIMx);
  271. /* TIM interrupt functions -------------*/
  272. void TIM_ClearIntPending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
  273. void TIM_ClearIntCapturePending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
  274. FlagStatus TIM_GetIntStatus(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
  275. FlagStatus TIM_GetIntCaptureStatus(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
  276. /* TIM configuration functions --------*/
  277. void TIM_ConfigStructInit(TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct);
  278. void TIM_ConfigMatch(LPC_TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct);
  279. void TIM_UpdateMatchValue(LPC_TIM_TypeDef *TIMx,uint8_t MatchChannel, uint32_t MatchValue);
  280. void TIM_SetMatchExt(LPC_TIM_TypeDef *TIMx,TIM_EXTMATCH_OPT ext_match );
  281. void TIM_ConfigCapture(LPC_TIM_TypeDef *TIMx, TIM_CAPTURECFG_Type *TIM_CaptureConfigStruct);
  282. void TIM_Cmd(LPC_TIM_TypeDef *TIMx, FunctionalState NewState);
  283. uint32_t TIM_GetCaptureValue(LPC_TIM_TypeDef *TIMx, TIM_COUNTER_INPUT_OPT CaptureChannel);
  284. void TIM_ResetCounter(LPC_TIM_TypeDef *TIMx);
  285. /**
  286. * @}
  287. */
  288. #ifdef __cplusplus
  289. }
  290. #endif
  291. #endif /* __LPC17XX_TIMER_H_ */
  292. /**
  293. * @}
  294. */
  295. /* --------------------------------- End Of File ------------------------------ */