Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sun May 14 23:18:57 2017 +0000
Revision:
18:6a4db94011d3
Publishing again

Who changed what in which revision?

UserRevisionLine numberNew contents of line
sahilmgandhi 18:6a4db94011d3 1 /**
sahilmgandhi 18:6a4db94011d3 2 * \file
sahilmgandhi 18:6a4db94011d3 3 *
sahilmgandhi 18:6a4db94011d3 4 * \brief SAM TC - Timer Counter Callback Driver
sahilmgandhi 18:6a4db94011d3 5 *
sahilmgandhi 18:6a4db94011d3 6 * Copyright (C) 2013-2015 Atmel Corporation. All rights reserved.
sahilmgandhi 18:6a4db94011d3 7 *
sahilmgandhi 18:6a4db94011d3 8 * \asf_license_start
sahilmgandhi 18:6a4db94011d3 9 *
sahilmgandhi 18:6a4db94011d3 10 * \page License
sahilmgandhi 18:6a4db94011d3 11 *
sahilmgandhi 18:6a4db94011d3 12 * Redistribution and use in source and binary forms, with or without
sahilmgandhi 18:6a4db94011d3 13 * modification, are permitted provided that the following conditions are met:
sahilmgandhi 18:6a4db94011d3 14 *
sahilmgandhi 18:6a4db94011d3 15 * 1. Redistributions of source code must retain the above copyright notice,
sahilmgandhi 18:6a4db94011d3 16 * this list of conditions and the following disclaimer.
sahilmgandhi 18:6a4db94011d3 17 *
sahilmgandhi 18:6a4db94011d3 18 * 2. Redistributions in binary form must reproduce the above copyright notice,
sahilmgandhi 18:6a4db94011d3 19 * this list of conditions and the following disclaimer in the documentation
sahilmgandhi 18:6a4db94011d3 20 * and/or other materials provided with the distribution.
sahilmgandhi 18:6a4db94011d3 21 *
sahilmgandhi 18:6a4db94011d3 22 * 3. The name of Atmel may not be used to endorse or promote products derived
sahilmgandhi 18:6a4db94011d3 23 * from this software without specific prior written permission.
sahilmgandhi 18:6a4db94011d3 24 *
sahilmgandhi 18:6a4db94011d3 25 * 4. This software may only be redistributed and used in connection with an
sahilmgandhi 18:6a4db94011d3 26 * Atmel microcontroller product.
sahilmgandhi 18:6a4db94011d3 27 *
sahilmgandhi 18:6a4db94011d3 28 * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
sahilmgandhi 18:6a4db94011d3 29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
sahilmgandhi 18:6a4db94011d3 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
sahilmgandhi 18:6a4db94011d3 31 * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
sahilmgandhi 18:6a4db94011d3 32 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
sahilmgandhi 18:6a4db94011d3 33 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
sahilmgandhi 18:6a4db94011d3 34 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
sahilmgandhi 18:6a4db94011d3 35 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
sahilmgandhi 18:6a4db94011d3 36 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
sahilmgandhi 18:6a4db94011d3 37 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
sahilmgandhi 18:6a4db94011d3 38 * POSSIBILITY OF SUCH DAMAGE.
sahilmgandhi 18:6a4db94011d3 39 *
sahilmgandhi 18:6a4db94011d3 40 * \asf_license_stop
sahilmgandhi 18:6a4db94011d3 41 *
sahilmgandhi 18:6a4db94011d3 42 */
sahilmgandhi 18:6a4db94011d3 43
sahilmgandhi 18:6a4db94011d3 44 /*
sahilmgandhi 18:6a4db94011d3 45 * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
sahilmgandhi 18:6a4db94011d3 46 */
sahilmgandhi 18:6a4db94011d3 47
sahilmgandhi 18:6a4db94011d3 48 #include "tc_interrupt.h"
sahilmgandhi 18:6a4db94011d3 49
sahilmgandhi 18:6a4db94011d3 50 void *_tc_instances[TC_INST_NUM];
sahilmgandhi 18:6a4db94011d3 51
sahilmgandhi 18:6a4db94011d3 52 void _tc_interrupt_handler(uint8_t instance);
sahilmgandhi 18:6a4db94011d3 53
sahilmgandhi 18:6a4db94011d3 54 /**
sahilmgandhi 18:6a4db94011d3 55 * \brief Registers a callback.
sahilmgandhi 18:6a4db94011d3 56 *
sahilmgandhi 18:6a4db94011d3 57 * Registers a callback function which is implemented by the user.
sahilmgandhi 18:6a4db94011d3 58 *
sahilmgandhi 18:6a4db94011d3 59 * \note The callback must be enabled by \ref tc_enable_callback,
sahilmgandhi 18:6a4db94011d3 60 * in order for the interrupt handler to call it when the conditions for the
sahilmgandhi 18:6a4db94011d3 61 * callback type is met.
sahilmgandhi 18:6a4db94011d3 62 *
sahilmgandhi 18:6a4db94011d3 63 * \param[in] module Pointer to TC software instance struct
sahilmgandhi 18:6a4db94011d3 64 * \param[in] callback_func Pointer to callback function
sahilmgandhi 18:6a4db94011d3 65 * \param[in] callback_type Callback type given by an enum
sahilmgandhi 18:6a4db94011d3 66 */
sahilmgandhi 18:6a4db94011d3 67 enum status_code tc_register_callback(
sahilmgandhi 18:6a4db94011d3 68 struct tc_module *const module,
sahilmgandhi 18:6a4db94011d3 69 tc_callback_t callback_func,
sahilmgandhi 18:6a4db94011d3 70 const enum tc_callback callback_type)
sahilmgandhi 18:6a4db94011d3 71 {
sahilmgandhi 18:6a4db94011d3 72 /* Sanity check arguments */
sahilmgandhi 18:6a4db94011d3 73 Assert(module);
sahilmgandhi 18:6a4db94011d3 74 Assert(callback_func);
sahilmgandhi 18:6a4db94011d3 75
sahilmgandhi 18:6a4db94011d3 76 /* Register callback function */
sahilmgandhi 18:6a4db94011d3 77 module->callback[callback_type] = callback_func;
sahilmgandhi 18:6a4db94011d3 78
sahilmgandhi 18:6a4db94011d3 79 /* Set the bit corresponding to the callback_type */
sahilmgandhi 18:6a4db94011d3 80 if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
sahilmgandhi 18:6a4db94011d3 81 module->register_callback_mask |= TC_INTFLAG_MC(1);
sahilmgandhi 18:6a4db94011d3 82 } else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
sahilmgandhi 18:6a4db94011d3 83 module->register_callback_mask |= TC_INTFLAG_MC(2);
sahilmgandhi 18:6a4db94011d3 84 } else {
sahilmgandhi 18:6a4db94011d3 85 module->register_callback_mask |= (1 << callback_type);
sahilmgandhi 18:6a4db94011d3 86 }
sahilmgandhi 18:6a4db94011d3 87 return STATUS_OK;
sahilmgandhi 18:6a4db94011d3 88 }
sahilmgandhi 18:6a4db94011d3 89
sahilmgandhi 18:6a4db94011d3 90 /**
sahilmgandhi 18:6a4db94011d3 91 * \brief Unregisters a callback.
sahilmgandhi 18:6a4db94011d3 92 *
sahilmgandhi 18:6a4db94011d3 93 * Unregisters a callback function implemented by the user. The callback should be
sahilmgandhi 18:6a4db94011d3 94 * disabled before it is unregistered.
sahilmgandhi 18:6a4db94011d3 95 *
sahilmgandhi 18:6a4db94011d3 96 * \param[in] module Pointer to TC software instance struct
sahilmgandhi 18:6a4db94011d3 97 * \param[in] callback_type Callback type given by an enum
sahilmgandhi 18:6a4db94011d3 98 */
sahilmgandhi 18:6a4db94011d3 99 enum status_code tc_unregister_callback(
sahilmgandhi 18:6a4db94011d3 100 struct tc_module *const module,
sahilmgandhi 18:6a4db94011d3 101 const enum tc_callback callback_type)
sahilmgandhi 18:6a4db94011d3 102 {
sahilmgandhi 18:6a4db94011d3 103 /* Sanity check arguments */
sahilmgandhi 18:6a4db94011d3 104 Assert(module);
sahilmgandhi 18:6a4db94011d3 105
sahilmgandhi 18:6a4db94011d3 106 /* Unregister callback function */
sahilmgandhi 18:6a4db94011d3 107 module->callback[callback_type] = NULL;
sahilmgandhi 18:6a4db94011d3 108
sahilmgandhi 18:6a4db94011d3 109 /* Clear the bit corresponding to the callback_type */
sahilmgandhi 18:6a4db94011d3 110 if (callback_type == TC_CALLBACK_CC_CHANNEL0) {
sahilmgandhi 18:6a4db94011d3 111 module->register_callback_mask &= ~TC_INTFLAG_MC(1);
sahilmgandhi 18:6a4db94011d3 112 } else if (callback_type == TC_CALLBACK_CC_CHANNEL1) {
sahilmgandhi 18:6a4db94011d3 113 module->register_callback_mask &= ~TC_INTFLAG_MC(2);
sahilmgandhi 18:6a4db94011d3 114 } else {
sahilmgandhi 18:6a4db94011d3 115 module->register_callback_mask &= ~(1 << callback_type);
sahilmgandhi 18:6a4db94011d3 116 }
sahilmgandhi 18:6a4db94011d3 117 return STATUS_OK;
sahilmgandhi 18:6a4db94011d3 118 }
sahilmgandhi 18:6a4db94011d3 119
sahilmgandhi 18:6a4db94011d3 120 /**
sahilmgandhi 18:6a4db94011d3 121 * \internal ISR handler for TC
sahilmgandhi 18:6a4db94011d3 122 *
sahilmgandhi 18:6a4db94011d3 123 * Auto-generate a set of interrupt handlers for each TC in the device.
sahilmgandhi 18:6a4db94011d3 124 */
sahilmgandhi 18:6a4db94011d3 125 #define _TC_INTERRUPT_HANDLER(n, m) \
sahilmgandhi 18:6a4db94011d3 126 void TC##n##_Handler(void) \
sahilmgandhi 18:6a4db94011d3 127 { \
sahilmgandhi 18:6a4db94011d3 128 _tc_interrupt_handler(m); \
sahilmgandhi 18:6a4db94011d3 129 }
sahilmgandhi 18:6a4db94011d3 130
sahilmgandhi 18:6a4db94011d3 131 #if (SAML21E) || (SAML21G)
sahilmgandhi 18:6a4db94011d3 132 _TC_INTERRUPT_HANDLER(0,0)
sahilmgandhi 18:6a4db94011d3 133 _TC_INTERRUPT_HANDLER(1,1)
sahilmgandhi 18:6a4db94011d3 134 _TC_INTERRUPT_HANDLER(4,2)
sahilmgandhi 18:6a4db94011d3 135 #else
sahilmgandhi 18:6a4db94011d3 136 MRECURSION(TC_INST_NUM, _TC_INTERRUPT_HANDLER, TC_INST_MAX_ID)
sahilmgandhi 18:6a4db94011d3 137 #endif
sahilmgandhi 18:6a4db94011d3 138
sahilmgandhi 18:6a4db94011d3 139
sahilmgandhi 18:6a4db94011d3 140 /**
sahilmgandhi 18:6a4db94011d3 141 * \internal Interrupt Handler for TC module
sahilmgandhi 18:6a4db94011d3 142 *
sahilmgandhi 18:6a4db94011d3 143 * Handles interrupts as they occur, it will run the callback functions
sahilmgandhi 18:6a4db94011d3 144 * that are registered and enabled.
sahilmgandhi 18:6a4db94011d3 145 *
sahilmgandhi 18:6a4db94011d3 146 * \param[in] instance ID of the TC instance calling the interrupt
sahilmgandhi 18:6a4db94011d3 147 * handler.
sahilmgandhi 18:6a4db94011d3 148 */
sahilmgandhi 18:6a4db94011d3 149 void _tc_interrupt_handler(
sahilmgandhi 18:6a4db94011d3 150 uint8_t instance)
sahilmgandhi 18:6a4db94011d3 151 {
sahilmgandhi 18:6a4db94011d3 152 /* Temporary variable */
sahilmgandhi 18:6a4db94011d3 153 uint8_t interrupt_and_callback_status_mask;
sahilmgandhi 18:6a4db94011d3 154
sahilmgandhi 18:6a4db94011d3 155 /* Get device instance from the look-up table */
sahilmgandhi 18:6a4db94011d3 156 struct tc_module *module
sahilmgandhi 18:6a4db94011d3 157 = (struct tc_module *)_tc_instances[instance];
sahilmgandhi 18:6a4db94011d3 158
sahilmgandhi 18:6a4db94011d3 159 /* Read and mask interrupt flag register */
sahilmgandhi 18:6a4db94011d3 160 interrupt_and_callback_status_mask = module->hw->COUNT8.INTFLAG.reg &
sahilmgandhi 18:6a4db94011d3 161 module->register_callback_mask &
sahilmgandhi 18:6a4db94011d3 162 module->enable_callback_mask;
sahilmgandhi 18:6a4db94011d3 163
sahilmgandhi 18:6a4db94011d3 164 /* Check if an Overflow interrupt has occurred */
sahilmgandhi 18:6a4db94011d3 165 if (interrupt_and_callback_status_mask & TC_INTFLAG_OVF) {
sahilmgandhi 18:6a4db94011d3 166 /* Invoke registered and enabled callback function */
sahilmgandhi 18:6a4db94011d3 167 (module->callback[TC_CALLBACK_OVERFLOW])(module);
sahilmgandhi 18:6a4db94011d3 168 /* Clear interrupt flag */
sahilmgandhi 18:6a4db94011d3 169 module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_OVF;
sahilmgandhi 18:6a4db94011d3 170 }
sahilmgandhi 18:6a4db94011d3 171
sahilmgandhi 18:6a4db94011d3 172 /* Check if an Error interrupt has occurred */
sahilmgandhi 18:6a4db94011d3 173 if (interrupt_and_callback_status_mask & TC_INTFLAG_ERR) {
sahilmgandhi 18:6a4db94011d3 174 /* Invoke registered and enabled callback function */
sahilmgandhi 18:6a4db94011d3 175 (module->callback[TC_CALLBACK_ERROR])(module);
sahilmgandhi 18:6a4db94011d3 176 /* Clear interrupt flag */
sahilmgandhi 18:6a4db94011d3 177 module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_ERR;
sahilmgandhi 18:6a4db94011d3 178 }
sahilmgandhi 18:6a4db94011d3 179
sahilmgandhi 18:6a4db94011d3 180 /* Check if an Match/Capture Channel 0 interrupt has occurred */
sahilmgandhi 18:6a4db94011d3 181 if (interrupt_and_callback_status_mask & TC_INTFLAG_MC(1)) {
sahilmgandhi 18:6a4db94011d3 182 /* Invoke registered and enabled callback function */
sahilmgandhi 18:6a4db94011d3 183 (module->callback[TC_CALLBACK_CC_CHANNEL0])(module);
sahilmgandhi 18:6a4db94011d3 184 /* Clear interrupt flag */
sahilmgandhi 18:6a4db94011d3 185 module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_MC(1);
sahilmgandhi 18:6a4db94011d3 186 }
sahilmgandhi 18:6a4db94011d3 187
sahilmgandhi 18:6a4db94011d3 188 /* Check if an Match/Capture Channel 1 interrupt has occurred */
sahilmgandhi 18:6a4db94011d3 189 if (interrupt_and_callback_status_mask & TC_INTFLAG_MC(2)) {
sahilmgandhi 18:6a4db94011d3 190 /* Invoke registered and enabled callback function */
sahilmgandhi 18:6a4db94011d3 191 (module->callback[TC_CALLBACK_CC_CHANNEL1])(module);
sahilmgandhi 18:6a4db94011d3 192 /* Clear interrupt flag */
sahilmgandhi 18:6a4db94011d3 193 module->hw->COUNT8.INTFLAG.reg = TC_INTFLAG_MC(2);
sahilmgandhi 18:6a4db94011d3 194 }
sahilmgandhi 18:6a4db94011d3 195 }