Mouse code for the MacroRat

Dependencies:   ITG3200 QEI

Committer:
sahilmgandhi
Date:
Sat Jun 03 00:22:44 2017 +0000
Revision:
46:b156ef445742
Parent:
18:6a4db94011d3
Final code for internal battlebot competition.

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