NXP's driver library for LPC17xx, ported to mbed's online compiler. Not tested! I had to fix a lot of warings and found a couple of pretty obvious bugs, so the chances are there are more. Original: http://ics.nxp.com/support/documents/microcontrollers/zip/lpc17xx.cmsis.driver.library.zip

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers lpc17xx_wdt.c Source File

lpc17xx_wdt.c

Go to the documentation of this file.
00001 /**
00002  * @file    : lpc17xx_wdt.c
00003  * @brief    : Contains all functions support for WDT firmware library on LPC17xx
00004  * @version    : 1.0
00005  * @date    : 9. April. 2009
00006  * @author    : HieuNguyen
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 /** @addtogroup WDT
00022  * @{
00023  */
00024 
00025 /* Includes ------------------------------------------------------------------- */
00026 #include "lpc17xx_wdt.h"
00027 #include "lpc17xx_clkpwr.h"
00028 #include "lpc17xx_pinsel.h"
00029 
00030 
00031 /* If this source file built with example, the LPC17xx FW library configuration
00032  * file in each example directory ("lpc17xx_libcfg.h") must be included,
00033  * otherwise the default FW library configuration file must be included instead
00034  */
00035 #ifdef __BUILD_WITH_EXAMPLE__
00036 #include "lpc17xx_libcfg.h"
00037 #else
00038 #include "lpc17xx_libcfg_default.h"
00039 #endif /* __BUILD_WITH_EXAMPLE__ */
00040 
00041 
00042 #ifdef _WDT
00043 
00044 /* Private Functions ---------------------------------------------------------- */
00045 /** @defgroup WDT_Private_Functions
00046  * @{
00047  */
00048 
00049 /********************************************************************//**
00050  * @brief         Set WDT time out value and WDT mode
00051  * @param[in]    clk_source select Clock source for WDT device
00052  * @param[in]    timeout value of time-out for WDT (us)
00053  * @return        None
00054  *********************************************************************/
00055 uint8_t WDT_SetTimeOut (uint8_t clk_source, uint32_t timeout)
00056 {
00057 
00058     uint32_t pclk_wdt = 0;
00059     uint32_t tempval = 0;
00060 
00061     switch ((WDT_CLK_OPT) clk_source)
00062     {
00063     case WDT_CLKSRC_IRC :
00064         pclk_wdt = 4000000;
00065         // Calculate TC in WDT
00066         tempval  = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
00067         // Check if it valid
00068         if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
00069         {
00070             LPC_WDT->WDTC = tempval;
00071             return    SUCCESS;
00072         }
00073 
00074         break;
00075 
00076     case WDT_CLKSRC_PCLK :
00077 
00078         // Get WDT clock with CCLK divider = 4
00079         pclk_wdt = SystemCoreClock / 4;
00080         // Calculate TC in WDT
00081         tempval  = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
00082 
00083         if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
00084         {
00085             CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_4);
00086             LPC_WDT->WDTC = (uint32_t) tempval;
00087             return SUCCESS;
00088         }
00089 
00090         // Get WDT clock with CCLK divider = 2
00091         pclk_wdt = SystemCoreClock / 2;
00092         // Calculate TC in WDT
00093         tempval  = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
00094 
00095         if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
00096         {
00097             CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_2);
00098             LPC_WDT->WDTC = (uint32_t) tempval;
00099             return    SUCCESS;
00100         }
00101 
00102         // Get WDT clock with CCLK divider = 1
00103         pclk_wdt = SystemCoreClock;
00104         // Calculate TC in WDT
00105         tempval  = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
00106 
00107         if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
00108         {
00109             CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_1);
00110             LPC_WDT->WDTC = (uint32_t) tempval;
00111             return    SUCCESS;
00112         }
00113         break ;
00114 
00115 
00116     case WDT_CLKSRC_RTC :
00117         pclk_wdt = 32768;
00118         // Calculate TC in WDT
00119         tempval  = (((pclk_wdt) / WDT_US_INDEX) * (timeout / 4));
00120         // Check if it valid
00121         if ((tempval >= WDT_TIMEOUT_MIN) && (tempval <= WDT_TIMEOUT_MAX))
00122         {
00123             LPC_WDT->WDTC = (uint32_t) tempval;
00124             return    SUCCESS;
00125         }
00126 
00127         break;
00128 
00129 // Error parameter
00130         default:
00131             break;
00132 }
00133 
00134     return ERROR;
00135 }
00136 
00137 /**
00138  * @}
00139  */
00140 
00141 
00142 /* Public Functions ----------------------------------------------------------- */
00143 /** @addtogroup WDT_Public_Functions
00144  * @{
00145  */
00146 
00147 
00148 /*********************************************************************//**
00149 * @brief         Initial for Watchdog function
00150 *                     Clock source = RTC ,
00151 *
00152 * @param[in]    ClkSrc  Select clock source
00153 *                 -    0:Clock source from Internal RC oscillator
00154 *                 -    1: Selects the APB peripheral clock (PCLK)
00155 *                 -    2:Selects the RTC oscillator
00156 *
00157 * @param[in]    WDTMode WDT mode
00158 *                 -    0: Use WDT to generate interrupt only
00159 *                 -    1:WDT_MODE_RESET
00160 * @return         None
00161  **********************************************************************/
00162 void WDT_Init (uint32_t ClkSrc, uint32_t WDTMode)
00163 {
00164     CHECK_PARAM(PARAM_WDT_CLK_OPT(ClkSrc));
00165     CHECK_PARAM(PARAM_WDT_MODE_OPT(WDTMode));
00166     CLKPWR_SetPCLKDiv (CLKPWR_PCLKSEL_WDT, CLKPWR_PCLKSEL_CCLK_DIV_4);
00167 
00168     //Set clock source
00169     LPC_WDT->WDCLKSEL &= ~WDT_WDCLKSEL_MASK;
00170     LPC_WDT->WDCLKSEL |= ClkSrc;
00171     //Set WDT mode
00172     if (WDTMode == WDT_MODE_RESET ){
00173         LPC_WDT->WDMOD |= WDT_WDMOD_WDRESET;
00174     }
00175 }
00176 
00177 /*********************************************************************//**
00178 * @brief         Start WDT activity with given timeout value
00179 * @param[in]    TimeOut WDT reset after timeout if it is not feed
00180 * @return         None
00181  **********************************************************************/
00182 void WDT_Start(uint32_t TimeOut)
00183 {
00184     uint32_t ClkSrc;
00185 
00186     ClkSrc = LPC_WDT->WDCLKSEL;
00187     ClkSrc &=WDT_WDCLKSEL_MASK;
00188     WDT_SetTimeOut(ClkSrc,TimeOut);
00189     //enable watchdog
00190     LPC_WDT->WDMOD |= WDT_WDMOD_WDEN;
00191     WDT_Feed();
00192 }
00193 
00194 /********************************************************************//**
00195  * @brief         Read WDT Time out flag
00196  * @param[in]    None
00197  * @return        Time out flag status of WDT
00198  *********************************************************************/
00199 FlagStatus WDT_ReadTimeOutFlag (void)
00200 {
00201     return ((LPC_WDT->WDMOD & WDT_WDMOD_WDTOF) != 0) ? SET : RESET;
00202 }
00203 
00204 /********************************************************************//**
00205  * @brief         Clear WDT Time out flag
00206  * @param[in]    None
00207  * @return        None
00208  *********************************************************************/
00209 void WDT_ClrTimeOutFlag (void)
00210 {
00211     LPC_WDT->WDMOD &=~WDT_WDMOD_WDTOF;
00212 }
00213 
00214 /********************************************************************//**
00215  * @brief         Update WDT timeout value and feed
00216  * @param[in]    TimeOut    TimeOut value to be updated
00217  * @return        None
00218  *********************************************************************/
00219 void WDT_UpdateTimeOut ( uint32_t TimeOut)
00220 {
00221     uint32_t ClkSrc;
00222     ClkSrc = LPC_WDT->WDCLKSEL;
00223     ClkSrc &=WDT_WDCLKSEL_MASK;
00224     WDT_SetTimeOut(ClkSrc,TimeOut);
00225     WDT_Feed();
00226 }
00227 
00228 
00229 /********************************************************************//**
00230  * @brief         After set WDTEN, call this function to start Watchdog
00231  *                 or reload the Watchdog timer
00232  * @param[in]    None
00233  *
00234  * @return        None
00235  *********************************************************************/
00236 void WDT_Feed (void)
00237 {
00238     // Disable irq interrupt
00239     __disable_irq();
00240     LPC_WDT->WDFEED = 0xAA;
00241     LPC_WDT->WDFEED = 0x55;
00242     // Then enable irq interrupt
00243     __enable_irq();
00244 }
00245 
00246 /********************************************************************//**
00247  * @brief         Get the current value of WDT
00248  * @param[in]    None
00249  * @return        current value of WDT
00250  *********************************************************************/
00251 uint32_t WDT_GetCurrentCount(void)
00252 {
00253     return LPC_WDT->WDTV;
00254 }
00255 
00256 /**
00257  * @}
00258  */
00259 
00260 #endif /* _WDT */
00261 
00262 /**
00263  * @}
00264  */
00265 
00266 /* --------------------------------- End Of File ------------------------------ */