1 /**
2  * \file
3  *
4  * \brief SAM Watchdog Driver for SAMB
5  *
6  * Copyright (c) 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 #ifndef WDT_SAM_B_H_INCLUDED
47 #define WDT_SAM_B_H_INCLUDED
48 
49 /**
50  * \defgroup asfdoc_samb_wdt_group SAM Watchdog Driver (WDT)
51  *
52  * This driver for Atmel&reg; | SMART SAM devices provides an interface for the configuration
53  * and management of the device's Watchdog Timer module, including the enabling,
54  * disabling, and kicking within the device.
55  *
56  * The following peripherals are used by this module:
57  *  - WDT (Watchdog Timer)
58  *
59  * The following devices can use this module:
60  *  - Atmel | SMART SAM B11
61  *
62  * The outline of this documentation is as follows:
63  *  - \ref asfdoc_samb_wdt_prerequisites
64  *  - \ref asfdoc_samb_wdt_module_overview
65  *  - \ref asfdoc_samb_wdt_special_considerations
66  *  - \ref asfdoc_samb_wdt_extra_info
67  *  - \ref asfdoc_samb_wdt_examples
68  *  - \ref asfdoc_samb_wdt_api_overview
69  *
70  *
71  * \section asfdoc_samb_wdt_prerequisites Prerequisites
72  *
73  * There are no prerequisites for this module.
74  *
75  *
76  * \section asfdoc_samb_wdt_module_overview Module Overview
77  * The watchdog module is based on a 32-bit down-counter that is initialized
78  * from the Reload Register. The watchdog module generates a regular interrupt,
79  * depending on a programmed value. The counter decrements by one on each
80  * positive clock edge of clock when the clock is enable. The watchdog monitors
81  * the interrupt and asserts a reset request signal when the counter reaches 0,
82  * and the counter is stopped. On the next enabled clock edge, the counter is
83  * reloaded from the WDT load Register and the countdown sequence continues. If
84  * the interrupt is not cleared by the time the counter next reaches 0, the
85  * watchdog module reasserts the reset signal.
86  *
87  * A simplified block diagram of the WDT can be seen in
88  * \ref asfdoc_samb_wdt_module_block_diagram "the figure below".
89  *
90  * \anchor asfdoc_samb_wdt_module_block_diagram
91  * \image html wdt_block_diagram.svg "WDT Block Diagram"
92  *
93  * \section asfdoc_samb_wdt_special_considerations Special Considerations
94  * There are no special considerations for this module.
95  *
96  * \section asfdoc_samb_wdt_extra_info Extra Information
97  *
98  * For extra information, see \ref asfdoc_samb_wdt_extra. This includes:
99  *  - \ref asfdoc_samb_wdt_extra_acronyms
100  *  - \ref asfdoc_samb_wdt_extra_dependencies
101  *  - \ref asfdoc_samb_wdt_extra_errata
102  *  - \ref asfdoc_samb_wdt_extra_history
103  *
104  *
105  * \section asfdoc_samb_wdt_examples Examples
106  *
107  * For a list of examples related to this driver, see
108  * \ref asfdoc_samb_wdt_exqsg.
109  *
110  * \section asfdoc_samb_wdt_api_overview API Overview
111  * @{
112  */
113 
114 #include <compiler.h>
115 #include <system_sam_b.h>
116 
117 #ifdef __cplusplus
118 extern "C" {
119 #endif
120 
121 #define WDT_WRITE_ACCESS_KEY    0x1ACCE551
122 
123 struct wdt_module;
124 /** Type definition for a WDT module callback function. */
125 typedef void (*wdt_callback_t)(void);
126 
127 /** Enum for the possible callback types for the WDT module. */
128 enum wdt_callback
129 {
130 	/**
131 	 * Callback type for when an early warning callback from the WDT module
132 	 * is issued
133 	 */
134 	WDT_CALLBACK_EARLY_WARNING,
135 	/** Number of available callbacks */
136 	WDT_CALLBACK_N,
137 };
138 
139 /**
140  * \brief Watchdog Timer configuration structure.
141  *
142  *  Configuration structure for a Watchdog Timer instance. This
143  *  structure should be initialized by the \ref wdt_get_config_defaults()
144  *  function before being modified by the user application.
145  */
146 struct wdt_config {
147 	/** Watchdog load value */
148 	uint32_t load_value;
149 	/** Enable reset output */
150 	bool enable_reset;
151 	/** Enable write access */
152 	bool write_access;
153 };
154 
155 /**
156  * \brief WDT driver software device instance structure.
157  *
158  * WDT driver software instance structure, used to retain software
159  * state information of an associated hardware module instance.
160  *
161  * \note The fields of this structure should not be altered by the user
162  *       application; they are reserved for module-internal use only.
163  */
164 struct wdt_module {
165 #if !defined(__DOXYGEN__)
166 	/** Pointer to the hardware instance */
167 	Wdt *hw;
168 	/** Array to store callback function pointers in */
169 	wdt_callback_t callback[WDT_CALLBACK_N];
170 	/** Bit mask for callbacks registered */
171 	uint8_t callback_reg_mask;
172 	/** Bit mask for callbacks enabled */
173 	uint8_t callback_enable_mask;
174 #endif
175 };
176 
177 /**
178  * \name Configuration and Initialization
179  * @{
180  */
181 void wdt_get_config_defaults(struct wdt_config *const config);
182 enum status_code wdt_set_config(struct wdt_module *const module, Wdt * const hw, \
183 			const struct wdt_config *const config);
184 /** @} */
185 
186 /**
187  * \name Reset
188  * @{
189  */
190 void wdt_reset(struct wdt_module *const module);
191 /** @} */
192 
193 /**
194  * \name Get and Clear status
195  * @{
196  */
197 uint8_t wdt_get_interrupt_status(struct wdt_module *const module);
198 uint8_t wdt_get_status(struct wdt_module *const module);
199 void wdt_clear_status(struct wdt_module *const module);
200 /** @} */
201 
202 /**
203  * \name Reload and get count value
204  * @{
205  */
206 enum status_code wdt_set_reload_count(struct wdt_module *const module, \
207 			uint32_t load_value);
208 void wdt_get_current_count(struct wdt_module *const module, \
209 			uint32_t * count_value);
210 /** @} */
211 
212 /**
213  * \name Callback
214  * @{
215  */
216 void wdt_register_callback(struct wdt_module *const module,
217 		wdt_callback_t callback_func,
218 		enum wdt_callback callback_type);
219 void wdt_unregister_callback(struct wdt_module *module,
220 		enum wdt_callback callback_type);
221 void wdt_enable_callback(struct wdt_module *module,
222 		enum wdt_callback callback_type);
223 void wdt_disable_callback(struct wdt_module *const module,
224 		enum wdt_callback callback_type);
225 /** @} */
226 
227 /** @} */
228 
229 #ifdef __cplusplus
230 }
231 #endif
232 
233 /** @} */
234 
235 /**
236  * \page asfdoc_samb_wdt_extra Extra Information for WDT Driver
237  *
238  * \section asfdoc_samb_wdt_extra_acronyms Acronyms
239  * The table below presents the acronyms used in this module:
240  *
241  * <table>
242  *	<tr>
243  *		<th>Acronym</th>
244  *		<th>Description</th>
245  *	</tr>
246  *	<tr>
247  *		<td>WDT</td>
248  *		<td>Watchdog Timer</td>
249  *	</tr>
250  * </table>
251  *
252  *
253  * \section asfdoc_samb_wdt_extra_dependencies Dependencies
254  * There are no dependencies related to this driver.
255  *
256  *
257  * \section asfdoc_samb_wdt_extra_errata Errata
258  * There are no errata related to this driver.
259  *
260  *
261  * \section asfdoc_samb_wdt_extra_history Module History
262  * An overview of the module history is presented in the table below, with
263  * details on the enhancements and fixes made to the module since its first
264  * release. The current version of this corresponds to the newest version in
265  * the table.
266  *
267  * <table>
268  *	<tr>
269  *		<th>Changelog</th>
270  *	</tr>
271  *	<tr>
272  *		<td>Initial Release</td>
273  *	</tr>
274  * </table>
275  */
276 
277 /**
278  * \page asfdoc_samb_wdt_exqsg Examples for WDT Driver
279  *
280  * This is a list of the available Quick Start guides (QSGs) and example
281  * applications for \ref asfdoc_samb_wdt_group. QSGs are simple examples with
282  * step-by-step instructions to configure and use this driver in a selection of
283  * use cases. Note that QSGs can be compiled as a standalone application or be
284  * added to the user application.
285  *
286  *  - \subpage asfdoc_samb_wdt_basic_use_case
287  *
288  * \page asfdoc_samb_wdt_document_revision_history Document Revision History
289  *
290  * <table>
291  *	<tr>
292  *		<th>Doc. Rev.</td>
293  *		<th>Date</td>
294  *		<th>Comments</td>
295  *	</tr>
296  *	<tr>
297  *		<td>A</td>
298  *		<td>09/2015</td>
299  *		<td>Initial release</td>
300  *	</tr>
301  * </table>
302  */
303 
304 #endif /* WDT_H_INCLUDED */
305