1 /**
2  * \file
3  *
4  * \brief SAM TC - Timer Counter 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 /*
45  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
46  */
47 
48 #ifndef TC_INTERRUPT_H_INCLUDED
49 #define TC_INTERRUPT_H_INCLUDED
50 
51 #include "tc.h"
52 #include <system_interrupt.h>
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 #if !defined(__DOXYGEN__)
59 extern void *_tc_instances[TC_INST_NUM];
60 
61 #  define _TC_INTERRUPT_VECT_NUM(n, unused) \
62 		  SYSTEM_INTERRUPT_MODULE_TC##n,
63 /**
64  * \internal Get the interrupt vector for the given device instance
65  *
66  * \param[in] TC module instance number
67  *
68  * \return Interrupt vector for of the given TC module instance.
69  */
_tc_interrupt_get_interrupt_vector(uint32_t inst_num)70 static enum system_interrupt_vector _tc_interrupt_get_interrupt_vector(
71 		uint32_t inst_num)
72 {
73 	static uint8_t tc_interrupt_vectors[TC_INST_NUM] =
74 		{
75 #if (SAML21E) || (SAML21G) || (SAMR30E) || (SAMR30G)
76 			SYSTEM_INTERRUPT_MODULE_TC0,
77 			SYSTEM_INTERRUPT_MODULE_TC1,
78 			SYSTEM_INTERRUPT_MODULE_TC4
79 #else
80 			MRECURSION(TC_INST_NUM, _TC_INTERRUPT_VECT_NUM, TC_INST_MAX_ID)
81 #endif
82 		};
83 
84 	return (enum system_interrupt_vector)tc_interrupt_vectors[inst_num];
85 }
86 #endif /* !defined(__DOXYGEN__) */
87 
88 /**
89  * \name Callback Management
90  * {@
91  */
92 
93 enum status_code tc_register_callback(
94 		struct tc_module *const module,
95 		tc_callback_t callback_func,
96 		const enum tc_callback callback_type);
97 
98 enum status_code tc_unregister_callback(
99 		struct tc_module *const module,
100 		const enum tc_callback callback_type);
101 
102 /**
103  * \brief Enables callback.
104  *
105  * Enables the callback function registered by the \ref
106  * tc_register_callback. The callback function will be called from the
107  * interrupt handler when the conditions for the callback type are
108  * met. This function will also enable the appropriate interrupts.
109  *
110  * \param[in]     module        Pointer to TC software instance struct
111  * \param[in]     callback_type Callback type given by an enum
112  */
tc_enable_callback(struct tc_module * const module,const enum tc_callback callback_type)113 static inline void tc_enable_callback(
114 		struct tc_module *const module,
115 		const enum tc_callback callback_type)
116 {
117 	/* Sanity check arguments */
118 	Assert(module);
119 
120 
121 	/* Enable interrupts for this TC module */
122 	system_interrupt_enable(_tc_interrupt_get_interrupt_vector(_tc_get_inst_index(module->hw)));
123 
124 	/* Enable callback */
125 	if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
126 		module->enable_callback_mask |= TC_INTFLAG_MC(1);
127 		module->hw->COUNT8.INTENSET.reg = TC_INTFLAG_MC(1);
128 	}
129 	else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
130 		module->enable_callback_mask |= TC_INTFLAG_MC(2);
131 		module->hw->COUNT8.INTENSET.reg = TC_INTFLAG_MC(2);
132 	}
133 	else {
134 		module->enable_callback_mask |= (1 << callback_type);
135 		module->hw->COUNT8.INTENSET.reg = (1 << callback_type);
136 	}
137 }
138 
139 /**
140  * \brief Disables callback.
141  *
142  * Disables the callback function registered by the \ref
143  * tc_register_callback, and the callback will not be called from the
144  * interrupt routine. The function will also disable the appropriate
145  * interrupts.
146  *
147  * \param[in]     module        Pointer to TC software instance struct
148  * \param[in]     callback_type Callback type given by an enum
149  */
tc_disable_callback(struct tc_module * const module,const enum tc_callback callback_type)150 static inline void tc_disable_callback(
151 		struct tc_module *const module,
152 		const enum tc_callback callback_type){
153 	/* Sanity check arguments */
154 	Assert(module);
155 
156 	/* Disable callback */
157 	if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
158 		module->hw->COUNT8.INTENCLR.reg = TC_INTFLAG_MC(1);
159 		module->enable_callback_mask &= ~TC_INTFLAG_MC(1);
160 	}
161 	else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
162 		module->hw->COUNT8.INTENCLR.reg = TC_INTFLAG_MC(2);
163 		module->enable_callback_mask &= ~TC_INTFLAG_MC(2);
164 	}
165 	else {
166 		module->hw->COUNT8.INTENCLR.reg = (1 << callback_type);
167 		module->enable_callback_mask &= ~(1 << callback_type);
168 	}
169 }
170 
171 /**
172  * @}
173  */
174 
175 #ifdef __cplusplus
176 }
177 #endif
178 
179 #endif /* TC_INTERRUPT_H_INCLUDED */
180