1 /**
2  *
3  * \file
4  *
5  * \brief SAM Frequency Meter driver.
6  *
7  * This file defines a useful set of functions for the FREQM on SAM devices.
8  *
9  * Copyright (c) 2015 Atmel Corporation. All rights reserved.
10  *
11  * \asf_license_start
12  *
13  * \page License
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions are met:
17  *
18  * 1. Redistributions of source code must retain the above copyright notice,
19  *    this list of conditions and the following disclaimer.
20  *
21  * 2. Redistributions in binary form must reproduce the above copyright notice,
22  *    this list of conditions and the following disclaimer in the documentation
23  *    and/or other materials provided with the distribution.
24  *
25  * 3. The name of Atmel may not be used to endorse or promote products derived
26  *    from this software without specific prior written permission.
27  *
28  * 4. This software may only be redistributed and used in connection with an
29  *    Atmel microcontroller product.
30  *
31  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
32  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
33  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
34  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
35  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
40  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGE.
42  *
43  * \asf_license_stop
44  *
45  */
46 /*
47  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
48  */
49 
50 #ifndef FREQM_CALLBACK_H_INCLUDED
51 #define FREQM_CALLBACK_H_INCLUDED
52 
53 #ifdef __cplusplus
54 extern "C" {
55 #endif
56 
57 #include <compiler.h>
58 #include <system_interrupt.h>
59 /**
60  * \addtogroup asfdoc_sam0_freqm_group
61  *
62  * @{
63  */
64 
65 /** FREQM callback type. */
66 enum freqm_callback_type {
67 	/** Measurement done callback.*/
68 	FREQM_CALLBACK_MEASURE_DONE = 0,
69 };
70 
71 /** \name Callback Configuration and Initialization
72  * @{
73  */
74 
75 enum status_code freqm_register_callback(
76 		struct freqm_module *const module,
77 		freqm_callback_t callback_func,
78 		enum freqm_callback callback_type);
79 
80 enum status_code freqm_unregister_callback(
81 		struct freqm_module *module,
82 		enum freqm_callback callback_type);
83 
84 /** @} */
85 
86 
87 /** \name Callback Enabling and Disabling
88  * @{
89  */
90 
91 /**
92  * \brief Enable an FREQM callback.
93  *
94  * \param[in,out] module  Pointer to the software instance struct
95  * \param[in] type Callback source type
96  *
97  * \return Status of the callback enable operation.
98  * \retval STATUS_OK The callback was enabled successfully
99  * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
100  */
freqm_enable_callback(struct freqm_module * const module,const enum freqm_callback_type type)101 static inline enum status_code freqm_enable_callback(struct freqm_module *const module,
102 		const enum freqm_callback_type type)
103 {
104 	/* Sanity check arguments */
105 	Assert(module);
106 
107 	if (type == FREQM_CALLBACK_MEASURE_DONE){
108 		module->hw->INTENSET.reg = FREQM_INTENSET_DONE;
109 	} else {
110 		Assert(false);
111 		return STATUS_ERR_INVALID_ARG;
112 	}
113 
114 	system_interrupt_enable(SYSTEM_INTERRUPT_MODULE_FREQM);
115 	return STATUS_OK;
116 }
117 
118 /**
119  * \brief Disable an FREQM callback.
120  *
121  * \param[in,out] module  Pointer to the software instance struct
122  * \param[in]  type Callback source type
123  *
124  * \return Status of the callback enable operation.
125  * \retval STATUS_OK The callback was enabled successfully
126  * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied
127  */
freqm_disable_callback(struct freqm_module * const module,const enum freqm_callback_type type)128 static inline enum status_code freqm_disable_callback(struct freqm_module *const module,
129 		 const enum freqm_callback_type type)
130 {
131 	/* Sanity check arguments */
132 	Assert(module);
133 
134 	if (type == FREQM_CALLBACK_MEASURE_DONE){
135 		module->hw->INTENCLR.reg = FREQM_INTENCLR_DONE;
136 	} else {
137 		Assert(false);
138 		return STATUS_ERR_INVALID_ARG;
139 	}
140 
141 	system_interrupt_disable(SYSTEM_INTERRUPT_MODULE_FREQM);
142 	return STATUS_OK;
143 }
144 
145 /** @} */
146 
147 /** @} */
148 
149 #ifdef __cplusplus
150 }
151 #endif
152 
153 #endif  /* FREQM_CALLBACK_H_INCLUDED */
154