summaryrefslogtreecommitdiffstats
path: root/bsps/arm/lpc176x/start/watchdog.c
blob: 4b8b23d9f0f8ee40c4ab6426ef512d93d174fa96 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/**
 * @file
 *
 * @ingroup lpc176x
 *
 * @brief Watchdog controller for the mbed lpc176x family boards.
 */

/*
 * Copyright (c) 2014 Taller Technologies.
 *
 * @author  Boretto Martin    (martin.boretto@tallertechnologies.com)
 * @author  Diaz Marcos (marcos.diaz@tallertechnologies.com)
 * @author  Lenarduzzi Federico  (federico.lenarduzzi@tallertechnologies.com)
 * @author  Daniel Chicco  (daniel.chicco@tallertechnologies.com)
 *
 * The license and distribution terms for this file may be
 * found in the file LICENSE in this distribution or at
 * http://www.rtems.org/license/LICENSE.
 */

#include <assert.h>
#include <rtems/status-checks.h>
#include <bsp.h>
#include <bsp/irq.h>
#include <bsp/watchdog.h>
#include <bsp/io.h>

inline bool lpc176x_been_reset_by_watchdog( void )
{
  return ( ( LPC176X_WDMOD & LPC176X_WWDT_MOD_WDTOF ) ==
           LPC176X_WWDT_MOD_WDTOF );
}

inline void lpc176x_watchdog_reset( void )
{
  LPC176X_WDFEED = LPC176X_WDFEED_CON;
  LPC176X_WDFEED = LPC176X_WDFEED_CFG;
}

/**
 * @brief Enables the watchdog module, sets wd clock and wd timer.
 *
 * @param  tcount Timer's out value.
 * @return RTEMS_SUCCESSFUL if the configuration was done successfully.
 */
static inline rtems_status_code enable_module_and_set_clocksel(
  const lpc176x_microseconds tcount )
{
  rtems_status_code status_code;

  /* Sets clock. */
  LPC176X_WDCLKSEL = LPC176X_WWDT_CLKSEL_WDSEL_PCLK;

  /* Enables the watchdog module.  */
  status_code = lpc176x_module_enable( LPC176X_MODULE_WD,
    LPC176X_MODULE_PCLK_DEFAULT );
  RTEMS_CHECK_SC( status_code, "Enabling the watchdog module." );

  /* Set the watchdog timer constant value. */
  LPC176X_WDTC = ( LPC176X_CCLK / LPC176X_WD_PRESCALER_DIVISOR ) * tcount;

  return status_code;
}

rtems_status_code lpc176x_watchdog_config( const lpc176x_microseconds tcount )
{
  rtems_status_code status_code = enable_module_and_set_clocksel( tcount );

  /* Setup the Watchdog timer operating mode in WDMOD register. */
  LPC176X_WDMOD = LPC176X_WWDT_MOD_WDEN | LPC176X_WWDT_MOD_WDRESET;

  /* Enable the Watchdog by writing 0xAA followed by 0x55 to the
      WDFEED register. */
  lpc176x_watchdog_reset();

  return status_code;
}

rtems_status_code lpc176x_watchdog_config_with_interrupt(
  const lpc176x_wd_isr_funct interrupt,
  const lpc176x_microseconds tcount
)
{
  rtems_status_code status_code = enable_module_and_set_clocksel( tcount );

  /* Setup the Watchdog timer operating mode in WDMOD register. */
  LPC176X_WDMOD = LPC176X_WWDT_MOD_WDEN | LPC176X_WWDT_MOD_WDINT;

  status_code = rtems_interrupt_handler_install(
    LPC176X_WD_INTERRUPT_VECTOR_NUMBER,
    "watchdog_interrupt",
    RTEMS_INTERRUPT_UNIQUE,
    interrupt,
    NULL );

  /* Enable the Watchdog by writing 0xAA followed by 0x55 to the
      WDFEED register. */
  lpc176x_watchdog_reset();

  return status_code;
}