1 /**
2  * \file
3  *
4  * \brief SAM Watchdog Driver
5  *
6  * Copyright (C) 2013-2015 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 #include "wdt.h"
47 #include <system.h>
48 #include <system_interrupt.h>
49 
50 wdt_callback_t wdt_early_warning_callback;
51 
52 /**
53  * \brief Registers an asynchronous callback function with the driver.
54  *
55  * Registers an asynchronous callback with the WDT driver, fired when a
56  * given criteria (such as an Early Warning) is met. Callbacks are fired once
57  * for each event.
58  *
59  * \param[in] callback  Pointer to the callback function to register
60  * \param[in] type      Type of callback function to register
61  *
62  * \return Status of the registration operation.
63  * \retval STATUS_OK               The callback was registered successfully
64  * \retval STATUS_ERR_INVALID_ARG  If an invalid callback type was supplied
65  */
wdt_register_callback(const wdt_callback_t callback,const enum wdt_callback type)66 enum status_code wdt_register_callback(
67 		const wdt_callback_t callback,
68 		const enum wdt_callback type)
69 {
70 	/* Sanity check arguments */
71 	Assert(callback);
72 
73 	switch (type)
74 	{
75 	case WDT_CALLBACK_EARLY_WARNING:
76 		wdt_early_warning_callback = callback;
77 		return STATUS_OK;
78 	default:
79 		Assert(false);
80 		return STATUS_ERR_INVALID_ARG;
81 	}
82 }
83 
84 /**
85  * \brief Unregisters an asynchronous callback function with the driver.
86  *
87  * Unregisters an asynchronous callback with the WDT driver, removing it
88  * from the internal callback registration table.
89  *
90  * \param[in] type  Type of callback function to unregister
91  *
92  * \return Status of the de-registration operation.
93  * \retval STATUS_OK               The callback was Unregistered successfully
94  * \retval STATUS_ERR_INVALID_ARG  If an invalid callback type was supplied
95  */
wdt_unregister_callback(const enum wdt_callback type)96 enum status_code wdt_unregister_callback(
97 		const enum wdt_callback type)
98 {
99 	switch (type)
100 	{
101 	case WDT_CALLBACK_EARLY_WARNING:
102 		wdt_early_warning_callback = NULL;
103 		return STATUS_OK;
104 	default:
105 		Assert(false);
106 		return STATUS_ERR_INVALID_ARG;
107 	}
108 }
109 
110 /**
111  * \brief Enables asynchronous callback generation for a given type.
112  *
113  * Enables asynchronous callbacks for a given callback type. This must be
114  * called before an external interrupt channel will generate callback events.
115  *
116  * \param[in] type  Type of callback function to enable
117  *
118  * \return Status of the callback enable operation.
119  * \retval STATUS_OK               The callback was enabled successfully
120  * \retval STATUS_ERR_INVALID_ARG  If an invalid callback type was supplied
121  */
wdt_enable_callback(const enum wdt_callback type)122 enum status_code wdt_enable_callback(
123 		const enum wdt_callback type)
124 {
125 	Wdt *const WDT_module = WDT;
126 
127 	switch (type)
128 	{
129 	case WDT_CALLBACK_EARLY_WARNING:
130 		WDT_module->INTENSET.reg = WDT_INTENSET_EW;
131 		system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_WDT);
132 		return STATUS_OK;
133 	default:
134 		Assert(false);
135 		return STATUS_ERR_INVALID_ARG;
136 	}
137 }
138 
139 /**
140  * \brief Disables asynchronous callback generation for a given type.
141  *
142  * Disables asynchronous callbacks for a given callback type.
143  *
144  * \param[in] type  Type of callback function to disable
145  *
146  * \return Status of the callback disable operation.
147  * \retval STATUS_OK               The callback was disabled successfully
148  * \retval STATUS_ERR_INVALID_ARG  If an invalid callback type was supplied
149  */
wdt_disable_callback(const enum wdt_callback type)150 enum status_code wdt_disable_callback(
151 		const enum wdt_callback type)
152 {
153 	Wdt *const WDT_module = WDT;
154 
155 	switch (type)
156 	{
157 	case WDT_CALLBACK_EARLY_WARNING:
158 		WDT_module->INTENCLR.reg = WDT_INTENCLR_EW;
159 		return STATUS_OK;
160 	default:
161 		Assert(false);
162 		return STATUS_ERR_INVALID_ARG;
163 	}
164 }
165 
166 /** Handler for the WDT hardware module interrupt. */
WDT_Handler(void)167 void WDT_Handler(void)
168 {
169 	wdt_clear_early_warning();
170 
171 	if (wdt_early_warning_callback) {
172 		wdt_early_warning_callback();
173 	}
174 }
175