Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:23:03

0001 /**
0002  * @file
0003  *
0004  * @ingroup RTEMSBSPsARMLPC176X
0005  *
0006  * @brief Watchdog controller for the mbed lpc176x family boards.
0007  */
0008 
0009 /*
0010  * Copyright (c) 2014 Taller Technologies.
0011  *
0012  * @author  Boretto Martin    (martin.boretto@tallertechnologies.com)
0013  * @author  Diaz Marcos (marcos.diaz@tallertechnologies.com)
0014  * @author  Lenarduzzi Federico  (federico.lenarduzzi@tallertechnologies.com)
0015  * @author  Daniel Chicco  (daniel.chicco@tallertechnologies.com)
0016  *
0017  * The license and distribution terms for this file may be
0018  * found in the file LICENSE in this distribution or at
0019  * http://www.rtems.org/license/LICENSE.
0020  */
0021 
0022 #include <assert.h>
0023 #include <rtems/status-checks.h>
0024 #include <bsp.h>
0025 #include <bsp/irq.h>
0026 #include <bsp/watchdog.h>
0027 #include <bsp/io.h>
0028 
0029 inline bool lpc176x_been_reset_by_watchdog( void )
0030 {
0031   return ( ( LPC176X_WDMOD & LPC176X_WWDT_MOD_WDTOF ) ==
0032            LPC176X_WWDT_MOD_WDTOF );
0033 }
0034 
0035 inline void lpc176x_watchdog_reset( void )
0036 {
0037   LPC176X_WDFEED = LPC176X_WDFEED_CON;
0038   LPC176X_WDFEED = LPC176X_WDFEED_CFG;
0039 }
0040 
0041 /**
0042  * @brief Enables the watchdog module, sets wd clock and wd timer.
0043  *
0044  * @param  tcount Timer's out value.
0045  * @return RTEMS_SUCCESSFUL if the configuration was done successfully.
0046  */
0047 static inline rtems_status_code enable_module_and_set_clocksel(
0048   const lpc176x_microseconds tcount )
0049 {
0050   rtems_status_code status_code;
0051 
0052   /* Sets clock. */
0053   LPC176X_WDCLKSEL = LPC176X_WWDT_CLKSEL_WDSEL_PCLK;
0054 
0055   /* Enables the watchdog module.  */
0056   status_code = lpc176x_module_enable( LPC176X_MODULE_WD,
0057     LPC176X_MODULE_PCLK_DEFAULT );
0058   RTEMS_CHECK_SC( status_code, "Enabling the watchdog module." );
0059 
0060   /* Set the watchdog timer constant value. */
0061   LPC176X_WDTC = ( LPC176X_CCLK / LPC176X_WD_PRESCALER_DIVISOR ) * tcount;
0062 
0063   return status_code;
0064 }
0065 
0066 rtems_status_code lpc176x_watchdog_config( const lpc176x_microseconds tcount )
0067 {
0068   rtems_status_code status_code = enable_module_and_set_clocksel( tcount );
0069 
0070   /* Setup the Watchdog timer operating mode in WDMOD register. */
0071   LPC176X_WDMOD = LPC176X_WWDT_MOD_WDEN | LPC176X_WWDT_MOD_WDRESET;
0072 
0073   /* Enable the Watchdog by writing 0xAA followed by 0x55 to the
0074       WDFEED register. */
0075   lpc176x_watchdog_reset();
0076 
0077   return status_code;
0078 }
0079 
0080 rtems_status_code lpc176x_watchdog_config_with_interrupt(
0081   const lpc176x_wd_isr_funct interrupt,
0082   const lpc176x_microseconds tcount
0083 )
0084 {
0085   rtems_status_code status_code = enable_module_and_set_clocksel( tcount );
0086 
0087   /* Setup the Watchdog timer operating mode in WDMOD register. */
0088   LPC176X_WDMOD = LPC176X_WWDT_MOD_WDEN | LPC176X_WWDT_MOD_WDINT;
0089 
0090   status_code = rtems_interrupt_handler_install(
0091     LPC176X_WD_INTERRUPT_VECTOR_NUMBER,
0092     "watchdog_interrupt",
0093     RTEMS_INTERRUPT_UNIQUE,
0094     interrupt,
0095     NULL );
0096 
0097   /* Enable the Watchdog by writing 0xAA followed by 0x55 to the
0098       WDFEED register. */
0099   lpc176x_watchdog_reset();
0100 
0101   return status_code;
0102 }