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 #include "tc_interrupt.h"
49
50 void *_tc_instances[TC_INST_NUM];
51
52 void _tc_interrupt_handler(uint8_t instance);
53
54 /**
55 * \brief Registers a callback.
56 *
57 * Registers a callback function which is implemented by the user.
58 *
59 * \note The callback must be enabled by \ref tc_enable_callback,
60 * in order for the interrupt handler to call it when the conditions for the
61 * callback type is met.
62 *
63 * \param[in] module Pointer to TC software instance struct
64 * \param[in] callback_func Pointer to callback function
65 * \param[in] callback_type Callback type given by an enum
66 */
tc_register_callback(struct tc_module * const module,tc_callback_t callback_func,const enum tc_callback callback_type)67 enum status_code tc_register_callback(
68 struct tc_module *const module,
69 tc_callback_t callback_func,
70 const enum tc_callback callback_type)
71 {
72 /* Sanity check arguments */
73 Assert(module);
74 Assert(callback_func);
75
76 /* Register callback function */
77 module->callback[callback_type] = callback_func;
78
79 /* Set the bit corresponding to the callback_type */
80 if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
81 module->register_callback_mask |= TC_INTFLAG_MC(1);
82 }
83 else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
84 module->register_callback_mask |= TC_INTFLAG_MC(2);
85 }
86 else {
87 module->register_callback_mask |= (1 << callback_type);
88 }
89 return STATUS_OK;
90 }
91
92 /**
93 * \brief Unregisters a callback.
94 *
95 * Unregisters a callback function implemented by the user. The callback should be
96 * disabled before it is unregistered.
97 *
98 * \param[in] module Pointer to TC software instance struct
99 * \param[in] callback_type Callback type given by an enum
100 */
tc_unregister_callback(struct tc_module * const module,const enum tc_callback callback_type)101 enum status_code tc_unregister_callback(
102 struct tc_module *const module,
103 const enum tc_callback callback_type)
104 {
105 /* Sanity check arguments */
106 Assert(module);
107
108 /* Unregister callback function */
109 module->callback[callback_type] = NULL;
110
111 /* Clear the bit corresponding to the callback_type */
112 if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
113 module->register_callback_mask &= ~TC_INTFLAG_MC(1);
114 }
115 else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
116 module->register_callback_mask &= ~TC_INTFLAG_MC(2);
117 }
118 else {
119 module->register_callback_mask &= ~(1 << callback_type);
120 }
121 return STATUS_OK;
122 }
123
124 /**
125 * \internal ISR handler for TC
126 *
127 * Auto-generate a set of interrupt handlers for each TC in the device.
128 */
129 #define _TC_INTERRUPT_HANDLER(n, m) \
130 void TC##n##_Handler(void) \
131 { \
132 _tc_interrupt_handler(m); \
133 }
134
135 #if (SAML21E) || (SAML21G) || (SAMR30E) || (SAMR30G)
136 _TC_INTERRUPT_HANDLER(0,0)
137 _TC_INTERRUPT_HANDLER(1,1)
138 _TC_INTERRUPT_HANDLER(4,2)
139 #else
140 MRECURSION(TC_INST_NUM, _TC_INTERRUPT_HANDLER, TC_INST_MAX_ID)
141 #endif
142
143
144 /**
145 * \internal Interrupt Handler for TC module
146 *
147 * Handles interrupts as they occur, it will run the callback functions
148 * that are registered and enabled.
149 *
150 * \param[in] instance ID of the TC instance calling the interrupt
151 * handler
152 */
_tc_interrupt_handler(uint8_t instance)153 void _tc_interrupt_handler(
154 uint8_t instance)
155 {
156 /* Temporary variable */
157 uint8_t interrupt_and_callback_status_mask;
158
159 /* Get device instance from the look-up table */
160 struct tc_module *module
161 = (struct tc_module *)_tc_instances[instance];
162
163 /* Read and mask interrupt flag register */
164 interrupt_and_callback_status_mask = module->hw->COUNT8.INTFLAG.reg &
165 module->register_callback_mask &
166 module->enable_callback_mask;
167
168 /* Check if an Overflow interrupt has occurred */
169 if (interrupt_and_callback_status_mask & TC_INTFLAG_OVF) {
170 /* Invoke registered and enabled callback function */
171 (module->callback[TC_CALLBACK_OVERFLOW])(module);
172 /* Clear interrupt flag */
173 module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_OVF;
174 }
175
176 /* Check if an Error interrupt has occurred */
177 if (interrupt_and_callback_status_mask & TC_INTFLAG_ERR) {
178 /* Invoke registered and enabled callback function */
179 (module->callback[TC_CALLBACK_ERROR])(module);
180 /* Clear interrupt flag */
181 module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_ERR;
182 }
183
184 /* Check if an Match/Capture Channel 0 interrupt has occurred */
185 if (interrupt_and_callback_status_mask & TC_INTFLAG_MC(1)) {
186 /* Invoke registered and enabled callback function */
187 (module->callback[TC_CALLBACK_CC_CHANNEL0])(module);
188 /* Clear interrupt flag */
189 module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_MC(1);
190 }
191
192 /* Check if an Match/Capture Channel 1 interrupt has occurred */
193 if (interrupt_and_callback_status_mask & TC_INTFLAG_MC(2)) {
194 /* Invoke registered and enabled callback function */
195 (module->callback[TC_CALLBACK_CC_CHANNEL1])(module);
196 /* Clear interrupt flag */
197 module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_MC(2);
198 }
199 }
200