mbed library sources. Supersedes mbed-src.
Fork of mbed-dev by
targets/hal/TARGET_Atmel/TARGET_SAM_CortexM0P/drivers/extint/extint_callback.c@15:a81a8d6c1dfe, 2015-11-04 (annotated)
- Committer:
- mbed_official
- Date:
- Wed Nov 04 16:30:11 2015 +0000
- Revision:
- 15:a81a8d6c1dfe
Synchronized with git revision 46af745ef4405614c3fa49abbd9a706a362ea514
Full URL: https://github.com/mbedmicro/mbed/commit/46af745ef4405614c3fa49abbd9a706a362ea514/
Renamed TARGET_SAM_CortexM0+ to TARGET_SAM_CortexM0P for compatiblity with online compiler
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mbed_official | 15:a81a8d6c1dfe | 1 | /** |
mbed_official | 15:a81a8d6c1dfe | 2 | * \file |
mbed_official | 15:a81a8d6c1dfe | 3 | * |
mbed_official | 15:a81a8d6c1dfe | 4 | * \brief SAM External Interrupt Driver |
mbed_official | 15:a81a8d6c1dfe | 5 | * |
mbed_official | 15:a81a8d6c1dfe | 6 | * Copyright (C) 2012-2015 Atmel Corporation. All rights reserved. |
mbed_official | 15:a81a8d6c1dfe | 7 | * |
mbed_official | 15:a81a8d6c1dfe | 8 | * \asf_license_start |
mbed_official | 15:a81a8d6c1dfe | 9 | * |
mbed_official | 15:a81a8d6c1dfe | 10 | * \page License |
mbed_official | 15:a81a8d6c1dfe | 11 | * |
mbed_official | 15:a81a8d6c1dfe | 12 | * Redistribution and use in source and binary forms, with or without |
mbed_official | 15:a81a8d6c1dfe | 13 | * modification, are permitted provided that the following conditions are met: |
mbed_official | 15:a81a8d6c1dfe | 14 | * |
mbed_official | 15:a81a8d6c1dfe | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
mbed_official | 15:a81a8d6c1dfe | 16 | * this list of conditions and the following disclaimer. |
mbed_official | 15:a81a8d6c1dfe | 17 | * |
mbed_official | 15:a81a8d6c1dfe | 18 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mbed_official | 15:a81a8d6c1dfe | 19 | * this list of conditions and the following disclaimer in the documentation |
mbed_official | 15:a81a8d6c1dfe | 20 | * and/or other materials provided with the distribution. |
mbed_official | 15:a81a8d6c1dfe | 21 | * |
mbed_official | 15:a81a8d6c1dfe | 22 | * 3. The name of Atmel may not be used to endorse or promote products derived |
mbed_official | 15:a81a8d6c1dfe | 23 | * from this software without specific prior written permission. |
mbed_official | 15:a81a8d6c1dfe | 24 | * |
mbed_official | 15:a81a8d6c1dfe | 25 | * 4. This software may only be redistributed and used in connection with an |
mbed_official | 15:a81a8d6c1dfe | 26 | * Atmel microcontroller product. |
mbed_official | 15:a81a8d6c1dfe | 27 | * |
mbed_official | 15:a81a8d6c1dfe | 28 | * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED |
mbed_official | 15:a81a8d6c1dfe | 29 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF |
mbed_official | 15:a81a8d6c1dfe | 30 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE |
mbed_official | 15:a81a8d6c1dfe | 31 | * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR |
mbed_official | 15:a81a8d6c1dfe | 32 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mbed_official | 15:a81a8d6c1dfe | 33 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
mbed_official | 15:a81a8d6c1dfe | 34 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
mbed_official | 15:a81a8d6c1dfe | 35 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
mbed_official | 15:a81a8d6c1dfe | 36 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
mbed_official | 15:a81a8d6c1dfe | 37 | * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
mbed_official | 15:a81a8d6c1dfe | 38 | * POSSIBILITY OF SUCH DAMAGE. |
mbed_official | 15:a81a8d6c1dfe | 39 | * |
mbed_official | 15:a81a8d6c1dfe | 40 | * \asf_license_stop |
mbed_official | 15:a81a8d6c1dfe | 41 | * |
mbed_official | 15:a81a8d6c1dfe | 42 | */ |
mbed_official | 15:a81a8d6c1dfe | 43 | /* |
mbed_official | 15:a81a8d6c1dfe | 44 | * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a> |
mbed_official | 15:a81a8d6c1dfe | 45 | */ |
mbed_official | 15:a81a8d6c1dfe | 46 | #include "extint.h" |
mbed_official | 15:a81a8d6c1dfe | 47 | #include "extint_callback.h" |
mbed_official | 15:a81a8d6c1dfe | 48 | |
mbed_official | 15:a81a8d6c1dfe | 49 | /** |
mbed_official | 15:a81a8d6c1dfe | 50 | * \internal |
mbed_official | 15:a81a8d6c1dfe | 51 | * Internal driver device instance struct, declared in the main module driver. |
mbed_official | 15:a81a8d6c1dfe | 52 | */ |
mbed_official | 15:a81a8d6c1dfe | 53 | extern struct _extint_module _extint_dev; |
mbed_official | 15:a81a8d6c1dfe | 54 | |
mbed_official | 15:a81a8d6c1dfe | 55 | /** |
mbed_official | 15:a81a8d6c1dfe | 56 | * \internal |
mbed_official | 15:a81a8d6c1dfe | 57 | * This is the number of the channel whose callback is currently running. |
mbed_official | 15:a81a8d6c1dfe | 58 | */ |
mbed_official | 15:a81a8d6c1dfe | 59 | uint8_t _current_channel; |
mbed_official | 15:a81a8d6c1dfe | 60 | |
mbed_official | 15:a81a8d6c1dfe | 61 | /** |
mbed_official | 15:a81a8d6c1dfe | 62 | * \brief Registers an asynchronous callback function with the driver. |
mbed_official | 15:a81a8d6c1dfe | 63 | * |
mbed_official | 15:a81a8d6c1dfe | 64 | * Registers an asynchronous callback with the EXTINT driver, fired when a |
mbed_official | 15:a81a8d6c1dfe | 65 | * channel detects the configured channel detection criteria |
mbed_official | 15:a81a8d6c1dfe | 66 | * (e.g. edge or level). Callbacks are fired once for each detected channel. |
mbed_official | 15:a81a8d6c1dfe | 67 | * |
mbed_official | 15:a81a8d6c1dfe | 68 | * \note NMI channel callbacks cannot be registered via this function; the |
mbed_official | 15:a81a8d6c1dfe | 69 | * device's NMI interrupt should be hooked directly in the user |
mbed_official | 15:a81a8d6c1dfe | 70 | * application and the NMI flags manually cleared via |
mbed_official | 15:a81a8d6c1dfe | 71 | * \ref extint_nmi_clear_detected(). |
mbed_official | 15:a81a8d6c1dfe | 72 | * |
mbed_official | 15:a81a8d6c1dfe | 73 | * \param[in] callback Pointer to the callback function to register |
mbed_official | 15:a81a8d6c1dfe | 74 | * \param[in] channel Logical channel to register callback for |
mbed_official | 15:a81a8d6c1dfe | 75 | * \param[in] type Type of callback function to register |
mbed_official | 15:a81a8d6c1dfe | 76 | * |
mbed_official | 15:a81a8d6c1dfe | 77 | * \return Status of the registration operation. |
mbed_official | 15:a81a8d6c1dfe | 78 | * \retval STATUS_OK The callback was registered successfully |
mbed_official | 15:a81a8d6c1dfe | 79 | * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied |
mbed_official | 15:a81a8d6c1dfe | 80 | * \retval STATUS_ERR_ALREADY_INITIALIZED Callback function has been |
mbed_official | 15:a81a8d6c1dfe | 81 | * registered, need unregister first |
mbed_official | 15:a81a8d6c1dfe | 82 | */ |
mbed_official | 15:a81a8d6c1dfe | 83 | enum status_code extint_register_callback( |
mbed_official | 15:a81a8d6c1dfe | 84 | const extint_callback_t callback, |
mbed_official | 15:a81a8d6c1dfe | 85 | const uint8_t channel, |
mbed_official | 15:a81a8d6c1dfe | 86 | const enum extint_callback_type type) |
mbed_official | 15:a81a8d6c1dfe | 87 | { |
mbed_official | 15:a81a8d6c1dfe | 88 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 89 | Assert(callback); |
mbed_official | 15:a81a8d6c1dfe | 90 | |
mbed_official | 15:a81a8d6c1dfe | 91 | if (type != EXTINT_CALLBACK_TYPE_DETECT) { |
mbed_official | 15:a81a8d6c1dfe | 92 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 93 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 94 | } |
mbed_official | 15:a81a8d6c1dfe | 95 | |
mbed_official | 15:a81a8d6c1dfe | 96 | if (_extint_dev.callbacks[channel] == NULL) { |
mbed_official | 15:a81a8d6c1dfe | 97 | _extint_dev.callbacks[channel] = callback; |
mbed_official | 15:a81a8d6c1dfe | 98 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 99 | } else if (_extint_dev.callbacks[channel] == callback) { |
mbed_official | 15:a81a8d6c1dfe | 100 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 101 | } |
mbed_official | 15:a81a8d6c1dfe | 102 | |
mbed_official | 15:a81a8d6c1dfe | 103 | return STATUS_ERR_ALREADY_INITIALIZED; |
mbed_official | 15:a81a8d6c1dfe | 104 | } |
mbed_official | 15:a81a8d6c1dfe | 105 | |
mbed_official | 15:a81a8d6c1dfe | 106 | /** |
mbed_official | 15:a81a8d6c1dfe | 107 | * \brief Unregisters an asynchronous callback function with the driver. |
mbed_official | 15:a81a8d6c1dfe | 108 | * |
mbed_official | 15:a81a8d6c1dfe | 109 | * Unregisters an asynchronous callback with the EXTINT driver, removing it |
mbed_official | 15:a81a8d6c1dfe | 110 | * from the internal callback registration table. |
mbed_official | 15:a81a8d6c1dfe | 111 | * |
mbed_official | 15:a81a8d6c1dfe | 112 | * \param[in] callback Pointer to the callback function to unregister |
mbed_official | 15:a81a8d6c1dfe | 113 | * \param[in] channel Logical channel to unregister callback for |
mbed_official | 15:a81a8d6c1dfe | 114 | * \param[in] type Type of callback function to unregister |
mbed_official | 15:a81a8d6c1dfe | 115 | * |
mbed_official | 15:a81a8d6c1dfe | 116 | * \return Status of the de-registration operation. |
mbed_official | 15:a81a8d6c1dfe | 117 | * \retval STATUS_OK The callback was Unregistered successfully |
mbed_official | 15:a81a8d6c1dfe | 118 | * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied |
mbed_official | 15:a81a8d6c1dfe | 119 | * \retval STATUS_ERR_BAD_ADDRESS No matching entry was found in the |
mbed_official | 15:a81a8d6c1dfe | 120 | * registration table |
mbed_official | 15:a81a8d6c1dfe | 121 | */ |
mbed_official | 15:a81a8d6c1dfe | 122 | enum status_code extint_unregister_callback( |
mbed_official | 15:a81a8d6c1dfe | 123 | const extint_callback_t callback, |
mbed_official | 15:a81a8d6c1dfe | 124 | const uint8_t channel, |
mbed_official | 15:a81a8d6c1dfe | 125 | const enum extint_callback_type type) |
mbed_official | 15:a81a8d6c1dfe | 126 | { |
mbed_official | 15:a81a8d6c1dfe | 127 | /* Sanity check arguments */ |
mbed_official | 15:a81a8d6c1dfe | 128 | Assert(callback); |
mbed_official | 15:a81a8d6c1dfe | 129 | |
mbed_official | 15:a81a8d6c1dfe | 130 | if (type != EXTINT_CALLBACK_TYPE_DETECT) { |
mbed_official | 15:a81a8d6c1dfe | 131 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 132 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 133 | } |
mbed_official | 15:a81a8d6c1dfe | 134 | |
mbed_official | 15:a81a8d6c1dfe | 135 | if (_extint_dev.callbacks[channel] == callback) { |
mbed_official | 15:a81a8d6c1dfe | 136 | _extint_dev.callbacks[channel] = NULL; |
mbed_official | 15:a81a8d6c1dfe | 137 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 138 | } |
mbed_official | 15:a81a8d6c1dfe | 139 | |
mbed_official | 15:a81a8d6c1dfe | 140 | return STATUS_ERR_BAD_ADDRESS; |
mbed_official | 15:a81a8d6c1dfe | 141 | } |
mbed_official | 15:a81a8d6c1dfe | 142 | |
mbed_official | 15:a81a8d6c1dfe | 143 | /** |
mbed_official | 15:a81a8d6c1dfe | 144 | * \brief Enables asynchronous callback generation for a given channel and type. |
mbed_official | 15:a81a8d6c1dfe | 145 | * |
mbed_official | 15:a81a8d6c1dfe | 146 | * Enables asynchronous callbacks for a given logical external interrupt channel |
mbed_official | 15:a81a8d6c1dfe | 147 | * and type. This must be called before an external interrupt channel will |
mbed_official | 15:a81a8d6c1dfe | 148 | * generate callback events. |
mbed_official | 15:a81a8d6c1dfe | 149 | * |
mbed_official | 15:a81a8d6c1dfe | 150 | * \param[in] channel Logical channel to enable callback generation for |
mbed_official | 15:a81a8d6c1dfe | 151 | * \param[in] type Type of callback function callbacks to enable |
mbed_official | 15:a81a8d6c1dfe | 152 | * |
mbed_official | 15:a81a8d6c1dfe | 153 | * \return Status of the callback enable operation. |
mbed_official | 15:a81a8d6c1dfe | 154 | * \retval STATUS_OK The callback was enabled successfully |
mbed_official | 15:a81a8d6c1dfe | 155 | * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied |
mbed_official | 15:a81a8d6c1dfe | 156 | */ |
mbed_official | 15:a81a8d6c1dfe | 157 | enum status_code extint_chan_enable_callback( |
mbed_official | 15:a81a8d6c1dfe | 158 | const uint8_t channel, |
mbed_official | 15:a81a8d6c1dfe | 159 | const enum extint_callback_type type) |
mbed_official | 15:a81a8d6c1dfe | 160 | { |
mbed_official | 15:a81a8d6c1dfe | 161 | if (type == EXTINT_CALLBACK_TYPE_DETECT) { |
mbed_official | 15:a81a8d6c1dfe | 162 | Eic *const eic = _extint_get_eic_from_channel(channel); |
mbed_official | 15:a81a8d6c1dfe | 163 | |
mbed_official | 15:a81a8d6c1dfe | 164 | eic->INTENSET.reg = (1UL << channel); |
mbed_official | 15:a81a8d6c1dfe | 165 | } else { |
mbed_official | 15:a81a8d6c1dfe | 166 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 167 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 168 | } |
mbed_official | 15:a81a8d6c1dfe | 169 | |
mbed_official | 15:a81a8d6c1dfe | 170 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 171 | } |
mbed_official | 15:a81a8d6c1dfe | 172 | |
mbed_official | 15:a81a8d6c1dfe | 173 | /** |
mbed_official | 15:a81a8d6c1dfe | 174 | * \brief Disables asynchronous callback generation for a given channel and type. |
mbed_official | 15:a81a8d6c1dfe | 175 | * |
mbed_official | 15:a81a8d6c1dfe | 176 | * Disables asynchronous callbacks for a given logical external interrupt |
mbed_official | 15:a81a8d6c1dfe | 177 | * channel and type. |
mbed_official | 15:a81a8d6c1dfe | 178 | * |
mbed_official | 15:a81a8d6c1dfe | 179 | * \param[in] channel Logical channel to disable callback generation for |
mbed_official | 15:a81a8d6c1dfe | 180 | * \param[in] type Type of callback function callbacks to disable |
mbed_official | 15:a81a8d6c1dfe | 181 | * |
mbed_official | 15:a81a8d6c1dfe | 182 | * \return Status of the callback disable operation. |
mbed_official | 15:a81a8d6c1dfe | 183 | * \retval STATUS_OK The callback was disabled successfully |
mbed_official | 15:a81a8d6c1dfe | 184 | * \retval STATUS_ERR_INVALID_ARG If an invalid callback type was supplied |
mbed_official | 15:a81a8d6c1dfe | 185 | */ |
mbed_official | 15:a81a8d6c1dfe | 186 | enum status_code extint_chan_disable_callback( |
mbed_official | 15:a81a8d6c1dfe | 187 | const uint8_t channel, |
mbed_official | 15:a81a8d6c1dfe | 188 | const enum extint_callback_type type) |
mbed_official | 15:a81a8d6c1dfe | 189 | { |
mbed_official | 15:a81a8d6c1dfe | 190 | if (type == EXTINT_CALLBACK_TYPE_DETECT) { |
mbed_official | 15:a81a8d6c1dfe | 191 | Eic *const eic = _extint_get_eic_from_channel(channel); |
mbed_official | 15:a81a8d6c1dfe | 192 | |
mbed_official | 15:a81a8d6c1dfe | 193 | eic->INTENCLR.reg = (1UL << channel); |
mbed_official | 15:a81a8d6c1dfe | 194 | } else { |
mbed_official | 15:a81a8d6c1dfe | 195 | Assert(false); |
mbed_official | 15:a81a8d6c1dfe | 196 | return STATUS_ERR_INVALID_ARG; |
mbed_official | 15:a81a8d6c1dfe | 197 | } |
mbed_official | 15:a81a8d6c1dfe | 198 | |
mbed_official | 15:a81a8d6c1dfe | 199 | return STATUS_OK; |
mbed_official | 15:a81a8d6c1dfe | 200 | } |
mbed_official | 15:a81a8d6c1dfe | 201 | |
mbed_official | 15:a81a8d6c1dfe | 202 | /** |
mbed_official | 15:a81a8d6c1dfe | 203 | * \brief Find what channel caused the callback. |
mbed_official | 15:a81a8d6c1dfe | 204 | * |
mbed_official | 15:a81a8d6c1dfe | 205 | * Can be used in an EXTINT callback function to find what channel caused |
mbed_official | 15:a81a8d6c1dfe | 206 | * the callback in case same callback is used by multiple channels. |
mbed_official | 15:a81a8d6c1dfe | 207 | * |
mbed_official | 15:a81a8d6c1dfe | 208 | * \return Channel number. |
mbed_official | 15:a81a8d6c1dfe | 209 | */ |
mbed_official | 15:a81a8d6c1dfe | 210 | uint8_t extint_get_current_channel(void) |
mbed_official | 15:a81a8d6c1dfe | 211 | { |
mbed_official | 15:a81a8d6c1dfe | 212 | return _current_channel; |
mbed_official | 15:a81a8d6c1dfe | 213 | } |
mbed_official | 15:a81a8d6c1dfe | 214 | |
mbed_official | 15:a81a8d6c1dfe | 215 | /** Handler for the EXTINT hardware module interrupt. */ |
mbed_official | 15:a81a8d6c1dfe | 216 | void EIC_Handler(void) |
mbed_official | 15:a81a8d6c1dfe | 217 | { |
mbed_official | 15:a81a8d6c1dfe | 218 | /* Find any triggered channels, run associated callback handlers */ |
mbed_official | 15:a81a8d6c1dfe | 219 | for (_current_channel = 0; _current_channel < EIC_NUMBER_OF_INTERRUPTS ; _current_channel++) { |
mbed_official | 15:a81a8d6c1dfe | 220 | if (extint_chan_is_detected(_current_channel)) { |
mbed_official | 15:a81a8d6c1dfe | 221 | /* Clear flag */ |
mbed_official | 15:a81a8d6c1dfe | 222 | extint_chan_clear_detected(_current_channel); |
mbed_official | 15:a81a8d6c1dfe | 223 | /* Find any associated callback entries in the callback table */ |
mbed_official | 15:a81a8d6c1dfe | 224 | if (_extint_dev.callbacks[_current_channel] != NULL) { |
mbed_official | 15:a81a8d6c1dfe | 225 | /* Run the registered callback */ |
mbed_official | 15:a81a8d6c1dfe | 226 | _extint_dev.callbacks[_current_channel](); |
mbed_official | 15:a81a8d6c1dfe | 227 | } |
mbed_official | 15:a81a8d6c1dfe | 228 | } |
mbed_official | 15:a81a8d6c1dfe | 229 | } |
mbed_official | 15:a81a8d6c1dfe | 230 | } |