1 /**
2  * \file
3  *
4  * \brief SAM TCC - Timer Counter for Control Applications Callback Driver
5  *
6  * Copyright (C) 2013-2016 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 "tcc_callback.h"
48 
49 void *_tcc_instances[TCC_INST_NUM];
50 
51 void _tcc_interrupt_handler(uint8_t module_index);
52 
53 const uint32_t _tcc_intflag[TCC_CALLBACK_N] = {
54 	TCC_INTFLAG_OVF,
55 	TCC_INTFLAG_TRG,
56 	TCC_INTFLAG_CNT,
57 	TCC_INTFLAG_ERR,
58 	TCC_INTFLAG_FAULTA,
59 	TCC_INTFLAG_FAULTB,
60 	TCC_INTFLAG_FAULT0,
61 	TCC_INTFLAG_FAULT1,
62 #define _TCC_INTFLAG_MC(n,dummy) TCC_INTFLAG_MC##n,
63 	/* TCC_INTFLAG_MC0 ~ ... */
64 	MREPEAT(TCC_NUM_CHANNELS, _TCC_INTFLAG_MC, 0)
65 #undef _TCC_INTFLAG_MC
66 };
67 
68 #  define _TCC_INTERRUPT_VECT_NUM(n, unused) \
69 		  SYSTEM_INTERRUPT_MODULE_TCC##n,
70 /**
71  * \internal Get the interrupt vector for the given device instance
72  *
73  * \param[in] The TCC module instance number
74  *
75  * \return Interrupt vector for of the given TCC module instance.
76  */
_tcc_interrupt_get_interrupt_vector(uint32_t inst_num)77 static enum system_interrupt_vector _tcc_interrupt_get_interrupt_vector(
78 		uint32_t inst_num)
79 {
80 	static uint8_t tcc_interrupt_vectors[TCC_INST_NUM] = {
81 		MREPEAT(TCC_INST_NUM, _TCC_INTERRUPT_VECT_NUM, 0)
82 	};
83 
84 	return (enum system_interrupt_vector)tcc_interrupt_vectors[inst_num];
85 }
86 
87 /**
88  * \brief Registers a callback
89  *
90  * Registers a callback function which is implemented by the user.
91  *
92  * \note The callback must be enabled by \ref tcc_enable_callback,
93  * in order for the interrupt handler to call it when the conditions for the
94  * callback type is met.
95  *
96  * \param[in]     module        Pointer to TCC software instance struct
97  * \param[in]     callback_func Pointer to callback function
98  * \param[in]     callback_type Callback type given by an enum
99  */
tcc_register_callback(struct tcc_module * const module,tcc_callback_t callback_func,const enum tcc_callback callback_type)100 enum status_code tcc_register_callback(
101 		struct tcc_module *const module,
102 		tcc_callback_t callback_func,
103 		const enum tcc_callback callback_type)
104 {
105 	/* Sanity check arguments */
106 	Assert(module);
107 	Assert(callback_func);
108 
109 	/* Register callback function */
110 	module->callback[callback_type] = callback_func;
111 
112 	/* Set the bit corresponding to the callback_type */
113 	module->register_callback_mask |= _tcc_intflag[callback_type];
114 
115 	return STATUS_OK;
116 }
117 
118 /**
119  * \brief Unregisters a callback
120  *
121  * Unregisters a callback function implemented by the user. The callback should
122  * be disabled before it is unregistered.
123  *
124  * \param[in]     module Pointer to TCC software instance struct
125  * \param[in]     callback_type Callback type given by an enum
126  */
tcc_unregister_callback(struct tcc_module * const module,const enum tcc_callback callback_type)127 enum status_code tcc_unregister_callback(
128 		struct tcc_module *const module,
129 		const enum tcc_callback callback_type)
130 {
131 	/* Sanity check arguments */
132 	Assert(module);
133 
134 	/* Unregister callback function */
135 	module->callback[callback_type] = NULL;
136 
137 	/* Clear the bit corresponding to the callback_type */
138 	module->register_callback_mask &= ~_tcc_intflag[callback_type];
139 
140 	return STATUS_OK;
141 }
142 
143 /**
144  * \brief Enables callback
145  *
146  * Enables the callback function registered by the \ref
147  * tcc_register_callback. The callback function will be called from the
148  * interrupt handler when the conditions for the callback type are
149  * met. This function will also enable the appropriate interrupts.
150  *
151  * \param[in]     module Pointer to TCC software instance struct
152  * \param[in]     callback_type Callback type given by an enum
153  */
tcc_enable_callback(struct tcc_module * const module,const enum tcc_callback callback_type)154 void tcc_enable_callback(
155 		struct tcc_module *const module,
156 		const enum tcc_callback callback_type)
157 {
158 	/* Sanity check arguments */
159 	Assert(module);
160 	Assert(module->hw);
161 
162 	/* Enable interrupts for this TCC module */
163 	system_interrupt_enable(_tcc_interrupt_get_interrupt_vector(
164 			_tcc_get_inst_index(module->hw)));
165 
166 	/* Enable channel or other callbacks */
167 	module->enable_callback_mask |= _tcc_intflag[callback_type];
168 	module->hw->INTENSET.reg = _tcc_intflag[callback_type];
169 }
170 
171 /**
172  * \brief Disables callback
173  *
174  * Disables the callback function registered by the \ref
175  * tcc_register_callback, and the callback will not be called from the
176  * interrupt routine. The function will also disable the appropriate
177  * interrupts.
178  *
179  * \param[in]     module Pointer to TCC software instance struct
180  * \param[in]     callback_type Callback type given by an enum
181  */
tcc_disable_callback(struct tcc_module * const module,const enum tcc_callback callback_type)182 void tcc_disable_callback(
183 		struct tcc_module *const module,
184 		const enum tcc_callback callback_type)
185 {
186 	/* Sanity check arguments */
187 	Assert(module);
188 	Assert(module->hw);
189 
190 	/* Disable interrupts for this TCC module */
191 	system_interrupt_disable(_tcc_interrupt_get_interrupt_vector(
192 			_tcc_get_inst_index(module->hw)));
193 
194 	/* Disable channel or other callbacks */
195 	module->enable_callback_mask &= ~_tcc_intflag[callback_type];
196 	module->hw->INTENCLR.reg = _tcc_intflag[callback_type];
197 }
198 
199 
200 /**
201  * \internal ISR handler for TCC
202  *
203  * Auto-generate a set of interrupt handlers for each TCC in the device.
204  */
205 #define _TCC_INTERRUPT_HANDLER(n, m) \
206 		void TCC##n##_Handler(void) \
207 		{ \
208 			_tcc_interrupt_handler(n); \
209 		}
210 
211 MREPEAT(TCC_INST_NUM, _TCC_INTERRUPT_HANDLER, 0)
212 
213 /**
214  * \internal Interrupt handler for the TCC module
215  *
216  * Handles interrupts as they occur, it will run the callback functions
217  * that are registered and enabled.
218  *
219  * \param[in]  module_index  ID of the TCC instance calling the interrupt
220  *                           handler
221  */
_tcc_interrupt_handler(uint8_t module_index)222 void _tcc_interrupt_handler(
223 		uint8_t module_index)
224 {
225 	int i;
226 
227 	uint32_t interrupt_and_callback_status_mask;
228 
229 	struct tcc_module *module =
230 			(struct tcc_module *)_tcc_instances[module_index];
231 
232 	interrupt_and_callback_status_mask = (module->hw->INTFLAG.reg &
233 			module->register_callback_mask &
234 			module->enable_callback_mask);
235 
236 	/* Check if callback interrupt has occured */
237 	for (i = 0; i < TCC_CALLBACK_N; i ++) {
238 		if (interrupt_and_callback_status_mask & _tcc_intflag[i]) {
239 			/* Invoke the registered and enabled callback function */
240 			(module->callback[i])(module);
241 			/* Clear interrupt flag */
242 			module->hw->INTFLAG.reg = _tcc_intflag[i];
243 		}
244 	}
245 }
246