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 #include "tsens_callback.h"
48 
49 struct tsens_module *_tsens_instances;
50 
51 /** Interrupt handler for the TSENS module. */
TSENS_Handler(void)52 void TSENS_Handler(void)
53 {
54 	struct tsens_module *module = _tsens_instances;
55 	Assert(module);
56 
57 	/* get interrupt flags and mask out enabled callbacks */
58 	uint32_t flags = TSENS->INTFLAG.reg;
59 
60 	/* store TSENS result in job buffer */
61 	uint32_t temp = TSENS->VALUE.reg;
62 	if(temp & 0x00800000) {
63 		temp |= ~TSENS_VALUE_MASK;
64 	}
65 #if (ERRATA_14476)
66 	*(module->value) = temp * (-1);
67 #endif
68 
69 	for(uint8_t i = 0; i < TSENS_CALLBACK_NUM; i++)
70 	{
71 		if (flags & ((uint32_t)0x01 << i)) {
72 			/* Clear the INTFLAG anyway */
73 			TSENS->INTFLAG.reg = (uint32_t)0x01 << i;
74 
75 			if(module->callback[i] != NULL) {
76 				module->callback[i]((enum tsens_callback)i);
77 			}
78 		}
79 	}
80 }
81 
82 /**
83  * \brief Registers a callback.
84  *
85  * Registers a callback function which is implemented by the user.
86  *
87  * \note The callback must be enabled by for the interrupt handler to call it
88  * when the condition for the callback is met.
89  *
90  * \param[in] module         Pointer to TSENS software instance struct
91  * \param[in] callback_func  Pointer to callback function
92  * \param[in] callback_type  Callback type given by an enum
93  *
94  */
tsens_register_callback(struct tsens_module * const module,tsens_callback_t callback_func,enum tsens_callback callback_type)95 enum status_code tsens_register_callback(
96 		struct tsens_module *const module,
97 		tsens_callback_t callback_func,
98 		enum tsens_callback callback_type)
99 {
100 	/* Sanity check arguments */
101 	Assert(module);
102 	Assert(callback_func);
103 
104 	if(callback_type > TSENS_CALLBACK_NUM) {
105 		return STATUS_ERR_INVALID_ARG;
106 	}
107 
108 	/* Register callback function */
109 	module->callback[callback_type] = callback_func;
110 	_tsens_instances = module;
111 
112 	return STATUS_OK;
113 }
114 
115 /**
116  * \brief Unregisters a callback.
117  *
118  * Unregisters a callback function which is implemented by the user.
119  *
120  * \param[in] module         Pointer to TSENS software instance struct
121  * \param[in] callback_type  Callback type given by an enum
122  *
123  */
tsens_unregister_callback(struct tsens_module * const module,enum tsens_callback callback_type)124 enum status_code tsens_unregister_callback(
125 		struct tsens_module *const module,
126 		enum tsens_callback callback_type)
127 {
128 	/* Sanity check arguments */
129 	Assert(module);
130 
131 	if(callback_type > TSENS_CALLBACK_NUM) {
132 		return STATUS_ERR_INVALID_ARG;
133 	}
134 
135 	/* Register callback function */
136 	module->callback[callback_type] = NULL;
137 
138 	return STATUS_OK;
139 }
140 
141 /**
142  * \brief Read result from TSENS.
143  *
144  * \param[in]  module_inst  Pointer to the TSENS software instance struct
145  * \param[out] result       Pointer to store the TSENS result
146  *
147  */
tsens_read_job(struct tsens_module * const module_inst,int32_t * result)148 void tsens_read_job(
149 		struct tsens_module *const module_inst,
150 		int32_t *result)
151 {
152 	Assert(module_inst);
153 	Assert(result);
154 
155 	module_inst->value = result;
156 
157 	if(!(TSENS->CTRLC.reg & TSENS_CTRLC_FREERUN)) {
158 		tsens_start_conversion();
159 	}
160 }
161