lpc-field

Template project for programming NXP's LPC1768 MCUs
git clone git://git.mdnr.space/lpc-field
Log | Files | Refs | README | LICENSE

lpc17xx_timer.h (13678B)


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