Contiki-NG
watchdog-arch.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holder nor the names of its
14  * contributors may be used to endorse or promote products derived
15  * from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21  * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28  * OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30  /**
31  * \addtogroup cc13xx-cc26xx-watchdog
32  * @{
33  *
34  * \file
35  * Implementation of the CC13xx/CC26xx watchdog driver.
36  */
37 /*---------------------------------------------------------------------------*/
38 #include "contiki.h"
39 #include "dev/watchdog.h"
40 /*---------------------------------------------------------------------------*/
41 #include <Board.h>
42 
43 #include <ti/drivers/Watchdog.h>
44 #include <ti/drivers/dpl/ClockP.h>
45 
46 #include <ti/devices/DeviceFamily.h>
47 #include DeviceFamily_constructPath(driverlib/watchdog.h)
48 /*---------------------------------------------------------------------------*/
49 #include "watchdog-arch.h"
50 /*---------------------------------------------------------------------------*/
51 #include <stdbool.h>
52 #include <stdint.h>
53 /*---------------------------------------------------------------------------*/
54 #define WATCHDOG_DIV_RATIO 32 /* Watchdog division ratio */
55 #define WATCHDOG_TIMEOUT_MARGIN 1500 /* 1ms margin in Watchdog ticks */
56 /*---------------------------------------------------------------------------*/
57 #if (WATCHDOG_DISABLE == 0)
58 static Watchdog_Handle wdt_handle;
59 #endif
60 /*---------------------------------------------------------------------------*/
61 /**
62  * \brief Initialises the Watchdog module.
63  *
64  * Simply sets the reload counter to a default value. The WDT is not
65  * started yet. To start it, watchdog_start() must be called.
66  */
67 void
69 {
70 #if (WATCHDOG_DISABLE == 0)
71  Watchdog_init();
72 
73  Watchdog_Params wdt_params;
74  Watchdog_Params_init(&wdt_params);
75 
76  wdt_params.resetMode = Watchdog_RESET_ON;
77  wdt_params.debugStallMode = Watchdog_DEBUG_STALL_ON;
78 
79  wdt_handle = Watchdog_open(Board_WATCHDOG0, &wdt_params);
80 #endif
81 }
82 /*---------------------------------------------------------------------------*/
83 uint32_t
85 {
86  if(!WatchdogRunning()) {
87  return 0;
88  }
89 
90  ClockP_FreqHz freq;
91  ClockP_getCpuFreq(&freq);
92  uint64_t value = (uint64_t)WatchdogValueGet();
93  uint32_t timeout = (uint32_t)((value * 1000 * 1000 * WATCHDOG_DIV_RATIO) / freq.lo);
94 
95  /*
96  * A margin should be applied to the timeout to ensure there is enough time
97  * to enter low-power mode, wakeup, and clear the Watchdog timer before it
98  * times out. If the timeout is equals to or less than the margin, simply
99  * return the lowest possible timeout.
100  */
101  if(timeout <= WATCHDOG_TIMEOUT_MARGIN) {
102  return 1;
103  } else {
104  return timeout - WATCHDOG_TIMEOUT_MARGIN;
105  }
106 }
107 /*---------------------------------------------------------------------------*/
108 /**
109  * \brief Start the Watchdog.
110  */
111 void
113 {
114 #if (WATCHDOG_DISABLE == 0)
116 #endif
117 }
118 /*---------------------------------------------------------------------------*/
119 /**
120  * \brief Refresh (feed) the Watchdog.
121  */
122 void
124 {
125 #if (WATCHDOG_DISABLE == 0)
126  uint32_t timeout_ticks = Watchdog_convertMsToTicks(wdt_handle, WATCHDOG_TIMEOUT_MS);
127  Watchdog_setReload(wdt_handle, timeout_ticks);
128 #endif
129 }
130 /*---------------------------------------------------------------------------*/
131 /**
132  * \brief Stop the Watchdog such that it won't timeout and cause a
133  * system reset.
134  */
135 void
137 {
138 #if (WATCHDOG_DISABLE == 0)
139  Watchdog_clear(wdt_handle);
140 #endif
141 }
142 /*---------------------------------------------------------------------------*/
143 /**
144  * \brief Manually trigger a Watchdog timeout.
145  */
146 void
148 {
149 #if (WATCHDOG_DISABLE == 0)
150  watchdog_start();
151 
152  /* Busy loop until watchdog times out */
153  for (;;) { /* hang */ }
154 #endif
155 }
156 /*---------------------------------------------------------------------------*/
157 /**
158  * @}
159  */
void watchdog_reboot(void)
Manually trigger a Watchdog timeout.
Header file of the CC13xx/CC26xx watchdog driver.
void watchdog_init(void)
Initialises the Watchdog module.
Definition: watchdog-arch.c:68
void watchdog_periodic(void)
Refresh (feed) the Watchdog.
uint32_t watchdog_arch_next_timeout(void)
Return the next expiration time for the Watchdog.
Definition: watchdog-arch.c:84
void watchdog_stop(void)
Stop the Watchdog such that it won&#39;t timeout and cause a system reset.
void watchdog_start(void)
Start the Watchdog.