lpc-field

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

lpc17xx_wdt.c (8323B)


      1 /**********************************************************************
      2 * $Id$		lpc17xx_wdt.c			2010-05-21
      3 *//**
      4 * @file		lpc17xx_wdt.c
      5 * @brief	Contains all functions support for WDT 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 WDT
     35  * @{
     36  */
     37 
     38 /* Includes ------------------------------------------------------------------- */
     39 #include "lpc17xx_wdt.h"
     40 #include "lpc17xx_clkpwr.h"
     41 #include "lpc17xx_pinsel.h"
     42 
     43 
     44 /* If this source file built with example, the LPC17xx FW library configuration
     45  * file in each example directory ("lpc17xx_libcfg.h") must be included,
     46  * otherwise the default FW library configuration file must be included instead
     47  */
     48 #ifdef __BUILD_WITH_EXAMPLE__
     49 #include "lpc17xx_libcfg.h"
     50 #else
     51 #include "lpc17xx_libcfg_default.h"
     52 #endif /* __BUILD_WITH_EXAMPLE__ */
     53 
     54 
     55 #ifdef _WDT
     56 
     57 /* Private Functions ---------------------------------------------------------- */
     58 
     59 static uint8_t WDT_SetTimeOut (uint8_t clk_source, uint32_t timeout);
     60 
     61 /********************************************************************//**
     62  * @brief 		Set WDT time out value and WDT mode
     63  * @param[in]	clk_source select Clock source for WDT device
     64  * @param[in]	timeout value of time-out for WDT (us)
     65  * @return		None
     66  *********************************************************************/
     67 static uint8_t WDT_SetTimeOut (uint8_t clk_source, uint32_t timeout)
     68 {
     69 
     70 	uint32_t pclk_wdt = 0;
     71 	uint32_t tempval = 0;
     72 
     73 	switch ((WDT_CLK_OPT) clk_source)
     74     {
     75     case WDT_CLKSRC_IRC:
     76     	pclk_wdt = 4000000;
     77     	// Calculate TC in WDT
     78     	tempval  = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
     79     	// Check if it valid
     80     	if (tempval >= WDT_TIMEOUT_MIN)
     81     	{
     82     		LPC_WDT->WDTC = tempval;
     83     		return	SUCCESS;
     84     	}
     85 
     86     	break;
     87 
     88     case WDT_CLKSRC_PCLK:
     89 
     90     	// Get WDT clock with CCLK divider = 4
     91 		pclk_wdt = SystemCoreClock / 4;
     92 		// Calculate TC in WDT
     93 		tempval  = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
     94 
     95 		if (tempval >= WDT_TIMEOUT_MIN)
     96 		{
     97 			CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_4);
     98 			LPC_WDT->WDTC = (uint32_t) tempval;
     99 			return SUCCESS;
    100 		}
    101 
    102 		// Get WDT clock with CCLK divider = 2
    103 		pclk_wdt = SystemCoreClock / 2;
    104 		// Calculate TC in WDT
    105 		tempval  = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
    106 
    107 		if (tempval >= WDT_TIMEOUT_MIN)
    108 		{
    109 			CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_2);
    110 			LPC_WDT->WDTC = (uint32_t) tempval;
    111 			return	SUCCESS;
    112 		}
    113 
    114 		// Get WDT clock with CCLK divider = 1
    115 		pclk_wdt = SystemCoreClock;
    116 		// Calculate TC in WDT
    117 		tempval  = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
    118 
    119 		if (tempval >= WDT_TIMEOUT_MIN)
    120 		{
    121 			CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_1);
    122 			LPC_WDT->WDTC = (uint32_t) tempval;
    123 			return	SUCCESS;
    124 		}
    125 		break ;
    126 
    127 
    128     case WDT_CLKSRC_RTC:
    129 		pclk_wdt = 32768;
    130 		// Calculate TC in WDT
    131 		tempval  = ((((uint64_t)pclk_wdt * (uint64_t)timeout / 4) / (uint64_t)WDT_US_INDEX));
    132 		// Check if it valid
    133 		if (tempval >= WDT_TIMEOUT_MIN)
    134 		{
    135 			LPC_WDT->WDTC = (uint32_t) tempval;
    136 			return	SUCCESS;
    137 		}
    138 
    139 		break;
    140 
    141 // Error parameter
    142 		default:
    143 			break;
    144 }
    145 
    146 	return ERROR;
    147 }
    148 
    149 /* End of Private Functions --------------------------------------------------- */
    150 
    151 
    152 /* Public Functions ----------------------------------------------------------- */
    153 /** @addtogroup WDT_Public_Functions
    154  * @{
    155  */
    156 
    157 
    158 /*********************************************************************//**
    159 * @brief 		Initial for Watchdog function
    160 * 					Clock source = RTC ,
    161 * @param[in]	ClkSrc  Select clock source, should be:
    162 * 				- WDT_CLKSRC_IRC: Clock source from Internal RC oscillator
    163 * 				- WDT_CLKSRC_PCLK: Selects the APB peripheral clock (PCLK)
    164 * 				- WDT_CLKSRC_RTC: Selects the RTC oscillator
    165 * @param[in]	WDTMode WDT mode, should be:
    166 * 				- WDT_MODE_INT_ONLY: Use WDT to generate interrupt only
    167 * 				- WDT_MODE_RESET: Use WDT to generate interrupt and reset MCU
    168 * @return 		None
    169  **********************************************************************/
    170 void WDT_Init (WDT_CLK_OPT ClkSrc, WDT_MODE_OPT WDTMode)
    171 {
    172 	CHECK_PARAM(PARAM_WDT_CLK_OPT(ClkSrc));
    173 	CHECK_PARAM(PARAM_WDT_MODE_OPT(WDTMode));
    174 	CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_4);
    175 
    176 	//Set clock source
    177 	LPC_WDT->WDCLKSEL &= ~WDT_WDCLKSEL_MASK;
    178 	LPC_WDT->WDCLKSEL |= ClkSrc;
    179 	//Set WDT mode
    180 	if (WDTMode == WDT_MODE_RESET){
    181 		LPC_WDT->WDMOD |= WDT_WDMOD(WDTMode);
    182 	}
    183 }
    184 
    185 /*********************************************************************//**
    186 * @brief 		Start WDT activity with given timeout value
    187 * @param[in]	TimeOut WDT reset after timeout if it is not feed
    188 * @return 		None
    189  **********************************************************************/
    190 void WDT_Start(uint32_t TimeOut)
    191 {
    192 	uint32_t ClkSrc;
    193 
    194 	ClkSrc = LPC_WDT->WDCLKSEL;
    195 	ClkSrc &=WDT_WDCLKSEL_MASK;
    196 	WDT_SetTimeOut(ClkSrc,TimeOut);
    197 	//enable watchdog
    198 	LPC_WDT->WDMOD |= WDT_WDMOD_WDEN;
    199 	WDT_Feed();
    200 }
    201 
    202 /********************************************************************//**
    203  * @brief 		Read WDT Time out flag
    204  * @param[in]	None
    205  * @return		Time out flag status of WDT
    206  *********************************************************************/
    207 FlagStatus WDT_ReadTimeOutFlag (void)
    208 {
    209 	return ((FlagStatus)((LPC_WDT->WDMOD & WDT_WDMOD_WDTOF) >>2));
    210 }
    211 
    212 /********************************************************************//**
    213  * @brief 		Clear WDT Time out flag
    214  * @param[in]	None
    215  * @return		None
    216  *********************************************************************/
    217 void WDT_ClrTimeOutFlag (void)
    218 {
    219 	LPC_WDT->WDMOD &=~WDT_WDMOD_WDTOF;
    220 }
    221 
    222 /********************************************************************//**
    223  * @brief 		Update WDT timeout value and feed
    224  * @param[in]	TimeOut	TimeOut value to be updated
    225  * @return		None
    226  *********************************************************************/
    227 void WDT_UpdateTimeOut ( uint32_t TimeOut)
    228 {
    229 	uint32_t ClkSrc;
    230 	ClkSrc = LPC_WDT->WDCLKSEL;
    231 	ClkSrc &=WDT_WDCLKSEL_MASK;
    232 	WDT_SetTimeOut(ClkSrc,TimeOut);
    233 	WDT_Feed();
    234 }
    235 
    236 
    237 /********************************************************************//**
    238  * @brief 		After set WDTEN, call this function to start Watchdog
    239  * 				or reload the Watchdog timer
    240  * @param[in]	None
    241  *
    242  * @return		None
    243  *********************************************************************/
    244 void WDT_Feed (void)
    245 {
    246 	// Disable irq interrupt
    247 	__disable_irq();
    248 	LPC_WDT->WDFEED = 0xAA;
    249 	LPC_WDT->WDFEED = 0x55;
    250 	// Then enable irq interrupt
    251 	__enable_irq();
    252 }
    253 
    254 /********************************************************************//**
    255  * @brief 		Get the current value of WDT
    256  * @param[in]	None
    257  * @return		current value of WDT
    258  *********************************************************************/
    259 uint32_t WDT_GetCurrentCount(void)
    260 {
    261 	return LPC_WDT->WDTV;
    262 }
    263 
    264 /**
    265  * @}
    266  */
    267 
    268 #endif /* _WDT */
    269 
    270 /**
    271  * @}
    272  */
    273 
    274 /* --------------------------------- End Of File ------------------------------ */