Mouse code for the MacroRat
mbed-dev/targets/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/extint/extint_callback.c@46:b156ef445742, 2017-06-03 (annotated)
- 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?
User | Revision | Line number | New contents of line |
---|---|---|---|
sahilmgandhi | 18:6a4db94011d3 | 1 | /** |
sahilmgandhi | 18:6a4db94011d3 | 2 | * \file |
sahilmgandhi | 18:6a4db94011d3 | 3 | * |
sahilmgandhi | 18:6a4db94011d3 | 4 | * \brief SAM External Interrupt Driver |
sahilmgandhi | 18:6a4db94011d3 | 5 | * |
sahilmgandhi | 18:6a4db94011d3 | 6 | * Copyright (C) 2012-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 | * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> |
sahilmgandhi | 18:6a4db94011d3 | 45 | */ |
sahilmgandhi | 18:6a4db94011d3 | 46 | #include "extint.h" |
sahilmgandhi | 18:6a4db94011d3 | 47 | #include "extint_callback.h" |
sahilmgandhi | 18:6a4db94011d3 | 48 | |
sahilmgandhi | 18:6a4db94011d3 | 49 | /** |
sahilmgandhi | 18:6a4db94011d3 | 50 | * \internal |
sahilmgandhi | 18:6a4db94011d3 | 51 | * Internal driver device instance struct, declared in the main module driver. |
sahilmgandhi | 18:6a4db94011d3 | 52 | */ |
sahilmgandhi | 18:6a4db94011d3 | 53 | extern struct _extint_module _extint_dev; |
sahilmgandhi | 18:6a4db94011d3 | 54 | |
sahilmgandhi | 18:6a4db94011d3 | 55 | /** |
sahilmgandhi | 18:6a4db94011d3 | 56 | * \internal |
sahilmgandhi | 18:6a4db94011d3 | 57 | * This is the number of the channel whose callback is currently running. |
sahilmgandhi | 18:6a4db94011d3 | 58 | */ |
sahilmgandhi | 18:6a4db94011d3 | 59 | uint8_t _current_channel; |
sahilmgandhi | 18:6a4db94011d3 | 60 | |
sahilmgandhi | 18:6a4db94011d3 | 61 | /** |
sahilmgandhi | 18:6a4db94011d3 | 62 | * \brief Registers an asynchronous callback function with the driver. |
sahilmgandhi | 18:6a4db94011d3 | 63 | * |
sahilmgandhi | 18:6a4db94011d3 | 64 | * Registers an asynchronous callback with the EXTINT driver, fired when a |
sahilmgandhi | 18:6a4db94011d3 | 65 | * channel detects the configured channel detection criteria |
sahilmgandhi | 18:6a4db94011d3 | 66 | * (e.g. edge or level). Callbacks are fired once for each detected channel. |
sahilmgandhi | 18:6a4db94011d3 | 67 | * |
sahilmgandhi | 18:6a4db94011d3 | 68 | * \note NMI channel callbacks cannot be registered via this function; the |
sahilmgandhi | 18:6a4db94011d3 | 69 | * device's NMI interrupt should be hooked directly in the user |
sahilmgandhi | 18:6a4db94011d3 | 70 | * application and the NMI flags manually cleared via |
sahilmgandhi | 18:6a4db94011d3 | 71 | * \ref extint_nmi_clear_detected(). |
sahilmgandhi | 18:6a4db94011d3 | 72 | * |
sahilmgandhi | 18:6a4db94011d3 | 73 | * \param[in] callback Pointer to the callback function to register |
sahilmgandhi | 18:6a4db94011d3 | 74 | * \param[in] channel Logical channel to register callback for |
sahilmgandhi | 18:6a4db94011d3 | 75 | * \param[in] type Type of callback function to register |
sahilmgandhi | 18:6a4db94011d3 | 76 | * |
sahilmgandhi | 18:6a4db94011d3 | 77 | * \return Status of the registration operation. |
sahilmgandhi | 18:6a4db94011d3 | 78 | * \retval STATUS_OK The callback was registered successfully |
sahilmgandhi | 18:6a4db94011d3 | 79 | * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied |
sahilmgandhi | 18:6a4db94011d3 | 80 | * \retval STATUS_ERR_ALREADY_INITIALIZED Callback function has been |
sahilmgandhi | 18:6a4db94011d3 | 81 | * registered, need unregister first |
sahilmgandhi | 18:6a4db94011d3 | 82 | */ |
sahilmgandhi | 18:6a4db94011d3 | 83 | enum status_code extint_register_callback( |
sahilmgandhi | 18:6a4db94011d3 | 84 | const extint_callback_t callback, |
sahilmgandhi | 18:6a4db94011d3 | 85 | const uint8_t channel, |
sahilmgandhi | 18:6a4db94011d3 | 86 | const enum extint_callback_type type) |
sahilmgandhi | 18:6a4db94011d3 | 87 | { |
sahilmgandhi | 18:6a4db94011d3 | 88 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 89 | Assert(callback); |
sahilmgandhi | 18:6a4db94011d3 | 90 | |
sahilmgandhi | 18:6a4db94011d3 | 91 | if (type != EXTINT_CALLBACK_TYPE_DETECT) { |
sahilmgandhi | 18:6a4db94011d3 | 92 | Assert(false); |
sahilmgandhi | 18:6a4db94011d3 | 93 | return STATUS_ERR_INVALID_ARG; |
sahilmgandhi | 18:6a4db94011d3 | 94 | } |
sahilmgandhi | 18:6a4db94011d3 | 95 | |
sahilmgandhi | 18:6a4db94011d3 | 96 | if (_extint_dev.callbacks[channel] == NULL) { |
sahilmgandhi | 18:6a4db94011d3 | 97 | _extint_dev.callbacks[channel] = callback; |
sahilmgandhi | 18:6a4db94011d3 | 98 | return STATUS_OK; |
sahilmgandhi | 18:6a4db94011d3 | 99 | } else if (_extint_dev.callbacks[channel] == callback) { |
sahilmgandhi | 18:6a4db94011d3 | 100 | return STATUS_OK; |
sahilmgandhi | 18:6a4db94011d3 | 101 | } |
sahilmgandhi | 18:6a4db94011d3 | 102 | |
sahilmgandhi | 18:6a4db94011d3 | 103 | return STATUS_ERR_ALREADY_INITIALIZED; |
sahilmgandhi | 18:6a4db94011d3 | 104 | } |
sahilmgandhi | 18:6a4db94011d3 | 105 | |
sahilmgandhi | 18:6a4db94011d3 | 106 | /** |
sahilmgandhi | 18:6a4db94011d3 | 107 | * \brief Unregisters an asynchronous callback function with the driver. |
sahilmgandhi | 18:6a4db94011d3 | 108 | * |
sahilmgandhi | 18:6a4db94011d3 | 109 | * Unregisters an asynchronous callback with the EXTINT driver, removing it |
sahilmgandhi | 18:6a4db94011d3 | 110 | * from the internal callback registration table. |
sahilmgandhi | 18:6a4db94011d3 | 111 | * |
sahilmgandhi | 18:6a4db94011d3 | 112 | * \param[in] callback Pointer to the callback function to unregister |
sahilmgandhi | 18:6a4db94011d3 | 113 | * \param[in] channel Logical channel to unregister callback for |
sahilmgandhi | 18:6a4db94011d3 | 114 | * \param[in] type Type of callback function to unregister |
sahilmgandhi | 18:6a4db94011d3 | 115 | * |
sahilmgandhi | 18:6a4db94011d3 | 116 | * \return Status of the de-registration operation. |
sahilmgandhi | 18:6a4db94011d3 | 117 | * \retval STATUS_OK The callback was Unregistered successfully |
sahilmgandhi | 18:6a4db94011d3 | 118 | * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied |
sahilmgandhi | 18:6a4db94011d3 | 119 | * \retval STATUS_ERR_BAD_ADDRESS No matching entry was found in the |
sahilmgandhi | 18:6a4db94011d3 | 120 | * registration table |
sahilmgandhi | 18:6a4db94011d3 | 121 | */ |
sahilmgandhi | 18:6a4db94011d3 | 122 | enum status_code extint_unregister_callback( |
sahilmgandhi | 18:6a4db94011d3 | 123 | const extint_callback_t callback, |
sahilmgandhi | 18:6a4db94011d3 | 124 | const uint8_t channel, |
sahilmgandhi | 18:6a4db94011d3 | 125 | const enum extint_callback_type type) |
sahilmgandhi | 18:6a4db94011d3 | 126 | { |
sahilmgandhi | 18:6a4db94011d3 | 127 | /* Sanity check arguments */ |
sahilmgandhi | 18:6a4db94011d3 | 128 | Assert(callback); |
sahilmgandhi | 18:6a4db94011d3 | 129 | |
sahilmgandhi | 18:6a4db94011d3 | 130 | if (type != EXTINT_CALLBACK_TYPE_DETECT) { |
sahilmgandhi | 18:6a4db94011d3 | 131 | Assert(false); |
sahilmgandhi | 18:6a4db94011d3 | 132 | return STATUS_ERR_INVALID_ARG; |
sahilmgandhi | 18:6a4db94011d3 | 133 | } |
sahilmgandhi | 18:6a4db94011d3 | 134 | |
sahilmgandhi | 18:6a4db94011d3 | 135 | if (_extint_dev.callbacks[channel] == callback) { |
sahilmgandhi | 18:6a4db94011d3 | 136 | _extint_dev.callbacks[channel] = NULL; |
sahilmgandhi | 18:6a4db94011d3 | 137 | return STATUS_OK; |
sahilmgandhi | 18:6a4db94011d3 | 138 | } |
sahilmgandhi | 18:6a4db94011d3 | 139 | |
sahilmgandhi | 18:6a4db94011d3 | 140 | return STATUS_ERR_BAD_ADDRESS; |
sahilmgandhi | 18:6a4db94011d3 | 141 | } |
sahilmgandhi | 18:6a4db94011d3 | 142 | |
sahilmgandhi | 18:6a4db94011d3 | 143 | /** |
sahilmgandhi | 18:6a4db94011d3 | 144 | * \brief Enables asynchronous callback generation for a given channel and type. |
sahilmgandhi | 18:6a4db94011d3 | 145 | * |
sahilmgandhi | 18:6a4db94011d3 | 146 | * Enables asynchronous callbacks for a given logical external interrupt channel |
sahilmgandhi | 18:6a4db94011d3 | 147 | * and type. This must be called before an external interrupt channel will |
sahilmgandhi | 18:6a4db94011d3 | 148 | * generate callback events. |
sahilmgandhi | 18:6a4db94011d3 | 149 | * |
sahilmgandhi | 18:6a4db94011d3 | 150 | * \param[in] channel Logical channel to enable callback generation for |
sahilmgandhi | 18:6a4db94011d3 | 151 | * \param[in] type Type of callback function callbacks to enable |
sahilmgandhi | 18:6a4db94011d3 | 152 | * |
sahilmgandhi | 18:6a4db94011d3 | 153 | * \return Status of the callback enable operation. |
sahilmgandhi | 18:6a4db94011d3 | 154 | * \retval STATUS_OK The callback was enabled successfully |
sahilmgandhi | 18:6a4db94011d3 | 155 | * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied |
sahilmgandhi | 18:6a4db94011d3 | 156 | */ |
sahilmgandhi | 18:6a4db94011d3 | 157 | enum status_code extint_chan_enable_callback( |
sahilmgandhi | 18:6a4db94011d3 | 158 | const uint8_t channel, |
sahilmgandhi | 18:6a4db94011d3 | 159 | const enum extint_callback_type type) |
sahilmgandhi | 18:6a4db94011d3 | 160 | { |
sahilmgandhi | 18:6a4db94011d3 | 161 | if (type == EXTINT_CALLBACK_TYPE_DETECT) { |
sahilmgandhi | 18:6a4db94011d3 | 162 | Eic *const eic = _extint_get_eic_from_channel(channel); |
sahilmgandhi | 18:6a4db94011d3 | 163 | |
sahilmgandhi | 18:6a4db94011d3 | 164 | eic->INTENSET.reg = (1UL << channel); |
sahilmgandhi | 18:6a4db94011d3 | 165 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 166 | Assert(false); |
sahilmgandhi | 18:6a4db94011d3 | 167 | return STATUS_ERR_INVALID_ARG; |
sahilmgandhi | 18:6a4db94011d3 | 168 | } |
sahilmgandhi | 18:6a4db94011d3 | 169 | |
sahilmgandhi | 18:6a4db94011d3 | 170 | return STATUS_OK; |
sahilmgandhi | 18:6a4db94011d3 | 171 | } |
sahilmgandhi | 18:6a4db94011d3 | 172 | |
sahilmgandhi | 18:6a4db94011d3 | 173 | /** |
sahilmgandhi | 18:6a4db94011d3 | 174 | * \brief Disables asynchronous callback generation for a given channel and type. |
sahilmgandhi | 18:6a4db94011d3 | 175 | * |
sahilmgandhi | 18:6a4db94011d3 | 176 | * Disables asynchronous callbacks for a given logical external interrupt |
sahilmgandhi | 18:6a4db94011d3 | 177 | * channel and type. |
sahilmgandhi | 18:6a4db94011d3 | 178 | * |
sahilmgandhi | 18:6a4db94011d3 | 179 | * \param[in] channel Logical channel to disable callback generation for |
sahilmgandhi | 18:6a4db94011d3 | 180 | * \param[in] type Type of callback function callbacks to disable |
sahilmgandhi | 18:6a4db94011d3 | 181 | * |
sahilmgandhi | 18:6a4db94011d3 | 182 | * \return Status of the callback disable operation. |
sahilmgandhi | 18:6a4db94011d3 | 183 | * \retval STATUS_OK The callback was disabled successfully |
sahilmgandhi | 18:6a4db94011d3 | 184 | * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied |
sahilmgandhi | 18:6a4db94011d3 | 185 | */ |
sahilmgandhi | 18:6a4db94011d3 | 186 | enum status_code extint_chan_disable_callback( |
sahilmgandhi | 18:6a4db94011d3 | 187 | const uint8_t channel, |
sahilmgandhi | 18:6a4db94011d3 | 188 | const enum extint_callback_type type) |
sahilmgandhi | 18:6a4db94011d3 | 189 | { |
sahilmgandhi | 18:6a4db94011d3 | 190 | if (type == EXTINT_CALLBACK_TYPE_DETECT) { |
sahilmgandhi | 18:6a4db94011d3 | 191 | Eic *const eic = _extint_get_eic_from_channel(channel); |
sahilmgandhi | 18:6a4db94011d3 | 192 | |
sahilmgandhi | 18:6a4db94011d3 | 193 | eic->INTENCLR.reg = (1UL << channel); |
sahilmgandhi | 18:6a4db94011d3 | 194 | } else { |
sahilmgandhi | 18:6a4db94011d3 | 195 | Assert(false); |
sahilmgandhi | 18:6a4db94011d3 | 196 | return STATUS_ERR_INVALID_ARG; |
sahilmgandhi | 18:6a4db94011d3 | 197 | } |
sahilmgandhi | 18:6a4db94011d3 | 198 | |
sahilmgandhi | 18:6a4db94011d3 | 199 | return STATUS_OK; |
sahilmgandhi | 18:6a4db94011d3 | 200 | } |
sahilmgandhi | 18:6a4db94011d3 | 201 | |
sahilmgandhi | 18:6a4db94011d3 | 202 | /** |
sahilmgandhi | 18:6a4db94011d3 | 203 | * \brief Find what channel caused the callback. |
sahilmgandhi | 18:6a4db94011d3 | 204 | * |
sahilmgandhi | 18:6a4db94011d3 | 205 | * Can be used in an EXTINT callback function to find what channel caused |
sahilmgandhi | 18:6a4db94011d3 | 206 | * the callback in case same callback is used by multiple channels. |
sahilmgandhi | 18:6a4db94011d3 | 207 | * |
sahilmgandhi | 18:6a4db94011d3 | 208 | * \return Channel number. |
sahilmgandhi | 18:6a4db94011d3 | 209 | */ |
sahilmgandhi | 18:6a4db94011d3 | 210 | uint8_t extint_get_current_channel(void) |
sahilmgandhi | 18:6a4db94011d3 | 211 | { |
sahilmgandhi | 18:6a4db94011d3 | 212 | return _current_channel; |
sahilmgandhi | 18:6a4db94011d3 | 213 | } |
sahilmgandhi | 18:6a4db94011d3 | 214 | |
sahilmgandhi | 18:6a4db94011d3 | 215 | /** Handler for the EXTINT hardware module interrupt. */ |
sahilmgandhi | 18:6a4db94011d3 | 216 | void EIC_Handler(void) |
sahilmgandhi | 18:6a4db94011d3 | 217 | { |
sahilmgandhi | 18:6a4db94011d3 | 218 | /* Find any triggered channels, run associated callback handlers */ |
sahilmgandhi | 18:6a4db94011d3 | 219 | for (_current_channel = 0; _current_channel < EIC_NUMBER_OF_INTERRUPTS ; _current_channel++) { |
sahilmgandhi | 18:6a4db94011d3 | 220 | if (extint_chan_is_detected(_current_channel)) { |
sahilmgandhi | 18:6a4db94011d3 | 221 | /* Clear flag */ |
sahilmgandhi | 18:6a4db94011d3 | 222 | extint_chan_clear_detected(_current_channel); |
sahilmgandhi | 18:6a4db94011d3 | 223 | /* Find any associated callback entries in the callback table */ |
sahilmgandhi | 18:6a4db94011d3 | 224 | if (_extint_dev.callbacks[_current_channel] != NULL) { |
sahilmgandhi | 18:6a4db94011d3 | 225 | /* Run the registered callback */ |
sahilmgandhi | 18:6a4db94011d3 | 226 | _extint_dev.callbacks[_current_channel](); |
sahilmgandhi | 18:6a4db94011d3 | 227 | } |
sahilmgandhi | 18:6a4db94011d3 | 228 | } |
sahilmgandhi | 18:6a4db94011d3 | 229 | } |
sahilmgandhi | 18:6a4db94011d3 | 230 | } |