Date: March 20, 2011 This library is created from "LPC17xx CMSIS-Compliant Standard Peripheral Firmware Driver Library (GNU, Keil, IAR) (Jan 28, 2011)", available from NXP's website, under "All microcontrollers support documents" [[http://ics.nxp.com/support/documents/microcontrollers/?type=software]] You will need to follow [[/projects/libraries/svn/mbed/trunk/LPC1768/LPC17xx.h]] while using this library Examples provided here [[/users/frank26080115/programs/LPC1700CMSIS_Examples/]] The beautiful thing is that NXP does not place copyright protection on any of the files in here Only a few modifications are made to make it compile with the mbed online compiler, I fixed some warnings as well. This is untested as of March 20, 2011 Forum post about this library: [[/forum/mbed/topic/2030/]]

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lpc17xx_timer.h Source File

lpc17xx_timer.h

Go to the documentation of this file.
00001 /***********************************************************************//**
00002  * @file        lpc17xx_timer.h
00003  * @brief       Contains all functions support for Timer firmware library on LPC17xx
00004  * @version     2.0
00005  * @date        21. May. 2010
00006  * @author      NXP MCU SW Application Team
00007  **************************************************************************
00008  * Software that is described herein is for illustrative purposes only
00009  * which provides customers with programming information regarding the
00010  * products. This software is supplied "AS IS" without any warranties.
00011  * NXP Semiconductors assumes no responsibility or liability for the
00012  * use of the software, conveys no license or title under any patent,
00013  * copyright, or mask work right to the product. NXP Semiconductors
00014  * reserves the right to make changes in the software without
00015  * notification. NXP Semiconductors also make no representation or
00016  * warranty that such application will be suitable for the specified
00017  * use without further testing or modification.
00018  **********************************************************************/
00019 
00020 /* Peripheral group ----------------------------------------------------------- */
00021 /** @defgroup TIM TIM
00022  * @ingroup LPC1700CMSIS_FwLib_Drivers
00023  * @{
00024  */
00025 
00026 #ifndef __LPC17XX_TIMER_H_
00027 #define __LPC17XX_TIMER_H_
00028 
00029 /* Includes ------------------------------------------------------------------- */
00030 #include "LPC17xx.h"
00031 #include "lpc_types.h"
00032 
00033 #ifdef __cplusplus
00034 extern "C"
00035 {
00036 #endif
00037 
00038 /* Private Macros ------------------------------------------------------------- */
00039 /** @defgroup TIM_Private_Macros TIM Private Macros
00040  * @{
00041  */
00042 
00043 /* --------------------- BIT DEFINITIONS -------------------------------------- */
00044 /**********************************************************************
00045 ** Interrupt information
00046 **********************************************************************/
00047 /** Macro to clean interrupt pending */
00048 #define TIM_IR_CLR(n) _BIT(n)
00049 
00050 /**********************************************************************
00051 ** Timer interrupt register definitions
00052 **********************************************************************/
00053 /** Macro for getting a timer match interrupt bit */
00054 #define TIM_MATCH_INT(n)        (_BIT(n & 0x0F))
00055 /** Macro for getting a capture event interrupt bit */
00056 #define TIM_CAP_INT(n)     (_BIT(((n & 0x0F) + 4)))
00057 
00058 /**********************************************************************
00059 * Timer control register definitions
00060 **********************************************************************/
00061 /** Timer/counter enable bit */
00062 #define TIM_ENABLE          ((uint32_t)(1<<0))
00063 /** Timer/counter reset bit */
00064 #define TIM_RESET           ((uint32_t)(1<<1))
00065 /** Timer control bit mask */
00066 #define TIM_TCR_MASKBIT     ((uint32_t)(3))
00067 
00068 /**********************************************************************
00069 * Timer match control register definitions
00070 **********************************************************************/
00071 /** Bit location for interrupt on MRx match, n = 0 to 3 */
00072 #define TIM_INT_ON_MATCH(n)         (_BIT((n * 3)))
00073 /** Bit location for reset on MRx match, n = 0 to 3 */
00074 #define TIM_RESET_ON_MATCH(n)       (_BIT(((n * 3) + 1)))
00075 /** Bit location for stop on MRx match, n = 0 to 3 */
00076 #define TIM_STOP_ON_MATCH(n)        (_BIT(((n * 3) + 2)))
00077 /** Timer Match control bit mask */
00078 #define TIM_MCR_MASKBIT            ((uint32_t)(0x0FFF))
00079 /** Timer Match control bit mask for specific channel*/
00080 #define TIM_MCR_CHANNEL_MASKBIT(n)      ((uint32_t)(7<<(n*3)))
00081 
00082 /**********************************************************************
00083 * Timer capture control register definitions
00084 **********************************************************************/
00085 /** Bit location for CAP.n on CRx rising edge, n = 0 to 3 */
00086 #define TIM_CAP_RISING(n)       (_BIT((n * 3)))
00087 /** Bit location for CAP.n on CRx falling edge, n = 0 to 3 */
00088 #define TIM_CAP_FALLING(n)      (_BIT(((n * 3) + 1)))
00089 /** Bit location for CAP.n on CRx interrupt enable, n = 0 to 3 */
00090 #define TIM_INT_ON_CAP(n)       (_BIT(((n * 3) + 2)))
00091 /** Mask bit for rising and falling edge bit */
00092 #define TIM_EDGE_MASK(n)        (_SBF((n * 3), 0x03))
00093 /** Timer capture control bit mask */
00094 #define TIM_CCR_MASKBIT         ((uint32_t)(0x3F))
00095 /** Timer Capture control bit mask for specific channel*/
00096 #define TIM_CCR_CHANNEL_MASKBIT(n)      ((uint32_t)(7<<(n*3)))
00097 
00098 /**********************************************************************
00099 * Timer external match register definitions
00100 **********************************************************************/
00101 /** Bit location for output state change of MAT.n when external match
00102    happens, n = 0 to 3 */
00103 #define TIM_EM(n)               _BIT(n)
00104 /** Output state change of MAT.n when external match happens: no change */
00105 #define TIM_EM_NOTHING      ((uint8_t)(0x0))
00106 /** Output state change of MAT.n when external match happens: low */
00107 #define TIM_EM_LOW          ((uint8_t)(0x1))
00108 /** Output state change of MAT.n when external match happens: high */
00109 #define TIM_EM_HIGH         ((uint8_t)(0x2))
00110 /** Output state change of MAT.n when external match happens: toggle */
00111 #define TIM_EM_TOGGLE       ((uint8_t)(0x3))
00112 /** Macro for setting for the MAT.n change state bits */
00113 #define TIM_EM_SET(n,s)     (_SBF(((n << 1) + 4), (s & 0x03)))
00114 /** Mask for the MAT.n change state bits */
00115 #define TIM_EM_MASK(n)      (_SBF(((n << 1) + 4), 0x03))
00116 /** Timer external match bit mask */
00117 #define TIM_EMR_MASKBIT 0x0FFF
00118 
00119 /**********************************************************************
00120 * Timer Count Control Register definitions
00121 **********************************************************************/
00122 /** Mask to get the Counter/timer mode bits */
00123 #define TIM_CTCR_MODE_MASK  0x3
00124 /** Mask to get the count input select bits */
00125 #define TIM_CTCR_INPUT_MASK 0xC
00126 /** Timer Count control bit mask */
00127 #define TIM_CTCR_MASKBIT    0xF
00128 #define TIM_COUNTER_MODE ((uint8_t)(1))
00129 
00130 
00131 /* ---------------- CHECK PARAMETER DEFINITIONS ---------------------------- */
00132 /** Macro to determine if it is valid TIMER peripheral */
00133 #define PARAM_TIMx(n)   ((((uint32_t *)n)==((uint32_t *)LPC_TIM0)) || (((uint32_t *)n)==((uint32_t *)LPC_TIM1)) \
00134 || (((uint32_t *)n)==((uint32_t *)LPC_TIM2)) || (((uint32_t *)n)==((uint32_t *)LPC_TIM3)))
00135 
00136 /* Macro check interrupt type */
00137 #define PARAM_TIM_INT_TYPE(TYPE)    ((TYPE ==TIM_MR0_INT)||(TYPE ==TIM_MR1_INT)\
00138 ||(TYPE ==TIM_MR2_INT)||(TYPE ==TIM_MR3_INT)\
00139 ||(TYPE ==TIM_CR0_INT)||(TYPE ==TIM_CR1_INT))
00140 
00141 /* Macro check TIMER mode */
00142 #define PARAM_TIM_MODE_OPT(MODE)    ((MODE == TIM_TIMER_MODE)||(MODE == TIM_COUNTER_RISING_MODE)\
00143 || (MODE == TIM_COUNTER_RISING_MODE)||(MODE == TIM_COUNTER_RISING_MODE))
00144 
00145 /* Macro check TIMER prescale value */
00146 #define PARAM_TIM_PRESCALE_OPT(OPT) ((OPT == TIM_PRESCALE_TICKVAL)||(OPT == TIM_PRESCALE_USVAL))
00147 
00148 /* Macro check TIMER counter intput mode */
00149 #define PARAM_TIM_COUNTER_INPUT_OPT(OPT)    ((OPT == TIM_COUNTER_INCAP0)||(OPT == TIM_COUNTER_INCAP1))
00150 
00151 /* Macro check TIMER external match mode */
00152 #define PARAM_TIM_EXTMATCH_OPT(OPT) ((OPT == TIM_EXTMATCH_NOTHING)||(OPT == TIM_EXTMATCH_LOW)\
00153 ||(OPT == TIM_EXTMATCH_HIGH)||(OPT == TIM_EXTMATCH_TOGGLE))
00154 
00155 /* Macro check TIMER external match mode */
00156 #define PARAM_TIM_CAP_MODE_OPT(OPT) ((OPT == TIM_CAPTURE_NONE)||(OPT == TIM_CAPTURE_RISING) \
00157 ||(OPT == TIM_CAPTURE_FALLING)||(OPT == TIM_CAPTURE_ANY))
00158 
00159 /**
00160  * @}
00161  */
00162 
00163 
00164 /* Public Types --------------------------------------------------------------- */
00165 /** @defgroup TIM_Public_Types TIM Public Types
00166  * @{
00167  */
00168 
00169 /***********************************************************************
00170  * Timer device enumeration
00171 **********************************************************************/
00172 /** @brief interrupt type */
00173 typedef enum
00174 {
00175     TIM_MR0_INT  =0, /*!< interrupt for Match channel 0*/
00176     TIM_MR1_INT  =1, /*!< interrupt for Match channel 1*/
00177     TIM_MR2_INT  =2, /*!< interrupt for Match channel 2*/
00178     TIM_MR3_INT  =3, /*!< interrupt for Match channel 3*/
00179     TIM_CR0_INT  =4, /*!< interrupt for Capture channel 0*/
00180     TIM_CR1_INT  =5, /*!< interrupt for Capture channel 1*/
00181 }TIM_INT_TYPE;
00182 
00183 /** @brief Timer/counter operating mode */
00184 typedef enum
00185 {
00186     TIM_TIMER_MODE  = 0,             /*!< Timer mode */
00187     TIM_COUNTER_RISING_MODE ,        /*!< Counter rising mode */
00188     TIM_COUNTER_FALLING_MODE ,       /*!< Counter falling mode */
00189     TIM_COUNTER_ANY_MODE             /*!< Counter on both edges */
00190 } TIM_MODE_OPT;
00191 
00192 /** @brief Timer/Counter prescale option */
00193 typedef enum
00194 {
00195     TIM_PRESCALE_TICKVAL  = 0,       /*!< Prescale in absolute value */
00196     TIM_PRESCALE_USVAL               /*!< Prescale in microsecond value */
00197 } TIM_PRESCALE_OPT;
00198 
00199 /** @brief Counter input option */
00200 typedef enum
00201 {
00202     TIM_COUNTER_INCAP0  = 0,         /*!< CAPn.0 input pin for TIMERn */
00203     TIM_COUNTER_INCAP1 ,             /*!< CAPn.1 input pin for TIMERn */
00204 } TIM_COUNTER_INPUT_OPT;
00205 
00206 /** @brief Timer/Counter external match option */
00207 typedef enum
00208 {
00209     TIM_EXTMATCH_NOTHING  = 0,       /*!< Do nothing for external output pin if match */
00210     TIM_EXTMATCH_LOW ,               /*!< Force external output pin to low if match */
00211     TIM_EXTMATCH_HIGH ,              /*!< Force external output pin to high if match */
00212     TIM_EXTMATCH_TOGGLE              /*!< Toggle external output pin if match */
00213 }TIM_EXTMATCH_OPT;
00214 
00215 /** @brief Timer/counter capture mode options */
00216 typedef enum {
00217     TIM_CAPTURE_NONE  = 0,   /*!< No Capture */
00218     TIM_CAPTURE_RISING ,     /*!< Rising capture mode */
00219     TIM_CAPTURE_FALLING ,    /*!< Falling capture mode */
00220     TIM_CAPTURE_ANY          /*!< On both edges */
00221 } TIM_CAP_MODE_OPT;
00222 
00223 /** @brief Configuration structure in TIMER mode */
00224 typedef struct
00225 {
00226 
00227     uint8_t PrescaleOption;     /**< Timer Prescale option, should be:
00228                                     - TIM_PRESCALE_TICKVAL: Prescale in absolute value
00229                                     - TIM_PRESCALE_USVAL: Prescale in microsecond value
00230                                     */
00231     uint8_t Reserved[3];        /**< Reserved */
00232     uint32_t PrescaleValue;     /**< Prescale value */
00233 } TIM_TIMERCFG_Type;
00234 
00235 /** @brief Configuration structure in COUNTER mode */
00236 typedef struct {
00237 
00238     uint8_t CounterOption;      /**< Counter Option, should be:
00239                                 - TIM_COUNTER_INCAP0: CAPn.0 input pin for TIMERn
00240                                 - TIM_COUNTER_INCAP1: CAPn.1 input pin for TIMERn
00241                                 */
00242     uint8_t CountInputSelect;
00243     uint8_t Reserved[2];
00244 } TIM_COUNTERCFG_Type;
00245 
00246 /** @brief Match channel configuration structure */
00247 typedef struct {
00248     uint8_t MatchChannel;   /**< Match channel, should be in range
00249                             from 0..3 */
00250     uint8_t IntOnMatch;     /**< Interrupt On match, should be:
00251                             - ENABLE: Enable this function.
00252                             - DISABLE: Disable this function.
00253                             */
00254     uint8_t StopOnMatch;    /**< Stop On match, should be:
00255                             - ENABLE: Enable this function.
00256                             - DISABLE: Disable this function.
00257                             */
00258     uint8_t ResetOnMatch;   /**< Reset On match, should be:
00259                             - ENABLE: Enable this function.
00260                             - DISABLE: Disable this function.
00261                             */
00262 
00263     uint8_t ExtMatchOutputType; /**< External Match Output type, should be:
00264                              -   TIM_EXTMATCH_NOTHING:  Do nothing for external output pin if match
00265                              -   TIM_EXTMATCH_LOW:  Force external output pin to low if match
00266                              -   TIM_EXTMATCH_HIGH: Force external output pin to high if match
00267                              -   TIM_EXTMATCH_TOGGLE: Toggle external output pin if match.
00268                             */
00269     uint8_t Reserved[3];    /** Reserved */
00270     uint32_t MatchValue;    /** Match value */
00271 } TIM_MATCHCFG_Type;
00272 
00273 /** @brief Capture Input configuration structure */
00274 typedef struct {
00275     uint8_t CaptureChannel; /**< Capture channel, should be in range
00276                             from 0..1 */
00277     uint8_t RisingEdge;     /**< caption rising edge, should be:
00278                             - ENABLE: Enable rising edge.
00279                             - DISABLE: Disable this function.
00280                             */
00281     uint8_t FallingEdge;        /**< caption falling edge, should be:
00282                             - ENABLE: Enable falling edge.
00283                             - DISABLE: Disable this function.
00284                                 */
00285     uint8_t IntOnCaption;   /**< Interrupt On caption, should be:
00286                             - ENABLE: Enable interrupt function.
00287                             - DISABLE: Disable this function.
00288                             */
00289 
00290 } TIM_CAPTURECFG_Type;
00291 
00292 /**
00293  * @}
00294  */
00295 
00296 
00297 /* Public Functions ----------------------------------------------------------- */
00298 /** @defgroup TIM_Public_Functions TIM Public Functions
00299  * @{
00300  */
00301 /* Init/DeInit TIM functions -----------*/
00302 void TIM_Init(LPC_TIM_TypeDef *TIMx, TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct);
00303 void TIM_DeInit(LPC_TIM_TypeDef *TIMx);
00304 
00305 /* TIM interrupt functions -------------*/
00306 void TIM_ClearIntPending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
00307 void TIM_ClearIntCapturePending(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
00308 FlagStatus TIM_GetIntStatus(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
00309 FlagStatus TIM_GetIntCaptureStatus(LPC_TIM_TypeDef *TIMx, TIM_INT_TYPE IntFlag);
00310 
00311 /* TIM configuration functions --------*/
00312 void TIM_ConfigStructInit(TIM_MODE_OPT TimerCounterMode, void *TIM_ConfigStruct);
00313 void TIM_ConfigMatch(LPC_TIM_TypeDef *TIMx, TIM_MATCHCFG_Type *TIM_MatchConfigStruct);
00314 void TIM_UpdateMatchValue(LPC_TIM_TypeDef *TIMx,uint8_t MatchChannel, uint32_t MatchValue);
00315 void TIM_SetMatchExt(LPC_TIM_TypeDef *TIMx,TIM_EXTMATCH_OPT ext_match );
00316 void TIM_ConfigCapture(LPC_TIM_TypeDef *TIMx, TIM_CAPTURECFG_Type *TIM_CaptureConfigStruct);
00317 void TIM_Cmd(LPC_TIM_TypeDef *TIMx, FunctionalState NewState);
00318 
00319 uint32_t TIM_GetCaptureValue(LPC_TIM_TypeDef *TIMx, TIM_COUNTER_INPUT_OPT CaptureChannel);
00320 void TIM_ResetCounter(LPC_TIM_TypeDef *TIMx);
00321 
00322 /**
00323  * @}
00324  */
00325 #ifdef __cplusplus
00326 }
00327 #endif
00328 
00329 #endif /* __LPC17XX_TIMER_H_ */
00330 
00331 /**
00332  * @}
00333  */
00334 
00335 /* --------------------------------- End Of File ------------------------------ */