1 /**
2  * \file
3  *
4  * \brief SAM Temperature Sensor Driver
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 
47 #ifndef TSENS_CALLBACK_H_INCLUDED
48 #define TSENS_CALLBACK_H_INCLUDED
49 
50 #ifdef __cplusplus
51 extern "C" {
52 #endif
53 
54 #include <compiler.h>
55 #include <system_interrupt.h>
56 #include "tsens.h"
57 
58 /**
59  * \addtogroup asfdoc_sam0_tsens_group
60  *
61  * @{
62  */
63 
64 /**
65  * \brief TSENS Callback Types.
66  *
67  * Callback types for TSENS callback driver.
68  *
69  */
70 enum tsens_callback {
71 	/** Callback for result ready */
72 	TSENS_CALLBACK_RESULT_READY,
73 	/** Callback when result overwritten before read */
74 	TSENS_CALLBACK_OVERRUN,
75 	/** Callback when window is hit */
76 	TSENS_CALLBACK_WINDOW,
77 	/** Callback when the result overflows */
78 	TSENS_CALLBACK_OVF,
79 #  if !defined(__DOXYGEN__)
80 	/** Number of available callbacks */
81 	TSENS_CALLBACK_NUM,
82 #  endif
83 };
84 
85 /** Type of the callback functions. */
86 typedef void (*tsens_callback_t)(enum tsens_callback);
87 
88 /**
89  * \brief TSENS software device instance structure.
90  *
91  * TSENS software instance structure, used to retain software state information
92  * of an associated hardware module instance.
93  *
94  * \note The fields of this structure should not be altered by the user
95  *       application; they are reserved for module-internal use only.
96  */
97 struct tsens_module {
98 #if !defined(__DOXYGEN__)
99 	/** Array to store callback functions. */
100 	tsens_callback_t callback[TSENS_CALLBACK_NUM];
101 	/** Pointer to used for TSENS results. */
102 	volatile int32_t *value;
103 #endif
104 };
105 
106 /**
107  * \name Callback Management
108  * @{
109  */
110 enum status_code tsens_register_callback(
111 			struct tsens_module *const module,
112 			tsens_callback_t callback_func,
113 			enum tsens_callback callback_type);
114 
115 enum status_code tsens_unregister_callback(
116 			struct tsens_module *const module,
117 			enum tsens_callback callback_type);
118 
119 /**
120  * \brief Enables callback.
121  *
122  * Enables the callback function registered by \ref
123  * tsens_register_callback. The callback function will be called from the
124  * interrupt handler when the conditions for the callback type are met.
125  *
126  * \param[in]     callback_type Callback type given by an enum
127  *
128  */
tsens_enable_callback(enum tsens_callback callback_type)129 static inline void tsens_enable_callback(enum tsens_callback callback_type)
130 {
131 	uint32_t inenset_temp = 0;
132 
133 	switch (callback_type) {
134 		case TSENS_CALLBACK_RESULT_READY:
135 			inenset_temp |= TSENS_INTFLAG_RESRDY;
136 			break;
137 		case TSENS_CALLBACK_OVERRUN:
138 			inenset_temp |= TSENS_INTENSET_OVERRUN;
139 			break;
140 		case TSENS_CALLBACK_WINDOW:
141 			inenset_temp |= TSENS_INTENSET_WINMON;
142 			break;
143 		case TSENS_CALLBACK_OVF:
144 			inenset_temp |= TSENS_INTENSET_OVF;
145 			break;
146 
147 		default:
148 			break;
149 	}
150 
151 	/* Enable the interrupt for the callback */
152 	TSENS->INTENSET.reg = inenset_temp;
153 }
154 
155 /**
156  * \brief Disables callback.
157  *
158  * Disables the callback function registered by the \ref
159  * tsens_register_callback.
160  *
161  * \param[in]     callback_type Callback type given by an enum
162  *
163  */
tsens_disable_callback(enum tsens_callback callback_type)164 static inline void tsens_disable_callback(enum tsens_callback callback_type)
165 {
166 	uint32_t inenclr_temp = 0;
167 
168 	switch (callback_type) {
169 		case TSENS_CALLBACK_RESULT_READY:
170 			inenclr_temp |= TSENS_INTENCLR_OVERRUN;
171 			break;
172 		case TSENS_CALLBACK_OVERRUN:
173 			inenclr_temp |= TSENS_INTENSET_OVERRUN;
174 			break;
175 		case TSENS_CALLBACK_WINDOW:
176 			inenclr_temp |= TSENS_INTENSET_WINMON;
177 			break;
178 		case TSENS_CALLBACK_OVF:
179 			inenclr_temp |= TSENS_INTENSET_OVF;
180 			break;
181 
182 		default:
183 			break;
184 	}
185 
186 	/* Disable the interrupt for the callback */
187 	TSENS->INTENCLR.reg = inenclr_temp;
188 }
189 
190 void tsens_read_job(struct tsens_module *const module_inst, int32_t *result);
191 /** @} */
192 
193 /** @} */
194 #ifdef __cplusplus
195 }
196 #endif
197 
198 #endif /* TSENS_CALLBACK_H_INCLUDED */
199