lpc-field

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

lpc17xx_systick.c (6153B)


      1 /**********************************************************************
      2 * $Id$		lpc17xx_systick.c				2010-05-21
      3 *//**
      4 * @file		lpc17xx_systick.c
      5 * @brief	Contains all functions support for SYSTICK firmware library
      6 * 			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 /** @addtogroup SYSTICK
     35  * @{
     36  */
     37 
     38 /* Includes ------------------------------------------------------------------- */
     39 #include "lpc17xx_systick.h"
     40 #include "lpc17xx_clkpwr.h"
     41 
     42 /* If this source file built with example, the LPC17xx FW library configuration
     43  * file in each example directory ("lpc17xx_libcfg.h") must be included,
     44  * otherwise the default FW library configuration file must be included instead
     45  */
     46 #ifdef __BUILD_WITH_EXAMPLE__
     47 #include "lpc17xx_libcfg.h"
     48 #else
     49 #include "lpc17xx_libcfg_default.h"
     50 #endif /* __BUILD_WITH_EXAMPLE__ */
     51 
     52 
     53 #ifdef _SYSTICK
     54 
     55 /* Public Functions ----------------------------------------------------------- */
     56 /** @addtogroup SYSTICK_Public_Functions
     57  * @{
     58  */
     59 /*********************************************************************//**
     60  * @brief 		Initial System Tick with using internal CPU clock source
     61  * @param[in]	time	time interval(ms)
     62  * @return 		None
     63  **********************************************************************/
     64 void SYSTICK_InternalInit(uint32_t time)
     65 {
     66 	uint32_t cclk;
     67 	float maxtime;
     68 
     69 	cclk = SystemCoreClock;
     70 	/* With internal CPU clock frequency for LPC17xx is 'SystemCoreClock'
     71 	 * And limit 24 bit for RELOAD value
     72 	 * So the maximum time can be set:
     73 	 * 1/SystemCoreClock * (2^24) * 1000 (ms)
     74 	 */
     75 	//check time value is available or not
     76 	maxtime = (1<<24)/(SystemCoreClock / 1000) ;
     77 	if(time > maxtime)
     78 		//Error loop
     79 		while(1);
     80 	else
     81 	{
     82 		//Select CPU clock is System Tick clock source
     83 		SysTick->CTRL |= ST_CTRL_CLKSOURCE;
     84 		/* Set RELOAD value
     85 		 * RELOAD = (SystemCoreClock/1000) * time - 1
     86 		 * with time base is millisecond
     87 		 */
     88 		SysTick->LOAD = (cclk/1000)*time - 1;
     89 	}
     90 }
     91 
     92 /*********************************************************************//**
     93  * @brief 		Initial System Tick with using external clock source
     94  * @param[in]	freq	external clock frequency(Hz)
     95  * @param[in]	time	time interval(ms)
     96  * @return 		None
     97  **********************************************************************/
     98 void SYSTICK_ExternalInit(uint32_t freq, uint32_t time)
     99 {
    100 	float maxtime;
    101 
    102 	/* With external clock frequency for LPC17xx is 'freq'
    103 	 * And limit 24 bit for RELOAD value
    104 	 * So the maximum time can be set:
    105 	 * 1/freq * (2^24) * 1000 (ms)
    106 	 */
    107 	//check time value is available or not
    108 	maxtime = (1<<24)/(freq / 1000) ;
    109 	if (time>maxtime)
    110 		//Error Loop
    111 		while(1);
    112 	else
    113 	{
    114 		//Select external clock is System Tick clock source
    115 		SysTick->CTRL &= ~ ST_CTRL_CLKSOURCE;
    116 		/* Set RELOAD value
    117 		 * RELOAD = (freq/1000) * time - 1
    118 		 * with time base is millisecond
    119 		 */
    120 		maxtime = (freq/1000)*time - 1;
    121 		SysTick->LOAD = (freq/1000)*time - 1;
    122 	}
    123 }
    124 
    125 /*********************************************************************//**
    126  * @brief 		Enable/disable System Tick counter
    127  * @param[in]	NewState	System Tick counter status, should be:
    128  * 					- ENABLE
    129  * 					- DISABLE
    130  * @return 		None
    131  **********************************************************************/
    132 void SYSTICK_Cmd(FunctionalState NewState)
    133 {
    134 	CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
    135 
    136 	if(NewState == ENABLE)
    137 		//Enable System Tick counter
    138 		SysTick->CTRL |= ST_CTRL_ENABLE;
    139 	else
    140 		//Disable System Tick counter
    141 		SysTick->CTRL &= ~ST_CTRL_ENABLE;
    142 }
    143 
    144 /*********************************************************************//**
    145  * @brief 		Enable/disable System Tick interrupt
    146  * @param[in]	NewState	System Tick interrupt status, should be:
    147  * 					- ENABLE
    148  * 					- DISABLE
    149  * @return 		None
    150  **********************************************************************/
    151 void SYSTICK_IntCmd(FunctionalState NewState)
    152 {
    153 	CHECK_PARAM(PARAM_FUNCTIONALSTATE(NewState));
    154 
    155 	if(NewState == ENABLE)
    156 		//Enable System Tick counter
    157 		SysTick->CTRL |= ST_CTRL_TICKINT;
    158 	else
    159 		//Disable System Tick counter
    160 		SysTick->CTRL &= ~ST_CTRL_TICKINT;
    161 }
    162 
    163 /*********************************************************************//**
    164  * @brief 		Get current value of System Tick counter
    165  * @param[in]	None
    166  * @return 		current value of System Tick counter
    167  **********************************************************************/
    168 uint32_t SYSTICK_GetCurrentValue(void)
    169 {
    170 	return (SysTick->VAL);
    171 }
    172 
    173 /*********************************************************************//**
    174  * @brief 		Clear Counter flag
    175  * @param[in]	None
    176  * @return 		None
    177  **********************************************************************/
    178 void SYSTICK_ClearCounterFlag(void)
    179 {
    180 	SysTick->CTRL &= ~ST_CTRL_COUNTFLAG;
    181 }
    182 /**
    183  * @}
    184  */
    185 
    186 #endif /* _SYSTICK */
    187 
    188 /**
    189  * @}
    190  */
    191 
    192 /* --------------------------------- End Of File ------------------------------ */
    193