Contiki-NG
contiki-watchdog.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2014, 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 cc26xx-clocks
32  * @{
33  *
34  * \defgroup cc26xx-wdt CC13xx/CC26xx watchdog timer driver
35  *
36  * Driver for the CC13xx/CC26xx Watchdog Timer
37  *
38  * This file is not called watchdog.c because the filename is in use by
39  * TI CC26xxware/CC13xxware
40  * @{
41  *
42  * \file
43  * Implementation of the CC13xx/CC26xx watchdog driver.
44  */
45 #include "contiki.h"
46 #include "dev/watchdog.h"
47 #include "ti-lib.h"
48 
49 #include <stdbool.h>
50 #include <stdint.h>
51 /*---------------------------------------------------------------------------*/
52 #ifdef CONTIKI_WATCHDOG_CONF_TIMER_TOP
53 #define CONTIKI_WATCHDOG_TIMER_TOP CONTIKI_WATCHDOG_CONF_TIMER_TOP
54 #else
55 #define CONTIKI_WATCHDOG_TIMER_TOP 0xFFFFF
56 #endif
57 
58 #ifdef CONTIKI_WATCHDOG_CONF_LOCK_CONFIG
59 #define CONTIKI_WATCHDOG_LOCK_CONFIG CONTIKI_WATCHDOG_CONF_LOCK_CONFIG
60 #else
61 #define CONTIKI_WATCHDOG_LOCK_CONFIG 1
62 #endif
63 
64 #define LOCK_INTERRUPTS_DISABLED 0x01
65 #define LOCK_REGISTERS_UNLOCKED 0x02
66 /*---------------------------------------------------------------------------*/
67 static uint32_t
68 unlock_config(void)
69 {
70  uint32_t ret = 0;
71  bool int_status;
72 
73  if(CONTIKI_WATCHDOG_LOCK_CONFIG) {
74  int_status = ti_lib_int_master_disable();
75 
76  if(ti_lib_watchdog_lock_state()) {
77  ret |= LOCK_REGISTERS_UNLOCKED;
78  ti_lib_watchdog_unlock();
79  }
80 
81  ret |= (int_status) ? (0) : (LOCK_INTERRUPTS_DISABLED);
82  }
83 
84  return ret;
85 }
86 /*---------------------------------------------------------------------------*/
87 static void
88 lock_config(uint32_t status)
89 {
90  if(CONTIKI_WATCHDOG_LOCK_CONFIG) {
91 
92  if(status & LOCK_REGISTERS_UNLOCKED) {
93  ti_lib_watchdog_lock();
94  }
95  if(status & LOCK_INTERRUPTS_DISABLED) {
96  ti_lib_int_master_enable();
97  }
98  }
99 }
100 /*---------------------------------------------------------------------------*/
101 /**
102  * \brief Initialises the CC26xx WDT
103  *
104  * Simply sets the reload counter to a default value. The WDT is not started
105  * yet. To start it, watchdog_start() must be called.
106  */
107 void
109 {
110  ti_lib_watchdog_reload_set(CONTIKI_WATCHDOG_TIMER_TOP);
111  lock_config(LOCK_REGISTERS_UNLOCKED);
112 }
113 /*---------------------------------------------------------------------------*/
114 /**
115  * \brief Starts the CC26xx WDT
116  */
117 void
119 {
120  uint32_t lock_status = unlock_config();
121 
123  ti_lib_watchdog_reset_enable();
124 
125  lock_config(lock_status);
126 }
127 /*---------------------------------------------------------------------------*/
128 /**
129  * \brief Refreshes the CC26xx WDT
130  */
131 void
133 {
134  ti_lib_watchdog_reload_set(CONTIKI_WATCHDOG_TIMER_TOP);
135  ti_lib_watchdog_int_clear();
136 }
137 /*---------------------------------------------------------------------------*/
138 /**
139  * \brief Stops the WDT such that it won't timeout and cause MCU reset
140  */
141 void
143 {
144  uint32_t lock_status = unlock_config();
145 
146  ti_lib_watchdog_reset_disable();
147 
148  lock_config(lock_status);
149 }
150 /*---------------------------------------------------------------------------*/
151 /**
152  * \brief Manually trigger a WDT reboot
153  */
154 void
156 {
157  watchdog_start();
158  while(1);
159 }
160 /*---------------------------------------------------------------------------*/
161 /**
162  * @}
163  * @}
164  */
Header file with macros which rename TI CC26xxware functions.
void watchdog_init(void)
Initialises the CC26xx WDT.
static uint8_t int_status(void)
Check whether a data or wake on motion interrupt has occurred.
void watchdog_start(void)
Starts the CC26xx WDT.
void watchdog_periodic(void)
Refreshes the CC26xx WDT.
void watchdog_reboot(void)
Manually trigger a WDT reboot.
void watchdog_stop(void)
Stops the WDT such that it won&#39;t timeout and cause MCU reset.